2 * Copyright (C) 2017 Jérémie Galarneau <jeremie.galarneau@efficios.com>
4 * SPDX-License-Identifier: LGPL-2.1-only
8 #include <lttng/trigger/trigger-internal.h>
9 #include <lttng/condition/condition-internal.h>
10 #include <lttng/condition/event-rule.h>
11 #include <lttng/condition/buffer-usage.h>
12 #include <lttng/event-rule/event-rule-internal.h>
13 #include <lttng/action/action-internal.h>
14 #include <common/credentials.h>
15 #include <common/payload.h>
16 #include <common/payload-view.h>
17 #include <lttng/domain.h>
18 #include <common/error.h>
19 #include <common/dynamic-array.h>
20 #include <common/optional.h>
25 bool lttng_trigger_validate(struct lttng_trigger
*trigger
)
34 if (!trigger
->creds
.uid
.is_set
) {
39 valid
= lttng_condition_validate(trigger
->condition
) &&
40 lttng_action_validate(trigger
->action
);
45 struct lttng_trigger
*lttng_trigger_create(
46 struct lttng_condition
*condition
,
47 struct lttng_action
*action
)
49 struct lttng_trigger
*trigger
= NULL
;
51 if (!condition
|| !action
) {
55 trigger
= zmalloc(sizeof(struct lttng_trigger
));
60 urcu_ref_init(&trigger
->ref
);
62 trigger
->firing_policy
.type
= LTTNG_TRIGGER_FIRING_POLICY_EVERY_N
;
63 trigger
->firing_policy
.threshold
= 1;
65 lttng_condition_get(condition
);
66 trigger
->condition
= condition
;
68 lttng_action_get(action
);
69 trigger
->action
= action
;
76 * Note: the lack of reference counting 'get' on the condition object is normal.
77 * This API was exposed as such in 2.11. The client is not expected to call
78 * lttng_condition_destroy on the returned object.
80 struct lttng_condition
*lttng_trigger_get_condition(
81 struct lttng_trigger
*trigger
)
83 return trigger
? trigger
->condition
: NULL
;
87 const struct lttng_condition
*lttng_trigger_get_const_condition(
88 const struct lttng_trigger
*trigger
)
90 return trigger
->condition
;
95 * Note: the lack of reference counting 'get' on the action object is normal.
96 * This API was exposed as such in 2.11. The client is not expected to call
97 * lttng_action_destroy on the returned object.
99 struct lttng_action
*lttng_trigger_get_action(
100 struct lttng_trigger
*trigger
)
102 return trigger
? trigger
->action
: NULL
;
106 const struct lttng_action
*lttng_trigger_get_const_action(
107 const struct lttng_trigger
*trigger
)
109 return trigger
->action
;
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
);
123 /* Release ownership. */
124 lttng_action_put(action
);
125 lttng_condition_put(condition
);
131 void lttng_trigger_destroy(struct lttng_trigger
*trigger
)
133 lttng_trigger_put(trigger
);
136 static bool is_firing_policy_valid(enum lttng_trigger_firing_policy policy
)
141 case LTTNG_TRIGGER_FIRING_POLICY_EVERY_N
:
142 case LTTNG_TRIGGER_FIRING_POLICY_ONCE_AFTER_N
:
154 ssize_t
lttng_trigger_create_from_payload(
155 struct lttng_payload_view
*src_view
,
156 struct lttng_trigger
**_trigger
)
158 ssize_t ret
, offset
= 0, condition_size
, action_size
, name_size
= 0;
159 struct lttng_condition
*condition
= NULL
;
160 struct lttng_action
*action
= NULL
;
161 const struct lttng_trigger_comm
*trigger_comm
;
162 const char *name
= NULL
;
163 uint64_t firing_policy_threshold
;
164 enum lttng_trigger_firing_policy firing_policy
;
165 struct lttng_credentials creds
= {
166 .uid
= LTTNG_OPTIONAL_INIT_UNSET
,
167 .gid
= LTTNG_OPTIONAL_INIT_UNSET
,
169 struct lttng_trigger
*trigger
= NULL
;
170 const struct lttng_payload_view trigger_comm_view
=
171 lttng_payload_view_from_view(
172 src_view
, 0, sizeof(*trigger_comm
));
174 if (!src_view
|| !_trigger
) {
179 if (!lttng_payload_view_is_valid(&trigger_comm_view
)) {
180 /* Payload not large enough to contain the header. */
185 /* lttng_trigger_comm header */
186 trigger_comm
= (typeof(trigger_comm
)) trigger_comm_view
.buffer
.data
;
188 /* Set the trigger's creds. */
189 if (trigger_comm
->uid
> (uint64_t) ((uid_t
) -1)) {
190 /* UID out of range for this platform. */
195 LTTNG_OPTIONAL_SET(&creds
.uid
, trigger_comm
->uid
);
197 offset
+= sizeof(*trigger_comm
);
199 firing_policy
= trigger_comm
->firing_policy_type
;
200 if (!is_firing_policy_valid(firing_policy
)) {
205 firing_policy_threshold
= trigger_comm
->firing_policy_threshold
;
206 if (trigger_comm
->name_length
!= 0) {
208 const struct lttng_payload_view name_view
=
209 lttng_payload_view_from_view(
211 trigger_comm
->name_length
);
213 if (!lttng_payload_view_is_valid(&name_view
)) {
218 name
= name_view
.buffer
.data
;
219 if (!lttng_buffer_view_contains_string(&name_view
.buffer
, name
,
220 trigger_comm
->name_length
)) {
225 offset
+= trigger_comm
->name_length
;
226 name_size
= trigger_comm
->name_length
;
230 /* struct lttng_condition */
231 struct lttng_payload_view condition_view
=
232 lttng_payload_view_from_view(
233 src_view
, offset
, -1);
235 condition_size
= lttng_condition_create_from_payload(&condition_view
,
239 if (condition_size
< 0) {
240 ret
= condition_size
;
244 offset
+= condition_size
;
246 /* struct lttng_action */
247 struct lttng_payload_view action_view
=
248 lttng_payload_view_from_view(
249 src_view
, offset
, -1);
251 action_size
= lttng_action_create_from_payload(&action_view
, &action
);
254 if (action_size
< 0) {
258 offset
+= action_size
;
260 /* Unexpected size of inner-elements; the buffer is corrupted. */
261 if ((ssize_t
) trigger_comm
->length
!= condition_size
+ action_size
+ name_size
) {
266 trigger
= lttng_trigger_create(condition
, action
);
272 lttng_trigger_set_credentials(trigger
, &creds
);
275 * The trigger object owns references to the action and condition
278 lttng_condition_put(condition
);
281 lttng_action_put(action
);
285 const enum lttng_trigger_status status
=
286 lttng_trigger_set_name(trigger
, name
);
288 if (status
!= LTTNG_TRIGGER_STATUS_OK
) {
294 /* Set the policy. */
296 const enum lttng_trigger_status status
=
297 lttng_trigger_set_firing_policy(trigger
,
299 firing_policy_threshold
);
301 if (status
!= LTTNG_TRIGGER_STATUS_OK
) {
310 lttng_condition_put(condition
);
311 lttng_action_put(action
);
316 lttng_trigger_put(trigger
);
323 * Both elements are stored contiguously, see their "*_comm" structure
324 * for the detailed format.
327 int lttng_trigger_serialize(const struct lttng_trigger
*trigger
,
328 struct lttng_payload
*payload
)
331 size_t header_offset
, size_before_payload
, size_name
;
332 struct lttng_trigger_comm trigger_comm
= {};
333 struct lttng_trigger_comm
*header
;
334 const struct lttng_credentials
*creds
= NULL
;
336 creds
= lttng_trigger_get_credentials(trigger
);
339 trigger_comm
.uid
= LTTNG_OPTIONAL_GET(creds
->uid
);
341 if (trigger
->name
!= NULL
) {
342 size_name
= strlen(trigger
->name
) + 1;
347 trigger_comm
.name_length
= size_name
;
348 trigger_comm
.firing_policy_type
= (uint8_t) trigger
->firing_policy
.type
;
349 trigger_comm
.firing_policy_threshold
= (uint64_t) trigger
->firing_policy
.threshold
;
351 header_offset
= payload
->buffer
.size
;
352 ret
= lttng_dynamic_buffer_append(&payload
->buffer
, &trigger_comm
,
353 sizeof(trigger_comm
));
358 size_before_payload
= payload
->buffer
.size
;
361 ret
= lttng_dynamic_buffer_append(
362 &payload
->buffer
, trigger
->name
, size_name
);
367 ret
= lttng_condition_serialize(trigger
->condition
, payload
);
372 ret
= lttng_action_serialize(trigger
->action
, payload
);
377 /* Update payload size. */
378 header
= (typeof(header
)) (payload
->buffer
.data
+ header_offset
);
379 header
->length
= payload
->buffer
.size
- size_before_payload
;
385 bool lttng_trigger_is_equal(
386 const struct lttng_trigger
*a
, const struct lttng_trigger
*b
)
388 if (a
->firing_policy
.type
!= b
->firing_policy
.type
) {
392 if (a
->firing_policy
.threshold
!= b
->firing_policy
.threshold
) {
397 * Name is not taken into account since it is cosmetic only.
399 if (!lttng_condition_is_equal(a
->condition
, b
->condition
)) {
403 if (!lttng_action_is_equal(a
->action
, b
->action
)) {
407 if (!lttng_credentials_is_equal(lttng_trigger_get_credentials(a
),
408 lttng_trigger_get_credentials(b
))) {
415 enum lttng_trigger_status
lttng_trigger_set_name(struct lttng_trigger
*trigger
,
418 char *name_copy
= NULL
;
419 enum lttng_trigger_status status
= LTTNG_TRIGGER_STATUS_OK
;
421 if (!trigger
|| !name
||
423 status
= LTTNG_TRIGGER_STATUS_INVALID
;
427 name_copy
= strdup(name
);
429 status
= LTTNG_TRIGGER_STATUS_ERROR
;
435 trigger
->name
= name_copy
;
441 enum lttng_trigger_status
lttng_trigger_get_name(
442 const struct lttng_trigger
*trigger
, const char **name
)
444 enum lttng_trigger_status status
= LTTNG_TRIGGER_STATUS_OK
;
446 if (!trigger
|| !name
) {
447 status
= LTTNG_TRIGGER_STATUS_INVALID
;
451 if (!trigger
->name
) {
452 status
= LTTNG_TRIGGER_STATUS_UNSET
;
455 *name
= trigger
->name
;
461 int lttng_trigger_assign_name(struct lttng_trigger
*dst
,
462 const struct lttng_trigger
*src
)
465 enum lttng_trigger_status status
;
467 status
= lttng_trigger_set_name(dst
, src
->name
);
468 if (status
!= LTTNG_TRIGGER_STATUS_OK
) {
470 ERR("Failed to set name for trigger");
478 void lttng_trigger_set_tracer_token(struct lttng_trigger
*trigger
,
482 LTTNG_OPTIONAL_SET(&trigger
->tracer_token
, token
);
486 uint64_t lttng_trigger_get_tracer_token(const struct lttng_trigger
*trigger
)
490 return LTTNG_OPTIONAL_GET(trigger
->tracer_token
);
494 int lttng_trigger_generate_name(struct lttng_trigger
*trigger
,
498 char *generated_name
= NULL
;
500 ret
= asprintf(&generated_name
, "T%" PRIu64
"", unique_id
);
502 ERR("Failed to generate trigger name");
509 trigger
->name
= generated_name
;
515 void lttng_trigger_get(struct lttng_trigger
*trigger
)
517 urcu_ref_get(&trigger
->ref
);
521 void lttng_trigger_put(struct lttng_trigger
*trigger
)
527 urcu_ref_put(&trigger
->ref
, trigger_destroy_ref
);
530 static void delete_trigger_array_element(void *ptr
)
532 struct lttng_trigger
*trigger
= ptr
;
534 lttng_trigger_put(trigger
);
538 struct lttng_triggers
*lttng_triggers_create(void)
540 struct lttng_triggers
*triggers
= NULL
;
542 triggers
= zmalloc(sizeof(*triggers
));
547 lttng_dynamic_pointer_array_init(&triggers
->array
, delete_trigger_array_element
);
554 struct lttng_trigger
*lttng_triggers_borrow_mutable_at_index(
555 const struct lttng_triggers
*triggers
, unsigned int index
)
557 struct lttng_trigger
*trigger
= NULL
;
560 if (index
>= lttng_dynamic_pointer_array_get_count(&triggers
->array
)) {
564 trigger
= (struct lttng_trigger
*)
565 lttng_dynamic_pointer_array_get_pointer(
566 &triggers
->array
, index
);
572 int lttng_triggers_add(
573 struct lttng_triggers
*triggers
, struct lttng_trigger
*trigger
)
580 lttng_trigger_get(trigger
);
582 ret
= lttng_dynamic_pointer_array_add_pointer(&triggers
->array
, trigger
);
584 lttng_trigger_put(trigger
);
590 const struct lttng_trigger
*lttng_triggers_get_at_index(
591 const struct lttng_triggers
*triggers
, unsigned int index
)
593 return lttng_triggers_borrow_mutable_at_index(triggers
, index
);
596 enum lttng_trigger_status
lttng_triggers_get_count(const struct lttng_triggers
*triggers
, unsigned int *count
)
598 enum lttng_trigger_status status
= LTTNG_TRIGGER_STATUS_OK
;
600 if (!triggers
|| !count
) {
601 status
= LTTNG_TRIGGER_STATUS_INVALID
;
605 *count
= lttng_dynamic_pointer_array_get_count(&triggers
->array
);
610 void lttng_triggers_destroy(struct lttng_triggers
*triggers
)
616 lttng_dynamic_pointer_array_reset(&triggers
->array
);
620 int lttng_triggers_serialize(const struct lttng_triggers
*triggers
,
621 struct lttng_payload
*payload
)
624 unsigned int i
, count
;
625 size_t size_before_payload
;
626 struct lttng_triggers_comm triggers_comm
= {};
627 struct lttng_triggers_comm
*header
;
628 enum lttng_trigger_status status
;
629 const size_t header_offset
= payload
->buffer
.size
;
631 status
= lttng_triggers_get_count(triggers
, &count
);
632 if (status
!= LTTNG_TRIGGER_STATUS_OK
) {
633 ret
= LTTNG_ERR_INVALID
;
637 triggers_comm
.count
= count
;
639 /* Placeholder header; updated at the end. */
640 ret
= lttng_dynamic_buffer_append(&payload
->buffer
, &triggers_comm
,
641 sizeof(triggers_comm
));
646 size_before_payload
= payload
->buffer
.size
;
648 for (i
= 0; i
< count
; i
++) {
649 const struct lttng_trigger
*trigger
=
650 lttng_triggers_get_at_index(triggers
, i
);
654 ret
= lttng_trigger_serialize(trigger
, payload
);
660 /* Update payload size. */
661 header
= (struct lttng_triggers_comm
*) ((char *) payload
->buffer
.data
+ header_offset
);
662 header
->length
= payload
->buffer
.size
- size_before_payload
;
668 ssize_t
lttng_triggers_create_from_payload(
669 struct lttng_payload_view
*src_view
,
670 struct lttng_triggers
**triggers
)
672 ssize_t ret
, offset
= 0, triggers_size
= 0;
674 const struct lttng_triggers_comm
*triggers_comm
;
675 struct lttng_triggers
*local_triggers
= NULL
;
677 if (!src_view
|| !triggers
) {
682 /* lttng_trigger_comms header */
683 triggers_comm
= (const struct lttng_triggers_comm
*) src_view
->buffer
.data
;
684 offset
+= sizeof(*triggers_comm
);
686 local_triggers
= lttng_triggers_create();
687 if (!local_triggers
) {
692 for (i
= 0; i
< triggers_comm
->count
; i
++) {
693 struct lttng_trigger
*trigger
= NULL
;
694 struct lttng_payload_view trigger_view
=
695 lttng_payload_view_from_view(src_view
, offset
, -1);
696 ssize_t trigger_size
;
698 trigger_size
= lttng_trigger_create_from_payload(
699 &trigger_view
, &trigger
);
700 if (trigger_size
< 0) {
705 /* Transfer ownership of the trigger to the collection. */
706 ret
= lttng_triggers_add(local_triggers
, trigger
);
707 lttng_trigger_put(trigger
);
713 offset
+= trigger_size
;
714 triggers_size
+= trigger_size
;
717 /* Unexpected size of inner-elements; the buffer is corrupted. */
718 if ((ssize_t
) triggers_comm
->length
!= triggers_size
) {
723 /* Pass ownership to caller. */
724 *triggers
= local_triggers
;
725 local_triggers
= NULL
;
730 lttng_triggers_destroy(local_triggers
);
735 const struct lttng_credentials
*lttng_trigger_get_credentials(
736 const struct lttng_trigger
*trigger
)
738 return &trigger
->creds
;
742 void lttng_trigger_set_credentials(struct lttng_trigger
*trigger
,
743 const struct lttng_credentials
*creds
)
746 trigger
->creds
= *creds
;
749 enum lttng_trigger_status
lttng_trigger_set_owner_uid(
750 struct lttng_trigger
*trigger
, uid_t uid
)
752 enum lttng_trigger_status ret
= LTTNG_TRIGGER_STATUS_OK
;
753 const struct lttng_credentials creds
= {
754 .uid
= LTTNG_OPTIONAL_INIT_VALUE(uid
),
755 .gid
= LTTNG_OPTIONAL_INIT_UNSET
,
759 ret
= LTTNG_TRIGGER_STATUS_INVALID
;
763 /* Client-side validation only to report a clearer error. */
764 if (geteuid() != 0) {
765 ret
= LTTNG_TRIGGER_STATUS_PERMISSION_DENIED
;
769 lttng_trigger_set_credentials(trigger
, &creds
);
775 enum lttng_trigger_status
lttng_trigger_get_owner_uid(
776 const struct lttng_trigger
*trigger
, uid_t
*uid
)
778 enum lttng_trigger_status ret
= LTTNG_TRIGGER_STATUS_OK
;
779 const struct lttng_credentials
*creds
= NULL
;
781 if (!trigger
|| !uid
) {
782 ret
= LTTNG_TRIGGER_STATUS_INVALID
;
786 if (!trigger
->creds
.uid
.is_set
) {
787 ret
= LTTNG_TRIGGER_STATUS_UNSET
;
791 creds
= lttng_trigger_get_credentials(trigger
);
792 *uid
= lttng_credentials_get_uid(creds
);
798 enum lttng_trigger_status
lttng_trigger_set_firing_policy(
799 struct lttng_trigger
*trigger
,
800 enum lttng_trigger_firing_policy policy_type
,
803 enum lttng_trigger_status ret
= LTTNG_TRIGGER_STATUS_OK
;
807 ret
= LTTNG_TRIGGER_STATUS_INVALID
;
811 trigger
->firing_policy
.type
= policy_type
;
812 trigger
->firing_policy
.threshold
= threshold
;
818 enum lttng_trigger_status
lttng_trigger_get_firing_policy(
819 const struct lttng_trigger
*trigger
,
820 enum lttng_trigger_firing_policy
*policy_type
,
823 enum lttng_trigger_status status
= LTTNG_TRIGGER_STATUS_OK
;
825 if (!trigger
|| !policy_type
|| !threshold
) {
826 status
= LTTNG_TRIGGER_STATUS_INVALID
;
830 *policy_type
= trigger
->firing_policy
.type
;
831 *threshold
= trigger
->firing_policy
.threshold
;
838 bool lttng_trigger_should_fire(const struct lttng_trigger
*trigger
)
840 bool ready_to_fire
= false;
844 switch (trigger
->firing_policy
.type
) {
845 case LTTNG_TRIGGER_FIRING_POLICY_EVERY_N
:
846 if (trigger
->firing_policy
.current_count
< trigger
->firing_policy
.threshold
) {
847 ready_to_fire
= true;
850 case LTTNG_TRIGGER_FIRING_POLICY_ONCE_AFTER_N
:
851 if (trigger
->firing_policy
.current_count
< trigger
->firing_policy
.threshold
) {
852 ready_to_fire
= true;
859 return ready_to_fire
;
863 void lttng_trigger_fire(struct lttng_trigger
*trigger
)
867 trigger
->firing_policy
.current_count
++;
869 switch (trigger
->firing_policy
.type
) {
870 case LTTNG_TRIGGER_FIRING_POLICY_EVERY_N
:
871 if (trigger
->firing_policy
.current_count
== trigger
->firing_policy
.threshold
) {
872 trigger
->firing_policy
.current_count
= 0;
876 case LTTNG_TRIGGER_FIRING_POLICY_ONCE_AFTER_N
:
879 * As an optimisation, deactivate the trigger condition and
880 * remove any checks in the traced application or kernel since
881 * the trigger will never fire again.
890 enum lttng_domain_type
lttng_trigger_get_underlying_domain_type_restriction(
891 const struct lttng_trigger
*trigger
)
893 enum lttng_domain_type type
= LTTNG_DOMAIN_NONE
;
894 const struct lttng_event_rule
*event_rule
;
895 enum lttng_condition_status c_status
;
896 enum lttng_condition_type c_type
;
899 assert(trigger
->condition
);
901 c_type
= lttng_condition_get_type(trigger
->condition
);
902 assert (c_type
!= LTTNG_CONDITION_TYPE_UNKNOWN
);
905 case LTTNG_CONDITION_TYPE_SESSION_CONSUMED_SIZE
:
906 case LTTNG_CONDITION_TYPE_SESSION_ROTATION_ONGOING
:
907 case LTTNG_CONDITION_TYPE_SESSION_ROTATION_COMPLETED
:
908 /* Apply to any domain. */
909 type
= LTTNG_DOMAIN_NONE
;
911 case LTTNG_CONDITION_TYPE_EVENT_RULE_HIT
:
912 /* Return the domain of the event rule. */
913 c_status
= lttng_condition_event_rule_get_rule(
914 trigger
->condition
, &event_rule
);
915 assert(c_status
== LTTNG_CONDITION_STATUS_OK
);
916 type
= lttng_event_rule_get_domain_type(event_rule
);
918 case LTTNG_CONDITION_TYPE_BUFFER_USAGE_HIGH
:
919 case LTTNG_CONDITION_TYPE_BUFFER_USAGE_LOW
:
920 /* Return the domain of the channel being monitored. */
921 c_status
= lttng_condition_buffer_usage_get_domain_type(
922 trigger
->condition
, &type
);
923 assert(c_status
== LTTNG_CONDITION_STATUS_OK
);