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