9e8e43577492b3f4d5bd0f3610fb529d7c37299a
[lttng-tools.git] / src / common / actions / snapshot-session.c
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>
12 #include <common/sessiond-comm/payload.h>
13 #include <common/sessiond-comm/payload-view.h>
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) \
22 (lttng_action_get_type_const(action) == LTTNG_ACTION_TYPE_SNAPSHOT_SESSION)
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
127 static size_t serialize_strlen(const char *s)
128 {
129
130 size_t len = 0;
131
132 if (s) {
133 len = strlen(s) + 1;
134 }
135
136 return len;
137 }
138
139 static int lttng_action_snapshot_session_serialize(
140 struct lttng_action *action, struct lttng_payload *payload)
141 {
142 struct lttng_action_snapshot_session *action_snapshot_session;
143 struct lttng_action_snapshot_session_comm comm = {};
144 int ret;
145 size_t size_before_comm;
146
147 assert(action);
148 assert(payload);
149
150 size_before_comm = payload->buffer.size;
151 size_before_comm = size_before_comm + sizeof(comm);
152
153 action_snapshot_session = action_snapshot_session_from_action(action);
154 comm.session_name_len =
155 serialize_strlen(action_snapshot_session->session_name);
156
157 /* Add header. */
158 ret = lttng_dynamic_buffer_append(
159 &payload->buffer, &comm, sizeof(comm));
160 if (ret) {
161 goto end;
162 }
163
164 assert(action_snapshot_session->session_name);
165 DBG("Serializing snapshot session action: session-name: %s",
166 action_snapshot_session->session_name);
167
168 /* Add session name. */
169 ret = lttng_dynamic_buffer_append(&payload->buffer,
170 action_snapshot_session->session_name,
171 comm.session_name_len);
172 if (ret) {
173 goto end;
174 }
175
176 /* Serialize the snapshot output object, if any. */
177 if (action_snapshot_session->output) {
178 const size_t size_before_output = payload->buffer.size;
179 struct lttng_action_snapshot_session_comm *comm_in_payload;
180
181 ret = lttng_snapshot_output_serialize(
182 action_snapshot_session->output,
183 payload);
184 if (ret) {
185 goto end;
186 }
187
188 /* Adjust action length in header. */
189 comm_in_payload = (typeof(comm_in_payload))(
190 payload->buffer.data + size_before_comm);
191 comm_in_payload->snapshot_output_len =
192 payload->buffer.size - size_before_output;
193 }
194
195 end:
196 return ret;
197 }
198
199 static void lttng_action_snapshot_session_destroy(struct lttng_action *action)
200 {
201 struct lttng_action_snapshot_session *action_snapshot_session;
202
203 if (!action) {
204 goto end;
205 }
206
207 action_snapshot_session = action_snapshot_session_from_action(action);
208
209 free(action_snapshot_session->session_name);
210 lttng_snapshot_output_destroy(action_snapshot_session->output);
211 free(action_snapshot_session);
212
213 end:
214 return;
215 }
216
217 ssize_t lttng_action_snapshot_session_create_from_payload(
218 struct lttng_payload_view *view,
219 struct lttng_action **p_action)
220 {
221 ssize_t consumed_len;
222 const struct lttng_action_snapshot_session_comm *comm;
223 const char *variable_data;
224 struct lttng_action *action;
225 enum lttng_action_status status;
226 struct lttng_snapshot_output *snapshot_output = NULL;
227
228 action = lttng_action_snapshot_session_create();
229 if (!action) {
230 goto error;
231 }
232
233 comm = (typeof(comm)) view->buffer.data;
234 variable_data = (const char *) &comm->data;
235
236 consumed_len = sizeof(struct lttng_action_snapshot_session_comm);
237
238 if (!lttng_buffer_view_contains_string(
239 &view->buffer, variable_data, comm->session_name_len)) {
240 goto error;
241 }
242
243 status = lttng_action_snapshot_session_set_session_name(
244 action, variable_data);
245 if (status != LTTNG_ACTION_STATUS_OK) {
246 goto error;
247 }
248
249 variable_data += comm->session_name_len;
250 consumed_len += comm->session_name_len;
251
252 /* If there is a snapshot output object, deserialize it. */
253 if (comm->snapshot_output_len > 0) {
254 ssize_t snapshot_output_consumed_len;
255 enum lttng_action_status action_status;
256 struct lttng_payload_view snapshot_output_buffer_view =
257 lttng_payload_view_from_view(view, consumed_len,
258 comm->snapshot_output_len);
259
260 if (!snapshot_output_buffer_view.buffer.data) {
261 fprintf(stderr, "Failed to create buffer view for snapshot output.\n");
262 goto error;
263 }
264
265 snapshot_output_consumed_len =
266 lttng_snapshot_output_create_from_payload(
267 &snapshot_output_buffer_view,
268 &snapshot_output);
269 if (snapshot_output_consumed_len != comm->snapshot_output_len) {
270 fprintf(stderr,
271 "Failed to deserialize snapshot output object: "
272 "consumed-len: %zd, expected-len: %" PRIu32,
273 snapshot_output_consumed_len,
274 comm->snapshot_output_len);
275 goto error;
276 }
277
278 action_status = lttng_action_snapshot_session_set_output(
279 action, snapshot_output);
280 if (action_status != LTTNG_ACTION_STATUS_OK) {
281 goto error;
282 }
283
284 /* Ownership has been transferred to the action. */
285 snapshot_output = NULL;
286 }
287
288 variable_data += comm->snapshot_output_len;
289 consumed_len += comm->snapshot_output_len;
290 *p_action = action;
291 action = NULL;
292
293 goto end;
294
295 error:
296 consumed_len = -1;
297
298 end:
299 lttng_action_snapshot_session_destroy(action);
300 lttng_snapshot_output_destroy(snapshot_output);
301
302 return consumed_len;
303 }
304
305 struct lttng_action *lttng_action_snapshot_session_create(void)
306 {
307 struct lttng_action *action;
308
309 action = zmalloc(sizeof(struct lttng_action_snapshot_session));
310 if (!action) {
311 goto end;
312 }
313
314 lttng_action_init(action, LTTNG_ACTION_TYPE_SNAPSHOT_SESSION,
315 lttng_action_snapshot_session_validate,
316 lttng_action_snapshot_session_serialize,
317 lttng_action_snapshot_session_is_equal,
318 lttng_action_snapshot_session_destroy);
319
320 end:
321 return action;
322 }
323
324 enum lttng_action_status lttng_action_snapshot_session_set_session_name(
325 struct lttng_action *action, const char *session_name)
326 {
327 struct lttng_action_snapshot_session *action_snapshot_session;
328 enum lttng_action_status status;
329
330 if (!action || !IS_SNAPSHOT_SESSION_ACTION(action) || !session_name ||
331 strlen(session_name) == 0) {
332 status = LTTNG_ACTION_STATUS_INVALID;
333 goto end;
334 }
335
336 action_snapshot_session = action_snapshot_session_from_action(action);
337
338 free(action_snapshot_session->session_name);
339
340 action_snapshot_session->session_name = strdup(session_name);
341 if (!action_snapshot_session->session_name) {
342 status = LTTNG_ACTION_STATUS_ERROR;
343 goto end;
344 }
345
346 status = LTTNG_ACTION_STATUS_OK;
347 end:
348 return status;
349 }
350
351 enum lttng_action_status lttng_action_snapshot_session_get_session_name(
352 const struct lttng_action *action, const char **session_name)
353 {
354 const struct lttng_action_snapshot_session *action_snapshot_session;
355 enum lttng_action_status status;
356
357 if (!action || !IS_SNAPSHOT_SESSION_ACTION(action) || !session_name) {
358 status = LTTNG_ACTION_STATUS_INVALID;
359 goto end;
360 }
361
362 action_snapshot_session = action_snapshot_session_from_action_const(action);
363
364 if (action_snapshot_session->session_name) {
365 *session_name = action_snapshot_session->session_name;
366 status = LTTNG_ACTION_STATUS_OK;
367 } else {
368 status = LTTNG_ACTION_STATUS_UNSET;
369 }
370
371 end:
372
373 return status;
374 }
375
376 enum lttng_action_status lttng_action_snapshot_session_set_output(
377 struct lttng_action *action,
378 struct lttng_snapshot_output *output)
379 {
380 struct lttng_action_snapshot_session *action_snapshot_session;
381 enum lttng_action_status status;
382
383 if (!action || !IS_SNAPSHOT_SESSION_ACTION(action) || !output) {
384 status = LTTNG_ACTION_STATUS_INVALID;
385 goto end;
386 }
387
388 action_snapshot_session = action_snapshot_session_from_action(action);
389
390 lttng_snapshot_output_destroy(action_snapshot_session->output);
391 action_snapshot_session->output = output;
392
393 status = LTTNG_ACTION_STATUS_OK;
394
395 end:
396 return status;
397 }
398
399 enum lttng_action_status lttng_action_snapshot_session_get_output(
400 const struct lttng_action *action,
401 const struct lttng_snapshot_output **output)
402 {
403 const struct lttng_action_snapshot_session *action_snapshot_session;
404 enum lttng_action_status status;
405
406 if (!action || !IS_SNAPSHOT_SESSION_ACTION(action)|| !output) {
407 status = LTTNG_ACTION_STATUS_INVALID;
408 goto end;
409 }
410
411 action_snapshot_session = action_snapshot_session_from_action_const(action);
412
413 if (action_snapshot_session->output) {
414 *output = action_snapshot_session->output;
415 status = LTTNG_ACTION_STATUS_OK;
416 } else {
417 status = LTTNG_ACTION_STATUS_UNSET;
418 }
419
420 end:
421 return status;
422 }
This page took 0.054753 seconds and 3 git commands to generate.