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
)
355 struct lttcomm_relayd_add_stream_2_11
*msg
= NULL
;
356 size_t channel_name_len
;
360 /* The two names are sent with a '\0' delimiter between them. */
361 channel_name_len
= strlen(channel_name
) + 1;
362 pathname_len
= strlen(pathname
) + 1;
364 msg_length
= sizeof(*msg
) + channel_name_len
+ pathname_len
;
365 msg
= zmalloc(msg_length
);
367 PERROR("zmalloc add_stream_2_11 command message");
372 assert(channel_name_len
<= UINT32_MAX
);
373 msg
->channel_name_len
= htobe32(channel_name_len
);
375 assert(pathname_len
<= UINT32_MAX
);
376 msg
->pathname_len
= htobe32(pathname_len
);
378 if (lttng_strncpy(msg
->names
, channel_name
, channel_name_len
)) {
382 if (lttng_strncpy(msg
->names
+ channel_name_len
, pathname
, pathname_len
)) {
387 msg
->tracefile_size
= htobe64(tracefile_size
);
388 msg
->tracefile_count
= htobe64(tracefile_count
);
391 ret
= send_command(rsock
, RELAYD_ADD_STREAM
, (void *) msg
, msg_length
, 0);
402 * Add stream on the relayd and assign stream handle to the stream_id argument.
404 * On success return 0 else return ret_code negative value.
406 int relayd_add_stream(struct lttcomm_relayd_sock
*rsock
, const char *channel_name
,
407 const char *pathname
, uint64_t *stream_id
,
408 uint64_t tracefile_size
, uint64_t tracefile_count
,
409 uint64_t trace_archive_id
)
412 struct lttcomm_relayd_status_stream reply
;
414 /* Code flow error. Safety net. */
416 assert(channel_name
);
419 DBG("Relayd adding stream for channel name %s", channel_name
);
421 /* Compat with relayd 2.1 */
422 if (rsock
->minor
== 1) {
424 ret
= relayd_add_stream_2_1(rsock
, channel_name
, pathname
);
426 } else if (rsock
->minor
> 1 && rsock
->minor
< 11) {
427 /* From 2.2 to 2.10 */
428 ret
= relayd_add_stream_2_2(rsock
, channel_name
, pathname
,
429 tracefile_size
, tracefile_count
);
431 /* From 2.11 to ...*/
432 ret
= relayd_add_stream_2_11(rsock
, channel_name
, pathname
,
433 tracefile_size
, tracefile_count
);
441 /* Waiting for reply */
442 ret
= recv_reply(rsock
, (void *) &reply
, sizeof(reply
));
447 /* Back to host bytes order. */
448 reply
.handle
= be64toh(reply
.handle
);
449 reply
.ret_code
= be32toh(reply
.ret_code
);
451 /* Return session id or negative ret code. */
452 if (reply
.ret_code
!= LTTNG_OK
) {
454 ERR("Relayd add stream replied error %d", reply
.ret_code
);
458 *stream_id
= reply
.handle
;
461 DBG("Relayd stream added successfully with handle %" PRIu64
,
469 * Inform the relay that all the streams for the current channel has been sent.
471 * On success return 0 else return ret_code negative value.
473 int relayd_streams_sent(struct lttcomm_relayd_sock
*rsock
)
476 struct lttcomm_relayd_generic_reply reply
;
478 /* Code flow error. Safety net. */
481 DBG("Relayd sending streams sent.");
483 /* This feature was introduced in 2.4, ignore it for earlier versions. */
484 if (rsock
->minor
< 4) {
490 ret
= send_command(rsock
, RELAYD_STREAMS_SENT
, NULL
, 0, 0);
495 /* Waiting for reply */
496 ret
= recv_reply(rsock
, (void *) &reply
, sizeof(reply
));
501 /* Back to host bytes order. */
502 reply
.ret_code
= be32toh(reply
.ret_code
);
504 /* Return session id or negative ret code. */
505 if (reply
.ret_code
!= LTTNG_OK
) {
507 ERR("Relayd streams sent replied error %d", reply
.ret_code
);
514 DBG("Relayd streams sent success");
522 * Check version numbers on the relayd.
523 * If major versions are compatible, we assign minor_to_use to the
524 * minor version of the procotol we are going to use for this session.
526 * Return 0 if the two daemons are compatible, LTTNG_ERR_RELAYD_VERSION_FAIL
527 * otherwise, or a negative value on network errors.
529 int relayd_version_check(struct lttcomm_relayd_sock
*rsock
)
532 struct lttcomm_relayd_version msg
;
534 /* Code flow error. Safety net. */
537 DBG("Relayd version check for major.minor %u.%u", rsock
->major
,
540 memset(&msg
, 0, sizeof(msg
));
541 /* Prepare network byte order before transmission. */
542 msg
.major
= htobe32(rsock
->major
);
543 msg
.minor
= htobe32(rsock
->minor
);
546 ret
= send_command(rsock
, RELAYD_VERSION
, (void *) &msg
, sizeof(msg
), 0);
551 /* Receive response */
552 ret
= recv_reply(rsock
, (void *) &msg
, sizeof(msg
));
557 /* Set back to host bytes order */
558 msg
.major
= be32toh(msg
.major
);
559 msg
.minor
= be32toh(msg
.minor
);
562 * Only validate the major version. If the other side is higher,
563 * communication is not possible. Only major version equal can talk to each
564 * other. If the minor version differs, the lowest version is used by both
567 if (msg
.major
!= rsock
->major
) {
569 ret
= LTTNG_ERR_RELAYD_VERSION_FAIL
;
570 DBG2("Relayd version is NOT compatible. Relayd version %u != %u (us)",
571 msg
.major
, rsock
->major
);
576 * If the relayd's minor version is higher, it will adapt to our version so
577 * we can continue to use the latest relayd communication data structure.
578 * If the received minor version is higher, the relayd should adapt to us.
580 if (rsock
->minor
> msg
.minor
) {
581 rsock
->minor
= msg
.minor
;
584 /* Version number compatible */
585 DBG2("Relayd version is compatible, using protocol version %u.%u",
586 rsock
->major
, rsock
->minor
);
594 * Add stream on the relayd and assign stream handle to the stream_id argument.
596 * On success return 0 else return ret_code negative value.
598 int relayd_send_metadata(struct lttcomm_relayd_sock
*rsock
, size_t len
)
602 /* Code flow error. Safety net. */
605 DBG("Relayd sending metadata of size %zu", len
);
608 ret
= send_command(rsock
, RELAYD_SEND_METADATA
, NULL
, len
, 0);
613 DBG2("Relayd metadata added successfully");
616 * After that call, the metadata data MUST be sent to the relayd so the
617 * receive size on the other end matches the len of the metadata packet
618 * header. This is why we don't wait for a reply here.
626 * Connect to relay daemon with an allocated lttcomm_relayd_sock.
628 int relayd_connect(struct lttcomm_relayd_sock
*rsock
)
630 /* Code flow error. Safety net. */
633 if (!rsock
->sock
.ops
) {
635 * Attempting a connect on a non-initialized socket.
640 DBG3("Relayd connect ...");
642 return rsock
->sock
.ops
->connect(&rsock
->sock
);
646 * Close relayd socket with an allocated lttcomm_relayd_sock.
648 * If no socket operations are found, simply return 0 meaning that everything
649 * is fine. Without operations, the socket can not possibly be opened or used.
650 * This is possible if the socket was allocated but not created. However, the
651 * caller could simply use it to store a valid file descriptor for instance
652 * passed over a Unix socket and call this to cleanup but still without a valid
655 * Return the close returned value. On error, a negative value is usually
656 * returned back from close(2).
658 int relayd_close(struct lttcomm_relayd_sock
*rsock
)
662 /* Code flow error. Safety net. */
665 /* An invalid fd is fine, return success. */
666 if (rsock
->sock
.fd
< 0) {
671 DBG3("Relayd closing socket %d", rsock
->sock
.fd
);
673 if (rsock
->sock
.ops
) {
674 ret
= rsock
->sock
.ops
->close(&rsock
->sock
);
676 /* Default call if no specific ops found. */
677 ret
= close(rsock
->sock
.fd
);
679 PERROR("relayd_close default close");
689 * Send data header structure to the relayd.
691 int relayd_send_data_hdr(struct lttcomm_relayd_sock
*rsock
,
692 struct lttcomm_relayd_data_hdr
*hdr
, size_t size
)
696 /* Code flow error. Safety net. */
700 if (rsock
->sock
.fd
< 0) {
704 DBG3("Relayd sending data header of size %zu", size
);
706 /* Again, safety net */
708 size
= sizeof(struct lttcomm_relayd_data_hdr
);
711 /* Only send data header. */
712 ret
= rsock
->sock
.ops
->sendmsg(&rsock
->sock
, hdr
, size
, 0);
719 * The data MUST be sent right after that command for the receive on the
720 * other end to match the size in the header.
728 * Send close stream command to the relayd.
730 int relayd_send_close_stream(struct lttcomm_relayd_sock
*rsock
, uint64_t stream_id
,
731 uint64_t last_net_seq_num
)
734 struct lttcomm_relayd_close_stream msg
;
735 struct lttcomm_relayd_generic_reply reply
;
737 /* Code flow error. Safety net. */
740 DBG("Relayd closing stream id %" PRIu64
, stream_id
);
742 memset(&msg
, 0, sizeof(msg
));
743 msg
.stream_id
= htobe64(stream_id
);
744 msg
.last_net_seq_num
= htobe64(last_net_seq_num
);
747 ret
= send_command(rsock
, RELAYD_CLOSE_STREAM
, (void *) &msg
, sizeof(msg
), 0);
752 /* Receive response */
753 ret
= recv_reply(rsock
, (void *) &reply
, sizeof(reply
));
758 reply
.ret_code
= be32toh(reply
.ret_code
);
760 /* Return session id or negative ret code. */
761 if (reply
.ret_code
!= LTTNG_OK
) {
763 ERR("Relayd close stream replied error %d", reply
.ret_code
);
769 DBG("Relayd close stream id %" PRIu64
" successfully", stream_id
);
776 * Check for data availability for a given stream id.
778 * Return 0 if NOT pending, 1 if so and a negative value on error.
780 int relayd_data_pending(struct lttcomm_relayd_sock
*rsock
, uint64_t stream_id
,
781 uint64_t last_net_seq_num
)
784 struct lttcomm_relayd_data_pending msg
;
785 struct lttcomm_relayd_generic_reply reply
;
787 /* Code flow error. Safety net. */
790 DBG("Relayd data pending for stream id %" PRIu64
, stream_id
);
792 memset(&msg
, 0, sizeof(msg
));
793 msg
.stream_id
= htobe64(stream_id
);
794 msg
.last_net_seq_num
= htobe64(last_net_seq_num
);
797 ret
= send_command(rsock
, RELAYD_DATA_PENDING
, (void *) &msg
,
803 /* Receive response */
804 ret
= recv_reply(rsock
, (void *) &reply
, sizeof(reply
));
809 reply
.ret_code
= be32toh(reply
.ret_code
);
811 /* Return session id or negative ret code. */
812 if (reply
.ret_code
>= LTTNG_OK
) {
813 ERR("Relayd data pending replied error %d", reply
.ret_code
);
816 /* At this point, the ret code is either 1 or 0 */
817 ret
= reply
.ret_code
;
819 DBG("Relayd data is %s pending for stream id %" PRIu64
,
820 ret
== 1 ? "" : "NOT", stream_id
);
827 * Check on the relayd side for a quiescent state on the control socket.
829 int relayd_quiescent_control(struct lttcomm_relayd_sock
*rsock
,
830 uint64_t metadata_stream_id
)
833 struct lttcomm_relayd_quiescent_control msg
;
834 struct lttcomm_relayd_generic_reply reply
;
836 /* Code flow error. Safety net. */
839 DBG("Relayd checking quiescent control state");
841 memset(&msg
, 0, sizeof(msg
));
842 msg
.stream_id
= htobe64(metadata_stream_id
);
845 ret
= send_command(rsock
, RELAYD_QUIESCENT_CONTROL
, &msg
, sizeof(msg
), 0);
850 /* Receive response */
851 ret
= recv_reply(rsock
, (void *) &reply
, sizeof(reply
));
856 reply
.ret_code
= be32toh(reply
.ret_code
);
858 /* Return session id or negative ret code. */
859 if (reply
.ret_code
!= LTTNG_OK
) {
861 ERR("Relayd quiescent control replied error %d", reply
.ret_code
);
865 /* Control socket is quiescent */
873 * Begin a data pending command for a specific session id.
875 int relayd_begin_data_pending(struct lttcomm_relayd_sock
*rsock
, uint64_t id
)
878 struct lttcomm_relayd_begin_data_pending msg
;
879 struct lttcomm_relayd_generic_reply reply
;
881 /* Code flow error. Safety net. */
884 DBG("Relayd begin data pending");
886 memset(&msg
, 0, sizeof(msg
));
887 msg
.session_id
= htobe64(id
);
890 ret
= send_command(rsock
, RELAYD_BEGIN_DATA_PENDING
, &msg
, sizeof(msg
), 0);
895 /* Receive response */
896 ret
= recv_reply(rsock
, (void *) &reply
, sizeof(reply
));
901 reply
.ret_code
= be32toh(reply
.ret_code
);
903 /* Return session id or negative ret code. */
904 if (reply
.ret_code
!= LTTNG_OK
) {
906 ERR("Relayd begin data pending replied error %d", reply
.ret_code
);
917 * End a data pending command for a specific session id.
919 * Return 0 on success and set is_data_inflight to 0 if no data is being
920 * streamed or 1 if it is the case.
922 int relayd_end_data_pending(struct lttcomm_relayd_sock
*rsock
, uint64_t id
,
923 unsigned int *is_data_inflight
)
926 struct lttcomm_relayd_end_data_pending msg
;
927 struct lttcomm_relayd_generic_reply reply
;
929 /* Code flow error. Safety net. */
932 DBG("Relayd end data pending");
934 memset(&msg
, 0, sizeof(msg
));
935 msg
.session_id
= htobe64(id
);
938 ret
= send_command(rsock
, RELAYD_END_DATA_PENDING
, &msg
, sizeof(msg
), 0);
943 /* Receive response */
944 ret
= recv_reply(rsock
, (void *) &reply
, sizeof(reply
));
949 recv_ret
= be32toh(reply
.ret_code
);
955 *is_data_inflight
= recv_ret
;
957 DBG("Relayd end data pending is data inflight: %d", recv_ret
);
966 * Send index to the relayd.
968 int relayd_send_index(struct lttcomm_relayd_sock
*rsock
,
969 struct ctf_packet_index
*index
, uint64_t relay_stream_id
,
970 uint64_t net_seq_num
)
973 struct lttcomm_relayd_index msg
;
974 struct lttcomm_relayd_generic_reply reply
;
976 /* Code flow error. Safety net. */
979 if (rsock
->minor
< 4) {
980 DBG("Not sending indexes before protocol 2.4");
985 DBG("Relayd sending index for stream ID %" PRIu64
, relay_stream_id
);
987 memset(&msg
, 0, sizeof(msg
));
988 msg
.relay_stream_id
= htobe64(relay_stream_id
);
989 msg
.net_seq_num
= htobe64(net_seq_num
);
991 /* The index is already in big endian. */
992 msg
.packet_size
= index
->packet_size
;
993 msg
.content_size
= index
->content_size
;
994 msg
.timestamp_begin
= index
->timestamp_begin
;
995 msg
.timestamp_end
= index
->timestamp_end
;
996 msg
.events_discarded
= index
->events_discarded
;
997 msg
.stream_id
= index
->stream_id
;
999 if (rsock
->minor
>= 8) {
1000 msg
.stream_instance_id
= index
->stream_instance_id
;
1001 msg
.packet_seq_num
= index
->packet_seq_num
;
1005 ret
= send_command(rsock
, RELAYD_SEND_INDEX
, &msg
,
1006 lttcomm_relayd_index_len(lttng_to_index_major(rsock
->major
,
1008 lttng_to_index_minor(rsock
->major
, rsock
->minor
)),
1014 /* Receive response */
1015 ret
= recv_reply(rsock
, (void *) &reply
, sizeof(reply
));
1020 reply
.ret_code
= be32toh(reply
.ret_code
);
1022 /* Return session id or negative ret code. */
1023 if (reply
.ret_code
!= LTTNG_OK
) {
1025 ERR("Relayd send index replied error %d", reply
.ret_code
);
1036 * Ask the relay to reset the metadata trace file (regeneration).
1038 int relayd_reset_metadata(struct lttcomm_relayd_sock
*rsock
,
1039 uint64_t stream_id
, uint64_t version
)
1042 struct lttcomm_relayd_reset_metadata msg
;
1043 struct lttcomm_relayd_generic_reply reply
;
1045 /* Code flow error. Safety net. */
1048 /* Should have been prevented by the sessiond. */
1049 if (rsock
->minor
< 8) {
1050 ERR("Metadata regeneration unsupported before 2.8");
1055 DBG("Relayd reset metadata stream id %" PRIu64
, stream_id
);
1057 memset(&msg
, 0, sizeof(msg
));
1058 msg
.stream_id
= htobe64(stream_id
);
1059 msg
.version
= htobe64(version
);
1062 ret
= send_command(rsock
, RELAYD_RESET_METADATA
, (void *) &msg
, sizeof(msg
), 0);
1067 /* Receive response */
1068 ret
= recv_reply(rsock
, (void *) &reply
, sizeof(reply
));
1073 reply
.ret_code
= be32toh(reply
.ret_code
);
1075 /* Return session id or negative ret code. */
1076 if (reply
.ret_code
!= LTTNG_OK
) {
1078 ERR("Relayd reset metadata replied error %d", reply
.ret_code
);
1084 DBG("Relayd reset metadata stream id %" PRIu64
" successfully", stream_id
);
1090 int relayd_rotate_stream(struct lttcomm_relayd_sock
*rsock
, uint64_t stream_id
,
1091 const char *new_pathname
, uint64_t new_chunk_id
,
1095 struct lttcomm_relayd_rotate_stream
*msg
= NULL
;
1096 struct lttcomm_relayd_generic_reply reply
;
1100 /* Code flow error. Safety net. */
1103 DBG("Sending rotate stream id %" PRIu64
" command to relayd", stream_id
);
1105 /* Account for the trailing NULL. */
1106 len
= lttng_strnlen(new_pathname
, LTTNG_PATH_MAX
) + 1;
1107 if (len
> LTTNG_PATH_MAX
) {
1108 ERR("Path used in relayd rotate stream command exceeds the maximal allowed length");
1113 msg_len
= offsetof(struct lttcomm_relayd_rotate_stream
, new_pathname
) + len
;
1114 msg
= zmalloc(msg_len
);
1116 PERROR("Failed to allocate relayd rotate stream command of %d bytes",
1122 if (lttng_strncpy(msg
->new_pathname
, new_pathname
, len
)) {
1124 ERR("Failed to copy relayd rotate stream command's new path name");
1128 msg
->pathname_length
= htobe32(len
);
1129 msg
->stream_id
= htobe64(stream_id
);
1130 msg
->new_chunk_id
= htobe64(new_chunk_id
);
1132 * The seq_num is invalid for metadata streams, but it is ignored on
1135 msg
->rotate_at_seq_num
= htobe64(seq_num
);
1138 ret
= send_command(rsock
, RELAYD_ROTATE_STREAM
, (void *) msg
, msg_len
, 0);
1140 ERR("Send rotate command");
1144 /* Receive response. */
1145 ret
= recv_reply(rsock
, (void *) &reply
, sizeof(reply
));
1147 ERR("Receive rotate reply");
1151 reply
.ret_code
= be32toh(reply
.ret_code
);
1153 /* Return session id or negative ret code. */
1154 if (reply
.ret_code
!= LTTNG_OK
) {
1156 ERR("Relayd rotate stream replied error %d", reply
.ret_code
);
1160 DBG("Relayd rotated stream id %" PRIu64
" successfully", stream_id
);
1168 int relayd_rotate_rename(struct lttcomm_relayd_sock
*rsock
,
1169 const char *old_path
, const char *new_path
)
1172 struct lttcomm_relayd_rotate_rename
*msg
= NULL
;
1173 struct lttcomm_relayd_generic_reply reply
;
1174 size_t old_path_length
, new_path_length
;
1177 /* Code flow error. Safety net. */
1180 DBG("Relayd rename chunk %s to %s", old_path
, new_path
);
1182 /* The two paths are sent with a '\0' delimiter between them. */
1183 old_path_length
= strlen(old_path
) + 1;
1184 new_path_length
= strlen(new_path
) + 1;
1186 msg_length
= sizeof(*msg
) + old_path_length
+ new_path_length
;
1187 msg
= zmalloc(msg_length
);
1189 PERROR("zmalloc rotate-rename command message");
1194 assert(old_path_length
<= UINT32_MAX
);
1195 msg
->old_path_length
= htobe32(old_path_length
);
1197 assert(new_path_length
<= UINT32_MAX
);
1198 msg
->new_path_length
= htobe32(new_path_length
);
1200 strcpy(msg
->paths
, old_path
);
1201 strcpy(msg
->paths
+ old_path_length
, new_path
);
1204 ret
= send_command(rsock
, RELAYD_ROTATE_RENAME
, (const void *) msg
,
1210 /* Receive response */
1211 ret
= recv_reply(rsock
, (void *) &reply
, sizeof(reply
));
1216 reply
.ret_code
= be32toh(reply
.ret_code
);
1218 /* Return session id or negative ret code. */
1219 if (reply
.ret_code
!= LTTNG_OK
) {
1221 ERR("Relayd rotate rename replied error %d", reply
.ret_code
);
1227 DBG("Relayd rotate rename completed successfully");
1234 int relayd_rotate_pending(struct lttcomm_relayd_sock
*rsock
, uint64_t chunk_id
)
1237 struct lttcomm_relayd_rotate_pending msg
;
1238 struct lttcomm_relayd_rotate_pending_reply reply
;
1240 /* Code flow error. Safety net. */
1243 DBG("Querying relayd for rotate pending with chunk_id %" PRIu64
,
1246 memset(&msg
, 0, sizeof(msg
));
1247 msg
.chunk_id
= htobe64(chunk_id
);
1250 ret
= send_command(rsock
, RELAYD_ROTATE_PENDING
, (void *) &msg
,
1256 /* Receive response */
1257 ret
= recv_reply(rsock
, (void *) &reply
, sizeof(reply
));
1262 reply
.generic
.ret_code
= be32toh(reply
.generic
.ret_code
);
1264 /* Return session id or negative ret code. */
1265 if (reply
.generic
.ret_code
!= LTTNG_OK
) {
1266 ret
= -reply
.generic
.ret_code
;
1267 ERR("Relayd rotate pending replied with error %d", ret
);
1270 /* No error, just rotate pending state */
1271 if (reply
.is_pending
== 0 || reply
.is_pending
== 1) {
1272 ret
= reply
.is_pending
;
1273 DBG("Relayd rotate pending command completed successfully with result \"%s\"",
1274 ret
? "rotation pending" : "rotation NOT pending");
1276 ret
= -LTTNG_ERR_UNK
;
1284 int relayd_mkdir(struct lttcomm_relayd_sock
*rsock
, const char *path
)
1287 struct lttcomm_relayd_mkdir
*msg
;
1288 struct lttcomm_relayd_generic_reply reply
;
1291 /* Code flow error. Safety net. */
1294 DBG("Relayd mkdir path %s", path
);
1296 len
= strlen(path
) + 1;
1297 msg
= zmalloc(sizeof(msg
->length
) + len
);
1299 PERROR("Alloc mkdir msg");
1303 msg
->length
= htobe32((uint32_t) len
);
1305 if (lttng_strncpy(msg
->path
, path
, len
)) {
1311 ret
= send_command(rsock
, RELAYD_MKDIR
, (void *) msg
,
1312 sizeof(msg
->length
) + len
, 0);
1317 /* Receive response */
1318 ret
= recv_reply(rsock
, (void *) &reply
, sizeof(reply
));
1323 reply
.ret_code
= be32toh(reply
.ret_code
);
1325 /* Return session id or negative ret code. */
1326 if (reply
.ret_code
!= LTTNG_OK
) {
1328 ERR("Relayd mkdir replied error %d", reply
.ret_code
);
1334 DBG("Relayd mkdir completed successfully");