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/urcu.hpp>
21 #include <common/uri.hpp>
28 #include <sys/types.h>
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
,
40 size_t *consumer_path_offset
)
45 LTTNG_ASSERT(consumer
);
46 LTTNG_ASSERT(session_path
);
51 * Allocate the string ourself to make sure we never exceed
54 pathname
= calloc
<char>(LTTNG_PATH_MAX
);
59 /* Get correct path name destination */
60 if (consumer
->type
== CONSUMER_DST_NET
&& consumer
->relay_major_version
== 2 &&
61 consumer
->relay_minor_version
< 11) {
62 ret
= snprintf(pathname
,
65 consumer
->dst
.net
.base_dir
,
67 consumer
->domain_subdir
,
69 *consumer_path_offset
= 0;
72 pathname
, LTTNG_PATH_MAX
, "%s/%s", consumer
->domain_subdir
, session_path
);
73 *consumer_path_offset
= strlen(consumer
->domain_subdir
) + 1;
75 DBG3("Consumer trace path relative to current trace chunk: \"%s\"", pathname
);
77 PERROR("Failed to format channel path");
79 } else if (ret
>= LTTNG_PATH_MAX
) {
80 ERR("Truncation occurred while formatting channel path");
91 * Send a data payload using a given consumer socket of size len.
93 * The consumer socket lock MUST be acquired before calling this since this
94 * function can change the fd value.
96 * Return 0 on success else a negative value on error.
98 int consumer_socket_send(struct consumer_socket
*socket
, const void *msg
, size_t len
)
103 LTTNG_ASSERT(socket
);
104 LTTNG_ASSERT(socket
->fd_ptr
);
107 /* Consumer socket is invalid. Stopping. */
108 fd
= *socket
->fd_ptr
;
113 size
= lttcomm_send_unix_sock(fd
, msg
, len
);
115 /* The above call will print a PERROR on error. */
116 DBG("Error when sending data to consumer on sock %d", fd
);
118 * At this point, the socket is not usable anymore thus closing it and
119 * setting the file descriptor to -1 so it is not reused.
122 /* This call will PERROR on error. */
123 (void) lttcomm_close_unix_sock(fd
);
124 *socket
->fd_ptr
= -1;
135 * Receive a data payload using a given consumer socket of size len.
137 * The consumer socket lock MUST be acquired before calling this since this
138 * function can change the fd value.
140 * Return 0 on success else a negative value on error.
142 int consumer_socket_recv(struct consumer_socket
*socket
, void *msg
, size_t len
)
147 LTTNG_ASSERT(socket
);
148 LTTNG_ASSERT(socket
->fd_ptr
);
151 /* Consumer socket is invalid. Stopping. */
152 fd
= *socket
->fd_ptr
;
157 size
= lttcomm_recv_unix_sock(fd
, msg
, len
);
159 /* The above call will print a PERROR on error. */
160 DBG("Error when receiving data from the consumer socket %d", fd
);
162 * At this point, the socket is not usable anymore thus closing it and
163 * setting the file descriptor to -1 so it is not reused.
166 /* This call will PERROR on error. */
167 (void) lttcomm_close_unix_sock(fd
);
168 *socket
->fd_ptr
= -1;
179 * Receive a reply command status message from the consumer. Consumer socket
180 * lock MUST be acquired before calling this function.
182 * Return 0 on success, -1 on recv error or a negative lttng error code which
183 * was possibly returned by the consumer.
185 int consumer_recv_status_reply(struct consumer_socket
*sock
)
188 struct lttcomm_consumer_status_msg reply
;
192 ret
= consumer_socket_recv(sock
, &reply
, sizeof(reply
));
197 if (reply
.ret_code
== LTTCOMM_CONSUMERD_SUCCESS
) {
201 ret
= -reply
.ret_code
;
202 DBG("Consumer ret code %d", ret
);
210 * Once the ASK_CHANNEL command is sent to the consumer, the channel
211 * information are sent back. This call receives that data and populates key
214 * On success return 0 and both key and stream_count are set. On error, a
215 * negative value is sent back and both parameters are untouched.
217 int consumer_recv_status_channel(struct consumer_socket
*sock
,
219 unsigned int *stream_count
)
222 struct lttcomm_consumer_status_channel reply
;
225 LTTNG_ASSERT(stream_count
);
228 ret
= consumer_socket_recv(sock
, &reply
, sizeof(reply
));
233 /* An error is possible so don't touch the key and stream_count. */
234 if (reply
.ret_code
!= LTTCOMM_CONSUMERD_SUCCESS
) {
240 *stream_count
= reply
.stream_count
;
248 * Send destroy relayd command to consumer.
250 * On success return positive value. On error, negative value.
252 int consumer_send_destroy_relayd(struct consumer_socket
*sock
, struct consumer_output
*consumer
)
255 struct lttcomm_consumer_msg msg
;
257 LTTNG_ASSERT(consumer
);
260 DBG2("Sending destroy relayd command to consumer sock %d", *sock
->fd_ptr
);
262 memset(&msg
, 0, sizeof(msg
));
263 msg
.cmd_type
= LTTNG_CONSUMER_DESTROY_RELAYD
;
264 msg
.u
.destroy_relayd
.net_seq_idx
= consumer
->net_seq_index
;
266 pthread_mutex_lock(sock
->lock
);
267 ret
= consumer_socket_send(sock
, &msg
, sizeof(msg
));
272 /* Don't check the return value. The caller will do it. */
273 ret
= consumer_recv_status_reply(sock
);
275 DBG2("Consumer send destroy relayd command done");
278 pthread_mutex_unlock(sock
->lock
);
283 * For each consumer socket in the consumer output object, send a destroy
286 void consumer_output_send_destroy_relayd(struct consumer_output
*consumer
)
288 struct lttng_ht_iter iter
;
289 struct consumer_socket
*socket
;
291 LTTNG_ASSERT(consumer
);
293 /* Destroy any relayd connection */
294 if (consumer
->type
== CONSUMER_DST_NET
) {
295 lttng::urcu::read_lock_guard read_lock
;
297 cds_lfht_for_each_entry (consumer
->socks
->ht
, &iter
.iter
, socket
, node
.node
) {
298 /* Send destroy relayd command. */
299 const int 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. */
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 lttng::urcu::read_lock_guard read_lock
;
324 if (output
== nullptr || 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.
332 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
;
342 consumer_add_socket(socket
, output
);
345 socket
->type
= data
->type
;
347 DBG3("Consumer socket created (fd: %d) and added to output", data
->cmd_sock
);
354 * Return the consumer socket from the given consumer output with the right
355 * bitness. On error, returns NULL.
357 * The caller MUST acquire a rcu read side lock and keep it until the socket
358 * object reference is not needed anymore.
360 struct consumer_socket
*consumer_find_socket_by_bitness(int bits
,
361 const struct consumer_output
*consumer
)
364 struct consumer_socket
*socket
= nullptr;
366 ASSERT_RCU_READ_LOCKED();
370 consumer_fd
= uatomic_read(&the_ust_consumerd64_fd
);
373 consumer_fd
= uatomic_read(&the_ust_consumerd32_fd
);
380 socket
= consumer_find_socket(consumer_fd
, consumer
);
382 ERR("Consumer socket fd %d not found in consumer obj %p", consumer_fd
, consumer
);
390 * Find a consumer_socket in a consumer_output hashtable. Read side lock must
391 * be acquired before calling this function and across use of the
392 * returned consumer_socket.
394 struct consumer_socket
*consumer_find_socket(int key
, const struct consumer_output
*consumer
)
396 struct lttng_ht_iter iter
;
397 struct lttng_ht_node_ulong
*node
;
398 struct consumer_socket
*socket
= nullptr;
400 ASSERT_RCU_READ_LOCKED();
402 /* Negative keys are lookup failures */
403 if (key
< 0 || consumer
== nullptr) {
407 lttng_ht_lookup(consumer
->socks
, (void *) ((unsigned long) key
), &iter
);
408 node
= lttng_ht_iter_get_node_ulong(&iter
);
409 if (node
!= nullptr) {
410 socket
= lttng::utils::container_of(node
, &consumer_socket::node
);
417 * Allocate a new consumer_socket and return the pointer.
419 struct consumer_socket
*consumer_allocate_socket(int *fd
)
421 struct consumer_socket
*socket
= nullptr;
425 socket
= zmalloc
<consumer_socket
>();
426 if (socket
== nullptr) {
427 PERROR("zmalloc consumer socket");
432 lttng_ht_node_init_ulong(&socket
->node
, *fd
);
439 * Add consumer socket to consumer output object. Read side lock must be
440 * acquired before calling this function.
442 void consumer_add_socket(struct consumer_socket
*sock
, struct consumer_output
*consumer
)
445 LTTNG_ASSERT(consumer
);
446 ASSERT_RCU_READ_LOCKED();
448 lttng_ht_add_unique_ulong(consumer
->socks
, &sock
->node
);
452 * Delete consumer socket to consumer output object. Read side lock must be
453 * acquired before calling this function.
455 void consumer_del_socket(struct consumer_socket
*sock
, struct consumer_output
*consumer
)
458 struct lttng_ht_iter iter
;
461 LTTNG_ASSERT(consumer
);
462 ASSERT_RCU_READ_LOCKED();
464 iter
.iter
.node
= &sock
->node
.node
;
465 ret
= lttng_ht_del(consumer
->socks
, &iter
);
470 * RCU destroy call function.
472 static void destroy_socket_rcu(struct rcu_head
*head
)
474 struct lttng_ht_node_ulong
*node
=
475 lttng::utils::container_of(head
, <tng_ht_node_ulong::head
);
476 struct consumer_socket
*socket
= lttng::utils::container_of(node
, &consumer_socket::node
);
482 * Destroy and free socket pointer in a call RCU. The call must either:
483 * - have acquired the read side lock before calling this function, or
484 * - guarantee the validity of the `struct consumer_socket` object for the
485 * duration of the call.
487 void consumer_destroy_socket(struct consumer_socket
*sock
)
492 * We DO NOT close the file descriptor here since it is global to the
493 * session daemon and is closed only if the consumer dies or a custom
494 * consumer was registered,
496 if (sock
->registered
) {
497 DBG3("Consumer socket was registered. Closing fd %d", *sock
->fd_ptr
);
498 lttcomm_close_unix_sock(*sock
->fd_ptr
);
501 call_rcu(&sock
->node
.head
, destroy_socket_rcu
);
505 * Allocate and assign data to a consumer_output object.
507 * Return pointer to structure.
509 struct consumer_output
*consumer_create_output(enum consumer_dst_type type
)
511 struct consumer_output
*output
= nullptr;
513 output
= zmalloc
<consumer_output
>();
514 if (output
== nullptr) {
515 PERROR("zmalloc consumer_output");
519 /* By default, consumer output is enabled */
520 output
->enabled
= true;
522 output
->net_seq_index
= (uint64_t) -1ULL;
523 urcu_ref_init(&output
->ref
);
525 output
->socks
= lttng_ht_new(0, LTTNG_HT_TYPE_ULONG
);
532 * Iterate over the consumer output socket hash table and destroy them. The
533 * socket file descriptor are only closed if the consumer output was
534 * registered meaning it's an external consumer.
536 void consumer_destroy_output_sockets(struct consumer_output
*obj
)
538 struct lttng_ht_iter iter
;
539 struct consumer_socket
*socket
;
546 lttng::urcu::read_lock_guard read_lock
;
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 lttng::urcu::read_lock_guard read_lock
;
642 cds_lfht_for_each_entry (src
->socks
->ht
, &iter
.iter
, socket
, node
.node
) {
643 /* Ignore socket that are already there. */
644 copy_sock
= consumer_find_socket(*socket
->fd_ptr
, dst
);
649 /* Create new socket object. */
650 copy_sock
= consumer_allocate_socket(socket
->fd_ptr
);
651 if (copy_sock
== nullptr) {
656 copy_sock
->registered
= socket
->registered
;
658 * This is valid because this lock is shared accross all consumer
659 * object being the global lock of the consumer data structure of the
662 copy_sock
->lock
= socket
->lock
;
663 consumer_add_socket(copy_sock
, dst
);
672 * Set network URI to the consumer output.
674 * Return 0 on success. Return 1 if the URI were equal. Else, negative value on
677 int consumer_set_network_uri(const struct ltt_session
*session
,
678 struct consumer_output
*output
,
679 struct lttng_uri
*uri
)
682 struct lttng_uri
*dst_uri
= nullptr;
684 /* Code flow error safety net. */
685 LTTNG_ASSERT(output
);
688 switch (uri
->stype
) {
689 case LTTNG_STREAM_CONTROL
:
690 dst_uri
= &output
->dst
.net
.control
;
691 output
->dst
.net
.control_isset
= 1;
692 if (uri
->port
== 0) {
693 /* Assign default port. */
694 uri
->port
= DEFAULT_NETWORK_CONTROL_PORT
;
696 if (output
->dst
.net
.data_isset
&& uri
->port
== output
->dst
.net
.data
.port
) {
697 ret
= -LTTNG_ERR_INVALID
;
701 DBG3("Consumer control URI set with port %d", uri
->port
);
703 case LTTNG_STREAM_DATA
:
704 dst_uri
= &output
->dst
.net
.data
;
705 output
->dst
.net
.data_isset
= 1;
706 if (uri
->port
== 0) {
707 /* Assign default port. */
708 uri
->port
= DEFAULT_NETWORK_DATA_PORT
;
710 if (output
->dst
.net
.control_isset
&&
711 uri
->port
== output
->dst
.net
.control
.port
) {
712 ret
= -LTTNG_ERR_INVALID
;
716 DBG3("Consumer data URI set with port %d", uri
->port
);
719 ERR("Set network uri type unknown %d", uri
->stype
);
720 ret
= -LTTNG_ERR_INVALID
;
724 ret
= uri_compare(dst_uri
, uri
);
726 /* Same URI, don't touch it and return success. */
727 DBG3("URI network compare are the same");
731 /* URIs were not equal, replacing it. */
732 memcpy(dst_uri
, uri
, sizeof(struct lttng_uri
));
733 output
->type
= CONSUMER_DST_NET
;
734 if (dst_uri
->stype
!= LTTNG_STREAM_CONTROL
) {
735 /* Only the control uri needs to contain the path. */
740 * If the user has specified a subdir as part of the control
741 * URL, the session's base output directory is:
742 * /RELAYD_OUTPUT_PATH/HOSTNAME/USER_SPECIFIED_DIR
744 * Hence, the "base_dir" from which all stream files and
745 * session rotation chunks are created takes the form
746 * /HOSTNAME/USER_SPECIFIED_DIR
748 * If the user has not specified an output directory as part of
749 * the control URL, the base output directory has the form:
750 * /RELAYD_OUTPUT_PATH/HOSTNAME/SESSION_NAME-CREATION_TIME
752 * Hence, the "base_dir" from which all stream files and
753 * session rotation chunks are created takes the form
754 * /HOSTNAME/SESSION_NAME-CREATION_TIME
756 * Note that automatically generated session names already
757 * contain the session's creation time. In that case, the
758 * creation time is omitted to prevent it from being duplicated
759 * in the final directory hierarchy.
762 if (strstr(uri
->subdir
, "../")) {
763 ERR("Network URI subdirs are not allowed to walk up the path hierarchy");
764 ret
= -LTTNG_ERR_INVALID
;
767 ret
= snprintf(output
->dst
.net
.base_dir
,
768 sizeof(output
->dst
.net
.base_dir
),
773 if (session
->has_auto_generated_name
) {
774 ret
= snprintf(output
->dst
.net
.base_dir
,
775 sizeof(output
->dst
.net
.base_dir
),
780 char session_creation_datetime
[16];
784 timeinfo
= localtime(&session
->creation_time
);
786 ret
= -LTTNG_ERR_FATAL
;
789 strftime_ret
= strftime(session_creation_datetime
,
790 sizeof(session_creation_datetime
),
793 if (strftime_ret
== 0) {
794 ERR("Failed to format session creation timestamp while setting network URI");
795 ret
= -LTTNG_ERR_FATAL
;
798 ret
= snprintf(output
->dst
.net
.base_dir
,
799 sizeof(output
->dst
.net
.base_dir
),
803 session_creation_datetime
);
806 if (ret
>= sizeof(output
->dst
.net
.base_dir
)) {
807 ret
= -LTTNG_ERR_INVALID
;
808 ERR("Truncation occurred while setting network output base directory");
810 } else if (ret
== -1) {
811 ret
= -LTTNG_ERR_INVALID
;
812 PERROR("Error occurred while setting network output base directory");
816 DBG3("Consumer set network uri base_dir path %s", output
->dst
.net
.base_dir
);
827 * Send file descriptor to consumer via sock.
829 * The consumer socket lock must be held by the caller.
831 int consumer_send_fds(struct consumer_socket
*sock
, const int *fds
, size_t nb_fd
)
837 LTTNG_ASSERT(nb_fd
> 0);
838 LTTNG_ASSERT(pthread_mutex_trylock(sock
->lock
) == EBUSY
);
840 ret
= lttcomm_send_fds_unix_sock(*sock
->fd_ptr
, fds
, nb_fd
);
842 /* The above call will print a PERROR on error. */
843 DBG("Error when sending consumer fds on sock %d", *sock
->fd_ptr
);
847 ret
= consumer_recv_status_reply(sock
);
853 * Consumer send communication message structure to consumer.
855 * The consumer socket lock must be held by the caller.
857 int consumer_send_msg(struct consumer_socket
*sock
, const struct lttcomm_consumer_msg
*msg
)
863 LTTNG_ASSERT(pthread_mutex_trylock(sock
->lock
) == EBUSY
);
865 ret
= consumer_socket_send(sock
, msg
, sizeof(struct lttcomm_consumer_msg
));
870 ret
= consumer_recv_status_reply(sock
);
877 * Consumer send channel communication message structure to consumer.
879 * The consumer socket lock must be held by the caller.
881 int consumer_send_channel(struct consumer_socket
*sock
, struct lttcomm_consumer_msg
*msg
)
888 ret
= consumer_send_msg(sock
, msg
);
898 * Populate the given consumer msg structure with the ask_channel command
901 void consumer_init_ask_channel_comm_msg(struct lttcomm_consumer_msg
*msg
,
902 uint64_t subbuf_size
,
905 unsigned int switch_timer_interval
,
906 unsigned int read_timer_interval
,
907 unsigned int live_timer_interval
,
908 bool is_in_live_session
,
909 unsigned int monitor_timer_interval
,
913 const char *pathname
,
917 const lttng_uuid
& uuid
,
919 uint64_t tracefile_size
,
920 uint64_t tracefile_count
,
921 uint64_t session_id_per_pid
,
922 unsigned int monitor
,
923 uint32_t ust_app_uid
,
924 int64_t blocking_timeout
,
925 const char *root_shm_path
,
926 const char *shm_path
,
927 struct lttng_trace_chunk
*trace_chunk
,
928 const struct lttng_credentials
*buffer_credentials
)
932 /* Zeroed structure */
933 memset(msg
, 0, sizeof(struct lttcomm_consumer_msg
));
934 msg
->u
.ask_channel
.buffer_credentials
.uid
= UINT32_MAX
;
935 msg
->u
.ask_channel
.buffer_credentials
.gid
= UINT32_MAX
;
939 enum lttng_trace_chunk_status chunk_status
;
941 chunk_status
= lttng_trace_chunk_get_id(trace_chunk
, &chunk_id
);
942 LTTNG_ASSERT(chunk_status
== LTTNG_TRACE_CHUNK_STATUS_OK
);
943 LTTNG_OPTIONAL_SET(&msg
->u
.ask_channel
.chunk_id
, chunk_id
);
945 msg
->u
.ask_channel
.buffer_credentials
.uid
= lttng_credentials_get_uid(buffer_credentials
);
946 msg
->u
.ask_channel
.buffer_credentials
.gid
= lttng_credentials_get_gid(buffer_credentials
);
948 msg
->cmd_type
= LTTNG_CONSUMER_ASK_CHANNEL_CREATION
;
949 msg
->u
.ask_channel
.subbuf_size
= subbuf_size
;
950 msg
->u
.ask_channel
.num_subbuf
= num_subbuf
;
951 msg
->u
.ask_channel
.overwrite
= overwrite
;
952 msg
->u
.ask_channel
.switch_timer_interval
= switch_timer_interval
;
953 msg
->u
.ask_channel
.read_timer_interval
= read_timer_interval
;
954 msg
->u
.ask_channel
.live_timer_interval
= live_timer_interval
;
955 msg
->u
.ask_channel
.is_live
= is_in_live_session
;
956 msg
->u
.ask_channel
.monitor_timer_interval
= monitor_timer_interval
;
957 msg
->u
.ask_channel
.output
= output
;
958 msg
->u
.ask_channel
.type
= type
;
959 msg
->u
.ask_channel
.session_id
= session_id
;
960 msg
->u
.ask_channel
.session_id_per_pid
= session_id_per_pid
;
961 msg
->u
.ask_channel
.relayd_id
= relayd_id
;
962 msg
->u
.ask_channel
.key
= key
;
963 msg
->u
.ask_channel
.chan_id
= chan_id
;
964 msg
->u
.ask_channel
.tracefile_size
= tracefile_size
;
965 msg
->u
.ask_channel
.tracefile_count
= tracefile_count
;
966 msg
->u
.ask_channel
.monitor
= monitor
;
967 msg
->u
.ask_channel
.ust_app_uid
= ust_app_uid
;
968 msg
->u
.ask_channel
.blocking_timeout
= blocking_timeout
;
970 std::copy(uuid
.begin(), uuid
.end(), msg
->u
.ask_channel
.uuid
);
973 strncpy(msg
->u
.ask_channel
.pathname
, pathname
, sizeof(msg
->u
.ask_channel
.pathname
));
974 msg
->u
.ask_channel
.pathname
[sizeof(msg
->u
.ask_channel
.pathname
) - 1] = '\0';
977 strncpy(msg
->u
.ask_channel
.name
, name
, sizeof(msg
->u
.ask_channel
.name
));
978 msg
->u
.ask_channel
.name
[sizeof(msg
->u
.ask_channel
.name
) - 1] = '\0';
981 strncpy(msg
->u
.ask_channel
.root_shm_path
,
983 sizeof(msg
->u
.ask_channel
.root_shm_path
));
984 msg
->u
.ask_channel
.root_shm_path
[sizeof(msg
->u
.ask_channel
.root_shm_path
) - 1] =
988 strncpy(msg
->u
.ask_channel
.shm_path
, shm_path
, sizeof(msg
->u
.ask_channel
.shm_path
));
989 msg
->u
.ask_channel
.shm_path
[sizeof(msg
->u
.ask_channel
.shm_path
) - 1] = '\0';
994 * Init channel communication message structure.
996 void consumer_init_add_channel_comm_msg(struct lttcomm_consumer_msg
*msg
,
997 uint64_t channel_key
,
999 const char *pathname
,
1002 unsigned int nb_init_streams
,
1003 enum lttng_event_output output
,
1005 uint64_t tracefile_size
,
1006 uint64_t tracefile_count
,
1007 unsigned int monitor
,
1008 unsigned int live_timer_interval
,
1009 bool is_in_live_session
,
1010 unsigned int monitor_timer_interval
,
1011 struct lttng_trace_chunk
*trace_chunk
)
1015 /* Zeroed structure */
1016 memset(msg
, 0, sizeof(struct lttcomm_consumer_msg
));
1020 enum lttng_trace_chunk_status chunk_status
;
1022 chunk_status
= lttng_trace_chunk_get_id(trace_chunk
, &chunk_id
);
1023 LTTNG_ASSERT(chunk_status
== LTTNG_TRACE_CHUNK_STATUS_OK
);
1024 LTTNG_OPTIONAL_SET(&msg
->u
.channel
.chunk_id
, chunk_id
);
1028 msg
->cmd_type
= LTTNG_CONSUMER_ADD_CHANNEL
;
1029 msg
->u
.channel
.channel_key
= channel_key
;
1030 msg
->u
.channel
.session_id
= session_id
;
1031 msg
->u
.channel
.relayd_id
= relayd_id
;
1032 msg
->u
.channel
.nb_init_streams
= nb_init_streams
;
1033 msg
->u
.channel
.output
= output
;
1034 msg
->u
.channel
.type
= type
;
1035 msg
->u
.channel
.tracefile_size
= tracefile_size
;
1036 msg
->u
.channel
.tracefile_count
= tracefile_count
;
1037 msg
->u
.channel
.monitor
= monitor
;
1038 msg
->u
.channel
.live_timer_interval
= live_timer_interval
;
1039 msg
->u
.channel
.is_live
= is_in_live_session
;
1040 msg
->u
.channel
.monitor_timer_interval
= monitor_timer_interval
;
1042 strncpy(msg
->u
.channel
.pathname
, pathname
, sizeof(msg
->u
.channel
.pathname
));
1043 msg
->u
.channel
.pathname
[sizeof(msg
->u
.channel
.pathname
) - 1] = '\0';
1045 strncpy(msg
->u
.channel
.name
, name
, sizeof(msg
->u
.channel
.name
));
1046 msg
->u
.channel
.name
[sizeof(msg
->u
.channel
.name
) - 1] = '\0';
1050 * Init stream communication message structure.
1052 void consumer_init_add_stream_comm_msg(struct lttcomm_consumer_msg
*msg
,
1053 uint64_t channel_key
,
1054 uint64_t stream_key
,
1059 memset(msg
, 0, sizeof(struct lttcomm_consumer_msg
));
1061 msg
->cmd_type
= LTTNG_CONSUMER_ADD_STREAM
;
1062 msg
->u
.stream
.channel_key
= channel_key
;
1063 msg
->u
.stream
.stream_key
= stream_key
;
1064 msg
->u
.stream
.cpu
= cpu
;
1067 void consumer_init_streams_sent_comm_msg(struct lttcomm_consumer_msg
*msg
,
1068 enum lttng_consumer_command cmd
,
1069 uint64_t channel_key
,
1070 uint64_t net_seq_idx
)
1074 memset(msg
, 0, sizeof(struct lttcomm_consumer_msg
));
1076 msg
->cmd_type
= cmd
;
1077 msg
->u
.sent_streams
.channel_key
= channel_key
;
1078 msg
->u
.sent_streams
.net_seq_idx
= net_seq_idx
;
1082 * Send stream communication structure to the consumer.
1084 int consumer_send_stream(struct consumer_socket
*sock
,
1085 struct consumer_output
*dst
,
1086 struct lttcomm_consumer_msg
*msg
,
1097 ret
= consumer_send_msg(sock
, msg
);
1102 ret
= consumer_send_fds(sock
, fds
, nb_fd
);
1112 * Send relayd socket to consumer associated with a session name.
1114 * The consumer socket lock must be held by the caller.
1116 * On success return positive value. On error, negative value.
1118 int consumer_send_relayd_socket(struct consumer_socket
*consumer_sock
,
1119 struct lttcomm_relayd_sock
*rsock
,
1120 struct consumer_output
*consumer
,
1121 enum lttng_stream_type type
,
1122 uint64_t session_id
,
1123 const char *session_name
,
1124 const char *hostname
,
1125 const char *base_path
,
1126 int session_live_timer
,
1127 const uint64_t *current_chunk_id
,
1128 time_t session_creation_time
,
1129 bool session_name_contains_creation_time
)
1133 struct lttcomm_consumer_msg msg
;
1135 /* Code flow error. Safety net. */
1136 LTTNG_ASSERT(rsock
);
1137 LTTNG_ASSERT(consumer
);
1138 LTTNG_ASSERT(consumer_sock
);
1140 memset(&msg
, 0, sizeof(msg
));
1141 /* Bail out if consumer is disabled */
1142 if (!consumer
->enabled
) {
1147 if (type
== LTTNG_STREAM_CONTROL
) {
1148 char output_path
[LTTNG_PATH_MAX
] = {};
1149 uint64_t relayd_session_id
;
1151 ret
= relayd_create_session(rsock
,
1161 session_creation_time
,
1162 session_name_contains_creation_time
,
1165 /* Close the control socket. */
1166 (void) relayd_close(rsock
);
1169 msg
.u
.relayd_sock
.relayd_session_id
= relayd_session_id
;
1170 DBG("Created session on relay, output path reply: %s", output_path
);
1173 msg
.cmd_type
= LTTNG_CONSUMER_ADD_RELAYD_SOCKET
;
1175 * Assign network consumer output index using the temporary consumer since
1176 * this call should only be made from within a set_consumer_uri() function
1177 * call in the session daemon.
1179 msg
.u
.relayd_sock
.net_index
= consumer
->net_seq_index
;
1180 msg
.u
.relayd_sock
.type
= type
;
1181 msg
.u
.relayd_sock
.session_id
= session_id
;
1182 msg
.u
.relayd_sock
.major
= rsock
->major
;
1183 msg
.u
.relayd_sock
.minor
= rsock
->minor
;
1184 msg
.u
.relayd_sock
.relayd_socket_protocol
= rsock
->sock
.proto
;
1186 DBG3("Sending relayd sock info to consumer on %d", *consumer_sock
->fd_ptr
);
1187 ret
= consumer_send_msg(consumer_sock
, &msg
);
1192 DBG3("Sending relayd socket file descriptor to consumer");
1193 fd
= rsock
->sock
.fd
;
1194 ret
= consumer_send_fds(consumer_sock
, &fd
, 1);
1199 DBG2("Consumer relayd socket sent");
1206 consumer_send_pipe(struct consumer_socket
*consumer_sock
, enum lttng_consumer_command cmd
, int pipe
)
1209 struct lttcomm_consumer_msg msg
;
1210 const char *pipe_name
;
1211 const char *command_name
;
1214 case LTTNG_CONSUMER_SET_CHANNEL_MONITOR_PIPE
:
1215 pipe_name
= "channel monitor";
1216 command_name
= "SET_CHANNEL_MONITOR_PIPE";
1219 ERR("Unexpected command received in %s (cmd = %d)", __func__
, (int) cmd
);
1223 /* Code flow error. Safety net. */
1225 memset(&msg
, 0, sizeof(msg
));
1228 pthread_mutex_lock(consumer_sock
->lock
);
1229 DBG3("Sending %s command to consumer", command_name
);
1230 ret
= consumer_send_msg(consumer_sock
, &msg
);
1235 DBG3("Sending %s pipe %d to consumer on socket %d", pipe_name
, pipe
, *consumer_sock
->fd_ptr
);
1236 ret
= consumer_send_fds(consumer_sock
, &pipe
, 1);
1241 DBG2("%s pipe successfully sent", pipe_name
);
1243 pthread_mutex_unlock(consumer_sock
->lock
);
1247 int consumer_send_channel_monitor_pipe(struct consumer_socket
*consumer_sock
, int pipe
)
1249 return consumer_send_pipe(consumer_sock
, LTTNG_CONSUMER_SET_CHANNEL_MONITOR_PIPE
, pipe
);
1253 * Ask the consumer if the data is pending for the specific session id.
1254 * Returns 1 if data is pending, 0 otherwise, or < 0 on error.
1256 int consumer_is_data_pending(uint64_t session_id
, struct consumer_output
*consumer
)
1259 int32_t ret_code
= 0; /* Default is that the data is NOT pending */
1260 struct consumer_socket
*socket
;
1261 struct lttng_ht_iter iter
;
1262 struct lttcomm_consumer_msg msg
;
1264 LTTNG_ASSERT(consumer
);
1266 DBG3("Consumer data pending for id %" PRIu64
, session_id
);
1268 memset(&msg
, 0, sizeof(msg
));
1269 msg
.cmd_type
= LTTNG_CONSUMER_DATA_PENDING
;
1270 msg
.u
.data_pending
.session_id
= session_id
;
1273 /* Send command for each consumer. */
1274 lttng::urcu::read_lock_guard read_lock
;
1276 cds_lfht_for_each_entry (consumer
->socks
->ht
, &iter
.iter
, socket
, node
.node
) {
1277 pthread_mutex_lock(socket
->lock
);
1278 ret
= consumer_socket_send(socket
, &msg
, sizeof(msg
));
1280 pthread_mutex_unlock(socket
->lock
);
1285 * No need for a recv reply status because the answer to the command is
1286 * the reply status message.
1288 ret
= consumer_socket_recv(socket
, &ret_code
, sizeof(ret_code
));
1290 pthread_mutex_unlock(socket
->lock
);
1294 pthread_mutex_unlock(socket
->lock
);
1296 if (ret_code
== 1) {
1302 DBG("Consumer data is %s pending for session id %" PRIu64
,
1303 ret_code
== 1 ? "" : "NOT",
1312 * Send a flush command to consumer using the given channel key.
1314 * Return 0 on success else a negative value.
1316 int consumer_flush_channel(struct consumer_socket
*socket
, uint64_t key
)
1319 struct lttcomm_consumer_msg msg
;
1321 LTTNG_ASSERT(socket
);
1323 DBG2("Consumer flush channel key %" PRIu64
, key
);
1325 memset(&msg
, 0, sizeof(msg
));
1326 msg
.cmd_type
= LTTNG_CONSUMER_FLUSH_CHANNEL
;
1327 msg
.u
.flush_channel
.key
= key
;
1329 pthread_mutex_lock(socket
->lock
);
1330 health_code_update();
1332 ret
= consumer_send_msg(socket
, &msg
);
1338 health_code_update();
1339 pthread_mutex_unlock(socket
->lock
);
1344 * Send a clear quiescent command to consumer using the given channel key.
1346 * Return 0 on success else a negative value.
1348 int consumer_clear_quiescent_channel(struct consumer_socket
*socket
, uint64_t key
)
1351 struct lttcomm_consumer_msg msg
;
1353 LTTNG_ASSERT(socket
);
1355 DBG2("Consumer clear quiescent channel key %" PRIu64
, key
);
1357 memset(&msg
, 0, sizeof(msg
));
1358 msg
.cmd_type
= LTTNG_CONSUMER_CLEAR_QUIESCENT_CHANNEL
;
1359 msg
.u
.clear_quiescent_channel
.key
= key
;
1361 pthread_mutex_lock(socket
->lock
);
1362 health_code_update();
1364 ret
= consumer_send_msg(socket
, &msg
);
1370 health_code_update();
1371 pthread_mutex_unlock(socket
->lock
);
1376 * Send a close metadata command to consumer using the given channel key.
1377 * Called with registry lock held.
1379 * Return 0 on success else a negative value.
1381 int consumer_close_metadata(struct consumer_socket
*socket
, uint64_t metadata_key
)
1384 struct lttcomm_consumer_msg msg
;
1386 LTTNG_ASSERT(socket
);
1388 DBG2("Consumer close metadata channel key %" PRIu64
, metadata_key
);
1390 memset(&msg
, 0, sizeof(msg
));
1391 msg
.cmd_type
= LTTNG_CONSUMER_CLOSE_METADATA
;
1392 msg
.u
.close_metadata
.key
= metadata_key
;
1394 pthread_mutex_lock(socket
->lock
);
1395 health_code_update();
1397 ret
= consumer_send_msg(socket
, &msg
);
1403 health_code_update();
1404 pthread_mutex_unlock(socket
->lock
);
1409 * Send a setup metdata command to consumer using the given channel key.
1411 * Return 0 on success else a negative value.
1413 int consumer_setup_metadata(struct consumer_socket
*socket
, uint64_t metadata_key
)
1416 struct lttcomm_consumer_msg msg
;
1418 LTTNG_ASSERT(socket
);
1420 DBG2("Consumer setup metadata channel key %" PRIu64
, metadata_key
);
1422 memset(&msg
, 0, sizeof(msg
));
1423 msg
.cmd_type
= LTTNG_CONSUMER_SETUP_METADATA
;
1424 msg
.u
.setup_metadata
.key
= metadata_key
;
1426 pthread_mutex_lock(socket
->lock
);
1427 health_code_update();
1429 ret
= consumer_send_msg(socket
, &msg
);
1435 health_code_update();
1436 pthread_mutex_unlock(socket
->lock
);
1441 * Send metadata string to consumer.
1442 * RCU read-side lock must be held to guarantee existence of socket.
1444 * Return 0 on success else a negative value.
1446 int consumer_push_metadata(struct consumer_socket
*socket
,
1447 uint64_t metadata_key
,
1450 size_t target_offset
,
1454 struct lttcomm_consumer_msg msg
;
1456 LTTNG_ASSERT(socket
);
1457 ASSERT_RCU_READ_LOCKED();
1459 DBG2("Consumer push metadata to consumer socket %d", *socket
->fd_ptr
);
1461 pthread_mutex_lock(socket
->lock
);
1463 memset(&msg
, 0, sizeof(msg
));
1464 msg
.cmd_type
= LTTNG_CONSUMER_PUSH_METADATA
;
1465 msg
.u
.push_metadata
.key
= metadata_key
;
1466 msg
.u
.push_metadata
.target_offset
= target_offset
;
1467 msg
.u
.push_metadata
.len
= len
;
1468 msg
.u
.push_metadata
.version
= version
;
1470 health_code_update();
1471 ret
= consumer_send_msg(socket
, &msg
);
1472 if (ret
< 0 || len
== 0) {
1476 DBG3("Consumer pushing metadata on sock %d of len %zu", *socket
->fd_ptr
, len
);
1478 ret
= consumer_socket_send(socket
, metadata_str
, len
);
1483 health_code_update();
1484 ret
= consumer_recv_status_reply(socket
);
1490 pthread_mutex_unlock(socket
->lock
);
1491 health_code_update();
1496 * Ask the consumer to snapshot a specific channel using the key.
1498 * Returns LTTNG_OK on success or else an LTTng error code.
1500 enum lttng_error_code
consumer_snapshot_channel(struct consumer_socket
*socket
,
1502 const struct consumer_output
*output
,
1504 const char *channel_path
,
1505 uint64_t nb_packets_per_stream
)
1508 enum lttng_error_code status
= LTTNG_OK
;
1509 struct lttcomm_consumer_msg msg
;
1511 LTTNG_ASSERT(socket
);
1512 LTTNG_ASSERT(output
);
1514 DBG("Consumer snapshot channel key %" PRIu64
, key
);
1516 memset(&msg
, 0, sizeof(msg
));
1517 msg
.cmd_type
= LTTNG_CONSUMER_SNAPSHOT_CHANNEL
;
1518 msg
.u
.snapshot_channel
.key
= key
;
1519 msg
.u
.snapshot_channel
.nb_packets_per_stream
= nb_packets_per_stream
;
1520 msg
.u
.snapshot_channel
.metadata
= metadata
;
1522 if (output
->type
== CONSUMER_DST_NET
) {
1523 msg
.u
.snapshot_channel
.relayd_id
= output
->net_seq_index
;
1524 msg
.u
.snapshot_channel
.use_relayd
= 1;
1526 msg
.u
.snapshot_channel
.relayd_id
= (uint64_t) -1ULL;
1528 ret
= lttng_strncpy(msg
.u
.snapshot_channel
.pathname
,
1530 sizeof(msg
.u
.snapshot_channel
.pathname
));
1532 ERR("Snapshot path exceeds the maximal allowed length of %zu bytes (%zu bytes required) with path \"%s\"",
1533 sizeof(msg
.u
.snapshot_channel
.pathname
),
1534 strlen(channel_path
),
1536 status
= LTTNG_ERR_SNAPSHOT_FAIL
;
1540 health_code_update();
1541 pthread_mutex_lock(socket
->lock
);
1542 ret
= consumer_send_msg(socket
, &msg
);
1543 pthread_mutex_unlock(socket
->lock
);
1546 case LTTCOMM_CONSUMERD_CHAN_NOT_FOUND
:
1547 status
= LTTNG_ERR_CHAN_NOT_FOUND
;
1550 status
= LTTNG_ERR_SNAPSHOT_FAIL
;
1557 health_code_update();
1562 * Ask the consumer the number of discarded events for a channel.
1564 int consumer_get_discarded_events(uint64_t session_id
,
1565 uint64_t channel_key
,
1566 struct consumer_output
*consumer
,
1567 uint64_t *discarded
)
1570 struct consumer_socket
*socket
;
1571 struct lttng_ht_iter iter
;
1572 struct lttcomm_consumer_msg msg
;
1574 LTTNG_ASSERT(consumer
);
1576 DBG3("Consumer discarded events id %" PRIu64
, session_id
);
1578 memset(&msg
, 0, sizeof(msg
));
1579 msg
.cmd_type
= LTTNG_CONSUMER_DISCARDED_EVENTS
;
1580 msg
.u
.discarded_events
.session_id
= session_id
;
1581 msg
.u
.discarded_events
.channel_key
= channel_key
;
1585 /* Send command for each consumer. */
1587 lttng::urcu::read_lock_guard read_lock
;
1589 cds_lfht_for_each_entry (consumer
->socks
->ht
, &iter
.iter
, socket
, node
.node
) {
1590 uint64_t consumer_discarded
= 0;
1592 pthread_mutex_lock(socket
->lock
);
1593 ret
= consumer_socket_send(socket
, &msg
, sizeof(msg
));
1595 pthread_mutex_unlock(socket
->lock
);
1600 * No need for a recv reply status because the answer to the
1601 * command is the reply status message.
1603 ret
= consumer_socket_recv(
1604 socket
, &consumer_discarded
, sizeof(consumer_discarded
));
1606 ERR("get discarded events");
1607 pthread_mutex_unlock(socket
->lock
);
1611 pthread_mutex_unlock(socket
->lock
);
1612 *discarded
+= consumer_discarded
;
1617 DBG("Consumer discarded %" PRIu64
" events in session id %" PRIu64
, *discarded
, session_id
);
1624 * Ask the consumer the number of lost packets for a channel.
1626 int consumer_get_lost_packets(uint64_t session_id
,
1627 uint64_t channel_key
,
1628 struct consumer_output
*consumer
,
1632 struct consumer_socket
*socket
;
1633 struct lttng_ht_iter iter
;
1634 struct lttcomm_consumer_msg msg
;
1636 LTTNG_ASSERT(consumer
);
1638 DBG3("Consumer lost packets id %" PRIu64
, session_id
);
1640 memset(&msg
, 0, sizeof(msg
));
1641 msg
.cmd_type
= LTTNG_CONSUMER_LOST_PACKETS
;
1642 msg
.u
.lost_packets
.session_id
= session_id
;
1643 msg
.u
.lost_packets
.channel_key
= channel_key
;
1647 /* Send command for each consumer. */
1649 lttng::urcu::read_lock_guard read_lock
;
1651 cds_lfht_for_each_entry (consumer
->socks
->ht
, &iter
.iter
, socket
, node
.node
) {
1652 uint64_t consumer_lost
= 0;
1653 pthread_mutex_lock(socket
->lock
);
1654 ret
= consumer_socket_send(socket
, &msg
, sizeof(msg
));
1656 pthread_mutex_unlock(socket
->lock
);
1661 * No need for a recv reply status because the answer to the
1662 * command is the reply status message.
1664 ret
= consumer_socket_recv(socket
, &consumer_lost
, sizeof(consumer_lost
));
1666 ERR("get lost packets");
1667 pthread_mutex_unlock(socket
->lock
);
1670 pthread_mutex_unlock(socket
->lock
);
1671 *lost
+= consumer_lost
;
1676 DBG("Consumer lost %" PRIu64
" packets in session id %" PRIu64
, *lost
, session_id
);
1683 * Ask the consumer to rotate a channel.
1685 * The new_chunk_id is the session->rotate_count that has been incremented
1686 * when the rotation started. On the relay, this allows to keep track in which
1687 * chunk each stream is currently writing to (for the rotate_pending operation).
1689 int consumer_rotate_channel(struct consumer_socket
*socket
,
1691 struct consumer_output
*output
,
1692 bool is_metadata_channel
)
1695 struct lttcomm_consumer_msg msg
;
1697 LTTNG_ASSERT(socket
);
1699 DBG("Consumer rotate channel key %" PRIu64
, key
);
1701 pthread_mutex_lock(socket
->lock
);
1702 memset(&msg
, 0, sizeof(msg
));
1703 msg
.cmd_type
= LTTNG_CONSUMER_ROTATE_CHANNEL
;
1704 msg
.u
.rotate_channel
.key
= key
;
1705 msg
.u
.rotate_channel
.metadata
= !!is_metadata_channel
;
1707 if (output
->type
== CONSUMER_DST_NET
) {
1708 msg
.u
.rotate_channel
.relayd_id
= output
->net_seq_index
;
1710 msg
.u
.rotate_channel
.relayd_id
= (uint64_t) -1ULL;
1713 health_code_update();
1714 ret
= consumer_send_msg(socket
, &msg
);
1717 case LTTCOMM_CONSUMERD_CHAN_NOT_FOUND
:
1718 ret
= -LTTNG_ERR_CHAN_NOT_FOUND
;
1721 ret
= -LTTNG_ERR_ROTATION_FAIL_CONSUMER
;
1727 pthread_mutex_unlock(socket
->lock
);
1728 health_code_update();
1732 int consumer_open_channel_packets(struct consumer_socket
*socket
, uint64_t key
)
1735 lttcomm_consumer_msg msg
= {
1736 .cmd_type
= LTTNG_CONSUMER_OPEN_CHANNEL_PACKETS
,
1739 msg
.u
.open_channel_packets
.key
= key
;
1741 LTTNG_ASSERT(socket
);
1743 DBG("Consumer open channel packets: channel key = %" PRIu64
, key
);
1745 health_code_update();
1747 pthread_mutex_lock(socket
->lock
);
1748 ret
= consumer_send_msg(socket
, &msg
);
1749 pthread_mutex_unlock(socket
->lock
);
1755 health_code_update();
1759 int consumer_clear_channel(struct consumer_socket
*socket
, uint64_t key
)
1762 struct lttcomm_consumer_msg msg
;
1764 LTTNG_ASSERT(socket
);
1766 DBG("Consumer clear channel %" PRIu64
, key
);
1768 memset(&msg
, 0, sizeof(msg
));
1769 msg
.cmd_type
= LTTNG_CONSUMER_CLEAR_CHANNEL
;
1770 msg
.u
.clear_channel
.key
= key
;
1772 health_code_update();
1774 pthread_mutex_lock(socket
->lock
);
1775 ret
= consumer_send_msg(socket
, &msg
);
1781 pthread_mutex_unlock(socket
->lock
);
1783 health_code_update();
1787 int consumer_init(struct consumer_socket
*socket
, const lttng_uuid
& sessiond_uuid
)
1790 struct lttcomm_consumer_msg msg
= {
1791 .cmd_type
= LTTNG_CONSUMER_INIT
,
1795 LTTNG_ASSERT(socket
);
1797 DBG("Sending consumer initialization command");
1798 std::copy(sessiond_uuid
.begin(), sessiond_uuid
.end(), msg
.u
.init
.sessiond_uuid
);
1800 health_code_update();
1801 ret
= consumer_send_msg(socket
, &msg
);
1807 health_code_update();
1812 * Ask the consumer to create a new chunk for a given session.
1814 * Called with the consumer socket lock held.
1816 int consumer_create_trace_chunk(struct consumer_socket
*socket
,
1818 uint64_t session_id
,
1819 struct lttng_trace_chunk
*chunk
,
1820 const char *domain_subdir
)
1823 enum lttng_trace_chunk_status chunk_status
;
1824 struct lttng_credentials chunk_credentials
;
1825 const struct lttng_directory_handle
*chunk_directory_handle
= nullptr;
1826 struct lttng_directory_handle
*domain_handle
= nullptr;
1828 const char *chunk_name
;
1829 bool chunk_name_overridden
;
1831 time_t creation_timestamp
;
1832 char creation_timestamp_buffer
[ISO8601_STR_LEN
];
1833 const char *creation_timestamp_str
= "(none)";
1834 const bool chunk_has_local_output
= relayd_id
== -1ULL;
1835 enum lttng_trace_chunk_status tc_status
;
1836 struct lttcomm_consumer_msg msg
= {
1837 .cmd_type
= LTTNG_CONSUMER_CREATE_TRACE_CHUNK
,
1840 msg
.u
.create_trace_chunk
.session_id
= session_id
;
1842 LTTNG_ASSERT(socket
);
1843 LTTNG_ASSERT(chunk
);
1845 if (relayd_id
!= -1ULL) {
1846 LTTNG_OPTIONAL_SET(&msg
.u
.create_trace_chunk
.relayd_id
, relayd_id
);
1849 chunk_status
= lttng_trace_chunk_get_name(chunk
, &chunk_name
, &chunk_name_overridden
);
1850 if (chunk_status
!= LTTNG_TRACE_CHUNK_STATUS_OK
&&
1851 chunk_status
!= LTTNG_TRACE_CHUNK_STATUS_NONE
) {
1852 ERR("Failed to get name of trace chunk");
1853 ret
= -LTTNG_ERR_FATAL
;
1856 if (chunk_name_overridden
) {
1857 ret
= lttng_strncpy(msg
.u
.create_trace_chunk
.override_name
,
1859 sizeof(msg
.u
.create_trace_chunk
.override_name
));
1861 ERR("Trace chunk name \"%s\" exceeds the maximal length allowed by the consumer protocol",
1863 ret
= -LTTNG_ERR_FATAL
;
1868 chunk_status
= lttng_trace_chunk_get_creation_timestamp(chunk
, &creation_timestamp
);
1869 if (chunk_status
!= LTTNG_TRACE_CHUNK_STATUS_OK
) {
1870 ret
= -LTTNG_ERR_FATAL
;
1873 msg
.u
.create_trace_chunk
.creation_timestamp
= (uint64_t) creation_timestamp
;
1874 /* Only used for logging purposes. */
1875 ret
= time_to_iso8601_str(
1876 creation_timestamp
, creation_timestamp_buffer
, sizeof(creation_timestamp_buffer
));
1877 creation_timestamp_str
= !ret
? creation_timestamp_buffer
: "(formatting error)";
1879 chunk_status
= lttng_trace_chunk_get_id(chunk
, &chunk_id
);
1880 if (chunk_status
!= LTTNG_TRACE_CHUNK_STATUS_OK
) {
1882 * Anonymous trace chunks should never be transmitted
1883 * to remote peers (consumerd and relayd). They are used
1884 * internally for backward-compatibility purposes.
1886 ret
= -LTTNG_ERR_FATAL
;
1889 msg
.u
.create_trace_chunk
.chunk_id
= chunk_id
;
1891 if (chunk_has_local_output
) {
1892 chunk_status
= lttng_trace_chunk_borrow_chunk_directory_handle(
1893 chunk
, &chunk_directory_handle
);
1894 if (chunk_status
!= LTTNG_TRACE_CHUNK_STATUS_OK
) {
1895 ret
= -LTTNG_ERR_FATAL
;
1898 chunk_status
= lttng_trace_chunk_get_credentials(chunk
, &chunk_credentials
);
1899 if (chunk_status
!= LTTNG_TRACE_CHUNK_STATUS_OK
) {
1901 * Not associating credentials to a sessiond chunk is a
1902 * fatal internal error.
1904 ret
= -LTTNG_ERR_FATAL
;
1907 tc_status
= lttng_trace_chunk_create_subdirectory(chunk
, domain_subdir
);
1908 if (tc_status
!= LTTNG_TRACE_CHUNK_STATUS_OK
) {
1909 PERROR("Failed to create chunk domain output directory \"%s\"",
1911 ret
= -LTTNG_ERR_FATAL
;
1914 domain_handle
= lttng_directory_handle_create_from_handle(domain_subdir
,
1915 chunk_directory_handle
);
1916 if (!domain_handle
) {
1917 ret
= -LTTNG_ERR_FATAL
;
1922 * This will only compile on platforms that support
1923 * dirfd (POSIX.2008). This is fine as the session daemon
1924 * is only built for such platforms.
1926 * The ownership of the chunk directory handle's is maintained
1927 * by the trace chunk.
1929 domain_dirfd
= lttng_directory_handle_get_dirfd(domain_handle
);
1930 LTTNG_ASSERT(domain_dirfd
>= 0);
1932 msg
.u
.create_trace_chunk
.credentials
.value
.uid
=
1933 lttng_credentials_get_uid(&chunk_credentials
);
1934 msg
.u
.create_trace_chunk
.credentials
.value
.gid
=
1935 lttng_credentials_get_gid(&chunk_credentials
);
1936 msg
.u
.create_trace_chunk
.credentials
.is_set
= 1;
1939 DBG("Sending consumer create trace chunk command: relayd_id = %" PRId64
1940 ", session_id = %" PRIu64
", chunk_id = %" PRIu64
", creation_timestamp = %s",
1944 creation_timestamp_str
);
1945 health_code_update();
1946 ret
= consumer_send_msg(socket
, &msg
);
1947 health_code_update();
1949 ERR("Trace chunk creation error on consumer");
1950 ret
= -LTTNG_ERR_CREATE_TRACE_CHUNK_FAIL_CONSUMER
;
1954 if (chunk_has_local_output
) {
1955 DBG("Sending trace chunk domain directory fd to consumer");
1956 health_code_update();
1957 ret
= consumer_send_fds(socket
, &domain_dirfd
, 1);
1958 health_code_update();
1960 ERR("Trace chunk creation error on consumer");
1961 ret
= -LTTNG_ERR_CREATE_TRACE_CHUNK_FAIL_CONSUMER
;
1966 lttng_directory_handle_put(domain_handle
);
1971 * Ask the consumer to close a trace chunk for a given session.
1973 * Called with the consumer socket lock held.
1975 int consumer_close_trace_chunk(struct consumer_socket
*socket
,
1977 uint64_t session_id
,
1978 struct lttng_trace_chunk
*chunk
,
1979 char *closed_trace_chunk_path
)
1982 enum lttng_trace_chunk_status chunk_status
;
1983 lttcomm_consumer_msg msg
= {
1984 .cmd_type
= LTTNG_CONSUMER_CLOSE_TRACE_CHUNK
,
1987 msg
.u
.close_trace_chunk
.session_id
= session_id
;
1989 struct lttcomm_consumer_close_trace_chunk_reply reply
;
1991 time_t close_timestamp
;
1992 enum lttng_trace_chunk_command_type close_command
;
1993 const char *close_command_name
= "none";
1994 struct lttng_dynamic_buffer path_reception_buffer
;
1996 LTTNG_ASSERT(socket
);
1997 lttng_dynamic_buffer_init(&path_reception_buffer
);
1999 if (relayd_id
!= -1ULL) {
2000 LTTNG_OPTIONAL_SET(&msg
.u
.close_trace_chunk
.relayd_id
, relayd_id
);
2003 chunk_status
= lttng_trace_chunk_get_close_command(chunk
, &close_command
);
2004 switch (chunk_status
) {
2005 case LTTNG_TRACE_CHUNK_STATUS_OK
:
2006 LTTNG_OPTIONAL_SET(&msg
.u
.close_trace_chunk
.close_command
,
2007 (uint32_t) close_command
);
2009 case LTTNG_TRACE_CHUNK_STATUS_NONE
:
2012 ERR("Failed to get trace chunk close command");
2017 chunk_status
= lttng_trace_chunk_get_id(chunk
, &chunk_id
);
2019 * Anonymous trace chunks should never be transmitted to remote peers
2020 * (consumerd and relayd). They are used internally for
2021 * backward-compatibility purposes.
2023 LTTNG_ASSERT(chunk_status
== LTTNG_TRACE_CHUNK_STATUS_OK
);
2024 msg
.u
.close_trace_chunk
.chunk_id
= chunk_id
;
2026 chunk_status
= lttng_trace_chunk_get_close_timestamp(chunk
, &close_timestamp
);
2028 * A trace chunk should be closed locally before being closed remotely.
2029 * Otherwise, the close timestamp would never be transmitted to the
2032 LTTNG_ASSERT(chunk_status
== LTTNG_TRACE_CHUNK_STATUS_OK
);
2033 msg
.u
.close_trace_chunk
.close_timestamp
= (uint64_t) close_timestamp
;
2035 if (msg
.u
.close_trace_chunk
.close_command
.is_set
) {
2036 close_command_name
= lttng_trace_chunk_command_type_get_name(close_command
);
2038 DBG("Sending consumer close trace chunk command: relayd_id = %" PRId64
2039 ", session_id = %" PRIu64
", chunk_id = %" PRIu64
", close command = \"%s\"",
2043 close_command_name
);
2045 health_code_update();
2046 ret
= consumer_socket_send(socket
, &msg
, sizeof(struct lttcomm_consumer_msg
));
2048 ret
= -LTTNG_ERR_CLOSE_TRACE_CHUNK_FAIL_CONSUMER
;
2051 ret
= consumer_socket_recv(socket
, &reply
, sizeof(reply
));
2053 ret
= -LTTNG_ERR_CLOSE_TRACE_CHUNK_FAIL_CONSUMER
;
2056 if (reply
.path_length
>= LTTNG_PATH_MAX
) {
2057 ERR("Invalid path returned by relay daemon: %" PRIu32
2058 "bytes exceeds maximal allowed length of %d bytes",
2061 ret
= -LTTNG_ERR_INVALID_PROTOCOL
;
2064 ret
= lttng_dynamic_buffer_set_size(&path_reception_buffer
, reply
.path_length
);
2066 ERR("Failed to allocate reception buffer of path returned by the \"close trace chunk\" command");
2067 ret
= -LTTNG_ERR_NOMEM
;
2070 ret
= consumer_socket_recv(socket
, path_reception_buffer
.data
, path_reception_buffer
.size
);
2072 ERR("Communication error while receiving path of closed trace chunk");
2073 ret
= -LTTNG_ERR_CLOSE_TRACE_CHUNK_FAIL_CONSUMER
;
2076 if (path_reception_buffer
.data
[path_reception_buffer
.size
- 1] != '\0') {
2077 ERR("Invalid path returned by relay daemon: not null-terminated");
2078 ret
= -LTTNG_ERR_INVALID_PROTOCOL
;
2081 if (closed_trace_chunk_path
) {
2083 * closed_trace_chunk_path is assumed to have a length >=
2086 memcpy(closed_trace_chunk_path
,
2087 path_reception_buffer
.data
,
2088 path_reception_buffer
.size
);
2091 lttng_dynamic_buffer_reset(&path_reception_buffer
);
2092 health_code_update();
2097 * Ask the consumer if a trace chunk exists.
2099 * Called with the consumer socket lock held.
2100 * Returns 0 on success, or a negative value on error.
2102 int consumer_trace_chunk_exists(struct consumer_socket
*socket
,
2104 uint64_t session_id
,
2105 struct lttng_trace_chunk
*chunk
,
2106 enum consumer_trace_chunk_exists_status
*result
)
2109 enum lttng_trace_chunk_status chunk_status
;
2110 lttcomm_consumer_msg msg
= {
2111 .cmd_type
= LTTNG_CONSUMER_TRACE_CHUNK_EXISTS
,
2114 msg
.u
.trace_chunk_exists
.session_id
= session_id
;
2117 const char *consumer_reply_str
;
2119 LTTNG_ASSERT(socket
);
2121 if (relayd_id
!= -1ULL) {
2122 LTTNG_OPTIONAL_SET(&msg
.u
.trace_chunk_exists
.relayd_id
, relayd_id
);
2125 chunk_status
= lttng_trace_chunk_get_id(chunk
, &chunk_id
);
2126 if (chunk_status
!= LTTNG_TRACE_CHUNK_STATUS_OK
) {
2128 * Anonymous trace chunks should never be transmitted
2129 * to remote peers (consumerd and relayd). They are used
2130 * internally for backward-compatibility purposes.
2132 ret
= -LTTNG_ERR_FATAL
;
2135 msg
.u
.trace_chunk_exists
.chunk_id
= chunk_id
;
2137 DBG("Sending consumer trace chunk exists command: relayd_id = %" PRId64
2138 ", session_id = %" PRIu64
", chunk_id = %" PRIu64
,
2143 health_code_update();
2144 ret
= consumer_send_msg(socket
, &msg
);
2146 case LTTCOMM_CONSUMERD_UNKNOWN_TRACE_CHUNK
:
2147 consumer_reply_str
= "unknown trace chunk";
2148 *result
= CONSUMER_TRACE_CHUNK_EXISTS_STATUS_UNKNOWN_CHUNK
;
2150 case LTTCOMM_CONSUMERD_TRACE_CHUNK_EXISTS_LOCAL
:
2151 consumer_reply_str
= "trace chunk exists locally";
2152 *result
= CONSUMER_TRACE_CHUNK_EXISTS_STATUS_EXISTS_LOCAL
;
2154 case LTTCOMM_CONSUMERD_TRACE_CHUNK_EXISTS_REMOTE
:
2155 consumer_reply_str
= "trace chunk exists on remote peer";
2156 *result
= CONSUMER_TRACE_CHUNK_EXISTS_STATUS_EXISTS_REMOTE
;
2159 ERR("Consumer returned an error from TRACE_CHUNK_EXISTS command");
2163 DBG("Consumer reply to TRACE_CHUNK_EXISTS command: %s", consumer_reply_str
);
2166 health_code_update();