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/payload.h>
12 #include <common/payload-view.h>
13 #include <common/error.h>
17 bool lttng_trigger_validate(struct lttng_trigger
*trigger
)
26 valid
= lttng_condition_validate(trigger
->condition
) &&
27 lttng_action_validate(trigger
->action
);
32 struct lttng_trigger
*lttng_trigger_create(
33 struct lttng_condition
*condition
,
34 struct lttng_action
*action
)
36 struct lttng_trigger
*trigger
= NULL
;
38 if (!condition
|| !action
) {
42 trigger
= zmalloc(sizeof(struct lttng_trigger
));
47 trigger
->condition
= condition
;
48 trigger
->action
= action
;
53 struct lttng_condition
*lttng_trigger_get_condition(
54 struct lttng_trigger
*trigger
)
56 return trigger
? trigger
->condition
: NULL
;
60 const struct lttng_condition
*lttng_trigger_get_const_condition(
61 const struct lttng_trigger
*trigger
)
63 return trigger
->condition
;
66 struct lttng_action
*lttng_trigger_get_action(
67 struct lttng_trigger
*trigger
)
69 return trigger
? trigger
->action
: NULL
;
73 const struct lttng_action
*lttng_trigger_get_const_action(
74 const struct lttng_trigger
*trigger
)
76 return trigger
->action
;
79 void lttng_trigger_destroy(struct lttng_trigger
*trigger
)
89 ssize_t
lttng_trigger_create_from_payload(
90 struct lttng_payload_view
*src_view
,
91 struct lttng_trigger
**trigger
)
93 ssize_t ret
, offset
= 0, condition_size
, action_size
;
94 struct lttng_condition
*condition
= NULL
;
95 struct lttng_action
*action
= NULL
;
96 const struct lttng_trigger_comm
*trigger_comm
;
98 if (!src_view
|| !trigger
) {
103 /* lttng_trigger_comm header */
104 trigger_comm
= (typeof(trigger_comm
)) src_view
->buffer
.data
;
105 offset
+= sizeof(*trigger_comm
);
107 /* struct lttng_condition */
108 struct lttng_payload_view condition_view
=
109 lttng_payload_view_from_view(
110 src_view
, offset
, -1);
112 condition_size
= lttng_condition_create_from_payload(&condition_view
,
116 if (condition_size
< 0) {
117 ret
= condition_size
;
121 offset
+= condition_size
;
123 /* struct lttng_action */
124 struct lttng_payload_view action_view
=
125 lttng_payload_view_from_view(
126 src_view
, offset
, -1);
128 action_size
= lttng_action_create_from_payload(&action_view
, &action
);
131 if (action_size
< 0) {
135 offset
+= action_size
;
137 /* Unexpected size of inner-elements; the buffer is corrupted. */
138 if ((ssize_t
) trigger_comm
->length
!= condition_size
+ action_size
) {
143 *trigger
= lttng_trigger_create(condition
, action
);
153 lttng_condition_destroy(condition
);
154 lttng_action_destroy(action
);
159 * Both elements are stored contiguously, see their "*_comm" structure
160 * for the detailed format.
163 int lttng_trigger_serialize(struct lttng_trigger
*trigger
,
164 struct lttng_payload
*payload
)
167 size_t header_offset
, size_before_payload
;
168 struct lttng_trigger_comm trigger_comm
= {};
169 struct lttng_trigger_comm
*header
;
171 header_offset
= payload
->buffer
.size
;
172 ret
= lttng_dynamic_buffer_append(&payload
->buffer
, &trigger_comm
,
173 sizeof(trigger_comm
));
178 size_before_payload
= payload
->buffer
.size
;
179 ret
= lttng_condition_serialize(trigger
->condition
, payload
);
184 ret
= lttng_action_serialize(trigger
->action
, payload
);
189 /* Update payload size. */
190 header
= (typeof(header
)) (payload
->buffer
.data
+ header_offset
);
191 header
->length
= payload
->buffer
.size
- size_before_payload
;