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
,
143 const struct lttng_payload_view trigger_comm_view
=
144 lttng_payload_view_from_view(
145 src_view
, 0, sizeof(*trigger_comm
));
147 if (!src_view
|| !trigger
) {
152 if (!lttng_payload_view_is_valid(&trigger_comm_view
)) {
153 /* Payload not large enough to contain the header. */
158 /* lttng_trigger_comm header */
159 trigger_comm
= (typeof(trigger_comm
)) trigger_comm_view
.buffer
.data
;
161 /* Set the trigger's creds. */
162 if (trigger_comm
->uid
> (uint64_t) ((uid_t
) -1)) {
163 /* UID out of range for this platform. */
168 LTTNG_OPTIONAL_SET(&creds
.uid
, trigger_comm
->uid
);
170 offset
+= sizeof(*trigger_comm
);
172 if (trigger_comm
->name_length
!= 0) {
174 const struct lttng_payload_view name_view
=
175 lttng_payload_view_from_view(
177 trigger_comm
->name_length
);
179 if (!lttng_payload_view_is_valid(&name_view
)) {
184 name
= name_view
.buffer
.data
;
185 if (!lttng_buffer_view_contains_string(&name_view
.buffer
, name
,
186 trigger_comm
->name_length
)) {
191 offset
+= trigger_comm
->name_length
;
192 name_size
= trigger_comm
->name_length
;
196 /* struct lttng_condition */
197 struct lttng_payload_view condition_view
=
198 lttng_payload_view_from_view(
199 src_view
, offset
, -1);
201 condition_size
= lttng_condition_create_from_payload(&condition_view
,
205 if (condition_size
< 0) {
206 ret
= condition_size
;
210 offset
+= condition_size
;
212 /* struct lttng_action */
213 struct lttng_payload_view action_view
=
214 lttng_payload_view_from_view(
215 src_view
, offset
, -1);
217 action_size
= lttng_action_create_from_payload(&action_view
, &action
);
220 if (action_size
< 0) {
224 offset
+= action_size
;
226 /* Unexpected size of inner-elements; the buffer is corrupted. */
227 if ((ssize_t
) trigger_comm
->length
!= condition_size
+ action_size
+ name_size
) {
232 *trigger
= lttng_trigger_create(condition
, action
);
238 lttng_trigger_set_credentials(*trigger
, &creds
);
241 * The trigger object owns references to the action and condition
244 lttng_condition_put(condition
);
247 lttng_action_put(action
);
251 const enum lttng_trigger_status status
=
252 lttng_trigger_set_name(*trigger
, name
);
254 if (status
!= LTTNG_TRIGGER_STATUS_OK
) {
263 lttng_condition_destroy(condition
);
264 lttng_action_destroy(action
);
270 * Both elements are stored contiguously, see their "*_comm" structure
271 * for the detailed format.
274 int lttng_trigger_serialize(const struct lttng_trigger
*trigger
,
275 struct lttng_payload
*payload
)
278 size_t header_offset
, size_before_payload
, size_name
;
279 struct lttng_trigger_comm trigger_comm
= {};
280 struct lttng_trigger_comm
*header
;
281 const struct lttng_credentials
*creds
= NULL
;
283 creds
= lttng_trigger_get_credentials(trigger
);
286 trigger_comm
.uid
= LTTNG_OPTIONAL_GET(creds
->uid
);
288 if (trigger
->name
!= NULL
) {
289 size_name
= strlen(trigger
->name
) + 1;
294 trigger_comm
.name_length
= size_name
;
296 header_offset
= payload
->buffer
.size
;
297 ret
= lttng_dynamic_buffer_append(&payload
->buffer
, &trigger_comm
,
298 sizeof(trigger_comm
));
303 size_before_payload
= payload
->buffer
.size
;
306 ret
= lttng_dynamic_buffer_append(
307 &payload
->buffer
, trigger
->name
, size_name
);
312 ret
= lttng_condition_serialize(trigger
->condition
, payload
);
317 ret
= lttng_action_serialize(trigger
->action
, payload
);
322 /* Update payload size. */
323 header
= (typeof(header
)) (payload
->buffer
.data
+ header_offset
);
324 header
->length
= payload
->buffer
.size
- size_before_payload
;
330 bool lttng_trigger_is_equal(
331 const struct lttng_trigger
*a
, const struct lttng_trigger
*b
)
334 * Name is not taken into account since it is cosmetic only.
336 if (!lttng_condition_is_equal(a
->condition
, b
->condition
)) {
340 if (!lttng_action_is_equal(a
->action
, b
->action
)) {
344 if (!lttng_credentials_is_equal(lttng_trigger_get_credentials(a
),
345 lttng_trigger_get_credentials(b
))) {
352 enum lttng_trigger_status
lttng_trigger_set_name(struct lttng_trigger
*trigger
,
355 char *name_copy
= NULL
;
356 enum lttng_trigger_status status
= LTTNG_TRIGGER_STATUS_OK
;
358 if (!trigger
|| !name
||
360 status
= LTTNG_TRIGGER_STATUS_INVALID
;
364 name_copy
= strdup(name
);
366 status
= LTTNG_TRIGGER_STATUS_ERROR
;
372 trigger
->name
= name_copy
;
378 enum lttng_trigger_status
lttng_trigger_get_name(
379 const struct lttng_trigger
*trigger
, const char **name
)
381 enum lttng_trigger_status status
= LTTNG_TRIGGER_STATUS_OK
;
383 if (!trigger
|| !name
) {
384 status
= LTTNG_TRIGGER_STATUS_INVALID
;
388 if (!trigger
->name
) {
389 status
= LTTNG_TRIGGER_STATUS_UNSET
;
392 *name
= trigger
->name
;
398 int lttng_trigger_assign_name(struct lttng_trigger
*dst
,
399 const struct lttng_trigger
*src
)
402 enum lttng_trigger_status status
;
404 status
= lttng_trigger_set_name(dst
, src
->name
);
405 if (status
!= LTTNG_TRIGGER_STATUS_OK
) {
407 ERR("Failed to set name for trigger");
415 void lttng_trigger_set_tracer_token(struct lttng_trigger
*trigger
,
419 LTTNG_OPTIONAL_SET(&trigger
->tracer_token
, token
);
423 uint64_t lttng_trigger_get_tracer_token(const struct lttng_trigger
*trigger
)
427 return LTTNG_OPTIONAL_GET(trigger
->tracer_token
);
431 int lttng_trigger_generate_name(struct lttng_trigger
*trigger
,
435 char *generated_name
= NULL
;
437 ret
= asprintf(&generated_name
, "T%" PRIu64
"", unique_id
);
439 ERR("Failed to generate trigger name");
446 trigger
->name
= generated_name
;
452 void lttng_trigger_get(struct lttng_trigger
*trigger
)
454 urcu_ref_get(&trigger
->ref
);
458 void lttng_trigger_put(struct lttng_trigger
*trigger
)
464 urcu_ref_put(&trigger
->ref
, trigger_destroy_ref
);
467 static void delete_trigger_array_element(void *ptr
)
469 struct lttng_trigger
*trigger
= ptr
;
471 lttng_trigger_put(trigger
);
475 struct lttng_triggers
*lttng_triggers_create(void)
477 struct lttng_triggers
*triggers
= NULL
;
479 triggers
= zmalloc(sizeof(*triggers
));
484 lttng_dynamic_pointer_array_init(&triggers
->array
, delete_trigger_array_element
);
491 struct lttng_trigger
*lttng_triggers_borrow_mutable_at_index(
492 const struct lttng_triggers
*triggers
, unsigned int index
)
494 struct lttng_trigger
*trigger
= NULL
;
497 if (index
>= lttng_dynamic_pointer_array_get_count(&triggers
->array
)) {
501 trigger
= (struct lttng_trigger
*)
502 lttng_dynamic_pointer_array_get_pointer(
503 &triggers
->array
, index
);
509 int lttng_triggers_add(
510 struct lttng_triggers
*triggers
, struct lttng_trigger
*trigger
)
517 lttng_trigger_get(trigger
);
519 ret
= lttng_dynamic_pointer_array_add_pointer(&triggers
->array
, trigger
);
521 lttng_trigger_put(trigger
);
527 const struct lttng_trigger
*lttng_triggers_get_at_index(
528 const struct lttng_triggers
*triggers
, unsigned int index
)
530 return lttng_triggers_borrow_mutable_at_index(triggers
, index
);
533 enum lttng_trigger_status
lttng_triggers_get_count(const struct lttng_triggers
*triggers
, unsigned int *count
)
535 enum lttng_trigger_status status
= LTTNG_TRIGGER_STATUS_OK
;
537 if (!triggers
|| !count
) {
538 status
= LTTNG_TRIGGER_STATUS_INVALID
;
542 *count
= lttng_dynamic_pointer_array_get_count(&triggers
->array
);
547 void lttng_triggers_destroy(struct lttng_triggers
*triggers
)
553 lttng_dynamic_pointer_array_reset(&triggers
->array
);
557 int lttng_triggers_serialize(const struct lttng_triggers
*triggers
,
558 struct lttng_payload
*payload
)
561 unsigned int i
, count
;
562 size_t size_before_payload
;
563 struct lttng_triggers_comm triggers_comm
= {};
564 struct lttng_triggers_comm
*header
;
565 enum lttng_trigger_status status
;
566 const size_t header_offset
= payload
->buffer
.size
;
568 status
= lttng_triggers_get_count(triggers
, &count
);
569 if (status
!= LTTNG_TRIGGER_STATUS_OK
) {
570 ret
= LTTNG_ERR_INVALID
;
574 triggers_comm
.count
= count
;
576 /* Placeholder header; updated at the end. */
577 ret
= lttng_dynamic_buffer_append(&payload
->buffer
, &triggers_comm
,
578 sizeof(triggers_comm
));
583 size_before_payload
= payload
->buffer
.size
;
585 for (i
= 0; i
< count
; i
++) {
586 const struct lttng_trigger
*trigger
=
587 lttng_triggers_get_at_index(triggers
, i
);
591 ret
= lttng_trigger_serialize(trigger
, payload
);
597 /* Update payload size. */
598 header
= (struct lttng_triggers_comm
*) ((char *) payload
->buffer
.data
+ header_offset
);
599 header
->length
= payload
->buffer
.size
- size_before_payload
;
605 ssize_t
lttng_triggers_create_from_payload(
606 struct lttng_payload_view
*src_view
,
607 struct lttng_triggers
**triggers
)
609 ssize_t ret
, offset
= 0, triggers_size
= 0;
611 const struct lttng_triggers_comm
*triggers_comm
;
612 struct lttng_triggers
*local_triggers
= NULL
;
614 if (!src_view
|| !triggers
) {
619 /* lttng_trigger_comms header */
620 triggers_comm
= (const struct lttng_triggers_comm
*) src_view
->buffer
.data
;
621 offset
+= sizeof(*triggers_comm
);
623 local_triggers
= lttng_triggers_create();
624 if (!local_triggers
) {
629 for (i
= 0; i
< triggers_comm
->count
; i
++) {
630 struct lttng_trigger
*trigger
= NULL
;
631 struct lttng_payload_view trigger_view
=
632 lttng_payload_view_from_view(src_view
, offset
, -1);
633 ssize_t trigger_size
;
635 trigger_size
= lttng_trigger_create_from_payload(
636 &trigger_view
, &trigger
);
637 if (trigger_size
< 0) {
642 /* Transfer ownership of the trigger to the collection. */
643 ret
= lttng_triggers_add(local_triggers
, trigger
);
644 lttng_trigger_put(trigger
);
650 offset
+= trigger_size
;
651 triggers_size
+= trigger_size
;
654 /* Unexpected size of inner-elements; the buffer is corrupted. */
655 if ((ssize_t
) triggers_comm
->length
!= triggers_size
) {
660 /* Pass ownership to caller. */
661 *triggers
= local_triggers
;
662 local_triggers
= NULL
;
667 lttng_triggers_destroy(local_triggers
);
672 const struct lttng_credentials
*lttng_trigger_get_credentials(
673 const struct lttng_trigger
*trigger
)
675 return &trigger
->creds
;
679 void lttng_trigger_set_credentials(struct lttng_trigger
*trigger
,
680 const struct lttng_credentials
*creds
)
683 trigger
->creds
= *creds
;
686 enum lttng_trigger_status
lttng_trigger_set_owner_uid(
687 struct lttng_trigger
*trigger
, uid_t uid
)
689 enum lttng_trigger_status ret
= LTTNG_TRIGGER_STATUS_OK
;
690 const struct lttng_credentials creds
= {
691 .uid
= LTTNG_OPTIONAL_INIT_VALUE(uid
),
692 .gid
= LTTNG_OPTIONAL_INIT_UNSET
,
696 ret
= LTTNG_TRIGGER_STATUS_INVALID
;
700 /* Client-side validation only to report a clearer error. */
701 if (geteuid() != 0) {
702 ret
= LTTNG_TRIGGER_STATUS_PERMISSION_DENIED
;
706 lttng_trigger_set_credentials(trigger
, &creds
);
712 enum lttng_trigger_status
lttng_trigger_get_owner_uid(
713 const struct lttng_trigger
*trigger
, uid_t
*uid
)
715 enum lttng_trigger_status ret
= LTTNG_TRIGGER_STATUS_OK
;
716 const struct lttng_credentials
*creds
= NULL
;
718 if (!trigger
|| !uid
) {
719 ret
= LTTNG_TRIGGER_STATUS_INVALID
;
723 if (!trigger
->creds
.uid
.is_set
) {
724 ret
= LTTNG_TRIGGER_STATUS_UNSET
;
728 creds
= lttng_trigger_get_credentials(trigger
);
729 *uid
= lttng_credentials_get_uid(creds
);