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