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