docs: Add supported versions and fix-backport policy
[lttng-tools.git] / src / common / snapshot.cpp
CommitLineData
b30fa191
JR
1/*
2 * Copyright (C) 2020 Simon Marchi <simon.marchi@efficios.com>
3 *
c922647d 4 * SPDX-License-Identifier: LGPL-2.1-only
b30fa191
JR
5 *
6 */
7
c9e313bc
SM
8#include <common/error.hpp>
9#include <common/mi-lttng.hpp>
10#include <common/payload-view.hpp>
11#include <common/payload.hpp>
12#include <common/snapshot.hpp>
28ab034a 13
c9e313bc 14#include <lttng/snapshot-internal.hpp>
b30fa191
JR
15#include <lttng/snapshot.h>
16
b30fa191
JR
17#include <stdlib.h>
18
b30fa191
JR
19bool lttng_snapshot_output_validate(const struct lttng_snapshot_output *output)
20{
21 bool valid = false;
22 size_t len;
23
24 /*
25 * It is mandatory to have a ctrl_url. If there is only one output
26 * URL (in the net://, net6:// or file:// form), it will be in this
27 * field.
28 */
29 len = lttng_strnlen(output->ctrl_url, sizeof(output->ctrl_url));
30 if (len == 0 || len >= sizeof(output->ctrl_url)) {
31 goto end;
32 }
33
34 len = lttng_strnlen(output->data_url, sizeof(output->data_url));
35 if (len >= sizeof(output->data_url)) {
36 goto end;
37 }
38
39 len = lttng_strnlen(output->name, sizeof(output->name));
40 if (len >= sizeof(output->name)) {
41 goto end;
42 }
43
44 valid = true;
45
46end:
47 return valid;
48}
49
28ab034a
JG
50bool lttng_snapshot_output_is_equal(const struct lttng_snapshot_output *a,
51 const struct lttng_snapshot_output *b)
b30fa191
JR
52{
53 bool equal = false;
54
a0377dfe
FD
55 LTTNG_ASSERT(a);
56 LTTNG_ASSERT(b);
b30fa191
JR
57
58 if (a->max_size != b->max_size) {
59 goto end;
60 }
61
62 if (strcmp(a->name, b->name) != 0) {
63 goto end;
64 }
65
66 if (strcmp(a->ctrl_url, b->ctrl_url) != 0) {
67 goto end;
68 }
69
70 if (strcmp(a->data_url, b->data_url) != 0) {
71 goto end;
72 }
73
74 equal = true;
75
76end:
77 return equal;
78}
79
f1494934 80namespace {
b30fa191
JR
81/*
82 * This is essentially the same as `struct lttng_snapshot_output`, but packed.
83 */
84struct lttng_snapshot_output_comm {
85 uint32_t id;
86 uint64_t max_size;
87 char name[LTTNG_NAME_MAX];
88 char ctrl_url[PATH_MAX];
89 char data_url[PATH_MAX];
90} LTTNG_PACKED;
f1494934 91} /* namespace */
b30fa191 92
28ab034a
JG
93int lttng_snapshot_output_serialize(const struct lttng_snapshot_output *output,
94 struct lttng_payload *payload)
b30fa191
JR
95{
96 struct lttng_snapshot_output_comm comm;
97 int ret;
98
99 comm.id = output->id;
100 comm.max_size = output->max_size;
101
102 ret = lttng_strncpy(comm.name, output->name, sizeof(comm.name));
103 if (ret) {
104 goto end;
105 }
106
28ab034a 107 ret = lttng_strncpy(comm.ctrl_url, output->ctrl_url, sizeof(comm.ctrl_url));
b30fa191
JR
108 if (ret) {
109 goto end;
110 }
111
28ab034a 112 ret = lttng_strncpy(comm.data_url, output->data_url, sizeof(comm.data_url));
b30fa191
JR
113 if (ret) {
114 goto end;
115 }
116
28ab034a 117 ret = lttng_dynamic_buffer_append(&payload->buffer, &comm, sizeof(comm));
b30fa191
JR
118 if (ret) {
119 goto end;
120 }
121
122end:
123 return ret;
124}
125
28ab034a
JG
126ssize_t lttng_snapshot_output_create_from_payload(struct lttng_payload_view *view,
127 struct lttng_snapshot_output **output_p)
b30fa191
JR
128{
129 const struct lttng_snapshot_output_comm *comm;
cd9adb8b 130 struct lttng_snapshot_output *output = nullptr;
b30fa191
JR
131 int ret;
132
757c48a2 133 if (view->buffer.size != sizeof(*comm)) {
b30fa191
JR
134 ret = -1;
135 goto end;
136 }
137
138 output = lttng_snapshot_output_create();
139 if (!output) {
140 ret = -1;
141 goto end;
142 }
143
757c48a2 144 comm = (typeof(comm)) view->buffer.data;
b30fa191
JR
145
146 output->id = comm->id;
147 output->max_size = comm->max_size;
148
149 ret = lttng_strncpy(output->name, comm->name, sizeof(output->name));
150 if (ret) {
151 goto end;
152 }
153
28ab034a 154 ret = lttng_strncpy(output->ctrl_url, comm->ctrl_url, sizeof(output->ctrl_url));
b30fa191
JR
155 if (ret) {
156 goto end;
157 }
158
28ab034a 159 ret = lttng_strncpy(output->data_url, comm->data_url, sizeof(output->data_url));
b30fa191
JR
160 if (ret) {
161 goto end;
162 }
163
164 *output_p = output;
cd9adb8b 165 output = nullptr;
b30fa191
JR
166 ret = sizeof(*comm);
167
168end:
169 lttng_snapshot_output_destroy(output);
170 return ret;
171}
6a751b95 172
28ab034a
JG
173enum lttng_error_code lttng_snapshot_output_mi_serialize(const struct lttng_snapshot_output *output,
174 struct mi_writer *writer)
6a751b95
JR
175{
176 int ret;
177 enum lttng_error_code ret_code;
178
a0377dfe
FD
179 LTTNG_ASSERT(output);
180 LTTNG_ASSERT(writer);
6a751b95
JR
181
182 /* Open output element. */
28ab034a 183 ret = mi_lttng_writer_open_element(writer, mi_lttng_element_action_snapshot_session_output);
6a751b95
JR
184 if (ret) {
185 goto mi_error;
186 }
187
188 /* Name. */
189 if (strnlen(output->name, LTTNG_NAME_MAX) != 0) {
190 ret = mi_lttng_writer_write_element_string(
28ab034a 191 writer, config_element_name, output->name);
6a751b95
JR
192 if (ret) {
193 goto mi_error;
194 }
195 }
196
197 /* Control url (always present). */
28ab034a
JG
198 ret = mi_lttng_writer_write_element_string(
199 writer, mi_lttng_element_snapshot_ctrl_url, output->ctrl_url);
6a751b95
JR
200 if (ret) {
201 goto mi_error;
202 }
203
204 /* Data url (optional). */
205 if (strnlen(output->data_url, PATH_MAX) != 0) {
28ab034a
JG
206 ret = mi_lttng_writer_write_element_string(
207 writer, mi_lttng_element_snapshot_data_url, output->data_url);
6a751b95
JR
208 if (ret) {
209 goto mi_error;
210 }
211 }
212
213 /*
214 * Maximum size in bytes of the snapshot meaning the total size of all
215 * streams combined. A value of 0 means unlimited. The default value is
216 * UINT64_MAX which also means unlimited in practice.
217 *
218 * The value is not serialized when it is set to either of those values
219 * to normalize them to '0'.
220 */
221 if (output->max_size > 0 && output->max_size != UINT64_MAX) {
222 /* Total size of all stream combined. */
28ab034a
JG
223 ret = mi_lttng_writer_write_element_unsigned_int(
224 writer, mi_lttng_element_snapshot_max_size, output->max_size);
6a751b95
JR
225 if (ret) {
226 goto mi_error;
227 }
228 }
229
230 /* Close output element. */
231 ret = mi_lttng_writer_close_element(writer);
232 if (ret) {
233 goto mi_error;
234 }
235
236 ret_code = LTTNG_OK;
237 goto end;
238
239mi_error:
240 ret_code = LTTNG_ERR_MI_IO_FAIL;
241end:
242 return ret_code;
243}
This page took 0.063431 seconds and 4 git commands to generate.