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
);
288 * Add stream on the relayd and assign stream handle to the stream_id argument.
290 * On success return 0 else return ret_code negative value.
292 int relayd_add_stream(struct lttcomm_relayd_sock
*rsock
, const char *channel_name
,
293 const char *pathname
, uint64_t *stream_id
,
294 uint64_t tracefile_size
, uint64_t tracefile_count
)
297 struct lttcomm_relayd_add_stream msg
;
298 struct lttcomm_relayd_add_stream_2_2 msg_2_2
;
299 struct lttcomm_relayd_status_stream reply
;
301 /* Code flow error. Safety net. */
303 assert(channel_name
);
306 DBG("Relayd adding stream for channel name %s", channel_name
);
308 /* Compat with relayd 2.1 */
309 if (rsock
->minor
== 1) {
310 memset(&msg
, 0, sizeof(msg
));
311 if (lttng_strncpy(msg
.channel_name
, channel_name
,
312 sizeof(msg
.channel_name
))) {
316 if (lttng_strncpy(msg
.pathname
, pathname
,
317 sizeof(msg
.pathname
))) {
323 ret
= send_command(rsock
, RELAYD_ADD_STREAM
, (void *) &msg
, sizeof(msg
), 0);
328 memset(&msg_2_2
, 0, sizeof(msg_2_2
));
329 /* Compat with relayd 2.2+ */
330 if (lttng_strncpy(msg_2_2
.channel_name
, channel_name
,
331 sizeof(msg_2_2
.channel_name
))) {
335 if (lttng_strncpy(msg_2_2
.pathname
, pathname
,
336 sizeof(msg_2_2
.pathname
))) {
340 msg_2_2
.tracefile_size
= htobe64(tracefile_size
);
341 msg_2_2
.tracefile_count
= htobe64(tracefile_count
);
344 ret
= send_command(rsock
, RELAYD_ADD_STREAM
, (void *) &msg_2_2
, sizeof(msg_2_2
), 0);
350 /* Waiting for reply */
351 ret
= recv_reply(rsock
, (void *) &reply
, sizeof(reply
));
356 /* Back to host bytes order. */
357 reply
.handle
= be64toh(reply
.handle
);
358 reply
.ret_code
= be32toh(reply
.ret_code
);
360 /* Return session id or negative ret code. */
361 if (reply
.ret_code
!= LTTNG_OK
) {
363 ERR("Relayd add stream replied error %d", reply
.ret_code
);
367 *stream_id
= reply
.handle
;
370 DBG("Relayd stream added successfully with handle %" PRIu64
,
378 * Inform the relay that all the streams for the current channel has been sent.
380 * On success return 0 else return ret_code negative value.
382 int relayd_streams_sent(struct lttcomm_relayd_sock
*rsock
)
385 struct lttcomm_relayd_generic_reply reply
;
387 /* Code flow error. Safety net. */
390 DBG("Relayd sending streams sent.");
392 /* This feature was introduced in 2.4, ignore it for earlier versions. */
393 if (rsock
->minor
< 4) {
399 ret
= send_command(rsock
, RELAYD_STREAMS_SENT
, NULL
, 0, 0);
404 /* Waiting for reply */
405 ret
= recv_reply(rsock
, (void *) &reply
, sizeof(reply
));
410 /* Back to host bytes order. */
411 reply
.ret_code
= be32toh(reply
.ret_code
);
413 /* Return session id or negative ret code. */
414 if (reply
.ret_code
!= LTTNG_OK
) {
416 ERR("Relayd streams sent replied error %d", reply
.ret_code
);
423 DBG("Relayd streams sent success");
431 * Check version numbers on the relayd.
432 * If major versions are compatible, we assign minor_to_use to the
433 * minor version of the procotol we are going to use for this session.
435 * Return 0 if the two daemons are compatible, LTTNG_ERR_RELAYD_VERSION_FAIL
436 * otherwise, or a negative value on network errors.
438 int relayd_version_check(struct lttcomm_relayd_sock
*rsock
)
441 struct lttcomm_relayd_version msg
;
443 /* Code flow error. Safety net. */
446 DBG("Relayd version check for major.minor %u.%u", rsock
->major
,
449 memset(&msg
, 0, sizeof(msg
));
450 /* Prepare network byte order before transmission. */
451 msg
.major
= htobe32(rsock
->major
);
452 msg
.minor
= htobe32(rsock
->minor
);
455 ret
= send_command(rsock
, RELAYD_VERSION
, (void *) &msg
, sizeof(msg
), 0);
460 /* Receive response */
461 ret
= recv_reply(rsock
, (void *) &msg
, sizeof(msg
));
466 /* Set back to host bytes order */
467 msg
.major
= be32toh(msg
.major
);
468 msg
.minor
= be32toh(msg
.minor
);
471 * Only validate the major version. If the other side is higher,
472 * communication is not possible. Only major version equal can talk to each
473 * other. If the minor version differs, the lowest version is used by both
476 if (msg
.major
!= rsock
->major
) {
478 ret
= LTTNG_ERR_RELAYD_VERSION_FAIL
;
479 DBG2("Relayd version is NOT compatible. Relayd version %u != %u (us)",
480 msg
.major
, rsock
->major
);
485 * If the relayd's minor version is higher, it will adapt to our version so
486 * we can continue to use the latest relayd communication data structure.
487 * If the received minor version is higher, the relayd should adapt to us.
489 if (rsock
->minor
> msg
.minor
) {
490 rsock
->minor
= msg
.minor
;
493 /* Version number compatible */
494 DBG2("Relayd version is compatible, using protocol version %u.%u",
495 rsock
->major
, rsock
->minor
);
503 * Add stream on the relayd and assign stream handle to the stream_id argument.
505 * On success return 0 else return ret_code negative value.
507 int relayd_send_metadata(struct lttcomm_relayd_sock
*rsock
, size_t len
)
511 /* Code flow error. Safety net. */
514 DBG("Relayd sending metadata of size %zu", len
);
517 ret
= send_command(rsock
, RELAYD_SEND_METADATA
, NULL
, len
, 0);
522 DBG2("Relayd metadata added successfully");
525 * After that call, the metadata data MUST be sent to the relayd so the
526 * receive size on the other end matches the len of the metadata packet
527 * header. This is why we don't wait for a reply here.
535 * Connect to relay daemon with an allocated lttcomm_relayd_sock.
537 int relayd_connect(struct lttcomm_relayd_sock
*rsock
)
539 /* Code flow error. Safety net. */
542 if (!rsock
->sock
.ops
) {
544 * Attempting a connect on a non-initialized socket.
549 DBG3("Relayd connect ...");
551 return rsock
->sock
.ops
->connect(&rsock
->sock
);
555 * Close relayd socket with an allocated lttcomm_relayd_sock.
557 * If no socket operations are found, simply return 0 meaning that everything
558 * is fine. Without operations, the socket can not possibly be opened or used.
559 * This is possible if the socket was allocated but not created. However, the
560 * caller could simply use it to store a valid file descriptor for instance
561 * passed over a Unix socket and call this to cleanup but still without a valid
564 * Return the close returned value. On error, a negative value is usually
565 * returned back from close(2).
567 int relayd_close(struct lttcomm_relayd_sock
*rsock
)
571 /* Code flow error. Safety net. */
574 /* An invalid fd is fine, return success. */
575 if (rsock
->sock
.fd
< 0) {
580 DBG3("Relayd closing socket %d", rsock
->sock
.fd
);
582 if (rsock
->sock
.ops
) {
583 ret
= rsock
->sock
.ops
->close(&rsock
->sock
);
585 /* Default call if no specific ops found. */
586 ret
= close(rsock
->sock
.fd
);
588 PERROR("relayd_close default close");
598 * Send data header structure to the relayd.
600 int relayd_send_data_hdr(struct lttcomm_relayd_sock
*rsock
,
601 struct lttcomm_relayd_data_hdr
*hdr
, size_t size
)
605 /* Code flow error. Safety net. */
609 if (rsock
->sock
.fd
< 0) {
613 DBG3("Relayd sending data header of size %zu", size
);
615 /* Again, safety net */
617 size
= sizeof(struct lttcomm_relayd_data_hdr
);
620 /* Only send data header. */
621 ret
= rsock
->sock
.ops
->sendmsg(&rsock
->sock
, hdr
, size
, 0);
628 * The data MUST be sent right after that command for the receive on the
629 * other end to match the size in the header.
637 * Send close stream command to the relayd.
639 int relayd_send_close_stream(struct lttcomm_relayd_sock
*rsock
, uint64_t stream_id
,
640 uint64_t last_net_seq_num
)
643 struct lttcomm_relayd_close_stream msg
;
644 struct lttcomm_relayd_generic_reply reply
;
646 /* Code flow error. Safety net. */
649 DBG("Relayd closing stream id %" PRIu64
, stream_id
);
651 memset(&msg
, 0, sizeof(msg
));
652 msg
.stream_id
= htobe64(stream_id
);
653 msg
.last_net_seq_num
= htobe64(last_net_seq_num
);
656 ret
= send_command(rsock
, RELAYD_CLOSE_STREAM
, (void *) &msg
, sizeof(msg
), 0);
661 /* Receive response */
662 ret
= recv_reply(rsock
, (void *) &reply
, sizeof(reply
));
667 reply
.ret_code
= be32toh(reply
.ret_code
);
669 /* Return session id or negative ret code. */
670 if (reply
.ret_code
!= LTTNG_OK
) {
672 ERR("Relayd close stream replied error %d", reply
.ret_code
);
678 DBG("Relayd close stream id %" PRIu64
" successfully", stream_id
);
685 * Check for data availability for a given stream id.
687 * Return 0 if NOT pending, 1 if so and a negative value on error.
689 int relayd_data_pending(struct lttcomm_relayd_sock
*rsock
, uint64_t stream_id
,
690 uint64_t last_net_seq_num
)
693 struct lttcomm_relayd_data_pending msg
;
694 struct lttcomm_relayd_generic_reply reply
;
696 /* Code flow error. Safety net. */
699 DBG("Relayd data pending for stream id %" PRIu64
, stream_id
);
701 memset(&msg
, 0, sizeof(msg
));
702 msg
.stream_id
= htobe64(stream_id
);
703 msg
.last_net_seq_num
= htobe64(last_net_seq_num
);
706 ret
= send_command(rsock
, RELAYD_DATA_PENDING
, (void *) &msg
,
712 /* Receive response */
713 ret
= recv_reply(rsock
, (void *) &reply
, sizeof(reply
));
718 reply
.ret_code
= be32toh(reply
.ret_code
);
720 /* Return session id or negative ret code. */
721 if (reply
.ret_code
>= LTTNG_OK
) {
722 ERR("Relayd data pending replied error %d", reply
.ret_code
);
725 /* At this point, the ret code is either 1 or 0 */
726 ret
= reply
.ret_code
;
728 DBG("Relayd data is %s pending for stream id %" PRIu64
,
729 ret
== 1 ? "" : "NOT", stream_id
);
736 * Check on the relayd side for a quiescent state on the control socket.
738 int relayd_quiescent_control(struct lttcomm_relayd_sock
*rsock
,
739 uint64_t metadata_stream_id
)
742 struct lttcomm_relayd_quiescent_control msg
;
743 struct lttcomm_relayd_generic_reply reply
;
745 /* Code flow error. Safety net. */
748 DBG("Relayd checking quiescent control state");
750 memset(&msg
, 0, sizeof(msg
));
751 msg
.stream_id
= htobe64(metadata_stream_id
);
754 ret
= send_command(rsock
, RELAYD_QUIESCENT_CONTROL
, &msg
, sizeof(msg
), 0);
759 /* Receive response */
760 ret
= recv_reply(rsock
, (void *) &reply
, sizeof(reply
));
765 reply
.ret_code
= be32toh(reply
.ret_code
);
767 /* Return session id or negative ret code. */
768 if (reply
.ret_code
!= LTTNG_OK
) {
770 ERR("Relayd quiescent control replied error %d", reply
.ret_code
);
774 /* Control socket is quiescent */
782 * Begin a data pending command for a specific session id.
784 int relayd_begin_data_pending(struct lttcomm_relayd_sock
*rsock
, uint64_t id
)
787 struct lttcomm_relayd_begin_data_pending msg
;
788 struct lttcomm_relayd_generic_reply reply
;
790 /* Code flow error. Safety net. */
793 DBG("Relayd begin data pending");
795 memset(&msg
, 0, sizeof(msg
));
796 msg
.session_id
= htobe64(id
);
799 ret
= send_command(rsock
, RELAYD_BEGIN_DATA_PENDING
, &msg
, sizeof(msg
), 0);
804 /* Receive response */
805 ret
= recv_reply(rsock
, (void *) &reply
, sizeof(reply
));
810 reply
.ret_code
= be32toh(reply
.ret_code
);
812 /* Return session id or negative ret code. */
813 if (reply
.ret_code
!= LTTNG_OK
) {
815 ERR("Relayd begin data pending replied error %d", reply
.ret_code
);
826 * End a data pending command for a specific session id.
828 * Return 0 on success and set is_data_inflight to 0 if no data is being
829 * streamed or 1 if it is the case.
831 int relayd_end_data_pending(struct lttcomm_relayd_sock
*rsock
, uint64_t id
,
832 unsigned int *is_data_inflight
)
835 struct lttcomm_relayd_end_data_pending msg
;
836 struct lttcomm_relayd_generic_reply reply
;
838 /* Code flow error. Safety net. */
841 DBG("Relayd end data pending");
843 memset(&msg
, 0, sizeof(msg
));
844 msg
.session_id
= htobe64(id
);
847 ret
= send_command(rsock
, RELAYD_END_DATA_PENDING
, &msg
, sizeof(msg
), 0);
852 /* Receive response */
853 ret
= recv_reply(rsock
, (void *) &reply
, sizeof(reply
));
858 recv_ret
= be32toh(reply
.ret_code
);
864 *is_data_inflight
= recv_ret
;
866 DBG("Relayd end data pending is data inflight: %d", recv_ret
);
875 * Send index to the relayd.
877 int relayd_send_index(struct lttcomm_relayd_sock
*rsock
,
878 struct ctf_packet_index
*index
, uint64_t relay_stream_id
,
879 uint64_t net_seq_num
)
882 struct lttcomm_relayd_index msg
;
883 struct lttcomm_relayd_generic_reply reply
;
885 /* Code flow error. Safety net. */
888 if (rsock
->minor
< 4) {
889 DBG("Not sending indexes before protocol 2.4");
894 DBG("Relayd sending index for stream ID %" PRIu64
, relay_stream_id
);
896 memset(&msg
, 0, sizeof(msg
));
897 msg
.relay_stream_id
= htobe64(relay_stream_id
);
898 msg
.net_seq_num
= htobe64(net_seq_num
);
900 /* The index is already in big endian. */
901 msg
.packet_size
= index
->packet_size
;
902 msg
.content_size
= index
->content_size
;
903 msg
.timestamp_begin
= index
->timestamp_begin
;
904 msg
.timestamp_end
= index
->timestamp_end
;
905 msg
.events_discarded
= index
->events_discarded
;
906 msg
.stream_id
= index
->stream_id
;
908 if (rsock
->minor
>= 8) {
909 msg
.stream_instance_id
= index
->stream_instance_id
;
910 msg
.packet_seq_num
= index
->packet_seq_num
;
914 ret
= send_command(rsock
, RELAYD_SEND_INDEX
, &msg
,
915 lttcomm_relayd_index_len(lttng_to_index_major(rsock
->major
,
917 lttng_to_index_minor(rsock
->major
, rsock
->minor
)),
923 /* Receive response */
924 ret
= recv_reply(rsock
, (void *) &reply
, sizeof(reply
));
929 reply
.ret_code
= be32toh(reply
.ret_code
);
931 /* Return session id or negative ret code. */
932 if (reply
.ret_code
!= LTTNG_OK
) {
934 ERR("Relayd send index replied error %d", reply
.ret_code
);
945 * Ask the relay to reset the metadata trace file (regeneration).
947 int relayd_reset_metadata(struct lttcomm_relayd_sock
*rsock
,
948 uint64_t stream_id
, uint64_t version
)
951 struct lttcomm_relayd_reset_metadata msg
;
952 struct lttcomm_relayd_generic_reply reply
;
954 /* Code flow error. Safety net. */
957 /* Should have been prevented by the sessiond. */
958 if (rsock
->minor
< 8) {
959 ERR("Metadata regeneration unsupported before 2.8");
964 DBG("Relayd reset metadata stream id %" PRIu64
, stream_id
);
966 memset(&msg
, 0, sizeof(msg
));
967 msg
.stream_id
= htobe64(stream_id
);
968 msg
.version
= htobe64(version
);
971 ret
= send_command(rsock
, RELAYD_RESET_METADATA
, (void *) &msg
, sizeof(msg
), 0);
976 /* Receive response */
977 ret
= recv_reply(rsock
, (void *) &reply
, sizeof(reply
));
982 reply
.ret_code
= be32toh(reply
.ret_code
);
984 /* Return session id or negative ret code. */
985 if (reply
.ret_code
!= LTTNG_OK
) {
987 ERR("Relayd reset metadata replied error %d", reply
.ret_code
);
993 DBG("Relayd reset metadata stream id %" PRIu64
" successfully", stream_id
);
999 int relayd_rotate_stream(struct lttcomm_relayd_sock
*rsock
, uint64_t stream_id
,
1000 const char *new_pathname
, uint64_t new_chunk_id
,
1004 struct lttcomm_relayd_rotate_stream
*msg
= NULL
;
1005 struct lttcomm_relayd_generic_reply reply
;
1009 /* Code flow error. Safety net. */
1012 DBG("Sending rotate stream id %" PRIu64
" command to relayd", stream_id
);
1014 /* Account for the trailing NULL. */
1015 len
= strnlen(new_pathname
, LTTNG_PATH_MAX
) + 1;
1016 if (len
> LTTNG_PATH_MAX
) {
1017 ERR("Path used in relayd rotate stream command exceeds the maximal allowed length");
1022 msg_len
= offsetof(struct lttcomm_relayd_rotate_stream
, new_pathname
) + len
;
1023 msg
= zmalloc(msg_len
);
1025 PERROR("Failed to allocate relayd rotate stream command of %d bytes",
1031 if (lttng_strncpy(msg
->new_pathname
, new_pathname
, len
)) {
1033 ERR("Failed to copy relayd rotate stream command's new path name");
1037 msg
->pathname_length
= htobe32(len
);
1038 msg
->stream_id
= htobe64(stream_id
);
1039 msg
->new_chunk_id
= htobe64(new_chunk_id
);
1041 * The seq_num is invalid for metadata streams, but it is ignored on
1044 msg
->rotate_at_seq_num
= htobe64(seq_num
);
1047 ret
= send_command(rsock
, RELAYD_ROTATE_STREAM
, (void *) msg
, msg_len
, 0);
1049 ERR("Send rotate command");
1053 /* Receive response. */
1054 ret
= recv_reply(rsock
, (void *) &reply
, sizeof(reply
));
1056 ERR("Receive rotate reply");
1060 reply
.ret_code
= be32toh(reply
.ret_code
);
1062 /* Return session id or negative ret code. */
1063 if (reply
.ret_code
!= LTTNG_OK
) {
1065 ERR("Relayd rotate stream replied error %d", reply
.ret_code
);
1069 DBG("Relayd rotated stream id %" PRIu64
" successfully", stream_id
);
1077 int relayd_rotate_rename(struct lttcomm_relayd_sock
*rsock
,
1078 const char *old_path
, const char *new_path
)
1081 struct lttcomm_relayd_rotate_rename
*msg
= NULL
;
1082 struct lttcomm_relayd_generic_reply reply
;
1083 size_t old_path_length
, new_path_length
;
1086 /* Code flow error. Safety net. */
1089 DBG("Relayd rename chunk %s to %s", old_path
, new_path
);
1091 /* The two paths are sent with a '\0' delimiter between them. */
1092 old_path_length
= strlen(old_path
) + 1;
1093 new_path_length
= strlen(new_path
) + 1;
1095 msg_length
= sizeof(*msg
) + old_path_length
+ new_path_length
;
1096 msg
= zmalloc(msg_length
);
1098 PERROR("zmalloc rotate-rename command message");
1103 assert(old_path_length
<= UINT32_MAX
);
1104 msg
->old_path_length
= htobe32(old_path_length
);
1106 assert(new_path_length
<= UINT32_MAX
);
1107 msg
->new_path_length
= htobe32(new_path_length
);
1109 strcpy(msg
->paths
, old_path
);
1110 strcpy(msg
->paths
+ old_path_length
, new_path
);
1113 ret
= send_command(rsock
, RELAYD_ROTATE_RENAME
, (const void *) msg
,
1119 /* Receive response */
1120 ret
= recv_reply(rsock
, (void *) &reply
, sizeof(reply
));
1125 reply
.ret_code
= be32toh(reply
.ret_code
);
1127 /* Return session id or negative ret code. */
1128 if (reply
.ret_code
!= LTTNG_OK
) {
1130 ERR("Relayd rotate rename replied error %d", reply
.ret_code
);
1136 DBG("Relayd rotate rename completed successfully");
1143 int relayd_rotate_pending(struct lttcomm_relayd_sock
*rsock
, uint64_t chunk_id
)
1146 struct lttcomm_relayd_rotate_pending msg
;
1147 struct lttcomm_relayd_rotate_pending_reply reply
;
1149 /* Code flow error. Safety net. */
1152 DBG("Querying relayd for rotate pending with chunk_id %" PRIu64
,
1155 memset(&msg
, 0, sizeof(msg
));
1156 msg
.chunk_id
= htobe64(chunk_id
);
1159 ret
= send_command(rsock
, RELAYD_ROTATE_PENDING
, (void *) &msg
,
1165 /* Receive response */
1166 ret
= recv_reply(rsock
, (void *) &reply
, sizeof(reply
));
1171 reply
.generic
.ret_code
= be32toh(reply
.generic
.ret_code
);
1173 /* Return session id or negative ret code. */
1174 if (reply
.generic
.ret_code
!= LTTNG_OK
) {
1175 ret
= -reply
.generic
.ret_code
;
1176 ERR("Relayd rotate pending replied with error %d", ret
);
1179 /* No error, just rotate pending state */
1180 if (reply
.is_pending
== 0 || reply
.is_pending
== 1) {
1181 ret
= reply
.is_pending
;
1182 DBG("Relayd rotate pending command completed successfully with result \"%s\"",
1183 ret
? "rotation pending" : "rotation NOT pending");
1185 ret
= -LTTNG_ERR_UNK
;
1193 int relayd_mkdir(struct lttcomm_relayd_sock
*rsock
, const char *path
)
1196 struct lttcomm_relayd_mkdir
*msg
;
1197 struct lttcomm_relayd_generic_reply reply
;
1200 /* Code flow error. Safety net. */
1203 DBG("Relayd mkdir path %s", path
);
1205 len
= strlen(path
) + 1;
1206 msg
= zmalloc(sizeof(msg
->length
) + len
);
1208 PERROR("Alloc mkdir msg");
1212 msg
->length
= htobe32((uint32_t) len
);
1214 if (lttng_strncpy(msg
->path
, path
, len
)) {
1220 ret
= send_command(rsock
, RELAYD_MKDIR
, (void *) msg
,
1221 sizeof(msg
->length
) + len
, 0);
1226 /* Receive response */
1227 ret
= recv_reply(rsock
, (void *) &reply
, sizeof(reply
));
1232 reply
.ret_code
= be32toh(reply
.ret_code
);
1234 /* Return session id or negative ret code. */
1235 if (reply
.ret_code
!= LTTNG_OK
) {
1237 ERR("Relayd mkdir replied error %d", reply
.ret_code
);
1243 DBG("Relayd mkdir completed successfully");