2 * Copyright (C) 2017 Jérémie Galarneau <jeremie.galarneau@efficios.com>
4 * SPDX-License-Identifier: LGPL-2.1-only
8 #include <common/credentials.h>
9 #include <common/dynamic-array.h>
10 #include <common/error.h>
11 #include <common/mi-lttng.h>
12 #include <common/optional.h>
13 #include <common/payload-view.h>
14 #include <common/payload.h>
16 #include <lttng/action/action-internal.h>
17 #include <lttng/condition/buffer-usage.h>
18 #include <lttng/condition/condition-internal.h>
19 #include <lttng/condition/event-rule-matches-internal.h>
20 #include <lttng/condition/event-rule-matches.h>
21 #include <lttng/domain.h>
22 #include <lttng/error-query-internal.h>
23 #include <lttng/event-expr-internal.h>
24 #include <lttng/event-rule/event-rule-internal.h>
25 #include <lttng/trigger/trigger-internal.h>
28 bool lttng_trigger_validate(const struct lttng_trigger
*trigger
)
37 if (!trigger
->creds
.uid
.is_set
) {
42 valid
= lttng_condition_validate(trigger
->condition
) &&
43 lttng_action_validate(trigger
->action
);
48 struct lttng_trigger
*lttng_trigger_create(
49 struct lttng_condition
*condition
,
50 struct lttng_action
*action
)
52 struct lttng_trigger
*trigger
= NULL
;
54 if (!condition
|| !action
) {
58 trigger
= (lttng_trigger
*) zmalloc(sizeof(struct lttng_trigger
));
63 urcu_ref_init(&trigger
->ref
);
65 lttng_condition_get(condition
);
66 trigger
->condition
= condition
;
68 lttng_action_get(action
);
69 trigger
->action
= action
;
71 pthread_mutex_init(&trigger
->lock
, NULL
);
72 trigger
->registered
= false;
79 * Note: the lack of reference counting 'get' on the condition object is normal.
80 * This API was exposed as such in 2.11. The client is not expected to call
81 * lttng_condition_destroy on the returned object.
83 struct lttng_condition
*lttng_trigger_get_condition(
84 struct lttng_trigger
*trigger
)
86 return trigger
? trigger
->condition
: NULL
;
89 const struct lttng_condition
*lttng_trigger_get_const_condition(
90 const struct lttng_trigger
*trigger
)
92 return trigger
? trigger
->condition
: NULL
;
96 * Note: the lack of reference counting 'get' on the action object is normal.
97 * This API was exposed as such in 2.11. The client is not expected to call
98 * lttng_action_destroy on the returned object.
100 struct lttng_action
*lttng_trigger_get_action(
101 struct lttng_trigger
*trigger
)
103 return trigger
? trigger
->action
: NULL
;
106 const struct lttng_action
*lttng_trigger_get_const_action(
107 const struct lttng_trigger
*trigger
)
109 return trigger
? trigger
->action
: NULL
;
112 static void trigger_destroy_ref(struct urcu_ref
*ref
)
114 struct lttng_trigger
*trigger
=
115 container_of(ref
, struct lttng_trigger
, ref
);
116 struct lttng_action
*action
= lttng_trigger_get_action(trigger
);
117 struct lttng_condition
*condition
=
118 lttng_trigger_get_condition(trigger
);
120 LTTNG_ASSERT(action
);
121 LTTNG_ASSERT(condition
);
123 /* Release ownership. */
124 lttng_action_put(action
);
125 lttng_condition_put(condition
);
127 pthread_mutex_destroy(&trigger
->lock
);
133 void lttng_trigger_destroy(struct lttng_trigger
*trigger
)
135 lttng_trigger_put(trigger
);
138 ssize_t
lttng_trigger_create_from_payload(
139 struct lttng_payload_view
*src_view
,
140 struct lttng_trigger
**_trigger
)
142 ssize_t ret
, offset
= 0, condition_size
, action_size
, name_size
= 0;
143 struct lttng_condition
*condition
= NULL
;
144 struct lttng_action
*action
= NULL
;
145 const struct lttng_trigger_comm
*trigger_comm
;
146 const char *name
= NULL
;
147 struct lttng_credentials creds
= {
148 .uid
= LTTNG_OPTIONAL_INIT_UNSET
,
149 .gid
= LTTNG_OPTIONAL_INIT_UNSET
,
151 struct lttng_trigger
*trigger
= NULL
;
152 const struct lttng_payload_view trigger_comm_view
=
153 lttng_payload_view_from_view(
154 src_view
, 0, sizeof(*trigger_comm
));
156 if (!src_view
|| !_trigger
) {
161 if (!lttng_payload_view_is_valid(&trigger_comm_view
)) {
162 /* Payload not large enough to contain the header. */
167 /* lttng_trigger_comm header */
168 trigger_comm
= (typeof(trigger_comm
)) trigger_comm_view
.buffer
.data
;
170 /* Set the trigger's creds. */
171 if (trigger_comm
->uid
> (uint64_t) ((uid_t
) -1)) {
172 /* UID out of range for this platform. */
177 LTTNG_OPTIONAL_SET(&creds
.uid
, trigger_comm
->uid
);
179 offset
+= sizeof(*trigger_comm
);
181 if (trigger_comm
->name_length
!= 0) {
183 const struct lttng_payload_view name_view
=
184 lttng_payload_view_from_view(
186 trigger_comm
->name_length
);
188 if (!lttng_payload_view_is_valid(&name_view
)) {
193 name
= name_view
.buffer
.data
;
194 if (!lttng_buffer_view_contains_string(&name_view
.buffer
, name
,
195 trigger_comm
->name_length
)) {
200 offset
+= trigger_comm
->name_length
;
201 name_size
= trigger_comm
->name_length
;
205 /* struct lttng_condition */
206 struct lttng_payload_view condition_view
=
207 lttng_payload_view_from_view(
208 src_view
, offset
, -1);
210 condition_size
= lttng_condition_create_from_payload(&condition_view
,
214 if (condition_size
< 0) {
215 ret
= condition_size
;
219 offset
+= condition_size
;
221 /* struct lttng_action */
222 struct lttng_payload_view action_view
=
223 lttng_payload_view_from_view(
224 src_view
, offset
, -1);
226 action_size
= lttng_action_create_from_payload(&action_view
, &action
);
229 if (action_size
< 0) {
233 offset
+= action_size
;
235 /* Unexpected size of inner-elements; the buffer is corrupted. */
236 if ((ssize_t
) trigger_comm
->length
!= condition_size
+ action_size
+ name_size
) {
241 trigger
= lttng_trigger_create(condition
, action
);
247 lttng_trigger_set_credentials(trigger
, &creds
);
250 * The trigger object owns references to the action and condition
253 lttng_condition_put(condition
);
256 lttng_action_put(action
);
260 const enum lttng_trigger_status status
=
261 lttng_trigger_set_name(trigger
, name
);
263 if (status
!= LTTNG_TRIGGER_STATUS_OK
) {
272 lttng_condition_put(condition
);
273 lttng_action_put(action
);
278 lttng_trigger_put(trigger
);
285 * Both elements are stored contiguously, see their "*_comm" structure
286 * for the detailed format.
288 int lttng_trigger_serialize(const struct lttng_trigger
*trigger
,
289 struct lttng_payload
*payload
)
292 size_t header_offset
, size_before_payload
, size_name
;
293 struct lttng_trigger_comm trigger_comm
= {};
294 struct lttng_trigger_comm
*header
;
295 const struct lttng_credentials
*creds
= NULL
;
297 creds
= lttng_trigger_get_credentials(trigger
);
300 trigger_comm
.uid
= LTTNG_OPTIONAL_GET(creds
->uid
);
302 if (trigger
->name
!= NULL
) {
303 size_name
= strlen(trigger
->name
) + 1;
308 trigger_comm
.name_length
= size_name
;
310 header_offset
= payload
->buffer
.size
;
311 ret
= lttng_dynamic_buffer_append(&payload
->buffer
, &trigger_comm
,
312 sizeof(trigger_comm
));
317 size_before_payload
= payload
->buffer
.size
;
320 ret
= lttng_dynamic_buffer_append(
321 &payload
->buffer
, trigger
->name
, size_name
);
326 ret
= lttng_condition_serialize(trigger
->condition
, payload
);
331 ret
= lttng_action_serialize(trigger
->action
, payload
);
336 /* Update payload size. */
337 header
= (typeof(header
)) (payload
->buffer
.data
+ header_offset
);
338 header
->length
= payload
->buffer
.size
- size_before_payload
;
343 bool lttng_trigger_is_equal(
344 const struct lttng_trigger
*a
, const struct lttng_trigger
*b
)
346 if (!!a
->name
!= !!b
->name
) {
347 /* Both must be either anonymous or named. */
351 if (a
->name
&& strcmp(a
->name
, b
->name
) != 0) {
355 if (!lttng_condition_is_equal(a
->condition
, b
->condition
)) {
359 if (!lttng_action_is_equal(a
->action
, b
->action
)) {
363 if (!lttng_credentials_is_equal(lttng_trigger_get_credentials(a
),
364 lttng_trigger_get_credentials(b
))) {
368 if (a
->is_hidden
!= b
->is_hidden
) {
375 bool lttng_trigger_is_hidden(const struct lttng_trigger
*trigger
)
377 return trigger
->is_hidden
;
380 void lttng_trigger_set_hidden(struct lttng_trigger
*trigger
)
382 LTTNG_ASSERT(!trigger
->is_hidden
);
383 trigger
->is_hidden
= true;
386 enum lttng_trigger_status
lttng_trigger_set_name(struct lttng_trigger
*trigger
,
389 char *name_copy
= NULL
;
390 enum lttng_trigger_status status
= LTTNG_TRIGGER_STATUS_OK
;
393 status
= LTTNG_TRIGGER_STATUS_INVALID
;
398 name_copy
= strdup(name
);
400 status
= LTTNG_TRIGGER_STATUS_ERROR
;
407 trigger
->name
= name_copy
;
413 enum lttng_trigger_status
lttng_trigger_get_name(
414 const struct lttng_trigger
*trigger
, const char **name
)
416 enum lttng_trigger_status status
= LTTNG_TRIGGER_STATUS_OK
;
418 if (!trigger
|| !name
) {
419 status
= LTTNG_TRIGGER_STATUS_INVALID
;
423 if (!trigger
->name
) {
424 status
= LTTNG_TRIGGER_STATUS_UNSET
;
427 *name
= trigger
->name
;
432 int lttng_trigger_assign_name(struct lttng_trigger
*dst
,
433 const struct lttng_trigger
*src
)
436 enum lttng_trigger_status status
;
438 status
= lttng_trigger_set_name(dst
, src
->name
);
439 if (status
!= LTTNG_TRIGGER_STATUS_OK
) {
441 ERR("Failed to set name for trigger");
448 void lttng_trigger_set_tracer_token(struct lttng_trigger
*trigger
,
451 LTTNG_ASSERT(trigger
);
452 LTTNG_OPTIONAL_SET(&trigger
->tracer_token
, token
);
455 uint64_t lttng_trigger_get_tracer_token(const struct lttng_trigger
*trigger
)
457 LTTNG_ASSERT(trigger
);
459 return LTTNG_OPTIONAL_GET(trigger
->tracer_token
);
462 int lttng_trigger_generate_name(struct lttng_trigger
*trigger
,
466 char *generated_name
= NULL
;
468 ret
= asprintf(&generated_name
, "trigger%" PRIu64
"", unique_id
);
470 ERR("Failed to generate trigger name");
477 trigger
->name
= generated_name
;
482 void lttng_trigger_get(struct lttng_trigger
*trigger
)
484 urcu_ref_get(&trigger
->ref
);
487 void lttng_trigger_put(struct lttng_trigger
*trigger
)
493 urcu_ref_put(&trigger
->ref
, trigger_destroy_ref
);
496 static void delete_trigger_array_element(void *ptr
)
498 struct lttng_trigger
*trigger
= (lttng_trigger
*) ptr
;
500 lttng_trigger_put(trigger
);
503 struct lttng_triggers
*lttng_triggers_create(void)
505 struct lttng_triggers
*triggers
= NULL
;
507 triggers
= (lttng_triggers
*) zmalloc(sizeof(*triggers
));
512 lttng_dynamic_pointer_array_init(&triggers
->array
, delete_trigger_array_element
);
518 struct lttng_trigger
*lttng_triggers_borrow_mutable_at_index(
519 const struct lttng_triggers
*triggers
, unsigned int index
)
521 struct lttng_trigger
*trigger
= NULL
;
523 LTTNG_ASSERT(triggers
);
524 if (index
>= lttng_dynamic_pointer_array_get_count(&triggers
->array
)) {
528 trigger
= (struct lttng_trigger
*)
529 lttng_dynamic_pointer_array_get_pointer(
530 &triggers
->array
, index
);
535 int lttng_triggers_add(
536 struct lttng_triggers
*triggers
, struct lttng_trigger
*trigger
)
540 LTTNG_ASSERT(triggers
);
541 LTTNG_ASSERT(trigger
);
543 lttng_trigger_get(trigger
);
545 ret
= lttng_dynamic_pointer_array_add_pointer(&triggers
->array
, trigger
);
547 lttng_trigger_put(trigger
);
553 int lttng_triggers_remove_hidden_triggers(struct lttng_triggers
*triggers
)
556 unsigned int trigger_count
, i
= 0;
557 enum lttng_trigger_status trigger_status
;
559 LTTNG_ASSERT(triggers
);
561 trigger_status
= lttng_triggers_get_count(triggers
, &trigger_count
);
562 LTTNG_ASSERT(trigger_status
== LTTNG_TRIGGER_STATUS_OK
);
564 while (i
< trigger_count
) {
565 const struct lttng_trigger
*trigger
=
566 lttng_triggers_get_at_index(triggers
, i
);
568 if (lttng_trigger_is_hidden(trigger
)) {
569 ret
= lttng_dynamic_pointer_array_remove_pointer(
570 &triggers
->array
, i
);
586 const struct lttng_trigger
*lttng_triggers_get_at_index(
587 const struct lttng_triggers
*triggers
, unsigned int index
)
589 return lttng_triggers_borrow_mutable_at_index(triggers
, index
);
592 enum lttng_trigger_status
lttng_triggers_get_count(const struct lttng_triggers
*triggers
, unsigned int *count
)
594 enum lttng_trigger_status status
= LTTNG_TRIGGER_STATUS_OK
;
596 if (!triggers
|| !count
) {
597 status
= LTTNG_TRIGGER_STATUS_INVALID
;
601 *count
= lttng_dynamic_pointer_array_get_count(&triggers
->array
);
606 void lttng_triggers_destroy(struct lttng_triggers
*triggers
)
612 lttng_dynamic_pointer_array_reset(&triggers
->array
);
616 int lttng_triggers_serialize(const struct lttng_triggers
*triggers
,
617 struct lttng_payload
*payload
)
620 unsigned int i
, count
;
621 size_t size_before_payload
;
622 struct lttng_triggers_comm triggers_comm
= {};
623 struct lttng_triggers_comm
*header
;
624 enum lttng_trigger_status status
;
625 const size_t header_offset
= payload
->buffer
.size
;
627 status
= lttng_triggers_get_count(triggers
, &count
);
628 if (status
!= LTTNG_TRIGGER_STATUS_OK
) {
629 ret
= LTTNG_ERR_INVALID
;
633 triggers_comm
.count
= count
;
635 /* Placeholder header; updated at the end. */
636 ret
= lttng_dynamic_buffer_append(&payload
->buffer
, &triggers_comm
,
637 sizeof(triggers_comm
));
642 size_before_payload
= payload
->buffer
.size
;
644 for (i
= 0; i
< count
; i
++) {
645 const struct lttng_trigger
*trigger
=
646 lttng_triggers_get_at_index(triggers
, i
);
648 LTTNG_ASSERT(trigger
);
650 ret
= lttng_trigger_serialize(trigger
, payload
);
656 /* Update payload size. */
657 header
= (struct lttng_triggers_comm
*) ((char *) payload
->buffer
.data
+ header_offset
);
658 header
->length
= payload
->buffer
.size
- size_before_payload
;
663 ssize_t
lttng_triggers_create_from_payload(
664 struct lttng_payload_view
*src_view
,
665 struct lttng_triggers
**triggers
)
667 ssize_t ret
, offset
= 0, triggers_size
= 0;
669 const struct lttng_triggers_comm
*triggers_comm
;
670 struct lttng_triggers
*local_triggers
= NULL
;
672 if (!src_view
|| !triggers
) {
677 /* lttng_trigger_comms header */
678 triggers_comm
= (const struct lttng_triggers_comm
*) src_view
->buffer
.data
;
679 offset
+= sizeof(*triggers_comm
);
681 local_triggers
= lttng_triggers_create();
682 if (!local_triggers
) {
687 for (i
= 0; i
< triggers_comm
->count
; i
++) {
688 struct lttng_trigger
*trigger
= NULL
;
689 struct lttng_payload_view trigger_view
=
690 lttng_payload_view_from_view(src_view
, offset
, -1);
691 ssize_t trigger_size
;
693 trigger_size
= lttng_trigger_create_from_payload(
694 &trigger_view
, &trigger
);
695 if (trigger_size
< 0) {
700 /* Transfer ownership of the trigger to the collection. */
701 ret
= lttng_triggers_add(local_triggers
, trigger
);
702 lttng_trigger_put(trigger
);
708 offset
+= trigger_size
;
709 triggers_size
+= trigger_size
;
712 /* Unexpected size of inner-elements; the buffer is corrupted. */
713 if ((ssize_t
) triggers_comm
->length
!= triggers_size
) {
718 /* Pass ownership to caller. */
719 *triggers
= local_triggers
;
720 local_triggers
= NULL
;
725 lttng_triggers_destroy(local_triggers
);
729 const struct lttng_credentials
*lttng_trigger_get_credentials(
730 const struct lttng_trigger
*trigger
)
732 return &trigger
->creds
;
735 void lttng_trigger_set_credentials(struct lttng_trigger
*trigger
,
736 const struct lttng_credentials
*creds
)
739 trigger
->creds
= *creds
;
742 enum lttng_trigger_status
lttng_trigger_set_owner_uid(
743 struct lttng_trigger
*trigger
, uid_t uid
)
745 enum lttng_trigger_status ret
= LTTNG_TRIGGER_STATUS_OK
;
746 const uid_t euid
= geteuid();
747 const struct lttng_credentials creds
= {
748 .uid
= LTTNG_OPTIONAL_INIT_VALUE(uid
),
749 .gid
= LTTNG_OPTIONAL_INIT_UNSET
,
753 ret
= LTTNG_TRIGGER_STATUS_INVALID
;
757 /* Client-side validation only to report a clearer error. */
758 if (euid
!= 0 && euid
!= uid
) {
759 ret
= LTTNG_TRIGGER_STATUS_PERMISSION_DENIED
;
763 lttng_trigger_set_credentials(trigger
, &creds
);
769 enum lttng_trigger_status
lttng_trigger_get_owner_uid(
770 const struct lttng_trigger
*trigger
, uid_t
*uid
)
772 enum lttng_trigger_status ret
= LTTNG_TRIGGER_STATUS_OK
;
773 const struct lttng_credentials
*creds
= NULL
;
775 if (!trigger
|| !uid
) {
776 ret
= LTTNG_TRIGGER_STATUS_INVALID
;
780 if (!trigger
->creds
.uid
.is_set
) {
781 ret
= LTTNG_TRIGGER_STATUS_UNSET
;
785 creds
= lttng_trigger_get_credentials(trigger
);
786 *uid
= lttng_credentials_get_uid(creds
);
792 enum lttng_domain_type
lttng_trigger_get_underlying_domain_type_restriction(
793 const struct lttng_trigger
*trigger
)
795 enum lttng_domain_type type
= LTTNG_DOMAIN_NONE
;
796 const struct lttng_event_rule
*event_rule
;
797 enum lttng_condition_status c_status
;
798 enum lttng_condition_type c_type
;
800 LTTNG_ASSERT(trigger
);
801 LTTNG_ASSERT(trigger
->condition
);
803 c_type
= lttng_condition_get_type(trigger
->condition
);
804 assert (c_type
!= LTTNG_CONDITION_TYPE_UNKNOWN
);
807 case LTTNG_CONDITION_TYPE_SESSION_CONSUMED_SIZE
:
808 case LTTNG_CONDITION_TYPE_SESSION_ROTATION_ONGOING
:
809 case LTTNG_CONDITION_TYPE_SESSION_ROTATION_COMPLETED
:
810 /* Apply to any domain. */
811 type
= LTTNG_DOMAIN_NONE
;
813 case LTTNG_CONDITION_TYPE_EVENT_RULE_MATCHES
:
814 /* Return the domain of the event rule. */
815 c_status
= lttng_condition_event_rule_matches_get_rule(
816 trigger
->condition
, &event_rule
);
817 LTTNG_ASSERT(c_status
== LTTNG_CONDITION_STATUS_OK
);
818 type
= lttng_event_rule_get_domain_type(event_rule
);
820 case LTTNG_CONDITION_TYPE_BUFFER_USAGE_HIGH
:
821 case LTTNG_CONDITION_TYPE_BUFFER_USAGE_LOW
:
822 /* Return the domain of the channel being monitored. */
823 c_status
= lttng_condition_buffer_usage_get_domain_type(
824 trigger
->condition
, &type
);
825 LTTNG_ASSERT(c_status
== LTTNG_CONDITION_STATUS_OK
);
835 * Generate bytecode related to the trigger.
836 * On success LTTNG_OK. On error, returns lttng_error code.
838 enum lttng_error_code
lttng_trigger_generate_bytecode(
839 struct lttng_trigger
*trigger
,
840 const struct lttng_credentials
*creds
)
842 enum lttng_error_code ret
;
843 struct lttng_condition
*condition
= NULL
;
845 condition
= lttng_trigger_get_condition(trigger
);
847 ret
= LTTNG_ERR_INVALID_TRIGGER
;
851 switch (lttng_condition_get_type(condition
)) {
852 case LTTNG_CONDITION_TYPE_EVENT_RULE_MATCHES
:
854 struct lttng_event_rule
*event_rule
;
855 const enum lttng_condition_status condition_status
=
856 lttng_condition_event_rule_matches_borrow_rule_mutable(
857 condition
, &event_rule
);
859 LTTNG_ASSERT(condition_status
== LTTNG_CONDITION_STATUS_OK
);
861 /* Generate the filter bytecode. */
862 ret
= lttng_event_rule_generate_filter_bytecode(
864 if (ret
!= LTTNG_OK
) {
868 /* Generate the capture bytecode. */
869 ret
= lttng_condition_event_rule_matches_generate_capture_descriptor_bytecode(
871 if (ret
!= LTTNG_OK
) {
886 struct lttng_trigger
*lttng_trigger_copy(const struct lttng_trigger
*trigger
)
889 struct lttng_payload copy_buffer
;
890 struct lttng_condition
*condition_copy
= NULL
;
891 struct lttng_action
*action_copy
= NULL
;
892 struct lttng_trigger
*copy
= NULL
;
893 enum lttng_trigger_status trigger_status
;
894 const char *trigger_name
;
895 uid_t trigger_owner_uid
;
897 lttng_payload_init(©_buffer
);
899 ret
= lttng_condition_serialize(trigger
->condition
, ©_buffer
);
905 struct lttng_payload_view view
=
906 lttng_payload_view_from_payload(
907 ©_buffer
, 0, -1);
909 ret
= lttng_condition_create_from_payload(
910 &view
, &condition_copy
);
916 lttng_payload_clear(©_buffer
);
918 ret
= lttng_action_serialize(trigger
->action
, ©_buffer
);
924 struct lttng_payload_view view
=
925 lttng_payload_view_from_payload(
926 ©_buffer
, 0, -1);
928 ret
= lttng_action_create_from_payload(
929 &view
, &action_copy
);
935 copy
= lttng_trigger_create(condition_copy
, action_copy
);
937 ERR("Failed to allocate trigger during trigger copy");
941 trigger_status
= lttng_trigger_get_name(trigger
, &trigger_name
);
942 switch (trigger_status
) {
943 case LTTNG_TRIGGER_STATUS_OK
:
944 trigger_status
= lttng_trigger_set_name(copy
, trigger_name
);
945 if (trigger_status
!= LTTNG_TRIGGER_STATUS_OK
) {
946 ERR("Failed to set name of new trigger during copy");
947 goto error_cleanup_trigger
;
950 case LTTNG_TRIGGER_STATUS_UNSET
:
953 ERR("Failed to get name of original trigger during copy");
954 goto error_cleanup_trigger
;
957 trigger_status
= lttng_trigger_get_owner_uid(
958 trigger
, &trigger_owner_uid
);
959 switch (trigger_status
) {
960 case LTTNG_TRIGGER_STATUS_OK
:
961 LTTNG_OPTIONAL_SET(©
->creds
.uid
, trigger_owner_uid
);
963 case LTTNG_TRIGGER_STATUS_UNSET
:
966 ERR("Failed to get owner uid of original trigger during copy");
967 goto error_cleanup_trigger
;
970 copy
->tracer_token
= trigger
->tracer_token
;
971 copy
->registered
= trigger
->registered
;
972 copy
->is_hidden
= trigger
->is_hidden
;
975 error_cleanup_trigger
:
976 lttng_trigger_destroy(copy
);
979 lttng_condition_put(condition_copy
);
980 lttng_action_put(action_copy
);
981 lttng_payload_reset(©_buffer
);
985 bool lttng_trigger_needs_tracer_notifier(const struct lttng_trigger
*trigger
)
987 bool needs_tracer_notifier
= false;
988 const struct lttng_condition
*condition
=
989 lttng_trigger_get_const_condition(trigger
);
991 switch (lttng_condition_get_type(condition
)) {
992 case LTTNG_CONDITION_TYPE_EVENT_RULE_MATCHES
:
993 needs_tracer_notifier
= true;
995 case LTTNG_CONDITION_TYPE_SESSION_CONSUMED_SIZE
:
996 case LTTNG_CONDITION_TYPE_BUFFER_USAGE_HIGH
:
997 case LTTNG_CONDITION_TYPE_BUFFER_USAGE_LOW
:
998 case LTTNG_CONDITION_TYPE_SESSION_ROTATION_ONGOING
:
999 case LTTNG_CONDITION_TYPE_SESSION_ROTATION_COMPLETED
:
1001 case LTTNG_CONDITION_TYPE_UNKNOWN
:
1006 return needs_tracer_notifier
;
1009 void lttng_trigger_set_as_registered(struct lttng_trigger
*trigger
)
1011 pthread_mutex_lock(&trigger
->lock
);
1012 trigger
->registered
= true;
1013 pthread_mutex_unlock(&trigger
->lock
);
1016 void lttng_trigger_set_as_unregistered(struct lttng_trigger
*trigger
)
1018 pthread_mutex_lock(&trigger
->lock
);
1019 trigger
->registered
= false;
1020 pthread_mutex_unlock(&trigger
->lock
);
1024 * The trigger must be locked before calling lttng_trigger_registered.
1025 * The lock is necessary since a trigger can be unregistered at anytime.
1026 * Manipulations requiring that the trigger be registered must always acquire
1027 * the trigger lock for the duration of the manipulation using
1028 * `lttng_trigger_lock` and `lttng_trigger_unlock`.
1030 bool lttng_trigger_is_registered(struct lttng_trigger
*trigger
)
1032 ASSERT_LOCKED(trigger
->lock
);
1033 return trigger
->registered
;
1036 void lttng_trigger_lock(struct lttng_trigger
*trigger
)
1038 pthread_mutex_lock(&trigger
->lock
);
1041 void lttng_trigger_unlock(struct lttng_trigger
*trigger
)
1043 pthread_mutex_unlock(&trigger
->lock
);
1046 enum lttng_error_code
lttng_trigger_mi_serialize(const struct lttng_trigger
*trigger
,
1047 struct mi_writer
*writer
,
1048 const struct mi_lttng_error_query_callbacks
1049 *error_query_callbacks
)
1052 enum lttng_error_code ret_code
;
1053 enum lttng_trigger_status trigger_status
;
1054 const struct lttng_condition
*condition
= NULL
;
1055 const struct lttng_action
*action
= NULL
;
1056 struct lttng_dynamic_array action_path_indexes
;
1059 LTTNG_ASSERT(trigger
);
1060 LTTNG_ASSERT(writer
);
1062 lttng_dynamic_array_init(&action_path_indexes
, sizeof(uint64_t), NULL
);
1064 /* Open trigger element. */
1065 ret
= mi_lttng_writer_open_element(writer
, mi_lttng_element_trigger
);
1070 trigger_status
= lttng_trigger_get_owner_uid(trigger
, &owner_uid
);
1071 LTTNG_ASSERT(trigger_status
== LTTNG_TRIGGER_STATUS_OK
);
1074 ret
= mi_lttng_writer_write_element_string(
1075 writer
, config_element_name
, trigger
->name
);
1081 ret
= mi_lttng_writer_write_element_signed_int(writer
,
1082 mi_lttng_element_trigger_owner_uid
,
1083 (int64_t) owner_uid
);
1089 condition
= lttng_trigger_get_const_condition(trigger
);
1090 LTTNG_ASSERT(condition
);
1091 ret_code
= lttng_condition_mi_serialize(
1092 trigger
, condition
, writer
, error_query_callbacks
);
1093 if (ret_code
!= LTTNG_OK
) {
1098 action
= lttng_trigger_get_const_action(trigger
);
1099 LTTNG_ASSERT(action
);
1100 ret_code
= lttng_action_mi_serialize(trigger
, action
, writer
,
1101 error_query_callbacks
, &action_path_indexes
);
1102 if (ret_code
!= LTTNG_OK
) {
1106 if (error_query_callbacks
&& error_query_callbacks
->trigger_cb
) {
1107 struct lttng_error_query_results
*results
= NULL
;
1109 ret_code
= error_query_callbacks
->trigger_cb(trigger
, &results
);
1110 if (ret_code
!= LTTNG_OK
) {
1114 ret_code
= lttng_error_query_results_mi_serialize(
1116 lttng_error_query_results_destroy(results
);
1117 if (ret_code
!= LTTNG_OK
) {
1122 /* Close trigger element. */
1123 ret
= mi_lttng_writer_close_element(writer
);
1128 ret_code
= LTTNG_OK
;
1132 ret_code
= LTTNG_ERR_MI_IO_FAIL
;
1134 lttng_dynamic_array_reset(&action_path_indexes
);
1138 /* Used by qsort, which expects the semantics of strcmp(). */
1139 static int compare_triggers_by_name(const void *a
, const void *b
)
1141 const struct lttng_trigger
*trigger_a
=
1142 *((const struct lttng_trigger
**) a
);
1143 const struct lttng_trigger
*trigger_b
=
1144 *((const struct lttng_trigger
**) b
);
1145 const char *name_a
, *name_b
;
1146 enum lttng_trigger_status trigger_status
;
1148 /* Anonymous triggers are not reachable here. */
1149 trigger_status
= lttng_trigger_get_name(trigger_a
, &name_a
);
1150 LTTNG_ASSERT(trigger_status
== LTTNG_TRIGGER_STATUS_OK
);
1152 trigger_status
= lttng_trigger_get_name(trigger_b
, &name_b
);
1153 LTTNG_ASSERT(trigger_status
== LTTNG_TRIGGER_STATUS_OK
);
1155 return strcmp(name_a
, name_b
);
1158 enum lttng_error_code
lttng_triggers_mi_serialize(const struct lttng_triggers
*triggers
,
1159 struct mi_writer
*writer
,
1160 const struct mi_lttng_error_query_callbacks
1161 *error_query_callbacks
)
1164 enum lttng_error_code ret_code
;
1165 enum lttng_trigger_status status
;
1166 unsigned int count
, i
;
1167 struct lttng_dynamic_pointer_array sorted_triggers
;
1169 LTTNG_ASSERT(triggers
);
1170 LTTNG_ASSERT(writer
);
1173 * Sort trigger by name to ensure an order at the MI level and ignore
1174 * any anonymous trigger present.
1176 lttng_dynamic_pointer_array_init(&sorted_triggers
, NULL
);
1178 status
= lttng_triggers_get_count(triggers
, &count
);
1179 LTTNG_ASSERT(status
== LTTNG_TRIGGER_STATUS_OK
);
1181 for (i
= 0; i
< count
; i
++) {
1183 const char *unused_name
;
1184 const struct lttng_trigger
*trigger
=
1185 lttng_triggers_get_at_index(triggers
, i
);
1187 status
= lttng_trigger_get_name(trigger
, &unused_name
);
1189 case LTTNG_TRIGGER_STATUS_OK
:
1191 case LTTNG_TRIGGER_STATUS_UNSET
:
1192 /* Don't list anonymous triggers. */
1198 add_ret
= lttng_dynamic_pointer_array_add_pointer(
1199 &sorted_triggers
, (void *) trigger
);
1202 ERR("Failed to lttng_trigger to sorting array.");
1203 ret_code
= LTTNG_ERR_NOMEM
;
1208 qsort(sorted_triggers
.array
.buffer
.data
, count
,
1209 sizeof(struct lttng_trigger
*),
1210 compare_triggers_by_name
);
1212 /* Open triggers element. */
1213 ret
= mi_lttng_writer_open_element(writer
, mi_lttng_element_triggers
);
1215 ret_code
= LTTNG_ERR_MI_IO_FAIL
;
1219 for (i
= 0; i
< lttng_dynamic_pointer_array_get_count(&sorted_triggers
); i
++) {
1220 const struct lttng_trigger
*trigger
=
1221 (const struct lttng_trigger
*)
1222 lttng_dynamic_pointer_array_get_pointer(
1223 &sorted_triggers
, i
);
1225 lttng_trigger_mi_serialize(trigger
, writer
, error_query_callbacks
);
1228 /* Close triggers element. */
1229 ret
= mi_lttng_writer_close_element(writer
);
1231 ret_code
= LTTNG_ERR_MI_IO_FAIL
;
1235 ret_code
= LTTNG_OK
;
1238 lttng_dynamic_pointer_array_reset(&sorted_triggers
);