2 * Copyright (C) 2017 - Jérémie Galarneau <jeremie.galarneau@efficios.com>
4 * This library is free software; you can redistribute it and/or modify it
5 * under the terms of the GNU Lesser General Public License, version 2.1 only,
6 * as published by the Free Software Foundation.
8 * This library is distributed in the hope that it will be useful, but WITHOUT
9 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
10 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
13 * You should have received a copy of the GNU Lesser General Public License
14 * along with this library; if not, write to the Free Software Foundation,
15 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
18 #include <lttng/trigger/trigger-internal.h>
19 #include <lttng/condition/condition-internal.h>
20 #include <lttng/action/action-internal.h>
21 #include <common/error.h>
25 bool lttng_trigger_validate(struct lttng_trigger
*trigger
)
34 valid
= lttng_condition_validate(trigger
->condition
) &&
35 lttng_action_validate(trigger
->action
);
40 struct lttng_trigger
*lttng_trigger_create(
41 struct lttng_condition
*condition
,
42 struct lttng_action
*action
)
44 struct lttng_trigger
*trigger
= NULL
;
46 if (!condition
|| !action
) {
50 trigger
= zmalloc(sizeof(struct lttng_trigger
));
55 trigger
->condition
= condition
;
56 trigger
->action
= action
;
61 struct lttng_condition
*lttng_trigger_get_condition(
62 struct lttng_trigger
*trigger
)
64 return trigger
? trigger
->condition
: NULL
;
68 const struct lttng_condition
*lttng_trigger_get_const_condition(
69 const struct lttng_trigger
*trigger
)
71 return trigger
->condition
;
74 struct lttng_action
*lttng_trigger_get_action(
75 struct lttng_trigger
*trigger
)
77 return trigger
? trigger
->action
: NULL
;
81 const struct lttng_action
*lttng_trigger_get_const_action(
82 const struct lttng_trigger
*trigger
)
84 return trigger
->action
;
87 void lttng_trigger_destroy(struct lttng_trigger
*trigger
)
97 ssize_t
lttng_trigger_create_from_buffer(
98 const struct lttng_buffer_view
*src_view
,
99 struct lttng_trigger
**trigger
)
101 ssize_t ret
, offset
= 0, condition_size
, action_size
;
102 struct lttng_condition
*condition
= NULL
;
103 struct lttng_action
*action
= NULL
;
104 const struct lttng_trigger_comm
*trigger_comm
;
105 struct lttng_buffer_view condition_view
;
106 struct lttng_buffer_view action_view
;
108 if (!src_view
|| !trigger
) {
113 /* lttng_trigger_comm header */
114 trigger_comm
= (const struct lttng_trigger_comm
*) src_view
->data
;
115 offset
+= sizeof(*trigger_comm
);
117 condition_view
= lttng_buffer_view_from_view(src_view
, offset
, -1);
119 /* struct lttng_condition */
120 condition_size
= lttng_condition_create_from_buffer(&condition_view
,
122 if (condition_size
< 0) {
123 ret
= condition_size
;
126 offset
+= condition_size
;
128 /* struct lttng_action */
129 action_view
= lttng_buffer_view_from_view(src_view
, offset
, -1);
130 action_size
= lttng_action_create_from_buffer(&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
);
152 lttng_condition_destroy(condition
);
153 lttng_action_destroy(action
);
158 * Both elements are stored contiguously, see their "*_comm" structure
159 * for the detailed format.
162 int lttng_trigger_serialize(struct lttng_trigger
*trigger
,
163 struct lttng_dynamic_buffer
*buf
)
166 size_t header_offset
, size_before_payload
;
167 struct lttng_trigger_comm trigger_comm
= { 0 };
168 struct lttng_trigger_comm
*header
;
170 header_offset
= buf
->size
;
171 ret
= lttng_dynamic_buffer_append(buf
, &trigger_comm
,
172 sizeof(trigger_comm
));
177 size_before_payload
= buf
->size
;
178 ret
= lttng_condition_serialize(trigger
->condition
, buf
);
183 ret
= lttng_action_serialize(trigger
->action
, buf
);
188 /* Update payload size. */
189 header
= (struct lttng_trigger_comm
*) ((char *) buf
->data
+ header_offset
);
190 header
->length
= buf
->size
- size_before_payload
;