Consumer rotate stream
[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
6c1c0768 18#define _LGPL_SOURCE
00e2e675
DG
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>
f263b7fd 28#include <common/compat/endian.h>
00e2e675 29#include <common/sessiond-comm/relayd.h>
50adc264 30#include <common/index/ctf-index.h>
00e2e675
DG
31
32#include "relayd.h"
33
34/*
35 * Send command. Fill up the header and append the data.
36 */
6151a90f 37static int send_command(struct lttcomm_relayd_sock *rsock,
76b9afaa 38 enum lttcomm_relayd_command cmd, const void *data, size_t size,
00e2e675
DG
39 int flags)
40{
41 int ret;
42 struct lttcomm_relayd_hdr header;
43 char *buf;
44 uint64_t buf_size = sizeof(header);
45
f96e4545
MD
46 if (rsock->sock.fd < 0) {
47 return -ECONNRESET;
48 }
49
00e2e675
DG
50 if (data) {
51 buf_size += size;
52 }
53
54 buf = zmalloc(buf_size);
55 if (buf == NULL) {
56 PERROR("zmalloc relayd send command buf");
57 ret = -1;
58 goto alloc_error;
59 }
60
53efb85a 61 memset(&header, 0, sizeof(header));
00e2e675
DG
62 header.cmd = htobe32(cmd);
63 header.data_size = htobe64(size);
64
65 /* Zeroed for now since not used. */
66 header.cmd_version = 0;
67 header.circuit_id = 0;
68
69 /* Prepare buffer to send. */
70 memcpy(buf, &header, sizeof(header));
71 if (data) {
72 memcpy(buf + sizeof(header), data, size);
73 }
74
06586bbe 75 DBG3("Relayd sending command %d of size %" PRIu64, (int) cmd, buf_size);
6151a90f 76 ret = rsock->sock.ops->sendmsg(&rsock->sock, buf, buf_size, flags);
00e2e675 77 if (ret < 0) {
06586bbe
JG
78 PERROR("Failed to send command %d of size %" PRIu64,
79 (int) cmd, buf_size);
8994307f 80 ret = -errno;
00e2e675
DG
81 goto error;
82 }
00e2e675
DG
83error:
84 free(buf);
85alloc_error:
86 return ret;
87}
88
89/*
90 * Receive reply data on socket. This MUST be call after send_command or else
91 * could result in unexpected behavior(s).
92 */
6151a90f 93static int recv_reply(struct lttcomm_relayd_sock *rsock, void *data, size_t size)
00e2e675
DG
94{
95 int ret;
96
f96e4545
MD
97 if (rsock->sock.fd < 0) {
98 return -ECONNRESET;
99 }
100
8fd623e0 101 DBG3("Relayd waiting for reply of size %zu", size);
00e2e675 102
6151a90f 103 ret = rsock->sock.ops->recvmsg(&rsock->sock, data, size, 0);
20275fe8
DG
104 if (ret <= 0 || ret != size) {
105 if (ret == 0) {
106 /* Orderly shutdown. */
6151a90f 107 DBG("Socket %d has performed an orderly shutdown", rsock->sock.fd);
20275fe8 108 } else {
8fd623e0 109 DBG("Receiving reply failed on sock %d for size %zu with ret %d",
6151a90f 110 rsock->sock.fd, size, ret);
20275fe8
DG
111 }
112 /* Always return -1 here and the caller can use errno. */
113 ret = -1;
00e2e675
DG
114 goto error;
115 }
116
117error:
118 return ret;
119}
120
d3e2ba59
JD
121/*
122 * Starting at 2.4, RELAYD_CREATE_SESSION takes additional parameters to
123 * support the live reading capability.
124 */
125static int relayd_create_session_2_4(struct lttcomm_relayd_sock *rsock,
126 uint64_t *session_id, char *session_name, char *hostname,
7d2f7452 127 int session_live_timer, unsigned int snapshot)
d3e2ba59
JD
128{
129 int ret;
130 struct lttcomm_relayd_create_session_2_4 msg;
131
3a13ffd5
MD
132 if (lttng_strncpy(msg.session_name, session_name,
133 sizeof(msg.session_name))) {
246777db
MD
134 ret = -1;
135 goto error;
136 }
3a13ffd5 137 if (lttng_strncpy(msg.hostname, hostname, sizeof(msg.hostname))) {
246777db
MD
138 ret = -1;
139 goto error;
140 }
d3e2ba59 141 msg.live_timer = htobe32(session_live_timer);
7d2f7452 142 msg.snapshot = htobe32(snapshot);
d3e2ba59
JD
143
144 /* Send command */
145 ret = send_command(rsock, RELAYD_CREATE_SESSION, &msg, sizeof(msg), 0);
146 if (ret < 0) {
147 goto error;
148 }
149
150error:
151 return ret;
152}
153
154/*
155 * RELAYD_CREATE_SESSION from 2.1 to 2.3.
156 */
157static int relayd_create_session_2_1(struct lttcomm_relayd_sock *rsock,
158 uint64_t *session_id)
159{
160 int ret;
161
162 /* Send command */
163 ret = send_command(rsock, RELAYD_CREATE_SESSION, NULL, 0, 0);
164 if (ret < 0) {
165 goto error;
166 }
167
168error:
169 return ret;
170}
171
c5b6f4f0
DG
172/*
173 * Send a RELAYD_CREATE_SESSION command to the relayd with the given socket and
174 * set session_id of the relayd if we have a successful reply from the relayd.
175 *
20275fe8
DG
176 * On success, return 0 else a negative value which is either an errno error or
177 * a lttng error code from the relayd.
c5b6f4f0 178 */
d3e2ba59 179int relayd_create_session(struct lttcomm_relayd_sock *rsock, uint64_t *session_id,
7d2f7452
DG
180 char *session_name, char *hostname, int session_live_timer,
181 unsigned int snapshot)
c5b6f4f0
DG
182{
183 int ret;
184 struct lttcomm_relayd_status_session reply;
185
6151a90f 186 assert(rsock);
c5b6f4f0
DG
187 assert(session_id);
188
189 DBG("Relayd create session");
190
d3e2ba59
JD
191 switch(rsock->minor) {
192 case 1:
193 case 2:
194 case 3:
195 ret = relayd_create_session_2_1(rsock, session_id);
31d74dc3 196 break;
d3e2ba59
JD
197 case 4:
198 default:
7d2f7452
DG
199 ret = relayd_create_session_2_4(rsock, session_id, session_name,
200 hostname, session_live_timer, snapshot);
31d74dc3 201 break;
d3e2ba59
JD
202 }
203
c5b6f4f0
DG
204 if (ret < 0) {
205 goto error;
206 }
207
20275fe8 208 /* Receive response */
6151a90f 209 ret = recv_reply(rsock, (void *) &reply, sizeof(reply));
c5b6f4f0
DG
210 if (ret < 0) {
211 goto error;
212 }
213
214 reply.session_id = be64toh(reply.session_id);
215 reply.ret_code = be32toh(reply.ret_code);
216
217 /* Return session id or negative ret code. */
218 if (reply.ret_code != LTTNG_OK) {
bb63afd9
DG
219 ret = -1;
220 ERR("Relayd create session replied error %d", reply.ret_code);
c5b6f4f0
DG
221 goto error;
222 } else {
223 ret = 0;
224 *session_id = reply.session_id;
225 }
226
227 DBG("Relayd session created with id %" PRIu64, reply.session_id);
228
229error:
230 return ret;
231}
232
00e2e675
DG
233/*
234 * Add stream on the relayd and assign stream handle to the stream_id argument.
235 *
236 * On success return 0 else return ret_code negative value.
237 */
6151a90f 238int relayd_add_stream(struct lttcomm_relayd_sock *rsock, const char *channel_name,
0f907de1
JD
239 const char *pathname, uint64_t *stream_id,
240 uint64_t tracefile_size, uint64_t tracefile_count)
00e2e675
DG
241{
242 int ret;
243 struct lttcomm_relayd_add_stream msg;
0f907de1 244 struct lttcomm_relayd_add_stream_2_2 msg_2_2;
00e2e675
DG
245 struct lttcomm_relayd_status_stream reply;
246
247 /* Code flow error. Safety net. */
6151a90f 248 assert(rsock);
00e2e675
DG
249 assert(channel_name);
250 assert(pathname);
251
252 DBG("Relayd adding stream for channel name %s", channel_name);
253
0f907de1
JD
254 /* Compat with relayd 2.1 */
255 if (rsock->minor == 1) {
53efb85a 256 memset(&msg, 0, sizeof(msg));
a7c918ad
MD
257 if (lttng_strncpy(msg.channel_name, channel_name,
258 sizeof(msg.channel_name))) {
246777db
MD
259 ret = -1;
260 goto error;
261 }
a7c918ad
MD
262 if (lttng_strncpy(msg.pathname, pathname,
263 sizeof(msg.pathname))) {
246777db
MD
264 ret = -1;
265 goto error;
266 }
00e2e675 267
0f907de1
JD
268 /* Send command */
269 ret = send_command(rsock, RELAYD_ADD_STREAM, (void *) &msg, sizeof(msg), 0);
270 if (ret < 0) {
271 goto error;
272 }
273 } else {
53efb85a 274 memset(&msg_2_2, 0, sizeof(msg_2_2));
0f907de1 275 /* Compat with relayd 2.2+ */
a7c918ad
MD
276 if (lttng_strncpy(msg_2_2.channel_name, channel_name,
277 sizeof(msg_2_2.channel_name))) {
246777db
MD
278 ret = -1;
279 goto error;
280 }
a7c918ad
MD
281 if (lttng_strncpy(msg_2_2.pathname, pathname,
282 sizeof(msg_2_2.pathname))) {
246777db
MD
283 ret = -1;
284 goto error;
285 }
0f907de1
JD
286 msg_2_2.tracefile_size = htobe64(tracefile_size);
287 msg_2_2.tracefile_count = htobe64(tracefile_count);
288
289 /* Send command */
290 ret = send_command(rsock, RELAYD_ADD_STREAM, (void *) &msg_2_2, sizeof(msg_2_2), 0);
291 if (ret < 0) {
292 goto error;
293 }
00e2e675
DG
294 }
295
633d0084 296 /* Waiting for reply */
6151a90f 297 ret = recv_reply(rsock, (void *) &reply, sizeof(reply));
00e2e675
DG
298 if (ret < 0) {
299 goto error;
300 }
301
302 /* Back to host bytes order. */
303 reply.handle = be64toh(reply.handle);
304 reply.ret_code = be32toh(reply.ret_code);
305
306 /* Return session id or negative ret code. */
f73fabfd 307 if (reply.ret_code != LTTNG_OK) {
bb63afd9
DG
308 ret = -1;
309 ERR("Relayd add stream replied error %d", reply.ret_code);
00e2e675
DG
310 } else {
311 /* Success */
312 ret = 0;
313 *stream_id = reply.handle;
314 }
315
77c7c900
MD
316 DBG("Relayd stream added successfully with handle %" PRIu64,
317 reply.handle);
00e2e675
DG
318
319error:
320 return ret;
321}
322
a4baae1b
JD
323/*
324 * Inform the relay that all the streams for the current channel has been sent.
325 *
326 * On success return 0 else return ret_code negative value.
327 */
328int relayd_streams_sent(struct lttcomm_relayd_sock *rsock)
329{
330 int ret;
331 struct lttcomm_relayd_generic_reply reply;
332
333 /* Code flow error. Safety net. */
334 assert(rsock);
335
336 DBG("Relayd sending streams sent.");
337
338 /* This feature was introduced in 2.4, ignore it for earlier versions. */
339 if (rsock->minor < 4) {
340 ret = 0;
341 goto end;
342 }
343
344 /* Send command */
345 ret = send_command(rsock, RELAYD_STREAMS_SENT, NULL, 0, 0);
346 if (ret < 0) {
347 goto error;
348 }
349
350 /* Waiting for reply */
351 ret = recv_reply(rsock, (void *) &reply, sizeof(reply));
352 if (ret < 0) {
353 goto error;
354 }
355
356 /* Back to host bytes order. */
357 reply.ret_code = be32toh(reply.ret_code);
358
359 /* Return session id or negative ret code. */
360 if (reply.ret_code != LTTNG_OK) {
361 ret = -1;
362 ERR("Relayd streams sent replied error %d", reply.ret_code);
363 goto error;
364 } else {
365 /* Success */
366 ret = 0;
367 }
368
369 DBG("Relayd streams sent success");
370
371error:
372end:
373 return ret;
374}
375
00e2e675
DG
376/*
377 * Check version numbers on the relayd.
d4519fa3
JD
378 * If major versions are compatible, we assign minor_to_use to the
379 * minor version of the procotol we are going to use for this session.
00e2e675 380 *
67d5aa28
JD
381 * Return 0 if the two daemons are compatible, LTTNG_ERR_RELAYD_VERSION_FAIL
382 * otherwise, or a negative value on network errors.
00e2e675 383 */
6151a90f 384int relayd_version_check(struct lttcomm_relayd_sock *rsock)
00e2e675
DG
385{
386 int ret;
092b6259 387 struct lttcomm_relayd_version msg;
00e2e675
DG
388
389 /* Code flow error. Safety net. */
6151a90f 390 assert(rsock);
00e2e675 391
6151a90f
JD
392 DBG("Relayd version check for major.minor %u.%u", rsock->major,
393 rsock->minor);
00e2e675 394
53efb85a 395 memset(&msg, 0, sizeof(msg));
092b6259 396 /* Prepare network byte order before transmission. */
6151a90f
JD
397 msg.major = htobe32(rsock->major);
398 msg.minor = htobe32(rsock->minor);
092b6259 399
00e2e675 400 /* Send command */
6151a90f 401 ret = send_command(rsock, RELAYD_VERSION, (void *) &msg, sizeof(msg), 0);
00e2e675
DG
402 if (ret < 0) {
403 goto error;
404 }
405
20275fe8 406 /* Receive response */
6151a90f 407 ret = recv_reply(rsock, (void *) &msg, sizeof(msg));
00e2e675
DG
408 if (ret < 0) {
409 goto error;
410 }
411
412 /* Set back to host bytes order */
092b6259
DG
413 msg.major = be32toh(msg.major);
414 msg.minor = be32toh(msg.minor);
415
416 /*
417 * Only validate the major version. If the other side is higher,
418 * communication is not possible. Only major version equal can talk to each
419 * other. If the minor version differs, the lowest version is used by both
420 * sides.
092b6259 421 */
6151a90f 422 if (msg.major != rsock->major) {
d4519fa3 423 /* Not compatible */
67d5aa28 424 ret = LTTNG_ERR_RELAYD_VERSION_FAIL;
d4519fa3 425 DBG2("Relayd version is NOT compatible. Relayd version %u != %u (us)",
6151a90f 426 msg.major, rsock->major);
092b6259 427 goto error;
00e2e675
DG
428 }
429
092b6259 430 /*
6151a90f
JD
431 * If the relayd's minor version is higher, it will adapt to our version so
432 * we can continue to use the latest relayd communication data structure.
433 * If the received minor version is higher, the relayd should adapt to us.
092b6259 434 */
6151a90f
JD
435 if (rsock->minor > msg.minor) {
436 rsock->minor = msg.minor;
d4519fa3 437 }
092b6259 438
d4519fa3
JD
439 /* Version number compatible */
440 DBG2("Relayd version is compatible, using protocol version %u.%u",
6151a90f 441 rsock->major, rsock->minor);
d4519fa3 442 ret = 0;
00e2e675
DG
443
444error:
445 return ret;
446}
447
00e2e675
DG
448/*
449 * Add stream on the relayd and assign stream handle to the stream_id argument.
450 *
451 * On success return 0 else return ret_code negative value.
452 */
6151a90f 453int relayd_send_metadata(struct lttcomm_relayd_sock *rsock, size_t len)
00e2e675
DG
454{
455 int ret;
456
457 /* Code flow error. Safety net. */
6151a90f 458 assert(rsock);
00e2e675 459
77c7c900 460 DBG("Relayd sending metadata of size %zu", len);
00e2e675
DG
461
462 /* Send command */
6151a90f 463 ret = send_command(rsock, RELAYD_SEND_METADATA, NULL, len, 0);
00e2e675
DG
464 if (ret < 0) {
465 goto error;
466 }
467
468 DBG2("Relayd metadata added successfully");
469
470 /*
471 * After that call, the metadata data MUST be sent to the relayd so the
472 * receive size on the other end matches the len of the metadata packet
633d0084 473 * header. This is why we don't wait for a reply here.
00e2e675
DG
474 */
475
476error:
477 return ret;
478}
479
480/*
6151a90f 481 * Connect to relay daemon with an allocated lttcomm_relayd_sock.
00e2e675 482 */
6151a90f 483int relayd_connect(struct lttcomm_relayd_sock *rsock)
00e2e675
DG
484{
485 /* Code flow error. Safety net. */
6151a90f 486 assert(rsock);
00e2e675 487
f96e4545
MD
488 if (!rsock->sock.ops) {
489 /*
490 * Attempting a connect on a non-initialized socket.
491 */
492 return -ECONNRESET;
493 }
494
00e2e675
DG
495 DBG3("Relayd connect ...");
496
6151a90f 497 return rsock->sock.ops->connect(&rsock->sock);
00e2e675
DG
498}
499
500/*
6151a90f 501 * Close relayd socket with an allocated lttcomm_relayd_sock.
ffe60014
DG
502 *
503 * If no socket operations are found, simply return 0 meaning that everything
504 * is fine. Without operations, the socket can not possibly be opened or used.
505 * This is possible if the socket was allocated but not created. However, the
506 * caller could simply use it to store a valid file descriptor for instance
507 * passed over a Unix socket and call this to cleanup but still without a valid
508 * ops pointer.
509 *
510 * Return the close returned value. On error, a negative value is usually
511 * returned back from close(2).
00e2e675 512 */
6151a90f 513int relayd_close(struct lttcomm_relayd_sock *rsock)
00e2e675 514{
ffe60014
DG
515 int ret;
516
00e2e675 517 /* Code flow error. Safety net. */
6151a90f 518 assert(rsock);
00e2e675 519
ffe60014 520 /* An invalid fd is fine, return success. */
6151a90f 521 if (rsock->sock.fd < 0) {
ffe60014
DG
522 ret = 0;
523 goto end;
524 }
525
6151a90f 526 DBG3("Relayd closing socket %d", rsock->sock.fd);
00e2e675 527
6151a90f
JD
528 if (rsock->sock.ops) {
529 ret = rsock->sock.ops->close(&rsock->sock);
ffe60014
DG
530 } else {
531 /* Default call if no specific ops found. */
6151a90f 532 ret = close(rsock->sock.fd);
ffe60014
DG
533 if (ret < 0) {
534 PERROR("relayd_close default close");
535 }
536 }
f96e4545 537 rsock->sock.fd = -1;
ffe60014
DG
538
539end:
540 return ret;
00e2e675
DG
541}
542
543/*
544 * Send data header structure to the relayd.
545 */
6151a90f 546int relayd_send_data_hdr(struct lttcomm_relayd_sock *rsock,
00e2e675
DG
547 struct lttcomm_relayd_data_hdr *hdr, size_t size)
548{
549 int ret;
550
551 /* Code flow error. Safety net. */
6151a90f 552 assert(rsock);
00e2e675
DG
553 assert(hdr);
554
f96e4545
MD
555 if (rsock->sock.fd < 0) {
556 return -ECONNRESET;
557 }
558
8fd623e0 559 DBG3("Relayd sending data header of size %zu", size);
00e2e675
DG
560
561 /* Again, safety net */
562 if (size == 0) {
563 size = sizeof(struct lttcomm_relayd_data_hdr);
564 }
565
566 /* Only send data header. */
6151a90f 567 ret = rsock->sock.ops->sendmsg(&rsock->sock, hdr, size, 0);
00e2e675 568 if (ret < 0) {
8994307f 569 ret = -errno;
00e2e675
DG
570 goto error;
571 }
572
573 /*
574 * The data MUST be sent right after that command for the receive on the
575 * other end to match the size in the header.
576 */
577
578error:
579 return ret;
580}
173af62f
DG
581
582/*
583 * Send close stream command to the relayd.
584 */
6151a90f 585int relayd_send_close_stream(struct lttcomm_relayd_sock *rsock, uint64_t stream_id,
173af62f
DG
586 uint64_t last_net_seq_num)
587{
588 int ret;
589 struct lttcomm_relayd_close_stream msg;
590 struct lttcomm_relayd_generic_reply reply;
591
592 /* Code flow error. Safety net. */
6151a90f 593 assert(rsock);
173af62f 594
77c7c900 595 DBG("Relayd closing stream id %" PRIu64, stream_id);
173af62f 596
53efb85a 597 memset(&msg, 0, sizeof(msg));
173af62f
DG
598 msg.stream_id = htobe64(stream_id);
599 msg.last_net_seq_num = htobe64(last_net_seq_num);
600
601 /* Send command */
6151a90f 602 ret = send_command(rsock, RELAYD_CLOSE_STREAM, (void *) &msg, sizeof(msg), 0);
173af62f
DG
603 if (ret < 0) {
604 goto error;
605 }
606
20275fe8 607 /* Receive response */
6151a90f 608 ret = recv_reply(rsock, (void *) &reply, sizeof(reply));
173af62f
DG
609 if (ret < 0) {
610 goto error;
611 }
612
613 reply.ret_code = be32toh(reply.ret_code);
614
615 /* Return session id or negative ret code. */
f73fabfd 616 if (reply.ret_code != LTTNG_OK) {
bb63afd9
DG
617 ret = -1;
618 ERR("Relayd close stream replied error %d", reply.ret_code);
173af62f
DG
619 } else {
620 /* Success */
621 ret = 0;
622 }
623
77c7c900 624 DBG("Relayd close stream id %" PRIu64 " successfully", stream_id);
173af62f
DG
625
626error:
627 return ret;
628}
c8f59ee5
DG
629
630/*
631 * Check for data availability for a given stream id.
632 *
6d805429 633 * Return 0 if NOT pending, 1 if so and a negative value on error.
c8f59ee5 634 */
6151a90f 635int relayd_data_pending(struct lttcomm_relayd_sock *rsock, uint64_t stream_id,
c8f59ee5
DG
636 uint64_t last_net_seq_num)
637{
638 int ret;
6d805429 639 struct lttcomm_relayd_data_pending msg;
c8f59ee5
DG
640 struct lttcomm_relayd_generic_reply reply;
641
642 /* Code flow error. Safety net. */
6151a90f 643 assert(rsock);
c8f59ee5 644
6d805429 645 DBG("Relayd data pending for stream id %" PRIu64, stream_id);
c8f59ee5 646
53efb85a 647 memset(&msg, 0, sizeof(msg));
c8f59ee5
DG
648 msg.stream_id = htobe64(stream_id);
649 msg.last_net_seq_num = htobe64(last_net_seq_num);
650
651 /* Send command */
6151a90f 652 ret = send_command(rsock, RELAYD_DATA_PENDING, (void *) &msg,
c8f59ee5
DG
653 sizeof(msg), 0);
654 if (ret < 0) {
655 goto error;
656 }
657
20275fe8 658 /* Receive response */
6151a90f 659 ret = recv_reply(rsock, (void *) &reply, sizeof(reply));
c8f59ee5
DG
660 if (ret < 0) {
661 goto error;
662 }
663
664 reply.ret_code = be32toh(reply.ret_code);
665
666 /* Return session id or negative ret code. */
667 if (reply.ret_code >= LTTNG_OK) {
bb63afd9 668 ERR("Relayd data pending replied error %d", reply.ret_code);
c8f59ee5
DG
669 }
670
671 /* At this point, the ret code is either 1 or 0 */
672 ret = reply.ret_code;
673
6d805429 674 DBG("Relayd data is %s pending for stream id %" PRIu64,
9dd26bb9 675 ret == 1 ? "" : "NOT", stream_id);
c8f59ee5
DG
676
677error:
678 return ret;
679}
680
681/*
682 * Check on the relayd side for a quiescent state on the control socket.
683 */
6151a90f 684int relayd_quiescent_control(struct lttcomm_relayd_sock *rsock,
ad7051c0 685 uint64_t metadata_stream_id)
c8f59ee5
DG
686{
687 int ret;
ad7051c0 688 struct lttcomm_relayd_quiescent_control msg;
c8f59ee5
DG
689 struct lttcomm_relayd_generic_reply reply;
690
691 /* Code flow error. Safety net. */
6151a90f 692 assert(rsock);
c8f59ee5
DG
693
694 DBG("Relayd checking quiescent control state");
695
53efb85a 696 memset(&msg, 0, sizeof(msg));
ad7051c0
DG
697 msg.stream_id = htobe64(metadata_stream_id);
698
c8f59ee5 699 /* Send command */
6151a90f 700 ret = send_command(rsock, RELAYD_QUIESCENT_CONTROL, &msg, sizeof(msg), 0);
c8f59ee5
DG
701 if (ret < 0) {
702 goto error;
703 }
704
20275fe8 705 /* Receive response */
6151a90f 706 ret = recv_reply(rsock, (void *) &reply, sizeof(reply));
c8f59ee5
DG
707 if (ret < 0) {
708 goto error;
709 }
710
711 reply.ret_code = be32toh(reply.ret_code);
712
713 /* Return session id or negative ret code. */
714 if (reply.ret_code != LTTNG_OK) {
bb63afd9
DG
715 ret = -1;
716 ERR("Relayd quiescent control replied error %d", reply.ret_code);
c8f59ee5
DG
717 goto error;
718 }
719
720 /* Control socket is quiescent */
6d805429 721 return 0;
c8f59ee5
DG
722
723error:
724 return ret;
725}
f7079f67
DG
726
727/*
728 * Begin a data pending command for a specific session id.
729 */
6151a90f 730int relayd_begin_data_pending(struct lttcomm_relayd_sock *rsock, uint64_t id)
f7079f67
DG
731{
732 int ret;
733 struct lttcomm_relayd_begin_data_pending msg;
734 struct lttcomm_relayd_generic_reply reply;
735
736 /* Code flow error. Safety net. */
6151a90f 737 assert(rsock);
f7079f67
DG
738
739 DBG("Relayd begin data pending");
740
53efb85a 741 memset(&msg, 0, sizeof(msg));
f7079f67
DG
742 msg.session_id = htobe64(id);
743
744 /* Send command */
6151a90f 745 ret = send_command(rsock, RELAYD_BEGIN_DATA_PENDING, &msg, sizeof(msg), 0);
f7079f67
DG
746 if (ret < 0) {
747 goto error;
748 }
749
20275fe8 750 /* Receive response */
6151a90f 751 ret = recv_reply(rsock, (void *) &reply, sizeof(reply));
f7079f67
DG
752 if (ret < 0) {
753 goto error;
754 }
755
756 reply.ret_code = be32toh(reply.ret_code);
757
758 /* Return session id or negative ret code. */
759 if (reply.ret_code != LTTNG_OK) {
bb63afd9
DG
760 ret = -1;
761 ERR("Relayd begin data pending replied error %d", reply.ret_code);
f7079f67
DG
762 goto error;
763 }
764
765 return 0;
766
767error:
768 return ret;
769}
770
771/*
772 * End a data pending command for a specific session id.
773 *
774 * Return 0 on success and set is_data_inflight to 0 if no data is being
775 * streamed or 1 if it is the case.
776 */
6151a90f 777int relayd_end_data_pending(struct lttcomm_relayd_sock *rsock, uint64_t id,
f7079f67
DG
778 unsigned int *is_data_inflight)
779{
af6c30b5 780 int ret, recv_ret;
f7079f67
DG
781 struct lttcomm_relayd_end_data_pending msg;
782 struct lttcomm_relayd_generic_reply reply;
783
784 /* Code flow error. Safety net. */
6151a90f 785 assert(rsock);
f7079f67
DG
786
787 DBG("Relayd end data pending");
788
53efb85a 789 memset(&msg, 0, sizeof(msg));
f7079f67
DG
790 msg.session_id = htobe64(id);
791
792 /* Send command */
6151a90f 793 ret = send_command(rsock, RELAYD_END_DATA_PENDING, &msg, sizeof(msg), 0);
f7079f67
DG
794 if (ret < 0) {
795 goto error;
796 }
797
20275fe8 798 /* Receive response */
6151a90f 799 ret = recv_reply(rsock, (void *) &reply, sizeof(reply));
f7079f67
DG
800 if (ret < 0) {
801 goto error;
802 }
803
af6c30b5
DG
804 recv_ret = be32toh(reply.ret_code);
805 if (recv_ret < 0) {
806 ret = recv_ret;
f7079f67
DG
807 goto error;
808 }
809
af6c30b5 810 *is_data_inflight = recv_ret;
f7079f67 811
af6c30b5 812 DBG("Relayd end data pending is data inflight: %d", recv_ret);
f7079f67
DG
813
814 return 0;
815
816error:
817 return ret;
818}
1c20f0e2
JD
819
820/*
821 * Send index to the relayd.
822 */
823int relayd_send_index(struct lttcomm_relayd_sock *rsock,
50adc264 824 struct ctf_packet_index *index, uint64_t relay_stream_id,
1c20f0e2
JD
825 uint64_t net_seq_num)
826{
827 int ret;
828 struct lttcomm_relayd_index msg;
829 struct lttcomm_relayd_generic_reply reply;
830
831 /* Code flow error. Safety net. */
832 assert(rsock);
833
834 if (rsock->minor < 4) {
835 DBG("Not sending indexes before protocol 2.4");
836 ret = 0;
837 goto error;
838 }
839
840 DBG("Relayd sending index for stream ID %" PRIu64, relay_stream_id);
841
53efb85a 842 memset(&msg, 0, sizeof(msg));
1c20f0e2
JD
843 msg.relay_stream_id = htobe64(relay_stream_id);
844 msg.net_seq_num = htobe64(net_seq_num);
845
846 /* The index is already in big endian. */
847 msg.packet_size = index->packet_size;
848 msg.content_size = index->content_size;
849 msg.timestamp_begin = index->timestamp_begin;
850 msg.timestamp_end = index->timestamp_end;
851 msg.events_discarded = index->events_discarded;
852 msg.stream_id = index->stream_id;
853
234cd636
JD
854 if (rsock->minor >= 8) {
855 msg.stream_instance_id = index->stream_instance_id;
856 msg.packet_seq_num = index->packet_seq_num;
857 }
858
1c20f0e2 859 /* Send command */
f8f3885c
MD
860 ret = send_command(rsock, RELAYD_SEND_INDEX, &msg,
861 lttcomm_relayd_index_len(lttng_to_index_major(rsock->major,
862 rsock->minor),
863 lttng_to_index_minor(rsock->major, rsock->minor)),
864 0);
1c20f0e2
JD
865 if (ret < 0) {
866 goto error;
867 }
868
869 /* Receive response */
870 ret = recv_reply(rsock, (void *) &reply, sizeof(reply));
871 if (ret < 0) {
872 goto error;
873 }
874
875 reply.ret_code = be32toh(reply.ret_code);
876
877 /* Return session id or negative ret code. */
878 if (reply.ret_code != LTTNG_OK) {
879 ret = -1;
880 ERR("Relayd send index replied error %d", reply.ret_code);
881 } else {
882 /* Success */
883 ret = 0;
884 }
885
886error:
887 return ret;
888}
93ec662e
JD
889
890/*
891 * Ask the relay to reset the metadata trace file (regeneration).
892 */
893int relayd_reset_metadata(struct lttcomm_relayd_sock *rsock,
894 uint64_t stream_id, uint64_t version)
895{
896 int ret;
897 struct lttcomm_relayd_reset_metadata msg;
898 struct lttcomm_relayd_generic_reply reply;
899
900 /* Code flow error. Safety net. */
901 assert(rsock);
902
903 /* Should have been prevented by the sessiond. */
904 if (rsock->minor < 8) {
905 ERR("Metadata regeneration unsupported before 2.8");
906 ret = -1;
907 goto error;
908 }
909
910 DBG("Relayd reset metadata stream id %" PRIu64, stream_id);
911
912 memset(&msg, 0, sizeof(msg));
913 msg.stream_id = htobe64(stream_id);
914 msg.version = htobe64(version);
915
916 /* Send command */
917 ret = send_command(rsock, RELAYD_RESET_METADATA, (void *) &msg, sizeof(msg), 0);
918 if (ret < 0) {
919 goto error;
920 }
921
922 /* Receive response */
923 ret = recv_reply(rsock, (void *) &reply, sizeof(reply));
924 if (ret < 0) {
925 goto error;
926 }
927
928 reply.ret_code = be32toh(reply.ret_code);
929
930 /* Return session id or negative ret code. */
931 if (reply.ret_code != LTTNG_OK) {
932 ret = -1;
933 ERR("Relayd reset metadata replied error %d", reply.ret_code);
934 } else {
935 /* Success */
936 ret = 0;
937 }
938
939 DBG("Relayd reset metadata stream id %" PRIu64 " successfully", stream_id);
940
941error:
942 return ret;
943}
a1ae2ea5 944
d73bf3d7
JD
945int relayd_rotate_stream(struct lttcomm_relayd_sock *rsock, uint64_t stream_id,
946 const char *new_pathname, uint64_t new_chunk_id,
947 uint64_t seq_num)
948{
949 int ret;
950 struct lttcomm_relayd_rotate_stream *msg = NULL;
951 struct lttcomm_relayd_generic_reply reply;
952 size_t len;
953 int msg_len;
954
955 /* Code flow error. Safety net. */
956 assert(rsock);
957
958 DBG("Sending rotate stream id %" PRIu64 " command to relayd", stream_id);
959
960 /* Account for the trailing NULL. */
961 len = strnlen(new_pathname, LTTNG_PATH_MAX) + 1;
962 if (len > LTTNG_PATH_MAX) {
963 ERR("Path used in relayd rotate stream command exceeds the maximal allowed length");
964 ret = -1;
965 goto error;
966 }
967
968 msg_len = offsetof(struct lttcomm_relayd_rotate_stream, new_pathname) + len;
969 msg = zmalloc(msg_len);
970 if (!msg) {
971 PERROR("Failed to allocate relayd rotate stream command of %d bytes",
972 msg_len);
973 ret = -1;
974 goto error;
975 }
976
977 if (lttng_strncpy(msg->new_pathname, new_pathname, len)) {
978 ret = -1;
979 ERR("Failed to copy relayd rotate stream command's new path name");
980 goto error;
981 }
982
983 msg->pathname_length = htobe32(len);
984 msg->stream_id = htobe64(stream_id);
985 msg->new_chunk_id = htobe64(new_chunk_id);
986 /*
987 * The seq_num is invalid for metadata streams, but it is ignored on
988 * the relay.
989 */
990 msg->rotate_at_seq_num = htobe64(seq_num);
991
992 /* Send command. */
993 ret = send_command(rsock, RELAYD_ROTATE_STREAM, (void *) msg, msg_len, 0);
994 if (ret < 0) {
995 ERR("Send rotate command");
996 goto error;
997 }
998
999 /* Receive response. */
1000 ret = recv_reply(rsock, (void *) &reply, sizeof(reply));
1001 if (ret < 0) {
1002 ERR("Receive rotate reply");
1003 goto error;
1004 }
1005
1006 reply.ret_code = be32toh(reply.ret_code);
1007
1008 /* Return session id or negative ret code. */
1009 if (reply.ret_code != LTTNG_OK) {
1010 ret = -1;
1011 ERR("Relayd rotate stream replied error %d", reply.ret_code);
1012 } else {
1013 /* Success. */
1014 ret = 0;
1015 DBG("Relayd rotated stream id %" PRIu64 " successfully", stream_id);
1016 }
1017
1018error:
1019 free(msg);
1020 return ret;
1021}
1022
00fb02ac
JD
1023int relayd_rotate_rename(struct lttcomm_relayd_sock *rsock,
1024 const char *old_path, const char *new_path)
1025{
1026 int ret;
1027 struct lttcomm_relayd_rotate_rename *msg = NULL;
1028 struct lttcomm_relayd_generic_reply reply;
1029 size_t old_path_length, new_path_length;
1030 size_t msg_length;
1031
1032 /* Code flow error. Safety net. */
1033 assert(rsock);
1034
1035 DBG("Relayd rename chunk %s to %s", old_path, new_path);
1036
1037 /* The two paths are sent with a '\0' delimiter between them. */
1038 old_path_length = strlen(old_path) + 1;
1039 new_path_length = strlen(new_path) + 1;
1040
1041 msg_length = sizeof(*msg) + old_path_length + new_path_length;
1042 msg = zmalloc(msg_length);
1043 if (!msg) {
1044 PERROR("zmalloc rotate-rename command message");
1045 ret = -1;
1046 goto error;
1047 }
1048
1049 assert(old_path_length <= UINT32_MAX);
1050 msg->old_path_length = htobe32(old_path_length);
1051
1052 assert(new_path_length <= UINT32_MAX);
1053 msg->new_path_length = htobe32(new_path_length);
1054
1055 strcpy(msg->paths, old_path);
1056 strcpy(msg->paths + old_path_length, new_path);
1057
1058 /* Send command */
1059 ret = send_command(rsock, RELAYD_ROTATE_RENAME, (const void *) msg,
1060 msg_length, 0);
1061 if (ret < 0) {
1062 goto error;
1063 }
1064
1065 /* Receive response */
1066 ret = recv_reply(rsock, (void *) &reply, sizeof(reply));
1067 if (ret < 0) {
1068 goto error;
1069 }
1070
1071 reply.ret_code = be32toh(reply.ret_code);
1072
1073 /* Return session id or negative ret code. */
1074 if (reply.ret_code != LTTNG_OK) {
1075 ret = -1;
1076 ERR("Relayd rotate rename replied error %d", reply.ret_code);
1077 } else {
1078 /* Success */
1079 ret = 0;
1080 }
1081
1082 DBG("Relayd rotate rename completed successfully");
1083
1084error:
1085 free(msg);
1086 return ret;
1087}
1088
a1ae2ea5
JD
1089int relayd_mkdir(struct lttcomm_relayd_sock *rsock, const char *path)
1090{
1091 int ret;
1092 struct lttcomm_relayd_mkdir *msg;
1093 struct lttcomm_relayd_generic_reply reply;
1094 size_t len;
1095
1096 /* Code flow error. Safety net. */
1097 assert(rsock);
1098
1099 DBG("Relayd mkdir path %s", path);
1100
1101 len = strlen(path) + 1;
1102 msg = zmalloc(sizeof(msg->length) + len);
1103 if (!msg) {
1104 PERROR("Alloc mkdir msg");
1105 ret = -1;
1106 goto error;
1107 }
1108 msg->length = htobe32((uint32_t) len);
1109
1110 if (lttng_strncpy(msg->path, path, len)) {
1111 ret = -1;
1112 goto error;
1113 }
1114
1115 /* Send command */
1116 ret = send_command(rsock, RELAYD_MKDIR, (void *) msg,
1117 sizeof(msg->length) + len, 0);
1118 if (ret < 0) {
1119 goto error;
1120 }
1121
1122 /* Receive response */
1123 ret = recv_reply(rsock, (void *) &reply, sizeof(reply));
1124 if (ret < 0) {
1125 goto error;
1126 }
1127
1128 reply.ret_code = be32toh(reply.ret_code);
1129
1130 /* Return session id or negative ret code. */
1131 if (reply.ret_code != LTTNG_OK) {
1132 ret = -1;
1133 ERR("Relayd mkdir replied error %d", reply.ret_code);
1134 } else {
1135 /* Success */
1136 ret = 0;
1137 }
1138
1139 DBG("Relayd mkdir completed successfully");
1140
1141error:
1142 free(msg);
1143 return ret;
a1ae2ea5 1144}
This page took 0.124993 seconds and 4 git commands to generate.