Rename C++ header files to .hpp
[lttng-tools.git] / src / lib / lttng-ctl / snapshot.cpp
CommitLineData
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 */
23int 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;
56end:
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 */
65int 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);
90end:
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 */
101int 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
4bd69c5f 123 new_list = (lttng_snapshot_output_list *) zmalloc(sizeof(*new_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
138free_error:
139 free(new_list);
140error:
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 */
151struct 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
168end:
169error:
170 return output;
171}
172
173/*
174 * Free an output list object.
175 */
176void 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 */
198int 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);
232end:
233 return ret;
da3c9ec1
DG
234}
235
236/*
237 * Return an newly allocated snapshot output object or NULL on error.
238 */
239struct lttng_snapshot_output *lttng_snapshot_output_create(void)
240{
e1986656
DG
241 struct lttng_snapshot_output *output;
242
4bd69c5f 243 output = (lttng_snapshot_output *) zmalloc(sizeof(struct lttng_snapshot_output));
e1986656
DG
244 if (!output) {
245 goto error;
246 }
247
248 output->max_size = (uint64_t) -1ULL;
249
250error:
251 return output;
da3c9ec1
DG
252}
253
254/*
255 * Free a given snapshot output object.
256 */
257void 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 268uint32_t lttng_snapshot_output_get_id(const struct lttng_snapshot_output *output)
da3c9ec1
DG
269{
270 return output->id;
271}
272
273const char *lttng_snapshot_output_get_name(
0de2479d 274 const struct lttng_snapshot_output *output)
da3c9ec1
DG
275{
276 return output->name;
277}
278
0de2479d 279const char *lttng_snapshot_output_get_data_url(const struct lttng_snapshot_output *output)
da3c9ec1
DG
280{
281 return output->data_url;
282}
283
0de2479d 284const char *lttng_snapshot_output_get_ctrl_url(const struct lttng_snapshot_output *output)
da3c9ec1
DG
285{
286 return output->ctrl_url;
287}
288
289uint64_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
299int 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
310int 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
321int 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
337end:
338 return ret;
da3c9ec1
DG
339}
340
341int 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
357end:
358 return ret;
da3c9ec1
DG
359}
360
361int 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
377end:
378 return ret;
da3c9ec1 379}
b30fa191
JR
380
381int 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
410end:
411 free(uris);
412 return ret;
413}
414
415int 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
451end:
452 free(uris);
453 return ret;
454}
455
456int 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
499end:
500 free(uris);
501 return ret;
502}
This page took 0.068601 seconds and 4 git commands to generate.