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