Force usage of assert() condition when NDEBUG is defined
[lttng-tools.git] / src / common / snapshot.c
CommitLineData
b30fa191
JR
1/*
2 * Copyright (C) 2020 Simon Marchi <simon.marchi@efficios.com>
3 *
4 * SPDX-License-Identifier: GPL-2.0-only
5 *
6 */
7
6a751b95
JR
8#include <common/error.h>
9#include <common/mi-lttng.h>
9e620ea7 10#include <common/payload-view.h>
6a751b95 11#include <common/payload.h>
b30fa191
JR
12#include <common/snapshot.h>
13#include <lttng/snapshot-internal.h>
14#include <lttng/snapshot.h>
15
b30fa191
JR
16#include <stdlib.h>
17
18LTTNG_HIDDEN
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
50LTTNG_HIDDEN
51bool lttng_snapshot_output_is_equal(
52 const struct lttng_snapshot_output *a,
53 const struct lttng_snapshot_output *b)
54{
55 bool equal = false;
56
a0377dfe
FD
57 LTTNG_ASSERT(a);
58 LTTNG_ASSERT(b);
b30fa191
JR
59
60 if (a->max_size != b->max_size) {
61 goto end;
62 }
63
64 if (strcmp(a->name, b->name) != 0) {
65 goto end;
66 }
67
68 if (strcmp(a->ctrl_url, b->ctrl_url) != 0) {
69 goto end;
70 }
71
72 if (strcmp(a->data_url, b->data_url) != 0) {
73 goto end;
74 }
75
76 equal = true;
77
78end:
79 return equal;
80}
81
82/*
83 * This is essentially the same as `struct lttng_snapshot_output`, but packed.
84 */
85struct lttng_snapshot_output_comm {
86 uint32_t id;
87 uint64_t max_size;
88 char name[LTTNG_NAME_MAX];
89 char ctrl_url[PATH_MAX];
90 char data_url[PATH_MAX];
91} LTTNG_PACKED;
92
93LTTNG_HIDDEN
94int lttng_snapshot_output_serialize(
95 const struct lttng_snapshot_output *output,
757c48a2 96 struct lttng_payload *payload)
b30fa191
JR
97{
98 struct lttng_snapshot_output_comm comm;
99 int ret;
100
101 comm.id = output->id;
102 comm.max_size = output->max_size;
103
104 ret = lttng_strncpy(comm.name, output->name, sizeof(comm.name));
105 if (ret) {
106 goto end;
107 }
108
757c48a2
SM
109 ret = lttng_strncpy(
110 comm.ctrl_url, output->ctrl_url, sizeof(comm.ctrl_url));
b30fa191
JR
111 if (ret) {
112 goto end;
113 }
114
757c48a2
SM
115 ret = lttng_strncpy(
116 comm.data_url, output->data_url, sizeof(comm.data_url));
b30fa191
JR
117 if (ret) {
118 goto end;
119 }
120
757c48a2
SM
121 ret = lttng_dynamic_buffer_append(
122 &payload->buffer, &comm, sizeof(comm));
b30fa191
JR
123 if (ret) {
124 goto end;
125 }
126
127end:
128 return ret;
129}
130
131LTTNG_HIDDEN
757c48a2
SM
132ssize_t lttng_snapshot_output_create_from_payload(
133 struct lttng_payload_view *view,
b30fa191
JR
134 struct lttng_snapshot_output **output_p)
135{
136 const struct lttng_snapshot_output_comm *comm;
137 struct lttng_snapshot_output *output = NULL;
138 int ret;
139
757c48a2 140 if (view->buffer.size != sizeof(*comm)) {
b30fa191
JR
141 ret = -1;
142 goto end;
143 }
144
145 output = lttng_snapshot_output_create();
146 if (!output) {
147 ret = -1;
148 goto end;
149 }
150
757c48a2 151 comm = (typeof(comm)) view->buffer.data;
b30fa191
JR
152
153 output->id = comm->id;
154 output->max_size = comm->max_size;
155
156 ret = lttng_strncpy(output->name, comm->name, sizeof(output->name));
157 if (ret) {
158 goto end;
159 }
160
757c48a2
SM
161 ret = lttng_strncpy(output->ctrl_url, comm->ctrl_url,
162 sizeof(output->ctrl_url));
b30fa191
JR
163 if (ret) {
164 goto end;
165 }
166
757c48a2
SM
167 ret = lttng_strncpy(output->data_url, comm->data_url,
168 sizeof(output->data_url));
b30fa191
JR
169 if (ret) {
170 goto end;
171 }
172
173 *output_p = output;
174 output = NULL;
175 ret = sizeof(*comm);
176
177end:
178 lttng_snapshot_output_destroy(output);
179 return ret;
180}
6a751b95
JR
181
182LTTNG_HIDDEN
183enum lttng_error_code lttng_snapshot_output_mi_serialize(
184 const struct lttng_snapshot_output *output,
185 struct mi_writer *writer)
186{
187 int ret;
188 enum lttng_error_code ret_code;
189
a0377dfe
FD
190 LTTNG_ASSERT(output);
191 LTTNG_ASSERT(writer);
6a751b95
JR
192
193 /* Open output element. */
194 ret = mi_lttng_writer_open_element(writer,
195 mi_lttng_element_action_snapshot_session_output);
196 if (ret) {
197 goto mi_error;
198 }
199
200 /* Name. */
201 if (strnlen(output->name, LTTNG_NAME_MAX) != 0) {
202 ret = mi_lttng_writer_write_element_string(
203 writer, config_element_name, output->name);
204 if (ret) {
205 goto mi_error;
206 }
207 }
208
209 /* Control url (always present). */
210 ret = mi_lttng_writer_write_element_string(writer,
211 mi_lttng_element_snapshot_ctrl_url, output->ctrl_url);
212 if (ret) {
213 goto mi_error;
214 }
215
216 /* Data url (optional). */
217 if (strnlen(output->data_url, PATH_MAX) != 0) {
218 ret = mi_lttng_writer_write_element_string(writer,
219 mi_lttng_element_snapshot_data_url,
220 output->data_url);
221 if (ret) {
222 goto mi_error;
223 }
224 }
225
226 /*
227 * Maximum size in bytes of the snapshot meaning the total size of all
228 * streams combined. A value of 0 means unlimited. The default value is
229 * UINT64_MAX which also means unlimited in practice.
230 *
231 * The value is not serialized when it is set to either of those values
232 * to normalize them to '0'.
233 */
234 if (output->max_size > 0 && output->max_size != UINT64_MAX) {
235 /* Total size of all stream combined. */
236 ret = mi_lttng_writer_write_element_unsigned_int(writer,
237 mi_lttng_element_snapshot_max_size,
238 output->max_size);
239 if (ret) {
240 goto mi_error;
241 }
242 }
243
244 /* Close output element. */
245 ret = mi_lttng_writer_close_element(writer);
246 if (ret) {
247 goto mi_error;
248 }
249
250 ret_code = LTTNG_OK;
251 goto end;
252
253mi_error:
254 ret_code = LTTNG_ERR_MI_IO_FAIL;
255end:
256 return ret_code;
257}
This page took 0.037338 seconds and 4 git commands to generate.