| 1 | /* |
| 2 | * Copyright (C) 2020 Simon Marchi <simon.marchi@efficios.com> |
| 3 | * |
| 4 | * SPDX-License-Identifier: LGPL-2.1-only |
| 5 | * |
| 6 | */ |
| 7 | |
| 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 | |
| 14 | #include <lttng/snapshot-internal.hpp> |
| 15 | #include <lttng/snapshot.h> |
| 16 | |
| 17 | #include <stdlib.h> |
| 18 | |
| 19 | bool lttng_snapshot_output_validate(const struct lttng_snapshot_output *output) |
| 20 | { |
| 21 | bool valid = false; |
| 22 | size_t len; |
| 23 | |
| 24 | /* |
| 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 |
| 27 | * field. |
| 28 | */ |
| 29 | len = lttng_strnlen(output->ctrl_url, sizeof(output->ctrl_url)); |
| 30 | if (len == 0 || len >= sizeof(output->ctrl_url)) { |
| 31 | goto end; |
| 32 | } |
| 33 | |
| 34 | len = lttng_strnlen(output->data_url, sizeof(output->data_url)); |
| 35 | if (len >= sizeof(output->data_url)) { |
| 36 | goto end; |
| 37 | } |
| 38 | |
| 39 | len = lttng_strnlen(output->name, sizeof(output->name)); |
| 40 | if (len >= sizeof(output->name)) { |
| 41 | goto end; |
| 42 | } |
| 43 | |
| 44 | valid = true; |
| 45 | |
| 46 | end: |
| 47 | return valid; |
| 48 | } |
| 49 | |
| 50 | bool lttng_snapshot_output_is_equal(const struct lttng_snapshot_output *a, |
| 51 | const struct lttng_snapshot_output *b) |
| 52 | { |
| 53 | bool equal = false; |
| 54 | |
| 55 | LTTNG_ASSERT(a); |
| 56 | LTTNG_ASSERT(b); |
| 57 | |
| 58 | if (a->max_size != b->max_size) { |
| 59 | goto end; |
| 60 | } |
| 61 | |
| 62 | if (strcmp(a->name, b->name) != 0) { |
| 63 | goto end; |
| 64 | } |
| 65 | |
| 66 | if (strcmp(a->ctrl_url, b->ctrl_url) != 0) { |
| 67 | goto end; |
| 68 | } |
| 69 | |
| 70 | if (strcmp(a->data_url, b->data_url) != 0) { |
| 71 | goto end; |
| 72 | } |
| 73 | |
| 74 | equal = true; |
| 75 | |
| 76 | end: |
| 77 | return equal; |
| 78 | } |
| 79 | |
| 80 | namespace { |
| 81 | /* |
| 82 | * This is essentially the same as `struct lttng_snapshot_output`, but packed. |
| 83 | */ |
| 84 | struct lttng_snapshot_output_comm { |
| 85 | uint32_t id; |
| 86 | uint64_t max_size; |
| 87 | char name[LTTNG_NAME_MAX]; |
| 88 | char ctrl_url[PATH_MAX]; |
| 89 | char data_url[PATH_MAX]; |
| 90 | } LTTNG_PACKED; |
| 91 | } /* namespace */ |
| 92 | |
| 93 | int lttng_snapshot_output_serialize(const struct lttng_snapshot_output *output, |
| 94 | struct lttng_payload *payload) |
| 95 | { |
| 96 | struct lttng_snapshot_output_comm comm; |
| 97 | int ret; |
| 98 | |
| 99 | comm.id = output->id; |
| 100 | comm.max_size = output->max_size; |
| 101 | |
| 102 | ret = lttng_strncpy(comm.name, output->name, sizeof(comm.name)); |
| 103 | if (ret) { |
| 104 | goto end; |
| 105 | } |
| 106 | |
| 107 | ret = lttng_strncpy(comm.ctrl_url, output->ctrl_url, sizeof(comm.ctrl_url)); |
| 108 | if (ret) { |
| 109 | goto end; |
| 110 | } |
| 111 | |
| 112 | ret = lttng_strncpy(comm.data_url, output->data_url, sizeof(comm.data_url)); |
| 113 | if (ret) { |
| 114 | goto end; |
| 115 | } |
| 116 | |
| 117 | ret = lttng_dynamic_buffer_append(&payload->buffer, &comm, sizeof(comm)); |
| 118 | if (ret) { |
| 119 | goto end; |
| 120 | } |
| 121 | |
| 122 | end: |
| 123 | return ret; |
| 124 | } |
| 125 | |
| 126 | ssize_t lttng_snapshot_output_create_from_payload(struct lttng_payload_view *view, |
| 127 | struct lttng_snapshot_output **output_p) |
| 128 | { |
| 129 | const struct lttng_snapshot_output_comm *comm; |
| 130 | struct lttng_snapshot_output *output = nullptr; |
| 131 | int ret; |
| 132 | |
| 133 | if (view->buffer.size != sizeof(*comm)) { |
| 134 | ret = -1; |
| 135 | goto end; |
| 136 | } |
| 137 | |
| 138 | output = lttng_snapshot_output_create(); |
| 139 | if (!output) { |
| 140 | ret = -1; |
| 141 | goto end; |
| 142 | } |
| 143 | |
| 144 | comm = (typeof(comm)) view->buffer.data; |
| 145 | |
| 146 | output->id = comm->id; |
| 147 | output->max_size = comm->max_size; |
| 148 | |
| 149 | ret = lttng_strncpy(output->name, comm->name, sizeof(output->name)); |
| 150 | if (ret) { |
| 151 | goto end; |
| 152 | } |
| 153 | |
| 154 | ret = lttng_strncpy(output->ctrl_url, comm->ctrl_url, sizeof(output->ctrl_url)); |
| 155 | if (ret) { |
| 156 | goto end; |
| 157 | } |
| 158 | |
| 159 | ret = lttng_strncpy(output->data_url, comm->data_url, sizeof(output->data_url)); |
| 160 | if (ret) { |
| 161 | goto end; |
| 162 | } |
| 163 | |
| 164 | *output_p = output; |
| 165 | output = nullptr; |
| 166 | ret = sizeof(*comm); |
| 167 | |
| 168 | end: |
| 169 | lttng_snapshot_output_destroy(output); |
| 170 | return ret; |
| 171 | } |
| 172 | |
| 173 | enum lttng_error_code lttng_snapshot_output_mi_serialize(const struct lttng_snapshot_output *output, |
| 174 | struct mi_writer *writer) |
| 175 | { |
| 176 | int ret; |
| 177 | enum lttng_error_code ret_code; |
| 178 | |
| 179 | LTTNG_ASSERT(output); |
| 180 | LTTNG_ASSERT(writer); |
| 181 | |
| 182 | /* Open output element. */ |
| 183 | ret = mi_lttng_writer_open_element(writer, mi_lttng_element_action_snapshot_session_output); |
| 184 | if (ret) { |
| 185 | goto mi_error; |
| 186 | } |
| 187 | |
| 188 | /* Name. */ |
| 189 | if (strnlen(output->name, LTTNG_NAME_MAX) != 0) { |
| 190 | ret = mi_lttng_writer_write_element_string( |
| 191 | writer, config_element_name, output->name); |
| 192 | if (ret) { |
| 193 | goto mi_error; |
| 194 | } |
| 195 | } |
| 196 | |
| 197 | /* Control url (always present). */ |
| 198 | ret = mi_lttng_writer_write_element_string( |
| 199 | writer, mi_lttng_element_snapshot_ctrl_url, output->ctrl_url); |
| 200 | if (ret) { |
| 201 | goto mi_error; |
| 202 | } |
| 203 | |
| 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); |
| 208 | if (ret) { |
| 209 | goto mi_error; |
| 210 | } |
| 211 | } |
| 212 | |
| 213 | /* |
| 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. |
| 217 | * |
| 218 | * The value is not serialized when it is set to either of those values |
| 219 | * to normalize them to '0'. |
| 220 | */ |
| 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); |
| 225 | if (ret) { |
| 226 | goto mi_error; |
| 227 | } |
| 228 | } |
| 229 | |
| 230 | /* Close output element. */ |
| 231 | ret = mi_lttng_writer_close_element(writer); |
| 232 | if (ret) { |
| 233 | goto mi_error; |
| 234 | } |
| 235 | |
| 236 | ret_code = LTTNG_OK; |
| 237 | goto end; |
| 238 | |
| 239 | mi_error: |
| 240 | ret_code = LTTNG_ERR_MI_IO_FAIL; |
| 241 | end: |
| 242 | return ret_code; |
| 243 | } |