2 * Copyright (C) 2020 Simon Marchi <simon.marchi@efficios.com>
4 * SPDX-License-Identifier: LGPL-2.1-only
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>
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) {
82 * This is essentially the same as `struct lttng_snapshot_output`, but packed.
84 struct lttng_snapshot_output_comm
{
87 char name
[LTTNG_NAME_MAX
];
88 char ctrl_url
[PATH_MAX
];
89 char data_url
[PATH_MAX
];
93 int lttng_snapshot_output_serialize(
94 const struct lttng_snapshot_output
*output
,
95 struct lttng_payload
*payload
)
97 struct lttng_snapshot_output_comm comm
;
100 comm
.id
= output
->id
;
101 comm
.max_size
= output
->max_size
;
103 ret
= lttng_strncpy(comm
.name
, output
->name
, sizeof(comm
.name
));
109 comm
.ctrl_url
, output
->ctrl_url
, sizeof(comm
.ctrl_url
));
115 comm
.data_url
, output
->data_url
, sizeof(comm
.data_url
));
120 ret
= lttng_dynamic_buffer_append(
121 &payload
->buffer
, &comm
, sizeof(comm
));
130 ssize_t
lttng_snapshot_output_create_from_payload(
131 struct lttng_payload_view
*view
,
132 struct lttng_snapshot_output
**output_p
)
134 const struct lttng_snapshot_output_comm
*comm
;
135 struct lttng_snapshot_output
*output
= NULL
;
138 if (view
->buffer
.size
!= sizeof(*comm
)) {
143 output
= lttng_snapshot_output_create();
149 comm
= (typeof(comm
)) view
->buffer
.data
;
151 output
->id
= comm
->id
;
152 output
->max_size
= comm
->max_size
;
154 ret
= lttng_strncpy(output
->name
, comm
->name
, sizeof(output
->name
));
159 ret
= lttng_strncpy(output
->ctrl_url
, comm
->ctrl_url
,
160 sizeof(output
->ctrl_url
));
165 ret
= lttng_strncpy(output
->data_url
, comm
->data_url
,
166 sizeof(output
->data_url
));
176 lttng_snapshot_output_destroy(output
);
180 enum lttng_error_code
lttng_snapshot_output_mi_serialize(
181 const struct lttng_snapshot_output
*output
,
182 struct mi_writer
*writer
)
185 enum lttng_error_code ret_code
;
187 LTTNG_ASSERT(output
);
188 LTTNG_ASSERT(writer
);
190 /* Open output element. */
191 ret
= mi_lttng_writer_open_element(writer
,
192 mi_lttng_element_action_snapshot_session_output
);
198 if (strnlen(output
->name
, LTTNG_NAME_MAX
) != 0) {
199 ret
= mi_lttng_writer_write_element_string(
200 writer
, config_element_name
, output
->name
);
206 /* Control url (always present). */
207 ret
= mi_lttng_writer_write_element_string(writer
,
208 mi_lttng_element_snapshot_ctrl_url
, output
->ctrl_url
);
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
,
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.
228 * The value is not serialized when it is set to either of those values
229 * to normalize them to '0'.
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
,
241 /* Close output element. */
242 ret
= mi_lttng_writer_close_element(writer
);
251 ret_code
= LTTNG_ERR_MI_IO_FAIL
;