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