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/error.h>
15 bool lttng_trigger_validate(struct lttng_trigger
*trigger
)
24 valid
= lttng_condition_validate(trigger
->condition
) &&
25 lttng_action_validate(trigger
->action
);
30 struct lttng_trigger
*lttng_trigger_create(
31 struct lttng_condition
*condition
,
32 struct lttng_action
*action
)
34 struct lttng_trigger
*trigger
= NULL
;
36 if (!condition
|| !action
) {
40 trigger
= zmalloc(sizeof(struct lttng_trigger
));
45 trigger
->condition
= condition
;
46 trigger
->action
= action
;
51 struct lttng_condition
*lttng_trigger_get_condition(
52 struct lttng_trigger
*trigger
)
54 return trigger
? trigger
->condition
: NULL
;
58 const struct lttng_condition
*lttng_trigger_get_const_condition(
59 const struct lttng_trigger
*trigger
)
61 return trigger
->condition
;
64 struct lttng_action
*lttng_trigger_get_action(
65 struct lttng_trigger
*trigger
)
67 return trigger
? trigger
->action
: NULL
;
71 const struct lttng_action
*lttng_trigger_get_const_action(
72 const struct lttng_trigger
*trigger
)
74 return trigger
->action
;
77 void lttng_trigger_destroy(struct lttng_trigger
*trigger
)
87 ssize_t
lttng_trigger_create_from_buffer(
88 const struct lttng_buffer_view
*src_view
,
89 struct lttng_trigger
**trigger
)
91 ssize_t ret
, offset
= 0, condition_size
, action_size
;
92 struct lttng_condition
*condition
= NULL
;
93 struct lttng_action
*action
= NULL
;
94 const struct lttng_trigger_comm
*trigger_comm
;
95 struct lttng_buffer_view condition_view
;
96 struct lttng_buffer_view action_view
;
98 if (!src_view
|| !trigger
) {
103 /* lttng_trigger_comm header */
104 trigger_comm
= (const struct lttng_trigger_comm
*) src_view
->data
;
105 offset
+= sizeof(*trigger_comm
);
107 condition_view
= lttng_buffer_view_from_view(src_view
, offset
, -1);
109 /* struct lttng_condition */
110 condition_size
= lttng_condition_create_from_buffer(&condition_view
,
112 if (condition_size
< 0) {
113 ret
= condition_size
;
116 offset
+= condition_size
;
118 /* struct lttng_action */
119 action_view
= lttng_buffer_view_from_view(src_view
, offset
, -1);
120 action_size
= lttng_action_create_from_buffer(&action_view
, &action
);
121 if (action_size
< 0) {
125 offset
+= action_size
;
127 /* Unexpected size of inner-elements; the buffer is corrupted. */
128 if ((ssize_t
) trigger_comm
->length
!= condition_size
+ action_size
) {
133 *trigger
= lttng_trigger_create(condition
, action
);
142 lttng_condition_destroy(condition
);
143 lttng_action_destroy(action
);
148 * Both elements are stored contiguously, see their "*_comm" structure
149 * for the detailed format.
152 int lttng_trigger_serialize(struct lttng_trigger
*trigger
,
153 struct lttng_dynamic_buffer
*buf
)
156 size_t header_offset
, size_before_payload
;
157 struct lttng_trigger_comm trigger_comm
= { 0 };
158 struct lttng_trigger_comm
*header
;
160 header_offset
= buf
->size
;
161 ret
= lttng_dynamic_buffer_append(buf
, &trigger_comm
,
162 sizeof(trigger_comm
));
167 size_before_payload
= buf
->size
;
168 ret
= lttng_condition_serialize(trigger
->condition
, buf
);
173 ret
= lttng_action_serialize(trigger
->action
, buf
);
178 /* Update payload size. */
179 header
= (struct lttng_trigger_comm
*) ((char *) buf
->data
+ header_offset
);
180 header
->length
= buf
->size
- size_before_payload
;