consumer relayd interaction: fix segfaults
[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 #include <inttypes.h>
25
26 #include <common/common.h>
27 #include <common/defaults.h>
28 #include <common/sessiond-comm/relayd.h>
29
30 #include "relayd.h"
31
32 /*
33 * Send command. Fill up the header and append the data.
34 */
35 static int send_command(struct lttcomm_relayd_sock *rsock,
36 enum lttcomm_relayd_command cmd, void *data, size_t size,
37 int flags)
38 {
39 int ret;
40 struct lttcomm_relayd_hdr header;
41 char *buf;
42 uint64_t buf_size = sizeof(header);
43
44 if (rsock->sock.fd < 0) {
45 return -ECONNRESET;
46 }
47
48 if (data) {
49 buf_size += size;
50 }
51
52 buf = zmalloc(buf_size);
53 if (buf == NULL) {
54 PERROR("zmalloc relayd send command buf");
55 ret = -1;
56 goto alloc_error;
57 }
58
59 header.cmd = htobe32(cmd);
60 header.data_size = htobe64(size);
61
62 /* Zeroed for now since not used. */
63 header.cmd_version = 0;
64 header.circuit_id = 0;
65
66 /* Prepare buffer to send. */
67 memcpy(buf, &header, sizeof(header));
68 if (data) {
69 memcpy(buf + sizeof(header), data, size);
70 }
71
72 ret = rsock->sock.ops->sendmsg(&rsock->sock, buf, buf_size, flags);
73 if (ret < 0) {
74 ret = -errno;
75 goto error;
76 }
77
78 DBG3("Relayd sending command %d of size %" PRIu64, cmd, buf_size);
79
80 error:
81 free(buf);
82 alloc_error:
83 return ret;
84 }
85
86 /*
87 * Receive reply data on socket. This MUST be call after send_command or else
88 * could result in unexpected behavior(s).
89 */
90 static int recv_reply(struct lttcomm_relayd_sock *rsock, void *data, size_t size)
91 {
92 int ret;
93
94 if (rsock->sock.fd < 0) {
95 return -ECONNRESET;
96 }
97
98 DBG3("Relayd waiting for reply of size %zu", size);
99
100 ret = rsock->sock.ops->recvmsg(&rsock->sock, data, size, 0);
101 if (ret <= 0 || ret != size) {
102 if (ret == 0) {
103 /* Orderly shutdown. */
104 DBG("Socket %d has performed an orderly shutdown", rsock->sock.fd);
105 } else {
106 DBG("Receiving reply failed on sock %d for size %zu with ret %d",
107 rsock->sock.fd, size, ret);
108 }
109 /* Always return -1 here and the caller can use errno. */
110 ret = -1;
111 goto error;
112 }
113
114 error:
115 return ret;
116 }
117
118 /*
119 * Send a RELAYD_CREATE_SESSION command to the relayd with the given socket and
120 * set session_id of the relayd if we have a successful reply from the relayd.
121 *
122 * On success, return 0 else a negative value which is either an errno error or
123 * a lttng error code from the relayd.
124 */
125 int relayd_create_session(struct lttcomm_relayd_sock *rsock, uint64_t *session_id)
126 {
127 int ret;
128 struct lttcomm_relayd_status_session reply;
129
130 assert(rsock);
131 assert(session_id);
132
133 DBG("Relayd create session");
134
135 /* Send command */
136 ret = send_command(rsock, RELAYD_CREATE_SESSION, NULL, 0, 0);
137 if (ret < 0) {
138 goto error;
139 }
140
141 /* Receive response */
142 ret = recv_reply(rsock, (void *) &reply, sizeof(reply));
143 if (ret < 0) {
144 goto error;
145 }
146
147 reply.session_id = be64toh(reply.session_id);
148 reply.ret_code = be32toh(reply.ret_code);
149
150 /* Return session id or negative ret code. */
151 if (reply.ret_code != LTTNG_OK) {
152 ret = -1;
153 ERR("Relayd create session replied error %d", reply.ret_code);
154 goto error;
155 } else {
156 ret = 0;
157 *session_id = reply.session_id;
158 }
159
160 DBG("Relayd session created with id %" PRIu64, reply.session_id);
161
162 error:
163 return ret;
164 }
165
166 /*
167 * Add stream on the relayd and assign stream handle to the stream_id argument.
168 *
169 * On success return 0 else return ret_code negative value.
170 */
171 int relayd_add_stream(struct lttcomm_relayd_sock *rsock, const char *channel_name,
172 const char *pathname, uint64_t *stream_id,
173 uint64_t tracefile_size, uint64_t tracefile_count)
174 {
175 int ret;
176 struct lttcomm_relayd_add_stream msg;
177 struct lttcomm_relayd_add_stream_2_2 msg_2_2;
178 struct lttcomm_relayd_status_stream reply;
179
180 /* Code flow error. Safety net. */
181 assert(rsock);
182 assert(channel_name);
183 assert(pathname);
184
185 DBG("Relayd adding stream for channel name %s", channel_name);
186
187 /* Compat with relayd 2.1 */
188 if (rsock->minor == 1) {
189 strncpy(msg.channel_name, channel_name, sizeof(msg.channel_name));
190 strncpy(msg.pathname, pathname, sizeof(msg.pathname));
191
192 /* Send command */
193 ret = send_command(rsock, RELAYD_ADD_STREAM, (void *) &msg, sizeof(msg), 0);
194 if (ret < 0) {
195 goto error;
196 }
197 } else {
198 /* Compat with relayd 2.2+ */
199 strncpy(msg_2_2.channel_name, channel_name, sizeof(msg_2_2.channel_name));
200 strncpy(msg_2_2.pathname, pathname, sizeof(msg_2_2.pathname));
201 msg_2_2.tracefile_size = htobe64(tracefile_size);
202 msg_2_2.tracefile_count = htobe64(tracefile_count);
203
204 /* Send command */
205 ret = send_command(rsock, RELAYD_ADD_STREAM, (void *) &msg_2_2, sizeof(msg_2_2), 0);
206 if (ret < 0) {
207 goto error;
208 }
209 }
210
211 /* Waiting for reply */
212 ret = recv_reply(rsock, (void *) &reply, sizeof(reply));
213 if (ret < 0) {
214 goto error;
215 }
216
217 /* Back to host bytes order. */
218 reply.handle = be64toh(reply.handle);
219 reply.ret_code = be32toh(reply.ret_code);
220
221 /* Return session id or negative ret code. */
222 if (reply.ret_code != LTTNG_OK) {
223 ret = -1;
224 ERR("Relayd add stream replied error %d", reply.ret_code);
225 } else {
226 /* Success */
227 ret = 0;
228 *stream_id = reply.handle;
229 }
230
231 DBG("Relayd stream added successfully with handle %" PRIu64,
232 reply.handle);
233
234 error:
235 return ret;
236 }
237
238 /*
239 * Check version numbers on the relayd.
240 * If major versions are compatible, we assign minor_to_use to the
241 * minor version of the procotol we are going to use for this session.
242 *
243 * Return 0 if compatible else negative value.
244 */
245 int relayd_version_check(struct lttcomm_relayd_sock *rsock)
246 {
247 int ret;
248 struct lttcomm_relayd_version msg;
249
250 /* Code flow error. Safety net. */
251 assert(rsock);
252
253 DBG("Relayd version check for major.minor %u.%u", rsock->major,
254 rsock->minor);
255
256 /* Prepare network byte order before transmission. */
257 msg.major = htobe32(rsock->major);
258 msg.minor = htobe32(rsock->minor);
259
260 /* Send command */
261 ret = send_command(rsock, RELAYD_VERSION, (void *) &msg, sizeof(msg), 0);
262 if (ret < 0) {
263 goto error;
264 }
265
266 /* Receive response */
267 ret = recv_reply(rsock, (void *) &msg, sizeof(msg));
268 if (ret < 0) {
269 goto error;
270 }
271
272 /* Set back to host bytes order */
273 msg.major = be32toh(msg.major);
274 msg.minor = be32toh(msg.minor);
275
276 /*
277 * Only validate the major version. If the other side is higher,
278 * communication is not possible. Only major version equal can talk to each
279 * other. If the minor version differs, the lowest version is used by both
280 * sides.
281 */
282 if (msg.major != rsock->major) {
283 /* Not compatible */
284 ret = -1;
285 DBG2("Relayd version is NOT compatible. Relayd version %u != %u (us)",
286 msg.major, rsock->major);
287 goto error;
288 }
289
290 /*
291 * If the relayd's minor version is higher, it will adapt to our version so
292 * we can continue to use the latest relayd communication data structure.
293 * If the received minor version is higher, the relayd should adapt to us.
294 */
295 if (rsock->minor > msg.minor) {
296 rsock->minor = msg.minor;
297 }
298
299 /* Version number compatible */
300 DBG2("Relayd version is compatible, using protocol version %u.%u",
301 rsock->major, rsock->minor);
302 ret = 0;
303
304 error:
305 return ret;
306 }
307
308 /*
309 * Add stream on the relayd and assign stream handle to the stream_id argument.
310 *
311 * On success return 0 else return ret_code negative value.
312 */
313 int relayd_send_metadata(struct lttcomm_relayd_sock *rsock, size_t len)
314 {
315 int ret;
316
317 /* Code flow error. Safety net. */
318 assert(rsock);
319
320 DBG("Relayd sending metadata of size %zu", len);
321
322 /* Send command */
323 ret = send_command(rsock, RELAYD_SEND_METADATA, NULL, len, 0);
324 if (ret < 0) {
325 goto error;
326 }
327
328 DBG2("Relayd metadata added successfully");
329
330 /*
331 * After that call, the metadata data MUST be sent to the relayd so the
332 * receive size on the other end matches the len of the metadata packet
333 * header. This is why we don't wait for a reply here.
334 */
335
336 error:
337 return ret;
338 }
339
340 /*
341 * Connect to relay daemon with an allocated lttcomm_relayd_sock.
342 */
343 int relayd_connect(struct lttcomm_relayd_sock *rsock)
344 {
345 /* Code flow error. Safety net. */
346 assert(rsock);
347
348 if (!rsock->sock.ops) {
349 /*
350 * Attempting a connect on a non-initialized socket.
351 */
352 return -ECONNRESET;
353 }
354
355 DBG3("Relayd connect ...");
356
357 return rsock->sock.ops->connect(&rsock->sock);
358 }
359
360 /*
361 * Close relayd socket with an allocated lttcomm_relayd_sock.
362 *
363 * If no socket operations are found, simply return 0 meaning that everything
364 * is fine. Without operations, the socket can not possibly be opened or used.
365 * This is possible if the socket was allocated but not created. However, the
366 * caller could simply use it to store a valid file descriptor for instance
367 * passed over a Unix socket and call this to cleanup but still without a valid
368 * ops pointer.
369 *
370 * Return the close returned value. On error, a negative value is usually
371 * returned back from close(2).
372 */
373 int relayd_close(struct lttcomm_relayd_sock *rsock)
374 {
375 int ret;
376
377 /* Code flow error. Safety net. */
378 assert(rsock);
379
380 /* An invalid fd is fine, return success. */
381 if (rsock->sock.fd < 0) {
382 ret = 0;
383 goto end;
384 }
385
386 DBG3("Relayd closing socket %d", rsock->sock.fd);
387
388 if (rsock->sock.ops) {
389 ret = rsock->sock.ops->close(&rsock->sock);
390 } else {
391 /* Default call if no specific ops found. */
392 ret = close(rsock->sock.fd);
393 if (ret < 0) {
394 PERROR("relayd_close default close");
395 }
396 }
397 rsock->sock.fd = -1;
398
399 end:
400 return ret;
401 }
402
403 /*
404 * Send data header structure to the relayd.
405 */
406 int relayd_send_data_hdr(struct lttcomm_relayd_sock *rsock,
407 struct lttcomm_relayd_data_hdr *hdr, size_t size)
408 {
409 int ret;
410
411 /* Code flow error. Safety net. */
412 assert(rsock);
413 assert(hdr);
414
415 if (rsock->sock.fd < 0) {
416 return -ECONNRESET;
417 }
418
419 DBG3("Relayd sending data header of size %zu", size);
420
421 /* Again, safety net */
422 if (size == 0) {
423 size = sizeof(struct lttcomm_relayd_data_hdr);
424 }
425
426 /* Only send data header. */
427 ret = rsock->sock.ops->sendmsg(&rsock->sock, hdr, size, 0);
428 if (ret < 0) {
429 ret = -errno;
430 goto error;
431 }
432
433 /*
434 * The data MUST be sent right after that command for the receive on the
435 * other end to match the size in the header.
436 */
437
438 error:
439 return ret;
440 }
441
442 /*
443 * Send close stream command to the relayd.
444 */
445 int relayd_send_close_stream(struct lttcomm_relayd_sock *rsock, uint64_t stream_id,
446 uint64_t last_net_seq_num)
447 {
448 int ret;
449 struct lttcomm_relayd_close_stream msg;
450 struct lttcomm_relayd_generic_reply reply;
451
452 /* Code flow error. Safety net. */
453 assert(rsock);
454
455 DBG("Relayd closing stream id %" PRIu64, stream_id);
456
457 msg.stream_id = htobe64(stream_id);
458 msg.last_net_seq_num = htobe64(last_net_seq_num);
459
460 /* Send command */
461 ret = send_command(rsock, RELAYD_CLOSE_STREAM, (void *) &msg, sizeof(msg), 0);
462 if (ret < 0) {
463 goto error;
464 }
465
466 /* Receive response */
467 ret = recv_reply(rsock, (void *) &reply, sizeof(reply));
468 if (ret < 0) {
469 goto error;
470 }
471
472 reply.ret_code = be32toh(reply.ret_code);
473
474 /* Return session id or negative ret code. */
475 if (reply.ret_code != LTTNG_OK) {
476 ret = -1;
477 ERR("Relayd close stream replied error %d", reply.ret_code);
478 } else {
479 /* Success */
480 ret = 0;
481 }
482
483 DBG("Relayd close stream id %" PRIu64 " successfully", stream_id);
484
485 error:
486 return ret;
487 }
488
489 /*
490 * Check for data availability for a given stream id.
491 *
492 * Return 0 if NOT pending, 1 if so and a negative value on error.
493 */
494 int relayd_data_pending(struct lttcomm_relayd_sock *rsock, uint64_t stream_id,
495 uint64_t last_net_seq_num)
496 {
497 int ret;
498 struct lttcomm_relayd_data_pending msg;
499 struct lttcomm_relayd_generic_reply reply;
500
501 /* Code flow error. Safety net. */
502 assert(rsock);
503
504 DBG("Relayd data pending for stream id %" PRIu64, stream_id);
505
506 msg.stream_id = htobe64(stream_id);
507 msg.last_net_seq_num = htobe64(last_net_seq_num);
508
509 /* Send command */
510 ret = send_command(rsock, RELAYD_DATA_PENDING, (void *) &msg,
511 sizeof(msg), 0);
512 if (ret < 0) {
513 goto error;
514 }
515
516 /* Receive response */
517 ret = recv_reply(rsock, (void *) &reply, sizeof(reply));
518 if (ret < 0) {
519 goto error;
520 }
521
522 reply.ret_code = be32toh(reply.ret_code);
523
524 /* Return session id or negative ret code. */
525 if (reply.ret_code >= LTTNG_OK) {
526 ERR("Relayd data pending replied error %d", reply.ret_code);
527 }
528
529 /* At this point, the ret code is either 1 or 0 */
530 ret = reply.ret_code;
531
532 DBG("Relayd data is %s pending for stream id %" PRIu64,
533 ret == 1 ? "" : "NOT", stream_id);
534
535 error:
536 return ret;
537 }
538
539 /*
540 * Check on the relayd side for a quiescent state on the control socket.
541 */
542 int relayd_quiescent_control(struct lttcomm_relayd_sock *rsock,
543 uint64_t metadata_stream_id)
544 {
545 int ret;
546 struct lttcomm_relayd_quiescent_control msg;
547 struct lttcomm_relayd_generic_reply reply;
548
549 /* Code flow error. Safety net. */
550 assert(rsock);
551
552 DBG("Relayd checking quiescent control state");
553
554 msg.stream_id = htobe64(metadata_stream_id);
555
556 /* Send command */
557 ret = send_command(rsock, RELAYD_QUIESCENT_CONTROL, &msg, sizeof(msg), 0);
558 if (ret < 0) {
559 goto error;
560 }
561
562 /* Receive response */
563 ret = recv_reply(rsock, (void *) &reply, sizeof(reply));
564 if (ret < 0) {
565 goto error;
566 }
567
568 reply.ret_code = be32toh(reply.ret_code);
569
570 /* Return session id or negative ret code. */
571 if (reply.ret_code != LTTNG_OK) {
572 ret = -1;
573 ERR("Relayd quiescent control replied error %d", reply.ret_code);
574 goto error;
575 }
576
577 /* Control socket is quiescent */
578 return 0;
579
580 error:
581 return ret;
582 }
583
584 /*
585 * Begin a data pending command for a specific session id.
586 */
587 int relayd_begin_data_pending(struct lttcomm_relayd_sock *rsock, uint64_t id)
588 {
589 int ret;
590 struct lttcomm_relayd_begin_data_pending msg;
591 struct lttcomm_relayd_generic_reply reply;
592
593 /* Code flow error. Safety net. */
594 assert(rsock);
595
596 DBG("Relayd begin data pending");
597
598 msg.session_id = htobe64(id);
599
600 /* Send command */
601 ret = send_command(rsock, RELAYD_BEGIN_DATA_PENDING, &msg, sizeof(msg), 0);
602 if (ret < 0) {
603 goto error;
604 }
605
606 /* Receive response */
607 ret = recv_reply(rsock, (void *) &reply, sizeof(reply));
608 if (ret < 0) {
609 goto error;
610 }
611
612 reply.ret_code = be32toh(reply.ret_code);
613
614 /* Return session id or negative ret code. */
615 if (reply.ret_code != LTTNG_OK) {
616 ret = -1;
617 ERR("Relayd begin data pending replied error %d", reply.ret_code);
618 goto error;
619 }
620
621 return 0;
622
623 error:
624 return ret;
625 }
626
627 /*
628 * End a data pending command for a specific session id.
629 *
630 * Return 0 on success and set is_data_inflight to 0 if no data is being
631 * streamed or 1 if it is the case.
632 */
633 int relayd_end_data_pending(struct lttcomm_relayd_sock *rsock, uint64_t id,
634 unsigned int *is_data_inflight)
635 {
636 int ret;
637 struct lttcomm_relayd_end_data_pending msg;
638 struct lttcomm_relayd_generic_reply reply;
639
640 /* Code flow error. Safety net. */
641 assert(rsock);
642
643 DBG("Relayd end data pending");
644
645 msg.session_id = htobe64(id);
646
647 /* Send command */
648 ret = send_command(rsock, RELAYD_END_DATA_PENDING, &msg, sizeof(msg), 0);
649 if (ret < 0) {
650 goto error;
651 }
652
653 /* Receive response */
654 ret = recv_reply(rsock, (void *) &reply, sizeof(reply));
655 if (ret < 0) {
656 goto error;
657 }
658
659 reply.ret_code = be32toh(reply.ret_code);
660 if (reply.ret_code < 0) {
661 ret = reply.ret_code;
662 goto error;
663 }
664
665 *is_data_inflight = reply.ret_code;
666
667 DBG("Relayd end data pending is data inflight: %d", reply.ret_code);
668
669 return 0;
670
671 error:
672 return ret;
673 }
This page took 0.042748 seconds and 4 git commands to generate.