2 * Copyright (C) 2012 - David Goulet <dgoulet@efficios.com>
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.
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
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.
26 #include <common/common.h>
27 #include <common/defaults.h>
28 #include <common/compat/endian.h>
29 #include <common/sessiond-comm/relayd.h>
30 #include <common/index/ctf-index.h>
35 * Send command. Fill up the header and append the data.
37 static int send_command(struct lttcomm_relayd_sock
*rsock
,
38 enum lttcomm_relayd_command cmd
, const void *data
, size_t size
,
42 struct lttcomm_relayd_hdr header
;
44 uint64_t buf_size
= sizeof(header
);
46 if (rsock
->sock
.fd
< 0) {
54 buf
= zmalloc(buf_size
);
56 PERROR("zmalloc relayd send command buf");
61 memset(&header
, 0, sizeof(header
));
62 header
.cmd
= htobe32(cmd
);
63 header
.data_size
= htobe64(size
);
65 /* Zeroed for now since not used. */
66 header
.cmd_version
= 0;
67 header
.circuit_id
= 0;
69 /* Prepare buffer to send. */
70 memcpy(buf
, &header
, sizeof(header
));
72 memcpy(buf
+ sizeof(header
), data
, size
);
75 DBG3("Relayd sending command %d of size %" PRIu64
, (int) cmd
, buf_size
);
76 ret
= rsock
->sock
.ops
->sendmsg(&rsock
->sock
, buf
, buf_size
, flags
);
78 PERROR("Failed to send command %d of size %" PRIu64
,
90 * Receive reply data on socket. This MUST be call after send_command or else
91 * could result in unexpected behavior(s).
93 static int recv_reply(struct lttcomm_relayd_sock
*rsock
, void *data
, size_t size
)
97 if (rsock
->sock
.fd
< 0) {
101 DBG3("Relayd waiting for reply of size %zu", size
);
103 ret
= rsock
->sock
.ops
->recvmsg(&rsock
->sock
, data
, size
, 0);
104 if (ret
<= 0 || ret
!= size
) {
106 /* Orderly shutdown. */
107 DBG("Socket %d has performed an orderly shutdown", rsock
->sock
.fd
);
109 DBG("Receiving reply failed on sock %d for size %zu with ret %d",
110 rsock
->sock
.fd
, size
, ret
);
112 /* Always return -1 here and the caller can use errno. */
122 * Starting from 2.11, RELAYD_CREATE_SESSION payload (session_name & hostname)
123 * have no length restriction on the sender side.
124 * Length for both payloads is stored in the msg struct. A new dynamic size
125 * payload size is introduced.
127 static int relayd_create_session_2_11(struct lttcomm_relayd_sock
*rsock
,
128 char *session_name
, char *hostname
,
129 int session_live_timer
, unsigned int snapshot
)
132 struct lttcomm_relayd_create_session_2_11
*msg
= NULL
;
133 size_t session_name_len
;
137 /* The two names are sent with a '\0' delimiter between them. */
138 session_name_len
= strlen(session_name
) + 1;
139 hostname_len
= strlen(hostname
) + 1;
141 msg_length
= sizeof(*msg
) + session_name_len
+ hostname_len
;
142 msg
= zmalloc(msg_length
);
144 PERROR("zmalloc create_session_2_11 command message");
149 assert(session_name_len
<= UINT32_MAX
);
150 msg
->session_name_len
= htobe32(session_name_len
);
152 assert(hostname_len
<= UINT32_MAX
);
153 msg
->hostname_len
= htobe32(hostname_len
);
155 if (lttng_strncpy(msg
->names
, session_name
, session_name_len
)) {
159 if (lttng_strncpy(msg
->names
+ session_name_len
, hostname
, hostname_len
)) {
164 msg
->live_timer
= htobe32(session_live_timer
);
165 msg
->snapshot
= !!snapshot
;
168 ret
= send_command(rsock
, RELAYD_CREATE_SESSION
, msg
, msg_length
, 0);
177 * From 2.4 to 2.10, RELAYD_CREATE_SESSION takes additional parameters to
178 * support the live reading capability.
180 static int relayd_create_session_2_4(struct lttcomm_relayd_sock
*rsock
,
181 char *session_name
, char *hostname
, int session_live_timer
,
182 unsigned int snapshot
)
185 struct lttcomm_relayd_create_session_2_4 msg
;
187 if (lttng_strncpy(msg
.session_name
, session_name
,
188 sizeof(msg
.session_name
))) {
192 if (lttng_strncpy(msg
.hostname
, hostname
, sizeof(msg
.hostname
))) {
196 msg
.live_timer
= htobe32(session_live_timer
);
197 msg
.snapshot
= htobe32(snapshot
);
200 ret
= send_command(rsock
, RELAYD_CREATE_SESSION
, &msg
, sizeof(msg
), 0);
210 * RELAYD_CREATE_SESSION from 2.1 to 2.3.
212 static int relayd_create_session_2_1(struct lttcomm_relayd_sock
*rsock
)
217 ret
= send_command(rsock
, RELAYD_CREATE_SESSION
, NULL
, 0, 0);
227 * Send a RELAYD_CREATE_SESSION command to the relayd with the given socket and
228 * set session_id of the relayd if we have a successful reply from the relayd.
230 * On success, return 0 else a negative value which is either an errno error or
231 * a lttng error code from the relayd.
233 int relayd_create_session(struct lttcomm_relayd_sock
*rsock
, uint64_t *session_id
,
234 char *session_name
, char *hostname
, int session_live_timer
,
235 unsigned int snapshot
)
238 struct lttcomm_relayd_status_session reply
;
243 DBG("Relayd create session");
245 if (rsock
->minor
< 4) {
246 /* From 2.1 to 2.3 */
247 ret
= relayd_create_session_2_1(rsock
);
248 } else if (rsock
->minor
>= 4 && rsock
->minor
< 11) {
249 /* From 2.4 to 2.10 */
250 ret
= relayd_create_session_2_4(rsock
, session_name
,
251 hostname
, session_live_timer
, snapshot
);
253 /* From 2.11 to ... */
254 ret
= relayd_create_session_2_11(rsock
, session_name
,
255 hostname
, session_live_timer
, snapshot
);
262 /* Receive response */
263 ret
= recv_reply(rsock
, (void *) &reply
, sizeof(reply
));
268 reply
.session_id
= be64toh(reply
.session_id
);
269 reply
.ret_code
= be32toh(reply
.ret_code
);
271 /* Return session id or negative ret code. */
272 if (reply
.ret_code
!= LTTNG_OK
) {
274 ERR("Relayd create session replied error %d", reply
.ret_code
);
278 *session_id
= reply
.session_id
;
281 DBG("Relayd session created with id %" PRIu64
, reply
.session_id
);
287 static int relayd_add_stream_2_1(struct lttcomm_relayd_sock
*rsock
,
288 const char *channel_name
, const char *pathname
)
291 struct lttcomm_relayd_add_stream msg
;
293 memset(&msg
, 0, sizeof(msg
));
294 if (lttng_strncpy(msg
.channel_name
, channel_name
,
295 sizeof(msg
.channel_name
))) {
300 if (lttng_strncpy(msg
.pathname
, pathname
,
301 sizeof(msg
.pathname
))) {
307 ret
= send_command(rsock
, RELAYD_ADD_STREAM
, (void *) &msg
, sizeof(msg
), 0);
317 static int relayd_add_stream_2_2(struct lttcomm_relayd_sock
*rsock
,
318 const char *channel_name
, const char *pathname
,
319 uint64_t tracefile_size
, uint64_t tracefile_count
)
322 struct lttcomm_relayd_add_stream_2_2 msg
;
324 memset(&msg
, 0, sizeof(msg
));
325 /* Compat with relayd 2.2 to 2.10 */
326 if (lttng_strncpy(msg
.channel_name
, channel_name
,
327 sizeof(msg
.channel_name
))) {
331 if (lttng_strncpy(msg
.pathname
, pathname
,
332 sizeof(msg
.pathname
))) {
336 msg
.tracefile_size
= htobe64(tracefile_size
);
337 msg
.tracefile_count
= htobe64(tracefile_count
);
340 ret
= send_command(rsock
, RELAYD_ADD_STREAM
, (void *) &msg
, sizeof(msg
), 0);
349 static int relayd_add_stream_2_11(struct lttcomm_relayd_sock
*rsock
,
350 const char *channel_name
, const char *pathname
,
351 uint64_t tracefile_size
, uint64_t tracefile_count
)
354 struct lttcomm_relayd_add_stream_2_11
*msg
= NULL
;
355 size_t channel_name_len
;
359 /* The two names are sent with a '\0' delimiter between them. */
360 channel_name_len
= strlen(channel_name
) + 1;
361 pathname_len
= strlen(pathname
) + 1;
363 msg_length
= sizeof(*msg
) + channel_name_len
+ pathname_len
;
364 msg
= zmalloc(msg_length
);
366 PERROR("zmalloc add_stream_2_11 command message");
371 assert(channel_name_len
<= UINT32_MAX
);
372 msg
->channel_name_len
= htobe32(channel_name_len
);
374 assert(pathname_len
<= UINT32_MAX
);
375 msg
->pathname_len
= htobe32(pathname_len
);
377 if (lttng_strncpy(msg
->names
, channel_name
, channel_name_len
)) {
381 if (lttng_strncpy(msg
->names
+ channel_name_len
, pathname
, pathname_len
)) {
386 msg
->tracefile_size
= htobe64(tracefile_size
);
387 msg
->tracefile_count
= htobe64(tracefile_count
);
390 ret
= send_command(rsock
, RELAYD_ADD_STREAM
, (void *) msg
, msg_length
, 0);
401 * Add stream on the relayd and assign stream handle to the stream_id argument.
403 * On success return 0 else return ret_code negative value.
405 int relayd_add_stream(struct lttcomm_relayd_sock
*rsock
, const char *channel_name
,
406 const char *pathname
, uint64_t *stream_id
,
407 uint64_t tracefile_size
, uint64_t tracefile_count
,
408 uint64_t trace_archive_id
)
411 struct lttcomm_relayd_status_stream reply
;
413 /* Code flow error. Safety net. */
415 assert(channel_name
);
418 DBG("Relayd adding stream for channel name %s", channel_name
);
420 /* Compat with relayd 2.1 */
421 if (rsock
->minor
== 1) {
423 ret
= relayd_add_stream_2_1(rsock
, channel_name
, pathname
);
425 } else if (rsock
->minor
> 1 && rsock
->minor
< 11) {
426 /* From 2.2 to 2.10 */
427 ret
= relayd_add_stream_2_2(rsock
, channel_name
, pathname
,
428 tracefile_size
, tracefile_count
);
430 /* From 2.11 to ...*/
431 ret
= relayd_add_stream_2_11(rsock
, channel_name
, pathname
,
432 tracefile_size
, tracefile_count
);
440 /* Waiting for reply */
441 ret
= recv_reply(rsock
, (void *) &reply
, sizeof(reply
));
446 /* Back to host bytes order. */
447 reply
.handle
= be64toh(reply
.handle
);
448 reply
.ret_code
= be32toh(reply
.ret_code
);
450 /* Return session id or negative ret code. */
451 if (reply
.ret_code
!= LTTNG_OK
) {
453 ERR("Relayd add stream replied error %d", reply
.ret_code
);
457 *stream_id
= reply
.handle
;
460 DBG("Relayd stream added successfully with handle %" PRIu64
,
468 * Inform the relay that all the streams for the current channel has been sent.
470 * On success return 0 else return ret_code negative value.
472 int relayd_streams_sent(struct lttcomm_relayd_sock
*rsock
)
475 struct lttcomm_relayd_generic_reply reply
;
477 /* Code flow error. Safety net. */
480 DBG("Relayd sending streams sent.");
482 /* This feature was introduced in 2.4, ignore it for earlier versions. */
483 if (rsock
->minor
< 4) {
489 ret
= send_command(rsock
, RELAYD_STREAMS_SENT
, NULL
, 0, 0);
494 /* Waiting for reply */
495 ret
= recv_reply(rsock
, (void *) &reply
, sizeof(reply
));
500 /* Back to host bytes order. */
501 reply
.ret_code
= be32toh(reply
.ret_code
);
503 /* Return session id or negative ret code. */
504 if (reply
.ret_code
!= LTTNG_OK
) {
506 ERR("Relayd streams sent replied error %d", reply
.ret_code
);
513 DBG("Relayd streams sent success");
521 * Check version numbers on the relayd.
522 * If major versions are compatible, we assign minor_to_use to the
523 * minor version of the procotol we are going to use for this session.
525 * Return 0 if the two daemons are compatible, LTTNG_ERR_RELAYD_VERSION_FAIL
526 * otherwise, or a negative value on network errors.
528 int relayd_version_check(struct lttcomm_relayd_sock
*rsock
)
531 struct lttcomm_relayd_version msg
;
533 /* Code flow error. Safety net. */
536 DBG("Relayd version check for major.minor %u.%u", rsock
->major
,
539 memset(&msg
, 0, sizeof(msg
));
540 /* Prepare network byte order before transmission. */
541 msg
.major
= htobe32(rsock
->major
);
542 msg
.minor
= htobe32(rsock
->minor
);
545 ret
= send_command(rsock
, RELAYD_VERSION
, (void *) &msg
, sizeof(msg
), 0);
550 /* Receive response */
551 ret
= recv_reply(rsock
, (void *) &msg
, sizeof(msg
));
556 /* Set back to host bytes order */
557 msg
.major
= be32toh(msg
.major
);
558 msg
.minor
= be32toh(msg
.minor
);
561 * Only validate the major version. If the other side is higher,
562 * communication is not possible. Only major version equal can talk to each
563 * other. If the minor version differs, the lowest version is used by both
566 if (msg
.major
!= rsock
->major
) {
568 ret
= LTTNG_ERR_RELAYD_VERSION_FAIL
;
569 DBG2("Relayd version is NOT compatible. Relayd version %u != %u (us)",
570 msg
.major
, rsock
->major
);
575 * If the relayd's minor version is higher, it will adapt to our version so
576 * we can continue to use the latest relayd communication data structure.
577 * If the received minor version is higher, the relayd should adapt to us.
579 if (rsock
->minor
> msg
.minor
) {
580 rsock
->minor
= msg
.minor
;
583 /* Version number compatible */
584 DBG2("Relayd version is compatible, using protocol version %u.%u",
585 rsock
->major
, rsock
->minor
);
593 * Add stream on the relayd and assign stream handle to the stream_id argument.
595 * On success return 0 else return ret_code negative value.
597 int relayd_send_metadata(struct lttcomm_relayd_sock
*rsock
, size_t len
)
601 /* Code flow error. Safety net. */
604 DBG("Relayd sending metadata of size %zu", len
);
607 ret
= send_command(rsock
, RELAYD_SEND_METADATA
, NULL
, len
, 0);
612 DBG2("Relayd metadata added successfully");
615 * After that call, the metadata data MUST be sent to the relayd so the
616 * receive size on the other end matches the len of the metadata packet
617 * header. This is why we don't wait for a reply here.
625 * Connect to relay daemon with an allocated lttcomm_relayd_sock.
627 int relayd_connect(struct lttcomm_relayd_sock
*rsock
)
629 /* Code flow error. Safety net. */
632 if (!rsock
->sock
.ops
) {
634 * Attempting a connect on a non-initialized socket.
639 DBG3("Relayd connect ...");
641 return rsock
->sock
.ops
->connect(&rsock
->sock
);
645 * Close relayd socket with an allocated lttcomm_relayd_sock.
647 * If no socket operations are found, simply return 0 meaning that everything
648 * is fine. Without operations, the socket can not possibly be opened or used.
649 * This is possible if the socket was allocated but not created. However, the
650 * caller could simply use it to store a valid file descriptor for instance
651 * passed over a Unix socket and call this to cleanup but still without a valid
654 * Return the close returned value. On error, a negative value is usually
655 * returned back from close(2).
657 int relayd_close(struct lttcomm_relayd_sock
*rsock
)
661 /* Code flow error. Safety net. */
664 /* An invalid fd is fine, return success. */
665 if (rsock
->sock
.fd
< 0) {
670 DBG3("Relayd closing socket %d", rsock
->sock
.fd
);
672 if (rsock
->sock
.ops
) {
673 ret
= rsock
->sock
.ops
->close(&rsock
->sock
);
675 /* Default call if no specific ops found. */
676 ret
= close(rsock
->sock
.fd
);
678 PERROR("relayd_close default close");
688 * Send data header structure to the relayd.
690 int relayd_send_data_hdr(struct lttcomm_relayd_sock
*rsock
,
691 struct lttcomm_relayd_data_hdr
*hdr
, size_t size
)
695 /* Code flow error. Safety net. */
699 if (rsock
->sock
.fd
< 0) {
703 DBG3("Relayd sending data header of size %zu", size
);
705 /* Again, safety net */
707 size
= sizeof(struct lttcomm_relayd_data_hdr
);
710 /* Only send data header. */
711 ret
= rsock
->sock
.ops
->sendmsg(&rsock
->sock
, hdr
, size
, 0);
718 * The data MUST be sent right after that command for the receive on the
719 * other end to match the size in the header.
727 * Send close stream command to the relayd.
729 int relayd_send_close_stream(struct lttcomm_relayd_sock
*rsock
, uint64_t stream_id
,
730 uint64_t last_net_seq_num
)
733 struct lttcomm_relayd_close_stream msg
;
734 struct lttcomm_relayd_generic_reply reply
;
736 /* Code flow error. Safety net. */
739 DBG("Relayd closing stream id %" PRIu64
, stream_id
);
741 memset(&msg
, 0, sizeof(msg
));
742 msg
.stream_id
= htobe64(stream_id
);
743 msg
.last_net_seq_num
= htobe64(last_net_seq_num
);
746 ret
= send_command(rsock
, RELAYD_CLOSE_STREAM
, (void *) &msg
, sizeof(msg
), 0);
751 /* Receive response */
752 ret
= recv_reply(rsock
, (void *) &reply
, sizeof(reply
));
757 reply
.ret_code
= be32toh(reply
.ret_code
);
759 /* Return session id or negative ret code. */
760 if (reply
.ret_code
!= LTTNG_OK
) {
762 ERR("Relayd close stream replied error %d", reply
.ret_code
);
768 DBG("Relayd close stream id %" PRIu64
" successfully", stream_id
);
775 * Check for data availability for a given stream id.
777 * Return 0 if NOT pending, 1 if so and a negative value on error.
779 int relayd_data_pending(struct lttcomm_relayd_sock
*rsock
, uint64_t stream_id
,
780 uint64_t last_net_seq_num
)
783 struct lttcomm_relayd_data_pending msg
;
784 struct lttcomm_relayd_generic_reply reply
;
786 /* Code flow error. Safety net. */
789 DBG("Relayd data pending for stream id %" PRIu64
, stream_id
);
791 memset(&msg
, 0, sizeof(msg
));
792 msg
.stream_id
= htobe64(stream_id
);
793 msg
.last_net_seq_num
= htobe64(last_net_seq_num
);
796 ret
= send_command(rsock
, RELAYD_DATA_PENDING
, (void *) &msg
,
802 /* Receive response */
803 ret
= recv_reply(rsock
, (void *) &reply
, sizeof(reply
));
808 reply
.ret_code
= be32toh(reply
.ret_code
);
810 /* Return session id or negative ret code. */
811 if (reply
.ret_code
>= LTTNG_OK
) {
812 ERR("Relayd data pending replied error %d", reply
.ret_code
);
815 /* At this point, the ret code is either 1 or 0 */
816 ret
= reply
.ret_code
;
818 DBG("Relayd data is %s pending for stream id %" PRIu64
,
819 ret
== 1 ? "" : "NOT", stream_id
);
826 * Check on the relayd side for a quiescent state on the control socket.
828 int relayd_quiescent_control(struct lttcomm_relayd_sock
*rsock
,
829 uint64_t metadata_stream_id
)
832 struct lttcomm_relayd_quiescent_control msg
;
833 struct lttcomm_relayd_generic_reply reply
;
835 /* Code flow error. Safety net. */
838 DBG("Relayd checking quiescent control state");
840 memset(&msg
, 0, sizeof(msg
));
841 msg
.stream_id
= htobe64(metadata_stream_id
);
844 ret
= send_command(rsock
, RELAYD_QUIESCENT_CONTROL
, &msg
, sizeof(msg
), 0);
849 /* Receive response */
850 ret
= recv_reply(rsock
, (void *) &reply
, sizeof(reply
));
855 reply
.ret_code
= be32toh(reply
.ret_code
);
857 /* Return session id or negative ret code. */
858 if (reply
.ret_code
!= LTTNG_OK
) {
860 ERR("Relayd quiescent control replied error %d", reply
.ret_code
);
864 /* Control socket is quiescent */
872 * Begin a data pending command for a specific session id.
874 int relayd_begin_data_pending(struct lttcomm_relayd_sock
*rsock
, uint64_t id
)
877 struct lttcomm_relayd_begin_data_pending msg
;
878 struct lttcomm_relayd_generic_reply reply
;
880 /* Code flow error. Safety net. */
883 DBG("Relayd begin data pending");
885 memset(&msg
, 0, sizeof(msg
));
886 msg
.session_id
= htobe64(id
);
889 ret
= send_command(rsock
, RELAYD_BEGIN_DATA_PENDING
, &msg
, sizeof(msg
), 0);
894 /* Receive response */
895 ret
= recv_reply(rsock
, (void *) &reply
, sizeof(reply
));
900 reply
.ret_code
= be32toh(reply
.ret_code
);
902 /* Return session id or negative ret code. */
903 if (reply
.ret_code
!= LTTNG_OK
) {
905 ERR("Relayd begin data pending replied error %d", reply
.ret_code
);
916 * End a data pending command for a specific session id.
918 * Return 0 on success and set is_data_inflight to 0 if no data is being
919 * streamed or 1 if it is the case.
921 int relayd_end_data_pending(struct lttcomm_relayd_sock
*rsock
, uint64_t id
,
922 unsigned int *is_data_inflight
)
925 struct lttcomm_relayd_end_data_pending msg
;
926 struct lttcomm_relayd_generic_reply reply
;
928 /* Code flow error. Safety net. */
931 DBG("Relayd end data pending");
933 memset(&msg
, 0, sizeof(msg
));
934 msg
.session_id
= htobe64(id
);
937 ret
= send_command(rsock
, RELAYD_END_DATA_PENDING
, &msg
, sizeof(msg
), 0);
942 /* Receive response */
943 ret
= recv_reply(rsock
, (void *) &reply
, sizeof(reply
));
948 recv_ret
= be32toh(reply
.ret_code
);
954 *is_data_inflight
= recv_ret
;
956 DBG("Relayd end data pending is data inflight: %d", recv_ret
);
965 * Send index to the relayd.
967 int relayd_send_index(struct lttcomm_relayd_sock
*rsock
,
968 struct ctf_packet_index
*index
, uint64_t relay_stream_id
,
969 uint64_t net_seq_num
)
972 struct lttcomm_relayd_index msg
;
973 struct lttcomm_relayd_generic_reply reply
;
975 /* Code flow error. Safety net. */
978 if (rsock
->minor
< 4) {
979 DBG("Not sending indexes before protocol 2.4");
984 DBG("Relayd sending index for stream ID %" PRIu64
, relay_stream_id
);
986 memset(&msg
, 0, sizeof(msg
));
987 msg
.relay_stream_id
= htobe64(relay_stream_id
);
988 msg
.net_seq_num
= htobe64(net_seq_num
);
990 /* The index is already in big endian. */
991 msg
.packet_size
= index
->packet_size
;
992 msg
.content_size
= index
->content_size
;
993 msg
.timestamp_begin
= index
->timestamp_begin
;
994 msg
.timestamp_end
= index
->timestamp_end
;
995 msg
.events_discarded
= index
->events_discarded
;
996 msg
.stream_id
= index
->stream_id
;
998 if (rsock
->minor
>= 8) {
999 msg
.stream_instance_id
= index
->stream_instance_id
;
1000 msg
.packet_seq_num
= index
->packet_seq_num
;
1004 ret
= send_command(rsock
, RELAYD_SEND_INDEX
, &msg
,
1005 lttcomm_relayd_index_len(lttng_to_index_major(rsock
->major
,
1007 lttng_to_index_minor(rsock
->major
, rsock
->minor
)),
1013 /* Receive response */
1014 ret
= recv_reply(rsock
, (void *) &reply
, sizeof(reply
));
1019 reply
.ret_code
= be32toh(reply
.ret_code
);
1021 /* Return session id or negative ret code. */
1022 if (reply
.ret_code
!= LTTNG_OK
) {
1024 ERR("Relayd send index replied error %d", reply
.ret_code
);
1035 * Ask the relay to reset the metadata trace file (regeneration).
1037 int relayd_reset_metadata(struct lttcomm_relayd_sock
*rsock
,
1038 uint64_t stream_id
, uint64_t version
)
1041 struct lttcomm_relayd_reset_metadata msg
;
1042 struct lttcomm_relayd_generic_reply reply
;
1044 /* Code flow error. Safety net. */
1047 /* Should have been prevented by the sessiond. */
1048 if (rsock
->minor
< 8) {
1049 ERR("Metadata regeneration unsupported before 2.8");
1054 DBG("Relayd reset metadata stream id %" PRIu64
, stream_id
);
1056 memset(&msg
, 0, sizeof(msg
));
1057 msg
.stream_id
= htobe64(stream_id
);
1058 msg
.version
= htobe64(version
);
1061 ret
= send_command(rsock
, RELAYD_RESET_METADATA
, (void *) &msg
, sizeof(msg
), 0);
1066 /* Receive response */
1067 ret
= recv_reply(rsock
, (void *) &reply
, sizeof(reply
));
1072 reply
.ret_code
= be32toh(reply
.ret_code
);
1074 /* Return session id or negative ret code. */
1075 if (reply
.ret_code
!= LTTNG_OK
) {
1077 ERR("Relayd reset metadata replied error %d", reply
.ret_code
);
1083 DBG("Relayd reset metadata stream id %" PRIu64
" successfully", stream_id
);
1089 int relayd_rotate_stream(struct lttcomm_relayd_sock
*rsock
, uint64_t stream_id
,
1090 const char *new_pathname
, uint64_t new_chunk_id
,
1094 struct lttcomm_relayd_rotate_stream
*msg
= NULL
;
1095 struct lttcomm_relayd_generic_reply reply
;
1099 /* Code flow error. Safety net. */
1102 DBG("Sending rotate stream id %" PRIu64
" command to relayd", stream_id
);
1104 /* Account for the trailing NULL. */
1105 len
= strnlen(new_pathname
, LTTNG_PATH_MAX
) + 1;
1106 if (len
> LTTNG_PATH_MAX
) {
1107 ERR("Path used in relayd rotate stream command exceeds the maximal allowed length");
1112 msg_len
= offsetof(struct lttcomm_relayd_rotate_stream
, new_pathname
) + len
;
1113 msg
= zmalloc(msg_len
);
1115 PERROR("Failed to allocate relayd rotate stream command of %d bytes",
1121 if (lttng_strncpy(msg
->new_pathname
, new_pathname
, len
)) {
1123 ERR("Failed to copy relayd rotate stream command's new path name");
1127 msg
->pathname_length
= htobe32(len
);
1128 msg
->stream_id
= htobe64(stream_id
);
1129 msg
->new_chunk_id
= htobe64(new_chunk_id
);
1131 * The seq_num is invalid for metadata streams, but it is ignored on
1134 msg
->rotate_at_seq_num
= htobe64(seq_num
);
1137 ret
= send_command(rsock
, RELAYD_ROTATE_STREAM
, (void *) msg
, msg_len
, 0);
1139 ERR("Send rotate command");
1143 /* Receive response. */
1144 ret
= recv_reply(rsock
, (void *) &reply
, sizeof(reply
));
1146 ERR("Receive rotate reply");
1150 reply
.ret_code
= be32toh(reply
.ret_code
);
1152 /* Return session id or negative ret code. */
1153 if (reply
.ret_code
!= LTTNG_OK
) {
1155 ERR("Relayd rotate stream replied error %d", reply
.ret_code
);
1159 DBG("Relayd rotated stream id %" PRIu64
" successfully", stream_id
);
1167 int relayd_rotate_rename(struct lttcomm_relayd_sock
*rsock
,
1168 const char *old_path
, const char *new_path
)
1171 struct lttcomm_relayd_rotate_rename
*msg
= NULL
;
1172 struct lttcomm_relayd_generic_reply reply
;
1173 size_t old_path_length
, new_path_length
;
1176 /* Code flow error. Safety net. */
1179 DBG("Relayd rename chunk %s to %s", old_path
, new_path
);
1181 /* The two paths are sent with a '\0' delimiter between them. */
1182 old_path_length
= strlen(old_path
) + 1;
1183 new_path_length
= strlen(new_path
) + 1;
1185 msg_length
= sizeof(*msg
) + old_path_length
+ new_path_length
;
1186 msg
= zmalloc(msg_length
);
1188 PERROR("zmalloc rotate-rename command message");
1193 assert(old_path_length
<= UINT32_MAX
);
1194 msg
->old_path_length
= htobe32(old_path_length
);
1196 assert(new_path_length
<= UINT32_MAX
);
1197 msg
->new_path_length
= htobe32(new_path_length
);
1199 strcpy(msg
->paths
, old_path
);
1200 strcpy(msg
->paths
+ old_path_length
, new_path
);
1203 ret
= send_command(rsock
, RELAYD_ROTATE_RENAME
, (const void *) msg
,
1209 /* Receive response */
1210 ret
= recv_reply(rsock
, (void *) &reply
, sizeof(reply
));
1215 reply
.ret_code
= be32toh(reply
.ret_code
);
1217 /* Return session id or negative ret code. */
1218 if (reply
.ret_code
!= LTTNG_OK
) {
1220 ERR("Relayd rotate rename replied error %d", reply
.ret_code
);
1226 DBG("Relayd rotate rename completed successfully");
1233 int relayd_rotate_pending(struct lttcomm_relayd_sock
*rsock
, uint64_t chunk_id
)
1236 struct lttcomm_relayd_rotate_pending msg
;
1237 struct lttcomm_relayd_rotate_pending_reply reply
;
1239 /* Code flow error. Safety net. */
1242 DBG("Querying relayd for rotate pending with chunk_id %" PRIu64
,
1245 memset(&msg
, 0, sizeof(msg
));
1246 msg
.chunk_id
= htobe64(chunk_id
);
1249 ret
= send_command(rsock
, RELAYD_ROTATE_PENDING
, (void *) &msg
,
1255 /* Receive response */
1256 ret
= recv_reply(rsock
, (void *) &reply
, sizeof(reply
));
1261 reply
.generic
.ret_code
= be32toh(reply
.generic
.ret_code
);
1263 /* Return session id or negative ret code. */
1264 if (reply
.generic
.ret_code
!= LTTNG_OK
) {
1265 ret
= -reply
.generic
.ret_code
;
1266 ERR("Relayd rotate pending replied with error %d", ret
);
1269 /* No error, just rotate pending state */
1270 if (reply
.is_pending
== 0 || reply
.is_pending
== 1) {
1271 ret
= reply
.is_pending
;
1272 DBG("Relayd rotate pending command completed successfully with result \"%s\"",
1273 ret
? "rotation pending" : "rotation NOT pending");
1275 ret
= -LTTNG_ERR_UNK
;
1283 int relayd_mkdir(struct lttcomm_relayd_sock
*rsock
, const char *path
)
1286 struct lttcomm_relayd_mkdir
*msg
;
1287 struct lttcomm_relayd_generic_reply reply
;
1290 /* Code flow error. Safety net. */
1293 DBG("Relayd mkdir path %s", path
);
1295 len
= strlen(path
) + 1;
1296 msg
= zmalloc(sizeof(msg
->length
) + len
);
1298 PERROR("Alloc mkdir msg");
1302 msg
->length
= htobe32((uint32_t) len
);
1304 if (lttng_strncpy(msg
->path
, path
, len
)) {
1310 ret
= send_command(rsock
, RELAYD_MKDIR
, (void *) msg
,
1311 sizeof(msg
->length
) + len
, 0);
1316 /* Receive response */
1317 ret
= recv_reply(rsock
, (void *) &reply
, sizeof(reply
));
1322 reply
.ret_code
= be32toh(reply
.ret_code
);
1324 /* Return session id or negative ret code. */
1325 if (reply
.ret_code
!= LTTNG_OK
) {
1327 ERR("Relayd mkdir replied error %d", reply
.ret_code
);
1333 DBG("Relayd mkdir completed successfully");