2 * Copyright (C) 2020 Simon Marchi <simon.marchi@efficios.com>
4 * SPDX-License-Identifier: GPL-2.0-only
8 #include <common/error.h>
9 #include <common/mi-lttng.h>
10 #include <common/payload-view.h>
11 #include <common/payload.h>
12 #include <common/snapshot.h>
13 #include <lttng/snapshot-internal.h>
14 #include <lttng/snapshot.h>
18 bool lttng_snapshot_output_validate(const struct lttng_snapshot_output
*output
)
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
28 len
= lttng_strnlen(output
->ctrl_url
, sizeof(output
->ctrl_url
));
29 if (len
== 0 || len
>= sizeof(output
->ctrl_url
)) {
33 len
= lttng_strnlen(output
->data_url
, sizeof(output
->data_url
));
34 if (len
>= sizeof(output
->data_url
)) {
38 len
= lttng_strnlen(output
->name
, sizeof(output
->name
));
39 if (len
>= sizeof(output
->name
)) {
49 bool lttng_snapshot_output_is_equal(
50 const struct lttng_snapshot_output
*a
,
51 const struct lttng_snapshot_output
*b
)
58 if (a
->max_size
!= b
->max_size
) {
62 if (strcmp(a
->name
, b
->name
) != 0) {
66 if (strcmp(a
->ctrl_url
, b
->ctrl_url
) != 0) {
70 if (strcmp(a
->data_url
, b
->data_url
) != 0) {
81 * This is essentially the same as `struct lttng_snapshot_output`, but packed.
83 struct lttng_snapshot_output_comm
{
86 char name
[LTTNG_NAME_MAX
];
87 char ctrl_url
[PATH_MAX
];
88 char data_url
[PATH_MAX
];
91 int lttng_snapshot_output_serialize(
92 const struct lttng_snapshot_output
*output
,
93 struct lttng_payload
*payload
)
95 struct lttng_snapshot_output_comm comm
;
99 comm
.max_size
= output
->max_size
;
101 ret
= lttng_strncpy(comm
.name
, output
->name
, sizeof(comm
.name
));
107 comm
.ctrl_url
, output
->ctrl_url
, sizeof(comm
.ctrl_url
));
113 comm
.data_url
, output
->data_url
, sizeof(comm
.data_url
));
118 ret
= lttng_dynamic_buffer_append(
119 &payload
->buffer
, &comm
, sizeof(comm
));
128 ssize_t
lttng_snapshot_output_create_from_payload(
129 struct lttng_payload_view
*view
,
130 struct lttng_snapshot_output
**output_p
)
132 const struct lttng_snapshot_output_comm
*comm
;
133 struct lttng_snapshot_output
*output
= NULL
;
136 if (view
->buffer
.size
!= sizeof(*comm
)) {
141 output
= lttng_snapshot_output_create();
147 comm
= (typeof(comm
)) view
->buffer
.data
;
149 output
->id
= comm
->id
;
150 output
->max_size
= comm
->max_size
;
152 ret
= lttng_strncpy(output
->name
, comm
->name
, sizeof(output
->name
));
157 ret
= lttng_strncpy(output
->ctrl_url
, comm
->ctrl_url
,
158 sizeof(output
->ctrl_url
));
163 ret
= lttng_strncpy(output
->data_url
, comm
->data_url
,
164 sizeof(output
->data_url
));
174 lttng_snapshot_output_destroy(output
);
178 enum lttng_error_code
lttng_snapshot_output_mi_serialize(
179 const struct lttng_snapshot_output
*output
,
180 struct mi_writer
*writer
)
183 enum lttng_error_code ret_code
;
185 LTTNG_ASSERT(output
);
186 LTTNG_ASSERT(writer
);
188 /* Open output element. */
189 ret
= mi_lttng_writer_open_element(writer
,
190 mi_lttng_element_action_snapshot_session_output
);
196 if (strnlen(output
->name
, LTTNG_NAME_MAX
) != 0) {
197 ret
= mi_lttng_writer_write_element_string(
198 writer
, config_element_name
, output
->name
);
204 /* Control url (always present). */
205 ret
= mi_lttng_writer_write_element_string(writer
,
206 mi_lttng_element_snapshot_ctrl_url
, output
->ctrl_url
);
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
,
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.
226 * The value is not serialized when it is set to either of those values
227 * to normalize them to '0'.
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
,
239 /* Close output element. */
240 ret
= mi_lttng_writer_close_element(writer
);
249 ret_code
= LTTNG_ERR_MI_IO_FAIL
;