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/compat/string.h>
30 #include <common/sessiond-comm/relayd.h>
31 #include <common/index/ctf-index.h>
36 * Send command. Fill up the header and append the data.
38 static int send_command(struct lttcomm_relayd_sock
*rsock
,
39 enum lttcomm_relayd_command cmd
, const void *data
, size_t size
,
43 struct lttcomm_relayd_hdr header
;
45 uint64_t buf_size
= sizeof(header
);
47 if (rsock
->sock
.fd
< 0) {
55 buf
= zmalloc(buf_size
);
57 PERROR("zmalloc relayd send command buf");
62 memset(&header
, 0, sizeof(header
));
63 header
.cmd
= htobe32(cmd
);
64 header
.data_size
= htobe64(size
);
66 /* Zeroed for now since not used. */
67 header
.cmd_version
= 0;
68 header
.circuit_id
= 0;
70 /* Prepare buffer to send. */
71 memcpy(buf
, &header
, sizeof(header
));
73 memcpy(buf
+ sizeof(header
), data
, size
);
76 DBG3("Relayd sending command %d of size %" PRIu64
, (int) cmd
, buf_size
);
77 ret
= rsock
->sock
.ops
->sendmsg(&rsock
->sock
, buf
, buf_size
, flags
);
79 PERROR("Failed to send command %d of size %" PRIu64
,
91 * Receive reply data on socket. This MUST be call after send_command or else
92 * could result in unexpected behavior(s).
94 static int recv_reply(struct lttcomm_relayd_sock
*rsock
, void *data
, size_t size
)
98 if (rsock
->sock
.fd
< 0) {
102 DBG3("Relayd waiting for reply of size %zu", size
);
104 ret
= rsock
->sock
.ops
->recvmsg(&rsock
->sock
, data
, size
, 0);
105 if (ret
<= 0 || ret
!= size
) {
107 /* Orderly shutdown. */
108 DBG("Socket %d has performed an orderly shutdown", rsock
->sock
.fd
);
110 DBG("Receiving reply failed on sock %d for size %zu with ret %d",
111 rsock
->sock
.fd
, size
, ret
);
113 /* Always return -1 here and the caller can use errno. */
123 * Starting from 2.11, RELAYD_CREATE_SESSION payload (session_name & hostname)
124 * have no length restriction on the sender side.
125 * Length for both payloads is stored in the msg struct. A new dynamic size
126 * payload size is introduced.
128 static int relayd_create_session_2_11(struct lttcomm_relayd_sock
*rsock
,
129 char *session_name
, char *hostname
,
130 int session_live_timer
, unsigned int snapshot
)
133 struct lttcomm_relayd_create_session_2_11
*msg
= NULL
;
134 size_t session_name_len
;
138 /* The two names are sent with a '\0' delimiter between them. */
139 session_name_len
= strlen(session_name
) + 1;
140 hostname_len
= strlen(hostname
) + 1;
142 msg_length
= sizeof(*msg
) + session_name_len
+ hostname_len
;
143 msg
= zmalloc(msg_length
);
145 PERROR("zmalloc create_session_2_11 command message");
150 assert(session_name_len
<= UINT32_MAX
);
151 msg
->session_name_len
= htobe32(session_name_len
);
153 assert(hostname_len
<= UINT32_MAX
);
154 msg
->hostname_len
= htobe32(hostname_len
);
156 if (lttng_strncpy(msg
->names
, session_name
, session_name_len
)) {
160 if (lttng_strncpy(msg
->names
+ session_name_len
, hostname
, hostname_len
)) {
165 msg
->live_timer
= htobe32(session_live_timer
);
166 msg
->snapshot
= !!snapshot
;
169 ret
= send_command(rsock
, RELAYD_CREATE_SESSION
, msg
, msg_length
, 0);
178 * From 2.4 to 2.10, RELAYD_CREATE_SESSION takes additional parameters to
179 * support the live reading capability.
181 static int relayd_create_session_2_4(struct lttcomm_relayd_sock
*rsock
,
182 char *session_name
, char *hostname
, int session_live_timer
,
183 unsigned int snapshot
)
186 struct lttcomm_relayd_create_session_2_4 msg
;
188 if (lttng_strncpy(msg
.session_name
, session_name
,
189 sizeof(msg
.session_name
))) {
193 if (lttng_strncpy(msg
.hostname
, hostname
, sizeof(msg
.hostname
))) {
197 msg
.live_timer
= htobe32(session_live_timer
);
198 msg
.snapshot
= htobe32(snapshot
);
201 ret
= send_command(rsock
, RELAYD_CREATE_SESSION
, &msg
, sizeof(msg
), 0);
211 * RELAYD_CREATE_SESSION from 2.1 to 2.3.
213 static int relayd_create_session_2_1(struct lttcomm_relayd_sock
*rsock
)
218 ret
= send_command(rsock
, RELAYD_CREATE_SESSION
, NULL
, 0, 0);
228 * Send a RELAYD_CREATE_SESSION command to the relayd with the given socket and
229 * set session_id of the relayd if we have a successful reply from the relayd.
231 * On success, return 0 else a negative value which is either an errno error or
232 * a lttng error code from the relayd.
234 int relayd_create_session(struct lttcomm_relayd_sock
*rsock
, uint64_t *session_id
,
235 char *session_name
, char *hostname
, int session_live_timer
,
236 unsigned int snapshot
)
239 struct lttcomm_relayd_status_session reply
;
244 DBG("Relayd create session");
246 if (rsock
->minor
< 4) {
247 /* From 2.1 to 2.3 */
248 ret
= relayd_create_session_2_1(rsock
);
249 } else if (rsock
->minor
>= 4 && rsock
->minor
< 11) {
250 /* From 2.4 to 2.10 */
251 ret
= relayd_create_session_2_4(rsock
, session_name
,
252 hostname
, session_live_timer
, snapshot
);
254 /* From 2.11 to ... */
255 ret
= relayd_create_session_2_11(rsock
, session_name
,
256 hostname
, session_live_timer
, snapshot
);
263 /* Receive response */
264 ret
= recv_reply(rsock
, (void *) &reply
, sizeof(reply
));
269 reply
.session_id
= be64toh(reply
.session_id
);
270 reply
.ret_code
= be32toh(reply
.ret_code
);
272 /* Return session id or negative ret code. */
273 if (reply
.ret_code
!= LTTNG_OK
) {
275 ERR("Relayd create session replied error %d", reply
.ret_code
);
279 *session_id
= reply
.session_id
;
282 DBG("Relayd session created with id %" PRIu64
, reply
.session_id
);
288 static int relayd_add_stream_2_1(struct lttcomm_relayd_sock
*rsock
,
289 const char *channel_name
, const char *pathname
)
292 struct lttcomm_relayd_add_stream msg
;
294 memset(&msg
, 0, sizeof(msg
));
295 if (lttng_strncpy(msg
.channel_name
, channel_name
,
296 sizeof(msg
.channel_name
))) {
301 if (lttng_strncpy(msg
.pathname
, pathname
,
302 sizeof(msg
.pathname
))) {
308 ret
= send_command(rsock
, RELAYD_ADD_STREAM
, (void *) &msg
, sizeof(msg
), 0);
318 static int relayd_add_stream_2_2(struct lttcomm_relayd_sock
*rsock
,
319 const char *channel_name
, const char *pathname
,
320 uint64_t tracefile_size
, uint64_t tracefile_count
)
323 struct lttcomm_relayd_add_stream_2_2 msg
;
325 memset(&msg
, 0, sizeof(msg
));
326 /* Compat with relayd 2.2 to 2.10 */
327 if (lttng_strncpy(msg
.channel_name
, channel_name
,
328 sizeof(msg
.channel_name
))) {
332 if (lttng_strncpy(msg
.pathname
, pathname
,
333 sizeof(msg
.pathname
))) {
337 msg
.tracefile_size
= htobe64(tracefile_size
);
338 msg
.tracefile_count
= htobe64(tracefile_count
);
341 ret
= send_command(rsock
, RELAYD_ADD_STREAM
, (void *) &msg
, sizeof(msg
), 0);
350 static int relayd_add_stream_2_11(struct lttcomm_relayd_sock
*rsock
,
351 const char *channel_name
, const char *pathname
,
352 uint64_t tracefile_size
, uint64_t tracefile_count
,
353 uint64_t trace_archive_id
)
356 struct lttcomm_relayd_add_stream_2_11
*msg
= NULL
;
357 size_t channel_name_len
;
361 /* The two names are sent with a '\0' delimiter between them. */
362 channel_name_len
= strlen(channel_name
) + 1;
363 pathname_len
= strlen(pathname
) + 1;
365 msg_length
= sizeof(*msg
) + channel_name_len
+ pathname_len
;
366 msg
= zmalloc(msg_length
);
368 PERROR("zmalloc add_stream_2_11 command message");
373 assert(channel_name_len
<= UINT32_MAX
);
374 msg
->channel_name_len
= htobe32(channel_name_len
);
376 assert(pathname_len
<= UINT32_MAX
);
377 msg
->pathname_len
= htobe32(pathname_len
);
379 if (lttng_strncpy(msg
->names
, channel_name
, channel_name_len
)) {
383 if (lttng_strncpy(msg
->names
+ channel_name_len
, pathname
, pathname_len
)) {
388 msg
->tracefile_size
= htobe64(tracefile_size
);
389 msg
->tracefile_count
= htobe64(tracefile_count
);
390 msg
->trace_archive_id
= htobe64(trace_archive_id
);
393 ret
= send_command(rsock
, RELAYD_ADD_STREAM
, (void *) msg
, msg_length
, 0);
404 * Add stream on the relayd and assign stream handle to the stream_id argument.
406 * On success return 0 else return ret_code negative value.
408 int relayd_add_stream(struct lttcomm_relayd_sock
*rsock
, const char *channel_name
,
409 const char *pathname
, uint64_t *stream_id
,
410 uint64_t tracefile_size
, uint64_t tracefile_count
,
411 uint64_t trace_archive_id
)
414 struct lttcomm_relayd_status_stream reply
;
416 /* Code flow error. Safety net. */
418 assert(channel_name
);
421 DBG("Relayd adding stream for channel name %s", channel_name
);
423 /* Compat with relayd 2.1 */
424 if (rsock
->minor
== 1) {
426 ret
= relayd_add_stream_2_1(rsock
, channel_name
, pathname
);
428 } else if (rsock
->minor
> 1 && rsock
->minor
< 11) {
429 /* From 2.2 to 2.10 */
430 ret
= relayd_add_stream_2_2(rsock
, channel_name
, pathname
,
431 tracefile_size
, tracefile_count
);
433 /* From 2.11 to ...*/
434 ret
= relayd_add_stream_2_11(rsock
, channel_name
, pathname
,
435 tracefile_size
, tracefile_count
,
444 /* Waiting for reply */
445 ret
= recv_reply(rsock
, (void *) &reply
, sizeof(reply
));
450 /* Back to host bytes order. */
451 reply
.handle
= be64toh(reply
.handle
);
452 reply
.ret_code
= be32toh(reply
.ret_code
);
454 /* Return session id or negative ret code. */
455 if (reply
.ret_code
!= LTTNG_OK
) {
457 ERR("Relayd add stream replied error %d", reply
.ret_code
);
461 *stream_id
= reply
.handle
;
464 DBG("Relayd stream added successfully with handle %" PRIu64
,
472 * Inform the relay that all the streams for the current channel has been sent.
474 * On success return 0 else return ret_code negative value.
476 int relayd_streams_sent(struct lttcomm_relayd_sock
*rsock
)
479 struct lttcomm_relayd_generic_reply reply
;
481 /* Code flow error. Safety net. */
484 DBG("Relayd sending streams sent.");
486 /* This feature was introduced in 2.4, ignore it for earlier versions. */
487 if (rsock
->minor
< 4) {
493 ret
= send_command(rsock
, RELAYD_STREAMS_SENT
, NULL
, 0, 0);
498 /* Waiting for reply */
499 ret
= recv_reply(rsock
, (void *) &reply
, sizeof(reply
));
504 /* Back to host bytes order. */
505 reply
.ret_code
= be32toh(reply
.ret_code
);
507 /* Return session id or negative ret code. */
508 if (reply
.ret_code
!= LTTNG_OK
) {
510 ERR("Relayd streams sent replied error %d", reply
.ret_code
);
517 DBG("Relayd streams sent success");
525 * Check version numbers on the relayd.
526 * If major versions are compatible, we assign minor_to_use to the
527 * minor version of the procotol we are going to use for this session.
529 * Return 0 if the two daemons are compatible, LTTNG_ERR_RELAYD_VERSION_FAIL
530 * otherwise, or a negative value on network errors.
532 int relayd_version_check(struct lttcomm_relayd_sock
*rsock
)
535 struct lttcomm_relayd_version msg
;
537 /* Code flow error. Safety net. */
540 DBG("Relayd version check for major.minor %u.%u", rsock
->major
,
543 memset(&msg
, 0, sizeof(msg
));
544 /* Prepare network byte order before transmission. */
545 msg
.major
= htobe32(rsock
->major
);
546 msg
.minor
= htobe32(rsock
->minor
);
549 ret
= send_command(rsock
, RELAYD_VERSION
, (void *) &msg
, sizeof(msg
), 0);
554 /* Receive response */
555 ret
= recv_reply(rsock
, (void *) &msg
, sizeof(msg
));
560 /* Set back to host bytes order */
561 msg
.major
= be32toh(msg
.major
);
562 msg
.minor
= be32toh(msg
.minor
);
565 * Only validate the major version. If the other side is higher,
566 * communication is not possible. Only major version equal can talk to each
567 * other. If the minor version differs, the lowest version is used by both
570 if (msg
.major
!= rsock
->major
) {
572 ret
= LTTNG_ERR_RELAYD_VERSION_FAIL
;
573 DBG2("Relayd version is NOT compatible. Relayd version %u != %u (us)",
574 msg
.major
, rsock
->major
);
579 * If the relayd's minor version is higher, it will adapt to our version so
580 * we can continue to use the latest relayd communication data structure.
581 * If the received minor version is higher, the relayd should adapt to us.
583 if (rsock
->minor
> msg
.minor
) {
584 rsock
->minor
= msg
.minor
;
587 /* Version number compatible */
588 DBG2("Relayd version is compatible, using protocol version %u.%u",
589 rsock
->major
, rsock
->minor
);
597 * Add stream on the relayd and assign stream handle to the stream_id argument.
599 * On success return 0 else return ret_code negative value.
601 int relayd_send_metadata(struct lttcomm_relayd_sock
*rsock
, size_t len
)
605 /* Code flow error. Safety net. */
608 DBG("Relayd sending metadata of size %zu", len
);
611 ret
= send_command(rsock
, RELAYD_SEND_METADATA
, NULL
, len
, 0);
616 DBG2("Relayd metadata added successfully");
619 * After that call, the metadata data MUST be sent to the relayd so the
620 * receive size on the other end matches the len of the metadata packet
621 * header. This is why we don't wait for a reply here.
629 * Connect to relay daemon with an allocated lttcomm_relayd_sock.
631 int relayd_connect(struct lttcomm_relayd_sock
*rsock
)
633 /* Code flow error. Safety net. */
636 if (!rsock
->sock
.ops
) {
638 * Attempting a connect on a non-initialized socket.
643 DBG3("Relayd connect ...");
645 return rsock
->sock
.ops
->connect(&rsock
->sock
);
649 * Close relayd socket with an allocated lttcomm_relayd_sock.
651 * If no socket operations are found, simply return 0 meaning that everything
652 * is fine. Without operations, the socket can not possibly be opened or used.
653 * This is possible if the socket was allocated but not created. However, the
654 * caller could simply use it to store a valid file descriptor for instance
655 * passed over a Unix socket and call this to cleanup but still without a valid
658 * Return the close returned value. On error, a negative value is usually
659 * returned back from close(2).
661 int relayd_close(struct lttcomm_relayd_sock
*rsock
)
665 /* Code flow error. Safety net. */
668 /* An invalid fd is fine, return success. */
669 if (rsock
->sock
.fd
< 0) {
674 DBG3("Relayd closing socket %d", rsock
->sock
.fd
);
676 if (rsock
->sock
.ops
) {
677 ret
= rsock
->sock
.ops
->close(&rsock
->sock
);
679 /* Default call if no specific ops found. */
680 ret
= close(rsock
->sock
.fd
);
682 PERROR("relayd_close default close");
692 * Send data header structure to the relayd.
694 int relayd_send_data_hdr(struct lttcomm_relayd_sock
*rsock
,
695 struct lttcomm_relayd_data_hdr
*hdr
, size_t size
)
699 /* Code flow error. Safety net. */
703 if (rsock
->sock
.fd
< 0) {
707 DBG3("Relayd sending data header of size %zu", size
);
709 /* Again, safety net */
711 size
= sizeof(struct lttcomm_relayd_data_hdr
);
714 /* Only send data header. */
715 ret
= rsock
->sock
.ops
->sendmsg(&rsock
->sock
, hdr
, size
, 0);
722 * The data MUST be sent right after that command for the receive on the
723 * other end to match the size in the header.
731 * Send close stream command to the relayd.
733 int relayd_send_close_stream(struct lttcomm_relayd_sock
*rsock
, uint64_t stream_id
,
734 uint64_t last_net_seq_num
)
737 struct lttcomm_relayd_close_stream msg
;
738 struct lttcomm_relayd_generic_reply reply
;
740 /* Code flow error. Safety net. */
743 DBG("Relayd closing stream id %" PRIu64
, stream_id
);
745 memset(&msg
, 0, sizeof(msg
));
746 msg
.stream_id
= htobe64(stream_id
);
747 msg
.last_net_seq_num
= htobe64(last_net_seq_num
);
750 ret
= send_command(rsock
, RELAYD_CLOSE_STREAM
, (void *) &msg
, sizeof(msg
), 0);
755 /* Receive response */
756 ret
= recv_reply(rsock
, (void *) &reply
, sizeof(reply
));
761 reply
.ret_code
= be32toh(reply
.ret_code
);
763 /* Return session id or negative ret code. */
764 if (reply
.ret_code
!= LTTNG_OK
) {
766 ERR("Relayd close stream replied error %d", reply
.ret_code
);
772 DBG("Relayd close stream id %" PRIu64
" successfully", stream_id
);
779 * Check for data availability for a given stream id.
781 * Return 0 if NOT pending, 1 if so and a negative value on error.
783 int relayd_data_pending(struct lttcomm_relayd_sock
*rsock
, uint64_t stream_id
,
784 uint64_t last_net_seq_num
)
787 struct lttcomm_relayd_data_pending msg
;
788 struct lttcomm_relayd_generic_reply reply
;
790 /* Code flow error. Safety net. */
793 DBG("Relayd data pending for stream id %" PRIu64
, stream_id
);
795 memset(&msg
, 0, sizeof(msg
));
796 msg
.stream_id
= htobe64(stream_id
);
797 msg
.last_net_seq_num
= htobe64(last_net_seq_num
);
800 ret
= send_command(rsock
, RELAYD_DATA_PENDING
, (void *) &msg
,
806 /* Receive response */
807 ret
= recv_reply(rsock
, (void *) &reply
, sizeof(reply
));
812 reply
.ret_code
= be32toh(reply
.ret_code
);
814 /* Return session id or negative ret code. */
815 if (reply
.ret_code
>= LTTNG_OK
) {
816 ERR("Relayd data pending replied error %d", reply
.ret_code
);
819 /* At this point, the ret code is either 1 or 0 */
820 ret
= reply
.ret_code
;
822 DBG("Relayd data is %s pending for stream id %" PRIu64
,
823 ret
== 1 ? "" : "NOT", stream_id
);
830 * Check on the relayd side for a quiescent state on the control socket.
832 int relayd_quiescent_control(struct lttcomm_relayd_sock
*rsock
,
833 uint64_t metadata_stream_id
)
836 struct lttcomm_relayd_quiescent_control msg
;
837 struct lttcomm_relayd_generic_reply reply
;
839 /* Code flow error. Safety net. */
842 DBG("Relayd checking quiescent control state");
844 memset(&msg
, 0, sizeof(msg
));
845 msg
.stream_id
= htobe64(metadata_stream_id
);
848 ret
= send_command(rsock
, RELAYD_QUIESCENT_CONTROL
, &msg
, sizeof(msg
), 0);
853 /* Receive response */
854 ret
= recv_reply(rsock
, (void *) &reply
, sizeof(reply
));
859 reply
.ret_code
= be32toh(reply
.ret_code
);
861 /* Return session id or negative ret code. */
862 if (reply
.ret_code
!= LTTNG_OK
) {
864 ERR("Relayd quiescent control replied error %d", reply
.ret_code
);
868 /* Control socket is quiescent */
876 * Begin a data pending command for a specific session id.
878 int relayd_begin_data_pending(struct lttcomm_relayd_sock
*rsock
, uint64_t id
)
881 struct lttcomm_relayd_begin_data_pending msg
;
882 struct lttcomm_relayd_generic_reply reply
;
884 /* Code flow error. Safety net. */
887 DBG("Relayd begin data pending");
889 memset(&msg
, 0, sizeof(msg
));
890 msg
.session_id
= htobe64(id
);
893 ret
= send_command(rsock
, RELAYD_BEGIN_DATA_PENDING
, &msg
, sizeof(msg
), 0);
898 /* Receive response */
899 ret
= recv_reply(rsock
, (void *) &reply
, sizeof(reply
));
904 reply
.ret_code
= be32toh(reply
.ret_code
);
906 /* Return session id or negative ret code. */
907 if (reply
.ret_code
!= LTTNG_OK
) {
909 ERR("Relayd begin data pending replied error %d", reply
.ret_code
);
920 * End a data pending command for a specific session id.
922 * Return 0 on success and set is_data_inflight to 0 if no data is being
923 * streamed or 1 if it is the case.
925 int relayd_end_data_pending(struct lttcomm_relayd_sock
*rsock
, uint64_t id
,
926 unsigned int *is_data_inflight
)
929 struct lttcomm_relayd_end_data_pending msg
;
930 struct lttcomm_relayd_generic_reply reply
;
932 /* Code flow error. Safety net. */
935 DBG("Relayd end data pending");
937 memset(&msg
, 0, sizeof(msg
));
938 msg
.session_id
= htobe64(id
);
941 ret
= send_command(rsock
, RELAYD_END_DATA_PENDING
, &msg
, sizeof(msg
), 0);
946 /* Receive response */
947 ret
= recv_reply(rsock
, (void *) &reply
, sizeof(reply
));
952 recv_ret
= be32toh(reply
.ret_code
);
958 *is_data_inflight
= recv_ret
;
960 DBG("Relayd end data pending is data inflight: %d", recv_ret
);
969 * Send index to the relayd.
971 int relayd_send_index(struct lttcomm_relayd_sock
*rsock
,
972 struct ctf_packet_index
*index
, uint64_t relay_stream_id
,
973 uint64_t net_seq_num
)
976 struct lttcomm_relayd_index msg
;
977 struct lttcomm_relayd_generic_reply reply
;
979 /* Code flow error. Safety net. */
982 if (rsock
->minor
< 4) {
983 DBG("Not sending indexes before protocol 2.4");
988 DBG("Relayd sending index for stream ID %" PRIu64
, relay_stream_id
);
990 memset(&msg
, 0, sizeof(msg
));
991 msg
.relay_stream_id
= htobe64(relay_stream_id
);
992 msg
.net_seq_num
= htobe64(net_seq_num
);
994 /* The index is already in big endian. */
995 msg
.packet_size
= index
->packet_size
;
996 msg
.content_size
= index
->content_size
;
997 msg
.timestamp_begin
= index
->timestamp_begin
;
998 msg
.timestamp_end
= index
->timestamp_end
;
999 msg
.events_discarded
= index
->events_discarded
;
1000 msg
.stream_id
= index
->stream_id
;
1002 if (rsock
->minor
>= 8) {
1003 msg
.stream_instance_id
= index
->stream_instance_id
;
1004 msg
.packet_seq_num
= index
->packet_seq_num
;
1008 ret
= send_command(rsock
, RELAYD_SEND_INDEX
, &msg
,
1009 lttcomm_relayd_index_len(lttng_to_index_major(rsock
->major
,
1011 lttng_to_index_minor(rsock
->major
, rsock
->minor
)),
1017 /* Receive response */
1018 ret
= recv_reply(rsock
, (void *) &reply
, sizeof(reply
));
1023 reply
.ret_code
= be32toh(reply
.ret_code
);
1025 /* Return session id or negative ret code. */
1026 if (reply
.ret_code
!= LTTNG_OK
) {
1028 ERR("Relayd send index replied error %d", reply
.ret_code
);
1039 * Ask the relay to reset the metadata trace file (regeneration).
1041 int relayd_reset_metadata(struct lttcomm_relayd_sock
*rsock
,
1042 uint64_t stream_id
, uint64_t version
)
1045 struct lttcomm_relayd_reset_metadata msg
;
1046 struct lttcomm_relayd_generic_reply reply
;
1048 /* Code flow error. Safety net. */
1051 /* Should have been prevented by the sessiond. */
1052 if (rsock
->minor
< 8) {
1053 ERR("Metadata regeneration unsupported before 2.8");
1058 DBG("Relayd reset metadata stream id %" PRIu64
, stream_id
);
1060 memset(&msg
, 0, sizeof(msg
));
1061 msg
.stream_id
= htobe64(stream_id
);
1062 msg
.version
= htobe64(version
);
1065 ret
= send_command(rsock
, RELAYD_RESET_METADATA
, (void *) &msg
, sizeof(msg
), 0);
1070 /* Receive response */
1071 ret
= recv_reply(rsock
, (void *) &reply
, sizeof(reply
));
1076 reply
.ret_code
= be32toh(reply
.ret_code
);
1078 /* Return session id or negative ret code. */
1079 if (reply
.ret_code
!= LTTNG_OK
) {
1081 ERR("Relayd reset metadata replied error %d", reply
.ret_code
);
1087 DBG("Relayd reset metadata stream id %" PRIu64
" successfully", stream_id
);
1093 int relayd_rotate_stream(struct lttcomm_relayd_sock
*rsock
, uint64_t stream_id
,
1094 const char *new_pathname
, uint64_t new_chunk_id
,
1098 struct lttcomm_relayd_rotate_stream
*msg
= NULL
;
1099 struct lttcomm_relayd_generic_reply reply
;
1103 /* Code flow error. Safety net. */
1106 DBG("Sending rotate stream id %" PRIu64
" command to relayd", stream_id
);
1108 /* Account for the trailing NULL. */
1109 len
= lttng_strnlen(new_pathname
, LTTNG_PATH_MAX
) + 1;
1110 if (len
> LTTNG_PATH_MAX
) {
1111 ERR("Path used in relayd rotate stream command exceeds the maximal allowed length");
1116 msg_len
= offsetof(struct lttcomm_relayd_rotate_stream
, new_pathname
) + len
;
1117 msg
= zmalloc(msg_len
);
1119 PERROR("Failed to allocate relayd rotate stream command of %d bytes",
1125 if (lttng_strncpy(msg
->new_pathname
, new_pathname
, len
)) {
1127 ERR("Failed to copy relayd rotate stream command's new path name");
1131 msg
->pathname_length
= htobe32(len
);
1132 msg
->stream_id
= htobe64(stream_id
);
1133 msg
->new_chunk_id
= htobe64(new_chunk_id
);
1135 * The seq_num is invalid for metadata streams, but it is ignored on
1138 msg
->rotate_at_seq_num
= htobe64(seq_num
);
1141 ret
= send_command(rsock
, RELAYD_ROTATE_STREAM
, (void *) msg
, msg_len
, 0);
1143 ERR("Send rotate command");
1147 /* Receive response. */
1148 ret
= recv_reply(rsock
, (void *) &reply
, sizeof(reply
));
1150 ERR("Receive rotate reply");
1154 reply
.ret_code
= be32toh(reply
.ret_code
);
1156 /* Return session id or negative ret code. */
1157 if (reply
.ret_code
!= LTTNG_OK
) {
1159 ERR("Relayd rotate stream replied error %d", reply
.ret_code
);
1163 DBG("Relayd rotated stream id %" PRIu64
" successfully", stream_id
);
1171 int relayd_rotate_rename(struct lttcomm_relayd_sock
*rsock
,
1172 const char *old_path
, const char *new_path
)
1175 struct lttcomm_relayd_rotate_rename
*msg
= NULL
;
1176 struct lttcomm_relayd_generic_reply reply
;
1177 size_t old_path_length
, new_path_length
;
1180 /* Code flow error. Safety net. */
1183 DBG("Relayd rename chunk %s to %s", old_path
, new_path
);
1185 /* The two paths are sent with a '\0' delimiter between them. */
1186 old_path_length
= strlen(old_path
) + 1;
1187 new_path_length
= strlen(new_path
) + 1;
1189 msg_length
= sizeof(*msg
) + old_path_length
+ new_path_length
;
1190 msg
= zmalloc(msg_length
);
1192 PERROR("zmalloc rotate-rename command message");
1197 assert(old_path_length
<= UINT32_MAX
);
1198 msg
->old_path_length
= htobe32(old_path_length
);
1200 assert(new_path_length
<= UINT32_MAX
);
1201 msg
->new_path_length
= htobe32(new_path_length
);
1203 strcpy(msg
->paths
, old_path
);
1204 strcpy(msg
->paths
+ old_path_length
, new_path
);
1207 ret
= send_command(rsock
, RELAYD_ROTATE_RENAME
, (const void *) msg
,
1213 /* Receive response */
1214 ret
= recv_reply(rsock
, (void *) &reply
, sizeof(reply
));
1219 reply
.ret_code
= be32toh(reply
.ret_code
);
1221 /* Return session id or negative ret code. */
1222 if (reply
.ret_code
!= LTTNG_OK
) {
1224 ERR("Relayd rotate rename replied error %d", reply
.ret_code
);
1230 DBG("Relayd rotate rename completed successfully");
1237 int relayd_rotate_pending(struct lttcomm_relayd_sock
*rsock
, uint64_t chunk_id
)
1240 struct lttcomm_relayd_rotate_pending msg
;
1241 struct lttcomm_relayd_rotate_pending_reply reply
;
1243 /* Code flow error. Safety net. */
1246 DBG("Querying relayd for rotate pending with chunk_id %" PRIu64
,
1249 memset(&msg
, 0, sizeof(msg
));
1250 msg
.chunk_id
= htobe64(chunk_id
);
1253 ret
= send_command(rsock
, RELAYD_ROTATE_PENDING
, (void *) &msg
,
1259 /* Receive response */
1260 ret
= recv_reply(rsock
, (void *) &reply
, sizeof(reply
));
1265 reply
.generic
.ret_code
= be32toh(reply
.generic
.ret_code
);
1267 /* Return session id or negative ret code. */
1268 if (reply
.generic
.ret_code
!= LTTNG_OK
) {
1269 ret
= -reply
.generic
.ret_code
;
1270 ERR("Relayd rotate pending replied with error %d", ret
);
1273 /* No error, just rotate pending state */
1274 if (reply
.is_pending
== 0 || reply
.is_pending
== 1) {
1275 ret
= reply
.is_pending
;
1276 DBG("Relayd rotate pending command completed successfully with result \"%s\"",
1277 ret
? "rotation pending" : "rotation NOT pending");
1279 ret
= -LTTNG_ERR_UNK
;
1287 int relayd_mkdir(struct lttcomm_relayd_sock
*rsock
, const char *path
)
1290 struct lttcomm_relayd_mkdir
*msg
;
1291 struct lttcomm_relayd_generic_reply reply
;
1294 /* Code flow error. Safety net. */
1297 DBG("Relayd mkdir path %s", path
);
1299 len
= strlen(path
) + 1;
1300 msg
= zmalloc(sizeof(msg
->length
) + len
);
1302 PERROR("Alloc mkdir msg");
1306 msg
->length
= htobe32((uint32_t) len
);
1308 if (lttng_strncpy(msg
->path
, path
, len
)) {
1314 ret
= send_command(rsock
, RELAYD_MKDIR
, (void *) msg
,
1315 sizeof(msg
->length
) + len
, 0);
1320 /* Receive response */
1321 ret
= recv_reply(rsock
, (void *) &reply
, sizeof(reply
));
1326 reply
.ret_code
= be32toh(reply
.ret_code
);
1328 /* Return session id or negative ret code. */
1329 if (reply
.ret_code
!= LTTNG_OK
) {
1331 ERR("Relayd mkdir replied error %d", reply
.ret_code
);
1337 DBG("Relayd mkdir completed successfully");