| 1 | /* |
| 2 | * Copyright (C) 2020 Simon Marchi <simon.marchi@efficios.com> |
| 3 | * |
| 4 | * SPDX-License-Identifier: GPL-2.0-only |
| 5 | * |
| 6 | */ |
| 7 | |
| 8 | #include <common/payload.h> |
| 9 | #include <common/payload-view.h> |
| 10 | #include <common/snapshot.h> |
| 11 | #include <lttng/snapshot-internal.h> |
| 12 | #include <lttng/snapshot.h> |
| 13 | |
| 14 | #include <assert.h> |
| 15 | #include <stdlib.h> |
| 16 | |
| 17 | LTTNG_HIDDEN |
| 18 | bool lttng_snapshot_output_validate(const struct lttng_snapshot_output *output) |
| 19 | { |
| 20 | bool valid = false; |
| 21 | size_t len; |
| 22 | |
| 23 | /* |
| 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 |
| 26 | * field. |
| 27 | */ |
| 28 | len = lttng_strnlen(output->ctrl_url, sizeof(output->ctrl_url)); |
| 29 | if (len == 0 || len >= sizeof(output->ctrl_url)) { |
| 30 | goto end; |
| 31 | } |
| 32 | |
| 33 | len = lttng_strnlen(output->data_url, sizeof(output->data_url)); |
| 34 | if (len >= sizeof(output->data_url)) { |
| 35 | goto end; |
| 36 | } |
| 37 | |
| 38 | len = lttng_strnlen(output->name, sizeof(output->name)); |
| 39 | if (len >= sizeof(output->name)) { |
| 40 | goto end; |
| 41 | } |
| 42 | |
| 43 | valid = true; |
| 44 | |
| 45 | end: |
| 46 | return valid; |
| 47 | } |
| 48 | |
| 49 | LTTNG_HIDDEN |
| 50 | bool lttng_snapshot_output_is_equal( |
| 51 | const struct lttng_snapshot_output *a, |
| 52 | const struct lttng_snapshot_output *b) |
| 53 | { |
| 54 | bool equal = false; |
| 55 | |
| 56 | assert(a); |
| 57 | assert(b); |
| 58 | |
| 59 | if (a->max_size != b->max_size) { |
| 60 | goto end; |
| 61 | } |
| 62 | |
| 63 | if (strcmp(a->name, b->name) != 0) { |
| 64 | goto end; |
| 65 | } |
| 66 | |
| 67 | if (strcmp(a->ctrl_url, b->ctrl_url) != 0) { |
| 68 | goto end; |
| 69 | } |
| 70 | |
| 71 | if (strcmp(a->data_url, b->data_url) != 0) { |
| 72 | goto end; |
| 73 | } |
| 74 | |
| 75 | equal = true; |
| 76 | |
| 77 | end: |
| 78 | return equal; |
| 79 | } |
| 80 | |
| 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 | |
| 92 | LTTNG_HIDDEN |
| 93 | int lttng_snapshot_output_serialize( |
| 94 | const struct lttng_snapshot_output *output, |
| 95 | struct lttng_payload *payload) |
| 96 | { |
| 97 | struct lttng_snapshot_output_comm comm; |
| 98 | int ret; |
| 99 | |
| 100 | comm.id = output->id; |
| 101 | comm.max_size = output->max_size; |
| 102 | |
| 103 | ret = lttng_strncpy(comm.name, output->name, sizeof(comm.name)); |
| 104 | if (ret) { |
| 105 | goto end; |
| 106 | } |
| 107 | |
| 108 | ret = lttng_strncpy( |
| 109 | comm.ctrl_url, output->ctrl_url, sizeof(comm.ctrl_url)); |
| 110 | if (ret) { |
| 111 | goto end; |
| 112 | } |
| 113 | |
| 114 | ret = lttng_strncpy( |
| 115 | comm.data_url, output->data_url, sizeof(comm.data_url)); |
| 116 | if (ret) { |
| 117 | goto end; |
| 118 | } |
| 119 | |
| 120 | ret = lttng_dynamic_buffer_append( |
| 121 | &payload->buffer, &comm, sizeof(comm)); |
| 122 | if (ret) { |
| 123 | goto end; |
| 124 | } |
| 125 | |
| 126 | end: |
| 127 | return ret; |
| 128 | } |
| 129 | |
| 130 | LTTNG_HIDDEN |
| 131 | ssize_t lttng_snapshot_output_create_from_payload( |
| 132 | struct lttng_payload_view *view, |
| 133 | struct lttng_snapshot_output **output_p) |
| 134 | { |
| 135 | const struct lttng_snapshot_output_comm *comm; |
| 136 | struct lttng_snapshot_output *output = NULL; |
| 137 | int ret; |
| 138 | |
| 139 | if (view->buffer.size != sizeof(*comm)) { |
| 140 | ret = -1; |
| 141 | goto end; |
| 142 | } |
| 143 | |
| 144 | output = lttng_snapshot_output_create(); |
| 145 | if (!output) { |
| 146 | ret = -1; |
| 147 | goto end; |
| 148 | } |
| 149 | |
| 150 | comm = (typeof(comm)) view->buffer.data; |
| 151 | |
| 152 | output->id = comm->id; |
| 153 | output->max_size = comm->max_size; |
| 154 | |
| 155 | ret = lttng_strncpy(output->name, comm->name, sizeof(output->name)); |
| 156 | if (ret) { |
| 157 | goto end; |
| 158 | } |
| 159 | |
| 160 | ret = lttng_strncpy(output->ctrl_url, comm->ctrl_url, |
| 161 | sizeof(output->ctrl_url)); |
| 162 | if (ret) { |
| 163 | goto end; |
| 164 | } |
| 165 | |
| 166 | ret = lttng_strncpy(output->data_url, comm->data_url, |
| 167 | sizeof(output->data_url)); |
| 168 | if (ret) { |
| 169 | goto end; |
| 170 | } |
| 171 | |
| 172 | *output_p = output; |
| 173 | output = NULL; |
| 174 | ret = sizeof(*comm); |
| 175 | |
| 176 | end: |
| 177 | lttng_snapshot_output_destroy(output); |
| 178 | return ret; |
| 179 | } |