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/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/event-expr-internal.h>
23 #include <lttng/event-rule/event-rule-internal.h>
24 #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
= 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
);
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
);
139 ssize_t
lttng_trigger_create_from_payload(
140 struct lttng_payload_view
*src_view
,
141 struct lttng_trigger
**_trigger
)
143 ssize_t ret
, offset
= 0, condition_size
, action_size
, name_size
= 0;
144 struct lttng_condition
*condition
= NULL
;
145 struct lttng_action
*action
= NULL
;
146 const struct lttng_trigger_comm
*trigger_comm
;
147 const char *name
= NULL
;
148 struct lttng_credentials creds
= {
149 .uid
= LTTNG_OPTIONAL_INIT_UNSET
,
150 .gid
= LTTNG_OPTIONAL_INIT_UNSET
,
152 struct lttng_trigger
*trigger
= NULL
;
153 const struct lttng_payload_view trigger_comm_view
=
154 lttng_payload_view_from_view(
155 src_view
, 0, sizeof(*trigger_comm
));
157 if (!src_view
|| !_trigger
) {
162 if (!lttng_payload_view_is_valid(&trigger_comm_view
)) {
163 /* Payload not large enough to contain the header. */
168 /* lttng_trigger_comm header */
169 trigger_comm
= (typeof(trigger_comm
)) trigger_comm_view
.buffer
.data
;
171 /* Set the trigger's creds. */
172 if (trigger_comm
->uid
> (uint64_t) ((uid_t
) -1)) {
173 /* UID out of range for this platform. */
178 LTTNG_OPTIONAL_SET(&creds
.uid
, trigger_comm
->uid
);
180 offset
+= sizeof(*trigger_comm
);
182 if (trigger_comm
->name_length
!= 0) {
184 const struct lttng_payload_view name_view
=
185 lttng_payload_view_from_view(
187 trigger_comm
->name_length
);
189 if (!lttng_payload_view_is_valid(&name_view
)) {
194 name
= name_view
.buffer
.data
;
195 if (!lttng_buffer_view_contains_string(&name_view
.buffer
, name
,
196 trigger_comm
->name_length
)) {
201 offset
+= trigger_comm
->name_length
;
202 name_size
= trigger_comm
->name_length
;
206 /* struct lttng_condition */
207 struct lttng_payload_view condition_view
=
208 lttng_payload_view_from_view(
209 src_view
, offset
, -1);
211 condition_size
= lttng_condition_create_from_payload(&condition_view
,
215 if (condition_size
< 0) {
216 ret
= condition_size
;
220 offset
+= condition_size
;
222 /* struct lttng_action */
223 struct lttng_payload_view action_view
=
224 lttng_payload_view_from_view(
225 src_view
, offset
, -1);
227 action_size
= lttng_action_create_from_payload(&action_view
, &action
);
230 if (action_size
< 0) {
234 offset
+= action_size
;
236 /* Unexpected size of inner-elements; the buffer is corrupted. */
237 if ((ssize_t
) trigger_comm
->length
!= condition_size
+ action_size
+ name_size
) {
242 trigger
= lttng_trigger_create(condition
, action
);
248 lttng_trigger_set_credentials(trigger
, &creds
);
251 * The trigger object owns references to the action and condition
254 lttng_condition_put(condition
);
257 lttng_action_put(action
);
261 const enum lttng_trigger_status status
=
262 lttng_trigger_set_name(trigger
, name
);
264 if (status
!= LTTNG_TRIGGER_STATUS_OK
) {
273 lttng_condition_put(condition
);
274 lttng_action_put(action
);
279 lttng_trigger_put(trigger
);
286 * Both elements are stored contiguously, see their "*_comm" structure
287 * for the detailed format.
290 int lttng_trigger_serialize(const struct lttng_trigger
*trigger
,
291 struct lttng_payload
*payload
)
294 size_t header_offset
, size_before_payload
, size_name
;
295 struct lttng_trigger_comm trigger_comm
= {};
296 struct lttng_trigger_comm
*header
;
297 const struct lttng_credentials
*creds
= NULL
;
299 creds
= lttng_trigger_get_credentials(trigger
);
302 trigger_comm
.uid
= LTTNG_OPTIONAL_GET(creds
->uid
);
304 if (trigger
->name
!= NULL
) {
305 size_name
= strlen(trigger
->name
) + 1;
310 trigger_comm
.name_length
= size_name
;
312 header_offset
= payload
->buffer
.size
;
313 ret
= lttng_dynamic_buffer_append(&payload
->buffer
, &trigger_comm
,
314 sizeof(trigger_comm
));
319 size_before_payload
= payload
->buffer
.size
;
322 ret
= lttng_dynamic_buffer_append(
323 &payload
->buffer
, trigger
->name
, size_name
);
328 ret
= lttng_condition_serialize(trigger
->condition
, payload
);
333 ret
= lttng_action_serialize(trigger
->action
, payload
);
338 /* Update payload size. */
339 header
= (typeof(header
)) (payload
->buffer
.data
+ header_offset
);
340 header
->length
= payload
->buffer
.size
- size_before_payload
;
346 bool lttng_trigger_is_equal(
347 const struct lttng_trigger
*a
, const struct lttng_trigger
*b
)
349 if (strcmp(a
->name
, b
->name
) != 0) {
353 if (!lttng_condition_is_equal(a
->condition
, b
->condition
)) {
357 if (!lttng_action_is_equal(a
->action
, b
->action
)) {
361 if (!lttng_credentials_is_equal(lttng_trigger_get_credentials(a
),
362 lttng_trigger_get_credentials(b
))) {
369 enum lttng_trigger_status
lttng_trigger_set_name(struct lttng_trigger
*trigger
,
372 char *name_copy
= NULL
;
373 enum lttng_trigger_status status
= LTTNG_TRIGGER_STATUS_OK
;
375 if (!trigger
|| !name
||
377 status
= LTTNG_TRIGGER_STATUS_INVALID
;
381 name_copy
= strdup(name
);
383 status
= LTTNG_TRIGGER_STATUS_ERROR
;
389 trigger
->name
= name_copy
;
395 enum lttng_trigger_status
lttng_trigger_get_name(
396 const struct lttng_trigger
*trigger
, const char **name
)
398 enum lttng_trigger_status status
= LTTNG_TRIGGER_STATUS_OK
;
400 if (!trigger
|| !name
) {
401 status
= LTTNG_TRIGGER_STATUS_INVALID
;
405 if (!trigger
->name
) {
406 status
= LTTNG_TRIGGER_STATUS_UNSET
;
409 *name
= trigger
->name
;
415 int lttng_trigger_assign_name(struct lttng_trigger
*dst
,
416 const struct lttng_trigger
*src
)
419 enum lttng_trigger_status status
;
421 status
= lttng_trigger_set_name(dst
, src
->name
);
422 if (status
!= LTTNG_TRIGGER_STATUS_OK
) {
424 ERR("Failed to set name for trigger");
432 void lttng_trigger_set_tracer_token(struct lttng_trigger
*trigger
,
436 LTTNG_OPTIONAL_SET(&trigger
->tracer_token
, token
);
440 uint64_t lttng_trigger_get_tracer_token(const struct lttng_trigger
*trigger
)
444 return LTTNG_OPTIONAL_GET(trigger
->tracer_token
);
448 int lttng_trigger_generate_name(struct lttng_trigger
*trigger
,
452 char *generated_name
= NULL
;
454 ret
= asprintf(&generated_name
, "T%" PRIu64
"", unique_id
);
456 ERR("Failed to generate trigger name");
463 trigger
->name
= generated_name
;
469 void lttng_trigger_get(struct lttng_trigger
*trigger
)
471 urcu_ref_get(&trigger
->ref
);
475 void lttng_trigger_put(struct lttng_trigger
*trigger
)
481 urcu_ref_put(&trigger
->ref
, trigger_destroy_ref
);
484 static void delete_trigger_array_element(void *ptr
)
486 struct lttng_trigger
*trigger
= ptr
;
488 lttng_trigger_put(trigger
);
492 struct lttng_triggers
*lttng_triggers_create(void)
494 struct lttng_triggers
*triggers
= NULL
;
496 triggers
= zmalloc(sizeof(*triggers
));
501 lttng_dynamic_pointer_array_init(&triggers
->array
, delete_trigger_array_element
);
508 struct lttng_trigger
*lttng_triggers_borrow_mutable_at_index(
509 const struct lttng_triggers
*triggers
, unsigned int index
)
511 struct lttng_trigger
*trigger
= NULL
;
514 if (index
>= lttng_dynamic_pointer_array_get_count(&triggers
->array
)) {
518 trigger
= (struct lttng_trigger
*)
519 lttng_dynamic_pointer_array_get_pointer(
520 &triggers
->array
, index
);
526 int lttng_triggers_add(
527 struct lttng_triggers
*triggers
, struct lttng_trigger
*trigger
)
534 lttng_trigger_get(trigger
);
536 ret
= lttng_dynamic_pointer_array_add_pointer(&triggers
->array
, trigger
);
538 lttng_trigger_put(trigger
);
544 const struct lttng_trigger
*lttng_triggers_get_at_index(
545 const struct lttng_triggers
*triggers
, unsigned int index
)
547 return lttng_triggers_borrow_mutable_at_index(triggers
, index
);
550 enum lttng_trigger_status
lttng_triggers_get_count(const struct lttng_triggers
*triggers
, unsigned int *count
)
552 enum lttng_trigger_status status
= LTTNG_TRIGGER_STATUS_OK
;
554 if (!triggers
|| !count
) {
555 status
= LTTNG_TRIGGER_STATUS_INVALID
;
559 *count
= lttng_dynamic_pointer_array_get_count(&triggers
->array
);
564 void lttng_triggers_destroy(struct lttng_triggers
*triggers
)
570 lttng_dynamic_pointer_array_reset(&triggers
->array
);
574 int lttng_triggers_serialize(const struct lttng_triggers
*triggers
,
575 struct lttng_payload
*payload
)
578 unsigned int i
, count
;
579 size_t size_before_payload
;
580 struct lttng_triggers_comm triggers_comm
= {};
581 struct lttng_triggers_comm
*header
;
582 enum lttng_trigger_status status
;
583 const size_t header_offset
= payload
->buffer
.size
;
585 status
= lttng_triggers_get_count(triggers
, &count
);
586 if (status
!= LTTNG_TRIGGER_STATUS_OK
) {
587 ret
= LTTNG_ERR_INVALID
;
591 triggers_comm
.count
= count
;
593 /* Placeholder header; updated at the end. */
594 ret
= lttng_dynamic_buffer_append(&payload
->buffer
, &triggers_comm
,
595 sizeof(triggers_comm
));
600 size_before_payload
= payload
->buffer
.size
;
602 for (i
= 0; i
< count
; i
++) {
603 const struct lttng_trigger
*trigger
=
604 lttng_triggers_get_at_index(triggers
, i
);
608 ret
= lttng_trigger_serialize(trigger
, payload
);
614 /* Update payload size. */
615 header
= (struct lttng_triggers_comm
*) ((char *) payload
->buffer
.data
+ header_offset
);
616 header
->length
= payload
->buffer
.size
- size_before_payload
;
622 ssize_t
lttng_triggers_create_from_payload(
623 struct lttng_payload_view
*src_view
,
624 struct lttng_triggers
**triggers
)
626 ssize_t ret
, offset
= 0, triggers_size
= 0;
628 const struct lttng_triggers_comm
*triggers_comm
;
629 struct lttng_triggers
*local_triggers
= NULL
;
631 if (!src_view
|| !triggers
) {
636 /* lttng_trigger_comms header */
637 triggers_comm
= (const struct lttng_triggers_comm
*) src_view
->buffer
.data
;
638 offset
+= sizeof(*triggers_comm
);
640 local_triggers
= lttng_triggers_create();
641 if (!local_triggers
) {
646 for (i
= 0; i
< triggers_comm
->count
; i
++) {
647 struct lttng_trigger
*trigger
= NULL
;
648 struct lttng_payload_view trigger_view
=
649 lttng_payload_view_from_view(src_view
, offset
, -1);
650 ssize_t trigger_size
;
652 trigger_size
= lttng_trigger_create_from_payload(
653 &trigger_view
, &trigger
);
654 if (trigger_size
< 0) {
659 /* Transfer ownership of the trigger to the collection. */
660 ret
= lttng_triggers_add(local_triggers
, trigger
);
661 lttng_trigger_put(trigger
);
667 offset
+= trigger_size
;
668 triggers_size
+= trigger_size
;
671 /* Unexpected size of inner-elements; the buffer is corrupted. */
672 if ((ssize_t
) triggers_comm
->length
!= triggers_size
) {
677 /* Pass ownership to caller. */
678 *triggers
= local_triggers
;
679 local_triggers
= NULL
;
684 lttng_triggers_destroy(local_triggers
);
689 const struct lttng_credentials
*lttng_trigger_get_credentials(
690 const struct lttng_trigger
*trigger
)
692 return &trigger
->creds
;
696 void lttng_trigger_set_credentials(struct lttng_trigger
*trigger
,
697 const struct lttng_credentials
*creds
)
700 trigger
->creds
= *creds
;
703 enum lttng_trigger_status
lttng_trigger_set_owner_uid(
704 struct lttng_trigger
*trigger
, uid_t uid
)
706 enum lttng_trigger_status ret
= LTTNG_TRIGGER_STATUS_OK
;
707 const uid_t euid
= geteuid();
708 const struct lttng_credentials creds
= {
709 .uid
= LTTNG_OPTIONAL_INIT_VALUE(uid
),
710 .gid
= LTTNG_OPTIONAL_INIT_UNSET
,
714 ret
= LTTNG_TRIGGER_STATUS_INVALID
;
718 /* Client-side validation only to report a clearer error. */
719 if (euid
!= 0 && euid
!= uid
) {
720 ret
= LTTNG_TRIGGER_STATUS_PERMISSION_DENIED
;
724 lttng_trigger_set_credentials(trigger
, &creds
);
730 enum lttng_trigger_status
lttng_trigger_get_owner_uid(
731 const struct lttng_trigger
*trigger
, uid_t
*uid
)
733 enum lttng_trigger_status ret
= LTTNG_TRIGGER_STATUS_OK
;
734 const struct lttng_credentials
*creds
= NULL
;
736 if (!trigger
|| !uid
) {
737 ret
= LTTNG_TRIGGER_STATUS_INVALID
;
741 if (!trigger
->creds
.uid
.is_set
) {
742 ret
= LTTNG_TRIGGER_STATUS_UNSET
;
746 creds
= lttng_trigger_get_credentials(trigger
);
747 *uid
= lttng_credentials_get_uid(creds
);
754 enum lttng_domain_type
lttng_trigger_get_underlying_domain_type_restriction(
755 const struct lttng_trigger
*trigger
)
757 enum lttng_domain_type type
= LTTNG_DOMAIN_NONE
;
758 const struct lttng_event_rule
*event_rule
;
759 enum lttng_condition_status c_status
;
760 enum lttng_condition_type c_type
;
763 assert(trigger
->condition
);
765 c_type
= lttng_condition_get_type(trigger
->condition
);
766 assert (c_type
!= LTTNG_CONDITION_TYPE_UNKNOWN
);
769 case LTTNG_CONDITION_TYPE_SESSION_CONSUMED_SIZE
:
770 case LTTNG_CONDITION_TYPE_SESSION_ROTATION_ONGOING
:
771 case LTTNG_CONDITION_TYPE_SESSION_ROTATION_COMPLETED
:
772 /* Apply to any domain. */
773 type
= LTTNG_DOMAIN_NONE
;
775 case LTTNG_CONDITION_TYPE_ON_EVENT
:
776 /* Return the domain of the event rule. */
777 c_status
= lttng_condition_on_event_get_rule(
778 trigger
->condition
, &event_rule
);
779 assert(c_status
== LTTNG_CONDITION_STATUS_OK
);
780 type
= lttng_event_rule_get_domain_type(event_rule
);
782 case LTTNG_CONDITION_TYPE_BUFFER_USAGE_HIGH
:
783 case LTTNG_CONDITION_TYPE_BUFFER_USAGE_LOW
:
784 /* Return the domain of the channel being monitored. */
785 c_status
= lttng_condition_buffer_usage_get_domain_type(
786 trigger
->condition
, &type
);
787 assert(c_status
== LTTNG_CONDITION_STATUS_OK
);
797 * Generate bytecode related to the trigger.
798 * On success LTTNG_OK. On error, returns lttng_error code.
801 enum lttng_error_code
lttng_trigger_generate_bytecode(
802 struct lttng_trigger
*trigger
,
803 const struct lttng_credentials
*creds
)
805 enum lttng_error_code ret
;
806 struct lttng_condition
*condition
= NULL
;
808 condition
= lttng_trigger_get_condition(trigger
);
810 ret
= LTTNG_ERR_INVALID_TRIGGER
;
814 switch (lttng_condition_get_type(condition
)) {
815 case LTTNG_CONDITION_TYPE_ON_EVENT
:
817 struct lttng_event_rule
*event_rule
;
818 const enum lttng_condition_status condition_status
=
819 lttng_condition_on_event_borrow_rule_mutable(
820 condition
, &event_rule
);
822 assert(condition_status
== LTTNG_CONDITION_STATUS_OK
);
824 /* Generate the filter bytecode. */
825 ret
= lttng_event_rule_generate_filter_bytecode(
827 if (ret
!= LTTNG_OK
) {
831 /* Generate the capture bytecode. */
832 ret
= lttng_condition_on_event_generate_capture_descriptor_bytecode(
834 if (ret
!= LTTNG_OK
) {
850 struct lttng_trigger
*lttng_trigger_copy(const struct lttng_trigger
*trigger
)
853 struct lttng_payload copy_buffer
;
854 struct lttng_trigger
*copy
= NULL
;
856 lttng_payload_init(©_buffer
);
858 ret
= lttng_trigger_serialize(trigger
, ©_buffer
);
864 struct lttng_payload_view view
=
865 lttng_payload_view_from_payload(
866 ©_buffer
, 0, -1);
867 ret
= lttng_trigger_create_from_payload(
876 lttng_payload_reset(©_buffer
);
881 bool lttng_trigger_needs_tracer_notifier(const struct lttng_trigger
*trigger
)
883 bool needs_tracer_notifier
= false;
884 const struct lttng_condition
*condition
=
885 lttng_trigger_get_const_condition(trigger
);
887 switch (lttng_condition_get_type(condition
)) {
888 case LTTNG_CONDITION_TYPE_ON_EVENT
:
889 needs_tracer_notifier
= true;
891 case LTTNG_CONDITION_TYPE_SESSION_CONSUMED_SIZE
:
892 case LTTNG_CONDITION_TYPE_BUFFER_USAGE_HIGH
:
893 case LTTNG_CONDITION_TYPE_BUFFER_USAGE_LOW
:
894 case LTTNG_CONDITION_TYPE_SESSION_ROTATION_ONGOING
:
895 case LTTNG_CONDITION_TYPE_SESSION_ROTATION_COMPLETED
:
897 case LTTNG_CONDITION_TYPE_UNKNOWN
:
902 return needs_tracer_notifier
;
906 void lttng_trigger_set_as_registered(struct lttng_trigger
*trigger
)
908 pthread_mutex_lock(&trigger
->lock
);
909 trigger
->registered
= true;
910 pthread_mutex_unlock(&trigger
->lock
);
914 void lttng_trigger_set_as_unregistered(struct lttng_trigger
*trigger
)
916 pthread_mutex_lock(&trigger
->lock
);
917 trigger
->registered
= false;
918 pthread_mutex_unlock(&trigger
->lock
);
922 * The trigger must be locked before calling lttng_trigger_registered.
923 * The lock is necessary since a trigger can be unregistered at anytime.
924 * Manipulations requiring that the trigger be registered must always acquire
925 * the trigger lock for the duration of the manipulation using
926 * `lttng_trigger_lock` and `lttng_trigger_unlock`.
929 bool lttng_trigger_is_registered(struct lttng_trigger
*trigger
)
931 ASSERT_LOCKED(trigger
->lock
);
932 return trigger
->registered
;
936 void lttng_trigger_lock(struct lttng_trigger
*trigger
)
938 pthread_mutex_lock(&trigger
->lock
);
942 void lttng_trigger_unlock(struct lttng_trigger
*trigger
)
944 pthread_mutex_unlock(&trigger
->lock
);