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