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.hpp"
11 #include "health-sessiond.hpp"
12 #include "lttng-sessiond.hpp"
13 #include "ust-app.hpp"
16 #include <common/common.hpp>
17 #include <common/defaults.hpp>
18 #include <common/relayd/relayd.hpp>
19 #include <common/string-utils/format.hpp>
20 #include <common/uri.hpp>
27 #include <sys/types.h>
31 * Return allocated full pathname of the session using the consumer trace path
32 * and subdir if available.
34 * The caller can safely free(3) the returned value. On error, NULL is
37 char *setup_channel_trace_path(struct consumer_output
*consumer
,
38 const char *session_path
,
39 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
&& consumer
->relay_major_version
== 2 &&
60 consumer
->relay_minor_version
< 11) {
61 ret
= snprintf(pathname
,
64 consumer
->dst
.net
.base_dir
,
66 consumer
->domain_subdir
,
68 *consumer_path_offset
= 0;
71 pathname
, LTTNG_PATH_MAX
, "%s/%s", consumer
->domain_subdir
, session_path
);
72 *consumer_path_offset
= strlen(consumer
->domain_subdir
) + 1;
74 DBG3("Consumer trace path relative to current trace chunk: \"%s\"", pathname
);
76 PERROR("Failed to format channel path");
78 } else if (ret
>= LTTNG_PATH_MAX
) {
79 ERR("Truncation occurred while formatting channel path");
90 * Send a data payload using a given consumer socket of size len.
92 * The consumer socket lock MUST be acquired before calling this since this
93 * function can change the fd value.
95 * Return 0 on success else a negative value on error.
97 int consumer_socket_send(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
,
218 unsigned int *stream_count
)
221 struct lttcomm_consumer_status_channel reply
;
224 LTTNG_ASSERT(stream_count
);
227 ret
= consumer_socket_recv(sock
, &reply
, sizeof(reply
));
232 /* An error is possible so don't touch the key and stream_count. */
233 if (reply
.ret_code
!= LTTCOMM_CONSUMERD_SUCCESS
) {
239 *stream_count
= reply
.stream_count
;
247 * Send destroy relayd command to consumer.
249 * On success return positive value. On error, negative value.
251 int consumer_send_destroy_relayd(struct consumer_socket
*sock
, 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
, node
.node
) {
298 /* Send destroy relayd command */
299 ret
= consumer_send_destroy_relayd(socket
, consumer
);
301 DBG("Unable to send destroy relayd command to consumer");
302 /* Continue since we MUST delete everything at this point. */
310 * From a consumer_data structure, allocate and add a consumer socket to the
313 * Return 0 on success, else negative value on error
315 int consumer_create_socket(struct consumer_data
*data
, struct consumer_output
*output
)
318 struct consumer_socket
*socket
;
322 if (output
== nullptr || data
->cmd_sock
< 0) {
324 * Not an error. Possible there is simply not spawned consumer or it's
325 * disabled for the tracing session asking the socket.
331 socket
= consumer_find_socket(data
->cmd_sock
, output
);
333 if (socket
== nullptr) {
334 socket
= consumer_allocate_socket(&data
->cmd_sock
);
335 if (socket
== nullptr) {
340 socket
->registered
= 0;
341 socket
->lock
= &data
->lock
;
343 consumer_add_socket(socket
, output
);
347 socket
->type
= data
->type
;
349 DBG3("Consumer socket created (fd: %d) and added to output", data
->cmd_sock
);
356 * Return the consumer socket from the given consumer output with the right
357 * bitness. On error, returns NULL.
359 * The caller MUST acquire a rcu read side lock and keep it until the socket
360 * object reference is not needed anymore.
362 struct consumer_socket
*consumer_find_socket_by_bitness(int bits
,
363 const struct consumer_output
*consumer
)
366 struct consumer_socket
*socket
= nullptr;
368 ASSERT_RCU_READ_LOCKED();
372 consumer_fd
= uatomic_read(&the_ust_consumerd64_fd
);
375 consumer_fd
= uatomic_read(&the_ust_consumerd32_fd
);
382 socket
= consumer_find_socket(consumer_fd
, consumer
);
384 ERR("Consumer socket fd %d not found in consumer obj %p", consumer_fd
, consumer
);
392 * Find a consumer_socket in a consumer_output hashtable. Read side lock must
393 * be acquired before calling this function and across use of the
394 * returned consumer_socket.
396 struct consumer_socket
*consumer_find_socket(int key
, const struct consumer_output
*consumer
)
398 struct lttng_ht_iter iter
;
399 struct lttng_ht_node_ulong
*node
;
400 struct consumer_socket
*socket
= nullptr;
402 ASSERT_RCU_READ_LOCKED();
404 /* Negative keys are lookup failures */
405 if (key
< 0 || consumer
== nullptr) {
409 lttng_ht_lookup(consumer
->socks
, (void *) ((unsigned long) key
), &iter
);
410 node
= lttng_ht_iter_get_node_ulong(&iter
);
411 if (node
!= nullptr) {
412 socket
= lttng::utils::container_of(node
, &consumer_socket::node
);
419 * Allocate a new consumer_socket and return the pointer.
421 struct consumer_socket
*consumer_allocate_socket(int *fd
)
423 struct consumer_socket
*socket
= nullptr;
427 socket
= zmalloc
<consumer_socket
>();
428 if (socket
== nullptr) {
429 PERROR("zmalloc consumer socket");
434 lttng_ht_node_init_ulong(&socket
->node
, *fd
);
441 * Add consumer socket to consumer output object. Read side lock must be
442 * acquired before calling this function.
444 void consumer_add_socket(struct consumer_socket
*sock
, struct consumer_output
*consumer
)
447 LTTNG_ASSERT(consumer
);
448 ASSERT_RCU_READ_LOCKED();
450 lttng_ht_add_unique_ulong(consumer
->socks
, &sock
->node
);
454 * Delete consumer socket to consumer output object. Read side lock must be
455 * acquired before calling this function.
457 void consumer_del_socket(struct consumer_socket
*sock
, struct consumer_output
*consumer
)
460 struct lttng_ht_iter iter
;
463 LTTNG_ASSERT(consumer
);
464 ASSERT_RCU_READ_LOCKED();
466 iter
.iter
.node
= &sock
->node
.node
;
467 ret
= lttng_ht_del(consumer
->socks
, &iter
);
472 * RCU destroy call function.
474 static void destroy_socket_rcu(struct rcu_head
*head
)
476 struct lttng_ht_node_ulong
*node
=
477 lttng::utils::container_of(head
, <tng_ht_node_ulong::head
);
478 struct consumer_socket
*socket
= lttng::utils::container_of(node
, &consumer_socket::node
);
484 * Destroy and free socket pointer in a call RCU. The call must either:
485 * - have acquired the read side lock before calling this function, or
486 * - guarantee the validity of the `struct consumer_socket` object for the
487 * duration of the call.
489 void consumer_destroy_socket(struct consumer_socket
*sock
)
494 * We DO NOT close the file descriptor here since it is global to the
495 * session daemon and is closed only if the consumer dies or a custom
496 * consumer was registered,
498 if (sock
->registered
) {
499 DBG3("Consumer socket was registered. Closing fd %d", *sock
->fd_ptr
);
500 lttcomm_close_unix_sock(*sock
->fd_ptr
);
503 call_rcu(&sock
->node
.head
, destroy_socket_rcu
);
507 * Allocate and assign data to a consumer_output object.
509 * Return pointer to structure.
511 struct consumer_output
*consumer_create_output(enum consumer_dst_type type
)
513 struct consumer_output
*output
= nullptr;
515 output
= zmalloc
<consumer_output
>();
516 if (output
== nullptr) {
517 PERROR("zmalloc consumer_output");
521 /* By default, consumer output is enabled */
524 output
->net_seq_index
= (uint64_t) -1ULL;
525 urcu_ref_init(&output
->ref
);
527 output
->socks
= lttng_ht_new(0, LTTNG_HT_TYPE_ULONG
);
534 * Iterate over the consumer output socket hash table and destroy them. The
535 * socket file descriptor are only closed if the consumer output was
536 * registered meaning it's an external consumer.
538 void consumer_destroy_output_sockets(struct consumer_output
*obj
)
540 struct lttng_ht_iter iter
;
541 struct consumer_socket
*socket
;
548 cds_lfht_for_each_entry (obj
->socks
->ht
, &iter
.iter
, socket
, node
.node
) {
549 consumer_del_socket(socket
, obj
);
550 consumer_destroy_socket(socket
);
556 * Delete the consumer_output object from the list and free the ptr.
558 static void consumer_release_output(struct urcu_ref
*ref
)
560 struct consumer_output
*obj
= lttng::utils::container_of(ref
, &consumer_output::ref
);
562 consumer_destroy_output_sockets(obj
);
565 /* Finally destroy HT */
566 lttng_ht_destroy(obj
->socks
);
573 * Get the consumer_output object.
575 void consumer_output_get(struct consumer_output
*obj
)
577 urcu_ref_get(&obj
->ref
);
581 * Put the consumer_output object.
583 void consumer_output_put(struct consumer_output
*obj
)
588 urcu_ref_put(&obj
->ref
, consumer_release_output
);
592 * Copy consumer output and returned the newly allocated copy.
594 struct consumer_output
*consumer_copy_output(struct consumer_output
*src
)
597 struct consumer_output
*output
;
601 output
= consumer_create_output(src
->type
);
602 if (output
== nullptr) {
605 output
->enabled
= src
->enabled
;
606 output
->net_seq_index
= src
->net_seq_index
;
607 memcpy(output
->domain_subdir
, src
->domain_subdir
, sizeof(output
->domain_subdir
));
608 output
->snapshot
= src
->snapshot
;
609 output
->relay_major_version
= src
->relay_major_version
;
610 output
->relay_minor_version
= src
->relay_minor_version
;
611 output
->relay_allows_clear
= src
->relay_allows_clear
;
612 memcpy(&output
->dst
, &src
->dst
, sizeof(output
->dst
));
613 ret
= consumer_copy_sockets(output
, src
);
621 consumer_output_put(output
);
626 * Copy consumer sockets from src to dst.
628 * Return 0 on success or else a negative value.
630 int consumer_copy_sockets(struct consumer_output
*dst
, struct consumer_output
*src
)
633 struct lttng_ht_iter iter
;
634 struct consumer_socket
*socket
, *copy_sock
;
640 cds_lfht_for_each_entry (src
->socks
->ht
, &iter
.iter
, socket
, node
.node
) {
641 /* Ignore socket that are already there. */
642 copy_sock
= consumer_find_socket(*socket
->fd_ptr
, dst
);
647 /* Create new socket object. */
648 copy_sock
= consumer_allocate_socket(socket
->fd_ptr
);
649 if (copy_sock
== nullptr) {
655 copy_sock
->registered
= socket
->registered
;
657 * This is valid because this lock is shared accross all consumer
658 * object being the global lock of the consumer data structure of the
661 copy_sock
->lock
= socket
->lock
;
662 consumer_add_socket(copy_sock
, dst
);
671 * Set network URI to the consumer output.
673 * Return 0 on success. Return 1 if the URI were equal. Else, negative value on
676 int consumer_set_network_uri(const struct ltt_session
*session
,
677 struct consumer_output
*output
,
678 struct lttng_uri
*uri
)
681 struct lttng_uri
*dst_uri
= nullptr;
683 /* Code flow error safety net. */
684 LTTNG_ASSERT(output
);
687 switch (uri
->stype
) {
688 case LTTNG_STREAM_CONTROL
:
689 dst_uri
= &output
->dst
.net
.control
;
690 output
->dst
.net
.control_isset
= 1;
691 if (uri
->port
== 0) {
692 /* Assign default port. */
693 uri
->port
= DEFAULT_NETWORK_CONTROL_PORT
;
695 if (output
->dst
.net
.data_isset
&& uri
->port
== output
->dst
.net
.data
.port
) {
696 ret
= -LTTNG_ERR_INVALID
;
700 DBG3("Consumer control URI set with port %d", uri
->port
);
702 case LTTNG_STREAM_DATA
:
703 dst_uri
= &output
->dst
.net
.data
;
704 output
->dst
.net
.data_isset
= 1;
705 if (uri
->port
== 0) {
706 /* Assign default port. */
707 uri
->port
= DEFAULT_NETWORK_DATA_PORT
;
709 if (output
->dst
.net
.control_isset
&&
710 uri
->port
== output
->dst
.net
.control
.port
) {
711 ret
= -LTTNG_ERR_INVALID
;
715 DBG3("Consumer data URI set with port %d", uri
->port
);
718 ERR("Set network uri type unknown %d", uri
->stype
);
719 ret
= -LTTNG_ERR_INVALID
;
723 ret
= uri_compare(dst_uri
, uri
);
725 /* Same URI, don't touch it and return success. */
726 DBG3("URI network compare are the same");
730 /* URIs were not equal, replacing it. */
731 memcpy(dst_uri
, uri
, sizeof(struct lttng_uri
));
732 output
->type
= CONSUMER_DST_NET
;
733 if (dst_uri
->stype
!= LTTNG_STREAM_CONTROL
) {
734 /* Only the control uri needs to contain the path. */
739 * If the user has specified a subdir as part of the control
740 * URL, the session's base output directory is:
741 * /RELAYD_OUTPUT_PATH/HOSTNAME/USER_SPECIFIED_DIR
743 * Hence, the "base_dir" from which all stream files and
744 * session rotation chunks are created takes the form
745 * /HOSTNAME/USER_SPECIFIED_DIR
747 * If the user has not specified an output directory as part of
748 * the control URL, the base output directory has the form:
749 * /RELAYD_OUTPUT_PATH/HOSTNAME/SESSION_NAME-CREATION_TIME
751 * Hence, the "base_dir" from which all stream files and
752 * session rotation chunks are created takes the form
753 * /HOSTNAME/SESSION_NAME-CREATION_TIME
755 * Note that automatically generated session names already
756 * contain the session's creation time. In that case, the
757 * creation time is omitted to prevent it from being duplicated
758 * in the final directory hierarchy.
761 if (strstr(uri
->subdir
, "../")) {
762 ERR("Network URI subdirs are not allowed to walk up the path hierarchy");
763 ret
= -LTTNG_ERR_INVALID
;
766 ret
= snprintf(output
->dst
.net
.base_dir
,
767 sizeof(output
->dst
.net
.base_dir
),
772 if (session
->has_auto_generated_name
) {
773 ret
= snprintf(output
->dst
.net
.base_dir
,
774 sizeof(output
->dst
.net
.base_dir
),
779 char session_creation_datetime
[16];
783 timeinfo
= localtime(&session
->creation_time
);
785 ret
= -LTTNG_ERR_FATAL
;
788 strftime_ret
= strftime(session_creation_datetime
,
789 sizeof(session_creation_datetime
),
792 if (strftime_ret
== 0) {
793 ERR("Failed to format session creation timestamp while setting network URI");
794 ret
= -LTTNG_ERR_FATAL
;
797 ret
= snprintf(output
->dst
.net
.base_dir
,
798 sizeof(output
->dst
.net
.base_dir
),
802 session_creation_datetime
);
805 if (ret
>= sizeof(output
->dst
.net
.base_dir
)) {
806 ret
= -LTTNG_ERR_INVALID
;
807 ERR("Truncation occurred while setting network output base directory");
809 } else if (ret
== -1) {
810 ret
= -LTTNG_ERR_INVALID
;
811 PERROR("Error occurred while setting network output base directory");
815 DBG3("Consumer set network uri base_dir path %s", output
->dst
.net
.base_dir
);
826 * Send file descriptor to consumer via sock.
828 * The consumer socket lock must be held by the caller.
830 int consumer_send_fds(struct consumer_socket
*sock
, const int *fds
, size_t nb_fd
)
836 LTTNG_ASSERT(nb_fd
> 0);
837 LTTNG_ASSERT(pthread_mutex_trylock(sock
->lock
) == EBUSY
);
839 ret
= lttcomm_send_fds_unix_sock(*sock
->fd_ptr
, fds
, nb_fd
);
841 /* The above call will print a PERROR on error. */
842 DBG("Error when sending consumer fds on sock %d", *sock
->fd_ptr
);
846 ret
= consumer_recv_status_reply(sock
);
852 * Consumer send communication message structure to consumer.
854 * The consumer socket lock must be held by the caller.
856 int consumer_send_msg(struct consumer_socket
*sock
, const struct lttcomm_consumer_msg
*msg
)
862 LTTNG_ASSERT(pthread_mutex_trylock(sock
->lock
) == EBUSY
);
864 ret
= consumer_socket_send(sock
, msg
, sizeof(struct lttcomm_consumer_msg
));
869 ret
= consumer_recv_status_reply(sock
);
876 * Consumer send channel communication message structure to consumer.
878 * The consumer socket lock must be held by the caller.
880 int consumer_send_channel(struct consumer_socket
*sock
, struct lttcomm_consumer_msg
*msg
)
887 ret
= consumer_send_msg(sock
, msg
);
897 * Populate the given consumer msg structure with the ask_channel command
900 void consumer_init_ask_channel_comm_msg(struct lttcomm_consumer_msg
*msg
,
901 uint64_t subbuf_size
,
904 unsigned int switch_timer_interval
,
905 unsigned int read_timer_interval
,
906 unsigned int live_timer_interval
,
907 bool is_in_live_session
,
908 unsigned int monitor_timer_interval
,
912 const char *pathname
,
916 const lttng_uuid
& uuid
,
918 uint64_t tracefile_size
,
919 uint64_t tracefile_count
,
920 uint64_t session_id_per_pid
,
921 unsigned int monitor
,
922 uint32_t ust_app_uid
,
923 int64_t blocking_timeout
,
924 const char *root_shm_path
,
925 const char *shm_path
,
926 struct lttng_trace_chunk
*trace_chunk
,
927 const struct lttng_credentials
*buffer_credentials
)
931 /* Zeroed structure */
932 memset(msg
, 0, sizeof(struct lttcomm_consumer_msg
));
933 msg
->u
.ask_channel
.buffer_credentials
.uid
= UINT32_MAX
;
934 msg
->u
.ask_channel
.buffer_credentials
.gid
= UINT32_MAX
;
938 enum lttng_trace_chunk_status chunk_status
;
940 chunk_status
= lttng_trace_chunk_get_id(trace_chunk
, &chunk_id
);
941 LTTNG_ASSERT(chunk_status
== LTTNG_TRACE_CHUNK_STATUS_OK
);
942 LTTNG_OPTIONAL_SET(&msg
->u
.ask_channel
.chunk_id
, chunk_id
);
944 msg
->u
.ask_channel
.buffer_credentials
.uid
= lttng_credentials_get_uid(buffer_credentials
);
945 msg
->u
.ask_channel
.buffer_credentials
.gid
= lttng_credentials_get_gid(buffer_credentials
);
947 msg
->cmd_type
= LTTNG_CONSUMER_ASK_CHANNEL_CREATION
;
948 msg
->u
.ask_channel
.subbuf_size
= subbuf_size
;
949 msg
->u
.ask_channel
.num_subbuf
= num_subbuf
;
950 msg
->u
.ask_channel
.overwrite
= overwrite
;
951 msg
->u
.ask_channel
.switch_timer_interval
= switch_timer_interval
;
952 msg
->u
.ask_channel
.read_timer_interval
= read_timer_interval
;
953 msg
->u
.ask_channel
.live_timer_interval
= live_timer_interval
;
954 msg
->u
.ask_channel
.is_live
= is_in_live_session
;
955 msg
->u
.ask_channel
.monitor_timer_interval
= monitor_timer_interval
;
956 msg
->u
.ask_channel
.output
= output
;
957 msg
->u
.ask_channel
.type
= type
;
958 msg
->u
.ask_channel
.session_id
= session_id
;
959 msg
->u
.ask_channel
.session_id_per_pid
= session_id_per_pid
;
960 msg
->u
.ask_channel
.relayd_id
= relayd_id
;
961 msg
->u
.ask_channel
.key
= key
;
962 msg
->u
.ask_channel
.chan_id
= chan_id
;
963 msg
->u
.ask_channel
.tracefile_size
= tracefile_size
;
964 msg
->u
.ask_channel
.tracefile_count
= tracefile_count
;
965 msg
->u
.ask_channel
.monitor
= monitor
;
966 msg
->u
.ask_channel
.ust_app_uid
= ust_app_uid
;
967 msg
->u
.ask_channel
.blocking_timeout
= blocking_timeout
;
969 std::copy(uuid
.begin(), uuid
.end(), msg
->u
.ask_channel
.uuid
);
972 strncpy(msg
->u
.ask_channel
.pathname
, pathname
, sizeof(msg
->u
.ask_channel
.pathname
));
973 msg
->u
.ask_channel
.pathname
[sizeof(msg
->u
.ask_channel
.pathname
) - 1] = '\0';
976 strncpy(msg
->u
.ask_channel
.name
, name
, sizeof(msg
->u
.ask_channel
.name
));
977 msg
->u
.ask_channel
.name
[sizeof(msg
->u
.ask_channel
.name
) - 1] = '\0';
980 strncpy(msg
->u
.ask_channel
.root_shm_path
,
982 sizeof(msg
->u
.ask_channel
.root_shm_path
));
983 msg
->u
.ask_channel
.root_shm_path
[sizeof(msg
->u
.ask_channel
.root_shm_path
) - 1] =
987 strncpy(msg
->u
.ask_channel
.shm_path
, shm_path
, sizeof(msg
->u
.ask_channel
.shm_path
));
988 msg
->u
.ask_channel
.shm_path
[sizeof(msg
->u
.ask_channel
.shm_path
) - 1] = '\0';
993 * Init channel communication message structure.
995 void consumer_init_add_channel_comm_msg(struct lttcomm_consumer_msg
*msg
,
996 uint64_t channel_key
,
998 const char *pathname
,
1001 unsigned int nb_init_streams
,
1002 enum lttng_event_output output
,
1004 uint64_t tracefile_size
,
1005 uint64_t tracefile_count
,
1006 unsigned int monitor
,
1007 unsigned int live_timer_interval
,
1008 bool is_in_live_session
,
1009 unsigned int monitor_timer_interval
,
1010 struct lttng_trace_chunk
*trace_chunk
)
1014 /* Zeroed structure */
1015 memset(msg
, 0, sizeof(struct lttcomm_consumer_msg
));
1019 enum lttng_trace_chunk_status chunk_status
;
1021 chunk_status
= lttng_trace_chunk_get_id(trace_chunk
, &chunk_id
);
1022 LTTNG_ASSERT(chunk_status
== LTTNG_TRACE_CHUNK_STATUS_OK
);
1023 LTTNG_OPTIONAL_SET(&msg
->u
.channel
.chunk_id
, chunk_id
);
1027 msg
->cmd_type
= LTTNG_CONSUMER_ADD_CHANNEL
;
1028 msg
->u
.channel
.channel_key
= channel_key
;
1029 msg
->u
.channel
.session_id
= session_id
;
1030 msg
->u
.channel
.relayd_id
= relayd_id
;
1031 msg
->u
.channel
.nb_init_streams
= nb_init_streams
;
1032 msg
->u
.channel
.output
= output
;
1033 msg
->u
.channel
.type
= type
;
1034 msg
->u
.channel
.tracefile_size
= tracefile_size
;
1035 msg
->u
.channel
.tracefile_count
= tracefile_count
;
1036 msg
->u
.channel
.monitor
= monitor
;
1037 msg
->u
.channel
.live_timer_interval
= live_timer_interval
;
1038 msg
->u
.channel
.is_live
= is_in_live_session
;
1039 msg
->u
.channel
.monitor_timer_interval
= monitor_timer_interval
;
1041 strncpy(msg
->u
.channel
.pathname
, pathname
, sizeof(msg
->u
.channel
.pathname
));
1042 msg
->u
.channel
.pathname
[sizeof(msg
->u
.channel
.pathname
) - 1] = '\0';
1044 strncpy(msg
->u
.channel
.name
, name
, sizeof(msg
->u
.channel
.name
));
1045 msg
->u
.channel
.name
[sizeof(msg
->u
.channel
.name
) - 1] = '\0';
1049 * Init stream communication message structure.
1051 void consumer_init_add_stream_comm_msg(struct lttcomm_consumer_msg
*msg
,
1052 uint64_t channel_key
,
1053 uint64_t stream_key
,
1058 memset(msg
, 0, sizeof(struct lttcomm_consumer_msg
));
1060 msg
->cmd_type
= LTTNG_CONSUMER_ADD_STREAM
;
1061 msg
->u
.stream
.channel_key
= channel_key
;
1062 msg
->u
.stream
.stream_key
= stream_key
;
1063 msg
->u
.stream
.cpu
= cpu
;
1066 void consumer_init_streams_sent_comm_msg(struct lttcomm_consumer_msg
*msg
,
1067 enum lttng_consumer_command cmd
,
1068 uint64_t channel_key
,
1069 uint64_t net_seq_idx
)
1073 memset(msg
, 0, sizeof(struct lttcomm_consumer_msg
));
1075 msg
->cmd_type
= cmd
;
1076 msg
->u
.sent_streams
.channel_key
= channel_key
;
1077 msg
->u
.sent_streams
.net_seq_idx
= net_seq_idx
;
1081 * Send stream communication structure to the consumer.
1083 int consumer_send_stream(struct consumer_socket
*sock
,
1084 struct consumer_output
*dst
,
1085 struct lttcomm_consumer_msg
*msg
,
1096 ret
= consumer_send_msg(sock
, msg
);
1101 ret
= consumer_send_fds(sock
, fds
, nb_fd
);
1111 * Send relayd socket to consumer associated with a session name.
1113 * The consumer socket lock must be held by the caller.
1115 * On success return positive value. On error, negative value.
1117 int consumer_send_relayd_socket(struct consumer_socket
*consumer_sock
,
1118 struct lttcomm_relayd_sock
*rsock
,
1119 struct consumer_output
*consumer
,
1120 enum lttng_stream_type type
,
1121 uint64_t session_id
,
1122 const char *session_name
,
1123 const char *hostname
,
1124 const char *base_path
,
1125 int session_live_timer
,
1126 const uint64_t *current_chunk_id
,
1127 time_t session_creation_time
,
1128 bool session_name_contains_creation_time
)
1132 struct lttcomm_consumer_msg msg
;
1134 /* Code flow error. Safety net. */
1135 LTTNG_ASSERT(rsock
);
1136 LTTNG_ASSERT(consumer
);
1137 LTTNG_ASSERT(consumer_sock
);
1139 memset(&msg
, 0, sizeof(msg
));
1140 /* Bail out if consumer is disabled */
1141 if (!consumer
->enabled
) {
1146 if (type
== LTTNG_STREAM_CONTROL
) {
1147 char output_path
[LTTNG_PATH_MAX
] = {};
1148 uint64_t relayd_session_id
;
1150 ret
= relayd_create_session(rsock
,
1160 session_creation_time
,
1161 session_name_contains_creation_time
,
1164 /* Close the control socket. */
1165 (void) relayd_close(rsock
);
1168 msg
.u
.relayd_sock
.relayd_session_id
= relayd_session_id
;
1169 DBG("Created session on relay, output path reply: %s", output_path
);
1172 msg
.cmd_type
= LTTNG_CONSUMER_ADD_RELAYD_SOCKET
;
1174 * Assign network consumer output index using the temporary consumer since
1175 * this call should only be made from within a set_consumer_uri() function
1176 * call in the session daemon.
1178 msg
.u
.relayd_sock
.net_index
= consumer
->net_seq_index
;
1179 msg
.u
.relayd_sock
.type
= type
;
1180 msg
.u
.relayd_sock
.session_id
= session_id
;
1181 msg
.u
.relayd_sock
.major
= rsock
->major
;
1182 msg
.u
.relayd_sock
.minor
= rsock
->minor
;
1183 msg
.u
.relayd_sock
.relayd_socket_protocol
= rsock
->sock
.proto
;
1185 DBG3("Sending relayd sock info to consumer on %d", *consumer_sock
->fd_ptr
);
1186 ret
= consumer_send_msg(consumer_sock
, &msg
);
1191 DBG3("Sending relayd socket file descriptor to consumer");
1192 fd
= rsock
->sock
.fd
;
1193 ret
= consumer_send_fds(consumer_sock
, &fd
, 1);
1198 DBG2("Consumer relayd socket sent");
1205 consumer_send_pipe(struct consumer_socket
*consumer_sock
, enum lttng_consumer_command cmd
, int pipe
)
1208 struct lttcomm_consumer_msg msg
;
1209 const char *pipe_name
;
1210 const char *command_name
;
1213 case LTTNG_CONSUMER_SET_CHANNEL_MONITOR_PIPE
:
1214 pipe_name
= "channel monitor";
1215 command_name
= "SET_CHANNEL_MONITOR_PIPE";
1218 ERR("Unexpected command received in %s (cmd = %d)", __func__
, (int) cmd
);
1222 /* Code flow error. Safety net. */
1224 memset(&msg
, 0, sizeof(msg
));
1227 pthread_mutex_lock(consumer_sock
->lock
);
1228 DBG3("Sending %s command to consumer", command_name
);
1229 ret
= consumer_send_msg(consumer_sock
, &msg
);
1234 DBG3("Sending %s pipe %d to consumer on socket %d", pipe_name
, pipe
, *consumer_sock
->fd_ptr
);
1235 ret
= consumer_send_fds(consumer_sock
, &pipe
, 1);
1240 DBG2("%s pipe successfully sent", pipe_name
);
1242 pthread_mutex_unlock(consumer_sock
->lock
);
1246 int consumer_send_channel_monitor_pipe(struct consumer_socket
*consumer_sock
, int pipe
)
1248 return consumer_send_pipe(consumer_sock
, LTTNG_CONSUMER_SET_CHANNEL_MONITOR_PIPE
, pipe
);
1252 * Ask the consumer if the data is pending for the specific session id.
1253 * Returns 1 if data is pending, 0 otherwise, or < 0 on error.
1255 int consumer_is_data_pending(uint64_t session_id
, struct consumer_output
*consumer
)
1258 int32_t ret_code
= 0; /* Default is that the data is NOT pending */
1259 struct consumer_socket
*socket
;
1260 struct lttng_ht_iter iter
;
1261 struct lttcomm_consumer_msg msg
;
1263 LTTNG_ASSERT(consumer
);
1265 DBG3("Consumer data pending for id %" PRIu64
, session_id
);
1267 memset(&msg
, 0, sizeof(msg
));
1268 msg
.cmd_type
= LTTNG_CONSUMER_DATA_PENDING
;
1269 msg
.u
.data_pending
.session_id
= session_id
;
1271 /* Send command for each consumer */
1273 cds_lfht_for_each_entry (consumer
->socks
->ht
, &iter
.iter
, socket
, node
.node
) {
1274 pthread_mutex_lock(socket
->lock
);
1275 ret
= consumer_socket_send(socket
, &msg
, sizeof(msg
));
1277 pthread_mutex_unlock(socket
->lock
);
1282 * No need for a recv reply status because the answer to the command is
1283 * the reply status message.
1286 ret
= consumer_socket_recv(socket
, &ret_code
, sizeof(ret_code
));
1288 pthread_mutex_unlock(socket
->lock
);
1291 pthread_mutex_unlock(socket
->lock
);
1293 if (ret_code
== 1) {
1299 DBG("Consumer data is %s pending for session id %" PRIu64
,
1300 ret_code
== 1 ? "" : "NOT",
1310 * Send a flush command to consumer using the given channel key.
1312 * Return 0 on success else a negative value.
1314 int consumer_flush_channel(struct consumer_socket
*socket
, uint64_t key
)
1317 struct lttcomm_consumer_msg msg
;
1319 LTTNG_ASSERT(socket
);
1321 DBG2("Consumer flush channel key %" PRIu64
, key
);
1323 memset(&msg
, 0, sizeof(msg
));
1324 msg
.cmd_type
= LTTNG_CONSUMER_FLUSH_CHANNEL
;
1325 msg
.u
.flush_channel
.key
= key
;
1327 pthread_mutex_lock(socket
->lock
);
1328 health_code_update();
1330 ret
= consumer_send_msg(socket
, &msg
);
1336 health_code_update();
1337 pthread_mutex_unlock(socket
->lock
);
1342 * Send a clear quiescent command to consumer using the given channel key.
1344 * Return 0 on success else a negative value.
1346 int consumer_clear_quiescent_channel(struct consumer_socket
*socket
, uint64_t key
)
1349 struct lttcomm_consumer_msg msg
;
1351 LTTNG_ASSERT(socket
);
1353 DBG2("Consumer clear quiescent channel key %" PRIu64
, key
);
1355 memset(&msg
, 0, sizeof(msg
));
1356 msg
.cmd_type
= LTTNG_CONSUMER_CLEAR_QUIESCENT_CHANNEL
;
1357 msg
.u
.clear_quiescent_channel
.key
= key
;
1359 pthread_mutex_lock(socket
->lock
);
1360 health_code_update();
1362 ret
= consumer_send_msg(socket
, &msg
);
1368 health_code_update();
1369 pthread_mutex_unlock(socket
->lock
);
1374 * Send a close metadata command to consumer using the given channel key.
1375 * Called with registry lock held.
1377 * Return 0 on success else a negative value.
1379 int consumer_close_metadata(struct consumer_socket
*socket
, uint64_t metadata_key
)
1382 struct lttcomm_consumer_msg msg
;
1384 LTTNG_ASSERT(socket
);
1386 DBG2("Consumer close metadata channel key %" PRIu64
, metadata_key
);
1388 memset(&msg
, 0, sizeof(msg
));
1389 msg
.cmd_type
= LTTNG_CONSUMER_CLOSE_METADATA
;
1390 msg
.u
.close_metadata
.key
= metadata_key
;
1392 pthread_mutex_lock(socket
->lock
);
1393 health_code_update();
1395 ret
= consumer_send_msg(socket
, &msg
);
1401 health_code_update();
1402 pthread_mutex_unlock(socket
->lock
);
1407 * Send a setup metdata command to consumer using the given channel key.
1409 * Return 0 on success else a negative value.
1411 int consumer_setup_metadata(struct consumer_socket
*socket
, uint64_t metadata_key
)
1414 struct lttcomm_consumer_msg msg
;
1416 LTTNG_ASSERT(socket
);
1418 DBG2("Consumer setup metadata channel key %" PRIu64
, metadata_key
);
1420 memset(&msg
, 0, sizeof(msg
));
1421 msg
.cmd_type
= LTTNG_CONSUMER_SETUP_METADATA
;
1422 msg
.u
.setup_metadata
.key
= metadata_key
;
1424 pthread_mutex_lock(socket
->lock
);
1425 health_code_update();
1427 ret
= consumer_send_msg(socket
, &msg
);
1433 health_code_update();
1434 pthread_mutex_unlock(socket
->lock
);
1439 * Send metadata string to consumer.
1440 * RCU read-side lock must be held to guarantee existence of socket.
1442 * Return 0 on success else a negative value.
1444 int consumer_push_metadata(struct consumer_socket
*socket
,
1445 uint64_t metadata_key
,
1448 size_t target_offset
,
1452 struct lttcomm_consumer_msg msg
;
1454 LTTNG_ASSERT(socket
);
1455 ASSERT_RCU_READ_LOCKED();
1457 DBG2("Consumer push metadata to consumer socket %d", *socket
->fd_ptr
);
1459 pthread_mutex_lock(socket
->lock
);
1461 memset(&msg
, 0, sizeof(msg
));
1462 msg
.cmd_type
= LTTNG_CONSUMER_PUSH_METADATA
;
1463 msg
.u
.push_metadata
.key
= metadata_key
;
1464 msg
.u
.push_metadata
.target_offset
= target_offset
;
1465 msg
.u
.push_metadata
.len
= len
;
1466 msg
.u
.push_metadata
.version
= version
;
1468 health_code_update();
1469 ret
= consumer_send_msg(socket
, &msg
);
1470 if (ret
< 0 || len
== 0) {
1474 DBG3("Consumer pushing metadata on sock %d of len %zu", *socket
->fd_ptr
, len
);
1476 ret
= consumer_socket_send(socket
, metadata_str
, len
);
1481 health_code_update();
1482 ret
= consumer_recv_status_reply(socket
);
1488 pthread_mutex_unlock(socket
->lock
);
1489 health_code_update();
1494 * Ask the consumer to snapshot a specific channel using the key.
1496 * Returns LTTNG_OK on success or else an LTTng error code.
1498 enum lttng_error_code
consumer_snapshot_channel(struct consumer_socket
*socket
,
1500 const struct consumer_output
*output
,
1502 const char *channel_path
,
1503 uint64_t nb_packets_per_stream
)
1506 enum lttng_error_code status
= LTTNG_OK
;
1507 struct lttcomm_consumer_msg msg
;
1509 LTTNG_ASSERT(socket
);
1510 LTTNG_ASSERT(output
);
1512 DBG("Consumer snapshot channel key %" PRIu64
, key
);
1514 memset(&msg
, 0, sizeof(msg
));
1515 msg
.cmd_type
= LTTNG_CONSUMER_SNAPSHOT_CHANNEL
;
1516 msg
.u
.snapshot_channel
.key
= key
;
1517 msg
.u
.snapshot_channel
.nb_packets_per_stream
= nb_packets_per_stream
;
1518 msg
.u
.snapshot_channel
.metadata
= metadata
;
1520 if (output
->type
== CONSUMER_DST_NET
) {
1521 msg
.u
.snapshot_channel
.relayd_id
= output
->net_seq_index
;
1522 msg
.u
.snapshot_channel
.use_relayd
= 1;
1524 msg
.u
.snapshot_channel
.relayd_id
= (uint64_t) -1ULL;
1526 ret
= lttng_strncpy(msg
.u
.snapshot_channel
.pathname
,
1528 sizeof(msg
.u
.snapshot_channel
.pathname
));
1530 ERR("Snapshot path exceeds the maximal allowed length of %zu bytes (%zu bytes required) with path \"%s\"",
1531 sizeof(msg
.u
.snapshot_channel
.pathname
),
1532 strlen(channel_path
),
1534 status
= LTTNG_ERR_SNAPSHOT_FAIL
;
1538 health_code_update();
1539 pthread_mutex_lock(socket
->lock
);
1540 ret
= consumer_send_msg(socket
, &msg
);
1541 pthread_mutex_unlock(socket
->lock
);
1544 case LTTCOMM_CONSUMERD_CHAN_NOT_FOUND
:
1545 status
= LTTNG_ERR_CHAN_NOT_FOUND
;
1548 status
= LTTNG_ERR_SNAPSHOT_FAIL
;
1555 health_code_update();
1560 * Ask the consumer the number of discarded events for a channel.
1562 int consumer_get_discarded_events(uint64_t session_id
,
1563 uint64_t channel_key
,
1564 struct consumer_output
*consumer
,
1565 uint64_t *discarded
)
1568 struct consumer_socket
*socket
;
1569 struct lttng_ht_iter iter
;
1570 struct lttcomm_consumer_msg msg
;
1572 LTTNG_ASSERT(consumer
);
1574 DBG3("Consumer discarded events id %" PRIu64
, session_id
);
1576 memset(&msg
, 0, sizeof(msg
));
1577 msg
.cmd_type
= LTTNG_CONSUMER_DISCARDED_EVENTS
;
1578 msg
.u
.discarded_events
.session_id
= session_id
;
1579 msg
.u
.discarded_events
.channel_key
= channel_key
;
1583 /* Send command for each consumer */
1585 cds_lfht_for_each_entry (consumer
->socks
->ht
, &iter
.iter
, socket
, node
.node
) {
1586 uint64_t consumer_discarded
= 0;
1587 pthread_mutex_lock(socket
->lock
);
1588 ret
= consumer_socket_send(socket
, &msg
, sizeof(msg
));
1590 pthread_mutex_unlock(socket
->lock
);
1595 * No need for a recv reply status because the answer to the
1596 * command is the reply status message.
1598 ret
= consumer_socket_recv(socket
, &consumer_discarded
, sizeof(consumer_discarded
));
1600 ERR("get discarded events");
1601 pthread_mutex_unlock(socket
->lock
);
1604 pthread_mutex_unlock(socket
->lock
);
1605 *discarded
+= consumer_discarded
;
1608 DBG("Consumer discarded %" PRIu64
" events in session id %" PRIu64
, *discarded
, session_id
);
1616 * Ask the consumer the number of lost packets for a channel.
1618 int consumer_get_lost_packets(uint64_t session_id
,
1619 uint64_t channel_key
,
1620 struct consumer_output
*consumer
,
1624 struct consumer_socket
*socket
;
1625 struct lttng_ht_iter iter
;
1626 struct lttcomm_consumer_msg msg
;
1628 LTTNG_ASSERT(consumer
);
1630 DBG3("Consumer lost packets id %" PRIu64
, session_id
);
1632 memset(&msg
, 0, sizeof(msg
));
1633 msg
.cmd_type
= LTTNG_CONSUMER_LOST_PACKETS
;
1634 msg
.u
.lost_packets
.session_id
= session_id
;
1635 msg
.u
.lost_packets
.channel_key
= channel_key
;
1639 /* Send command for each consumer */
1641 cds_lfht_for_each_entry (consumer
->socks
->ht
, &iter
.iter
, socket
, node
.node
) {
1642 uint64_t consumer_lost
= 0;
1643 pthread_mutex_lock(socket
->lock
);
1644 ret
= consumer_socket_send(socket
, &msg
, sizeof(msg
));
1646 pthread_mutex_unlock(socket
->lock
);
1651 * No need for a recv reply status because the answer to the
1652 * command is the reply status message.
1654 ret
= consumer_socket_recv(socket
, &consumer_lost
, sizeof(consumer_lost
));
1656 ERR("get lost packets");
1657 pthread_mutex_unlock(socket
->lock
);
1660 pthread_mutex_unlock(socket
->lock
);
1661 *lost
+= consumer_lost
;
1664 DBG("Consumer lost %" PRIu64
" packets in session id %" PRIu64
, *lost
, session_id
);
1672 * Ask the consumer to rotate a channel.
1674 * The new_chunk_id is the session->rotate_count that has been incremented
1675 * when the rotation started. On the relay, this allows to keep track in which
1676 * chunk each stream is currently writing to (for the rotate_pending operation).
1678 int consumer_rotate_channel(struct consumer_socket
*socket
,
1680 struct consumer_output
*output
,
1681 bool is_metadata_channel
)
1684 struct lttcomm_consumer_msg msg
;
1686 LTTNG_ASSERT(socket
);
1688 DBG("Consumer rotate channel key %" PRIu64
, key
);
1690 pthread_mutex_lock(socket
->lock
);
1691 memset(&msg
, 0, sizeof(msg
));
1692 msg
.cmd_type
= LTTNG_CONSUMER_ROTATE_CHANNEL
;
1693 msg
.u
.rotate_channel
.key
= key
;
1694 msg
.u
.rotate_channel
.metadata
= !!is_metadata_channel
;
1696 if (output
->type
== CONSUMER_DST_NET
) {
1697 msg
.u
.rotate_channel
.relayd_id
= output
->net_seq_index
;
1699 msg
.u
.rotate_channel
.relayd_id
= (uint64_t) -1ULL;
1702 health_code_update();
1703 ret
= consumer_send_msg(socket
, &msg
);
1706 case LTTCOMM_CONSUMERD_CHAN_NOT_FOUND
:
1707 ret
= -LTTNG_ERR_CHAN_NOT_FOUND
;
1710 ret
= -LTTNG_ERR_ROTATION_FAIL_CONSUMER
;
1716 pthread_mutex_unlock(socket
->lock
);
1717 health_code_update();
1721 int consumer_open_channel_packets(struct consumer_socket
*socket
, uint64_t key
)
1724 lttcomm_consumer_msg msg
= {
1725 .cmd_type
= LTTNG_CONSUMER_OPEN_CHANNEL_PACKETS
,
1728 msg
.u
.open_channel_packets
.key
= key
;
1730 LTTNG_ASSERT(socket
);
1732 DBG("Consumer open channel packets: channel key = %" PRIu64
, key
);
1734 health_code_update();
1736 pthread_mutex_lock(socket
->lock
);
1737 ret
= consumer_send_msg(socket
, &msg
);
1738 pthread_mutex_unlock(socket
->lock
);
1744 health_code_update();
1748 int consumer_clear_channel(struct consumer_socket
*socket
, uint64_t key
)
1751 struct lttcomm_consumer_msg msg
;
1753 LTTNG_ASSERT(socket
);
1755 DBG("Consumer clear channel %" PRIu64
, key
);
1757 memset(&msg
, 0, sizeof(msg
));
1758 msg
.cmd_type
= LTTNG_CONSUMER_CLEAR_CHANNEL
;
1759 msg
.u
.clear_channel
.key
= key
;
1761 health_code_update();
1763 pthread_mutex_lock(socket
->lock
);
1764 ret
= consumer_send_msg(socket
, &msg
);
1770 pthread_mutex_unlock(socket
->lock
);
1772 health_code_update();
1776 int consumer_init(struct consumer_socket
*socket
, const lttng_uuid
& sessiond_uuid
)
1779 struct lttcomm_consumer_msg msg
= {
1780 .cmd_type
= LTTNG_CONSUMER_INIT
,
1784 LTTNG_ASSERT(socket
);
1786 DBG("Sending consumer initialization command");
1787 std::copy(sessiond_uuid
.begin(), sessiond_uuid
.end(), msg
.u
.init
.sessiond_uuid
);
1789 health_code_update();
1790 ret
= consumer_send_msg(socket
, &msg
);
1796 health_code_update();
1801 * Ask the consumer to create a new chunk for a given session.
1803 * Called with the consumer socket lock held.
1805 int consumer_create_trace_chunk(struct consumer_socket
*socket
,
1807 uint64_t session_id
,
1808 struct lttng_trace_chunk
*chunk
,
1809 const char *domain_subdir
)
1812 enum lttng_trace_chunk_status chunk_status
;
1813 struct lttng_credentials chunk_credentials
;
1814 const struct lttng_directory_handle
*chunk_directory_handle
= nullptr;
1815 struct lttng_directory_handle
*domain_handle
= nullptr;
1817 const char *chunk_name
;
1818 bool chunk_name_overridden
;
1820 time_t creation_timestamp
;
1821 char creation_timestamp_buffer
[ISO8601_STR_LEN
];
1822 const char *creation_timestamp_str
= "(none)";
1823 const bool chunk_has_local_output
= relayd_id
== -1ULL;
1824 enum lttng_trace_chunk_status tc_status
;
1825 struct lttcomm_consumer_msg msg
= {
1826 .cmd_type
= LTTNG_CONSUMER_CREATE_TRACE_CHUNK
,
1829 msg
.u
.create_trace_chunk
.session_id
= session_id
;
1831 LTTNG_ASSERT(socket
);
1832 LTTNG_ASSERT(chunk
);
1834 if (relayd_id
!= -1ULL) {
1835 LTTNG_OPTIONAL_SET(&msg
.u
.create_trace_chunk
.relayd_id
, relayd_id
);
1838 chunk_status
= lttng_trace_chunk_get_name(chunk
, &chunk_name
, &chunk_name_overridden
);
1839 if (chunk_status
!= LTTNG_TRACE_CHUNK_STATUS_OK
&&
1840 chunk_status
!= LTTNG_TRACE_CHUNK_STATUS_NONE
) {
1841 ERR("Failed to get name of trace chunk");
1842 ret
= -LTTNG_ERR_FATAL
;
1845 if (chunk_name_overridden
) {
1846 ret
= lttng_strncpy(msg
.u
.create_trace_chunk
.override_name
,
1848 sizeof(msg
.u
.create_trace_chunk
.override_name
));
1850 ERR("Trace chunk name \"%s\" exceeds the maximal length allowed by the consumer protocol",
1852 ret
= -LTTNG_ERR_FATAL
;
1857 chunk_status
= lttng_trace_chunk_get_creation_timestamp(chunk
, &creation_timestamp
);
1858 if (chunk_status
!= LTTNG_TRACE_CHUNK_STATUS_OK
) {
1859 ret
= -LTTNG_ERR_FATAL
;
1862 msg
.u
.create_trace_chunk
.creation_timestamp
= (uint64_t) creation_timestamp
;
1863 /* Only used for logging purposes. */
1864 ret
= time_to_iso8601_str(
1865 creation_timestamp
, creation_timestamp_buffer
, sizeof(creation_timestamp_buffer
));
1866 creation_timestamp_str
= !ret
? creation_timestamp_buffer
: "(formatting error)";
1868 chunk_status
= lttng_trace_chunk_get_id(chunk
, &chunk_id
);
1869 if (chunk_status
!= LTTNG_TRACE_CHUNK_STATUS_OK
) {
1871 * Anonymous trace chunks should never be transmitted
1872 * to remote peers (consumerd and relayd). They are used
1873 * internally for backward-compatibility purposes.
1875 ret
= -LTTNG_ERR_FATAL
;
1878 msg
.u
.create_trace_chunk
.chunk_id
= chunk_id
;
1880 if (chunk_has_local_output
) {
1881 chunk_status
= lttng_trace_chunk_borrow_chunk_directory_handle(
1882 chunk
, &chunk_directory_handle
);
1883 if (chunk_status
!= LTTNG_TRACE_CHUNK_STATUS_OK
) {
1884 ret
= -LTTNG_ERR_FATAL
;
1887 chunk_status
= lttng_trace_chunk_get_credentials(chunk
, &chunk_credentials
);
1888 if (chunk_status
!= LTTNG_TRACE_CHUNK_STATUS_OK
) {
1890 * Not associating credentials to a sessiond chunk is a
1891 * fatal internal error.
1893 ret
= -LTTNG_ERR_FATAL
;
1896 tc_status
= lttng_trace_chunk_create_subdirectory(chunk
, domain_subdir
);
1897 if (tc_status
!= LTTNG_TRACE_CHUNK_STATUS_OK
) {
1898 PERROR("Failed to create chunk domain output directory \"%s\"",
1900 ret
= -LTTNG_ERR_FATAL
;
1903 domain_handle
= lttng_directory_handle_create_from_handle(domain_subdir
,
1904 chunk_directory_handle
);
1905 if (!domain_handle
) {
1906 ret
= -LTTNG_ERR_FATAL
;
1911 * This will only compile on platforms that support
1912 * dirfd (POSIX.2008). This is fine as the session daemon
1913 * is only built for such platforms.
1915 * The ownership of the chunk directory handle's is maintained
1916 * by the trace chunk.
1918 domain_dirfd
= lttng_directory_handle_get_dirfd(domain_handle
);
1919 LTTNG_ASSERT(domain_dirfd
>= 0);
1921 msg
.u
.create_trace_chunk
.credentials
.value
.uid
=
1922 lttng_credentials_get_uid(&chunk_credentials
);
1923 msg
.u
.create_trace_chunk
.credentials
.value
.gid
=
1924 lttng_credentials_get_gid(&chunk_credentials
);
1925 msg
.u
.create_trace_chunk
.credentials
.is_set
= 1;
1928 DBG("Sending consumer create trace chunk command: relayd_id = %" PRId64
1929 ", session_id = %" PRIu64
", chunk_id = %" PRIu64
", creation_timestamp = %s",
1933 creation_timestamp_str
);
1934 health_code_update();
1935 ret
= consumer_send_msg(socket
, &msg
);
1936 health_code_update();
1938 ERR("Trace chunk creation error on consumer");
1939 ret
= -LTTNG_ERR_CREATE_TRACE_CHUNK_FAIL_CONSUMER
;
1943 if (chunk_has_local_output
) {
1944 DBG("Sending trace chunk domain directory fd to consumer");
1945 health_code_update();
1946 ret
= consumer_send_fds(socket
, &domain_dirfd
, 1);
1947 health_code_update();
1949 ERR("Trace chunk creation error on consumer");
1950 ret
= -LTTNG_ERR_CREATE_TRACE_CHUNK_FAIL_CONSUMER
;
1955 lttng_directory_handle_put(domain_handle
);
1960 * Ask the consumer to close a trace chunk for a given session.
1962 * Called with the consumer socket lock held.
1964 int consumer_close_trace_chunk(struct consumer_socket
*socket
,
1966 uint64_t session_id
,
1967 struct lttng_trace_chunk
*chunk
,
1968 char *closed_trace_chunk_path
)
1971 enum lttng_trace_chunk_status chunk_status
;
1972 lttcomm_consumer_msg msg
= {
1973 .cmd_type
= LTTNG_CONSUMER_CLOSE_TRACE_CHUNK
,
1976 msg
.u
.close_trace_chunk
.session_id
= session_id
;
1978 struct lttcomm_consumer_close_trace_chunk_reply reply
;
1980 time_t close_timestamp
;
1981 enum lttng_trace_chunk_command_type close_command
;
1982 const char *close_command_name
= "none";
1983 struct lttng_dynamic_buffer path_reception_buffer
;
1985 LTTNG_ASSERT(socket
);
1986 lttng_dynamic_buffer_init(&path_reception_buffer
);
1988 if (relayd_id
!= -1ULL) {
1989 LTTNG_OPTIONAL_SET(&msg
.u
.close_trace_chunk
.relayd_id
, relayd_id
);
1992 chunk_status
= lttng_trace_chunk_get_close_command(chunk
, &close_command
);
1993 switch (chunk_status
) {
1994 case LTTNG_TRACE_CHUNK_STATUS_OK
:
1995 LTTNG_OPTIONAL_SET(&msg
.u
.close_trace_chunk
.close_command
,
1996 (uint32_t) close_command
);
1998 case LTTNG_TRACE_CHUNK_STATUS_NONE
:
2001 ERR("Failed to get trace chunk close command");
2006 chunk_status
= lttng_trace_chunk_get_id(chunk
, &chunk_id
);
2008 * Anonymous trace chunks should never be transmitted to remote peers
2009 * (consumerd and relayd). They are used internally for
2010 * backward-compatibility purposes.
2012 LTTNG_ASSERT(chunk_status
== LTTNG_TRACE_CHUNK_STATUS_OK
);
2013 msg
.u
.close_trace_chunk
.chunk_id
= chunk_id
;
2015 chunk_status
= lttng_trace_chunk_get_close_timestamp(chunk
, &close_timestamp
);
2017 * A trace chunk should be closed locally before being closed remotely.
2018 * Otherwise, the close timestamp would never be transmitted to the
2021 LTTNG_ASSERT(chunk_status
== LTTNG_TRACE_CHUNK_STATUS_OK
);
2022 msg
.u
.close_trace_chunk
.close_timestamp
= (uint64_t) close_timestamp
;
2024 if (msg
.u
.close_trace_chunk
.close_command
.is_set
) {
2025 close_command_name
= lttng_trace_chunk_command_type_get_name(close_command
);
2027 DBG("Sending consumer close trace chunk command: relayd_id = %" PRId64
2028 ", session_id = %" PRIu64
", chunk_id = %" PRIu64
", close command = \"%s\"",
2032 close_command_name
);
2034 health_code_update();
2035 ret
= consumer_socket_send(socket
, &msg
, sizeof(struct lttcomm_consumer_msg
));
2037 ret
= -LTTNG_ERR_CLOSE_TRACE_CHUNK_FAIL_CONSUMER
;
2040 ret
= consumer_socket_recv(socket
, &reply
, sizeof(reply
));
2042 ret
= -LTTNG_ERR_CLOSE_TRACE_CHUNK_FAIL_CONSUMER
;
2045 if (reply
.path_length
>= LTTNG_PATH_MAX
) {
2046 ERR("Invalid path returned by relay daemon: %" PRIu32
2047 "bytes exceeds maximal allowed length of %d bytes",
2050 ret
= -LTTNG_ERR_INVALID_PROTOCOL
;
2053 ret
= lttng_dynamic_buffer_set_size(&path_reception_buffer
, reply
.path_length
);
2055 ERR("Failed to allocate reception buffer of path returned by the \"close trace chunk\" command");
2056 ret
= -LTTNG_ERR_NOMEM
;
2059 ret
= consumer_socket_recv(socket
, path_reception_buffer
.data
, path_reception_buffer
.size
);
2061 ERR("Communication error while receiving path of closed trace chunk");
2062 ret
= -LTTNG_ERR_CLOSE_TRACE_CHUNK_FAIL_CONSUMER
;
2065 if (path_reception_buffer
.data
[path_reception_buffer
.size
- 1] != '\0') {
2066 ERR("Invalid path returned by relay daemon: not null-terminated");
2067 ret
= -LTTNG_ERR_INVALID_PROTOCOL
;
2070 if (closed_trace_chunk_path
) {
2072 * closed_trace_chunk_path is assumed to have a length >=
2075 memcpy(closed_trace_chunk_path
,
2076 path_reception_buffer
.data
,
2077 path_reception_buffer
.size
);
2080 lttng_dynamic_buffer_reset(&path_reception_buffer
);
2081 health_code_update();
2086 * Ask the consumer if a trace chunk exists.
2088 * Called with the consumer socket lock held.
2089 * Returns 0 on success, or a negative value on error.
2091 int consumer_trace_chunk_exists(struct consumer_socket
*socket
,
2093 uint64_t session_id
,
2094 struct lttng_trace_chunk
*chunk
,
2095 enum consumer_trace_chunk_exists_status
*result
)
2098 enum lttng_trace_chunk_status chunk_status
;
2099 lttcomm_consumer_msg msg
= {
2100 .cmd_type
= LTTNG_CONSUMER_TRACE_CHUNK_EXISTS
,
2103 msg
.u
.trace_chunk_exists
.session_id
= session_id
;
2106 const char *consumer_reply_str
;
2108 LTTNG_ASSERT(socket
);
2110 if (relayd_id
!= -1ULL) {
2111 LTTNG_OPTIONAL_SET(&msg
.u
.trace_chunk_exists
.relayd_id
, relayd_id
);
2114 chunk_status
= lttng_trace_chunk_get_id(chunk
, &chunk_id
);
2115 if (chunk_status
!= LTTNG_TRACE_CHUNK_STATUS_OK
) {
2117 * Anonymous trace chunks should never be transmitted
2118 * to remote peers (consumerd and relayd). They are used
2119 * internally for backward-compatibility purposes.
2121 ret
= -LTTNG_ERR_FATAL
;
2124 msg
.u
.trace_chunk_exists
.chunk_id
= chunk_id
;
2126 DBG("Sending consumer trace chunk exists command: relayd_id = %" PRId64
2127 ", session_id = %" PRIu64
", chunk_id = %" PRIu64
,
2132 health_code_update();
2133 ret
= consumer_send_msg(socket
, &msg
);
2135 case LTTCOMM_CONSUMERD_UNKNOWN_TRACE_CHUNK
:
2136 consumer_reply_str
= "unknown trace chunk";
2137 *result
= CONSUMER_TRACE_CHUNK_EXISTS_STATUS_UNKNOWN_CHUNK
;
2139 case LTTCOMM_CONSUMERD_TRACE_CHUNK_EXISTS_LOCAL
:
2140 consumer_reply_str
= "trace chunk exists locally";
2141 *result
= CONSUMER_TRACE_CHUNK_EXISTS_STATUS_EXISTS_LOCAL
;
2143 case LTTCOMM_CONSUMERD_TRACE_CHUNK_EXISTS_REMOTE
:
2144 consumer_reply_str
= "trace chunk exists on remote peer";
2145 *result
= CONSUMER_TRACE_CHUNK_EXISTS_STATUS_EXISTS_REMOTE
;
2148 ERR("Consumer returned an error from TRACE_CHUNK_EXISTS command");
2152 DBG("Consumer reply to TRACE_CHUNK_EXISTS command: %s", consumer_reply_str
);
2155 health_code_update();