Commit | Line | Data |
---|---|---|
0c51e8f3 SM |
1 | /* |
2 | * Copyright (C) 2019 Simon Marchi <simon.marchi@efficios.com> | |
3 | * | |
4 | * SPDX-License-Identifier: LGPL-2.1-only | |
5 | * | |
6 | */ | |
7 | ||
8 | #include <assert.h> | |
9 | #include <common/dynamic-array.h> | |
9e620ea7 JG |
10 | #include <common/payload.h> |
11 | #include <common/payload-view.h> | |
0c51e8f3 SM |
12 | #include <common/error.h> |
13 | #include <common/macros.h> | |
14 | #include <lttng/action/action-internal.h> | |
15 | #include <lttng/action/group-internal.h> | |
16 | #include <lttng/action/group.h> | |
17 | ||
18 | #define IS_GROUP_ACTION(action) \ | |
17182cfd | 19 | (lttng_action_get_type(action) == LTTNG_ACTION_TYPE_GROUP) |
0c51e8f3 SM |
20 | |
21 | struct lttng_action_group { | |
22 | struct lttng_action parent; | |
23 | ||
24 | /* The array owns the action elements. */ | |
25 | struct lttng_dynamic_pointer_array actions; | |
26 | }; | |
27 | ||
28 | struct lttng_action_group_comm { | |
29 | uint32_t action_count; | |
30 | ||
31 | /* | |
32 | * Variable data: each element serialized sequentially. | |
33 | */ | |
34 | char data[]; | |
35 | } LTTNG_PACKED; | |
36 | ||
37 | static void destroy_lttng_action_group_element(void *ptr) | |
38 | { | |
39 | struct lttng_action *element = (struct lttng_action *) ptr; | |
40 | ||
41 | lttng_action_destroy(element); | |
42 | } | |
43 | ||
44 | static struct lttng_action_group *action_group_from_action( | |
45 | const struct lttng_action *action) | |
46 | { | |
47 | assert(action); | |
48 | ||
49 | return container_of(action, struct lttng_action_group, parent); | |
50 | } | |
51 | ||
52 | static const struct lttng_action_group *action_group_from_action_const( | |
53 | const struct lttng_action *action) | |
54 | { | |
55 | assert(action); | |
56 | ||
57 | return container_of(action, struct lttng_action_group, parent); | |
58 | } | |
59 | ||
60 | static bool lttng_action_group_validate(struct lttng_action *action) | |
61 | { | |
62 | unsigned int i, count; | |
63 | struct lttng_action_group *action_group; | |
64 | bool valid; | |
65 | ||
66 | assert(IS_GROUP_ACTION(action)); | |
67 | ||
68 | action_group = action_group_from_action(action); | |
69 | ||
70 | count = lttng_dynamic_pointer_array_get_count(&action_group->actions); | |
71 | ||
72 | for (i = 0; i < count; i++) { | |
73 | struct lttng_action *child = | |
74 | lttng_dynamic_pointer_array_get_pointer( | |
75 | &action_group->actions, i); | |
76 | ||
77 | assert(child); | |
78 | ||
79 | if (!lttng_action_validate(child)) { | |
80 | valid = false; | |
81 | goto end; | |
82 | } | |
83 | } | |
84 | ||
85 | valid = true; | |
86 | ||
87 | end: | |
88 | return valid; | |
89 | } | |
90 | ||
91 | static bool lttng_action_group_is_equal( | |
92 | const struct lttng_action *_a, const struct lttng_action *_b) | |
93 | { | |
94 | bool is_equal = false; | |
95 | unsigned int i; | |
96 | unsigned int a_count, b_count; | |
97 | ||
98 | if (lttng_action_group_get_count(_a, &a_count) != | |
99 | LTTNG_ACTION_STATUS_OK) { | |
100 | goto end; | |
101 | } | |
102 | ||
103 | if (lttng_action_group_get_count(_b, &b_count) != | |
104 | LTTNG_ACTION_STATUS_OK) { | |
105 | goto end; | |
106 | } | |
107 | ||
108 | if (a_count != b_count) { | |
109 | goto end; | |
110 | } | |
111 | ||
112 | for (i = 0; i < a_count; i++) { | |
113 | const struct lttng_action *child_a = | |
114 | lttng_action_group_get_at_index(_a, i); | |
115 | const struct lttng_action *child_b = | |
116 | lttng_action_group_get_at_index(_b, i); | |
117 | ||
118 | assert(child_a); | |
119 | assert(child_b); | |
120 | ||
121 | if (!lttng_action_is_equal(child_a, child_b)) { | |
122 | goto end; | |
123 | } | |
124 | } | |
125 | ||
126 | is_equal = true; | |
127 | end: | |
128 | return is_equal; | |
129 | } | |
130 | ||
131 | static int lttng_action_group_serialize( | |
132 | struct lttng_action *action, struct lttng_payload *payload) | |
133 | { | |
134 | struct lttng_action_group *action_group; | |
135 | struct lttng_action_group_comm comm; | |
136 | int ret; | |
137 | unsigned int i, count; | |
138 | ||
139 | assert(action); | |
140 | assert(payload); | |
141 | assert(IS_GROUP_ACTION(action)); | |
142 | ||
143 | action_group = action_group_from_action(action); | |
144 | ||
145 | DBG("Serializing action group"); | |
146 | ||
147 | count = lttng_dynamic_pointer_array_get_count(&action_group->actions); | |
148 | ||
149 | comm.action_count = count; | |
150 | ||
151 | ret = lttng_dynamic_buffer_append( | |
152 | &payload->buffer, &comm, sizeof(comm)); | |
153 | if (ret) { | |
154 | ret = -1; | |
155 | goto end; | |
156 | } | |
157 | ||
158 | for (i = 0; i < count; i++) { | |
159 | struct lttng_action *child = | |
160 | lttng_dynamic_pointer_array_get_pointer( | |
161 | &action_group->actions, i); | |
162 | ||
163 | assert(child); | |
164 | ||
165 | ret = lttng_action_serialize(child, payload); | |
166 | if (ret) { | |
167 | goto end; | |
168 | } | |
169 | } | |
170 | ||
171 | ret = 0; | |
172 | ||
173 | end: | |
174 | return ret; | |
175 | } | |
176 | ||
177 | static void lttng_action_group_destroy(struct lttng_action *action) | |
178 | { | |
179 | struct lttng_action_group *action_group; | |
180 | ||
181 | if (!action) { | |
182 | goto end; | |
183 | } | |
184 | ||
185 | action_group = action_group_from_action(action); | |
186 | lttng_dynamic_pointer_array_reset(&action_group->actions); | |
187 | free(action_group); | |
188 | ||
189 | end: | |
190 | return; | |
191 | } | |
192 | ||
193 | ssize_t lttng_action_group_create_from_payload( | |
194 | struct lttng_payload_view *view, | |
195 | struct lttng_action **p_action) | |
196 | { | |
197 | ssize_t consumed_len; | |
198 | const struct lttng_action_group_comm *comm; | |
199 | struct lttng_action *group; | |
200 | struct lttng_action *child_action = NULL; | |
201 | enum lttng_action_status status; | |
202 | size_t i; | |
203 | ||
204 | group = lttng_action_group_create(); | |
205 | if (!group) { | |
206 | consumed_len = -1; | |
207 | goto end; | |
208 | } | |
209 | ||
210 | comm = (typeof(comm)) view->buffer.data; | |
211 | ||
212 | consumed_len = sizeof(struct lttng_action_group_comm); | |
213 | ||
214 | for (i = 0; i < comm->action_count; i++) { | |
215 | ssize_t consumed_len_child; | |
216 | struct lttng_payload_view child_view = | |
217 | lttng_payload_view_from_view(view, consumed_len, | |
218 | view->buffer.size - consumed_len); | |
219 | ||
3e6e0df2 JG |
220 | if (!lttng_payload_view_is_valid(&child_view)) { |
221 | consumed_len = -1; | |
222 | goto end; | |
223 | } | |
224 | ||
0c51e8f3 SM |
225 | consumed_len_child = lttng_action_create_from_payload( |
226 | &child_view, &child_action); | |
227 | if (consumed_len_child < 0) { | |
228 | consumed_len = -1; | |
229 | goto end; | |
230 | } | |
231 | ||
232 | status = lttng_action_group_add_action(group, child_action); | |
233 | if (status != LTTNG_ACTION_STATUS_OK) { | |
234 | consumed_len = -1; | |
235 | goto end; | |
236 | } | |
237 | ||
238 | /* Transfer ownership to the action group. */ | |
239 | lttng_action_put(child_action); | |
240 | child_action = NULL; | |
241 | ||
242 | consumed_len += consumed_len_child; | |
243 | } | |
244 | ||
245 | *p_action = group; | |
246 | group = NULL; | |
247 | ||
248 | end: | |
249 | lttng_action_group_destroy(group); | |
250 | return consumed_len; | |
251 | } | |
252 | ||
253 | struct lttng_action *lttng_action_group_create(void) | |
254 | { | |
255 | struct lttng_action_group *action_group; | |
256 | struct lttng_action *action; | |
257 | ||
258 | action_group = zmalloc(sizeof(struct lttng_action_group)); | |
259 | if (!action_group) { | |
260 | action = NULL; | |
261 | goto end; | |
262 | } | |
263 | ||
264 | action = &action_group->parent; | |
265 | ||
266 | lttng_action_init(action, LTTNG_ACTION_TYPE_GROUP, | |
267 | lttng_action_group_validate, | |
268 | lttng_action_group_serialize, | |
269 | lttng_action_group_is_equal, | |
270 | lttng_action_group_destroy); | |
271 | ||
272 | lttng_dynamic_pointer_array_init(&action_group->actions, | |
273 | destroy_lttng_action_group_element); | |
274 | ||
275 | end: | |
276 | return action; | |
277 | } | |
278 | ||
279 | enum lttng_action_status lttng_action_group_add_action( | |
280 | struct lttng_action *group, struct lttng_action *action) | |
281 | { | |
282 | struct lttng_action_group *action_group; | |
283 | enum lttng_action_status status; | |
284 | int ret; | |
285 | ||
f3a9cc34 | 286 | if (!group || !IS_GROUP_ACTION(group) || !action) { |
0c51e8f3 SM |
287 | status = LTTNG_ACTION_STATUS_INVALID; |
288 | goto end; | |
289 | } | |
290 | ||
291 | /* | |
292 | * Don't allow adding groups in groups for now, since we're afraid of | |
293 | * cycles. | |
294 | */ | |
295 | if (IS_GROUP_ACTION(action)) { | |
296 | status = LTTNG_ACTION_STATUS_INVALID; | |
297 | goto end; | |
298 | } | |
299 | ||
300 | action_group = action_group_from_action(group); | |
301 | ||
302 | ret = lttng_dynamic_pointer_array_add_pointer(&action_group->actions, | |
303 | action); | |
304 | if (ret < 0) { | |
305 | status = LTTNG_ACTION_STATUS_ERROR; | |
306 | goto end; | |
307 | } | |
308 | ||
309 | /* Take ownership of the object. */ | |
310 | lttng_action_get(action); | |
311 | status = LTTNG_ACTION_STATUS_OK; | |
312 | end: | |
313 | return status; | |
314 | } | |
315 | ||
316 | enum lttng_action_status lttng_action_group_get_count( | |
317 | const struct lttng_action *group, unsigned int *count) | |
318 | { | |
319 | const struct lttng_action_group *action_group; | |
320 | enum lttng_action_status status = LTTNG_ACTION_STATUS_OK; | |
321 | ||
322 | if (!group || !IS_GROUP_ACTION(group)) { | |
323 | status = LTTNG_ACTION_STATUS_INVALID; | |
324 | *count = 0; | |
325 | goto end; | |
326 | } | |
327 | ||
328 | action_group = action_group_from_action_const(group); | |
329 | *count = lttng_dynamic_pointer_array_get_count(&action_group->actions); | |
330 | end: | |
331 | return status; | |
332 | } | |
333 | ||
334 | const struct lttng_action *lttng_action_group_get_at_index( | |
335 | const struct lttng_action *group, unsigned int index) | |
336 | { | |
337 | unsigned int count; | |
338 | const struct lttng_action_group *action_group; | |
339 | const struct lttng_action * action = NULL; | |
340 | ||
341 | if (lttng_action_group_get_count(group, &count) != | |
342 | LTTNG_ACTION_STATUS_OK) { | |
343 | goto end; | |
344 | } | |
345 | ||
346 | if (index >= count) { | |
347 | goto end; | |
348 | } | |
349 | ||
350 | action_group = action_group_from_action_const(group); | |
351 | action = lttng_dynamic_pointer_array_get_pointer(&action_group->actions, | |
352 | index); | |
353 | end: | |
354 | return action; | |
355 | } |