Move stream file rotation functions to utils
[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
633d0084 90 DBG3("Relayd waiting for reply of size %ld", 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
DG
97 } else {
98 DBG("Receiving reply failed on sock %d for size %lu 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,
00e2e675
DG
164 const char *pathname, uint64_t *stream_id)
165{
166 int ret;
167 struct lttcomm_relayd_add_stream msg;
168 struct lttcomm_relayd_status_stream reply;
169
170 /* Code flow error. Safety net. */
6151a90f 171 assert(rsock);
00e2e675
DG
172 assert(channel_name);
173 assert(pathname);
174
175 DBG("Relayd adding stream for channel name %s", channel_name);
176
177 strncpy(msg.channel_name, channel_name, sizeof(msg.channel_name));
178 strncpy(msg.pathname, pathname, sizeof(msg.pathname));
179
180 /* Send command */
6151a90f 181 ret = send_command(rsock, RELAYD_ADD_STREAM, (void *) &msg, sizeof(msg), 0);
00e2e675
DG
182 if (ret < 0) {
183 goto error;
184 }
185
633d0084 186 /* Waiting for reply */
6151a90f 187 ret = recv_reply(rsock, (void *) &reply, sizeof(reply));
00e2e675
DG
188 if (ret < 0) {
189 goto error;
190 }
191
192 /* Back to host bytes order. */
193 reply.handle = be64toh(reply.handle);
194 reply.ret_code = be32toh(reply.ret_code);
195
196 /* Return session id or negative ret code. */
f73fabfd 197 if (reply.ret_code != LTTNG_OK) {
bb63afd9
DG
198 ret = -1;
199 ERR("Relayd add stream replied error %d", reply.ret_code);
00e2e675
DG
200 } else {
201 /* Success */
202 ret = 0;
203 *stream_id = reply.handle;
204 }
205
77c7c900
MD
206 DBG("Relayd stream added successfully with handle %" PRIu64,
207 reply.handle);
00e2e675
DG
208
209error:
210 return ret;
211}
212
213/*
214 * Check version numbers on the relayd.
d4519fa3
JD
215 * If major versions are compatible, we assign minor_to_use to the
216 * minor version of the procotol we are going to use for this session.
00e2e675
DG
217 *
218 * Return 0 if compatible else negative value.
219 */
6151a90f 220int relayd_version_check(struct lttcomm_relayd_sock *rsock)
00e2e675
DG
221{
222 int ret;
092b6259 223 struct lttcomm_relayd_version msg;
00e2e675
DG
224
225 /* Code flow error. Safety net. */
6151a90f 226 assert(rsock);
00e2e675 227
6151a90f
JD
228 DBG("Relayd version check for major.minor %u.%u", rsock->major,
229 rsock->minor);
00e2e675 230
092b6259 231 /* Prepare network byte order before transmission. */
6151a90f
JD
232 msg.major = htobe32(rsock->major);
233 msg.minor = htobe32(rsock->minor);
092b6259 234
00e2e675 235 /* Send command */
6151a90f 236 ret = send_command(rsock, RELAYD_VERSION, (void *) &msg, sizeof(msg), 0);
00e2e675
DG
237 if (ret < 0) {
238 goto error;
239 }
240
20275fe8 241 /* Receive response */
6151a90f 242 ret = recv_reply(rsock, (void *) &msg, sizeof(msg));
00e2e675
DG
243 if (ret < 0) {
244 goto error;
245 }
246
247 /* Set back to host bytes order */
092b6259
DG
248 msg.major = be32toh(msg.major);
249 msg.minor = be32toh(msg.minor);
250
251 /*
252 * Only validate the major version. If the other side is higher,
253 * communication is not possible. Only major version equal can talk to each
254 * other. If the minor version differs, the lowest version is used by both
255 * sides.
092b6259 256 */
6151a90f 257 if (msg.major != rsock->major) {
d4519fa3
JD
258 /* Not compatible */
259 ret = -1;
260 DBG2("Relayd version is NOT compatible. Relayd version %u != %u (us)",
6151a90f 261 msg.major, rsock->major);
092b6259 262 goto error;
00e2e675
DG
263 }
264
092b6259 265 /*
6151a90f
JD
266 * If the relayd's minor version is higher, it will adapt to our version so
267 * we can continue to use the latest relayd communication data structure.
268 * If the received minor version is higher, the relayd should adapt to us.
092b6259 269 */
6151a90f
JD
270 if (rsock->minor > msg.minor) {
271 rsock->minor = msg.minor;
d4519fa3 272 }
092b6259 273
d4519fa3
JD
274 /* Version number compatible */
275 DBG2("Relayd version is compatible, using protocol version %u.%u",
6151a90f 276 rsock->major, rsock->minor);
d4519fa3 277 ret = 0;
00e2e675
DG
278
279error:
280 return ret;
281}
282
00e2e675
DG
283/*
284 * Add stream on the relayd and assign stream handle to the stream_id argument.
285 *
286 * On success return 0 else return ret_code negative value.
287 */
6151a90f 288int relayd_send_metadata(struct lttcomm_relayd_sock *rsock, size_t len)
00e2e675
DG
289{
290 int ret;
291
292 /* Code flow error. Safety net. */
6151a90f 293 assert(rsock);
00e2e675 294
77c7c900 295 DBG("Relayd sending metadata of size %zu", len);
00e2e675
DG
296
297 /* Send command */
6151a90f 298 ret = send_command(rsock, RELAYD_SEND_METADATA, NULL, len, 0);
00e2e675
DG
299 if (ret < 0) {
300 goto error;
301 }
302
303 DBG2("Relayd metadata added successfully");
304
305 /*
306 * After that call, the metadata data MUST be sent to the relayd so the
307 * receive size on the other end matches the len of the metadata packet
633d0084 308 * header. This is why we don't wait for a reply here.
00e2e675
DG
309 */
310
311error:
312 return ret;
313}
314
315/*
6151a90f 316 * Connect to relay daemon with an allocated lttcomm_relayd_sock.
00e2e675 317 */
6151a90f 318int relayd_connect(struct lttcomm_relayd_sock *rsock)
00e2e675
DG
319{
320 /* Code flow error. Safety net. */
6151a90f 321 assert(rsock);
00e2e675
DG
322
323 DBG3("Relayd connect ...");
324
6151a90f 325 return rsock->sock.ops->connect(&rsock->sock);
00e2e675
DG
326}
327
328/*
6151a90f 329 * Close relayd socket with an allocated lttcomm_relayd_sock.
ffe60014
DG
330 *
331 * If no socket operations are found, simply return 0 meaning that everything
332 * is fine. Without operations, the socket can not possibly be opened or used.
333 * This is possible if the socket was allocated but not created. However, the
334 * caller could simply use it to store a valid file descriptor for instance
335 * passed over a Unix socket and call this to cleanup but still without a valid
336 * ops pointer.
337 *
338 * Return the close returned value. On error, a negative value is usually
339 * returned back from close(2).
00e2e675 340 */
6151a90f 341int relayd_close(struct lttcomm_relayd_sock *rsock)
00e2e675 342{
ffe60014
DG
343 int ret;
344
00e2e675 345 /* Code flow error. Safety net. */
6151a90f 346 assert(rsock);
00e2e675 347
ffe60014 348 /* An invalid fd is fine, return success. */
6151a90f 349 if (rsock->sock.fd < 0) {
ffe60014
DG
350 ret = 0;
351 goto end;
352 }
353
6151a90f 354 DBG3("Relayd closing socket %d", rsock->sock.fd);
00e2e675 355
6151a90f
JD
356 if (rsock->sock.ops) {
357 ret = rsock->sock.ops->close(&rsock->sock);
ffe60014
DG
358 } else {
359 /* Default call if no specific ops found. */
6151a90f 360 ret = close(rsock->sock.fd);
ffe60014
DG
361 if (ret < 0) {
362 PERROR("relayd_close default close");
363 }
364 }
365
366end:
367 return ret;
00e2e675
DG
368}
369
370/*
371 * Send data header structure to the relayd.
372 */
6151a90f 373int relayd_send_data_hdr(struct lttcomm_relayd_sock *rsock,
00e2e675
DG
374 struct lttcomm_relayd_data_hdr *hdr, size_t size)
375{
376 int ret;
377
378 /* Code flow error. Safety net. */
6151a90f 379 assert(rsock);
00e2e675
DG
380 assert(hdr);
381
633d0084 382 DBG3("Relayd sending data header of size %ld", size);
00e2e675
DG
383
384 /* Again, safety net */
385 if (size == 0) {
386 size = sizeof(struct lttcomm_relayd_data_hdr);
387 }
388
389 /* Only send data header. */
6151a90f 390 ret = rsock->sock.ops->sendmsg(&rsock->sock, hdr, size, 0);
00e2e675 391 if (ret < 0) {
8994307f 392 ret = -errno;
00e2e675
DG
393 goto error;
394 }
395
396 /*
397 * The data MUST be sent right after that command for the receive on the
398 * other end to match the size in the header.
399 */
400
401error:
402 return ret;
403}
173af62f
DG
404
405/*
406 * Send close stream command to the relayd.
407 */
6151a90f 408int relayd_send_close_stream(struct lttcomm_relayd_sock *rsock, uint64_t stream_id,
173af62f
DG
409 uint64_t last_net_seq_num)
410{
411 int ret;
412 struct lttcomm_relayd_close_stream msg;
413 struct lttcomm_relayd_generic_reply reply;
414
415 /* Code flow error. Safety net. */
6151a90f 416 assert(rsock);
173af62f 417
77c7c900 418 DBG("Relayd closing stream id %" PRIu64, stream_id);
173af62f
DG
419
420 msg.stream_id = htobe64(stream_id);
421 msg.last_net_seq_num = htobe64(last_net_seq_num);
422
423 /* Send command */
6151a90f 424 ret = send_command(rsock, RELAYD_CLOSE_STREAM, (void *) &msg, sizeof(msg), 0);
173af62f
DG
425 if (ret < 0) {
426 goto error;
427 }
428
20275fe8 429 /* Receive response */
6151a90f 430 ret = recv_reply(rsock, (void *) &reply, sizeof(reply));
173af62f
DG
431 if (ret < 0) {
432 goto error;
433 }
434
435 reply.ret_code = be32toh(reply.ret_code);
436
437 /* Return session id or negative ret code. */
f73fabfd 438 if (reply.ret_code != LTTNG_OK) {
bb63afd9
DG
439 ret = -1;
440 ERR("Relayd close stream replied error %d", reply.ret_code);
173af62f
DG
441 } else {
442 /* Success */
443 ret = 0;
444 }
445
77c7c900 446 DBG("Relayd close stream id %" PRIu64 " successfully", stream_id);
173af62f
DG
447
448error:
449 return ret;
450}
c8f59ee5
DG
451
452/*
453 * Check for data availability for a given stream id.
454 *
6d805429 455 * Return 0 if NOT pending, 1 if so and a negative value on error.
c8f59ee5 456 */
6151a90f 457int relayd_data_pending(struct lttcomm_relayd_sock *rsock, uint64_t stream_id,
c8f59ee5
DG
458 uint64_t last_net_seq_num)
459{
460 int ret;
6d805429 461 struct lttcomm_relayd_data_pending msg;
c8f59ee5
DG
462 struct lttcomm_relayd_generic_reply reply;
463
464 /* Code flow error. Safety net. */
6151a90f 465 assert(rsock);
c8f59ee5 466
6d805429 467 DBG("Relayd data pending for stream id %" PRIu64, stream_id);
c8f59ee5
DG
468
469 msg.stream_id = htobe64(stream_id);
470 msg.last_net_seq_num = htobe64(last_net_seq_num);
471
472 /* Send command */
6151a90f 473 ret = send_command(rsock, RELAYD_DATA_PENDING, (void *) &msg,
c8f59ee5
DG
474 sizeof(msg), 0);
475 if (ret < 0) {
476 goto error;
477 }
478
20275fe8 479 /* Receive response */
6151a90f 480 ret = recv_reply(rsock, (void *) &reply, sizeof(reply));
c8f59ee5
DG
481 if (ret < 0) {
482 goto error;
483 }
484
485 reply.ret_code = be32toh(reply.ret_code);
486
487 /* Return session id or negative ret code. */
488 if (reply.ret_code >= LTTNG_OK) {
bb63afd9 489 ERR("Relayd data pending replied error %d", reply.ret_code);
c8f59ee5
DG
490 }
491
492 /* At this point, the ret code is either 1 or 0 */
493 ret = reply.ret_code;
494
6d805429 495 DBG("Relayd data is %s pending for stream id %" PRIu64,
9dd26bb9 496 ret == 1 ? "" : "NOT", stream_id);
c8f59ee5
DG
497
498error:
499 return ret;
500}
501
502/*
503 * Check on the relayd side for a quiescent state on the control socket.
504 */
6151a90f 505int relayd_quiescent_control(struct lttcomm_relayd_sock *rsock,
ad7051c0 506 uint64_t metadata_stream_id)
c8f59ee5
DG
507{
508 int ret;
ad7051c0 509 struct lttcomm_relayd_quiescent_control msg;
c8f59ee5
DG
510 struct lttcomm_relayd_generic_reply reply;
511
512 /* Code flow error. Safety net. */
6151a90f 513 assert(rsock);
c8f59ee5
DG
514
515 DBG("Relayd checking quiescent control state");
516
ad7051c0
DG
517 msg.stream_id = htobe64(metadata_stream_id);
518
c8f59ee5 519 /* Send command */
6151a90f 520 ret = send_command(rsock, RELAYD_QUIESCENT_CONTROL, &msg, sizeof(msg), 0);
c8f59ee5
DG
521 if (ret < 0) {
522 goto error;
523 }
524
20275fe8 525 /* Receive response */
6151a90f 526 ret = recv_reply(rsock, (void *) &reply, sizeof(reply));
c8f59ee5
DG
527 if (ret < 0) {
528 goto error;
529 }
530
531 reply.ret_code = be32toh(reply.ret_code);
532
533 /* Return session id or negative ret code. */
534 if (reply.ret_code != LTTNG_OK) {
bb63afd9
DG
535 ret = -1;
536 ERR("Relayd quiescent control replied error %d", reply.ret_code);
c8f59ee5
DG
537 goto error;
538 }
539
540 /* Control socket is quiescent */
6d805429 541 return 0;
c8f59ee5
DG
542
543error:
544 return ret;
545}
f7079f67
DG
546
547/*
548 * Begin a data pending command for a specific session id.
549 */
6151a90f 550int relayd_begin_data_pending(struct lttcomm_relayd_sock *rsock, uint64_t id)
f7079f67
DG
551{
552 int ret;
553 struct lttcomm_relayd_begin_data_pending msg;
554 struct lttcomm_relayd_generic_reply reply;
555
556 /* Code flow error. Safety net. */
6151a90f 557 assert(rsock);
f7079f67
DG
558
559 DBG("Relayd begin data pending");
560
561 msg.session_id = htobe64(id);
562
563 /* Send command */
6151a90f 564 ret = send_command(rsock, RELAYD_BEGIN_DATA_PENDING, &msg, sizeof(msg), 0);
f7079f67
DG
565 if (ret < 0) {
566 goto error;
567 }
568
20275fe8 569 /* Receive response */
6151a90f 570 ret = recv_reply(rsock, (void *) &reply, sizeof(reply));
f7079f67
DG
571 if (ret < 0) {
572 goto error;
573 }
574
575 reply.ret_code = be32toh(reply.ret_code);
576
577 /* Return session id or negative ret code. */
578 if (reply.ret_code != LTTNG_OK) {
bb63afd9
DG
579 ret = -1;
580 ERR("Relayd begin data pending replied error %d", reply.ret_code);
f7079f67
DG
581 goto error;
582 }
583
584 return 0;
585
586error:
587 return ret;
588}
589
590/*
591 * End a data pending command for a specific session id.
592 *
593 * Return 0 on success and set is_data_inflight to 0 if no data is being
594 * streamed or 1 if it is the case.
595 */
6151a90f 596int relayd_end_data_pending(struct lttcomm_relayd_sock *rsock, uint64_t id,
f7079f67
DG
597 unsigned int *is_data_inflight)
598{
599 int ret;
600 struct lttcomm_relayd_end_data_pending msg;
601 struct lttcomm_relayd_generic_reply reply;
602
603 /* Code flow error. Safety net. */
6151a90f 604 assert(rsock);
f7079f67
DG
605
606 DBG("Relayd end data pending");
607
608 msg.session_id = htobe64(id);
609
610 /* Send command */
6151a90f 611 ret = send_command(rsock, RELAYD_END_DATA_PENDING, &msg, sizeof(msg), 0);
f7079f67
DG
612 if (ret < 0) {
613 goto error;
614 }
615
20275fe8 616 /* Receive response */
6151a90f 617 ret = recv_reply(rsock, (void *) &reply, sizeof(reply));
f7079f67
DG
618 if (ret < 0) {
619 goto error;
620 }
621
622 reply.ret_code = be32toh(reply.ret_code);
623 if (reply.ret_code < 0) {
624 ret = reply.ret_code;
625 goto error;
626 }
627
628 *is_data_inflight = reply.ret_code;
629
630 DBG("Relayd end data pending is data inflight: %d", reply.ret_code);
631
632 return 0;
633
634error:
635 return ret;
636}
This page took 0.057272 seconds and 5 git commands to generate.