8945afea3645948d07a590b37ce81a916ab372ff
[lttng-tools.git] / src / common / relayd / relayd.c
1 /*
2 * Copyright (C) 2012 - David Goulet <dgoulet@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 _GNU_SOURCE
19 #include <assert.h>
20 #include <stdio.h>
21 #include <stdlib.h>
22 #include <string.h>
23 #include <sys/stat.h>
24
25 #include <common/common.h>
26 #include <common/defaults.h>
27 #include <common/sessiond-comm/relayd.h>
28
29 #include "relayd.h"
30
31 /*
32 * Send command. Fill up the header and append the data.
33 */
34 static int send_command(struct lttcomm_sock *sock,
35 enum lttcomm_sessiond_command cmd, void *data, size_t size,
36 int flags)
37 {
38 int ret;
39 struct lttcomm_relayd_hdr header;
40 char *buf;
41 uint64_t buf_size = sizeof(header);
42
43 if (data) {
44 buf_size += size;
45 }
46
47 buf = zmalloc(buf_size);
48 if (buf == NULL) {
49 PERROR("zmalloc relayd send command buf");
50 ret = -1;
51 goto alloc_error;
52 }
53
54 header.cmd = htobe32(cmd);
55 header.data_size = htobe64(size);
56
57 /* Zeroed for now since not used. */
58 header.cmd_version = 0;
59 header.circuit_id = 0;
60
61 /* Prepare buffer to send. */
62 memcpy(buf, &header, sizeof(header));
63 if (data) {
64 memcpy(buf + sizeof(header), data, size);
65 }
66
67 ret = sock->ops->sendmsg(sock, buf, buf_size, flags);
68 if (ret < 0) {
69 goto error;
70 }
71
72 DBG3("Relayd sending command %d", cmd);
73
74 error:
75 free(buf);
76 alloc_error:
77 return ret;
78 }
79
80 /*
81 * Receive reply data on socket. This MUST be call after send_command or else
82 * could result in unexpected behavior(s).
83 */
84 static int recv_reply(struct lttcomm_sock *sock, void *data, size_t size)
85 {
86 int ret;
87
88 DBG3("Relayd waiting for reply...");
89
90 ret = sock->ops->recvmsg(sock, data, size, 0);
91 if (ret < 0) {
92 goto error;
93 }
94
95 error:
96 return ret;
97 }
98
99 #if 0
100 /*
101 * Create session on the relayd.
102 *
103 * On error, return ret_code negative value else return 0.
104 */
105 int relayd_create_session(struct lttcomm_sock *sock, const char *hostname,
106 const char *session_name)
107 {
108 int ret;
109 struct lttcomm_relayd_create_session msg;
110 struct lttcomm_relayd_generic_reply reply;
111
112 /* Code flow error. Safety net. */
113 assert(sock);
114 assert(hostname);
115 assert(session_name);
116
117 DBG("Relayd creating session for hostname %s and session name %s",
118 hostname, session_name);
119
120 strncpy(msg.hostname, hostname, sizeof(msg.hostname));
121 strncpy(msg.session_name, session_name, sizeof(msg.session_name));
122
123 /* Send command */
124 ret = send_command(sock, RELAYD_CREATE_SESSION, (void *) &msg,
125 sizeof(msg), 0);
126 if (ret < 0) {
127 goto error;
128 }
129
130 /* Recevie response */
131 ret = recv_reply(sock, (void *) &reply, sizeof(reply));
132 if (ret < 0) {
133 goto error;
134 }
135
136 /* Return session id or negative ret code. */
137 if (reply.ret_code != LTTCOMM_OK) {
138 ret = -reply.ret_code;
139 } else {
140 /* Success */
141 ret = 0;
142 }
143
144 DBG2("Relayd created session for %s", session_name);
145
146 error:
147 return ret;
148 }
149 #endif
150
151 /*
152 * Add stream on the relayd and assign stream handle to the stream_id argument.
153 *
154 * On success return 0 else return ret_code negative value.
155 */
156 int relayd_add_stream(struct lttcomm_sock *sock, const char *channel_name,
157 const char *pathname, uint64_t *stream_id)
158 {
159 int ret;
160 struct lttcomm_relayd_add_stream msg;
161 struct lttcomm_relayd_status_stream reply;
162
163 /* Code flow error. Safety net. */
164 assert(sock);
165 assert(channel_name);
166 assert(pathname);
167
168 DBG("Relayd adding stream for channel name %s", channel_name);
169
170 strncpy(msg.channel_name, channel_name, sizeof(msg.channel_name));
171 strncpy(msg.pathname, pathname, sizeof(msg.pathname));
172
173 /* Send command */
174 ret = send_command(sock, RELAYD_ADD_STREAM, (void *) &msg, sizeof(msg), 0);
175 if (ret < 0) {
176 goto error;
177 }
178
179 /* Recevie response */
180 ret = recv_reply(sock, (void *) &reply, sizeof(reply));
181 if (ret < 0) {
182 goto error;
183 }
184
185 /* Back to host bytes order. */
186 reply.handle = be64toh(reply.handle);
187 reply.ret_code = be32toh(reply.ret_code);
188
189 /* Return session id or negative ret code. */
190 if (reply.ret_code != LTTCOMM_OK) {
191 ret = -reply.ret_code;
192 ERR("Relayd add stream replied error %d", ret);
193 } else {
194 /* Success */
195 ret = 0;
196 *stream_id = reply.handle;
197 }
198
199 DBG("Relayd stream added successfully with handle %zu", reply.handle);
200
201 error:
202 return ret;
203 }
204
205 /*
206 * Check version numbers on the relayd.
207 *
208 * Return 0 if compatible else negative value.
209 */
210 int relayd_version_check(struct lttcomm_sock *sock, uint32_t major,
211 uint32_t minor)
212 {
213 int ret;
214 struct lttcomm_relayd_version reply;
215
216 /* Code flow error. Safety net. */
217 assert(sock);
218
219 DBG("Relayd version check for major.minor %u.%u", major, minor);
220
221 /* Send command */
222 ret = send_command(sock, RELAYD_VERSION, NULL, 0, 0);
223 if (ret < 0) {
224 goto error;
225 }
226
227 /* Recevie response */
228 ret = recv_reply(sock, (void *) &reply, sizeof(reply));
229 if (ret < 0) {
230 goto error;
231 }
232
233 /* Set back to host bytes order */
234 reply.major = be32toh(reply.major);
235 reply.minor = be32toh(reply.minor);
236
237 /* Validate version */
238 if (reply.major <= major) {
239 if (reply.minor <= minor) {
240 /* Compatible */
241 ret = 0;
242 DBG2("Relayd version is compatible");
243 goto error;
244 }
245 }
246
247 /* Version number not compatible */
248 DBG2("Relayd version is NOT compatible %u.%u > %u.%u", reply.major,
249 reply.minor, major, minor);
250 ret = -1;
251
252 error:
253 return ret;
254 }
255
256 #if 0
257 /*
258 * Start data command on the relayd.
259 *
260 * On success return 0 else return ret_code negative value.
261 */
262 int relayd_start_data(struct lttcomm_sock *sock)
263 {
264 int ret;
265 struct lttcomm_relayd_generic_reply reply;
266
267 /* Code flow error. Safety net. */
268 assert(sock);
269
270 DBG("Relayd start data command");
271
272 /* Send command */
273 ret = send_command(sock, RELAYD_START_DATA, NULL, 0, 0);
274 if (ret < 0) {
275 goto error;
276 }
277
278 /* Recevie response */
279 ret = recv_reply(sock, (void *) &reply, sizeof(reply));
280 if (ret < 0) {
281 goto error;
282 }
283
284 /* Return session id or negative ret code. */
285 if (reply.ret_code != LTTCOMM_OK) {
286 ret = -reply.ret_code;
287 } else {
288 /* Success */
289 ret = 0;
290 }
291
292 error:
293 return ret;
294 }
295 #endif
296
297 /*
298 * Add stream on the relayd and assign stream handle to the stream_id argument.
299 *
300 * On success return 0 else return ret_code negative value.
301 */
302 int relayd_send_metadata(struct lttcomm_sock *sock, size_t len)
303 {
304 int ret;
305
306 /* Code flow error. Safety net. */
307 assert(sock);
308
309 DBG("Relayd sending metadata of size %lu", len);
310
311 /* Send command */
312 ret = send_command(sock, RELAYD_SEND_METADATA, NULL, len, 0);
313 if (ret < 0) {
314 goto error;
315 }
316
317 DBG2("Relayd metadata added successfully");
318
319 /*
320 * After that call, the metadata data MUST be sent to the relayd so the
321 * receive size on the other end matches the len of the metadata packet
322 * header.
323 */
324
325 error:
326 return ret;
327 }
328
329 /*
330 * Connect to relay daemon with an allocated lttcomm_sock.
331 */
332 int relayd_connect(struct lttcomm_sock *sock)
333 {
334 /* Code flow error. Safety net. */
335 assert(sock);
336
337 DBG3("Relayd connect ...");
338
339 return sock->ops->connect(sock);
340 }
341
342 /*
343 * Close relayd socket with an allocated lttcomm_sock.
344 */
345 int relayd_close(struct lttcomm_sock *sock)
346 {
347 /* Code flow error. Safety net. */
348 assert(sock);
349
350 DBG3("Relayd closing socket %d", sock->fd);
351
352 return sock->ops->close(sock);
353 }
354
355 /*
356 * Send data header structure to the relayd.
357 */
358 int relayd_send_data_hdr(struct lttcomm_sock *sock,
359 struct lttcomm_relayd_data_hdr *hdr, size_t size)
360 {
361 int ret;
362
363 /* Code flow error. Safety net. */
364 assert(sock);
365 assert(hdr);
366
367 DBG3("Relayd sending data header...");
368
369 /* Again, safety net */
370 if (size == 0) {
371 size = sizeof(struct lttcomm_relayd_data_hdr);
372 }
373
374 /* Only send data header. */
375 ret = sock->ops->sendmsg(sock, hdr, size, 0);
376 if (ret < 0) {
377 goto error;
378 }
379
380 /*
381 * The data MUST be sent right after that command for the receive on the
382 * other end to match the size in the header.
383 */
384
385 error:
386 return ret;
387 }
This page took 0.036417 seconds and 3 git commands to generate.