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>
19 bool lttng_snapshot_output_validate(const struct lttng_snapshot_output
*output
)
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
29 len
= lttng_strnlen(output
->ctrl_url
, sizeof(output
->ctrl_url
));
30 if (len
== 0 || len
>= sizeof(output
->ctrl_url
)) {
34 len
= lttng_strnlen(output
->data_url
, sizeof(output
->data_url
));
35 if (len
>= sizeof(output
->data_url
)) {
39 len
= lttng_strnlen(output
->name
, sizeof(output
->name
));
40 if (len
>= sizeof(output
->name
)) {
51 bool lttng_snapshot_output_is_equal(
52 const struct lttng_snapshot_output
*a
,
53 const struct lttng_snapshot_output
*b
)
60 if (a
->max_size
!= b
->max_size
) {
64 if (strcmp(a
->name
, b
->name
) != 0) {
68 if (strcmp(a
->ctrl_url
, b
->ctrl_url
) != 0) {
72 if (strcmp(a
->data_url
, b
->data_url
) != 0) {
83 * This is essentially the same as `struct lttng_snapshot_output`, but packed.
85 struct lttng_snapshot_output_comm
{
88 char name
[LTTNG_NAME_MAX
];
89 char ctrl_url
[PATH_MAX
];
90 char data_url
[PATH_MAX
];
94 int lttng_snapshot_output_serialize(
95 const struct lttng_snapshot_output
*output
,
96 struct lttng_payload
*payload
)
98 struct lttng_snapshot_output_comm comm
;
101 comm
.id
= output
->id
;
102 comm
.max_size
= output
->max_size
;
104 ret
= lttng_strncpy(comm
.name
, output
->name
, sizeof(comm
.name
));
110 comm
.ctrl_url
, output
->ctrl_url
, sizeof(comm
.ctrl_url
));
116 comm
.data_url
, output
->data_url
, sizeof(comm
.data_url
));
121 ret
= lttng_dynamic_buffer_append(
122 &payload
->buffer
, &comm
, sizeof(comm
));
132 ssize_t
lttng_snapshot_output_create_from_payload(
133 struct lttng_payload_view
*view
,
134 struct lttng_snapshot_output
**output_p
)
136 const struct lttng_snapshot_output_comm
*comm
;
137 struct lttng_snapshot_output
*output
= NULL
;
140 if (view
->buffer
.size
!= sizeof(*comm
)) {
145 output
= lttng_snapshot_output_create();
151 comm
= (typeof(comm
)) view
->buffer
.data
;
153 output
->id
= comm
->id
;
154 output
->max_size
= comm
->max_size
;
156 ret
= lttng_strncpy(output
->name
, comm
->name
, sizeof(output
->name
));
161 ret
= lttng_strncpy(output
->ctrl_url
, comm
->ctrl_url
,
162 sizeof(output
->ctrl_url
));
167 ret
= lttng_strncpy(output
->data_url
, comm
->data_url
,
168 sizeof(output
->data_url
));
178 lttng_snapshot_output_destroy(output
);
183 enum lttng_error_code
lttng_snapshot_output_mi_serialize(
184 const struct lttng_snapshot_output
*output
,
185 struct mi_writer
*writer
)
188 enum lttng_error_code ret_code
;
190 LTTNG_ASSERT(output
);
191 LTTNG_ASSERT(writer
);
193 /* Open output element. */
194 ret
= mi_lttng_writer_open_element(writer
,
195 mi_lttng_element_action_snapshot_session_output
);
201 if (strnlen(output
->name
, LTTNG_NAME_MAX
) != 0) {
202 ret
= mi_lttng_writer_write_element_string(
203 writer
, config_element_name
, output
->name
);
209 /* Control url (always present). */
210 ret
= mi_lttng_writer_write_element_string(writer
,
211 mi_lttng_element_snapshot_ctrl_url
, output
->ctrl_url
);
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
,
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.
231 * The value is not serialized when it is set to either of those values
232 * to normalize them to '0'.
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
,
244 /* Close output element. */
245 ret
= mi_lttng_writer_close_element(writer
);
254 ret_code
= LTTNG_ERR_MI_IO_FAIL
;