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