2 * Copyright (C) 2012 David Goulet <dgoulet@efficios.com>
3 * Copyright (C) 2018 Jérémie Galarneau <jeremie.galarneau@efficios.com>
5 * SPDX-License-Identifier: GPL-2.0-only
10 #include "consumer-output.hpp"
11 #include "consumer.hpp"
12 #include "health-sessiond.hpp"
13 #include "lttng-sessiond.hpp"
14 #include "ust-app.hpp"
17 #include <common/common.hpp>
18 #include <common/defaults.hpp>
19 #include <common/relayd/relayd.hpp>
20 #include <common/string-utils/format.hpp>
21 #include <common/urcu.hpp>
22 #include <common/uri.hpp>
29 #include <sys/types.h>
33 * Return allocated full pathname of the session using the consumer trace path
34 * and subdir if available.
36 * The caller can safely free(3) the returned value. On error, NULL is
39 char *setup_channel_trace_path(struct consumer_output
*consumer
,
40 const char *session_path
,
41 size_t *consumer_path_offset
)
46 LTTNG_ASSERT(consumer
);
47 LTTNG_ASSERT(session_path
);
52 * Allocate the string ourself to make sure we never exceed
55 pathname
= calloc
<char>(LTTNG_PATH_MAX
);
60 /* Get correct path name destination */
61 if (consumer
->type
== CONSUMER_DST_NET
&& consumer
->relay_major_version
== 2 &&
62 consumer
->relay_minor_version
< 11) {
63 ret
= snprintf(pathname
,
66 consumer
->dst
.net
.base_dir
,
68 consumer
->domain_subdir
,
70 *consumer_path_offset
= 0;
73 pathname
, LTTNG_PATH_MAX
, "%s/%s", consumer
->domain_subdir
, session_path
);
74 *consumer_path_offset
= strlen(consumer
->domain_subdir
) + 1;
76 DBG3("Consumer trace path relative to current trace chunk: \"%s\"", pathname
);
78 PERROR("Failed to format channel path");
80 } else if (ret
>= LTTNG_PATH_MAX
) {
81 ERR("Truncation occurred while formatting channel path");
92 * Send a data payload using a given consumer socket of size len.
94 * The consumer socket lock MUST be acquired before calling this since this
95 * function can change the fd value.
97 * Return 0 on success else a negative value on error.
99 int consumer_socket_send(struct consumer_socket
*socket
, const void *msg
, size_t len
)
104 LTTNG_ASSERT(socket
);
105 LTTNG_ASSERT(socket
->fd_ptr
);
108 /* Consumer socket is invalid. Stopping. */
109 fd
= *socket
->fd_ptr
;
114 size
= lttcomm_send_unix_sock(fd
, msg
, len
);
116 /* The above call will print a PERROR on error. */
117 DBG("Error when sending data to consumer on sock %d", fd
);
119 * At this point, the socket is not usable anymore thus closing it and
120 * setting the file descriptor to -1 so it is not reused.
123 /* This call will PERROR on error. */
124 (void) lttcomm_close_unix_sock(fd
);
125 *socket
->fd_ptr
= -1;
136 * Receive a data payload using a given consumer socket of size len.
138 * The consumer socket lock MUST be acquired before calling this since this
139 * function can change the fd value.
141 * Return 0 on success else a negative value on error.
143 int consumer_socket_recv(struct consumer_socket
*socket
, void *msg
, size_t len
)
148 LTTNG_ASSERT(socket
);
149 LTTNG_ASSERT(socket
->fd_ptr
);
152 /* Consumer socket is invalid. Stopping. */
153 fd
= *socket
->fd_ptr
;
158 size
= lttcomm_recv_unix_sock(fd
, msg
, len
);
160 /* The above call will print a PERROR on error. */
161 DBG("Error when receiving data from the consumer socket %d", fd
);
163 * At this point, the socket is not usable anymore thus closing it and
164 * setting the file descriptor to -1 so it is not reused.
167 /* This call will PERROR on error. */
168 (void) lttcomm_close_unix_sock(fd
);
169 *socket
->fd_ptr
= -1;
180 * Receive a reply command status message from the consumer. Consumer socket
181 * lock MUST be acquired before calling this function.
183 * Return 0 on success, -1 on recv error or a negative lttng error code which
184 * was possibly returned by the consumer.
186 int consumer_recv_status_reply(struct consumer_socket
*sock
)
189 struct lttcomm_consumer_status_msg reply
;
193 ret
= consumer_socket_recv(sock
, &reply
, sizeof(reply
));
198 if (reply
.ret_code
== LTTCOMM_CONSUMERD_SUCCESS
) {
202 ret
= -reply
.ret_code
;
203 DBG("Consumer ret code %d", ret
);
211 * Once the ASK_CHANNEL command is sent to the consumer, the channel
212 * information are sent back. This call receives that data and populates key
215 * On success return 0 and both key and stream_count are set. On error, a
216 * negative value is sent back and both parameters are untouched.
218 int consumer_recv_status_channel(struct consumer_socket
*sock
,
220 unsigned int *stream_count
)
223 struct lttcomm_consumer_status_channel reply
;
226 LTTNG_ASSERT(stream_count
);
229 ret
= consumer_socket_recv(sock
, &reply
, sizeof(reply
));
234 /* An error is possible so don't touch the key and stream_count. */
235 if (reply
.ret_code
!= LTTCOMM_CONSUMERD_SUCCESS
) {
241 *stream_count
= reply
.stream_count
;
249 * Send destroy relayd command to consumer.
251 * On success return positive value. On error, negative value.
253 int consumer_send_destroy_relayd(struct consumer_socket
*sock
, struct consumer_output
*consumer
)
256 struct lttcomm_consumer_msg msg
;
258 LTTNG_ASSERT(consumer
);
261 DBG2("Sending destroy relayd command to consumer sock %d", *sock
->fd_ptr
);
263 memset(&msg
, 0, sizeof(msg
));
264 msg
.cmd_type
= LTTNG_CONSUMER_DESTROY_RELAYD
;
265 msg
.u
.destroy_relayd
.net_seq_idx
= consumer
->net_seq_index
;
267 pthread_mutex_lock(sock
->lock
);
268 ret
= consumer_socket_send(sock
, &msg
, sizeof(msg
));
273 /* Don't check the return value. The caller will do it. */
274 ret
= consumer_recv_status_reply(sock
);
276 DBG2("Consumer send destroy relayd command done");
279 pthread_mutex_unlock(sock
->lock
);
284 * For each consumer socket in the consumer output object, send a destroy
287 void consumer_output_send_destroy_relayd(struct consumer_output
*consumer
)
289 LTTNG_ASSERT(consumer
);
291 /* Destroy any relayd connection */
292 if (consumer
->type
!= CONSUMER_DST_NET
) {
297 lttng::urcu::lfht_iteration_adapter
<consumer_socket
,
298 decltype(consumer_socket::node
),
299 &consumer_socket::node
>(*consumer
->socks
->ht
)) {
300 /* Send destroy relayd command. */
301 const int ret
= consumer_send_destroy_relayd(socket
, consumer
);
304 DBG("Unable to send destroy relayd command to consumer");
305 /* Continue since we MUST delete everything at this point. */
311 * From a consumer_data structure, allocate and add a consumer socket to the
314 * Return 0 on success, else negative value on error
316 int consumer_create_socket(struct consumer_data
*data
, struct consumer_output
*output
)
319 struct consumer_socket
*socket
;
323 const lttng::urcu::read_lock_guard read_lock
;
325 if (output
== nullptr || data
->cmd_sock
< 0) {
327 * Not an error. Possible there is simply not spawned consumer or it's
328 * disabled for the tracing session asking the socket.
333 socket
= consumer_find_socket(data
->cmd_sock
, output
);
334 if (socket
== nullptr) {
335 socket
= consumer_allocate_socket(&data
->cmd_sock
);
336 if (socket
== nullptr) {
341 socket
->registered
= 0;
342 socket
->lock
= &data
->lock
;
343 consumer_add_socket(socket
, output
);
346 socket
->type
= data
->type
;
348 DBG3("Consumer socket created (fd: %d) and added to output", data
->cmd_sock
);
355 * Return the consumer socket from the given consumer output with the right
356 * bitness. On error, returns NULL.
358 * The caller MUST acquire a rcu read side lock and keep it until the socket
359 * object reference is not needed anymore.
361 struct consumer_socket
*consumer_find_socket_by_bitness(int bits
,
362 const struct consumer_output
*consumer
)
365 struct consumer_socket
*socket
= nullptr;
367 ASSERT_RCU_READ_LOCKED();
371 consumer_fd
= uatomic_read(&the_ust_consumerd64_fd
);
374 consumer_fd
= uatomic_read(&the_ust_consumerd32_fd
);
381 socket
= consumer_find_socket(consumer_fd
, consumer
);
383 ERR("Consumer socket fd %d not found in consumer obj %p", consumer_fd
, consumer
);
391 * Find a consumer_socket in a consumer_output hashtable. Read side lock must
392 * be acquired before calling this function and across use of the
393 * returned consumer_socket.
395 struct consumer_socket
*consumer_find_socket(int key
, const struct consumer_output
*consumer
)
397 struct lttng_ht_iter iter
;
398 struct lttng_ht_node_ulong
*node
;
399 struct consumer_socket
*socket
= nullptr;
401 ASSERT_RCU_READ_LOCKED();
403 /* Negative keys are lookup failures */
404 if (key
< 0 || consumer
== nullptr) {
408 lttng_ht_lookup(consumer
->socks
, (void *) ((unsigned long) key
), &iter
);
409 node
= lttng_ht_iter_get_node
<lttng_ht_node_ulong
>(&iter
);
410 if (node
!= nullptr) {
411 socket
= lttng::utils::container_of(node
, &consumer_socket::node
);
418 * Allocate a new consumer_socket and return the pointer.
420 struct consumer_socket
*consumer_allocate_socket(int *fd
)
422 struct consumer_socket
*socket
= nullptr;
426 socket
= zmalloc
<consumer_socket
>();
427 if (socket
== nullptr) {
428 PERROR("zmalloc consumer socket");
433 lttng_ht_node_init_ulong(&socket
->node
, *fd
);
440 * Add consumer socket to consumer output object. Read side lock must be
441 * acquired before calling this function.
443 void consumer_add_socket(struct consumer_socket
*sock
, struct consumer_output
*consumer
)
446 LTTNG_ASSERT(consumer
);
447 ASSERT_RCU_READ_LOCKED();
449 lttng_ht_add_unique_ulong(consumer
->socks
, &sock
->node
);
453 * Delete consumer socket to consumer output object. Read side lock must be
454 * acquired before calling this function.
456 void consumer_del_socket(struct consumer_socket
*sock
, struct consumer_output
*consumer
)
459 struct lttng_ht_iter iter
;
462 LTTNG_ASSERT(consumer
);
463 ASSERT_RCU_READ_LOCKED();
465 iter
.iter
.node
= &sock
->node
.node
;
466 ret
= lttng_ht_del(consumer
->socks
, &iter
);
471 * RCU destroy call function.
473 static void destroy_socket_rcu(struct rcu_head
*head
)
475 struct lttng_ht_node_ulong
*node
=
476 lttng::utils::container_of(head
, <tng_ht_node_ulong::head
);
477 struct consumer_socket
*socket
= lttng::utils::container_of(node
, &consumer_socket::node
);
483 * Destroy and free socket pointer in a call RCU. The call must either:
484 * - have acquired the read side lock before calling this function, or
485 * - guarantee the validity of the `struct consumer_socket` object for the
486 * duration of the call.
488 void consumer_destroy_socket(struct consumer_socket
*sock
)
493 * We DO NOT close the file descriptor here since it is global to the
494 * session daemon and is closed only if the consumer dies or a custom
495 * consumer was registered,
497 if (sock
->registered
) {
498 DBG3("Consumer socket was registered. Closing fd %d", *sock
->fd_ptr
);
499 lttcomm_close_unix_sock(*sock
->fd_ptr
);
502 call_rcu(&sock
->node
.head
, destroy_socket_rcu
);
506 * Allocate and assign data to a consumer_output object.
508 * Return pointer to structure.
510 struct consumer_output
*consumer_create_output(enum consumer_dst_type type
)
512 struct consumer_output
*output
= nullptr;
514 output
= zmalloc
<consumer_output
>();
515 if (output
== nullptr) {
516 PERROR("zmalloc consumer_output");
520 /* By default, consumer output is enabled */
521 output
->enabled
= true;
523 output
->net_seq_index
= (uint64_t) -1ULL;
524 urcu_ref_init(&output
->ref
);
526 output
->socks
= lttng_ht_new(0, LTTNG_HT_TYPE_ULONG
);
533 * Iterate over the consumer output socket hash table and destroy them. The
534 * socket file descriptor are only closed if the consumer output was
535 * registered meaning it's an external consumer.
537 void consumer_destroy_output_sockets(struct consumer_output
*obj
)
544 lttng::urcu::lfht_iteration_adapter
<consumer_socket
,
545 decltype(consumer_socket::node
),
546 &consumer_socket::node
>(*obj
->socks
->ht
)) {
547 consumer_del_socket(socket
, obj
);
548 consumer_destroy_socket(socket
);
553 * Delete the consumer_output object from the list and free the ptr.
555 static void consumer_release_output(struct urcu_ref
*ref
)
557 struct consumer_output
*obj
= lttng::utils::container_of(ref
, &consumer_output::ref
);
559 consumer_destroy_output_sockets(obj
);
562 /* Finally destroy HT */
563 lttng_ht_destroy(obj
->socks
);
570 * Get the consumer_output object.
572 void consumer_output_get(struct consumer_output
*obj
)
574 urcu_ref_get(&obj
->ref
);
578 * Put the consumer_output object.
580 void consumer_output_put(struct consumer_output
*obj
)
585 urcu_ref_put(&obj
->ref
, consumer_release_output
);
589 * Copy consumer output and returned the newly allocated copy.
591 struct consumer_output
*consumer_copy_output(struct consumer_output
*src
)
594 struct consumer_output
*output
;
598 output
= consumer_create_output(src
->type
);
599 if (output
== nullptr) {
602 output
->enabled
= src
->enabled
;
603 output
->net_seq_index
= src
->net_seq_index
;
604 memcpy(output
->domain_subdir
, src
->domain_subdir
, sizeof(output
->domain_subdir
));
605 output
->snapshot
= src
->snapshot
;
606 output
->relay_major_version
= src
->relay_major_version
;
607 output
->relay_minor_version
= src
->relay_minor_version
;
608 output
->relay_allows_clear
= src
->relay_allows_clear
;
609 memcpy(&output
->dst
, &src
->dst
, sizeof(output
->dst
));
610 ret
= consumer_copy_sockets(output
, src
);
618 consumer_output_put(output
);
623 * Copy consumer sockets from src to dst.
625 * Return 0 on success or else a negative value.
627 int consumer_copy_sockets(struct consumer_output
*dst
, struct consumer_output
*src
)
635 lttng::urcu::lfht_iteration_adapter
<consumer_socket
,
636 decltype(consumer_socket::node
),
637 &consumer_socket::node
>(*src
->socks
->ht
)) {
638 /* Ignore socket that are already there. */
639 auto *copy_sock
= consumer_find_socket(*socket
->fd_ptr
, dst
);
644 /* Create new socket object. */
645 copy_sock
= consumer_allocate_socket(socket
->fd_ptr
);
646 if (copy_sock
== nullptr) {
651 copy_sock
->registered
= socket
->registered
;
653 * This is valid because this lock is shared accross all consumer
654 * object being the global lock of the consumer data structure of the
657 copy_sock
->lock
= socket
->lock
;
658 consumer_add_socket(copy_sock
, dst
);
666 * Set network URI to the consumer output.
668 * Return 0 on success. Return 1 if the URI were equal. Else, negative value on
671 int consumer_set_network_uri(const ltt_session::locked_ref
& session
,
672 struct consumer_output
*output
,
673 struct lttng_uri
*uri
)
676 struct lttng_uri
*dst_uri
= nullptr;
678 /* Code flow error safety net. */
679 LTTNG_ASSERT(output
);
682 switch (uri
->stype
) {
683 case LTTNG_STREAM_CONTROL
:
684 dst_uri
= &output
->dst
.net
.control
;
685 output
->dst
.net
.control_isset
= 1;
686 if (uri
->port
== 0) {
687 /* Assign default port. */
688 uri
->port
= DEFAULT_NETWORK_CONTROL_PORT
;
690 if (output
->dst
.net
.data_isset
&& uri
->port
== output
->dst
.net
.data
.port
) {
691 ret
= -LTTNG_ERR_INVALID
;
695 DBG3("Consumer control URI set with port %d", uri
->port
);
697 case LTTNG_STREAM_DATA
:
698 dst_uri
= &output
->dst
.net
.data
;
699 output
->dst
.net
.data_isset
= 1;
700 if (uri
->port
== 0) {
701 /* Assign default port. */
702 uri
->port
= DEFAULT_NETWORK_DATA_PORT
;
704 if (output
->dst
.net
.control_isset
&&
705 uri
->port
== output
->dst
.net
.control
.port
) {
706 ret
= -LTTNG_ERR_INVALID
;
710 DBG3("Consumer data URI set with port %d", uri
->port
);
713 ERR("Set network uri type unknown %d", uri
->stype
);
714 ret
= -LTTNG_ERR_INVALID
;
718 ret
= uri_compare(dst_uri
, uri
);
720 /* Same URI, don't touch it and return success. */
721 DBG3("URI network compare are the same");
725 /* URIs were not equal, replacing it. */
726 memcpy(dst_uri
, uri
, sizeof(struct lttng_uri
));
727 output
->type
= CONSUMER_DST_NET
;
728 if (dst_uri
->stype
!= LTTNG_STREAM_CONTROL
) {
729 /* Only the control uri needs to contain the path. */
734 * If the user has specified a subdir as part of the control
735 * URL, the session's base output directory is:
736 * /RELAYD_OUTPUT_PATH/HOSTNAME/USER_SPECIFIED_DIR
738 * Hence, the "base_dir" from which all stream files and
739 * session rotation chunks are created takes the form
740 * /HOSTNAME/USER_SPECIFIED_DIR
742 * If the user has not specified an output directory as part of
743 * the control URL, the base output directory has the form:
744 * /RELAYD_OUTPUT_PATH/HOSTNAME/SESSION_NAME-CREATION_TIME
746 * Hence, the "base_dir" from which all stream files and
747 * session rotation chunks are created takes the form
748 * /HOSTNAME/SESSION_NAME-CREATION_TIME
750 * Note that automatically generated session names already
751 * contain the session's creation time. In that case, the
752 * creation time is omitted to prevent it from being duplicated
753 * in the final directory hierarchy.
756 if (strstr(uri
->subdir
, "../")) {
757 ERR("Network URI subdirs are not allowed to walk up the path hierarchy");
758 ret
= -LTTNG_ERR_INVALID
;
761 ret
= snprintf(output
->dst
.net
.base_dir
,
762 sizeof(output
->dst
.net
.base_dir
),
767 if (session
->has_auto_generated_name
) {
768 ret
= snprintf(output
->dst
.net
.base_dir
,
769 sizeof(output
->dst
.net
.base_dir
),
774 char session_creation_datetime
[16];
778 timeinfo
= localtime(&session
->creation_time
);
780 ret
= -LTTNG_ERR_FATAL
;
783 strftime_ret
= strftime(session_creation_datetime
,
784 sizeof(session_creation_datetime
),
787 if (strftime_ret
== 0) {
788 ERR("Failed to format session creation timestamp while setting network URI");
789 ret
= -LTTNG_ERR_FATAL
;
792 ret
= snprintf(output
->dst
.net
.base_dir
,
793 sizeof(output
->dst
.net
.base_dir
),
797 session_creation_datetime
);
800 if (ret
>= sizeof(output
->dst
.net
.base_dir
)) {
801 ret
= -LTTNG_ERR_INVALID
;
802 ERR("Truncation occurred while setting network output base directory");
804 } else if (ret
== -1) {
805 ret
= -LTTNG_ERR_INVALID
;
806 PERROR("Error occurred while setting network output base directory");
810 DBG3("Consumer set network uri base_dir path %s", output
->dst
.net
.base_dir
);
821 * Send file descriptor to consumer via sock.
823 * The consumer socket lock must be held by the caller.
825 int consumer_send_fds(struct consumer_socket
*sock
, const int *fds
, size_t nb_fd
)
831 LTTNG_ASSERT(nb_fd
> 0);
832 LTTNG_ASSERT(pthread_mutex_trylock(sock
->lock
) == EBUSY
);
834 ret
= lttcomm_send_fds_unix_sock(*sock
->fd_ptr
, fds
, nb_fd
);
836 /* The above call will print a PERROR on error. */
837 DBG("Error when sending consumer fds on sock %d", *sock
->fd_ptr
);
841 ret
= consumer_recv_status_reply(sock
);
847 * Consumer send communication message structure to consumer.
849 * The consumer socket lock must be held by the caller.
851 int consumer_send_msg(struct consumer_socket
*sock
, const struct lttcomm_consumer_msg
*msg
)
857 LTTNG_ASSERT(pthread_mutex_trylock(sock
->lock
) == EBUSY
);
859 ret
= consumer_socket_send(sock
, msg
, sizeof(struct lttcomm_consumer_msg
));
864 ret
= consumer_recv_status_reply(sock
);
871 * Consumer send channel communication message structure to consumer.
873 * The consumer socket lock must be held by the caller.
875 int consumer_send_channel(struct consumer_socket
*sock
, struct lttcomm_consumer_msg
*msg
)
882 ret
= consumer_send_msg(sock
, msg
);
892 * Populate the given consumer msg structure with the ask_channel command
895 void consumer_init_ask_channel_comm_msg(struct lttcomm_consumer_msg
*msg
,
896 uint64_t subbuf_size
,
899 unsigned int switch_timer_interval
,
900 unsigned int read_timer_interval
,
901 unsigned int live_timer_interval
,
902 bool is_in_live_session
,
903 unsigned int monitor_timer_interval
,
907 const char *pathname
,
911 const lttng_uuid
& uuid
,
913 uint64_t tracefile_size
,
914 uint64_t tracefile_count
,
915 uint64_t session_id_per_pid
,
916 unsigned int monitor
,
917 uint32_t ust_app_uid
,
918 int64_t blocking_timeout
,
919 const char *root_shm_path
,
920 const char *shm_path
,
921 struct lttng_trace_chunk
*trace_chunk
,
922 const struct lttng_credentials
*buffer_credentials
)
926 /* Zeroed structure */
927 memset(msg
, 0, sizeof(struct lttcomm_consumer_msg
));
928 msg
->u
.ask_channel
.buffer_credentials
.uid
= UINT32_MAX
;
929 msg
->u
.ask_channel
.buffer_credentials
.gid
= UINT32_MAX
;
933 enum lttng_trace_chunk_status chunk_status
;
935 chunk_status
= lttng_trace_chunk_get_id(trace_chunk
, &chunk_id
);
936 LTTNG_ASSERT(chunk_status
== LTTNG_TRACE_CHUNK_STATUS_OK
);
937 LTTNG_OPTIONAL_SET(&msg
->u
.ask_channel
.chunk_id
, chunk_id
);
939 msg
->u
.ask_channel
.buffer_credentials
.uid
= lttng_credentials_get_uid(buffer_credentials
);
940 msg
->u
.ask_channel
.buffer_credentials
.gid
= lttng_credentials_get_gid(buffer_credentials
);
942 msg
->cmd_type
= LTTNG_CONSUMER_ASK_CHANNEL_CREATION
;
943 msg
->u
.ask_channel
.subbuf_size
= subbuf_size
;
944 msg
->u
.ask_channel
.num_subbuf
= num_subbuf
;
945 msg
->u
.ask_channel
.overwrite
= overwrite
;
946 msg
->u
.ask_channel
.switch_timer_interval
= switch_timer_interval
;
947 msg
->u
.ask_channel
.read_timer_interval
= read_timer_interval
;
948 msg
->u
.ask_channel
.live_timer_interval
= live_timer_interval
;
949 msg
->u
.ask_channel
.is_live
= is_in_live_session
;
950 msg
->u
.ask_channel
.monitor_timer_interval
= monitor_timer_interval
;
951 msg
->u
.ask_channel
.output
= output
;
952 msg
->u
.ask_channel
.type
= type
;
953 msg
->u
.ask_channel
.session_id
= session_id
;
954 msg
->u
.ask_channel
.session_id_per_pid
= session_id_per_pid
;
955 msg
->u
.ask_channel
.relayd_id
= relayd_id
;
956 msg
->u
.ask_channel
.key
= key
;
957 msg
->u
.ask_channel
.chan_id
= chan_id
;
958 msg
->u
.ask_channel
.tracefile_size
= tracefile_size
;
959 msg
->u
.ask_channel
.tracefile_count
= tracefile_count
;
960 msg
->u
.ask_channel
.monitor
= monitor
;
961 msg
->u
.ask_channel
.ust_app_uid
= ust_app_uid
;
962 msg
->u
.ask_channel
.blocking_timeout
= blocking_timeout
;
964 std::copy(uuid
.begin(), uuid
.end(), msg
->u
.ask_channel
.uuid
);
967 strncpy(msg
->u
.ask_channel
.pathname
, pathname
, sizeof(msg
->u
.ask_channel
.pathname
));
968 msg
->u
.ask_channel
.pathname
[sizeof(msg
->u
.ask_channel
.pathname
) - 1] = '\0';
971 strncpy(msg
->u
.ask_channel
.name
, name
, sizeof(msg
->u
.ask_channel
.name
));
972 msg
->u
.ask_channel
.name
[sizeof(msg
->u
.ask_channel
.name
) - 1] = '\0';
975 strncpy(msg
->u
.ask_channel
.root_shm_path
,
977 sizeof(msg
->u
.ask_channel
.root_shm_path
));
978 msg
->u
.ask_channel
.root_shm_path
[sizeof(msg
->u
.ask_channel
.root_shm_path
) - 1] =
982 strncpy(msg
->u
.ask_channel
.shm_path
, shm_path
, sizeof(msg
->u
.ask_channel
.shm_path
));
983 msg
->u
.ask_channel
.shm_path
[sizeof(msg
->u
.ask_channel
.shm_path
) - 1] = '\0';
988 * Init channel communication message structure.
990 void consumer_init_add_channel_comm_msg(struct lttcomm_consumer_msg
*msg
,
991 uint64_t channel_key
,
993 const char *pathname
,
996 unsigned int nb_init_streams
,
997 enum lttng_event_output output
,
999 uint64_t tracefile_size
,
1000 uint64_t tracefile_count
,
1001 unsigned int monitor
,
1002 unsigned int live_timer_interval
,
1003 bool is_in_live_session
,
1004 unsigned int monitor_timer_interval
,
1005 struct lttng_trace_chunk
*trace_chunk
)
1009 /* Zeroed structure */
1010 memset(msg
, 0, sizeof(struct lttcomm_consumer_msg
));
1014 enum lttng_trace_chunk_status chunk_status
;
1016 chunk_status
= lttng_trace_chunk_get_id(trace_chunk
, &chunk_id
);
1017 LTTNG_ASSERT(chunk_status
== LTTNG_TRACE_CHUNK_STATUS_OK
);
1018 LTTNG_OPTIONAL_SET(&msg
->u
.channel
.chunk_id
, chunk_id
);
1022 msg
->cmd_type
= LTTNG_CONSUMER_ADD_CHANNEL
;
1023 msg
->u
.channel
.channel_key
= channel_key
;
1024 msg
->u
.channel
.session_id
= session_id
;
1025 msg
->u
.channel
.relayd_id
= relayd_id
;
1026 msg
->u
.channel
.nb_init_streams
= nb_init_streams
;
1027 msg
->u
.channel
.output
= output
;
1028 msg
->u
.channel
.type
= type
;
1029 msg
->u
.channel
.tracefile_size
= tracefile_size
;
1030 msg
->u
.channel
.tracefile_count
= tracefile_count
;
1031 msg
->u
.channel
.monitor
= monitor
;
1032 msg
->u
.channel
.live_timer_interval
= live_timer_interval
;
1033 msg
->u
.channel
.is_live
= is_in_live_session
;
1034 msg
->u
.channel
.monitor_timer_interval
= monitor_timer_interval
;
1036 strncpy(msg
->u
.channel
.pathname
, pathname
, sizeof(msg
->u
.channel
.pathname
));
1037 msg
->u
.channel
.pathname
[sizeof(msg
->u
.channel
.pathname
) - 1] = '\0';
1039 strncpy(msg
->u
.channel
.name
, name
, sizeof(msg
->u
.channel
.name
));
1040 msg
->u
.channel
.name
[sizeof(msg
->u
.channel
.name
) - 1] = '\0';
1044 * Init stream communication message structure.
1046 void consumer_init_add_stream_comm_msg(struct lttcomm_consumer_msg
*msg
,
1047 uint64_t channel_key
,
1048 uint64_t stream_key
,
1053 memset(msg
, 0, sizeof(struct lttcomm_consumer_msg
));
1055 msg
->cmd_type
= LTTNG_CONSUMER_ADD_STREAM
;
1056 msg
->u
.stream
.channel_key
= channel_key
;
1057 msg
->u
.stream
.stream_key
= stream_key
;
1058 msg
->u
.stream
.cpu
= cpu
;
1061 void consumer_init_streams_sent_comm_msg(struct lttcomm_consumer_msg
*msg
,
1062 enum lttng_consumer_command cmd
,
1063 uint64_t channel_key
,
1064 uint64_t net_seq_idx
)
1068 memset(msg
, 0, sizeof(struct lttcomm_consumer_msg
));
1070 msg
->cmd_type
= cmd
;
1071 msg
->u
.sent_streams
.channel_key
= channel_key
;
1072 msg
->u
.sent_streams
.net_seq_idx
= net_seq_idx
;
1076 * Send stream communication structure to the consumer.
1078 int consumer_send_stream(struct consumer_socket
*sock
,
1079 struct consumer_output
*dst
,
1080 struct lttcomm_consumer_msg
*msg
,
1091 ret
= consumer_send_msg(sock
, msg
);
1096 ret
= consumer_send_fds(sock
, fds
, nb_fd
);
1106 * Send relayd socket to consumer associated with a session name.
1108 * The consumer socket lock must be held by the caller.
1110 * On success return positive value. On error, negative value.
1112 int consumer_send_relayd_socket(struct consumer_socket
*consumer_sock
,
1113 struct lttcomm_relayd_sock
*rsock
,
1114 struct consumer_output
*consumer
,
1115 enum lttng_stream_type type
,
1116 uint64_t session_id
,
1117 const char *session_name
,
1118 const char *hostname
,
1119 const char *base_path
,
1120 int session_live_timer
,
1121 const uint64_t *current_chunk_id
,
1122 time_t session_creation_time
,
1123 bool session_name_contains_creation_time
)
1127 struct lttcomm_consumer_msg msg
;
1129 /* Code flow error. Safety net. */
1130 LTTNG_ASSERT(rsock
);
1131 LTTNG_ASSERT(consumer
);
1132 LTTNG_ASSERT(consumer_sock
);
1134 memset(&msg
, 0, sizeof(msg
));
1135 /* Bail out if consumer is disabled */
1136 if (!consumer
->enabled
) {
1141 if (type
== LTTNG_STREAM_CONTROL
) {
1142 char output_path
[LTTNG_PATH_MAX
] = {};
1143 uint64_t relayd_session_id
;
1145 ret
= relayd_create_session(rsock
,
1155 session_creation_time
,
1156 session_name_contains_creation_time
,
1159 /* Close the control socket. */
1160 (void) relayd_close(rsock
);
1163 msg
.u
.relayd_sock
.relayd_session_id
= relayd_session_id
;
1164 DBG("Created session on relay, output path reply: %s", output_path
);
1167 msg
.cmd_type
= LTTNG_CONSUMER_ADD_RELAYD_SOCKET
;
1169 * Assign network consumer output index using the temporary consumer since
1170 * this call should only be made from within a set_consumer_uri() function
1171 * call in the session daemon.
1173 msg
.u
.relayd_sock
.net_index
= consumer
->net_seq_index
;
1174 msg
.u
.relayd_sock
.type
= type
;
1175 msg
.u
.relayd_sock
.session_id
= session_id
;
1176 msg
.u
.relayd_sock
.major
= rsock
->major
;
1177 msg
.u
.relayd_sock
.minor
= rsock
->minor
;
1178 msg
.u
.relayd_sock
.relayd_socket_protocol
= rsock
->sock
.proto
;
1180 DBG3("Sending relayd sock info to consumer on %d", *consumer_sock
->fd_ptr
);
1181 ret
= consumer_send_msg(consumer_sock
, &msg
);
1186 DBG3("Sending relayd socket file descriptor to consumer");
1187 fd
= rsock
->sock
.fd
;
1188 ret
= consumer_send_fds(consumer_sock
, &fd
, 1);
1193 DBG2("Consumer relayd socket sent");
1200 consumer_send_pipe(struct consumer_socket
*consumer_sock
, enum lttng_consumer_command cmd
, int pipe
)
1203 struct lttcomm_consumer_msg msg
;
1204 const char *pipe_name
;
1205 const char *command_name
;
1208 case LTTNG_CONSUMER_SET_CHANNEL_MONITOR_PIPE
:
1209 pipe_name
= "channel monitor";
1210 command_name
= "SET_CHANNEL_MONITOR_PIPE";
1213 ERR("Unexpected command received in %s (cmd = %d)", __func__
, (int) cmd
);
1217 /* Code flow error. Safety net. */
1219 memset(&msg
, 0, sizeof(msg
));
1222 pthread_mutex_lock(consumer_sock
->lock
);
1223 DBG3("Sending %s command to consumer", command_name
);
1224 ret
= consumer_send_msg(consumer_sock
, &msg
);
1229 DBG3("Sending %s pipe %d to consumer on socket %d", pipe_name
, pipe
, *consumer_sock
->fd_ptr
);
1230 ret
= consumer_send_fds(consumer_sock
, &pipe
, 1);
1235 DBG2("%s pipe successfully sent", pipe_name
);
1237 pthread_mutex_unlock(consumer_sock
->lock
);
1241 int consumer_send_channel_monitor_pipe(struct consumer_socket
*consumer_sock
, int pipe
)
1243 return consumer_send_pipe(consumer_sock
, LTTNG_CONSUMER_SET_CHANNEL_MONITOR_PIPE
, pipe
);
1247 * Ask the consumer if the data is pending for the specific session id.
1248 * Returns 1 if data is pending, 0 otherwise, or < 0 on error.
1250 int consumer_is_data_pending(uint64_t session_id
, struct consumer_output
*consumer
)
1253 int32_t ret_code
= 0; /* Default is that the data is NOT pending */
1254 struct lttcomm_consumer_msg msg
;
1256 LTTNG_ASSERT(consumer
);
1258 DBG3("Consumer data pending for id %" PRIu64
, session_id
);
1260 memset(&msg
, 0, sizeof(msg
));
1261 msg
.cmd_type
= LTTNG_CONSUMER_DATA_PENDING
;
1262 msg
.u
.data_pending
.session_id
= session_id
;
1265 lttng::urcu::lfht_iteration_adapter
<consumer_socket
,
1266 decltype(consumer_socket::node
),
1267 &consumer_socket::node
>(*consumer
->socks
->ht
)) {
1268 pthread_mutex_lock(socket
->lock
);
1269 ret
= consumer_socket_send(socket
, &msg
, sizeof(msg
));
1271 pthread_mutex_unlock(socket
->lock
);
1276 * No need for a recv reply status because the answer to the command is
1277 * the reply status message.
1279 ret
= consumer_socket_recv(socket
, &ret_code
, sizeof(ret_code
));
1281 pthread_mutex_unlock(socket
->lock
);
1285 pthread_mutex_unlock(socket
->lock
);
1287 if (ret_code
== 1) {
1292 DBG("Consumer data is %s pending for session id %" PRIu64
,
1293 ret_code
== 1 ? "" : "NOT",
1302 * Send a flush command to consumer using the given channel key.
1304 * Return 0 on success else a negative value.
1306 int consumer_flush_channel(struct consumer_socket
*socket
, uint64_t key
)
1309 struct lttcomm_consumer_msg msg
;
1311 LTTNG_ASSERT(socket
);
1313 DBG2("Consumer flush channel key %" PRIu64
, key
);
1315 memset(&msg
, 0, sizeof(msg
));
1316 msg
.cmd_type
= LTTNG_CONSUMER_FLUSH_CHANNEL
;
1317 msg
.u
.flush_channel
.key
= key
;
1319 pthread_mutex_lock(socket
->lock
);
1320 health_code_update();
1322 ret
= consumer_send_msg(socket
, &msg
);
1328 health_code_update();
1329 pthread_mutex_unlock(socket
->lock
);
1334 * Send a clear quiescent command to consumer using the given channel key.
1336 * Return 0 on success else a negative value.
1338 int consumer_clear_quiescent_channel(struct consumer_socket
*socket
, uint64_t key
)
1341 struct lttcomm_consumer_msg msg
;
1343 LTTNG_ASSERT(socket
);
1345 DBG2("Consumer clear quiescent channel key %" PRIu64
, key
);
1347 memset(&msg
, 0, sizeof(msg
));
1348 msg
.cmd_type
= LTTNG_CONSUMER_CLEAR_QUIESCENT_CHANNEL
;
1349 msg
.u
.clear_quiescent_channel
.key
= key
;
1351 pthread_mutex_lock(socket
->lock
);
1352 health_code_update();
1354 ret
= consumer_send_msg(socket
, &msg
);
1360 health_code_update();
1361 pthread_mutex_unlock(socket
->lock
);
1366 * Send a close metadata command to consumer using the given channel key.
1367 * Called with registry lock held.
1369 * Return 0 on success else a negative value.
1371 int consumer_close_metadata(struct consumer_socket
*socket
, uint64_t metadata_key
)
1374 struct lttcomm_consumer_msg msg
;
1376 LTTNG_ASSERT(socket
);
1378 DBG2("Consumer close metadata channel key %" PRIu64
, metadata_key
);
1380 memset(&msg
, 0, sizeof(msg
));
1381 msg
.cmd_type
= LTTNG_CONSUMER_CLOSE_METADATA
;
1382 msg
.u
.close_metadata
.key
= metadata_key
;
1384 pthread_mutex_lock(socket
->lock
);
1385 health_code_update();
1387 ret
= consumer_send_msg(socket
, &msg
);
1393 health_code_update();
1394 pthread_mutex_unlock(socket
->lock
);
1399 * Send a setup metdata command to consumer using the given channel key.
1401 * Return 0 on success else a negative value.
1403 int consumer_setup_metadata(struct consumer_socket
*socket
, uint64_t metadata_key
)
1406 struct lttcomm_consumer_msg msg
;
1408 LTTNG_ASSERT(socket
);
1410 DBG2("Consumer setup metadata channel key %" PRIu64
, metadata_key
);
1412 memset(&msg
, 0, sizeof(msg
));
1413 msg
.cmd_type
= LTTNG_CONSUMER_SETUP_METADATA
;
1414 msg
.u
.setup_metadata
.key
= metadata_key
;
1416 pthread_mutex_lock(socket
->lock
);
1417 health_code_update();
1419 ret
= consumer_send_msg(socket
, &msg
);
1425 health_code_update();
1426 pthread_mutex_unlock(socket
->lock
);
1431 * Send metadata string to consumer.
1432 * RCU read-side lock must be held to guarantee existence of socket.
1434 * Return 0 on success else a negative value.
1436 int consumer_push_metadata(struct consumer_socket
*socket
,
1437 uint64_t metadata_key
,
1440 size_t target_offset
,
1444 struct lttcomm_consumer_msg msg
;
1446 LTTNG_ASSERT(socket
);
1447 ASSERT_RCU_READ_LOCKED();
1449 DBG2("Consumer push metadata to consumer socket %d", *socket
->fd_ptr
);
1451 pthread_mutex_lock(socket
->lock
);
1453 memset(&msg
, 0, sizeof(msg
));
1454 msg
.cmd_type
= LTTNG_CONSUMER_PUSH_METADATA
;
1455 msg
.u
.push_metadata
.key
= metadata_key
;
1456 msg
.u
.push_metadata
.target_offset
= target_offset
;
1457 msg
.u
.push_metadata
.len
= len
;
1458 msg
.u
.push_metadata
.version
= version
;
1460 health_code_update();
1461 ret
= consumer_send_msg(socket
, &msg
);
1462 if (ret
< 0 || len
== 0) {
1466 DBG3("Consumer pushing metadata on sock %d of len %zu", *socket
->fd_ptr
, len
);
1468 ret
= consumer_socket_send(socket
, metadata_str
, len
);
1473 health_code_update();
1474 ret
= consumer_recv_status_reply(socket
);
1480 pthread_mutex_unlock(socket
->lock
);
1481 health_code_update();
1486 * Ask the consumer to snapshot a specific channel using the key.
1488 * Returns LTTNG_OK on success or else an LTTng error code.
1490 enum lttng_error_code
consumer_snapshot_channel(struct consumer_socket
*socket
,
1492 const struct consumer_output
*output
,
1494 const char *channel_path
,
1495 uint64_t nb_packets_per_stream
)
1498 enum lttng_error_code status
= LTTNG_OK
;
1499 struct lttcomm_consumer_msg msg
;
1501 LTTNG_ASSERT(socket
);
1502 LTTNG_ASSERT(output
);
1504 DBG("Consumer snapshot channel key %" PRIu64
, key
);
1506 memset(&msg
, 0, sizeof(msg
));
1507 msg
.cmd_type
= LTTNG_CONSUMER_SNAPSHOT_CHANNEL
;
1508 msg
.u
.snapshot_channel
.key
= key
;
1509 msg
.u
.snapshot_channel
.nb_packets_per_stream
= nb_packets_per_stream
;
1510 msg
.u
.snapshot_channel
.metadata
= metadata
;
1512 if (output
->type
== CONSUMER_DST_NET
) {
1513 msg
.u
.snapshot_channel
.relayd_id
= output
->net_seq_index
;
1514 msg
.u
.snapshot_channel
.use_relayd
= 1;
1516 msg
.u
.snapshot_channel
.relayd_id
= (uint64_t) -1ULL;
1518 ret
= lttng_strncpy(msg
.u
.snapshot_channel
.pathname
,
1520 sizeof(msg
.u
.snapshot_channel
.pathname
));
1522 ERR("Snapshot path exceeds the maximal allowed length of %zu bytes (%zu bytes required) with path \"%s\"",
1523 sizeof(msg
.u
.snapshot_channel
.pathname
),
1524 strlen(channel_path
),
1526 status
= LTTNG_ERR_SNAPSHOT_FAIL
;
1530 health_code_update();
1531 pthread_mutex_lock(socket
->lock
);
1532 ret
= consumer_send_msg(socket
, &msg
);
1533 pthread_mutex_unlock(socket
->lock
);
1536 case LTTCOMM_CONSUMERD_CHAN_NOT_FOUND
:
1537 status
= LTTNG_ERR_CHAN_NOT_FOUND
;
1540 status
= LTTNG_ERR_SNAPSHOT_FAIL
;
1547 health_code_update();
1552 * Ask the consumer the number of discarded events for a channel.
1554 int consumer_get_discarded_events(uint64_t session_id
,
1555 uint64_t channel_key
,
1556 struct consumer_output
*consumer
,
1557 uint64_t *discarded
)
1560 struct lttcomm_consumer_msg msg
;
1562 LTTNG_ASSERT(consumer
);
1564 DBG3("Consumer discarded events id %" PRIu64
, session_id
);
1566 memset(&msg
, 0, sizeof(msg
));
1567 msg
.cmd_type
= LTTNG_CONSUMER_DISCARDED_EVENTS
;
1568 msg
.u
.discarded_events
.session_id
= session_id
;
1569 msg
.u
.discarded_events
.channel_key
= channel_key
;
1573 /* Send command for each consumer. */
1575 lttng::urcu::lfht_iteration_adapter
<consumer_socket
,
1576 decltype(consumer_socket::node
),
1577 &consumer_socket::node
>(*consumer
->socks
->ht
)) {
1578 uint64_t consumer_discarded
= 0;
1580 pthread_mutex_lock(socket
->lock
);
1581 ret
= consumer_socket_send(socket
, &msg
, sizeof(msg
));
1583 pthread_mutex_unlock(socket
->lock
);
1588 * No need for a recv reply status because the answer to the
1589 * command is the reply status message.
1591 ret
= consumer_socket_recv(socket
, &consumer_discarded
, sizeof(consumer_discarded
));
1593 ERR("get discarded events");
1594 pthread_mutex_unlock(socket
->lock
);
1598 pthread_mutex_unlock(socket
->lock
);
1599 *discarded
+= consumer_discarded
;
1603 DBG("Consumer discarded %" PRIu64
" events in session id %" PRIu64
, *discarded
, session_id
);
1610 * Ask the consumer the number of lost packets for a channel.
1612 int consumer_get_lost_packets(uint64_t session_id
,
1613 uint64_t channel_key
,
1614 struct consumer_output
*consumer
,
1618 struct lttcomm_consumer_msg msg
;
1620 LTTNG_ASSERT(consumer
);
1622 DBG3("Consumer lost packets id %" PRIu64
, session_id
);
1624 memset(&msg
, 0, sizeof(msg
));
1625 msg
.cmd_type
= LTTNG_CONSUMER_LOST_PACKETS
;
1626 msg
.u
.lost_packets
.session_id
= session_id
;
1627 msg
.u
.lost_packets
.channel_key
= channel_key
;
1631 /* Send command for each consumer. */
1633 lttng::urcu::lfht_iteration_adapter
<consumer_socket
,
1634 decltype(consumer_socket::node
),
1635 &consumer_socket::node
>(*consumer
->socks
->ht
)) {
1636 uint64_t consumer_lost
= 0;
1637 pthread_mutex_lock(socket
->lock
);
1638 ret
= consumer_socket_send(socket
, &msg
, sizeof(msg
));
1640 pthread_mutex_unlock(socket
->lock
);
1645 * No need for a recv reply status because the answer to the
1646 * command is the reply status message.
1648 ret
= consumer_socket_recv(socket
, &consumer_lost
, sizeof(consumer_lost
));
1650 ERR("get lost packets");
1651 pthread_mutex_unlock(socket
->lock
);
1654 pthread_mutex_unlock(socket
->lock
);
1655 *lost
+= consumer_lost
;
1659 DBG("Consumer lost %" PRIu64
" packets in session id %" PRIu64
, *lost
, session_id
);
1666 * Ask the consumer to rotate a channel.
1668 * The new_chunk_id is the session->rotate_count that has been incremented
1669 * when the rotation started. On the relay, this allows to keep track in which
1670 * chunk each stream is currently writing to (for the rotate_pending operation).
1672 int consumer_rotate_channel(struct consumer_socket
*socket
,
1674 struct consumer_output
*output
,
1675 bool is_metadata_channel
)
1678 struct lttcomm_consumer_msg msg
;
1680 LTTNG_ASSERT(socket
);
1682 DBG("Consumer rotate channel key %" PRIu64
, key
);
1684 pthread_mutex_lock(socket
->lock
);
1685 memset(&msg
, 0, sizeof(msg
));
1686 msg
.cmd_type
= LTTNG_CONSUMER_ROTATE_CHANNEL
;
1687 msg
.u
.rotate_channel
.key
= key
;
1688 msg
.u
.rotate_channel
.metadata
= !!is_metadata_channel
;
1690 if (output
->type
== CONSUMER_DST_NET
) {
1691 msg
.u
.rotate_channel
.relayd_id
= output
->net_seq_index
;
1693 msg
.u
.rotate_channel
.relayd_id
= (uint64_t) -1ULL;
1696 health_code_update();
1697 ret
= consumer_send_msg(socket
, &msg
);
1700 case LTTCOMM_CONSUMERD_CHAN_NOT_FOUND
:
1701 ret
= -LTTNG_ERR_CHAN_NOT_FOUND
;
1704 ret
= -LTTNG_ERR_ROTATION_FAIL_CONSUMER
;
1710 pthread_mutex_unlock(socket
->lock
);
1711 health_code_update();
1715 int consumer_open_channel_packets(struct consumer_socket
*socket
, uint64_t key
)
1718 lttcomm_consumer_msg msg
= {
1719 .cmd_type
= LTTNG_CONSUMER_OPEN_CHANNEL_PACKETS
,
1722 msg
.u
.open_channel_packets
.key
= key
;
1724 LTTNG_ASSERT(socket
);
1726 DBG("Consumer open channel packets: channel key = %" PRIu64
, key
);
1728 health_code_update();
1730 pthread_mutex_lock(socket
->lock
);
1731 ret
= consumer_send_msg(socket
, &msg
);
1732 pthread_mutex_unlock(socket
->lock
);
1738 health_code_update();
1742 int consumer_clear_channel(struct consumer_socket
*socket
, uint64_t key
)
1745 struct lttcomm_consumer_msg msg
;
1747 LTTNG_ASSERT(socket
);
1749 DBG("Consumer clear channel %" PRIu64
, key
);
1751 memset(&msg
, 0, sizeof(msg
));
1752 msg
.cmd_type
= LTTNG_CONSUMER_CLEAR_CHANNEL
;
1753 msg
.u
.clear_channel
.key
= key
;
1755 health_code_update();
1757 pthread_mutex_lock(socket
->lock
);
1758 ret
= consumer_send_msg(socket
, &msg
);
1764 pthread_mutex_unlock(socket
->lock
);
1766 health_code_update();
1770 int consumer_init(struct consumer_socket
*socket
, const lttng_uuid
& sessiond_uuid
)
1773 struct lttcomm_consumer_msg msg
= {
1774 .cmd_type
= LTTNG_CONSUMER_INIT
,
1778 LTTNG_ASSERT(socket
);
1780 DBG("Sending consumer initialization command");
1781 std::copy(sessiond_uuid
.begin(), sessiond_uuid
.end(), msg
.u
.init
.sessiond_uuid
);
1783 health_code_update();
1784 ret
= consumer_send_msg(socket
, &msg
);
1790 health_code_update();
1795 * Ask the consumer to create a new chunk for a given session.
1797 * Called with the consumer socket lock held.
1799 int consumer_create_trace_chunk(struct consumer_socket
*socket
,
1801 uint64_t session_id
,
1802 struct lttng_trace_chunk
*chunk
,
1803 const char *domain_subdir
)
1806 enum lttng_trace_chunk_status chunk_status
;
1807 struct lttng_credentials chunk_credentials
;
1808 const struct lttng_directory_handle
*chunk_directory_handle
= nullptr;
1809 struct lttng_directory_handle
*domain_handle
= nullptr;
1811 const char *chunk_name
;
1812 bool chunk_name_overridden
;
1814 time_t creation_timestamp
;
1815 char creation_timestamp_buffer
[ISO8601_STR_LEN
];
1816 const char *creation_timestamp_str
= "(none)";
1817 const bool chunk_has_local_output
= relayd_id
== -1ULL;
1818 enum lttng_trace_chunk_status tc_status
;
1819 struct lttcomm_consumer_msg msg
= {
1820 .cmd_type
= LTTNG_CONSUMER_CREATE_TRACE_CHUNK
,
1823 msg
.u
.create_trace_chunk
.session_id
= session_id
;
1825 LTTNG_ASSERT(socket
);
1826 LTTNG_ASSERT(chunk
);
1828 if (relayd_id
!= -1ULL) {
1829 LTTNG_OPTIONAL_SET(&msg
.u
.create_trace_chunk
.relayd_id
, relayd_id
);
1832 chunk_status
= lttng_trace_chunk_get_name(chunk
, &chunk_name
, &chunk_name_overridden
);
1833 if (chunk_status
!= LTTNG_TRACE_CHUNK_STATUS_OK
&&
1834 chunk_status
!= LTTNG_TRACE_CHUNK_STATUS_NONE
) {
1835 ERR("Failed to get name of trace chunk");
1836 ret
= -LTTNG_ERR_FATAL
;
1839 if (chunk_name_overridden
) {
1840 ret
= lttng_strncpy(msg
.u
.create_trace_chunk
.override_name
,
1842 sizeof(msg
.u
.create_trace_chunk
.override_name
));
1844 ERR("Trace chunk name \"%s\" exceeds the maximal length allowed by the consumer protocol",
1846 ret
= -LTTNG_ERR_FATAL
;
1851 chunk_status
= lttng_trace_chunk_get_creation_timestamp(chunk
, &creation_timestamp
);
1852 if (chunk_status
!= LTTNG_TRACE_CHUNK_STATUS_OK
) {
1853 ret
= -LTTNG_ERR_FATAL
;
1856 msg
.u
.create_trace_chunk
.creation_timestamp
= (uint64_t) creation_timestamp
;
1857 /* Only used for logging purposes. */
1858 ret
= time_to_iso8601_str(
1859 creation_timestamp
, creation_timestamp_buffer
, sizeof(creation_timestamp_buffer
));
1860 creation_timestamp_str
= !ret
? creation_timestamp_buffer
: "(formatting error)";
1862 chunk_status
= lttng_trace_chunk_get_id(chunk
, &chunk_id
);
1863 if (chunk_status
!= LTTNG_TRACE_CHUNK_STATUS_OK
) {
1865 * Anonymous trace chunks should never be transmitted
1866 * to remote peers (consumerd and relayd). They are used
1867 * internally for backward-compatibility purposes.
1869 ret
= -LTTNG_ERR_FATAL
;
1872 msg
.u
.create_trace_chunk
.chunk_id
= chunk_id
;
1874 if (chunk_has_local_output
) {
1875 chunk_status
= lttng_trace_chunk_borrow_chunk_directory_handle(
1876 chunk
, &chunk_directory_handle
);
1877 if (chunk_status
!= LTTNG_TRACE_CHUNK_STATUS_OK
) {
1878 ret
= -LTTNG_ERR_FATAL
;
1881 chunk_status
= lttng_trace_chunk_get_credentials(chunk
, &chunk_credentials
);
1882 if (chunk_status
!= LTTNG_TRACE_CHUNK_STATUS_OK
) {
1884 * Not associating credentials to a sessiond chunk is a
1885 * fatal internal error.
1887 ret
= -LTTNG_ERR_FATAL
;
1890 tc_status
= lttng_trace_chunk_create_subdirectory(chunk
, domain_subdir
);
1891 if (tc_status
!= LTTNG_TRACE_CHUNK_STATUS_OK
) {
1892 PERROR("Failed to create chunk domain output directory \"%s\"",
1894 ret
= -LTTNG_ERR_FATAL
;
1897 domain_handle
= lttng_directory_handle_create_from_handle(domain_subdir
,
1898 chunk_directory_handle
);
1899 if (!domain_handle
) {
1900 ret
= -LTTNG_ERR_FATAL
;
1905 * This will only compile on platforms that support
1906 * dirfd (POSIX.2008). This is fine as the session daemon
1907 * is only built for such platforms.
1909 * The ownership of the chunk directory handle's is maintained
1910 * by the trace chunk.
1912 domain_dirfd
= lttng_directory_handle_get_dirfd(domain_handle
);
1913 LTTNG_ASSERT(domain_dirfd
>= 0);
1915 msg
.u
.create_trace_chunk
.credentials
.value
.uid
=
1916 lttng_credentials_get_uid(&chunk_credentials
);
1917 msg
.u
.create_trace_chunk
.credentials
.value
.gid
=
1918 lttng_credentials_get_gid(&chunk_credentials
);
1919 msg
.u
.create_trace_chunk
.credentials
.is_set
= 1;
1922 DBG("Sending consumer create trace chunk command: relayd_id = %" PRId64
1923 ", session_id = %" PRIu64
", chunk_id = %" PRIu64
", creation_timestamp = %s",
1927 creation_timestamp_str
);
1928 health_code_update();
1929 ret
= consumer_send_msg(socket
, &msg
);
1930 health_code_update();
1932 ERR("Trace chunk creation error on consumer");
1933 ret
= -LTTNG_ERR_CREATE_TRACE_CHUNK_FAIL_CONSUMER
;
1937 if (chunk_has_local_output
) {
1938 DBG("Sending trace chunk domain directory fd to consumer");
1939 health_code_update();
1940 ret
= consumer_send_fds(socket
, &domain_dirfd
, 1);
1941 health_code_update();
1943 ERR("Trace chunk creation error on consumer");
1944 ret
= -LTTNG_ERR_CREATE_TRACE_CHUNK_FAIL_CONSUMER
;
1949 lttng_directory_handle_put(domain_handle
);
1954 * Ask the consumer to close a trace chunk for a given session.
1956 * Called with the consumer socket lock held.
1958 int consumer_close_trace_chunk(struct consumer_socket
*socket
,
1960 uint64_t session_id
,
1961 struct lttng_trace_chunk
*chunk
,
1962 char *closed_trace_chunk_path
)
1965 enum lttng_trace_chunk_status chunk_status
;
1966 lttcomm_consumer_msg msg
= {
1967 .cmd_type
= LTTNG_CONSUMER_CLOSE_TRACE_CHUNK
,
1970 msg
.u
.close_trace_chunk
.session_id
= session_id
;
1972 struct lttcomm_consumer_close_trace_chunk_reply reply
;
1974 time_t close_timestamp
;
1975 enum lttng_trace_chunk_command_type close_command
;
1976 const char *close_command_name
= "none";
1977 struct lttng_dynamic_buffer path_reception_buffer
;
1979 LTTNG_ASSERT(socket
);
1980 lttng_dynamic_buffer_init(&path_reception_buffer
);
1982 if (relayd_id
!= -1ULL) {
1983 LTTNG_OPTIONAL_SET(&msg
.u
.close_trace_chunk
.relayd_id
, relayd_id
);
1986 chunk_status
= lttng_trace_chunk_get_close_command(chunk
, &close_command
);
1987 switch (chunk_status
) {
1988 case LTTNG_TRACE_CHUNK_STATUS_OK
:
1989 LTTNG_OPTIONAL_SET(&msg
.u
.close_trace_chunk
.close_command
,
1990 (uint32_t) close_command
);
1992 case LTTNG_TRACE_CHUNK_STATUS_NONE
:
1995 ERR("Failed to get trace chunk close command");
2000 chunk_status
= lttng_trace_chunk_get_id(chunk
, &chunk_id
);
2002 * Anonymous trace chunks should never be transmitted to remote peers
2003 * (consumerd and relayd). They are used internally for
2004 * backward-compatibility purposes.
2006 LTTNG_ASSERT(chunk_status
== LTTNG_TRACE_CHUNK_STATUS_OK
);
2007 msg
.u
.close_trace_chunk
.chunk_id
= chunk_id
;
2009 chunk_status
= lttng_trace_chunk_get_close_timestamp(chunk
, &close_timestamp
);
2011 * A trace chunk should be closed locally before being closed remotely.
2012 * Otherwise, the close timestamp would never be transmitted to the
2015 LTTNG_ASSERT(chunk_status
== LTTNG_TRACE_CHUNK_STATUS_OK
);
2016 msg
.u
.close_trace_chunk
.close_timestamp
= (uint64_t) close_timestamp
;
2018 if (msg
.u
.close_trace_chunk
.close_command
.is_set
) {
2019 close_command_name
= lttng_trace_chunk_command_type_get_name(close_command
);
2021 DBG("Sending consumer close trace chunk command: relayd_id = %" PRId64
2022 ", session_id = %" PRIu64
", chunk_id = %" PRIu64
", close command = \"%s\"",
2026 close_command_name
);
2028 health_code_update();
2029 ret
= consumer_socket_send(socket
, &msg
, sizeof(struct lttcomm_consumer_msg
));
2031 ret
= -LTTNG_ERR_CLOSE_TRACE_CHUNK_FAIL_CONSUMER
;
2034 ret
= consumer_socket_recv(socket
, &reply
, sizeof(reply
));
2036 ret
= -LTTNG_ERR_CLOSE_TRACE_CHUNK_FAIL_CONSUMER
;
2039 if (reply
.path_length
>= LTTNG_PATH_MAX
) {
2040 ERR("Invalid path returned by relay daemon: %" PRIu32
2041 "bytes exceeds maximal allowed length of %d bytes",
2044 ret
= -LTTNG_ERR_INVALID_PROTOCOL
;
2047 ret
= lttng_dynamic_buffer_set_size(&path_reception_buffer
, reply
.path_length
);
2049 ERR("Failed to allocate reception buffer of path returned by the \"close trace chunk\" command");
2050 ret
= -LTTNG_ERR_NOMEM
;
2053 ret
= consumer_socket_recv(socket
, path_reception_buffer
.data
, path_reception_buffer
.size
);
2055 ERR("Communication error while receiving path of closed trace chunk");
2056 ret
= -LTTNG_ERR_CLOSE_TRACE_CHUNK_FAIL_CONSUMER
;
2059 if (path_reception_buffer
.data
[path_reception_buffer
.size
- 1] != '\0') {
2060 ERR("Invalid path returned by relay daemon: not null-terminated");
2061 ret
= -LTTNG_ERR_INVALID_PROTOCOL
;
2064 if (closed_trace_chunk_path
) {
2066 * closed_trace_chunk_path is assumed to have a length >=
2069 memcpy(closed_trace_chunk_path
,
2070 path_reception_buffer
.data
,
2071 path_reception_buffer
.size
);
2074 lttng_dynamic_buffer_reset(&path_reception_buffer
);
2075 health_code_update();
2080 * Ask the consumer if a trace chunk exists.
2082 * Called with the consumer socket lock held.
2083 * Returns 0 on success, or a negative value on error.
2085 int consumer_trace_chunk_exists(struct consumer_socket
*socket
,
2087 uint64_t session_id
,
2088 struct lttng_trace_chunk
*chunk
,
2089 enum consumer_trace_chunk_exists_status
*result
)
2092 enum lttng_trace_chunk_status chunk_status
;
2093 lttcomm_consumer_msg msg
= {
2094 .cmd_type
= LTTNG_CONSUMER_TRACE_CHUNK_EXISTS
,
2097 msg
.u
.trace_chunk_exists
.session_id
= session_id
;
2100 const char *consumer_reply_str
;
2102 LTTNG_ASSERT(socket
);
2104 if (relayd_id
!= -1ULL) {
2105 LTTNG_OPTIONAL_SET(&msg
.u
.trace_chunk_exists
.relayd_id
, relayd_id
);
2108 chunk_status
= lttng_trace_chunk_get_id(chunk
, &chunk_id
);
2109 if (chunk_status
!= LTTNG_TRACE_CHUNK_STATUS_OK
) {
2111 * Anonymous trace chunks should never be transmitted
2112 * to remote peers (consumerd and relayd). They are used
2113 * internally for backward-compatibility purposes.
2115 ret
= -LTTNG_ERR_FATAL
;
2118 msg
.u
.trace_chunk_exists
.chunk_id
= chunk_id
;
2120 DBG("Sending consumer trace chunk exists command: relayd_id = %" PRId64
2121 ", session_id = %" PRIu64
", chunk_id = %" PRIu64
,
2126 health_code_update();
2127 ret
= consumer_send_msg(socket
, &msg
);
2129 case LTTCOMM_CONSUMERD_UNKNOWN_TRACE_CHUNK
:
2130 consumer_reply_str
= "unknown trace chunk";
2131 *result
= CONSUMER_TRACE_CHUNK_EXISTS_STATUS_UNKNOWN_CHUNK
;
2133 case LTTCOMM_CONSUMERD_TRACE_CHUNK_EXISTS_LOCAL
:
2134 consumer_reply_str
= "trace chunk exists locally";
2135 *result
= CONSUMER_TRACE_CHUNK_EXISTS_STATUS_EXISTS_LOCAL
;
2137 case LTTCOMM_CONSUMERD_TRACE_CHUNK_EXISTS_REMOTE
:
2138 consumer_reply_str
= "trace chunk exists on remote peer";
2139 *result
= CONSUMER_TRACE_CHUNK_EXISTS_STATUS_EXISTS_REMOTE
;
2142 ERR("Consumer returned an error from TRACE_CHUNK_EXISTS command");
2146 DBG("Consumer reply to TRACE_CHUNK_EXISTS command: %s", consumer_reply_str
);
2149 health_code_update();