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 lttng_condition_get(condition
);
50 trigger
->condition
= condition
;
52 lttng_action_get(action
);
53 trigger
->action
= action
;
60 * Note: the lack of reference counting 'get' on the condition object is normal.
61 * This API was exposed as such in 2.11. The client is not expected to call
62 * lttng_condition_destroy on the returned object.
64 struct lttng_condition
*lttng_trigger_get_condition(
65 struct lttng_trigger
*trigger
)
67 return trigger
? trigger
->condition
: NULL
;
71 const struct lttng_condition
*lttng_trigger_get_const_condition(
72 const struct lttng_trigger
*trigger
)
74 return trigger
->condition
;
79 * Note: the lack of reference counting 'get' on the action object is normal.
80 * This API was exposed as such in 2.11. The client is not expected to call
81 * lttng_action_destroy on the returned object.
83 struct lttng_action
*lttng_trigger_get_action(
84 struct lttng_trigger
*trigger
)
86 return trigger
? trigger
->action
: NULL
;
90 const struct lttng_action
*lttng_trigger_get_const_action(
91 const struct lttng_trigger
*trigger
)
93 return trigger
->action
;
96 void lttng_trigger_destroy(struct lttng_trigger
*trigger
)
98 struct lttng_action
*action
= lttng_trigger_get_action(trigger
);
99 struct lttng_condition
*condition
=
100 lttng_trigger_get_condition(trigger
);
109 /* Release ownership. */
110 lttng_action_put(action
);
111 lttng_condition_put(condition
);
117 ssize_t
lttng_trigger_create_from_payload(
118 struct lttng_payload_view
*src_view
,
119 struct lttng_trigger
**trigger
)
121 ssize_t ret
, offset
= 0, condition_size
, action_size
;
122 struct lttng_condition
*condition
= NULL
;
123 struct lttng_action
*action
= NULL
;
124 const struct lttng_trigger_comm
*trigger_comm
;
126 if (!src_view
|| !trigger
) {
131 /* lttng_trigger_comm header */
132 trigger_comm
= (typeof(trigger_comm
)) src_view
->buffer
.data
;
133 offset
+= sizeof(*trigger_comm
);
135 /* struct lttng_condition */
136 struct lttng_payload_view condition_view
=
137 lttng_payload_view_from_view(
138 src_view
, offset
, -1);
140 condition_size
= lttng_condition_create_from_payload(&condition_view
,
144 if (condition_size
< 0) {
145 ret
= condition_size
;
149 offset
+= condition_size
;
151 /* struct lttng_action */
152 struct lttng_payload_view action_view
=
153 lttng_payload_view_from_view(
154 src_view
, offset
, -1);
156 action_size
= lttng_action_create_from_payload(&action_view
, &action
);
159 if (action_size
< 0) {
163 offset
+= action_size
;
165 /* Unexpected size of inner-elements; the buffer is corrupted. */
166 if ((ssize_t
) trigger_comm
->length
!= condition_size
+ action_size
) {
171 *trigger
= lttng_trigger_create(condition
, action
);
178 * The trigger object owns references to the action and condition
181 lttng_condition_put(condition
);
184 lttng_action_put(action
);
190 lttng_condition_destroy(condition
);
191 lttng_action_destroy(action
);
197 * Both elements are stored contiguously, see their "*_comm" structure
198 * for the detailed format.
201 int lttng_trigger_serialize(struct lttng_trigger
*trigger
,
202 struct lttng_payload
*payload
)
205 size_t header_offset
, size_before_payload
;
206 struct lttng_trigger_comm trigger_comm
= {};
207 struct lttng_trigger_comm
*header
;
209 header_offset
= payload
->buffer
.size
;
210 ret
= lttng_dynamic_buffer_append(&payload
->buffer
, &trigger_comm
,
211 sizeof(trigger_comm
));
216 size_before_payload
= payload
->buffer
.size
;
217 ret
= lttng_condition_serialize(trigger
->condition
, payload
);
222 ret
= lttng_action_serialize(trigger
->action
, payload
);
227 /* Update payload size. */
228 header
= (typeof(header
)) (payload
->buffer
.data
+ header_offset
);
229 header
->length
= payload
->buffer
.size
- size_before_payload
;
235 const struct lttng_credentials
*lttng_trigger_get_credentials(
236 const struct lttng_trigger
*trigger
)
238 return LTTNG_OPTIONAL_GET_PTR(trigger
->creds
);
242 void lttng_trigger_set_credentials(
243 struct lttng_trigger
*trigger
,
244 const struct lttng_credentials
*creds
)
247 LTTNG_OPTIONAL_SET(&trigger
->creds
, *creds
);