2 * Copyright (C) 2020 Simon Marchi <simon.marchi@efficios.com>
4 * SPDX-License-Identifier: LGPL-2.1-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>
20 bool lttng_snapshot_output_validate(const struct lttng_snapshot_output
*output
)
26 * It is mandatory to have a ctrl_url. If there is only one output
27 * URL (in the net://, net6:// or file:// form), it will be in this
30 len
= lttng_strnlen(output
->ctrl_url
, sizeof(output
->ctrl_url
));
31 if (len
== 0 || len
>= sizeof(output
->ctrl_url
)) {
35 len
= lttng_strnlen(output
->data_url
, sizeof(output
->data_url
));
36 if (len
>= sizeof(output
->data_url
)) {
40 len
= lttng_strnlen(output
->name
, sizeof(output
->name
));
41 if (len
>= sizeof(output
->name
)) {
52 bool lttng_snapshot_output_is_equal(
53 const struct lttng_snapshot_output
*a
,
54 const struct lttng_snapshot_output
*b
)
61 if (a
->max_size
!= b
->max_size
) {
65 if (strcmp(a
->name
, b
->name
) != 0) {
69 if (strcmp(a
->ctrl_url
, b
->ctrl_url
) != 0) {
73 if (strcmp(a
->data_url
, b
->data_url
) != 0) {
84 * This is essentially the same as `struct lttng_snapshot_output`, but packed.
86 struct lttng_snapshot_output_comm
{
89 char name
[LTTNG_NAME_MAX
];
90 char ctrl_url
[PATH_MAX
];
91 char data_url
[PATH_MAX
];
95 int lttng_snapshot_output_serialize(
96 const struct lttng_snapshot_output
*output
,
97 struct lttng_payload
*payload
)
99 struct lttng_snapshot_output_comm comm
;
102 comm
.id
= output
->id
;
103 comm
.max_size
= output
->max_size
;
105 ret
= lttng_strncpy(comm
.name
, output
->name
, sizeof(comm
.name
));
111 comm
.ctrl_url
, output
->ctrl_url
, sizeof(comm
.ctrl_url
));
117 comm
.data_url
, output
->data_url
, sizeof(comm
.data_url
));
122 ret
= lttng_dynamic_buffer_append(
123 &payload
->buffer
, &comm
, sizeof(comm
));
133 ssize_t
lttng_snapshot_output_create_from_payload(
134 struct lttng_payload_view
*view
,
135 struct lttng_snapshot_output
**output_p
)
137 const struct lttng_snapshot_output_comm
*comm
;
138 struct lttng_snapshot_output
*output
= NULL
;
141 if (view
->buffer
.size
!= sizeof(*comm
)) {
146 output
= lttng_snapshot_output_create();
152 comm
= (typeof(comm
)) view
->buffer
.data
;
154 output
->id
= comm
->id
;
155 output
->max_size
= comm
->max_size
;
157 ret
= lttng_strncpy(output
->name
, comm
->name
, sizeof(output
->name
));
162 ret
= lttng_strncpy(output
->ctrl_url
, comm
->ctrl_url
,
163 sizeof(output
->ctrl_url
));
168 ret
= lttng_strncpy(output
->data_url
, comm
->data_url
,
169 sizeof(output
->data_url
));
179 lttng_snapshot_output_destroy(output
);
184 enum lttng_error_code
lttng_snapshot_output_mi_serialize(
185 const struct lttng_snapshot_output
*output
,
186 struct mi_writer
*writer
)
189 enum lttng_error_code ret_code
;
194 /* Open output element. */
195 ret
= mi_lttng_writer_open_element(writer
,
196 mi_lttng_element_action_snapshot_session_output
);
202 if (strnlen(output
->name
, LTTNG_NAME_MAX
) != 0) {
203 ret
= mi_lttng_writer_write_element_string(
204 writer
, config_element_name
, output
->name
);
210 /* Control url (always present). */
211 ret
= mi_lttng_writer_write_element_string(writer
,
212 mi_lttng_element_snapshot_ctrl_url
, output
->ctrl_url
);
217 /* Data url (optional). */
218 if (strnlen(output
->data_url
, PATH_MAX
) != 0) {
219 ret
= mi_lttng_writer_write_element_string(writer
,
220 mi_lttng_element_snapshot_data_url
,
228 * Maximum size in bytes of the snapshot meaning the total size of all
229 * streams combined. A value of 0 means unlimited. The default value is
230 * UINT64_MAX which also means unlimited in practice.
232 * The value is not serialized when it is set to either of those values
233 * to normalize them to '0'.
235 if (output
->max_size
> 0 && output
->max_size
!= UINT64_MAX
) {
236 /* Total size of all stream combined. */
237 ret
= mi_lttng_writer_write_element_unsigned_int(writer
,
238 mi_lttng_element_snapshot_max_size
,
245 /* Close output element. */
246 ret
= mi_lttng_writer_close_element(writer
);
255 ret_code
= LTTNG_ERR_MI_IO_FAIL
;