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/optional.h>
19 bool lttng_trigger_validate(struct lttng_trigger
*trigger
)
28 valid
= lttng_condition_validate(trigger
->condition
) &&
29 lttng_action_validate(trigger
->action
);
34 struct lttng_trigger
*lttng_trigger_create(
35 struct lttng_condition
*condition
,
36 struct lttng_action
*action
)
38 struct lttng_trigger
*trigger
= NULL
;
40 if (!condition
|| !action
) {
44 trigger
= zmalloc(sizeof(struct lttng_trigger
));
49 trigger
->condition
= condition
;
50 trigger
->action
= action
;
56 struct lttng_condition
*lttng_trigger_get_condition(
57 struct lttng_trigger
*trigger
)
59 return trigger
? trigger
->condition
: NULL
;
63 const struct lttng_condition
*lttng_trigger_get_const_condition(
64 const struct lttng_trigger
*trigger
)
66 return trigger
->condition
;
69 struct lttng_action
*lttng_trigger_get_action(
70 struct lttng_trigger
*trigger
)
72 return trigger
? trigger
->action
: NULL
;
76 const struct lttng_action
*lttng_trigger_get_const_action(
77 const struct lttng_trigger
*trigger
)
79 return trigger
->action
;
82 void lttng_trigger_destroy(struct lttng_trigger
*trigger
)
92 ssize_t
lttng_trigger_create_from_payload(
93 struct lttng_payload_view
*src_view
,
94 struct lttng_trigger
**trigger
)
96 ssize_t ret
, offset
= 0, condition_size
, action_size
;
97 struct lttng_condition
*condition
= NULL
;
98 struct lttng_action
*action
= NULL
;
99 const struct lttng_trigger_comm
*trigger_comm
;
101 if (!src_view
|| !trigger
) {
106 /* lttng_trigger_comm header */
107 trigger_comm
= (typeof(trigger_comm
)) src_view
->buffer
.data
;
108 offset
+= sizeof(*trigger_comm
);
110 /* struct lttng_condition */
111 struct lttng_payload_view condition_view
=
112 lttng_payload_view_from_view(
113 src_view
, offset
, -1);
115 condition_size
= lttng_condition_create_from_payload(&condition_view
,
119 if (condition_size
< 0) {
120 ret
= condition_size
;
124 offset
+= condition_size
;
126 /* struct lttng_action */
127 struct lttng_payload_view action_view
=
128 lttng_payload_view_from_view(
129 src_view
, offset
, -1);
131 action_size
= lttng_action_create_from_payload(&action_view
, &action
);
134 if (action_size
< 0) {
138 offset
+= action_size
;
140 /* Unexpected size of inner-elements; the buffer is corrupted. */
141 if ((ssize_t
) trigger_comm
->length
!= condition_size
+ action_size
) {
146 *trigger
= lttng_trigger_create(condition
, action
);
156 lttng_condition_destroy(condition
);
157 lttng_action_destroy(action
);
162 * Both elements are stored contiguously, see their "*_comm" structure
163 * for the detailed format.
166 int lttng_trigger_serialize(struct lttng_trigger
*trigger
,
167 struct lttng_payload
*payload
)
170 size_t header_offset
, size_before_payload
;
171 struct lttng_trigger_comm trigger_comm
= {};
172 struct lttng_trigger_comm
*header
;
174 header_offset
= payload
->buffer
.size
;
175 ret
= lttng_dynamic_buffer_append(&payload
->buffer
, &trigger_comm
,
176 sizeof(trigger_comm
));
181 size_before_payload
= payload
->buffer
.size
;
182 ret
= lttng_condition_serialize(trigger
->condition
, payload
);
187 ret
= lttng_action_serialize(trigger
->action
, payload
);
192 /* Update payload size. */
193 header
= (typeof(header
)) (payload
->buffer
.data
+ header_offset
);
194 header
->length
= payload
->buffer
.size
- size_before_payload
;
200 const struct lttng_credentials
*lttng_trigger_get_credentials(
201 const struct lttng_trigger
*trigger
)
203 return LTTNG_OPTIONAL_GET_PTR(trigger
->creds
);
207 void lttng_trigger_set_credentials(
208 struct lttng_trigger
*trigger
,
209 const struct lttng_credentials
*creds
)
212 LTTNG_OPTIONAL_SET(&trigger
->creds
, *creds
);