2 * Copyright (C) 2017 Jérémie Galarneau <jeremie.galarneau@efficios.com>
4 * SPDX-License-Identifier: LGPL-2.1-only
8 #include <common/credentials.hpp>
9 #include <common/dynamic-array.hpp>
10 #include <common/error.hpp>
11 #include <common/mi-lttng.hpp>
12 #include <common/optional.hpp>
13 #include <common/payload-view.hpp>
14 #include <common/payload.hpp>
16 #include <lttng/action/action-internal.hpp>
17 #include <lttng/condition/buffer-usage.h>
18 #include <lttng/condition/condition-internal.hpp>
19 #include <lttng/condition/event-rule-matches-internal.hpp>
20 #include <lttng/condition/event-rule-matches.h>
21 #include <lttng/domain.h>
22 #include <lttng/error-query-internal.hpp>
23 #include <lttng/event-expr-internal.hpp>
24 #include <lttng/event-rule/event-rule-internal.hpp>
25 #include <lttng/trigger/trigger-internal.hpp>
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
= zmalloc
<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 lttng::utils::container_of(ref
, <tng_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 LTTNG_ASSERT(trigger
);
378 return trigger
->is_hidden
;
381 void lttng_trigger_set_hidden(struct lttng_trigger
*trigger
)
383 LTTNG_ASSERT(!trigger
->is_hidden
);
384 trigger
->is_hidden
= true;
387 enum lttng_trigger_status
lttng_trigger_set_name(struct lttng_trigger
*trigger
,
390 char *name_copy
= NULL
;
391 enum lttng_trigger_status status
= LTTNG_TRIGGER_STATUS_OK
;
394 status
= LTTNG_TRIGGER_STATUS_INVALID
;
399 name_copy
= strdup(name
);
401 status
= LTTNG_TRIGGER_STATUS_ERROR
;
408 trigger
->name
= name_copy
;
414 enum lttng_trigger_status
lttng_trigger_get_name(
415 const struct lttng_trigger
*trigger
, const char **name
)
417 enum lttng_trigger_status status
= LTTNG_TRIGGER_STATUS_OK
;
419 if (!trigger
|| !name
) {
420 status
= LTTNG_TRIGGER_STATUS_INVALID
;
424 if (!trigger
->name
) {
425 status
= LTTNG_TRIGGER_STATUS_UNSET
;
428 *name
= trigger
->name
;
433 int lttng_trigger_assign_name(struct lttng_trigger
*dst
,
434 const struct lttng_trigger
*src
)
437 enum lttng_trigger_status status
;
439 status
= lttng_trigger_set_name(dst
, src
->name
);
440 if (status
!= LTTNG_TRIGGER_STATUS_OK
) {
442 ERR("Failed to set name for trigger");
449 void lttng_trigger_set_tracer_token(struct lttng_trigger
*trigger
,
452 LTTNG_ASSERT(trigger
);
453 LTTNG_OPTIONAL_SET(&trigger
->tracer_token
, token
);
456 uint64_t lttng_trigger_get_tracer_token(const struct lttng_trigger
*trigger
)
458 LTTNG_ASSERT(trigger
);
460 return LTTNG_OPTIONAL_GET(trigger
->tracer_token
);
463 int lttng_trigger_generate_name(struct lttng_trigger
*trigger
,
467 char *generated_name
= NULL
;
469 ret
= asprintf(&generated_name
, "trigger%" PRIu64
"", unique_id
);
471 ERR("Failed to generate trigger name");
478 trigger
->name
= generated_name
;
483 void lttng_trigger_get(struct lttng_trigger
*trigger
)
485 urcu_ref_get(&trigger
->ref
);
488 void lttng_trigger_put(struct lttng_trigger
*trigger
)
494 urcu_ref_put(&trigger
->ref
, trigger_destroy_ref
);
497 static void delete_trigger_array_element(void *ptr
)
499 struct lttng_trigger
*trigger
= (lttng_trigger
*) ptr
;
501 lttng_trigger_put(trigger
);
504 struct lttng_triggers
*lttng_triggers_create(void)
506 struct lttng_triggers
*triggers
= NULL
;
508 triggers
= zmalloc
<lttng_triggers
>();
513 lttng_dynamic_pointer_array_init(&triggers
->array
, delete_trigger_array_element
);
519 struct lttng_trigger
*lttng_triggers_borrow_mutable_at_index(
520 const struct lttng_triggers
*triggers
, unsigned int index
)
522 struct lttng_trigger
*trigger
= NULL
;
524 LTTNG_ASSERT(triggers
);
525 if (index
>= lttng_dynamic_pointer_array_get_count(&triggers
->array
)) {
529 trigger
= (struct lttng_trigger
*)
530 lttng_dynamic_pointer_array_get_pointer(
531 &triggers
->array
, index
);
536 int lttng_triggers_add(
537 struct lttng_triggers
*triggers
, struct lttng_trigger
*trigger
)
541 LTTNG_ASSERT(triggers
);
542 LTTNG_ASSERT(trigger
);
544 lttng_trigger_get(trigger
);
546 ret
= lttng_dynamic_pointer_array_add_pointer(&triggers
->array
, trigger
);
548 lttng_trigger_put(trigger
);
554 int lttng_triggers_remove_hidden_triggers(struct lttng_triggers
*triggers
)
557 unsigned int trigger_count
, i
= 0;
558 enum lttng_trigger_status trigger_status
;
560 LTTNG_ASSERT(triggers
);
562 trigger_status
= lttng_triggers_get_count(triggers
, &trigger_count
);
563 LTTNG_ASSERT(trigger_status
== LTTNG_TRIGGER_STATUS_OK
);
565 while (i
< trigger_count
) {
566 const struct lttng_trigger
*trigger
=
567 lttng_triggers_get_at_index(triggers
, i
);
569 if (lttng_trigger_is_hidden(trigger
)) {
570 ret
= lttng_dynamic_pointer_array_remove_pointer(
571 &triggers
->array
, i
);
587 const struct lttng_trigger
*lttng_triggers_get_at_index(
588 const struct lttng_triggers
*triggers
, unsigned int index
)
590 return lttng_triggers_borrow_mutable_at_index(triggers
, index
);
593 enum lttng_trigger_status
lttng_triggers_get_count(const struct lttng_triggers
*triggers
, unsigned int *count
)
595 enum lttng_trigger_status status
= LTTNG_TRIGGER_STATUS_OK
;
597 if (!triggers
|| !count
) {
598 status
= LTTNG_TRIGGER_STATUS_INVALID
;
602 *count
= lttng_dynamic_pointer_array_get_count(&triggers
->array
);
607 void lttng_triggers_destroy(struct lttng_triggers
*triggers
)
613 lttng_dynamic_pointer_array_reset(&triggers
->array
);
617 int lttng_triggers_serialize(const struct lttng_triggers
*triggers
,
618 struct lttng_payload
*payload
)
621 unsigned int i
, count
;
622 size_t size_before_payload
;
623 struct lttng_triggers_comm triggers_comm
= {};
624 struct lttng_triggers_comm
*header
;
625 enum lttng_trigger_status status
;
626 const size_t header_offset
= payload
->buffer
.size
;
628 status
= lttng_triggers_get_count(triggers
, &count
);
629 if (status
!= LTTNG_TRIGGER_STATUS_OK
) {
630 ret
= LTTNG_ERR_INVALID
;
634 triggers_comm
.count
= count
;
636 /* Placeholder header; updated at the end. */
637 ret
= lttng_dynamic_buffer_append(&payload
->buffer
, &triggers_comm
,
638 sizeof(triggers_comm
));
643 size_before_payload
= payload
->buffer
.size
;
645 for (i
= 0; i
< count
; i
++) {
646 const struct lttng_trigger
*trigger
=
647 lttng_triggers_get_at_index(triggers
, i
);
649 LTTNG_ASSERT(trigger
);
651 ret
= lttng_trigger_serialize(trigger
, payload
);
657 /* Update payload size. */
658 header
= (struct lttng_triggers_comm
*) ((char *) payload
->buffer
.data
+ header_offset
);
659 header
->length
= payload
->buffer
.size
- size_before_payload
;
664 ssize_t
lttng_triggers_create_from_payload(
665 struct lttng_payload_view
*src_view
,
666 struct lttng_triggers
**triggers
)
668 ssize_t ret
, offset
= 0, triggers_size
= 0;
670 const struct lttng_triggers_comm
*triggers_comm
;
671 struct lttng_triggers
*local_triggers
= NULL
;
673 if (!src_view
|| !triggers
) {
678 /* lttng_trigger_comms header */
679 triggers_comm
= (const struct lttng_triggers_comm
*) src_view
->buffer
.data
;
680 offset
+= sizeof(*triggers_comm
);
682 local_triggers
= lttng_triggers_create();
683 if (!local_triggers
) {
688 for (i
= 0; i
< triggers_comm
->count
; i
++) {
689 struct lttng_trigger
*trigger
= NULL
;
690 struct lttng_payload_view trigger_view
=
691 lttng_payload_view_from_view(src_view
, offset
, -1);
692 ssize_t trigger_size
;
694 trigger_size
= lttng_trigger_create_from_payload(
695 &trigger_view
, &trigger
);
696 if (trigger_size
< 0) {
701 /* Transfer ownership of the trigger to the collection. */
702 ret
= lttng_triggers_add(local_triggers
, trigger
);
703 lttng_trigger_put(trigger
);
709 offset
+= trigger_size
;
710 triggers_size
+= trigger_size
;
713 /* Unexpected size of inner-elements; the buffer is corrupted. */
714 if ((ssize_t
) triggers_comm
->length
!= triggers_size
) {
719 /* Pass ownership to caller. */
720 *triggers
= local_triggers
;
721 local_triggers
= NULL
;
726 lttng_triggers_destroy(local_triggers
);
730 const struct lttng_credentials
*lttng_trigger_get_credentials(
731 const struct lttng_trigger
*trigger
)
733 return &trigger
->creds
;
736 void lttng_trigger_set_credentials(struct lttng_trigger
*trigger
,
737 const struct lttng_credentials
*creds
)
740 trigger
->creds
= *creds
;
743 enum lttng_trigger_status
lttng_trigger_set_owner_uid(
744 struct lttng_trigger
*trigger
, uid_t uid
)
746 enum lttng_trigger_status ret
= LTTNG_TRIGGER_STATUS_OK
;
747 const uid_t euid
= geteuid();
748 const struct lttng_credentials creds
= {
749 .uid
= LTTNG_OPTIONAL_INIT_VALUE(uid
),
750 .gid
= LTTNG_OPTIONAL_INIT_UNSET
,
754 ret
= LTTNG_TRIGGER_STATUS_INVALID
;
758 /* Client-side validation only to report a clearer error. */
759 if (euid
!= 0 && euid
!= uid
) {
760 ret
= LTTNG_TRIGGER_STATUS_PERMISSION_DENIED
;
764 lttng_trigger_set_credentials(trigger
, &creds
);
770 enum lttng_trigger_status
lttng_trigger_get_owner_uid(
771 const struct lttng_trigger
*trigger
, uid_t
*uid
)
773 enum lttng_trigger_status ret
= LTTNG_TRIGGER_STATUS_OK
;
774 const struct lttng_credentials
*creds
= NULL
;
776 if (!trigger
|| !uid
) {
777 ret
= LTTNG_TRIGGER_STATUS_INVALID
;
781 if (!trigger
->creds
.uid
.is_set
) {
782 ret
= LTTNG_TRIGGER_STATUS_UNSET
;
786 creds
= lttng_trigger_get_credentials(trigger
);
787 *uid
= lttng_credentials_get_uid(creds
);
793 enum lttng_domain_type
lttng_trigger_get_underlying_domain_type_restriction(
794 const struct lttng_trigger
*trigger
)
796 enum lttng_domain_type type
= LTTNG_DOMAIN_NONE
;
797 const struct lttng_event_rule
*event_rule
;
798 enum lttng_condition_status c_status
;
799 enum lttng_condition_type c_type
;
801 LTTNG_ASSERT(trigger
);
802 LTTNG_ASSERT(trigger
->condition
);
804 c_type
= lttng_condition_get_type(trigger
->condition
);
805 assert (c_type
!= LTTNG_CONDITION_TYPE_UNKNOWN
);
808 case LTTNG_CONDITION_TYPE_SESSION_CONSUMED_SIZE
:
809 case LTTNG_CONDITION_TYPE_SESSION_ROTATION_ONGOING
:
810 case LTTNG_CONDITION_TYPE_SESSION_ROTATION_COMPLETED
:
811 /* Apply to any domain. */
812 type
= LTTNG_DOMAIN_NONE
;
814 case LTTNG_CONDITION_TYPE_EVENT_RULE_MATCHES
:
815 /* Return the domain of the event rule. */
816 c_status
= lttng_condition_event_rule_matches_get_rule(
817 trigger
->condition
, &event_rule
);
818 LTTNG_ASSERT(c_status
== LTTNG_CONDITION_STATUS_OK
);
819 type
= lttng_event_rule_get_domain_type(event_rule
);
821 case LTTNG_CONDITION_TYPE_BUFFER_USAGE_HIGH
:
822 case LTTNG_CONDITION_TYPE_BUFFER_USAGE_LOW
:
823 /* Return the domain of the channel being monitored. */
824 c_status
= lttng_condition_buffer_usage_get_domain_type(
825 trigger
->condition
, &type
);
826 LTTNG_ASSERT(c_status
== LTTNG_CONDITION_STATUS_OK
);
836 * Generate bytecode related to the trigger.
837 * On success LTTNG_OK. On error, returns lttng_error code.
839 enum lttng_error_code
lttng_trigger_generate_bytecode(
840 struct lttng_trigger
*trigger
,
841 const struct lttng_credentials
*creds
)
843 enum lttng_error_code ret
;
844 struct lttng_condition
*condition
= NULL
;
846 condition
= lttng_trigger_get_condition(trigger
);
848 ret
= LTTNG_ERR_INVALID_TRIGGER
;
852 switch (lttng_condition_get_type(condition
)) {
853 case LTTNG_CONDITION_TYPE_EVENT_RULE_MATCHES
:
855 struct lttng_event_rule
*event_rule
;
856 const enum lttng_condition_status condition_status
=
857 lttng_condition_event_rule_matches_borrow_rule_mutable(
858 condition
, &event_rule
);
860 LTTNG_ASSERT(condition_status
== LTTNG_CONDITION_STATUS_OK
);
862 /* Generate the filter bytecode. */
863 ret
= lttng_event_rule_generate_filter_bytecode(
865 if (ret
!= LTTNG_OK
) {
869 /* Generate the capture bytecode. */
870 ret
= lttng_condition_event_rule_matches_generate_capture_descriptor_bytecode(
872 if (ret
!= LTTNG_OK
) {
887 struct lttng_trigger
*lttng_trigger_copy(const struct lttng_trigger
*trigger
)
890 struct lttng_payload copy_buffer
;
891 struct lttng_condition
*condition_copy
= NULL
;
892 struct lttng_action
*action_copy
= NULL
;
893 struct lttng_trigger
*copy
= NULL
;
894 enum lttng_trigger_status trigger_status
;
895 const char *trigger_name
;
896 uid_t trigger_owner_uid
;
898 lttng_payload_init(©_buffer
);
900 ret
= lttng_condition_serialize(trigger
->condition
, ©_buffer
);
906 struct lttng_payload_view view
=
907 lttng_payload_view_from_payload(
908 ©_buffer
, 0, -1);
910 ret
= lttng_condition_create_from_payload(
911 &view
, &condition_copy
);
917 lttng_payload_clear(©_buffer
);
919 ret
= lttng_action_serialize(trigger
->action
, ©_buffer
);
925 struct lttng_payload_view view
=
926 lttng_payload_view_from_payload(
927 ©_buffer
, 0, -1);
929 ret
= lttng_action_create_from_payload(
930 &view
, &action_copy
);
936 copy
= lttng_trigger_create(condition_copy
, action_copy
);
938 ERR("Failed to allocate trigger during trigger copy");
942 trigger_status
= lttng_trigger_get_name(trigger
, &trigger_name
);
943 switch (trigger_status
) {
944 case LTTNG_TRIGGER_STATUS_OK
:
945 trigger_status
= lttng_trigger_set_name(copy
, trigger_name
);
946 if (trigger_status
!= LTTNG_TRIGGER_STATUS_OK
) {
947 ERR("Failed to set name of new trigger during copy");
948 goto error_cleanup_trigger
;
951 case LTTNG_TRIGGER_STATUS_UNSET
:
954 ERR("Failed to get name of original trigger during copy");
955 goto error_cleanup_trigger
;
958 trigger_status
= lttng_trigger_get_owner_uid(
959 trigger
, &trigger_owner_uid
);
960 switch (trigger_status
) {
961 case LTTNG_TRIGGER_STATUS_OK
:
962 LTTNG_OPTIONAL_SET(©
->creds
.uid
, trigger_owner_uid
);
964 case LTTNG_TRIGGER_STATUS_UNSET
:
967 ERR("Failed to get owner uid of original trigger during copy");
968 goto error_cleanup_trigger
;
971 copy
->tracer_token
= trigger
->tracer_token
;
972 copy
->registered
= trigger
->registered
;
973 copy
->is_hidden
= trigger
->is_hidden
;
976 error_cleanup_trigger
:
977 lttng_trigger_destroy(copy
);
980 lttng_condition_put(condition_copy
);
981 lttng_action_put(action_copy
);
982 lttng_payload_reset(©_buffer
);
986 bool lttng_trigger_needs_tracer_notifier(const struct lttng_trigger
*trigger
)
988 bool needs_tracer_notifier
= false;
989 const struct lttng_condition
*condition
=
990 lttng_trigger_get_const_condition(trigger
);
992 switch (lttng_condition_get_type(condition
)) {
993 case LTTNG_CONDITION_TYPE_EVENT_RULE_MATCHES
:
994 needs_tracer_notifier
= true;
996 case LTTNG_CONDITION_TYPE_SESSION_CONSUMED_SIZE
:
997 case LTTNG_CONDITION_TYPE_BUFFER_USAGE_HIGH
:
998 case LTTNG_CONDITION_TYPE_BUFFER_USAGE_LOW
:
999 case LTTNG_CONDITION_TYPE_SESSION_ROTATION_ONGOING
:
1000 case LTTNG_CONDITION_TYPE_SESSION_ROTATION_COMPLETED
:
1002 case LTTNG_CONDITION_TYPE_UNKNOWN
:
1007 return needs_tracer_notifier
;
1010 void lttng_trigger_set_as_registered(struct lttng_trigger
*trigger
)
1012 pthread_mutex_lock(&trigger
->lock
);
1013 trigger
->registered
= true;
1014 pthread_mutex_unlock(&trigger
->lock
);
1017 void lttng_trigger_set_as_unregistered(struct lttng_trigger
*trigger
)
1019 pthread_mutex_lock(&trigger
->lock
);
1020 trigger
->registered
= false;
1021 pthread_mutex_unlock(&trigger
->lock
);
1025 * The trigger must be locked before calling lttng_trigger_registered.
1026 * The lock is necessary since a trigger can be unregistered at anytime.
1027 * Manipulations requiring that the trigger be registered must always acquire
1028 * the trigger lock for the duration of the manipulation using
1029 * `lttng_trigger_lock` and `lttng_trigger_unlock`.
1031 bool lttng_trigger_is_registered(struct lttng_trigger
*trigger
)
1033 ASSERT_LOCKED(trigger
->lock
);
1034 return trigger
->registered
;
1037 void lttng_trigger_lock(struct lttng_trigger
*trigger
)
1039 pthread_mutex_lock(&trigger
->lock
);
1042 void lttng_trigger_unlock(struct lttng_trigger
*trigger
)
1044 pthread_mutex_unlock(&trigger
->lock
);
1047 enum lttng_error_code
lttng_trigger_mi_serialize(const struct lttng_trigger
*trigger
,
1048 struct mi_writer
*writer
,
1049 const struct mi_lttng_error_query_callbacks
1050 *error_query_callbacks
)
1053 enum lttng_error_code ret_code
;
1054 enum lttng_trigger_status trigger_status
;
1055 const struct lttng_condition
*condition
= NULL
;
1056 const struct lttng_action
*action
= NULL
;
1057 struct lttng_dynamic_array action_path_indexes
;
1060 LTTNG_ASSERT(trigger
);
1061 LTTNG_ASSERT(writer
);
1063 lttng_dynamic_array_init(&action_path_indexes
, sizeof(uint64_t), NULL
);
1065 /* Open trigger element. */
1066 ret
= mi_lttng_writer_open_element(writer
, mi_lttng_element_trigger
);
1071 trigger_status
= lttng_trigger_get_owner_uid(trigger
, &owner_uid
);
1072 LTTNG_ASSERT(trigger_status
== LTTNG_TRIGGER_STATUS_OK
);
1075 ret
= mi_lttng_writer_write_element_string(
1076 writer
, config_element_name
, trigger
->name
);
1082 ret
= mi_lttng_writer_write_element_signed_int(writer
,
1083 mi_lttng_element_trigger_owner_uid
,
1084 (int64_t) owner_uid
);
1090 condition
= lttng_trigger_get_const_condition(trigger
);
1091 LTTNG_ASSERT(condition
);
1092 ret_code
= lttng_condition_mi_serialize(
1093 trigger
, condition
, writer
, error_query_callbacks
);
1094 if (ret_code
!= LTTNG_OK
) {
1099 action
= lttng_trigger_get_const_action(trigger
);
1100 LTTNG_ASSERT(action
);
1101 ret_code
= lttng_action_mi_serialize(trigger
, action
, writer
,
1102 error_query_callbacks
, &action_path_indexes
);
1103 if (ret_code
!= LTTNG_OK
) {
1107 if (error_query_callbacks
&& error_query_callbacks
->trigger_cb
) {
1108 struct lttng_error_query_results
*results
= NULL
;
1110 ret_code
= error_query_callbacks
->trigger_cb(trigger
, &results
);
1111 if (ret_code
!= LTTNG_OK
) {
1115 ret_code
= lttng_error_query_results_mi_serialize(
1117 lttng_error_query_results_destroy(results
);
1118 if (ret_code
!= LTTNG_OK
) {
1123 /* Close trigger element. */
1124 ret
= mi_lttng_writer_close_element(writer
);
1129 ret_code
= LTTNG_OK
;
1133 ret_code
= LTTNG_ERR_MI_IO_FAIL
;
1135 lttng_dynamic_array_reset(&action_path_indexes
);
1139 /* Used by qsort, which expects the semantics of strcmp(). */
1140 static int compare_triggers_by_name(const void *a
, const void *b
)
1142 const struct lttng_trigger
*trigger_a
=
1143 *((const struct lttng_trigger
**) a
);
1144 const struct lttng_trigger
*trigger_b
=
1145 *((const struct lttng_trigger
**) b
);
1146 const char *name_a
, *name_b
;
1147 enum lttng_trigger_status trigger_status
;
1149 /* Anonymous triggers are not reachable here. */
1150 trigger_status
= lttng_trigger_get_name(trigger_a
, &name_a
);
1151 LTTNG_ASSERT(trigger_status
== LTTNG_TRIGGER_STATUS_OK
);
1153 trigger_status
= lttng_trigger_get_name(trigger_b
, &name_b
);
1154 LTTNG_ASSERT(trigger_status
== LTTNG_TRIGGER_STATUS_OK
);
1156 return strcmp(name_a
, name_b
);
1159 enum lttng_error_code
lttng_triggers_mi_serialize(const struct lttng_triggers
*triggers
,
1160 struct mi_writer
*writer
,
1161 const struct mi_lttng_error_query_callbacks
1162 *error_query_callbacks
)
1165 enum lttng_error_code ret_code
;
1166 enum lttng_trigger_status status
;
1167 unsigned int count
, i
;
1168 struct lttng_dynamic_pointer_array sorted_triggers
;
1170 LTTNG_ASSERT(triggers
);
1171 LTTNG_ASSERT(writer
);
1174 * Sort trigger by name to ensure an order at the MI level and ignore
1175 * any anonymous trigger present.
1177 lttng_dynamic_pointer_array_init(&sorted_triggers
, NULL
);
1179 status
= lttng_triggers_get_count(triggers
, &count
);
1180 LTTNG_ASSERT(status
== LTTNG_TRIGGER_STATUS_OK
);
1182 for (i
= 0; i
< count
; i
++) {
1184 const char *unused_name
;
1185 const struct lttng_trigger
*trigger
=
1186 lttng_triggers_get_at_index(triggers
, i
);
1188 status
= lttng_trigger_get_name(trigger
, &unused_name
);
1190 case LTTNG_TRIGGER_STATUS_OK
:
1192 case LTTNG_TRIGGER_STATUS_UNSET
:
1193 /* Don't list anonymous triggers. */
1199 add_ret
= lttng_dynamic_pointer_array_add_pointer(
1200 &sorted_triggers
, (void *) trigger
);
1203 ERR("Failed to lttng_trigger to sorting array.");
1204 ret_code
= LTTNG_ERR_NOMEM
;
1209 qsort(sorted_triggers
.array
.buffer
.data
, count
,
1210 sizeof(struct lttng_trigger
*),
1211 compare_triggers_by_name
);
1213 /* Open triggers element. */
1214 ret
= mi_lttng_writer_open_element(writer
, mi_lttng_element_triggers
);
1216 ret_code
= LTTNG_ERR_MI_IO_FAIL
;
1220 for (i
= 0; i
< lttng_dynamic_pointer_array_get_count(&sorted_triggers
); i
++) {
1221 const struct lttng_trigger
*trigger
=
1222 (const struct lttng_trigger
*)
1223 lttng_dynamic_pointer_array_get_pointer(
1224 &sorted_triggers
, i
);
1226 lttng_trigger_mi_serialize(trigger
, writer
, error_query_callbacks
);
1229 /* Close triggers element. */
1230 ret
= mi_lttng_writer_close_element(writer
);
1232 ret_code
= LTTNG_ERR_MI_IO_FAIL
;
1236 ret_code
= LTTNG_OK
;
1239 lttng_dynamic_pointer_array_reset(&sorted_triggers
);