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