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