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
14 #include <sys/types.h>
18 #include <common/common.hpp>
19 #include <common/defaults.hpp>
20 #include <common/relayd/relayd.hpp>
21 #include <common/string-utils/format.hpp>
22 #include <common/uri.hpp>
23 #include <lttng/trace-format-descriptor-internal.hpp>
25 #include "consumer.hpp"
26 #include "health-sessiond.hpp"
27 #include "ust-app.hpp"
29 #include "lttng-sessiond.hpp"
32 * Return allocated full pathname of the session using the consumer trace path
33 * and subdir if available.
35 * The caller can safely free(3) the returned value. On error, NULL is
38 char *setup_channel_trace_path(struct consumer_output
*consumer
,
39 const char *session_path
, size_t *consumer_path_offset
)
44 LTTNG_ASSERT(consumer
);
45 LTTNG_ASSERT(session_path
);
50 * Allocate the string ourself to make sure we never exceed
53 pathname
= calloc
<char>(LTTNG_PATH_MAX
);
58 /* Get correct path name destination */
59 if (consumer
->type
== CONSUMER_DST_NET
&&
60 consumer
->relay_major_version
== 2 &&
61 consumer
->relay_minor_version
< 11) {
62 ret
= snprintf(pathname
, LTTNG_PATH_MAX
, "%s%s/%s/%s",
63 consumer
->dst
.net
.base_dir
,
64 consumer
->chunk_path
, consumer
->domain_subdir
,
66 *consumer_path_offset
= 0;
68 ret
= snprintf(pathname
, LTTNG_PATH_MAX
, "%s/%s",
69 consumer
->domain_subdir
, session_path
);
70 *consumer_path_offset
= strlen(consumer
->domain_subdir
) + 1;
72 DBG3("Consumer trace path relative to current trace chunk: \"%s\"",
75 PERROR("Failed to format channel path");
77 } else if (ret
>= LTTNG_PATH_MAX
) {
78 ERR("Truncation occurred while formatting channel path");
89 * Send a data payload using a given consumer socket of size len.
91 * The consumer socket lock MUST be acquired before calling this since this
92 * function can change the fd value.
94 * Return 0 on success else a negative value on error.
96 int consumer_socket_send(
97 struct consumer_socket
*socket
, const void *msg
, size_t len
)
102 LTTNG_ASSERT(socket
);
103 LTTNG_ASSERT(socket
->fd_ptr
);
106 /* Consumer socket is invalid. Stopping. */
107 fd
= *socket
->fd_ptr
;
112 size
= lttcomm_send_unix_sock(fd
, msg
, len
);
114 /* The above call will print a PERROR on error. */
115 DBG("Error when sending data to consumer on sock %d", fd
);
117 * At this point, the socket is not usable anymore thus closing it and
118 * setting the file descriptor to -1 so it is not reused.
121 /* This call will PERROR on error. */
122 (void) lttcomm_close_unix_sock(fd
);
123 *socket
->fd_ptr
= -1;
134 * Receive a data payload using a given consumer socket of size len.
136 * The consumer socket lock MUST be acquired before calling this since this
137 * function can change the fd value.
139 * Return 0 on success else a negative value on error.
141 int consumer_socket_recv(struct consumer_socket
*socket
, void *msg
, size_t len
)
146 LTTNG_ASSERT(socket
);
147 LTTNG_ASSERT(socket
->fd_ptr
);
150 /* Consumer socket is invalid. Stopping. */
151 fd
= *socket
->fd_ptr
;
156 size
= lttcomm_recv_unix_sock(fd
, msg
, len
);
158 /* The above call will print a PERROR on error. */
159 DBG("Error when receiving data from the consumer socket %d", fd
);
161 * At this point, the socket is not usable anymore thus closing it and
162 * setting the file descriptor to -1 so it is not reused.
165 /* This call will PERROR on error. */
166 (void) lttcomm_close_unix_sock(fd
);
167 *socket
->fd_ptr
= -1;
178 * Receive a reply command status message from the consumer. Consumer socket
179 * lock MUST be acquired before calling this function.
181 * Return 0 on success, -1 on recv error or a negative lttng error code which
182 * was possibly returned by the consumer.
184 int consumer_recv_status_reply(struct consumer_socket
*sock
)
187 struct lttcomm_consumer_status_msg reply
;
191 ret
= consumer_socket_recv(sock
, &reply
, sizeof(reply
));
196 if (reply
.ret_code
== LTTCOMM_CONSUMERD_SUCCESS
) {
200 ret
= -reply
.ret_code
;
201 DBG("Consumer ret code %d", ret
);
209 * Once the ASK_CHANNEL command is sent to the consumer, the channel
210 * information are sent back. This call receives that data and populates key
213 * On success return 0 and both key and stream_count are set. On error, a
214 * negative value is sent back and both parameters are untouched.
216 int consumer_recv_status_channel(struct consumer_socket
*sock
,
217 uint64_t *key
, unsigned int *stream_count
)
220 struct lttcomm_consumer_status_channel reply
;
223 LTTNG_ASSERT(stream_count
);
226 ret
= consumer_socket_recv(sock
, &reply
, sizeof(reply
));
231 /* An error is possible so don't touch the key and stream_count. */
232 if (reply
.ret_code
!= LTTCOMM_CONSUMERD_SUCCESS
) {
238 *stream_count
= reply
.stream_count
;
246 * Send destroy relayd command to consumer.
248 * On success return positive value. On error, negative value.
250 int consumer_send_destroy_relayd(struct consumer_socket
*sock
,
251 struct consumer_output
*consumer
)
254 struct lttcomm_consumer_msg msg
;
256 LTTNG_ASSERT(consumer
);
259 DBG2("Sending destroy relayd command to consumer sock %d", *sock
->fd_ptr
);
261 memset(&msg
, 0, sizeof(msg
));
262 msg
.cmd_type
= LTTNG_CONSUMER_DESTROY_RELAYD
;
263 msg
.u
.destroy_relayd
.net_seq_idx
= consumer
->net_seq_index
;
265 pthread_mutex_lock(sock
->lock
);
266 ret
= consumer_socket_send(sock
, &msg
, sizeof(msg
));
271 /* Don't check the return value. The caller will do it. */
272 ret
= consumer_recv_status_reply(sock
);
274 DBG2("Consumer send destroy relayd command done");
277 pthread_mutex_unlock(sock
->lock
);
282 * For each consumer socket in the consumer output object, send a destroy
285 void consumer_output_send_destroy_relayd(struct consumer_output
*consumer
)
287 struct lttng_ht_iter iter
;
288 struct consumer_socket
*socket
;
290 LTTNG_ASSERT(consumer
);
292 /* Destroy any relayd connection */
293 if (consumer
->type
== CONSUMER_DST_NET
) {
295 cds_lfht_for_each_entry(consumer
->socks
->ht
, &iter
.iter
, socket
,
299 /* Send destroy relayd command */
300 ret
= consumer_send_destroy_relayd(socket
, consumer
);
302 DBG("Unable to send destroy relayd command to consumer");
303 /* 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
,
317 struct consumer_output
*output
)
320 struct consumer_socket
*socket
;
324 if (output
== NULL
|| data
->cmd_sock
< 0) {
326 * Not an error. Possible there is simply not spawned consumer or it's
327 * disabled for the tracing session asking the socket.
333 socket
= consumer_find_socket(data
->cmd_sock
, output
);
335 if (socket
== NULL
) {
336 socket
= consumer_allocate_socket(&data
->cmd_sock
);
337 if (socket
== NULL
) {
342 socket
->registered
= 0;
343 socket
->lock
= &data
->lock
;
345 consumer_add_socket(socket
, output
);
349 socket
->type
= data
->type
;
351 DBG3("Consumer socket created (fd: %d) and added to output",
359 * Return the consumer socket from the given consumer output with the right
360 * bitness. On error, returns NULL.
362 * The caller MUST acquire a rcu read side lock and keep it until the socket
363 * object reference is not needed anymore.
365 struct consumer_socket
*consumer_find_socket_by_bitness(int bits
,
366 const struct consumer_output
*consumer
)
369 struct consumer_socket
*socket
= NULL
;
371 ASSERT_RCU_READ_LOCKED();
375 consumer_fd
= uatomic_read(&the_ust_consumerd64_fd
);
378 consumer_fd
= uatomic_read(&the_ust_consumerd32_fd
);
385 socket
= consumer_find_socket(consumer_fd
, consumer
);
387 ERR("Consumer socket fd %d not found in consumer obj %p",
388 consumer_fd
, consumer
);
396 * Find a consumer_socket in a consumer_output hashtable. Read side lock must
397 * be acquired before calling this function and across use of the
398 * returned consumer_socket.
400 struct consumer_socket
*consumer_find_socket(int key
,
401 const struct consumer_output
*consumer
)
403 struct lttng_ht_iter iter
;
404 struct lttng_ht_node_ulong
*node
;
405 struct consumer_socket
*socket
= NULL
;
407 ASSERT_RCU_READ_LOCKED();
409 /* Negative keys are lookup failures */
410 if (key
< 0 || consumer
== NULL
) {
414 lttng_ht_lookup(consumer
->socks
, (void *)((unsigned long) key
),
416 node
= lttng_ht_iter_get_node_ulong(&iter
);
418 socket
= lttng::utils::container_of(node
, &consumer_socket::node
);
425 * Allocate a new consumer_socket and return the pointer.
427 struct consumer_socket
*consumer_allocate_socket(int *fd
)
429 struct consumer_socket
*socket
= NULL
;
433 socket
= zmalloc
<consumer_socket
>();
434 if (socket
== NULL
) {
435 PERROR("zmalloc consumer socket");
440 lttng_ht_node_init_ulong(&socket
->node
, *fd
);
447 * Add consumer socket to consumer output object. Read side lock must be
448 * acquired before calling this function.
450 void consumer_add_socket(struct consumer_socket
*sock
,
451 struct consumer_output
*consumer
)
454 LTTNG_ASSERT(consumer
);
455 ASSERT_RCU_READ_LOCKED();
457 lttng_ht_add_unique_ulong(consumer
->socks
, &sock
->node
);
461 * Delete consumer socket to consumer output object. Read side lock must be
462 * acquired before calling this function.
464 void consumer_del_socket(struct consumer_socket
*sock
,
465 struct consumer_output
*consumer
)
468 struct lttng_ht_iter iter
;
471 LTTNG_ASSERT(consumer
);
472 ASSERT_RCU_READ_LOCKED();
474 iter
.iter
.node
= &sock
->node
.node
;
475 ret
= lttng_ht_del(consumer
->socks
, &iter
);
480 * RCU destroy call function.
482 static void destroy_socket_rcu(struct rcu_head
*head
)
484 struct lttng_ht_node_ulong
*node
=
485 lttng::utils::container_of(head
, <tng_ht_node_ulong::head
);
486 struct consumer_socket
*socket
=
487 lttng::utils::container_of(node
, &consumer_socket::node
);
493 * Destroy and free socket pointer in a call RCU. The call must either:
494 * - have acquired the read side lock before calling this function, or
495 * - guarantee the validity of the `struct consumer_socket` object for the
496 * duration of the call.
498 void consumer_destroy_socket(struct consumer_socket
*sock
)
503 * We DO NOT close the file descriptor here since it is global to the
504 * session daemon and is closed only if the consumer dies or a custom
505 * consumer was registered,
507 if (sock
->registered
) {
508 DBG3("Consumer socket was registered. Closing fd %d", *sock
->fd_ptr
);
509 lttcomm_close_unix_sock(*sock
->fd_ptr
);
512 call_rcu(&sock
->node
.head
, destroy_socket_rcu
);
516 * Allocate and assign data to a consumer_output object.
518 * Return pointer to structure.
520 struct consumer_output
*consumer_create_output(enum consumer_dst_type type
)
522 struct consumer_output
*output
= NULL
;
524 output
= zmalloc
<consumer_output
>();
525 if (output
== NULL
) {
526 PERROR("zmalloc consumer_output");
530 /* By default, consumer output is enabled */
533 output
->net_seq_index
= (uint64_t) -1ULL;
534 urcu_ref_init(&output
->ref
);
536 output
->socks
= lttng_ht_new(0, LTTNG_HT_TYPE_ULONG
);
543 * Iterate over the consumer output socket hash table and destroy them. The
544 * socket file descriptor are only closed if the consumer output was
545 * registered meaning it's an external consumer.
547 void consumer_destroy_output_sockets(struct consumer_output
*obj
)
549 struct lttng_ht_iter iter
;
550 struct consumer_socket
*socket
;
557 cds_lfht_for_each_entry(obj
->socks
->ht
, &iter
.iter
, socket
, node
.node
) {
558 consumer_del_socket(socket
, obj
);
559 consumer_destroy_socket(socket
);
565 * Delete the consumer_output object from the list and free the ptr.
567 static void consumer_release_output(struct urcu_ref
*ref
)
569 struct consumer_output
*obj
=
570 lttng::utils::container_of(ref
, &consumer_output::ref
);
572 consumer_destroy_output_sockets(obj
);
575 /* Finally destroy HT */
576 lttng_ht_destroy(obj
->socks
);
583 * Get the consumer_output object.
585 void consumer_output_get(struct consumer_output
*obj
)
587 urcu_ref_get(&obj
->ref
);
591 * Put the consumer_output object.
593 void consumer_output_put(struct consumer_output
*obj
)
598 urcu_ref_put(&obj
->ref
, consumer_release_output
);
602 * Copy consumer output and returned the newly allocated copy.
604 struct consumer_output
*consumer_copy_output(struct consumer_output
*src
)
607 struct consumer_output
*output
;
611 output
= consumer_create_output(src
->type
);
612 if (output
== NULL
) {
615 output
->enabled
= src
->enabled
;
616 output
->net_seq_index
= src
->net_seq_index
;
617 memcpy(output
->domain_subdir
, src
->domain_subdir
,
618 sizeof(output
->domain_subdir
));
619 output
->snapshot
= src
->snapshot
;
620 output
->relay_major_version
= src
->relay_major_version
;
621 output
->relay_minor_version
= src
->relay_minor_version
;
622 output
->relay_allows_clear
= src
->relay_allows_clear
;
623 memcpy(&output
->dst
, &src
->dst
, sizeof(output
->dst
));
624 ret
= consumer_copy_sockets(output
, src
);
632 consumer_output_put(output
);
637 * Copy consumer sockets from src to dst.
639 * Return 0 on success or else a negative value.
641 int consumer_copy_sockets(struct consumer_output
*dst
,
642 struct consumer_output
*src
)
645 struct lttng_ht_iter iter
;
646 struct consumer_socket
*socket
, *copy_sock
;
652 cds_lfht_for_each_entry(src
->socks
->ht
, &iter
.iter
, socket
, node
.node
) {
653 /* Ignore socket that are already there. */
654 copy_sock
= consumer_find_socket(*socket
->fd_ptr
, dst
);
659 /* Create new socket object. */
660 copy_sock
= consumer_allocate_socket(socket
->fd_ptr
);
661 if (copy_sock
== NULL
) {
667 copy_sock
->registered
= socket
->registered
;
669 * This is valid because this lock is shared accross all consumer
670 * object being the global lock of the consumer data structure of the
673 copy_sock
->lock
= socket
->lock
;
674 consumer_add_socket(copy_sock
, dst
);
683 * Set network URI to the consumer output.
685 * Return 0 on success. Return 1 if the URI were equal. Else, negative value on
688 int consumer_set_network_uri(const struct ltt_session
*session
,
689 struct consumer_output
*output
,
690 struct lttng_uri
*uri
)
693 struct lttng_uri
*dst_uri
= NULL
;
695 /* Code flow error safety net. */
696 LTTNG_ASSERT(output
);
699 switch (uri
->stype
) {
700 case LTTNG_STREAM_CONTROL
:
701 dst_uri
= &output
->dst
.net
.control
;
702 output
->dst
.net
.control_isset
= 1;
703 if (uri
->port
== 0) {
704 /* Assign default port. */
705 uri
->port
= DEFAULT_NETWORK_CONTROL_PORT
;
707 if (output
->dst
.net
.data_isset
&& uri
->port
==
708 output
->dst
.net
.data
.port
) {
709 ret
= -LTTNG_ERR_INVALID
;
713 DBG3("Consumer control URI set with port %d", uri
->port
);
715 case LTTNG_STREAM_DATA
:
716 dst_uri
= &output
->dst
.net
.data
;
717 output
->dst
.net
.data_isset
= 1;
718 if (uri
->port
== 0) {
719 /* Assign default port. */
720 uri
->port
= DEFAULT_NETWORK_DATA_PORT
;
722 if (output
->dst
.net
.control_isset
&& uri
->port
==
723 output
->dst
.net
.control
.port
) {
724 ret
= -LTTNG_ERR_INVALID
;
728 DBG3("Consumer data URI set with port %d", uri
->port
);
731 ERR("Set network uri type unknown %d", uri
->stype
);
732 ret
= -LTTNG_ERR_INVALID
;
736 ret
= uri_compare(dst_uri
, uri
);
738 /* Same URI, don't touch it and return success. */
739 DBG3("URI network compare are the same");
743 /* URIs were not equal, replacing it. */
744 memcpy(dst_uri
, uri
, sizeof(struct lttng_uri
));
745 output
->type
= CONSUMER_DST_NET
;
746 if (dst_uri
->stype
!= LTTNG_STREAM_CONTROL
) {
747 /* Only the control uri needs to contain the path. */
752 * If the user has specified a subdir as part of the control
753 * URL, the session's base output directory is:
754 * /RELAYD_OUTPUT_PATH/HOSTNAME/USER_SPECIFIED_DIR
756 * Hence, the "base_dir" from which all stream files and
757 * session rotation chunks are created takes the form
758 * /HOSTNAME/USER_SPECIFIED_DIR
760 * If the user has not specified an output directory as part of
761 * the control URL, the base output directory has the form:
762 * /RELAYD_OUTPUT_PATH/HOSTNAME/SESSION_NAME-CREATION_TIME
764 * Hence, the "base_dir" from which all stream files and
765 * session rotation chunks are created takes the form
766 * /HOSTNAME/SESSION_NAME-CREATION_TIME
768 * Note that automatically generated session names already
769 * contain the session's creation time. In that case, the
770 * creation time is omitted to prevent it from being duplicated
771 * in the final directory hierarchy.
774 if (strstr(uri
->subdir
, "../")) {
775 ERR("Network URI subdirs are not allowed to walk up the path hierarchy");
776 ret
= -LTTNG_ERR_INVALID
;
779 ret
= snprintf(output
->dst
.net
.base_dir
,
780 sizeof(output
->dst
.net
.base_dir
),
781 "/%s/%s/", session
->hostname
, uri
->subdir
);
783 if (session
->has_auto_generated_name
) {
784 ret
= snprintf(output
->dst
.net
.base_dir
,
785 sizeof(output
->dst
.net
.base_dir
),
786 "/%s/%s/", session
->hostname
,
789 char session_creation_datetime
[16];
793 timeinfo
= localtime(&session
->creation_time
);
795 ret
= -LTTNG_ERR_FATAL
;
798 strftime_ret
= strftime(session_creation_datetime
,
799 sizeof(session_creation_datetime
),
800 "%Y%m%d-%H%M%S", timeinfo
);
801 if (strftime_ret
== 0) {
802 ERR("Failed to format session creation timestamp while setting network URI");
803 ret
= -LTTNG_ERR_FATAL
;
806 ret
= snprintf(output
->dst
.net
.base_dir
,
807 sizeof(output
->dst
.net
.base_dir
),
808 "/%s/%s-%s/", session
->hostname
,
810 session_creation_datetime
);
813 if (ret
>= sizeof(output
->dst
.net
.base_dir
)) {
814 ret
= -LTTNG_ERR_INVALID
;
815 ERR("Truncation occurred while setting network output base directory");
817 } else if (ret
== -1) {
818 ret
= -LTTNG_ERR_INVALID
;
819 PERROR("Error occurred while setting network output base directory");
823 DBG3("Consumer set network uri base_dir path %s",
824 output
->dst
.net
.base_dir
);
835 * Send file descriptor to consumer via sock.
837 * The consumer socket lock must be held by the caller.
839 int consumer_send_fds(struct consumer_socket
*sock
, const int *fds
,
846 LTTNG_ASSERT(nb_fd
> 0);
847 LTTNG_ASSERT(pthread_mutex_trylock(sock
->lock
) == EBUSY
);
849 ret
= lttcomm_send_fds_unix_sock(*sock
->fd_ptr
, fds
, nb_fd
);
851 /* The above call will print a PERROR on error. */
852 DBG("Error when sending consumer fds on sock %d", *sock
->fd_ptr
);
856 ret
= consumer_recv_status_reply(sock
);
862 * Consumer send communication message structure to consumer.
864 * The consumer socket lock must be held by the caller.
866 int consumer_send_msg(struct consumer_socket
*sock
,
867 const struct lttcomm_consumer_msg
*msg
)
873 LTTNG_ASSERT(pthread_mutex_trylock(sock
->lock
) == EBUSY
);
875 ret
= consumer_socket_send(sock
, msg
, sizeof(struct lttcomm_consumer_msg
));
880 ret
= consumer_recv_status_reply(sock
);
887 * Consumer send channel communication message structure to consumer.
889 * The consumer socket lock must be held by the caller.
891 int consumer_send_channel(struct consumer_socket
*sock
,
892 struct lttcomm_consumer_msg
*msg
)
899 ret
= consumer_send_msg(sock
, msg
);
909 * Populate the given consumer msg structure with the ask_channel command
912 void consumer_init_ask_channel_comm_msg(struct lttcomm_consumer_msg
*msg
,
913 uint64_t subbuf_size
,
916 unsigned int switch_timer_interval
,
917 unsigned int read_timer_interval
,
918 unsigned int live_timer_interval
,
919 bool is_in_live_session
,
920 unsigned int monitor_timer_interval
,
924 const char *pathname
,
928 const lttng_uuid
& uuid
,
930 uint64_t tracefile_size
,
931 uint64_t tracefile_count
,
932 uint64_t session_id_per_pid
,
933 unsigned int monitor
,
934 uint32_t ust_app_uid
,
935 int64_t blocking_timeout
,
936 const char *root_shm_path
,
937 const char *shm_path
,
938 struct lttng_trace_chunk
*trace_chunk
,
939 const struct lttng_credentials
*buffer_credentials
,
940 const lttng::trace_format_descriptor
& trace_format
)
944 /* Zeroed structure */
945 memset(msg
, 0, sizeof(struct lttcomm_consumer_msg
));
946 msg
->u
.ask_channel
.buffer_credentials
.uid
= UINT32_MAX
;
947 msg
->u
.ask_channel
.buffer_credentials
.gid
= UINT32_MAX
;
951 enum lttng_trace_chunk_status chunk_status
;
953 chunk_status
= lttng_trace_chunk_get_id(trace_chunk
, &chunk_id
);
954 LTTNG_ASSERT(chunk_status
== LTTNG_TRACE_CHUNK_STATUS_OK
);
955 LTTNG_OPTIONAL_SET(&msg
->u
.ask_channel
.chunk_id
, chunk_id
);
957 msg
->u
.ask_channel
.buffer_credentials
.uid
=
958 lttng_credentials_get_uid(buffer_credentials
);
959 msg
->u
.ask_channel
.buffer_credentials
.gid
=
960 lttng_credentials_get_gid(buffer_credentials
);
962 msg
->cmd_type
= LTTNG_CONSUMER_ASK_CHANNEL_CREATION
;
963 msg
->u
.ask_channel
.subbuf_size
= subbuf_size
;
964 msg
->u
.ask_channel
.num_subbuf
= num_subbuf
;
965 msg
->u
.ask_channel
.overwrite
= overwrite
;
966 msg
->u
.ask_channel
.switch_timer_interval
= switch_timer_interval
;
967 msg
->u
.ask_channel
.read_timer_interval
= read_timer_interval
;
968 msg
->u
.ask_channel
.live_timer_interval
= live_timer_interval
;
969 msg
->u
.ask_channel
.is_live
= is_in_live_session
;
970 msg
->u
.ask_channel
.monitor_timer_interval
= monitor_timer_interval
;
971 msg
->u
.ask_channel
.output
= output
;
972 msg
->u
.ask_channel
.type
= type
;
973 msg
->u
.ask_channel
.session_id
= session_id
;
974 msg
->u
.ask_channel
.session_id_per_pid
= session_id_per_pid
;
975 msg
->u
.ask_channel
.relayd_id
= relayd_id
;
976 msg
->u
.ask_channel
.key
= key
;
977 msg
->u
.ask_channel
.chan_id
= chan_id
;
978 msg
->u
.ask_channel
.tracefile_size
= tracefile_size
;
979 msg
->u
.ask_channel
.tracefile_count
= tracefile_count
;
980 msg
->u
.ask_channel
.monitor
= monitor
;
981 msg
->u
.ask_channel
.ust_app_uid
= ust_app_uid
;
982 msg
->u
.ask_channel
.blocking_timeout
= blocking_timeout
;
983 if (trace_format
.type() == LTTNG_TRACE_FORMAT_DESCRIPTOR_TYPE_CTF_1
) {
984 msg
->u
.ask_channel
.trace_format
= 1;
986 msg
->u
.ask_channel
.trace_format
= 2;
989 std::copy(uuid
.begin(), uuid
.end(), msg
->u
.ask_channel
.uuid
);
992 strncpy(msg
->u
.ask_channel
.pathname
, pathname
,
993 sizeof(msg
->u
.ask_channel
.pathname
));
994 msg
->u
.ask_channel
.pathname
[sizeof(msg
->u
.ask_channel
.pathname
)-1] = '\0';
997 strncpy(msg
->u
.ask_channel
.name
, name
, sizeof(msg
->u
.ask_channel
.name
));
998 msg
->u
.ask_channel
.name
[sizeof(msg
->u
.ask_channel
.name
) - 1] = '\0';
1000 if (root_shm_path
) {
1001 strncpy(msg
->u
.ask_channel
.root_shm_path
, root_shm_path
,
1002 sizeof(msg
->u
.ask_channel
.root_shm_path
));
1003 msg
->u
.ask_channel
.root_shm_path
[sizeof(msg
->u
.ask_channel
.root_shm_path
) - 1] = '\0';
1006 strncpy(msg
->u
.ask_channel
.shm_path
, shm_path
,
1007 sizeof(msg
->u
.ask_channel
.shm_path
));
1008 msg
->u
.ask_channel
.shm_path
[sizeof(msg
->u
.ask_channel
.shm_path
) - 1] = '\0';
1013 * Init channel communication message structure.
1015 void consumer_init_add_channel_comm_msg(struct lttcomm_consumer_msg
*msg
,
1016 uint64_t channel_key
,
1017 uint64_t session_id
,
1018 const char *pathname
,
1021 unsigned int nb_init_streams
,
1022 enum lttng_event_output output
,
1024 uint64_t tracefile_size
,
1025 uint64_t tracefile_count
,
1026 unsigned int monitor
,
1027 unsigned int live_timer_interval
,
1028 bool is_in_live_session
,
1029 unsigned int monitor_timer_interval
,
1030 struct lttng_trace_chunk
*trace_chunk
,
1031 const lttng::trace_format_descriptor
& trace_format
)
1035 /* Zeroed structure */
1036 memset(msg
, 0, sizeof(struct lttcomm_consumer_msg
));
1040 enum lttng_trace_chunk_status chunk_status
;
1042 chunk_status
= lttng_trace_chunk_get_id(trace_chunk
, &chunk_id
);
1043 LTTNG_ASSERT(chunk_status
== LTTNG_TRACE_CHUNK_STATUS_OK
);
1044 LTTNG_OPTIONAL_SET(&msg
->u
.channel
.chunk_id
, chunk_id
);
1048 msg
->cmd_type
= LTTNG_CONSUMER_ADD_CHANNEL
;
1049 msg
->u
.channel
.channel_key
= channel_key
;
1050 msg
->u
.channel
.session_id
= session_id
;
1051 msg
->u
.channel
.relayd_id
= relayd_id
;
1052 msg
->u
.channel
.nb_init_streams
= nb_init_streams
;
1053 msg
->u
.channel
.output
= output
;
1054 msg
->u
.channel
.type
= type
;
1055 msg
->u
.channel
.tracefile_size
= tracefile_size
;
1056 msg
->u
.channel
.tracefile_count
= tracefile_count
;
1057 msg
->u
.channel
.monitor
= monitor
;
1058 msg
->u
.channel
.live_timer_interval
= live_timer_interval
;
1059 msg
->u
.channel
.is_live
= is_in_live_session
;
1060 msg
->u
.channel
.monitor_timer_interval
= monitor_timer_interval
;
1061 if (trace_format
.type() == LTTNG_TRACE_FORMAT_DESCRIPTOR_TYPE_CTF_1
) {
1062 msg
->u
.channel
.trace_format
= 1;
1064 msg
->u
.channel
.trace_format
= 2;
1067 strncpy(msg
->u
.channel
.pathname
, pathname
,
1068 sizeof(msg
->u
.channel
.pathname
));
1069 msg
->u
.channel
.pathname
[sizeof(msg
->u
.channel
.pathname
) - 1] = '\0';
1071 strncpy(msg
->u
.channel
.name
, name
, sizeof(msg
->u
.channel
.name
));
1072 msg
->u
.channel
.name
[sizeof(msg
->u
.channel
.name
) - 1] = '\0';
1076 * Init stream communication message structure.
1078 void consumer_init_add_stream_comm_msg(struct lttcomm_consumer_msg
*msg
,
1079 uint64_t channel_key
,
1080 uint64_t stream_key
,
1085 memset(msg
, 0, sizeof(struct lttcomm_consumer_msg
));
1087 msg
->cmd_type
= LTTNG_CONSUMER_ADD_STREAM
;
1088 msg
->u
.stream
.channel_key
= channel_key
;
1089 msg
->u
.stream
.stream_key
= stream_key
;
1090 msg
->u
.stream
.cpu
= cpu
;
1093 void consumer_init_streams_sent_comm_msg(struct lttcomm_consumer_msg
*msg
,
1094 enum lttng_consumer_command cmd
,
1095 uint64_t channel_key
, uint64_t net_seq_idx
)
1099 memset(msg
, 0, sizeof(struct lttcomm_consumer_msg
));
1101 msg
->cmd_type
= cmd
;
1102 msg
->u
.sent_streams
.channel_key
= channel_key
;
1103 msg
->u
.sent_streams
.net_seq_idx
= net_seq_idx
;
1107 * Send stream communication structure to the consumer.
1109 int consumer_send_stream(struct consumer_socket
*sock
,
1110 struct consumer_output
*dst
, struct lttcomm_consumer_msg
*msg
,
1111 const int *fds
, size_t nb_fd
)
1120 ret
= consumer_send_msg(sock
, msg
);
1125 ret
= consumer_send_fds(sock
, fds
, nb_fd
);
1135 * Send relayd socket to consumer associated with a session name.
1137 * The consumer socket lock must be held by the caller.
1139 * On success return positive value. On error, negative value.
1141 int consumer_send_relayd_socket(struct consumer_socket
*consumer_sock
,
1142 struct lttcomm_relayd_sock
*rsock
, struct consumer_output
*consumer
,
1143 enum lttng_stream_type type
, uint64_t session_id
,
1144 const char *session_name
, const char *hostname
,
1145 const char *base_path
, int session_live_timer
,
1146 const uint64_t *current_chunk_id
, time_t session_creation_time
,
1147 bool session_name_contains_creation_time
)
1151 struct lttcomm_consumer_msg msg
;
1153 /* Code flow error. Safety net. */
1154 LTTNG_ASSERT(rsock
);
1155 LTTNG_ASSERT(consumer
);
1156 LTTNG_ASSERT(consumer_sock
);
1158 memset(&msg
, 0, sizeof(msg
));
1159 /* Bail out if consumer is disabled */
1160 if (!consumer
->enabled
) {
1165 if (type
== LTTNG_STREAM_CONTROL
) {
1166 char output_path
[LTTNG_PATH_MAX
] = {};
1167 uint64_t relayd_session_id
;
1169 ret
= relayd_create_session(rsock
, &relayd_session_id
,
1170 session_name
, hostname
, base_path
,
1171 session_live_timer
, consumer
->snapshot
,
1172 session_id
, the_sessiond_uuid
, current_chunk_id
,
1173 session_creation_time
,
1174 session_name_contains_creation_time
,
1177 /* Close the control socket. */
1178 (void) relayd_close(rsock
);
1181 msg
.u
.relayd_sock
.relayd_session_id
= relayd_session_id
;
1182 DBG("Created session on relay, output path reply: %s",
1186 msg
.cmd_type
= LTTNG_CONSUMER_ADD_RELAYD_SOCKET
;
1188 * Assign network consumer output index using the temporary consumer since
1189 * this call should only be made from within a set_consumer_uri() function
1190 * call in the session daemon.
1192 msg
.u
.relayd_sock
.net_index
= consumer
->net_seq_index
;
1193 msg
.u
.relayd_sock
.type
= type
;
1194 msg
.u
.relayd_sock
.session_id
= session_id
;
1195 msg
.u
.relayd_sock
.major
= rsock
->major
;
1196 msg
.u
.relayd_sock
.minor
= rsock
->minor
;
1197 msg
.u
.relayd_sock
.relayd_socket_protocol
= rsock
->sock
.proto
;
1199 DBG3("Sending relayd sock info to consumer on %d", *consumer_sock
->fd_ptr
);
1200 ret
= consumer_send_msg(consumer_sock
, &msg
);
1205 DBG3("Sending relayd socket file descriptor to consumer");
1206 fd
= rsock
->sock
.fd
;
1207 ret
= consumer_send_fds(consumer_sock
, &fd
, 1);
1212 DBG2("Consumer relayd socket sent");
1219 int consumer_send_pipe(struct consumer_socket
*consumer_sock
,
1220 enum lttng_consumer_command cmd
, int pipe
)
1223 struct lttcomm_consumer_msg msg
;
1224 const char *pipe_name
;
1225 const char *command_name
;
1228 case LTTNG_CONSUMER_SET_CHANNEL_MONITOR_PIPE
:
1229 pipe_name
= "channel monitor";
1230 command_name
= "SET_CHANNEL_MONITOR_PIPE";
1233 ERR("Unexpected command received in %s (cmd = %d)", __func__
,
1238 /* Code flow error. Safety net. */
1240 memset(&msg
, 0, sizeof(msg
));
1243 pthread_mutex_lock(consumer_sock
->lock
);
1244 DBG3("Sending %s command to consumer", command_name
);
1245 ret
= consumer_send_msg(consumer_sock
, &msg
);
1250 DBG3("Sending %s pipe %d to consumer on socket %d",
1252 pipe
, *consumer_sock
->fd_ptr
);
1253 ret
= consumer_send_fds(consumer_sock
, &pipe
, 1);
1258 DBG2("%s pipe successfully sent", pipe_name
);
1260 pthread_mutex_unlock(consumer_sock
->lock
);
1264 int consumer_send_channel_monitor_pipe(struct consumer_socket
*consumer_sock
,
1267 return consumer_send_pipe(consumer_sock
,
1268 LTTNG_CONSUMER_SET_CHANNEL_MONITOR_PIPE
, pipe
);
1272 * Ask the consumer if the data is pending for the specific session id.
1273 * Returns 1 if data is pending, 0 otherwise, or < 0 on error.
1275 int consumer_is_data_pending(uint64_t session_id
,
1276 struct consumer_output
*consumer
)
1279 int32_t ret_code
= 0; /* Default is that the data is NOT pending */
1280 struct consumer_socket
*socket
;
1281 struct lttng_ht_iter iter
;
1282 struct lttcomm_consumer_msg msg
;
1284 LTTNG_ASSERT(consumer
);
1286 DBG3("Consumer data pending for id %" PRIu64
, session_id
);
1288 memset(&msg
, 0, sizeof(msg
));
1289 msg
.cmd_type
= LTTNG_CONSUMER_DATA_PENDING
;
1290 msg
.u
.data_pending
.session_id
= session_id
;
1292 /* Send command for each consumer */
1294 cds_lfht_for_each_entry(consumer
->socks
->ht
, &iter
.iter
, socket
,
1296 pthread_mutex_lock(socket
->lock
);
1297 ret
= consumer_socket_send(socket
, &msg
, sizeof(msg
));
1299 pthread_mutex_unlock(socket
->lock
);
1304 * No need for a recv reply status because the answer to the command is
1305 * the reply status message.
1308 ret
= consumer_socket_recv(socket
, &ret_code
, sizeof(ret_code
));
1310 pthread_mutex_unlock(socket
->lock
);
1313 pthread_mutex_unlock(socket
->lock
);
1315 if (ret_code
== 1) {
1321 DBG("Consumer data is %s pending for session id %" PRIu64
,
1322 ret_code
== 1 ? "" : "NOT", session_id
);
1331 * Send a flush command to consumer using the given channel key.
1333 * Return 0 on success else a negative value.
1335 int consumer_flush_channel(struct consumer_socket
*socket
, uint64_t key
)
1338 struct lttcomm_consumer_msg msg
;
1340 LTTNG_ASSERT(socket
);
1342 DBG2("Consumer flush channel key %" PRIu64
, key
);
1344 memset(&msg
, 0, sizeof(msg
));
1345 msg
.cmd_type
= LTTNG_CONSUMER_FLUSH_CHANNEL
;
1346 msg
.u
.flush_channel
.key
= key
;
1348 pthread_mutex_lock(socket
->lock
);
1349 health_code_update();
1351 ret
= consumer_send_msg(socket
, &msg
);
1357 health_code_update();
1358 pthread_mutex_unlock(socket
->lock
);
1363 * Send a clear quiescent command to consumer using the given channel key.
1365 * Return 0 on success else a negative value.
1367 int consumer_clear_quiescent_channel(struct consumer_socket
*socket
, uint64_t key
)
1370 struct lttcomm_consumer_msg msg
;
1372 LTTNG_ASSERT(socket
);
1374 DBG2("Consumer clear quiescent channel key %" PRIu64
, key
);
1376 memset(&msg
, 0, sizeof(msg
));
1377 msg
.cmd_type
= LTTNG_CONSUMER_CLEAR_QUIESCENT_CHANNEL
;
1378 msg
.u
.clear_quiescent_channel
.key
= key
;
1380 pthread_mutex_lock(socket
->lock
);
1381 health_code_update();
1383 ret
= consumer_send_msg(socket
, &msg
);
1389 health_code_update();
1390 pthread_mutex_unlock(socket
->lock
);
1395 * Send a close metadata command to consumer using the given channel key.
1396 * Called with registry lock held.
1398 * Return 0 on success else a negative value.
1400 int consumer_close_metadata(struct consumer_socket
*socket
,
1401 uint64_t metadata_key
)
1404 struct lttcomm_consumer_msg msg
;
1406 LTTNG_ASSERT(socket
);
1408 DBG2("Consumer close metadata channel key %" PRIu64
, metadata_key
);
1410 memset(&msg
, 0, sizeof(msg
));
1411 msg
.cmd_type
= LTTNG_CONSUMER_CLOSE_METADATA
;
1412 msg
.u
.close_metadata
.key
= metadata_key
;
1414 pthread_mutex_lock(socket
->lock
);
1415 health_code_update();
1417 ret
= consumer_send_msg(socket
, &msg
);
1423 health_code_update();
1424 pthread_mutex_unlock(socket
->lock
);
1429 * Send a setup metdata command to consumer using the given channel key.
1431 * Return 0 on success else a negative value.
1433 int consumer_setup_metadata(struct consumer_socket
*socket
,
1434 uint64_t metadata_key
)
1437 struct lttcomm_consumer_msg msg
;
1439 LTTNG_ASSERT(socket
);
1441 DBG2("Consumer setup metadata channel key %" PRIu64
, metadata_key
);
1443 memset(&msg
, 0, sizeof(msg
));
1444 msg
.cmd_type
= LTTNG_CONSUMER_SETUP_METADATA
;
1445 msg
.u
.setup_metadata
.key
= metadata_key
;
1447 pthread_mutex_lock(socket
->lock
);
1448 health_code_update();
1450 ret
= consumer_send_msg(socket
, &msg
);
1456 health_code_update();
1457 pthread_mutex_unlock(socket
->lock
);
1462 * Send metadata string to consumer.
1463 * RCU read-side lock must be held to guarantee existence of socket.
1465 * Return 0 on success else a negative value.
1467 int consumer_push_metadata(struct consumer_socket
*socket
,
1468 uint64_t metadata_key
, char *metadata_str
, size_t len
,
1469 size_t target_offset
, uint64_t version
)
1472 struct lttcomm_consumer_msg msg
;
1474 LTTNG_ASSERT(socket
);
1475 ASSERT_RCU_READ_LOCKED();
1477 DBG2("Consumer push metadata to consumer socket %d", *socket
->fd_ptr
);
1479 pthread_mutex_lock(socket
->lock
);
1481 memset(&msg
, 0, sizeof(msg
));
1482 msg
.cmd_type
= LTTNG_CONSUMER_PUSH_METADATA
;
1483 msg
.u
.push_metadata
.key
= metadata_key
;
1484 msg
.u
.push_metadata
.target_offset
= target_offset
;
1485 msg
.u
.push_metadata
.len
= len
;
1486 msg
.u
.push_metadata
.version
= version
;
1488 health_code_update();
1489 ret
= consumer_send_msg(socket
, &msg
);
1490 if (ret
< 0 || len
== 0) {
1494 DBG3("Consumer pushing metadata on sock %d of len %zu", *socket
->fd_ptr
,
1497 ret
= consumer_socket_send(socket
, metadata_str
, len
);
1502 health_code_update();
1503 ret
= consumer_recv_status_reply(socket
);
1509 pthread_mutex_unlock(socket
->lock
);
1510 health_code_update();
1515 * Ask the consumer to snapshot a specific channel using the key.
1517 * Returns LTTNG_OK on success or else an LTTng error code.
1519 enum lttng_error_code
consumer_snapshot_channel(struct consumer_socket
*socket
,
1520 uint64_t key
, const struct consumer_output
*output
, int metadata
,
1521 const char *channel_path
,
1522 uint64_t nb_packets_per_stream
)
1525 enum lttng_error_code status
= LTTNG_OK
;
1526 struct lttcomm_consumer_msg msg
;
1528 LTTNG_ASSERT(socket
);
1529 LTTNG_ASSERT(output
);
1531 DBG("Consumer snapshot channel key %" PRIu64
, key
);
1533 memset(&msg
, 0, sizeof(msg
));
1534 msg
.cmd_type
= LTTNG_CONSUMER_SNAPSHOT_CHANNEL
;
1535 msg
.u
.snapshot_channel
.key
= key
;
1536 msg
.u
.snapshot_channel
.nb_packets_per_stream
= nb_packets_per_stream
;
1537 msg
.u
.snapshot_channel
.metadata
= metadata
;
1539 if (output
->type
== CONSUMER_DST_NET
) {
1540 msg
.u
.snapshot_channel
.relayd_id
=
1541 output
->net_seq_index
;
1542 msg
.u
.snapshot_channel
.use_relayd
= 1;
1544 msg
.u
.snapshot_channel
.relayd_id
= (uint64_t) -1ULL;
1546 ret
= lttng_strncpy(msg
.u
.snapshot_channel
.pathname
,
1548 sizeof(msg
.u
.snapshot_channel
.pathname
));
1550 ERR("Snapshot path exceeds the maximal allowed length of %zu bytes (%zu bytes required) with path \"%s\"",
1551 sizeof(msg
.u
.snapshot_channel
.pathname
),
1552 strlen(channel_path
),
1554 status
= LTTNG_ERR_SNAPSHOT_FAIL
;
1558 health_code_update();
1559 pthread_mutex_lock(socket
->lock
);
1560 ret
= consumer_send_msg(socket
, &msg
);
1561 pthread_mutex_unlock(socket
->lock
);
1564 case LTTCOMM_CONSUMERD_CHAN_NOT_FOUND
:
1565 status
= LTTNG_ERR_CHAN_NOT_FOUND
;
1568 status
= LTTNG_ERR_SNAPSHOT_FAIL
;
1575 health_code_update();
1580 * Ask the consumer the number of discarded events for a channel.
1582 int consumer_get_discarded_events(uint64_t session_id
, uint64_t channel_key
,
1583 struct consumer_output
*consumer
, uint64_t *discarded
)
1586 struct consumer_socket
*socket
;
1587 struct lttng_ht_iter iter
;
1588 struct lttcomm_consumer_msg msg
;
1590 LTTNG_ASSERT(consumer
);
1592 DBG3("Consumer discarded events id %" PRIu64
, session_id
);
1594 memset(&msg
, 0, sizeof(msg
));
1595 msg
.cmd_type
= LTTNG_CONSUMER_DISCARDED_EVENTS
;
1596 msg
.u
.discarded_events
.session_id
= session_id
;
1597 msg
.u
.discarded_events
.channel_key
= channel_key
;
1601 /* Send command for each consumer */
1603 cds_lfht_for_each_entry(consumer
->socks
->ht
, &iter
.iter
, socket
,
1605 uint64_t consumer_discarded
= 0;
1606 pthread_mutex_lock(socket
->lock
);
1607 ret
= consumer_socket_send(socket
, &msg
, sizeof(msg
));
1609 pthread_mutex_unlock(socket
->lock
);
1614 * No need for a recv reply status because the answer to the
1615 * command is the reply status message.
1617 ret
= consumer_socket_recv(socket
, &consumer_discarded
,
1618 sizeof(consumer_discarded
));
1620 ERR("get discarded events");
1621 pthread_mutex_unlock(socket
->lock
);
1624 pthread_mutex_unlock(socket
->lock
);
1625 *discarded
+= consumer_discarded
;
1628 DBG("Consumer discarded %" PRIu64
" events in session id %" PRIu64
,
1629 *discarded
, session_id
);
1637 * Ask the consumer the number of lost packets for a channel.
1639 int consumer_get_lost_packets(uint64_t session_id
, uint64_t channel_key
,
1640 struct consumer_output
*consumer
, uint64_t *lost
)
1643 struct consumer_socket
*socket
;
1644 struct lttng_ht_iter iter
;
1645 struct lttcomm_consumer_msg msg
;
1647 LTTNG_ASSERT(consumer
);
1649 DBG3("Consumer lost packets id %" PRIu64
, session_id
);
1651 memset(&msg
, 0, sizeof(msg
));
1652 msg
.cmd_type
= LTTNG_CONSUMER_LOST_PACKETS
;
1653 msg
.u
.lost_packets
.session_id
= session_id
;
1654 msg
.u
.lost_packets
.channel_key
= channel_key
;
1658 /* Send command for each consumer */
1660 cds_lfht_for_each_entry(consumer
->socks
->ht
, &iter
.iter
, socket
,
1662 uint64_t consumer_lost
= 0;
1663 pthread_mutex_lock(socket
->lock
);
1664 ret
= consumer_socket_send(socket
, &msg
, sizeof(msg
));
1666 pthread_mutex_unlock(socket
->lock
);
1671 * No need for a recv reply status because the answer to the
1672 * command is the reply status message.
1674 ret
= consumer_socket_recv(socket
, &consumer_lost
,
1675 sizeof(consumer_lost
));
1677 ERR("get lost packets");
1678 pthread_mutex_unlock(socket
->lock
);
1681 pthread_mutex_unlock(socket
->lock
);
1682 *lost
+= consumer_lost
;
1685 DBG("Consumer lost %" PRIu64
" packets in session id %" PRIu64
,
1694 * Ask the consumer to rotate a channel.
1696 * The new_chunk_id is the session->rotate_count that has been incremented
1697 * when the rotation started. On the relay, this allows to keep track in which
1698 * chunk each stream is currently writing to (for the rotate_pending operation).
1700 int consumer_rotate_channel(struct consumer_socket
*socket
, uint64_t key
,
1701 struct consumer_output
*output
,
1702 bool is_metadata_channel
)
1705 struct lttcomm_consumer_msg msg
;
1707 LTTNG_ASSERT(socket
);
1709 DBG("Consumer rotate channel key %" PRIu64
, key
);
1711 pthread_mutex_lock(socket
->lock
);
1712 memset(&msg
, 0, sizeof(msg
));
1713 msg
.cmd_type
= LTTNG_CONSUMER_ROTATE_CHANNEL
;
1714 msg
.u
.rotate_channel
.key
= key
;
1715 msg
.u
.rotate_channel
.metadata
= !!is_metadata_channel
;
1717 if (output
->type
== CONSUMER_DST_NET
) {
1718 msg
.u
.rotate_channel
.relayd_id
= output
->net_seq_index
;
1720 msg
.u
.rotate_channel
.relayd_id
= (uint64_t) -1ULL;
1723 health_code_update();
1724 ret
= consumer_send_msg(socket
, &msg
);
1727 case LTTCOMM_CONSUMERD_CHAN_NOT_FOUND
:
1728 ret
= -LTTNG_ERR_CHAN_NOT_FOUND
;
1731 ret
= -LTTNG_ERR_ROTATION_FAIL_CONSUMER
;
1737 pthread_mutex_unlock(socket
->lock
);
1738 health_code_update();
1742 int consumer_open_channel_packets(struct consumer_socket
*socket
, uint64_t key
)
1745 lttcomm_consumer_msg msg
= {
1746 .cmd_type
= LTTNG_CONSUMER_OPEN_CHANNEL_PACKETS
,
1749 msg
.u
.open_channel_packets
.key
= key
;
1751 LTTNG_ASSERT(socket
);
1753 DBG("Consumer open channel packets: channel key = %" PRIu64
, key
);
1755 health_code_update();
1757 pthread_mutex_lock(socket
->lock
);
1758 ret
= consumer_send_msg(socket
, &msg
);
1759 pthread_mutex_unlock(socket
->lock
);
1765 health_code_update();
1769 int consumer_clear_channel(struct consumer_socket
*socket
, uint64_t key
)
1772 struct lttcomm_consumer_msg msg
;
1774 LTTNG_ASSERT(socket
);
1776 DBG("Consumer clear channel %" PRIu64
, key
);
1778 memset(&msg
, 0, sizeof(msg
));
1779 msg
.cmd_type
= LTTNG_CONSUMER_CLEAR_CHANNEL
;
1780 msg
.u
.clear_channel
.key
= key
;
1782 health_code_update();
1784 pthread_mutex_lock(socket
->lock
);
1785 ret
= consumer_send_msg(socket
, &msg
);
1791 pthread_mutex_unlock(socket
->lock
);
1793 health_code_update();
1797 int consumer_init(struct consumer_socket
*socket
,
1798 const lttng_uuid
& sessiond_uuid
)
1801 struct lttcomm_consumer_msg msg
= {
1802 .cmd_type
= LTTNG_CONSUMER_INIT
,
1806 LTTNG_ASSERT(socket
);
1808 DBG("Sending consumer initialization command");
1809 std::copy(sessiond_uuid
.begin(), sessiond_uuid
.end(), msg
.u
.init
.sessiond_uuid
);
1811 health_code_update();
1812 ret
= consumer_send_msg(socket
, &msg
);
1818 health_code_update();
1823 * Ask the consumer to create a new chunk for a given session.
1825 * Called with the consumer socket lock held.
1827 int consumer_create_trace_chunk(struct consumer_socket
*socket
,
1828 uint64_t relayd_id
, uint64_t session_id
,
1829 struct lttng_trace_chunk
*chunk
,
1830 const char *domain_subdir
)
1833 enum lttng_trace_chunk_status chunk_status
;
1834 struct lttng_credentials chunk_credentials
;
1835 const struct lttng_directory_handle
*chunk_directory_handle
= NULL
;
1836 struct lttng_directory_handle
*domain_handle
= NULL
;
1838 const char *chunk_name
;
1839 bool chunk_name_overridden
;
1841 time_t creation_timestamp
;
1842 char creation_timestamp_buffer
[ISO8601_STR_LEN
];
1843 const char *creation_timestamp_str
= "(none)";
1844 const bool chunk_has_local_output
= relayd_id
== -1ULL;
1845 enum lttng_trace_chunk_status tc_status
;
1846 struct lttcomm_consumer_msg msg
= {
1847 .cmd_type
= LTTNG_CONSUMER_CREATE_TRACE_CHUNK
,
1850 msg
.u
.create_trace_chunk
.session_id
= session_id
;
1852 LTTNG_ASSERT(socket
);
1853 LTTNG_ASSERT(chunk
);
1855 if (relayd_id
!= -1ULL) {
1856 LTTNG_OPTIONAL_SET(&msg
.u
.create_trace_chunk
.relayd_id
,
1860 chunk_status
= lttng_trace_chunk_get_name(chunk
, &chunk_name
,
1861 &chunk_name_overridden
);
1862 if (chunk_status
!= LTTNG_TRACE_CHUNK_STATUS_OK
&&
1863 chunk_status
!= LTTNG_TRACE_CHUNK_STATUS_NONE
) {
1864 ERR("Failed to get name of trace chunk");
1865 ret
= -LTTNG_ERR_FATAL
;
1868 if (chunk_name_overridden
) {
1869 ret
= lttng_strncpy(msg
.u
.create_trace_chunk
.override_name
,
1871 sizeof(msg
.u
.create_trace_chunk
.override_name
));
1873 ERR("Trace chunk name \"%s\" exceeds the maximal length allowed by the consumer protocol",
1875 ret
= -LTTNG_ERR_FATAL
;
1880 chunk_status
= lttng_trace_chunk_get_creation_timestamp(chunk
,
1881 &creation_timestamp
);
1882 if (chunk_status
!= LTTNG_TRACE_CHUNK_STATUS_OK
) {
1883 ret
= -LTTNG_ERR_FATAL
;
1886 msg
.u
.create_trace_chunk
.creation_timestamp
=
1887 (uint64_t) creation_timestamp
;
1888 /* Only used for logging purposes. */
1889 ret
= time_to_iso8601_str(creation_timestamp
,
1890 creation_timestamp_buffer
,
1891 sizeof(creation_timestamp_buffer
));
1892 creation_timestamp_str
= !ret
? creation_timestamp_buffer
:
1893 "(formatting error)";
1895 chunk_status
= lttng_trace_chunk_get_id(chunk
, &chunk_id
);
1896 if (chunk_status
!= LTTNG_TRACE_CHUNK_STATUS_OK
) {
1898 * Anonymous trace chunks should never be transmitted
1899 * to remote peers (consumerd and relayd). They are used
1900 * internally for backward-compatibility purposes.
1902 ret
= -LTTNG_ERR_FATAL
;
1905 msg
.u
.create_trace_chunk
.chunk_id
= chunk_id
;
1907 if (chunk_has_local_output
) {
1908 chunk_status
= lttng_trace_chunk_borrow_chunk_directory_handle(
1909 chunk
, &chunk_directory_handle
);
1910 if (chunk_status
!= LTTNG_TRACE_CHUNK_STATUS_OK
) {
1911 ret
= -LTTNG_ERR_FATAL
;
1914 chunk_status
= lttng_trace_chunk_get_credentials(
1915 chunk
, &chunk_credentials
);
1916 if (chunk_status
!= LTTNG_TRACE_CHUNK_STATUS_OK
) {
1918 * Not associating credentials to a sessiond chunk is a
1919 * fatal internal error.
1921 ret
= -LTTNG_ERR_FATAL
;
1924 tc_status
= lttng_trace_chunk_create_subdirectory(
1925 chunk
, domain_subdir
);
1926 if (tc_status
!= LTTNG_TRACE_CHUNK_STATUS_OK
) {
1927 PERROR("Failed to create chunk domain output directory \"%s\"",
1929 ret
= -LTTNG_ERR_FATAL
;
1932 domain_handle
= lttng_directory_handle_create_from_handle(
1934 chunk_directory_handle
);
1935 if (!domain_handle
) {
1936 ret
= -LTTNG_ERR_FATAL
;
1941 * This will only compile on platforms that support
1942 * dirfd (POSIX.2008). This is fine as the session daemon
1943 * is only built for such platforms.
1945 * The ownership of the chunk directory handle's is maintained
1946 * by the trace chunk.
1948 domain_dirfd
= lttng_directory_handle_get_dirfd(
1950 LTTNG_ASSERT(domain_dirfd
>= 0);
1952 msg
.u
.create_trace_chunk
.credentials
.value
.uid
=
1953 lttng_credentials_get_uid(&chunk_credentials
);
1954 msg
.u
.create_trace_chunk
.credentials
.value
.gid
=
1955 lttng_credentials_get_gid(&chunk_credentials
);
1956 msg
.u
.create_trace_chunk
.credentials
.is_set
= 1;
1959 DBG("Sending consumer create trace chunk command: relayd_id = %" PRId64
1960 ", session_id = %" PRIu64
", chunk_id = %" PRIu64
1961 ", creation_timestamp = %s",
1962 relayd_id
, session_id
, chunk_id
,
1963 creation_timestamp_str
);
1964 health_code_update();
1965 ret
= consumer_send_msg(socket
, &msg
);
1966 health_code_update();
1968 ERR("Trace chunk creation error on consumer");
1969 ret
= -LTTNG_ERR_CREATE_TRACE_CHUNK_FAIL_CONSUMER
;
1973 if (chunk_has_local_output
) {
1974 DBG("Sending trace chunk domain directory fd to consumer");
1975 health_code_update();
1976 ret
= consumer_send_fds(socket
, &domain_dirfd
, 1);
1977 health_code_update();
1979 ERR("Trace chunk creation error on consumer");
1980 ret
= -LTTNG_ERR_CREATE_TRACE_CHUNK_FAIL_CONSUMER
;
1985 lttng_directory_handle_put(domain_handle
);
1990 * Ask the consumer to close a trace chunk for a given session.
1992 * Called with the consumer socket lock held.
1994 int consumer_close_trace_chunk(struct consumer_socket
*socket
,
1995 uint64_t relayd_id
, uint64_t session_id
,
1996 struct lttng_trace_chunk
*chunk
,
1997 char *closed_trace_chunk_path
)
2000 enum lttng_trace_chunk_status chunk_status
;
2001 lttcomm_consumer_msg msg
= {
2002 .cmd_type
= LTTNG_CONSUMER_CLOSE_TRACE_CHUNK
,
2005 msg
.u
.close_trace_chunk
.session_id
= session_id
;
2007 struct lttcomm_consumer_close_trace_chunk_reply reply
;
2009 time_t close_timestamp
;
2010 enum lttng_trace_chunk_command_type close_command
;
2011 const char *close_command_name
= "none";
2012 struct lttng_dynamic_buffer path_reception_buffer
;
2014 LTTNG_ASSERT(socket
);
2015 lttng_dynamic_buffer_init(&path_reception_buffer
);
2017 if (relayd_id
!= -1ULL) {
2019 &msg
.u
.close_trace_chunk
.relayd_id
, relayd_id
);
2022 chunk_status
= lttng_trace_chunk_get_close_command(
2023 chunk
, &close_command
);
2024 switch (chunk_status
) {
2025 case LTTNG_TRACE_CHUNK_STATUS_OK
:
2026 LTTNG_OPTIONAL_SET(&msg
.u
.close_trace_chunk
.close_command
,
2027 (uint32_t) close_command
);
2029 case LTTNG_TRACE_CHUNK_STATUS_NONE
:
2032 ERR("Failed to get trace chunk close command");
2037 chunk_status
= lttng_trace_chunk_get_id(chunk
, &chunk_id
);
2039 * Anonymous trace chunks should never be transmitted to remote peers
2040 * (consumerd and relayd). They are used internally for
2041 * backward-compatibility purposes.
2043 LTTNG_ASSERT(chunk_status
== LTTNG_TRACE_CHUNK_STATUS_OK
);
2044 msg
.u
.close_trace_chunk
.chunk_id
= chunk_id
;
2046 chunk_status
= lttng_trace_chunk_get_close_timestamp(chunk
,
2049 * A trace chunk should be closed locally before being closed remotely.
2050 * Otherwise, the close timestamp would never be transmitted to the
2053 LTTNG_ASSERT(chunk_status
== LTTNG_TRACE_CHUNK_STATUS_OK
);
2054 msg
.u
.close_trace_chunk
.close_timestamp
= (uint64_t) close_timestamp
;
2056 if (msg
.u
.close_trace_chunk
.close_command
.is_set
) {
2057 close_command_name
= lttng_trace_chunk_command_type_get_name(
2060 DBG("Sending consumer close trace chunk command: relayd_id = %" PRId64
2061 ", session_id = %" PRIu64
", chunk_id = %" PRIu64
2062 ", close command = \"%s\"",
2063 relayd_id
, session_id
, chunk_id
, close_command_name
);
2065 health_code_update();
2066 ret
= consumer_socket_send(socket
, &msg
, sizeof(struct lttcomm_consumer_msg
));
2068 ret
= -LTTNG_ERR_CLOSE_TRACE_CHUNK_FAIL_CONSUMER
;
2071 ret
= consumer_socket_recv(socket
, &reply
, sizeof(reply
));
2073 ret
= -LTTNG_ERR_CLOSE_TRACE_CHUNK_FAIL_CONSUMER
;
2076 if (reply
.path_length
>= LTTNG_PATH_MAX
) {
2077 ERR("Invalid path returned by relay daemon: %" PRIu32
"bytes exceeds maximal allowed length of %d bytes",
2078 reply
.path_length
, LTTNG_PATH_MAX
);
2079 ret
= -LTTNG_ERR_INVALID_PROTOCOL
;
2082 ret
= lttng_dynamic_buffer_set_size(&path_reception_buffer
,
2085 ERR("Failed to allocate reception buffer of path returned by the \"close trace chunk\" command");
2086 ret
= -LTTNG_ERR_NOMEM
;
2089 ret
= consumer_socket_recv(socket
, path_reception_buffer
.data
,
2090 path_reception_buffer
.size
);
2092 ERR("Communication error while receiving path of closed trace chunk");
2093 ret
= -LTTNG_ERR_CLOSE_TRACE_CHUNK_FAIL_CONSUMER
;
2096 if (path_reception_buffer
.data
[path_reception_buffer
.size
- 1] != '\0') {
2097 ERR("Invalid path returned by relay daemon: not null-terminated");
2098 ret
= -LTTNG_ERR_INVALID_PROTOCOL
;
2101 if (closed_trace_chunk_path
) {
2103 * closed_trace_chunk_path is assumed to have a length >=
2106 memcpy(closed_trace_chunk_path
, path_reception_buffer
.data
,
2107 path_reception_buffer
.size
);
2110 lttng_dynamic_buffer_reset(&path_reception_buffer
);
2111 health_code_update();
2116 * Ask the consumer if a trace chunk exists.
2118 * Called with the consumer socket lock held.
2119 * Returns 0 on success, or a negative value on error.
2121 int consumer_trace_chunk_exists(struct consumer_socket
*socket
,
2122 uint64_t relayd_id
, uint64_t session_id
,
2123 struct lttng_trace_chunk
*chunk
,
2124 enum consumer_trace_chunk_exists_status
*result
)
2127 enum lttng_trace_chunk_status chunk_status
;
2128 lttcomm_consumer_msg msg
= {
2129 .cmd_type
= LTTNG_CONSUMER_TRACE_CHUNK_EXISTS
,
2132 msg
.u
.trace_chunk_exists
.session_id
= session_id
;
2135 const char *consumer_reply_str
;
2137 LTTNG_ASSERT(socket
);
2139 if (relayd_id
!= -1ULL) {
2140 LTTNG_OPTIONAL_SET(&msg
.u
.trace_chunk_exists
.relayd_id
,
2144 chunk_status
= lttng_trace_chunk_get_id(chunk
, &chunk_id
);
2145 if (chunk_status
!= LTTNG_TRACE_CHUNK_STATUS_OK
) {
2147 * Anonymous trace chunks should never be transmitted
2148 * to remote peers (consumerd and relayd). They are used
2149 * internally for backward-compatibility purposes.
2151 ret
= -LTTNG_ERR_FATAL
;
2154 msg
.u
.trace_chunk_exists
.chunk_id
= chunk_id
;
2156 DBG("Sending consumer trace chunk exists command: relayd_id = %" PRId64
2157 ", session_id = %" PRIu64
2158 ", chunk_id = %" PRIu64
, relayd_id
, session_id
, chunk_id
);
2160 health_code_update();
2161 ret
= consumer_send_msg(socket
, &msg
);
2163 case LTTCOMM_CONSUMERD_UNKNOWN_TRACE_CHUNK
:
2164 consumer_reply_str
= "unknown trace chunk";
2165 *result
= CONSUMER_TRACE_CHUNK_EXISTS_STATUS_UNKNOWN_CHUNK
;
2167 case LTTCOMM_CONSUMERD_TRACE_CHUNK_EXISTS_LOCAL
:
2168 consumer_reply_str
= "trace chunk exists locally";
2169 *result
= CONSUMER_TRACE_CHUNK_EXISTS_STATUS_EXISTS_LOCAL
;
2171 case LTTCOMM_CONSUMERD_TRACE_CHUNK_EXISTS_REMOTE
:
2172 consumer_reply_str
= "trace chunk exists on remote peer";
2173 *result
= CONSUMER_TRACE_CHUNK_EXISTS_STATUS_EXISTS_REMOTE
;
2176 ERR("Consumer returned an error from TRACE_CHUNK_EXISTS command");
2180 DBG("Consumer reply to TRACE_CHUNK_EXISTS command: %s",
2181 consumer_reply_str
);
2184 health_code_update();