| 1 | /* |
| 2 | * Copyright (C) 2020 - EfficiOS, Inc. |
| 3 | * |
| 4 | * This program is free software; you can redistribute it and/or modify |
| 5 | * it under the terms of the GNU General Public License, version 2 only, |
| 6 | * as published by the Free Software Foundation. |
| 7 | * |
| 8 | * This program is distributed in the hope that it will be useful, but WITHOUT |
| 9 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
| 10 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for |
| 11 | * more details. |
| 12 | * |
| 13 | * You should have received a copy of the GNU General Public License along |
| 14 | * with this program; if not, write to the Free Software Foundation, Inc., |
| 15 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. |
| 16 | */ |
| 17 | |
| 18 | #include "buffer-view.h" |
| 19 | #include "dynamic-buffer.h" |
| 20 | #include "lttng/snapshot.h" |
| 21 | #include "lttng/snapshot-internal.h" |
| 22 | #include "snapshot.h" |
| 23 | |
| 24 | #include <assert.h> |
| 25 | #include <stdlib.h> |
| 26 | |
| 27 | LTTNG_HIDDEN |
| 28 | bool lttng_snapshot_output_validate(const struct lttng_snapshot_output *output) |
| 29 | { |
| 30 | bool valid = false; |
| 31 | size_t len; |
| 32 | |
| 33 | /* |
| 34 | * It is mandatory to have a ctrl_url. If there is only one output |
| 35 | * URL (in the net://, net6:// or file:// form), it will be in this |
| 36 | * field. |
| 37 | */ |
| 38 | len = lttng_strnlen(output->ctrl_url, sizeof(output->ctrl_url)); |
| 39 | if (len == 0 || len >= sizeof(output->ctrl_url)) { |
| 40 | goto end; |
| 41 | } |
| 42 | |
| 43 | len = lttng_strnlen(output->data_url, sizeof(output->data_url)); |
| 44 | if (len >= sizeof(output->data_url)) { |
| 45 | goto end; |
| 46 | } |
| 47 | |
| 48 | len = lttng_strnlen(output->name, sizeof(output->name)); |
| 49 | if (len >= sizeof(output->name)) { |
| 50 | goto end; |
| 51 | } |
| 52 | |
| 53 | valid = true; |
| 54 | |
| 55 | end: |
| 56 | return valid; |
| 57 | } |
| 58 | |
| 59 | LTTNG_HIDDEN |
| 60 | bool lttng_snapshot_output_is_equal( |
| 61 | const struct lttng_snapshot_output *a, |
| 62 | const struct lttng_snapshot_output *b) |
| 63 | { |
| 64 | bool equal = false; |
| 65 | |
| 66 | assert(a); |
| 67 | assert(b); |
| 68 | |
| 69 | if (a->max_size != b->max_size) { |
| 70 | goto end; |
| 71 | } |
| 72 | |
| 73 | if (strcmp(a->name, b->name) != 0) { |
| 74 | goto end; |
| 75 | } |
| 76 | |
| 77 | if (strcmp(a->ctrl_url, b->ctrl_url) != 0) { |
| 78 | goto end; |
| 79 | } |
| 80 | |
| 81 | if (strcmp(a->data_url, b->data_url) != 0) { |
| 82 | goto end; |
| 83 | } |
| 84 | |
| 85 | equal = true; |
| 86 | |
| 87 | end: |
| 88 | return equal; |
| 89 | } |
| 90 | |
| 91 | /* |
| 92 | * This is essentially the same as `struct lttng_snapshot_output`, but packed. |
| 93 | */ |
| 94 | struct LTTNG_PACKED lttng_snapshot_output_comm { |
| 95 | uint32_t id; |
| 96 | uint64_t max_size; |
| 97 | char name[LTTNG_NAME_MAX]; |
| 98 | char ctrl_url[PATH_MAX]; |
| 99 | char data_url[PATH_MAX]; |
| 100 | }; |
| 101 | |
| 102 | LTTNG_HIDDEN |
| 103 | int lttng_snapshot_output_serialize( |
| 104 | const struct lttng_snapshot_output *output, |
| 105 | struct lttng_dynamic_buffer *buf) |
| 106 | { |
| 107 | struct lttng_snapshot_output_comm comm; |
| 108 | int ret; |
| 109 | |
| 110 | comm.id = output->id; |
| 111 | comm.max_size = output->max_size; |
| 112 | |
| 113 | ret = lttng_strncpy(comm.name, output->name, sizeof(comm.name)); |
| 114 | if (ret) { |
| 115 | goto end; |
| 116 | } |
| 117 | |
| 118 | ret = lttng_strncpy(comm.ctrl_url, output->ctrl_url, sizeof(comm.ctrl_url)); |
| 119 | if (ret) { |
| 120 | goto end; |
| 121 | } |
| 122 | |
| 123 | ret = lttng_strncpy(comm.data_url, output->data_url, sizeof(comm.data_url)); |
| 124 | if (ret) { |
| 125 | goto end; |
| 126 | } |
| 127 | |
| 128 | ret = lttng_dynamic_buffer_append(buf, &comm, sizeof(comm)); |
| 129 | if (ret) { |
| 130 | goto end; |
| 131 | } |
| 132 | |
| 133 | end: |
| 134 | return ret; |
| 135 | } |
| 136 | |
| 137 | LTTNG_HIDDEN |
| 138 | ssize_t lttng_snapshot_output_create_from_buffer( |
| 139 | const struct lttng_buffer_view *view, |
| 140 | struct lttng_snapshot_output **output_p) |
| 141 | { |
| 142 | const struct lttng_snapshot_output_comm *comm; |
| 143 | struct lttng_snapshot_output *output = NULL; |
| 144 | int ret; |
| 145 | |
| 146 | if (view->size != sizeof(*comm)) { |
| 147 | ret = -1; |
| 148 | goto end; |
| 149 | } |
| 150 | |
| 151 | output = lttng_snapshot_output_create(); |
| 152 | if (!output) { |
| 153 | ret = -1; |
| 154 | goto end; |
| 155 | } |
| 156 | |
| 157 | comm = (struct lttng_snapshot_output_comm *) view->data; |
| 158 | |
| 159 | output->id = comm->id; |
| 160 | output->max_size = comm->max_size; |
| 161 | |
| 162 | ret = lttng_strncpy(output->name, comm->name, sizeof(output->name)); |
| 163 | if (ret) { |
| 164 | goto end; |
| 165 | } |
| 166 | |
| 167 | ret = lttng_strncpy(output->ctrl_url, comm->ctrl_url, sizeof(output->ctrl_url)); |
| 168 | if (ret) { |
| 169 | goto end; |
| 170 | } |
| 171 | |
| 172 | ret = lttng_strncpy(output->data_url, comm->data_url, sizeof(output->data_url)); |
| 173 | if (ret) { |
| 174 | goto end; |
| 175 | } |
| 176 | |
| 177 | *output_p = output; |
| 178 | output = NULL; |
| 179 | ret = sizeof(*comm); |
| 180 | |
| 181 | end: |
| 182 | lttng_snapshot_output_destroy(output); |
| 183 | return ret; |
| 184 | } |