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/action/action-internal.h>
11 #include <common/credentials.h>
12 #include <common/payload.h>
13 #include <common/payload-view.h>
14 #include <common/error.h>
15 #include <common/dynamic-array.h>
16 #include <common/optional.h>
21 bool lttng_trigger_validate(struct lttng_trigger
*trigger
)
30 if (!trigger
->creds
.uid
.is_set
) {
35 valid
= lttng_condition_validate(trigger
->condition
) &&
36 lttng_action_validate(trigger
->action
);
41 struct lttng_trigger
*lttng_trigger_create(
42 struct lttng_condition
*condition
,
43 struct lttng_action
*action
)
45 struct lttng_trigger
*trigger
= NULL
;
47 if (!condition
|| !action
) {
51 trigger
= zmalloc(sizeof(struct lttng_trigger
));
56 urcu_ref_init(&trigger
->ref
);
58 lttng_condition_get(condition
);
59 trigger
->condition
= condition
;
61 lttng_action_get(action
);
62 trigger
->action
= action
;
69 * Note: the lack of reference counting 'get' on the condition object is normal.
70 * This API was exposed as such in 2.11. The client is not expected to call
71 * lttng_condition_destroy on the returned object.
73 struct lttng_condition
*lttng_trigger_get_condition(
74 struct lttng_trigger
*trigger
)
76 return trigger
? trigger
->condition
: NULL
;
80 const struct lttng_condition
*lttng_trigger_get_const_condition(
81 const struct lttng_trigger
*trigger
)
83 return trigger
->condition
;
88 * Note: the lack of reference counting 'get' on the action object is normal.
89 * This API was exposed as such in 2.11. The client is not expected to call
90 * lttng_action_destroy on the returned object.
92 struct lttng_action
*lttng_trigger_get_action(
93 struct lttng_trigger
*trigger
)
95 return trigger
? trigger
->action
: NULL
;
99 const struct lttng_action
*lttng_trigger_get_const_action(
100 const struct lttng_trigger
*trigger
)
102 return trigger
->action
;
105 static void trigger_destroy_ref(struct urcu_ref
*ref
)
107 struct lttng_trigger
*trigger
=
108 container_of(ref
, struct lttng_trigger
, ref
);
109 struct lttng_action
*action
= lttng_trigger_get_action(trigger
);
110 struct lttng_condition
*condition
=
111 lttng_trigger_get_condition(trigger
);
116 /* Release ownership. */
117 lttng_action_put(action
);
118 lttng_condition_put(condition
);
124 void lttng_trigger_destroy(struct lttng_trigger
*trigger
)
126 lttng_trigger_put(trigger
);
130 ssize_t
lttng_trigger_create_from_payload(
131 struct lttng_payload_view
*src_view
,
132 struct lttng_trigger
**trigger
)
134 ssize_t ret
, offset
= 0, condition_size
, action_size
, name_size
= 0;
135 struct lttng_condition
*condition
= NULL
;
136 struct lttng_action
*action
= NULL
;
137 const struct lttng_trigger_comm
*trigger_comm
;
138 const char *name
= NULL
;
139 struct lttng_credentials creds
= {
140 .uid
= LTTNG_OPTIONAL_INIT_UNSET
,
141 .gid
= LTTNG_OPTIONAL_INIT_UNSET
,
144 if (!src_view
|| !trigger
) {
149 /* lttng_trigger_comm header */
150 trigger_comm
= (typeof(trigger_comm
)) src_view
->buffer
.data
;
152 /* Set the trigger's creds. */
153 if (trigger_comm
->uid
> (uint64_t) ((uid_t
) -1)) {
154 /* UID out of range for this platform. */
159 LTTNG_OPTIONAL_SET(&creds
.uid
, trigger_comm
->uid
);
161 offset
+= sizeof(*trigger_comm
);
163 if (trigger_comm
->name_length
!= 0) {
165 const struct lttng_payload_view name_view
=
166 lttng_payload_view_from_view(
167 src_view
, offset
, trigger_comm
->name_length
);
169 name
= name_view
.buffer
.data
;
170 if (!lttng_buffer_view_contains_string(&name_view
.buffer
, name
,
171 trigger_comm
->name_length
)) {
176 offset
+= trigger_comm
->name_length
;
177 name_size
= trigger_comm
->name_length
;
181 /* struct lttng_condition */
182 struct lttng_payload_view condition_view
=
183 lttng_payload_view_from_view(
184 src_view
, offset
, -1);
186 condition_size
= lttng_condition_create_from_payload(&condition_view
,
190 if (condition_size
< 0) {
191 ret
= condition_size
;
195 offset
+= condition_size
;
197 /* struct lttng_action */
198 struct lttng_payload_view action_view
=
199 lttng_payload_view_from_view(
200 src_view
, offset
, -1);
202 action_size
= lttng_action_create_from_payload(&action_view
, &action
);
205 if (action_size
< 0) {
209 offset
+= action_size
;
211 /* Unexpected size of inner-elements; the buffer is corrupted. */
212 if ((ssize_t
) trigger_comm
->length
!= condition_size
+ action_size
+ name_size
) {
217 *trigger
= lttng_trigger_create(condition
, action
);
223 lttng_trigger_set_credentials(*trigger
, &creds
);
226 * The trigger object owns references to the action and condition
229 lttng_condition_put(condition
);
232 lttng_action_put(action
);
236 const enum lttng_trigger_status status
=
237 lttng_trigger_set_name(*trigger
, name
);
239 if (status
!= LTTNG_TRIGGER_STATUS_OK
) {
248 lttng_condition_destroy(condition
);
249 lttng_action_destroy(action
);
255 * Both elements are stored contiguously, see their "*_comm" structure
256 * for the detailed format.
259 int lttng_trigger_serialize(const struct lttng_trigger
*trigger
,
260 struct lttng_payload
*payload
)
263 size_t header_offset
, size_before_payload
, size_name
;
264 struct lttng_trigger_comm trigger_comm
= {};
265 struct lttng_trigger_comm
*header
;
266 const struct lttng_credentials
*creds
= NULL
;
268 creds
= lttng_trigger_get_credentials(trigger
);
271 trigger_comm
.uid
= LTTNG_OPTIONAL_GET(creds
->uid
);
273 if (trigger
->name
!= NULL
) {
274 size_name
= strlen(trigger
->name
) + 1;
279 trigger_comm
.name_length
= size_name
;
281 header_offset
= payload
->buffer
.size
;
282 ret
= lttng_dynamic_buffer_append(&payload
->buffer
, &trigger_comm
,
283 sizeof(trigger_comm
));
288 size_before_payload
= payload
->buffer
.size
;
291 ret
= lttng_dynamic_buffer_append(
292 &payload
->buffer
, trigger
->name
, size_name
);
297 ret
= lttng_condition_serialize(trigger
->condition
, payload
);
302 ret
= lttng_action_serialize(trigger
->action
, payload
);
307 /* Update payload size. */
308 header
= (typeof(header
)) (payload
->buffer
.data
+ header_offset
);
309 header
->length
= payload
->buffer
.size
- size_before_payload
;
315 bool lttng_trigger_is_equal(
316 const struct lttng_trigger
*a
, const struct lttng_trigger
*b
)
319 * Name is not taken into account since it is cosmetic only.
321 if (!lttng_condition_is_equal(a
->condition
, b
->condition
)) {
325 if (!lttng_action_is_equal(a
->action
, b
->action
)) {
329 if (!lttng_credentials_is_equal(lttng_trigger_get_credentials(a
),
330 lttng_trigger_get_credentials(b
))) {
337 enum lttng_trigger_status
lttng_trigger_set_name(struct lttng_trigger
*trigger
,
340 char *name_copy
= NULL
;
341 enum lttng_trigger_status status
= LTTNG_TRIGGER_STATUS_OK
;
343 if (!trigger
|| !name
||
345 status
= LTTNG_TRIGGER_STATUS_INVALID
;
349 name_copy
= strdup(name
);
351 status
= LTTNG_TRIGGER_STATUS_ERROR
;
357 trigger
->name
= name_copy
;
363 enum lttng_trigger_status
lttng_trigger_get_name(
364 const struct lttng_trigger
*trigger
, const char **name
)
366 enum lttng_trigger_status status
= LTTNG_TRIGGER_STATUS_OK
;
368 if (!trigger
|| !name
) {
369 status
= LTTNG_TRIGGER_STATUS_INVALID
;
373 if (!trigger
->name
) {
374 status
= LTTNG_TRIGGER_STATUS_UNSET
;
377 *name
= trigger
->name
;
383 int lttng_trigger_assign_name(struct lttng_trigger
*dst
,
384 const struct lttng_trigger
*src
)
387 enum lttng_trigger_status status
;
389 status
= lttng_trigger_set_name(dst
, src
->name
);
390 if (status
!= LTTNG_TRIGGER_STATUS_OK
) {
392 ERR("Failed to set name for trigger");
400 void lttng_trigger_set_tracer_token(struct lttng_trigger
*trigger
,
404 LTTNG_OPTIONAL_SET(&trigger
->tracer_token
, token
);
408 uint64_t lttng_trigger_get_tracer_token(const struct lttng_trigger
*trigger
)
412 return LTTNG_OPTIONAL_GET(trigger
->tracer_token
);
416 int lttng_trigger_generate_name(struct lttng_trigger
*trigger
,
420 char *generated_name
= NULL
;
422 ret
= asprintf(&generated_name
, "T%" PRIu64
"", unique_id
);
424 ERR("Failed to generate trigger name");
431 trigger
->name
= generated_name
;
437 void lttng_trigger_get(struct lttng_trigger
*trigger
)
439 urcu_ref_get(&trigger
->ref
);
443 void lttng_trigger_put(struct lttng_trigger
*trigger
)
449 urcu_ref_put(&trigger
->ref
, trigger_destroy_ref
);
452 static void delete_trigger_array_element(void *ptr
)
454 struct lttng_trigger
*trigger
= ptr
;
456 lttng_trigger_put(trigger
);
460 struct lttng_triggers
*lttng_triggers_create(void)
462 struct lttng_triggers
*triggers
= NULL
;
464 triggers
= zmalloc(sizeof(*triggers
));
469 lttng_dynamic_pointer_array_init(&triggers
->array
, delete_trigger_array_element
);
476 struct lttng_trigger
*lttng_triggers_borrow_mutable_at_index(
477 const struct lttng_triggers
*triggers
, unsigned int index
)
479 struct lttng_trigger
*trigger
= NULL
;
482 if (index
>= lttng_dynamic_pointer_array_get_count(&triggers
->array
)) {
486 trigger
= (struct lttng_trigger
*)
487 lttng_dynamic_pointer_array_get_pointer(
488 &triggers
->array
, index
);
494 int lttng_triggers_add(
495 struct lttng_triggers
*triggers
, struct lttng_trigger
*trigger
)
502 lttng_trigger_get(trigger
);
504 ret
= lttng_dynamic_pointer_array_add_pointer(&triggers
->array
, trigger
);
506 lttng_trigger_put(trigger
);
512 const struct lttng_trigger
*lttng_triggers_get_at_index(
513 const struct lttng_triggers
*triggers
, unsigned int index
)
515 return lttng_triggers_borrow_mutable_at_index(triggers
, index
);
518 enum lttng_trigger_status
lttng_triggers_get_count(const struct lttng_triggers
*triggers
, unsigned int *count
)
520 enum lttng_trigger_status status
= LTTNG_TRIGGER_STATUS_OK
;
522 if (!triggers
|| !count
) {
523 status
= LTTNG_TRIGGER_STATUS_INVALID
;
527 *count
= lttng_dynamic_pointer_array_get_count(&triggers
->array
);
532 void lttng_triggers_destroy(struct lttng_triggers
*triggers
)
538 lttng_dynamic_pointer_array_reset(&triggers
->array
);
542 int lttng_triggers_serialize(const struct lttng_triggers
*triggers
,
543 struct lttng_payload
*payload
)
546 unsigned int i
, count
;
547 size_t size_before_payload
;
548 struct lttng_triggers_comm triggers_comm
= {};
549 struct lttng_triggers_comm
*header
;
550 enum lttng_trigger_status status
;
551 const size_t header_offset
= payload
->buffer
.size
;
553 status
= lttng_triggers_get_count(triggers
, &count
);
554 if (status
!= LTTNG_TRIGGER_STATUS_OK
) {
555 ret
= LTTNG_ERR_INVALID
;
559 triggers_comm
.count
= count
;
561 /* Placeholder header; updated at the end. */
562 ret
= lttng_dynamic_buffer_append(&payload
->buffer
, &triggers_comm
,
563 sizeof(triggers_comm
));
568 size_before_payload
= payload
->buffer
.size
;
570 for (i
= 0; i
< count
; i
++) {
571 const struct lttng_trigger
*trigger
=
572 lttng_triggers_get_at_index(triggers
, i
);
576 ret
= lttng_trigger_serialize(trigger
, payload
);
582 /* Update payload size. */
583 header
= (struct lttng_triggers_comm
*) ((char *) payload
->buffer
.data
+ header_offset
);
584 header
->length
= payload
->buffer
.size
- size_before_payload
;
590 ssize_t
lttng_triggers_create_from_payload(
591 struct lttng_payload_view
*src_view
,
592 struct lttng_triggers
**triggers
)
594 ssize_t ret
, offset
= 0, triggers_size
= 0;
596 const struct lttng_triggers_comm
*triggers_comm
;
597 struct lttng_triggers
*local_triggers
= NULL
;
599 if (!src_view
|| !triggers
) {
604 /* lttng_trigger_comms header */
605 triggers_comm
= (const struct lttng_triggers_comm
*) src_view
->buffer
.data
;
606 offset
+= sizeof(*triggers_comm
);
608 local_triggers
= lttng_triggers_create();
609 if (!local_triggers
) {
614 for (i
= 0; i
< triggers_comm
->count
; i
++) {
615 struct lttng_trigger
*trigger
= NULL
;
616 struct lttng_payload_view trigger_view
=
617 lttng_payload_view_from_view(src_view
, offset
, -1);
618 ssize_t trigger_size
;
620 trigger_size
= lttng_trigger_create_from_payload(
621 &trigger_view
, &trigger
);
622 if (trigger_size
< 0) {
627 /* Transfer ownership of the trigger to the collection. */
628 ret
= lttng_triggers_add(local_triggers
, trigger
);
629 lttng_trigger_put(trigger
);
635 offset
+= trigger_size
;
636 triggers_size
+= trigger_size
;
639 /* Unexpected size of inner-elements; the buffer is corrupted. */
640 if ((ssize_t
) triggers_comm
->length
!= triggers_size
) {
645 /* Pass ownership to caller. */
646 *triggers
= local_triggers
;
647 local_triggers
= NULL
;
652 lttng_triggers_destroy(local_triggers
);
657 const struct lttng_credentials
*lttng_trigger_get_credentials(
658 const struct lttng_trigger
*trigger
)
660 return &trigger
->creds
;
664 void lttng_trigger_set_credentials(struct lttng_trigger
*trigger
,
665 const struct lttng_credentials
*creds
)
668 trigger
->creds
= *creds
;
671 enum lttng_trigger_status
lttng_trigger_set_owner_uid(
672 struct lttng_trigger
*trigger
, uid_t uid
)
674 enum lttng_trigger_status ret
= LTTNG_TRIGGER_STATUS_OK
;
675 const struct lttng_credentials creds
= {
676 .uid
= LTTNG_OPTIONAL_INIT_VALUE(uid
),
677 .gid
= LTTNG_OPTIONAL_INIT_UNSET
,
681 ret
= LTTNG_TRIGGER_STATUS_INVALID
;
685 /* Client-side validation only to report a clearer error. */
686 if (geteuid() != 0) {
687 ret
= LTTNG_TRIGGER_STATUS_PERMISSION_DENIED
;
691 lttng_trigger_set_credentials(trigger
, &creds
);
697 enum lttng_trigger_status
lttng_trigger_get_owner_uid(
698 const struct lttng_trigger
*trigger
, uid_t
*uid
)
700 enum lttng_trigger_status ret
= LTTNG_TRIGGER_STATUS_OK
;
701 const struct lttng_credentials
*creds
= NULL
;
703 if (!trigger
|| !uid
) {
704 ret
= LTTNG_TRIGGER_STATUS_INVALID
;
708 if (!trigger
->creds
.uid
.is_set
) {
709 ret
= LTTNG_TRIGGER_STATUS_UNSET
;
713 creds
= lttng_trigger_get_credentials(trigger
);
714 *uid
= lttng_credentials_get_uid(creds
);