Tests: Add test to check shared-memory FD leaks after relayd dies
[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 namespace {
81 /*
82 * This is essentially the same as `struct lttng_snapshot_output`, but packed.
83 */
84 struct 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;
91 } /* namespace */
92
93 int lttng_snapshot_output_serialize(
94 const struct lttng_snapshot_output *output,
95 struct lttng_payload *payload)
96 {
97 struct lttng_snapshot_output_comm comm;
98 int ret;
99
100 comm.id = output->id;
101 comm.max_size = output->max_size;
102
103 ret = lttng_strncpy(comm.name, output->name, sizeof(comm.name));
104 if (ret) {
105 goto end;
106 }
107
108 ret = lttng_strncpy(
109 comm.ctrl_url, output->ctrl_url, sizeof(comm.ctrl_url));
110 if (ret) {
111 goto end;
112 }
113
114 ret = lttng_strncpy(
115 comm.data_url, output->data_url, sizeof(comm.data_url));
116 if (ret) {
117 goto end;
118 }
119
120 ret = lttng_dynamic_buffer_append(
121 &payload->buffer, &comm, sizeof(comm));
122 if (ret) {
123 goto end;
124 }
125
126 end:
127 return ret;
128 }
129
130 ssize_t lttng_snapshot_output_create_from_payload(
131 struct lttng_payload_view *view,
132 struct lttng_snapshot_output **output_p)
133 {
134 const struct lttng_snapshot_output_comm *comm;
135 struct lttng_snapshot_output *output = NULL;
136 int ret;
137
138 if (view->buffer.size != sizeof(*comm)) {
139 ret = -1;
140 goto end;
141 }
142
143 output = lttng_snapshot_output_create();
144 if (!output) {
145 ret = -1;
146 goto end;
147 }
148
149 comm = (typeof(comm)) view->buffer.data;
150
151 output->id = comm->id;
152 output->max_size = comm->max_size;
153
154 ret = lttng_strncpy(output->name, comm->name, sizeof(output->name));
155 if (ret) {
156 goto end;
157 }
158
159 ret = lttng_strncpy(output->ctrl_url, comm->ctrl_url,
160 sizeof(output->ctrl_url));
161 if (ret) {
162 goto end;
163 }
164
165 ret = lttng_strncpy(output->data_url, comm->data_url,
166 sizeof(output->data_url));
167 if (ret) {
168 goto end;
169 }
170
171 *output_p = output;
172 output = NULL;
173 ret = sizeof(*comm);
174
175 end:
176 lttng_snapshot_output_destroy(output);
177 return ret;
178 }
179
180 enum lttng_error_code lttng_snapshot_output_mi_serialize(
181 const struct lttng_snapshot_output *output,
182 struct mi_writer *writer)
183 {
184 int ret;
185 enum lttng_error_code ret_code;
186
187 LTTNG_ASSERT(output);
188 LTTNG_ASSERT(writer);
189
190 /* Open output element. */
191 ret = mi_lttng_writer_open_element(writer,
192 mi_lttng_element_action_snapshot_session_output);
193 if (ret) {
194 goto mi_error;
195 }
196
197 /* Name. */
198 if (strnlen(output->name, LTTNG_NAME_MAX) != 0) {
199 ret = mi_lttng_writer_write_element_string(
200 writer, config_element_name, output->name);
201 if (ret) {
202 goto mi_error;
203 }
204 }
205
206 /* Control url (always present). */
207 ret = mi_lttng_writer_write_element_string(writer,
208 mi_lttng_element_snapshot_ctrl_url, output->ctrl_url);
209 if (ret) {
210 goto mi_error;
211 }
212
213 /* Data url (optional). */
214 if (strnlen(output->data_url, PATH_MAX) != 0) {
215 ret = mi_lttng_writer_write_element_string(writer,
216 mi_lttng_element_snapshot_data_url,
217 output->data_url);
218 if (ret) {
219 goto mi_error;
220 }
221 }
222
223 /*
224 * Maximum size in bytes of the snapshot meaning the total size of all
225 * streams combined. A value of 0 means unlimited. The default value is
226 * UINT64_MAX which also means unlimited in practice.
227 *
228 * The value is not serialized when it is set to either of those values
229 * to normalize them to '0'.
230 */
231 if (output->max_size > 0 && output->max_size != UINT64_MAX) {
232 /* Total size of all stream combined. */
233 ret = mi_lttng_writer_write_element_unsigned_int(writer,
234 mi_lttng_element_snapshot_max_size,
235 output->max_size);
236 if (ret) {
237 goto mi_error;
238 }
239 }
240
241 /* Close output element. */
242 ret = mi_lttng_writer_close_element(writer);
243 if (ret) {
244 goto mi_error;
245 }
246
247 ret_code = LTTNG_OK;
248 goto end;
249
250 mi_error:
251 ret_code = LTTNG_ERR_MI_IO_FAIL;
252 end:
253 return ret_code;
254 }
This page took 0.033234 seconds and 4 git commands to generate.