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>
14 #include <lttng/snapshot-internal.hpp>
15 #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
)) {
50 bool lttng_snapshot_output_is_equal(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(const struct lttng_snapshot_output
*output
,
94 struct lttng_payload
*payload
)
96 struct lttng_snapshot_output_comm comm
;
100 comm
.max_size
= output
->max_size
;
102 ret
= lttng_strncpy(comm
.name
, output
->name
, sizeof(comm
.name
));
107 ret
= lttng_strncpy(comm
.ctrl_url
, output
->ctrl_url
, sizeof(comm
.ctrl_url
));
112 ret
= lttng_strncpy(comm
.data_url
, output
->data_url
, sizeof(comm
.data_url
));
117 ret
= lttng_dynamic_buffer_append(&payload
->buffer
, &comm
, sizeof(comm
));
126 ssize_t
lttng_snapshot_output_create_from_payload(struct lttng_payload_view
*view
,
127 struct lttng_snapshot_output
**output_p
)
129 const struct lttng_snapshot_output_comm
*comm
;
130 struct lttng_snapshot_output
*output
= nullptr;
133 if (view
->buffer
.size
!= sizeof(*comm
)) {
138 output
= lttng_snapshot_output_create();
144 comm
= (typeof(comm
)) view
->buffer
.data
;
146 output
->id
= comm
->id
;
147 output
->max_size
= comm
->max_size
;
149 ret
= lttng_strncpy(output
->name
, comm
->name
, sizeof(output
->name
));
154 ret
= lttng_strncpy(output
->ctrl_url
, comm
->ctrl_url
, sizeof(output
->ctrl_url
));
159 ret
= lttng_strncpy(output
->data_url
, comm
->data_url
, sizeof(output
->data_url
));
169 lttng_snapshot_output_destroy(output
);
173 enum lttng_error_code
lttng_snapshot_output_mi_serialize(const struct lttng_snapshot_output
*output
,
174 struct mi_writer
*writer
)
177 enum lttng_error_code ret_code
;
179 LTTNG_ASSERT(output
);
180 LTTNG_ASSERT(writer
);
182 /* Open output element. */
183 ret
= mi_lttng_writer_open_element(writer
, mi_lttng_element_action_snapshot_session_output
);
189 if (strnlen(output
->name
, LTTNG_NAME_MAX
) != 0) {
190 ret
= mi_lttng_writer_write_element_string(
191 writer
, config_element_name
, output
->name
);
197 /* Control url (always present). */
198 ret
= mi_lttng_writer_write_element_string(
199 writer
, mi_lttng_element_snapshot_ctrl_url
, output
->ctrl_url
);
204 /* Data url (optional). */
205 if (strnlen(output
->data_url
, PATH_MAX
) != 0) {
206 ret
= mi_lttng_writer_write_element_string(
207 writer
, mi_lttng_element_snapshot_data_url
, output
->data_url
);
214 * Maximum size in bytes of the snapshot meaning the total size of all
215 * streams combined. A value of 0 means unlimited. The default value is
216 * UINT64_MAX which also means unlimited in practice.
218 * The value is not serialized when it is set to either of those values
219 * to normalize them to '0'.
221 if (output
->max_size
> 0 && output
->max_size
!= UINT64_MAX
) {
222 /* Total size of all stream combined. */
223 ret
= mi_lttng_writer_write_element_unsigned_int(
224 writer
, mi_lttng_element_snapshot_max_size
, output
->max_size
);
230 /* Close output element. */
231 ret
= mi_lttng_writer_close_element(writer
);
240 ret_code
= LTTNG_ERR_MI_IO_FAIL
;