| 1 | /* |
| 2 | * Copyright (C) 2018 - Jonathan Rajotte <jonathan.rajotte-julien@efficios.com> |
| 3 | * |
| 4 | * This program is free software; you can redistribute it and/or modify it |
| 5 | * under the terms of the GNU General Public License, version 2 only, as |
| 6 | * 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 with |
| 14 | * this program; if not, write to the Free Software Foundation, Inc., 51 |
| 15 | * Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. |
| 16 | */ |
| 17 | |
| 18 | #define _LGPL_SOURCE |
| 19 | #include <assert.h> |
| 20 | #include <inttypes.h> |
| 21 | |
| 22 | #include <common/common.h> |
| 23 | #include <common/sessiond-comm/relayd.h> |
| 24 | |
| 25 | #include <common/compat/endian.h> |
| 26 | #include <common/compat/string.h> |
| 27 | #include <lttng/constant.h> |
| 28 | |
| 29 | #include "cmd-2-11.h" |
| 30 | #include "utils.h" |
| 31 | |
| 32 | int cmd_create_session_2_11(const struct lttng_buffer_view *payload, |
| 33 | char *session_name, char *hostname, |
| 34 | uint32_t *live_timer, bool *snapshot, |
| 35 | uint64_t *id_sessiond, lttng_uuid sessiond_uuid, |
| 36 | bool *has_current_chunk, uint64_t *current_chunk_id, |
| 37 | time_t *creation_time) |
| 38 | { |
| 39 | int ret; |
| 40 | struct lttcomm_relayd_create_session_2_11 header; |
| 41 | size_t header_len, received_names_size; |
| 42 | struct lttng_buffer_view session_name_view; |
| 43 | struct lttng_buffer_view hostname_view; |
| 44 | |
| 45 | header_len = sizeof(header); |
| 46 | |
| 47 | if (payload->size < header_len) { |
| 48 | ERR("Unexpected payload size in \"cmd_create_session_2_11\": expected >= %zu bytes, got %zu bytes", |
| 49 | header_len, payload->size); |
| 50 | ret = -1; |
| 51 | goto error; |
| 52 | } |
| 53 | memcpy(&header, payload->data, header_len); |
| 54 | |
| 55 | header.session_name_len = be32toh(header.session_name_len); |
| 56 | header.hostname_len = be32toh(header.hostname_len); |
| 57 | header.live_timer = be32toh(header.live_timer); |
| 58 | header.current_chunk_id.value = be64toh(header.current_chunk_id.value); |
| 59 | header.current_chunk_id.is_set = !!header.current_chunk_id.is_set; |
| 60 | header.creation_time = be64toh(header.creation_time); |
| 61 | |
| 62 | lttng_uuid_copy(sessiond_uuid, header.sessiond_uuid); |
| 63 | |
| 64 | received_names_size = header.session_name_len + header.hostname_len; |
| 65 | if (payload->size < header_len + received_names_size) { |
| 66 | ERR("Unexpected payload size in \"cmd_create_session_2_11\": expected >= %zu bytes, got %zu bytes", |
| 67 | header_len + received_names_size, payload->size); |
| 68 | ret = -1; |
| 69 | goto error; |
| 70 | } |
| 71 | |
| 72 | /* Validate length against defined constant. */ |
| 73 | if (header.session_name_len > LTTNG_NAME_MAX) { |
| 74 | ret = -ENAMETOOLONG; |
| 75 | ERR("Length of session name (%" PRIu32 " bytes) received in create_session command exceeds maximum length (%d bytes)", header.session_name_len, LTTNG_NAME_MAX); |
| 76 | goto error; |
| 77 | } |
| 78 | if (header.hostname_len > LTTNG_HOST_NAME_MAX) { |
| 79 | ret = -ENAMETOOLONG; |
| 80 | ERR("Length of hostname (%" PRIu32 " bytes) received in create_session command exceeds maximum length (%d bytes)", header.hostname_len, LTTNG_HOST_NAME_MAX); |
| 81 | goto error; |
| 82 | } |
| 83 | |
| 84 | session_name_view = lttng_buffer_view_from_view(payload, header_len, |
| 85 | header.session_name_len); |
| 86 | hostname_view = lttng_buffer_view_from_view(payload, |
| 87 | header_len + header.session_name_len, header.hostname_len); |
| 88 | |
| 89 | /* Validate that names are NULL terminated. */ |
| 90 | if (session_name_view.data[session_name_view.size - 1] != '\0') { |
| 91 | ERR("cmd_create_session_2_11 session_name is invalid (not NULL terminated)"); |
| 92 | ret = -1; |
| 93 | goto error; |
| 94 | } |
| 95 | |
| 96 | if (hostname_view.data[hostname_view.size - 1] != '\0') { |
| 97 | ERR("cmd_create_session_2_11 hostname is invalid (not NULL terminated)"); |
| 98 | ret = -1; |
| 99 | goto error; |
| 100 | } |
| 101 | |
| 102 | /* |
| 103 | * Length and null-termination check are already performed. |
| 104 | * LTTNG_NAME_MAX and LTTNG_HOST_NAME_MAX max size are expected. |
| 105 | */ |
| 106 | strcpy(session_name, session_name_view.data); |
| 107 | strcpy(hostname, hostname_view.data); |
| 108 | |
| 109 | *live_timer = header.live_timer; |
| 110 | *snapshot = !!header.snapshot; |
| 111 | *current_chunk_id = header.current_chunk_id.value; |
| 112 | *has_current_chunk = header.current_chunk_id.is_set; |
| 113 | *creation_time = (time_t) header.creation_time; |
| 114 | |
| 115 | ret = 0; |
| 116 | |
| 117 | error: |
| 118 | return ret; |
| 119 | } |
| 120 | |
| 121 | /* |
| 122 | * cmd_recv_stream_2_11 allocates path_name and channel_name. |
| 123 | */ |
| 124 | int cmd_recv_stream_2_11(const struct lttng_buffer_view *payload, |
| 125 | char **ret_path_name, char **ret_channel_name, |
| 126 | uint64_t *tracefile_size, uint64_t *tracefile_count, |
| 127 | uint64_t *trace_archive_id) |
| 128 | { |
| 129 | int ret; |
| 130 | struct lttcomm_relayd_add_stream_2_11 header; |
| 131 | size_t header_len, received_names_size; |
| 132 | struct lttng_buffer_view channel_name_view; |
| 133 | struct lttng_buffer_view pathname_view; |
| 134 | char *path_name = NULL; |
| 135 | char *channel_name = NULL; |
| 136 | |
| 137 | header_len = sizeof(header); |
| 138 | |
| 139 | if (payload->size < header_len) { |
| 140 | ERR("Unexpected payload size in \"cmd_recv_stream_2_11\": expected >= %zu bytes, got %zu bytes", |
| 141 | header_len, payload->size); |
| 142 | ret = -1; |
| 143 | goto error; |
| 144 | } |
| 145 | memcpy(&header, payload->data, header_len); |
| 146 | |
| 147 | header.channel_name_len = be32toh(header.channel_name_len); |
| 148 | header.pathname_len = be32toh(header.pathname_len); |
| 149 | header.tracefile_size = be64toh(header.tracefile_size); |
| 150 | header.tracefile_count = be64toh(header.tracefile_count); |
| 151 | header.trace_chunk_id = be64toh(header.trace_chunk_id); |
| 152 | |
| 153 | received_names_size = header.channel_name_len + header.pathname_len; |
| 154 | if (payload->size < header_len + received_names_size) { |
| 155 | ERR("Unexpected payload size in \"cmd_recv_stream_2_11\": expected >= %zu bytes, got %zu bytes", |
| 156 | header_len + received_names_size, payload->size); |
| 157 | ret = -1; |
| 158 | goto error; |
| 159 | } |
| 160 | |
| 161 | /* Validate length against defined constant. */ |
| 162 | if (header.channel_name_len > DEFAULT_STREAM_NAME_LEN) { |
| 163 | ret = -ENAMETOOLONG; |
| 164 | ERR("Channel name too long"); |
| 165 | goto error; |
| 166 | } |
| 167 | if (header.pathname_len > LTTNG_NAME_MAX) { |
| 168 | ret = -ENAMETOOLONG; |
| 169 | ERR("Pathname too long"); |
| 170 | goto error; |
| 171 | } |
| 172 | |
| 173 | /* Validate that names are (NULL terminated. */ |
| 174 | channel_name_view = lttng_buffer_view_from_view(payload, header_len, |
| 175 | header.channel_name_len); |
| 176 | pathname_view = lttng_buffer_view_from_view(payload, |
| 177 | header_len + header.channel_name_len, header.pathname_len); |
| 178 | |
| 179 | if (channel_name_view.data[channel_name_view.size - 1] != '\0') { |
| 180 | ERR("cmd_recv_stream_2_11 channel_name is invalid (not NULL terminated)"); |
| 181 | ret = -1; |
| 182 | goto error; |
| 183 | } |
| 184 | |
| 185 | if (pathname_view.data[pathname_view.size - 1] != '\0') { |
| 186 | ERR("cmd_recv_stream_2_11 patname is invalid (not NULL terminated)"); |
| 187 | ret = -1; |
| 188 | goto error; |
| 189 | } |
| 190 | |
| 191 | channel_name = strdup(channel_name_view.data); |
| 192 | if (!channel_name) { |
| 193 | ret = -errno; |
| 194 | PERROR("Channel name allocation"); |
| 195 | goto error; |
| 196 | } |
| 197 | |
| 198 | path_name = strdup(pathname_view.data); |
| 199 | if (!path_name) { |
| 200 | PERROR("Path name allocation"); |
| 201 | ret = -ENOMEM; |
| 202 | goto error; |
| 203 | } |
| 204 | |
| 205 | *tracefile_size = header.tracefile_size; |
| 206 | *tracefile_count = header.tracefile_count; |
| 207 | *trace_archive_id = header.trace_chunk_id; |
| 208 | *ret_path_name = path_name; |
| 209 | *ret_channel_name = channel_name; |
| 210 | /* Move ownership to caller */ |
| 211 | path_name = NULL; |
| 212 | channel_name = NULL; |
| 213 | ret = 0; |
| 214 | error: |
| 215 | free(channel_name); |
| 216 | free(path_name); |
| 217 | return ret; |
| 218 | } |