Commit | Line | Data |
---|---|---|
757c48a2 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/error.h> | |
10 | #include <common/macros.h> | |
11 | #include <common/snapshot.h> | |
9e620ea7 JG |
12 | #include <common/payload.h> |
13 | #include <common/payload-view.h> | |
757c48a2 SM |
14 | #include <lttng/action/action-internal.h> |
15 | #include <lttng/action/snapshot-session-internal.h> | |
16 | #include <lttng/action/snapshot-session.h> | |
17 | #include <lttng/snapshot.h> | |
18 | #include <lttng/snapshot-internal.h> | |
19 | #include <inttypes.h> | |
20 | ||
21 | #define IS_SNAPSHOT_SESSION_ACTION(action) \ | |
17182cfd | 22 | (lttng_action_get_type(action) == LTTNG_ACTION_TYPE_SNAPSHOT_SESSION) |
757c48a2 SM |
23 | |
24 | struct lttng_action_snapshot_session { | |
25 | struct lttng_action parent; | |
26 | ||
27 | /* Owned by this. */ | |
28 | char *session_name; | |
29 | ||
30 | /* | |
31 | * When non-NULL, use this custom output when taking the snapshot, | |
32 | * rather than the session's registered snapshot output. | |
33 | * | |
34 | * Owned by this. | |
35 | */ | |
36 | struct lttng_snapshot_output *output; | |
37 | }; | |
38 | ||
39 | struct lttng_action_snapshot_session_comm { | |
40 | /* All string lengths include the trailing \0. */ | |
41 | uint32_t session_name_len; | |
42 | uint32_t snapshot_output_len; | |
43 | ||
44 | /* | |
45 | * Variable data (all strings are null-terminated): | |
46 | * | |
47 | * - session name string | |
48 | * - snapshot output object | |
49 | * | |
50 | */ | |
51 | char data[]; | |
52 | } LTTNG_PACKED; | |
53 | ||
54 | static struct lttng_action_snapshot_session * | |
55 | action_snapshot_session_from_action(struct lttng_action *action) | |
56 | { | |
57 | assert(action); | |
58 | ||
59 | return container_of( | |
60 | action, struct lttng_action_snapshot_session, parent); | |
61 | } | |
62 | ||
63 | static const struct lttng_action_snapshot_session * | |
64 | action_snapshot_session_from_action_const(const struct lttng_action *action) | |
65 | { | |
66 | assert(action); | |
67 | ||
68 | return container_of( | |
69 | action, struct lttng_action_snapshot_session, parent); | |
70 | } | |
71 | ||
72 | static bool lttng_action_snapshot_session_validate(struct lttng_action *action) | |
73 | { | |
74 | bool valid = false; | |
75 | struct lttng_action_snapshot_session *action_snapshot_session; | |
76 | ||
77 | if (!action) { | |
78 | goto end; | |
79 | } | |
80 | ||
81 | action_snapshot_session = action_snapshot_session_from_action(action); | |
82 | ||
83 | /* A non-empty session name is mandatory. */ | |
84 | if (!action_snapshot_session->session_name || | |
85 | strlen(action_snapshot_session->session_name) == 0) { | |
86 | goto end; | |
87 | } | |
88 | ||
89 | if (action_snapshot_session->output && | |
90 | !lttng_snapshot_output_validate(action_snapshot_session->output)) { | |
91 | goto end; | |
92 | } | |
93 | ||
94 | valid = true; | |
95 | end: | |
96 | return valid; | |
97 | } | |
98 | ||
99 | static bool lttng_action_snapshot_session_is_equal( | |
100 | const struct lttng_action *_a, const struct lttng_action *_b) | |
101 | { | |
102 | bool is_equal = false; | |
103 | const struct lttng_action_snapshot_session *a, *b; | |
104 | ||
105 | a = action_snapshot_session_from_action_const(_a); | |
106 | b = action_snapshot_session_from_action_const(_b); | |
107 | ||
108 | /* Action is not valid if this is not true. */ | |
109 | assert(a->session_name); | |
110 | assert(b->session_name); | |
111 | if (strcmp(a->session_name, b->session_name)) { | |
112 | goto end; | |
113 | } | |
114 | ||
115 | if (a->output && b->output && | |
116 | !lttng_snapshot_output_is_equal(a->output, b->output)) { | |
117 | goto end; | |
118 | } else if (!!a->output != !!b->output) { | |
119 | goto end; | |
120 | } | |
121 | ||
122 | is_equal = true; | |
123 | end: | |
124 | return is_equal; | |
125 | } | |
126 | ||
0c51e8f3 | 127 | static size_t serialize_strlen(const char *str) |
757c48a2 | 128 | { |
0c51e8f3 | 129 | return str ? strlen(str) + 1 : 0; |
757c48a2 SM |
130 | } |
131 | ||
132 | static int lttng_action_snapshot_session_serialize( | |
133 | struct lttng_action *action, struct lttng_payload *payload) | |
134 | { | |
135 | struct lttng_action_snapshot_session *action_snapshot_session; | |
136 | struct lttng_action_snapshot_session_comm comm = {}; | |
137 | int ret; | |
138 | size_t size_before_comm; | |
139 | ||
140 | assert(action); | |
141 | assert(payload); | |
142 | ||
143 | size_before_comm = payload->buffer.size; | |
757c48a2 SM |
144 | |
145 | action_snapshot_session = action_snapshot_session_from_action(action); | |
146 | comm.session_name_len = | |
147 | serialize_strlen(action_snapshot_session->session_name); | |
148 | ||
149 | /* Add header. */ | |
150 | ret = lttng_dynamic_buffer_append( | |
151 | &payload->buffer, &comm, sizeof(comm)); | |
152 | if (ret) { | |
153 | goto end; | |
154 | } | |
155 | ||
156 | assert(action_snapshot_session->session_name); | |
157 | DBG("Serializing snapshot session action: session-name: %s", | |
158 | action_snapshot_session->session_name); | |
159 | ||
160 | /* Add session name. */ | |
161 | ret = lttng_dynamic_buffer_append(&payload->buffer, | |
162 | action_snapshot_session->session_name, | |
163 | comm.session_name_len); | |
164 | if (ret) { | |
165 | goto end; | |
166 | } | |
167 | ||
168 | /* Serialize the snapshot output object, if any. */ | |
169 | if (action_snapshot_session->output) { | |
170 | const size_t size_before_output = payload->buffer.size; | |
171 | struct lttng_action_snapshot_session_comm *comm_in_payload; | |
172 | ||
173 | ret = lttng_snapshot_output_serialize( | |
174 | action_snapshot_session->output, | |
175 | payload); | |
176 | if (ret) { | |
177 | goto end; | |
178 | } | |
179 | ||
180 | /* Adjust action length in header. */ | |
181 | comm_in_payload = (typeof(comm_in_payload))( | |
182 | payload->buffer.data + size_before_comm); | |
183 | comm_in_payload->snapshot_output_len = | |
184 | payload->buffer.size - size_before_output; | |
185 | } | |
186 | ||
187 | end: | |
188 | return ret; | |
189 | } | |
190 | ||
191 | static void lttng_action_snapshot_session_destroy(struct lttng_action *action) | |
192 | { | |
193 | struct lttng_action_snapshot_session *action_snapshot_session; | |
194 | ||
195 | if (!action) { | |
196 | goto end; | |
197 | } | |
198 | ||
199 | action_snapshot_session = action_snapshot_session_from_action(action); | |
200 | ||
201 | free(action_snapshot_session->session_name); | |
202 | lttng_snapshot_output_destroy(action_snapshot_session->output); | |
203 | free(action_snapshot_session); | |
204 | ||
205 | end: | |
206 | return; | |
207 | } | |
208 | ||
209 | ssize_t lttng_action_snapshot_session_create_from_payload( | |
210 | struct lttng_payload_view *view, | |
211 | struct lttng_action **p_action) | |
212 | { | |
213 | ssize_t consumed_len; | |
214 | const struct lttng_action_snapshot_session_comm *comm; | |
215 | const char *variable_data; | |
216 | struct lttng_action *action; | |
217 | enum lttng_action_status status; | |
218 | struct lttng_snapshot_output *snapshot_output = NULL; | |
219 | ||
220 | action = lttng_action_snapshot_session_create(); | |
221 | if (!action) { | |
222 | goto error; | |
223 | } | |
224 | ||
225 | comm = (typeof(comm)) view->buffer.data; | |
226 | variable_data = (const char *) &comm->data; | |
227 | ||
228 | consumed_len = sizeof(struct lttng_action_snapshot_session_comm); | |
229 | ||
230 | if (!lttng_buffer_view_contains_string( | |
231 | &view->buffer, variable_data, comm->session_name_len)) { | |
232 | goto error; | |
233 | } | |
234 | ||
235 | status = lttng_action_snapshot_session_set_session_name( | |
236 | action, variable_data); | |
237 | if (status != LTTNG_ACTION_STATUS_OK) { | |
238 | goto error; | |
239 | } | |
240 | ||
241 | variable_data += comm->session_name_len; | |
242 | consumed_len += comm->session_name_len; | |
243 | ||
244 | /* If there is a snapshot output object, deserialize it. */ | |
245 | if (comm->snapshot_output_len > 0) { | |
246 | ssize_t snapshot_output_consumed_len; | |
247 | enum lttng_action_status action_status; | |
248 | struct lttng_payload_view snapshot_output_buffer_view = | |
249 | lttng_payload_view_from_view(view, consumed_len, | |
250 | comm->snapshot_output_len); | |
251 | ||
252 | if (!snapshot_output_buffer_view.buffer.data) { | |
0c51e8f3 | 253 | ERR("Failed to create buffer view for snapshot output."); |
757c48a2 SM |
254 | goto error; |
255 | } | |
256 | ||
257 | snapshot_output_consumed_len = | |
258 | lttng_snapshot_output_create_from_payload( | |
259 | &snapshot_output_buffer_view, | |
260 | &snapshot_output); | |
261 | if (snapshot_output_consumed_len != comm->snapshot_output_len) { | |
0c51e8f3 | 262 | ERR("Failed to deserialize snapshot output object: " |
757c48a2 SM |
263 | "consumed-len: %zd, expected-len: %" PRIu32, |
264 | snapshot_output_consumed_len, | |
265 | comm->snapshot_output_len); | |
266 | goto error; | |
267 | } | |
268 | ||
269 | action_status = lttng_action_snapshot_session_set_output( | |
270 | action, snapshot_output); | |
271 | if (action_status != LTTNG_ACTION_STATUS_OK) { | |
272 | goto error; | |
273 | } | |
274 | ||
275 | /* Ownership has been transferred to the action. */ | |
276 | snapshot_output = NULL; | |
277 | } | |
278 | ||
279 | variable_data += comm->snapshot_output_len; | |
280 | consumed_len += comm->snapshot_output_len; | |
281 | *p_action = action; | |
282 | action = NULL; | |
283 | ||
284 | goto end; | |
285 | ||
286 | error: | |
287 | consumed_len = -1; | |
288 | ||
289 | end: | |
290 | lttng_action_snapshot_session_destroy(action); | |
291 | lttng_snapshot_output_destroy(snapshot_output); | |
292 | ||
293 | return consumed_len; | |
294 | } | |
295 | ||
296 | struct lttng_action *lttng_action_snapshot_session_create(void) | |
297 | { | |
298 | struct lttng_action *action; | |
299 | ||
300 | action = zmalloc(sizeof(struct lttng_action_snapshot_session)); | |
301 | if (!action) { | |
302 | goto end; | |
303 | } | |
304 | ||
305 | lttng_action_init(action, LTTNG_ACTION_TYPE_SNAPSHOT_SESSION, | |
306 | lttng_action_snapshot_session_validate, | |
307 | lttng_action_snapshot_session_serialize, | |
308 | lttng_action_snapshot_session_is_equal, | |
309 | lttng_action_snapshot_session_destroy); | |
310 | ||
311 | end: | |
312 | return action; | |
313 | } | |
314 | ||
315 | enum lttng_action_status lttng_action_snapshot_session_set_session_name( | |
316 | struct lttng_action *action, const char *session_name) | |
317 | { | |
318 | struct lttng_action_snapshot_session *action_snapshot_session; | |
319 | enum lttng_action_status status; | |
320 | ||
321 | if (!action || !IS_SNAPSHOT_SESSION_ACTION(action) || !session_name || | |
322 | strlen(session_name) == 0) { | |
323 | status = LTTNG_ACTION_STATUS_INVALID; | |
324 | goto end; | |
325 | } | |
326 | ||
327 | action_snapshot_session = action_snapshot_session_from_action(action); | |
328 | ||
329 | free(action_snapshot_session->session_name); | |
330 | ||
331 | action_snapshot_session->session_name = strdup(session_name); | |
332 | if (!action_snapshot_session->session_name) { | |
333 | status = LTTNG_ACTION_STATUS_ERROR; | |
334 | goto end; | |
335 | } | |
336 | ||
337 | status = LTTNG_ACTION_STATUS_OK; | |
338 | end: | |
339 | return status; | |
340 | } | |
341 | ||
342 | enum lttng_action_status lttng_action_snapshot_session_get_session_name( | |
343 | const struct lttng_action *action, const char **session_name) | |
344 | { | |
345 | const struct lttng_action_snapshot_session *action_snapshot_session; | |
346 | enum lttng_action_status status; | |
347 | ||
348 | if (!action || !IS_SNAPSHOT_SESSION_ACTION(action) || !session_name) { | |
349 | status = LTTNG_ACTION_STATUS_INVALID; | |
350 | goto end; | |
351 | } | |
352 | ||
353 | action_snapshot_session = action_snapshot_session_from_action_const(action); | |
354 | ||
355 | if (action_snapshot_session->session_name) { | |
356 | *session_name = action_snapshot_session->session_name; | |
357 | status = LTTNG_ACTION_STATUS_OK; | |
358 | } else { | |
359 | status = LTTNG_ACTION_STATUS_UNSET; | |
360 | } | |
361 | ||
362 | end: | |
363 | ||
364 | return status; | |
365 | } | |
366 | ||
367 | enum lttng_action_status lttng_action_snapshot_session_set_output( | |
368 | struct lttng_action *action, | |
369 | struct lttng_snapshot_output *output) | |
370 | { | |
371 | struct lttng_action_snapshot_session *action_snapshot_session; | |
372 | enum lttng_action_status status; | |
373 | ||
374 | if (!action || !IS_SNAPSHOT_SESSION_ACTION(action) || !output) { | |
375 | status = LTTNG_ACTION_STATUS_INVALID; | |
376 | goto end; | |
377 | } | |
378 | ||
379 | action_snapshot_session = action_snapshot_session_from_action(action); | |
380 | ||
381 | lttng_snapshot_output_destroy(action_snapshot_session->output); | |
382 | action_snapshot_session->output = output; | |
383 | ||
384 | status = LTTNG_ACTION_STATUS_OK; | |
385 | ||
386 | end: | |
387 | return status; | |
388 | } | |
389 | ||
390 | enum lttng_action_status lttng_action_snapshot_session_get_output( | |
391 | const struct lttng_action *action, | |
392 | const struct lttng_snapshot_output **output) | |
393 | { | |
394 | const struct lttng_action_snapshot_session *action_snapshot_session; | |
395 | enum lttng_action_status status; | |
396 | ||
397 | if (!action || !IS_SNAPSHOT_SESSION_ACTION(action)|| !output) { | |
398 | status = LTTNG_ACTION_STATUS_INVALID; | |
399 | goto end; | |
400 | } | |
401 | ||
402 | action_snapshot_session = action_snapshot_session_from_action_const(action); | |
403 | ||
404 | if (action_snapshot_session->output) { | |
405 | *output = action_snapshot_session->output; | |
406 | status = LTTNG_ACTION_STATUS_OK; | |
407 | } else { | |
408 | status = LTTNG_ACTION_STATUS_UNSET; | |
409 | } | |
410 | ||
411 | end: | |
412 | return status; | |
413 | } |