Commit | Line | Data |
---|---|---|
da3c9ec1 | 1 | /* |
ab5be9fa | 2 | * Copyright (C) 2013 David Goulet <dgoulet@efficios.com> |
da3c9ec1 | 3 | * |
ab5be9fa | 4 | * SPDX-License-Identifier: LGPL-2.1-only |
da3c9ec1 | 5 | * |
da3c9ec1 DG |
6 | */ |
7 | ||
6c1c0768 | 8 | #define _LGPL_SOURCE |
da3c9ec1 DG |
9 | #include <string.h> |
10 | ||
c9e313bc | 11 | #include <common/sessiond-comm/sessiond-comm.hpp> |
da3c9ec1 DG |
12 | #include <lttng/lttng-error.h> |
13 | #include <lttng/snapshot.h> | |
c9e313bc | 14 | #include <lttng/snapshot-internal.hpp> |
da3c9ec1 | 15 | |
c9e313bc | 16 | #include "lttng-ctl-helper.hpp" |
da3c9ec1 DG |
17 | |
18 | /* | |
19 | * Add an output object to a session identified by name. | |
20 | * | |
21 | * Return 0 on success or else a negative LTTNG_ERR code. | |
22 | */ | |
23 | int lttng_snapshot_add_output(const char *session_name, | |
24 | struct lttng_snapshot_output *output) | |
25 | { | |
26 | int ret; | |
27 | struct lttcomm_session_msg lsm; | |
28 | struct lttcomm_lttng_output_id *reply; | |
29 | ||
30 | if (!session_name || !output) { | |
e1b624d0 JG |
31 | ret = -LTTNG_ERR_INVALID; |
32 | goto end; | |
da3c9ec1 DG |
33 | } |
34 | ||
35 | memset(&lsm, 0, sizeof(lsm)); | |
36 | lsm.cmd_type = LTTNG_SNAPSHOT_ADD_OUTPUT; | |
37 | ||
e1b624d0 | 38 | ret = lttng_strncpy(lsm.session.name, session_name, |
da3c9ec1 | 39 | sizeof(lsm.session.name)); |
e1b624d0 JG |
40 | if (ret) { |
41 | ret = -LTTNG_ERR_INVALID; | |
42 | goto end; | |
43 | } | |
44 | ||
da3c9ec1 DG |
45 | memcpy(&lsm.u.snapshot_output.output, output, |
46 | sizeof(lsm.u.snapshot_output.output)); | |
47 | ||
48 | ret = lttng_ctl_ask_sessiond(&lsm, (void **) &reply); | |
49 | if (ret < 0) { | |
e1b624d0 | 50 | goto end; |
da3c9ec1 DG |
51 | } |
52 | ||
53 | output->id = reply->id; | |
54 | free(reply); | |
e1b624d0 JG |
55 | ret = 0; |
56 | end: | |
57 | return ret; | |
da3c9ec1 DG |
58 | } |
59 | ||
60 | /* | |
61 | * Delete an output object to a session identified by name. | |
62 | * | |
63 | * Return 0 on success or else a negative LTTNG_ERR code. | |
64 | */ | |
65 | int lttng_snapshot_del_output(const char *session_name, | |
66 | struct lttng_snapshot_output *output) | |
67 | { | |
e1b624d0 | 68 | int ret; |
da3c9ec1 DG |
69 | struct lttcomm_session_msg lsm; |
70 | ||
71 | if (!session_name || !output) { | |
e1b624d0 JG |
72 | ret = -LTTNG_ERR_INVALID; |
73 | goto end; | |
da3c9ec1 DG |
74 | } |
75 | ||
76 | memset(&lsm, 0, sizeof(lsm)); | |
77 | lsm.cmd_type = LTTNG_SNAPSHOT_DEL_OUTPUT; | |
78 | ||
e1b624d0 JG |
79 | ret = lttng_strncpy(lsm.session.name, session_name, |
80 | sizeof(lsm.session.name)); | |
81 | if (ret) { | |
82 | ret = -LTTNG_ERR_INVALID; | |
83 | goto end; | |
84 | } | |
85 | ||
da3c9ec1 DG |
86 | memcpy(&lsm.u.snapshot_output.output, output, |
87 | sizeof(lsm.u.snapshot_output.output)); | |
88 | ||
e1b624d0 JG |
89 | ret = lttng_ctl_ask_sessiond(&lsm, NULL); |
90 | end: | |
91 | return ret; | |
da3c9ec1 DG |
92 | } |
93 | ||
94 | /* | |
95 | * List all snapshot output(s) of a session identified by name. The output list | |
96 | * object is populated and can be iterated over with the get_next call below. | |
97 | * | |
98 | * Return 0 on success or else a negative LTTNG_ERR code and the list pointer | |
99 | * is untouched. | |
100 | */ | |
101 | int lttng_snapshot_list_output(const char *session_name, | |
102 | struct lttng_snapshot_output_list **list) | |
103 | { | |
104 | int ret; | |
105 | struct lttcomm_session_msg lsm; | |
106 | struct lttng_snapshot_output_list *new_list = NULL; | |
107 | ||
108 | if (!session_name || !list) { | |
109 | ret = -LTTNG_ERR_INVALID; | |
110 | goto error; | |
111 | } | |
112 | ||
113 | memset(&lsm, 0, sizeof(lsm)); | |
114 | lsm.cmd_type = LTTNG_SNAPSHOT_LIST_OUTPUT; | |
115 | ||
e1b624d0 JG |
116 | ret = lttng_strncpy(lsm.session.name, session_name, |
117 | sizeof(lsm.session.name)); | |
118 | if (ret) { | |
119 | ret = -LTTNG_ERR_INVALID; | |
120 | goto error; | |
121 | } | |
da3c9ec1 | 122 | |
64803277 | 123 | new_list = zmalloc<lttng_snapshot_output_list>(); |
da3c9ec1 DG |
124 | if (!new_list) { |
125 | ret = -LTTNG_ERR_NOMEM; | |
126 | goto error; | |
127 | } | |
128 | ||
129 | ret = lttng_ctl_ask_sessiond(&lsm, (void **) &new_list->array); | |
130 | if (ret < 0) { | |
131 | goto free_error; | |
132 | } | |
133 | ||
134 | new_list->count = ret / sizeof(struct lttng_snapshot_output); | |
135 | *list = new_list; | |
136 | return 0; | |
137 | ||
138 | free_error: | |
139 | free(new_list); | |
140 | error: | |
141 | return ret; | |
142 | } | |
143 | ||
144 | /* | |
145 | * Return the next available snapshot output object in the given list. A list | |
146 | * output command MUST have been done before. | |
147 | * | |
148 | * Return the next object on success or else NULL indicating the end of the | |
149 | * list. | |
150 | */ | |
151 | struct lttng_snapshot_output *lttng_snapshot_output_list_get_next( | |
152 | struct lttng_snapshot_output_list *list) | |
153 | { | |
154 | struct lttng_snapshot_output *output = NULL; | |
155 | ||
156 | if (!list) { | |
157 | goto error; | |
158 | } | |
159 | ||
160 | /* We've reached the end. */ | |
161 | if (list->index == list->count) { | |
162 | goto end; | |
163 | } | |
164 | ||
165 | output = &list->array[list->index]; | |
166 | list->index++; | |
167 | ||
168 | end: | |
169 | error: | |
170 | return output; | |
171 | } | |
172 | ||
173 | /* | |
174 | * Free an output list object. | |
175 | */ | |
176 | void lttng_snapshot_output_list_destroy(struct lttng_snapshot_output_list *list) | |
177 | { | |
178 | if (!list) { | |
179 | return; | |
180 | } | |
181 | ||
182 | free(list->array); | |
183 | free(list); | |
184 | } | |
185 | ||
186 | /* | |
187 | * Snapshot a trace for the given session. | |
188 | * | |
189 | * The output object can be NULL but an add output MUST be done prior to this | |
190 | * call. If it's not NULL, it will be used to snapshot a trace. | |
191 | * | |
192 | * The wait parameter is ignored for now. The snapshot record command will | |
193 | * ALWAYS wait for the snapshot to complete before returning meaning the | |
194 | * snapshot has been written on disk or streamed over the network to a relayd. | |
195 | * | |
196 | * Return 0 on success or else a negative LTTNG_ERR value. | |
197 | */ | |
198 | int lttng_snapshot_record(const char *session_name, | |
f46376a1 MJ |
199 | struct lttng_snapshot_output *output, |
200 | int wait __attribute__((unused))) | |
da3c9ec1 | 201 | { |
e1b624d0 | 202 | int ret; |
da3c9ec1 DG |
203 | struct lttcomm_session_msg lsm; |
204 | ||
205 | if (!session_name) { | |
e1b624d0 JG |
206 | ret = -LTTNG_ERR_INVALID; |
207 | goto end; | |
da3c9ec1 DG |
208 | } |
209 | ||
210 | memset(&lsm, 0, sizeof(lsm)); | |
211 | lsm.cmd_type = LTTNG_SNAPSHOT_RECORD; | |
212 | ||
e1b624d0 | 213 | ret = lttng_strncpy(lsm.session.name, session_name, |
da3c9ec1 | 214 | sizeof(lsm.session.name)); |
e1b624d0 JG |
215 | if (ret) { |
216 | ret = -LTTNG_ERR_INVALID; | |
217 | goto end; | |
218 | } | |
da3c9ec1 DG |
219 | |
220 | /* | |
221 | * Not having an output object will use the default one of the session that | |
222 | * would need to be set by a call to add output prior to calling snapshot | |
223 | * record. | |
224 | */ | |
225 | if (output) { | |
226 | memcpy(&lsm.u.snapshot_record.output, output, | |
227 | sizeof(lsm.u.snapshot_record.output)); | |
228 | } | |
229 | ||
230 | /* The wait param is ignored. */ | |
e1b624d0 JG |
231 | ret = lttng_ctl_ask_sessiond(&lsm, NULL); |
232 | end: | |
233 | return ret; | |
da3c9ec1 DG |
234 | } |
235 | ||
236 | /* | |
237 | * Return an newly allocated snapshot output object or NULL on error. | |
238 | */ | |
239 | struct lttng_snapshot_output *lttng_snapshot_output_create(void) | |
240 | { | |
e1986656 DG |
241 | struct lttng_snapshot_output *output; |
242 | ||
64803277 | 243 | output = zmalloc<lttng_snapshot_output>(); |
e1986656 DG |
244 | if (!output) { |
245 | goto error; | |
246 | } | |
247 | ||
248 | output->max_size = (uint64_t) -1ULL; | |
249 | ||
250 | error: | |
251 | return output; | |
da3c9ec1 DG |
252 | } |
253 | ||
254 | /* | |
255 | * Free a given snapshot output object. | |
256 | */ | |
257 | void lttng_snapshot_output_destroy(struct lttng_snapshot_output *obj) | |
258 | { | |
259 | if (obj) { | |
260 | free(obj); | |
261 | } | |
262 | } | |
263 | ||
264 | /* | |
265 | * Getter family functions of snapshot output. | |
266 | */ | |
267 | ||
0de2479d | 268 | uint32_t lttng_snapshot_output_get_id(const struct lttng_snapshot_output *output) |
da3c9ec1 DG |
269 | { |
270 | return output->id; | |
271 | } | |
272 | ||
273 | const char *lttng_snapshot_output_get_name( | |
0de2479d | 274 | const struct lttng_snapshot_output *output) |
da3c9ec1 DG |
275 | { |
276 | return output->name; | |
277 | } | |
278 | ||
0de2479d | 279 | const char *lttng_snapshot_output_get_data_url(const struct lttng_snapshot_output *output) |
da3c9ec1 DG |
280 | { |
281 | return output->data_url; | |
282 | } | |
283 | ||
0de2479d | 284 | const char *lttng_snapshot_output_get_ctrl_url(const struct lttng_snapshot_output *output) |
da3c9ec1 DG |
285 | { |
286 | return output->ctrl_url; | |
287 | } | |
288 | ||
289 | uint64_t lttng_snapshot_output_get_maxsize( | |
0de2479d | 290 | const struct lttng_snapshot_output *output) |
da3c9ec1 DG |
291 | { |
292 | return output->max_size; | |
293 | } | |
294 | ||
295 | /* | |
296 | * Setter family functions for snapshot output. | |
297 | */ | |
298 | ||
299 | int lttng_snapshot_output_set_id(uint32_t id, | |
300 | struct lttng_snapshot_output *output) | |
301 | { | |
302 | if (!output || id == 0) { | |
303 | return -LTTNG_ERR_INVALID; | |
304 | } | |
305 | ||
306 | output->id = id; | |
307 | return 0; | |
308 | } | |
309 | ||
310 | int lttng_snapshot_output_set_size(uint64_t size, | |
311 | struct lttng_snapshot_output *output) | |
312 | { | |
313 | if (!output) { | |
314 | return -LTTNG_ERR_INVALID; | |
315 | } | |
316 | ||
317 | output->max_size = size; | |
318 | return 0; | |
319 | } | |
320 | ||
321 | int lttng_snapshot_output_set_name(const char *name, | |
322 | struct lttng_snapshot_output *output) | |
323 | { | |
e1b624d0 JG |
324 | int ret; |
325 | ||
da3c9ec1 | 326 | if (!output || !name) { |
e1b624d0 JG |
327 | ret = -LTTNG_ERR_INVALID; |
328 | goto end; | |
da3c9ec1 DG |
329 | } |
330 | ||
e1b624d0 JG |
331 | ret = lttng_strncpy(output->name, name, sizeof(output->name)); |
332 | if (ret) { | |
333 | ret = -LTTNG_ERR_INVALID; | |
334 | goto end; | |
335 | } | |
336 | ||
337 | end: | |
338 | return ret; | |
da3c9ec1 DG |
339 | } |
340 | ||
341 | int lttng_snapshot_output_set_ctrl_url(const char *url, | |
342 | struct lttng_snapshot_output *output) | |
343 | { | |
e1b624d0 JG |
344 | int ret; |
345 | ||
da3c9ec1 | 346 | if (!output || !url) { |
e1b624d0 JG |
347 | ret = -LTTNG_ERR_INVALID; |
348 | goto end; | |
da3c9ec1 DG |
349 | } |
350 | ||
e1b624d0 JG |
351 | ret = lttng_strncpy(output->ctrl_url, url, sizeof(output->ctrl_url)); |
352 | if (ret) { | |
353 | ret = -LTTNG_ERR_INVALID; | |
354 | goto end; | |
355 | } | |
356 | ||
357 | end: | |
358 | return ret; | |
da3c9ec1 DG |
359 | } |
360 | ||
361 | int lttng_snapshot_output_set_data_url(const char *url, | |
362 | struct lttng_snapshot_output *output) | |
363 | { | |
e1b624d0 JG |
364 | int ret; |
365 | ||
da3c9ec1 | 366 | if (!output || !url) { |
e1b624d0 JG |
367 | ret = -LTTNG_ERR_INVALID; |
368 | goto end; | |
da3c9ec1 DG |
369 | } |
370 | ||
e1b624d0 JG |
371 | ret = lttng_strncpy(output->data_url, url, sizeof(output->data_url)); |
372 | if (ret) { | |
373 | ret = -LTTNG_ERR_INVALID; | |
374 | goto end; | |
375 | } | |
376 | ||
377 | end: | |
378 | return ret; | |
da3c9ec1 | 379 | } |
b30fa191 JR |
380 | |
381 | int lttng_snapshot_output_set_local_path(const char *path, | |
382 | struct lttng_snapshot_output *output) | |
383 | { | |
384 | int ret; | |
385 | struct lttng_uri *uris = NULL; | |
386 | ssize_t num_uris; | |
387 | ||
388 | if (!path || !output) { | |
389 | ret = -LTTNG_ERR_INVALID; | |
390 | goto end; | |
391 | } | |
392 | ||
393 | num_uris = uri_parse_str_urls(path, NULL, &uris); | |
394 | if (num_uris != 1) { | |
395 | ret = -LTTNG_ERR_INVALID; | |
396 | goto end; | |
397 | } | |
398 | ||
399 | if (uris[0].dtype != LTTNG_DST_PATH) { | |
400 | ret = -LTTNG_ERR_INVALID; | |
401 | goto end; | |
402 | } | |
403 | ||
404 | ret = lttng_strncpy(output->ctrl_url, path, sizeof(output->ctrl_url)); | |
405 | if (ret != 0) { | |
406 | ret = -LTTNG_ERR_INVALID; | |
407 | goto end; | |
408 | } | |
409 | ||
410 | end: | |
411 | free(uris); | |
412 | return ret; | |
413 | } | |
414 | ||
415 | int lttng_snapshot_output_set_network_url(const char *url, | |
416 | struct lttng_snapshot_output *output) | |
417 | { | |
418 | int ret; | |
419 | struct lttng_uri *uris = NULL; | |
420 | ssize_t num_uris; | |
421 | ||
422 | if (!url || !output) { | |
423 | ret = -LTTNG_ERR_INVALID; | |
424 | goto end; | |
425 | } | |
426 | ||
427 | num_uris = uri_parse_str_urls(url, NULL, &uris); | |
428 | if (num_uris != 2) { | |
429 | ret = -LTTNG_ERR_INVALID; | |
430 | goto end; | |
431 | } | |
432 | ||
433 | if (uris[0].dtype != LTTNG_DST_IPV4 && | |
434 | uris[0].dtype != LTTNG_DST_IPV6) { | |
435 | ret = -LTTNG_ERR_INVALID; | |
436 | goto end; | |
437 | } | |
438 | ||
439 | if (uris[1].dtype != LTTNG_DST_IPV4 && | |
440 | uris[1].dtype != LTTNG_DST_IPV6) { | |
441 | ret = -LTTNG_ERR_INVALID; | |
442 | goto end; | |
443 | } | |
444 | ||
445 | ret = lttng_strncpy(output->ctrl_url, url, sizeof(output->ctrl_url)); | |
446 | if (ret != 0) { | |
447 | ret = -LTTNG_ERR_INVALID; | |
448 | goto end; | |
449 | } | |
450 | ||
451 | end: | |
452 | free(uris); | |
453 | return ret; | |
454 | } | |
455 | ||
456 | int lttng_snapshot_output_set_network_urls( | |
457 | const char *ctrl_url, const char *data_url, | |
458 | struct lttng_snapshot_output *output) | |
459 | { | |
460 | int ret; | |
461 | struct lttng_uri *uris = NULL; | |
462 | ssize_t num_uris; | |
463 | ||
464 | if (!ctrl_url || !data_url || !output) { | |
465 | ret = -LTTNG_ERR_INVALID; | |
466 | goto end; | |
467 | } | |
468 | ||
469 | num_uris = uri_parse_str_urls(ctrl_url, data_url, &uris); | |
470 | if (num_uris != 2) { | |
471 | ret = -LTTNG_ERR_INVALID; | |
472 | goto end; | |
473 | } | |
474 | ||
475 | if (uris[0].dtype != LTTNG_DST_IPV4 && | |
476 | uris[0].dtype != LTTNG_DST_IPV6) { | |
477 | ret = -LTTNG_ERR_INVALID; | |
478 | goto end; | |
479 | } | |
480 | ||
481 | if (uris[1].dtype != LTTNG_DST_IPV4 && | |
482 | uris[1].dtype != LTTNG_DST_IPV6) { | |
483 | ret = -LTTNG_ERR_INVALID; | |
484 | goto end; | |
485 | } | |
486 | ||
487 | ret = lttng_strncpy(output->ctrl_url, ctrl_url, sizeof(output->ctrl_url)); | |
488 | if (ret != 0) { | |
489 | ret = -LTTNG_ERR_INVALID; | |
490 | goto end; | |
491 | } | |
492 | ||
493 | ret = lttng_strncpy(output->data_url, data_url, sizeof(output->data_url)); | |
494 | if (ret != 0) { | |
495 | ret = -LTTNG_ERR_INVALID; | |
496 | goto end; | |
497 | } | |
498 | ||
499 | end: | |
500 | free(uris); | |
501 | return ret; | |
502 | } |