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
15 #include <sys/types.h>
19 #include <common/common.h>
20 #include <common/defaults.h>
21 #include <common/uri.h>
22 #include <common/relayd/relayd.h>
23 #include <common/string-utils/format.h>
26 #include "health-sessiond.h"
29 #include "lttng-sessiond.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
, size_t *consumer_path_offset
)
50 * Allocate the string ourself to make sure we never exceed
53 pathname
= zmalloc(LTTNG_PATH_MAX
);
58 /* Get correct path name destination */
59 if (consumer
->type
== CONSUMER_DST_NET
&&
60 consumer
->relay_major_version
== 2 &&
61 consumer
->relay_minor_version
< 11) {
62 ret
= snprintf(pathname
, LTTNG_PATH_MAX
, "%s%s/%s/%s",
63 consumer
->dst
.net
.base_dir
,
64 consumer
->chunk_path
, consumer
->domain_subdir
,
66 *consumer_path_offset
= 0;
68 ret
= snprintf(pathname
, LTTNG_PATH_MAX
, "%s/%s",
69 consumer
->domain_subdir
, session_path
);
70 *consumer_path_offset
= strlen(consumer
->domain_subdir
) + 1;
72 DBG3("Consumer trace path relative to current trace chunk: \"%s\"",
75 PERROR("Failed to format channel path");
77 } else if (ret
>= LTTNG_PATH_MAX
) {
78 ERR("Truncation occurred while formatting channel path");
89 * Send a data payload using a given consumer socket of size len.
91 * The consumer socket lock MUST be acquired before calling this since this
92 * function can change the fd value.
94 * Return 0 on success else a negative value on error.
96 int consumer_socket_send(struct consumer_socket
*socket
, void *msg
, size_t len
)
102 assert(socket
->fd_ptr
);
105 /* Consumer socket is invalid. Stopping. */
106 fd
= *socket
->fd_ptr
;
111 size
= lttcomm_send_unix_sock(fd
, msg
, len
);
113 /* The above call will print a PERROR on error. */
114 DBG("Error when sending data to consumer on sock %d", fd
);
116 * At this point, the socket is not usable anymore thus closing it and
117 * setting the file descriptor to -1 so it is not reused.
120 /* This call will PERROR on error. */
121 (void) lttcomm_close_unix_sock(fd
);
122 *socket
->fd_ptr
= -1;
133 * Receive a data payload using a given consumer socket of size len.
135 * The consumer socket lock MUST be acquired before calling this since this
136 * function can change the fd value.
138 * Return 0 on success else a negative value on error.
140 int consumer_socket_recv(struct consumer_socket
*socket
, void *msg
, size_t len
)
146 assert(socket
->fd_ptr
);
149 /* Consumer socket is invalid. Stopping. */
150 fd
= *socket
->fd_ptr
;
155 size
= lttcomm_recv_unix_sock(fd
, msg
, len
);
157 /* The above call will print a PERROR on error. */
158 DBG("Error when receiving data from the consumer socket %d", fd
);
160 * At this point, the socket is not usable anymore thus closing it and
161 * setting the file descriptor to -1 so it is not reused.
164 /* This call will PERROR on error. */
165 (void) lttcomm_close_unix_sock(fd
);
166 *socket
->fd_ptr
= -1;
177 * Receive a reply command status message from the consumer. Consumer socket
178 * lock MUST be acquired before calling this function.
180 * Return 0 on success, -1 on recv error or a negative lttng error code which
181 * was possibly returned by the consumer.
183 int consumer_recv_status_reply(struct consumer_socket
*sock
)
186 struct lttcomm_consumer_status_msg reply
;
190 ret
= consumer_socket_recv(sock
, &reply
, sizeof(reply
));
195 if (reply
.ret_code
== LTTCOMM_CONSUMERD_SUCCESS
) {
199 ret
= -reply
.ret_code
;
200 DBG("Consumer ret code %d", ret
);
208 * Once the ASK_CHANNEL command is sent to the consumer, the channel
209 * information are sent back. This call receives that data and populates key
212 * On success return 0 and both key and stream_count are set. On error, a
213 * negative value is sent back and both parameters are untouched.
215 int consumer_recv_status_channel(struct consumer_socket
*sock
,
216 uint64_t *key
, unsigned int *stream_count
)
219 struct lttcomm_consumer_status_channel reply
;
222 assert(stream_count
);
225 ret
= consumer_socket_recv(sock
, &reply
, sizeof(reply
));
230 /* An error is possible so don't touch the key and stream_count. */
231 if (reply
.ret_code
!= LTTCOMM_CONSUMERD_SUCCESS
) {
237 *stream_count
= reply
.stream_count
;
245 * Send destroy relayd command to consumer.
247 * On success return positive value. On error, negative value.
249 int consumer_send_destroy_relayd(struct consumer_socket
*sock
,
250 struct consumer_output
*consumer
)
253 struct lttcomm_consumer_msg msg
;
258 DBG2("Sending destroy relayd command to consumer sock %d", *sock
->fd_ptr
);
260 memset(&msg
, 0, sizeof(msg
));
261 msg
.cmd_type
= LTTNG_CONSUMER_DESTROY_RELAYD
;
262 msg
.u
.destroy_relayd
.net_seq_idx
= consumer
->net_seq_index
;
264 pthread_mutex_lock(sock
->lock
);
265 ret
= consumer_socket_send(sock
, &msg
, sizeof(msg
));
270 /* Don't check the return value. The caller will do it. */
271 ret
= consumer_recv_status_reply(sock
);
273 DBG2("Consumer send destroy relayd command done");
276 pthread_mutex_unlock(sock
->lock
);
281 * For each consumer socket in the consumer output object, send a destroy
284 void consumer_output_send_destroy_relayd(struct consumer_output
*consumer
)
286 struct lttng_ht_iter iter
;
287 struct consumer_socket
*socket
;
291 /* Destroy any relayd connection */
292 if (consumer
->type
== CONSUMER_DST_NET
) {
294 cds_lfht_for_each_entry(consumer
->socks
->ht
, &iter
.iter
, socket
,
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
,
316 struct consumer_output
*output
)
319 struct consumer_socket
*socket
;
323 if (output
== NULL
|| data
->cmd_sock
< 0) {
325 * Not an error. Possible there is simply not spawned consumer or it's
326 * disabled for the tracing session asking the socket.
332 socket
= consumer_find_socket(data
->cmd_sock
, output
);
334 if (socket
== NULL
) {
335 socket
= consumer_allocate_socket(&data
->cmd_sock
);
336 if (socket
== NULL
) {
341 socket
->registered
= 0;
342 socket
->lock
= &data
->lock
;
344 consumer_add_socket(socket
, output
);
348 socket
->type
= data
->type
;
350 DBG3("Consumer socket created (fd: %d) and added to output",
358 * Return the consumer socket from the given consumer output with the right
359 * bitness. On error, returns NULL.
361 * The caller MUST acquire a rcu read side lock and keep it until the socket
362 * object reference is not needed anymore.
364 struct consumer_socket
*consumer_find_socket_by_bitness(int bits
,
365 const struct consumer_output
*consumer
)
368 struct consumer_socket
*socket
= NULL
;
372 consumer_fd
= uatomic_read(&ust_consumerd64_fd
);
375 consumer_fd
= uatomic_read(&ust_consumerd32_fd
);
382 socket
= consumer_find_socket(consumer_fd
, consumer
);
384 ERR("Consumer socket fd %d not found in consumer obj %p",
385 consumer_fd
, consumer
);
393 * Find a consumer_socket in a consumer_output hashtable. Read side lock must
394 * be acquired before calling this function and across use of the
395 * returned consumer_socket.
397 struct consumer_socket
*consumer_find_socket(int key
,
398 const struct consumer_output
*consumer
)
400 struct lttng_ht_iter iter
;
401 struct lttng_ht_node_ulong
*node
;
402 struct consumer_socket
*socket
= NULL
;
404 /* Negative keys are lookup failures */
405 if (key
< 0 || consumer
== NULL
) {
409 lttng_ht_lookup(consumer
->socks
, (void *)((unsigned long) key
),
411 node
= lttng_ht_iter_get_node_ulong(&iter
);
413 socket
= caa_container_of(node
, struct consumer_socket
, node
);
420 * Allocate a new consumer_socket and return the pointer.
422 struct consumer_socket
*consumer_allocate_socket(int *fd
)
424 struct consumer_socket
*socket
= NULL
;
428 socket
= zmalloc(sizeof(struct consumer_socket
));
429 if (socket
== NULL
) {
430 PERROR("zmalloc consumer socket");
435 lttng_ht_node_init_ulong(&socket
->node
, *fd
);
442 * Add consumer socket to consumer output object. Read side lock must be
443 * acquired before calling this function.
445 void consumer_add_socket(struct consumer_socket
*sock
,
446 struct consumer_output
*consumer
)
451 lttng_ht_add_unique_ulong(consumer
->socks
, &sock
->node
);
455 * Delete consumer socket to consumer output object. Read side lock must be
456 * acquired before calling this function.
458 void consumer_del_socket(struct consumer_socket
*sock
,
459 struct consumer_output
*consumer
)
462 struct lttng_ht_iter iter
;
467 iter
.iter
.node
= &sock
->node
.node
;
468 ret
= lttng_ht_del(consumer
->socks
, &iter
);
473 * RCU destroy call function.
475 static void destroy_socket_rcu(struct rcu_head
*head
)
477 struct lttng_ht_node_ulong
*node
=
478 caa_container_of(head
, struct lttng_ht_node_ulong
, head
);
479 struct consumer_socket
*socket
=
480 caa_container_of(node
, struct consumer_socket
, node
);
486 * Destroy and free socket pointer in a call RCU. Read side lock must be
487 * acquired before calling this function.
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
= NULL
;
515 output
= zmalloc(sizeof(struct consumer_output
));
516 if (output
== NULL
) {
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 * Should *NOT* be called with RCU read-side lock held.
560 static void consumer_release_output(struct urcu_ref
*ref
)
562 struct consumer_output
*obj
=
563 caa_container_of(ref
, struct consumer_output
, ref
);
565 consumer_destroy_output_sockets(obj
);
568 /* Finally destroy HT */
569 ht_cleanup_push(obj
->socks
);
576 * Get the consumer_output object.
578 void consumer_output_get(struct consumer_output
*obj
)
580 urcu_ref_get(&obj
->ref
);
584 * Put the consumer_output object.
586 * Should *NOT* be called with RCU read-side lock held.
588 void consumer_output_put(struct consumer_output
*obj
)
593 urcu_ref_put(&obj
->ref
, consumer_release_output
);
597 * Copy consumer output and returned the newly allocated copy.
599 * Should *NOT* be called with RCU read-side lock held.
601 struct consumer_output
*consumer_copy_output(struct consumer_output
*src
)
604 struct consumer_output
*output
;
608 output
= consumer_create_output(src
->type
);
609 if (output
== NULL
) {
612 output
->enabled
= src
->enabled
;
613 output
->net_seq_index
= src
->net_seq_index
;
614 memcpy(output
->domain_subdir
, src
->domain_subdir
,
615 sizeof(output
->domain_subdir
));
616 output
->snapshot
= src
->snapshot
;
617 output
->relay_major_version
= src
->relay_major_version
;
618 output
->relay_minor_version
= src
->relay_minor_version
;
619 output
->relay_allows_clear
= src
->relay_allows_clear
;
620 memcpy(&output
->dst
, &src
->dst
, sizeof(output
->dst
));
621 ret
= consumer_copy_sockets(output
, src
);
629 consumer_output_put(output
);
634 * Copy consumer sockets from src to dst.
636 * Return 0 on success or else a negative value.
638 int consumer_copy_sockets(struct consumer_output
*dst
,
639 struct consumer_output
*src
)
642 struct lttng_ht_iter iter
;
643 struct consumer_socket
*socket
, *copy_sock
;
649 cds_lfht_for_each_entry(src
->socks
->ht
, &iter
.iter
, socket
, node
.node
) {
650 /* Ignore socket that are already there. */
651 copy_sock
= consumer_find_socket(*socket
->fd_ptr
, dst
);
656 /* Create new socket object. */
657 copy_sock
= consumer_allocate_socket(socket
->fd_ptr
);
658 if (copy_sock
== NULL
) {
664 copy_sock
->registered
= socket
->registered
;
666 * This is valid because this lock is shared accross all consumer
667 * object being the global lock of the consumer data structure of the
670 copy_sock
->lock
= socket
->lock
;
671 consumer_add_socket(copy_sock
, dst
);
680 * Set network URI to the consumer output.
682 * Return 0 on success. Return 1 if the URI were equal. Else, negative value on
685 int consumer_set_network_uri(const struct ltt_session
*session
,
686 struct consumer_output
*output
,
687 struct lttng_uri
*uri
)
690 struct lttng_uri
*dst_uri
= NULL
;
692 /* Code flow error safety net. */
696 switch (uri
->stype
) {
697 case LTTNG_STREAM_CONTROL
:
698 dst_uri
= &output
->dst
.net
.control
;
699 output
->dst
.net
.control_isset
= 1;
700 if (uri
->port
== 0) {
701 /* Assign default port. */
702 uri
->port
= DEFAULT_NETWORK_CONTROL_PORT
;
704 if (output
->dst
.net
.data_isset
&& uri
->port
==
705 output
->dst
.net
.data
.port
) {
706 ret
= -LTTNG_ERR_INVALID
;
710 DBG3("Consumer control URI set with port %d", uri
->port
);
712 case LTTNG_STREAM_DATA
:
713 dst_uri
= &output
->dst
.net
.data
;
714 output
->dst
.net
.data_isset
= 1;
715 if (uri
->port
== 0) {
716 /* Assign default port. */
717 uri
->port
= DEFAULT_NETWORK_DATA_PORT
;
719 if (output
->dst
.net
.control_isset
&& uri
->port
==
720 output
->dst
.net
.control
.port
) {
721 ret
= -LTTNG_ERR_INVALID
;
725 DBG3("Consumer data URI set with port %d", uri
->port
);
728 ERR("Set network uri type unknown %d", uri
->stype
);
729 ret
= -LTTNG_ERR_INVALID
;
733 ret
= uri_compare(dst_uri
, uri
);
735 /* Same URI, don't touch it and return success. */
736 DBG3("URI network compare are the same");
740 /* URIs were not equal, replacing it. */
741 memcpy(dst_uri
, uri
, sizeof(struct lttng_uri
));
742 output
->type
= CONSUMER_DST_NET
;
743 if (dst_uri
->stype
!= LTTNG_STREAM_CONTROL
) {
744 /* Only the control uri needs to contain the path. */
749 * If the user has specified a subdir as part of the control
750 * URL, the session's base output directory is:
751 * /RELAYD_OUTPUT_PATH/HOSTNAME/USER_SPECIFIED_DIR
753 * Hence, the "base_dir" from which all stream files and
754 * session rotation chunks are created takes the form
755 * /HOSTNAME/USER_SPECIFIED_DIR
757 * If the user has not specified an output directory as part of
758 * the control URL, the base output directory has the form:
759 * /RELAYD_OUTPUT_PATH/HOSTNAME/SESSION_NAME-CREATION_TIME
761 * Hence, the "base_dir" from which all stream files and
762 * session rotation chunks are created takes the form
763 * /HOSTNAME/SESSION_NAME-CREATION_TIME
765 * Note that automatically generated session names already
766 * contain the session's creation time. In that case, the
767 * creation time is omitted to prevent it from being duplicated
768 * in the final directory hierarchy.
771 if (strstr(uri
->subdir
, "../")) {
772 ERR("Network URI subdirs are not allowed to walk up the path hierarchy");
773 ret
= -LTTNG_ERR_INVALID
;
776 ret
= snprintf(output
->dst
.net
.base_dir
,
777 sizeof(output
->dst
.net
.base_dir
),
778 "/%s/%s/", session
->hostname
, uri
->subdir
);
780 if (session
->has_auto_generated_name
) {
781 ret
= snprintf(output
->dst
.net
.base_dir
,
782 sizeof(output
->dst
.net
.base_dir
),
783 "/%s/%s/", session
->hostname
,
786 char session_creation_datetime
[16];
790 timeinfo
= localtime(&session
->creation_time
);
792 ret
= -LTTNG_ERR_FATAL
;
795 strftime_ret
= strftime(session_creation_datetime
,
796 sizeof(session_creation_datetime
),
797 "%Y%m%d-%H%M%S", timeinfo
);
798 if (strftime_ret
== 0) {
799 ERR("Failed to format session creation timestamp while setting network URI");
800 ret
= -LTTNG_ERR_FATAL
;
803 ret
= snprintf(output
->dst
.net
.base_dir
,
804 sizeof(output
->dst
.net
.base_dir
),
805 "/%s/%s-%s/", session
->hostname
,
807 session_creation_datetime
);
810 if (ret
>= sizeof(output
->dst
.net
.base_dir
)) {
811 ret
= -LTTNG_ERR_INVALID
;
812 ERR("Truncation occurred while setting network output base directory");
814 } else if (ret
== -1) {
815 ret
= -LTTNG_ERR_INVALID
;
816 PERROR("Error occurred while setting network output base directory");
820 DBG3("Consumer set network uri base_dir path %s",
821 output
->dst
.net
.base_dir
);
832 * Send file descriptor to consumer via sock.
834 * The consumer socket lock must be held by the caller.
836 int consumer_send_fds(struct consumer_socket
*sock
, const int *fds
,
844 assert(pthread_mutex_trylock(sock
->lock
) == EBUSY
);
846 ret
= lttcomm_send_fds_unix_sock(*sock
->fd_ptr
, fds
, nb_fd
);
848 /* The above call will print a PERROR on error. */
849 DBG("Error when sending consumer fds on sock %d", *sock
->fd_ptr
);
853 ret
= consumer_recv_status_reply(sock
);
859 * Consumer send communication message structure to consumer.
861 * The consumer socket lock must be held by the caller.
863 int consumer_send_msg(struct consumer_socket
*sock
,
864 struct lttcomm_consumer_msg
*msg
)
870 assert(pthread_mutex_trylock(sock
->lock
) == EBUSY
);
872 ret
= consumer_socket_send(sock
, msg
, sizeof(struct lttcomm_consumer_msg
));
877 ret
= consumer_recv_status_reply(sock
);
884 * Consumer send channel communication message structure to consumer.
886 * The consumer socket lock must be held by the caller.
888 int consumer_send_channel(struct consumer_socket
*sock
,
889 struct lttcomm_consumer_msg
*msg
)
896 ret
= consumer_send_msg(sock
, msg
);
906 * Populate the given consumer msg structure with the ask_channel command
909 void consumer_init_ask_channel_comm_msg(struct lttcomm_consumer_msg
*msg
,
910 uint64_t subbuf_size
,
913 unsigned int switch_timer_interval
,
914 unsigned int read_timer_interval
,
915 unsigned int live_timer_interval
,
916 bool is_in_live_session
,
917 unsigned int monitor_timer_interval
,
921 const char *pathname
,
927 uint64_t tracefile_size
,
928 uint64_t tracefile_count
,
929 uint64_t session_id_per_pid
,
930 unsigned int monitor
,
931 uint32_t ust_app_uid
,
932 int64_t blocking_timeout
,
933 const char *root_shm_path
,
934 const char *shm_path
,
935 struct lttng_trace_chunk
*trace_chunk
,
936 const struct lttng_credentials
*buffer_credentials
)
940 /* Zeroed structure */
941 memset(msg
, 0, sizeof(struct lttcomm_consumer_msg
));
942 msg
->u
.ask_channel
.buffer_credentials
.uid
= UINT32_MAX
;
943 msg
->u
.ask_channel
.buffer_credentials
.gid
= UINT32_MAX
;
947 enum lttng_trace_chunk_status chunk_status
;
949 chunk_status
= lttng_trace_chunk_get_id(trace_chunk
, &chunk_id
);
950 assert(chunk_status
== LTTNG_TRACE_CHUNK_STATUS_OK
);
951 LTTNG_OPTIONAL_SET(&msg
->u
.ask_channel
.chunk_id
, chunk_id
);
953 msg
->u
.ask_channel
.buffer_credentials
.uid
= buffer_credentials
->uid
;
954 msg
->u
.ask_channel
.buffer_credentials
.gid
= buffer_credentials
->gid
;
956 msg
->cmd_type
= LTTNG_CONSUMER_ASK_CHANNEL_CREATION
;
957 msg
->u
.ask_channel
.subbuf_size
= subbuf_size
;
958 msg
->u
.ask_channel
.num_subbuf
= num_subbuf
;
959 msg
->u
.ask_channel
.overwrite
= overwrite
;
960 msg
->u
.ask_channel
.switch_timer_interval
= switch_timer_interval
;
961 msg
->u
.ask_channel
.read_timer_interval
= read_timer_interval
;
962 msg
->u
.ask_channel
.live_timer_interval
= live_timer_interval
;
963 msg
->u
.ask_channel
.is_live
= is_in_live_session
;
964 msg
->u
.ask_channel
.monitor_timer_interval
= monitor_timer_interval
;
965 msg
->u
.ask_channel
.output
= output
;
966 msg
->u
.ask_channel
.type
= type
;
967 msg
->u
.ask_channel
.session_id
= session_id
;
968 msg
->u
.ask_channel
.session_id_per_pid
= session_id_per_pid
;
969 msg
->u
.ask_channel
.relayd_id
= relayd_id
;
970 msg
->u
.ask_channel
.key
= key
;
971 msg
->u
.ask_channel
.chan_id
= chan_id
;
972 msg
->u
.ask_channel
.tracefile_size
= tracefile_size
;
973 msg
->u
.ask_channel
.tracefile_count
= tracefile_count
;
974 msg
->u
.ask_channel
.monitor
= monitor
;
975 msg
->u
.ask_channel
.ust_app_uid
= ust_app_uid
;
976 msg
->u
.ask_channel
.blocking_timeout
= blocking_timeout
;
978 memcpy(msg
->u
.ask_channel
.uuid
, uuid
, sizeof(msg
->u
.ask_channel
.uuid
));
981 strncpy(msg
->u
.ask_channel
.pathname
, pathname
,
982 sizeof(msg
->u
.ask_channel
.pathname
));
983 msg
->u
.ask_channel
.pathname
[sizeof(msg
->u
.ask_channel
.pathname
)-1] = '\0';
986 strncpy(msg
->u
.ask_channel
.name
, name
, sizeof(msg
->u
.ask_channel
.name
));
987 msg
->u
.ask_channel
.name
[sizeof(msg
->u
.ask_channel
.name
) - 1] = '\0';
990 strncpy(msg
->u
.ask_channel
.root_shm_path
, root_shm_path
,
991 sizeof(msg
->u
.ask_channel
.root_shm_path
));
992 msg
->u
.ask_channel
.root_shm_path
[sizeof(msg
->u
.ask_channel
.root_shm_path
) - 1] = '\0';
995 strncpy(msg
->u
.ask_channel
.shm_path
, shm_path
,
996 sizeof(msg
->u
.ask_channel
.shm_path
));
997 msg
->u
.ask_channel
.shm_path
[sizeof(msg
->u
.ask_channel
.shm_path
) - 1] = '\0';
1002 * Init channel communication message structure.
1004 void consumer_init_add_channel_comm_msg(struct lttcomm_consumer_msg
*msg
,
1005 uint64_t channel_key
,
1006 uint64_t session_id
,
1007 const char *pathname
,
1012 unsigned int nb_init_streams
,
1013 enum lttng_event_output output
,
1015 uint64_t tracefile_size
,
1016 uint64_t tracefile_count
,
1017 unsigned int monitor
,
1018 unsigned int live_timer_interval
,
1019 bool is_in_live_session
,
1020 unsigned int monitor_timer_interval
,
1021 struct lttng_trace_chunk
*trace_chunk
)
1025 /* Zeroed structure */
1026 memset(msg
, 0, sizeof(struct lttcomm_consumer_msg
));
1030 enum lttng_trace_chunk_status chunk_status
;
1032 chunk_status
= lttng_trace_chunk_get_id(trace_chunk
, &chunk_id
);
1033 assert(chunk_status
== LTTNG_TRACE_CHUNK_STATUS_OK
);
1034 LTTNG_OPTIONAL_SET(&msg
->u
.channel
.chunk_id
, chunk_id
);
1038 msg
->cmd_type
= LTTNG_CONSUMER_ADD_CHANNEL
;
1039 msg
->u
.channel
.channel_key
= channel_key
;
1040 msg
->u
.channel
.session_id
= session_id
;
1041 msg
->u
.channel
.relayd_id
= relayd_id
;
1042 msg
->u
.channel
.nb_init_streams
= nb_init_streams
;
1043 msg
->u
.channel
.output
= output
;
1044 msg
->u
.channel
.type
= type
;
1045 msg
->u
.channel
.tracefile_size
= tracefile_size
;
1046 msg
->u
.channel
.tracefile_count
= tracefile_count
;
1047 msg
->u
.channel
.monitor
= monitor
;
1048 msg
->u
.channel
.live_timer_interval
= live_timer_interval
;
1049 msg
->u
.channel
.is_live
= is_in_live_session
;
1050 msg
->u
.channel
.monitor_timer_interval
= monitor_timer_interval
;
1052 strncpy(msg
->u
.channel
.pathname
, pathname
,
1053 sizeof(msg
->u
.channel
.pathname
));
1054 msg
->u
.channel
.pathname
[sizeof(msg
->u
.channel
.pathname
) - 1] = '\0';
1056 strncpy(msg
->u
.channel
.name
, name
, sizeof(msg
->u
.channel
.name
));
1057 msg
->u
.channel
.name
[sizeof(msg
->u
.channel
.name
) - 1] = '\0';
1061 * Init stream communication message structure.
1063 void consumer_init_add_stream_comm_msg(struct lttcomm_consumer_msg
*msg
,
1064 uint64_t channel_key
,
1065 uint64_t stream_key
,
1070 memset(msg
, 0, sizeof(struct lttcomm_consumer_msg
));
1072 msg
->cmd_type
= LTTNG_CONSUMER_ADD_STREAM
;
1073 msg
->u
.stream
.channel_key
= channel_key
;
1074 msg
->u
.stream
.stream_key
= stream_key
;
1075 msg
->u
.stream
.cpu
= cpu
;
1078 void consumer_init_streams_sent_comm_msg(struct lttcomm_consumer_msg
*msg
,
1079 enum lttng_consumer_command cmd
,
1080 uint64_t channel_key
, uint64_t net_seq_idx
)
1084 memset(msg
, 0, sizeof(struct lttcomm_consumer_msg
));
1086 msg
->cmd_type
= cmd
;
1087 msg
->u
.sent_streams
.channel_key
= channel_key
;
1088 msg
->u
.sent_streams
.net_seq_idx
= net_seq_idx
;
1092 * Send stream communication structure to the consumer.
1094 int consumer_send_stream(struct consumer_socket
*sock
,
1095 struct consumer_output
*dst
, struct lttcomm_consumer_msg
*msg
,
1096 const int *fds
, size_t nb_fd
)
1105 ret
= consumer_send_msg(sock
, msg
);
1110 ret
= consumer_send_fds(sock
, fds
, nb_fd
);
1120 * Send relayd socket to consumer associated with a session name.
1122 * The consumer socket lock must be held by the caller.
1124 * On success return positive value. On error, negative value.
1126 int consumer_send_relayd_socket(struct consumer_socket
*consumer_sock
,
1127 struct lttcomm_relayd_sock
*rsock
, struct consumer_output
*consumer
,
1128 enum lttng_stream_type type
, uint64_t session_id
,
1129 const char *session_name
, const char *hostname
,
1130 const char *base_path
, int session_live_timer
,
1131 const uint64_t *current_chunk_id
, time_t session_creation_time
,
1132 bool session_name_contains_creation_time
)
1135 struct lttcomm_consumer_msg msg
;
1137 /* Code flow error. Safety net. */
1140 assert(consumer_sock
);
1142 memset(&msg
, 0, sizeof(msg
));
1143 /* Bail out if consumer is disabled */
1144 if (!consumer
->enabled
) {
1149 if (type
== LTTNG_STREAM_CONTROL
) {
1150 char output_path
[LTTNG_PATH_MAX
] = {};
1151 uint64_t relayd_session_id
;
1153 ret
= relayd_create_session(rsock
,
1155 session_name
, hostname
, base_path
,
1157 consumer
->snapshot
, session_id
,
1158 sessiond_uuid
, current_chunk_id
,
1159 session_creation_time
,
1160 session_name_contains_creation_time
,
1163 /* Close the control socket. */
1164 (void) relayd_close(rsock
);
1167 msg
.u
.relayd_sock
.relayd_session_id
= relayd_session_id
;
1168 DBG("Created session on relay, output path reply: %s",
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 memcpy(&msg
.u
.relayd_sock
.sock
, rsock
, sizeof(msg
.u
.relayd_sock
.sock
));
1183 DBG3("Sending relayd sock info to consumer on %d", *consumer_sock
->fd_ptr
);
1184 ret
= consumer_send_msg(consumer_sock
, &msg
);
1189 DBG3("Sending relayd socket file descriptor to consumer");
1190 ret
= consumer_send_fds(consumer_sock
, ALIGNED_CONST_PTR(rsock
->sock
.fd
), 1);
1195 DBG2("Consumer relayd socket sent");
1202 int consumer_send_pipe(struct consumer_socket
*consumer_sock
,
1203 enum lttng_consumer_command cmd
, int pipe
)
1206 struct lttcomm_consumer_msg msg
;
1207 const char *pipe_name
;
1208 const char *command_name
;
1211 case LTTNG_CONSUMER_SET_CHANNEL_MONITOR_PIPE
:
1212 pipe_name
= "channel monitor";
1213 command_name
= "SET_CHANNEL_MONITOR_PIPE";
1216 ERR("Unexpected command received in %s (cmd = %d)", __func__
,
1221 /* Code flow error. Safety net. */
1223 memset(&msg
, 0, sizeof(msg
));
1226 pthread_mutex_lock(consumer_sock
->lock
);
1227 DBG3("Sending %s command to consumer", command_name
);
1228 ret
= consumer_send_msg(consumer_sock
, &msg
);
1233 DBG3("Sending %s pipe %d to consumer on socket %d",
1235 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
,
1250 return consumer_send_pipe(consumer_sock
,
1251 LTTNG_CONSUMER_SET_CHANNEL_MONITOR_PIPE
, pipe
);
1255 * Ask the consumer if the data is pending for the specific session id.
1256 * Returns 1 if data is pending, 0 otherwise, or < 0 on error.
1258 int consumer_is_data_pending(uint64_t session_id
,
1259 struct consumer_output
*consumer
)
1262 int32_t ret_code
= 0; /* Default is that the data is NOT pending */
1263 struct consumer_socket
*socket
;
1264 struct lttng_ht_iter iter
;
1265 struct lttcomm_consumer_msg msg
;
1269 DBG3("Consumer data pending for id %" PRIu64
, session_id
);
1271 memset(&msg
, 0, sizeof(msg
));
1272 msg
.cmd_type
= LTTNG_CONSUMER_DATA_PENDING
;
1273 msg
.u
.data_pending
.session_id
= session_id
;
1275 /* Send command for each consumer */
1277 cds_lfht_for_each_entry(consumer
->socks
->ht
, &iter
.iter
, socket
,
1279 pthread_mutex_lock(socket
->lock
);
1280 ret
= consumer_socket_send(socket
, &msg
, sizeof(msg
));
1282 pthread_mutex_unlock(socket
->lock
);
1287 * No need for a recv reply status because the answer to the command is
1288 * the reply status message.
1291 ret
= consumer_socket_recv(socket
, &ret_code
, sizeof(ret_code
));
1293 pthread_mutex_unlock(socket
->lock
);
1296 pthread_mutex_unlock(socket
->lock
);
1298 if (ret_code
== 1) {
1304 DBG("Consumer data is %s pending for session id %" PRIu64
,
1305 ret_code
== 1 ? "" : "NOT", session_id
);
1314 * Send a flush command to consumer using the given channel key.
1316 * Return 0 on success else a negative value.
1318 int consumer_flush_channel(struct consumer_socket
*socket
, uint64_t key
)
1321 struct lttcomm_consumer_msg msg
;
1325 DBG2("Consumer flush channel key %" PRIu64
, key
);
1327 memset(&msg
, 0, sizeof(msg
));
1328 msg
.cmd_type
= LTTNG_CONSUMER_FLUSH_CHANNEL
;
1329 msg
.u
.flush_channel
.key
= key
;
1331 pthread_mutex_lock(socket
->lock
);
1332 health_code_update();
1334 ret
= consumer_send_msg(socket
, &msg
);
1340 health_code_update();
1341 pthread_mutex_unlock(socket
->lock
);
1346 * Send a clear quiescent command to consumer using the given channel key.
1348 * Return 0 on success else a negative value.
1350 int consumer_clear_quiescent_channel(struct consumer_socket
*socket
, uint64_t key
)
1353 struct lttcomm_consumer_msg msg
;
1357 DBG2("Consumer clear quiescent channel key %" PRIu64
, key
);
1359 memset(&msg
, 0, sizeof(msg
));
1360 msg
.cmd_type
= LTTNG_CONSUMER_CLEAR_QUIESCENT_CHANNEL
;
1361 msg
.u
.clear_quiescent_channel
.key
= key
;
1363 pthread_mutex_lock(socket
->lock
);
1364 health_code_update();
1366 ret
= consumer_send_msg(socket
, &msg
);
1372 health_code_update();
1373 pthread_mutex_unlock(socket
->lock
);
1378 * Send a close metadata command to consumer using the given channel key.
1379 * Called with registry lock held.
1381 * Return 0 on success else a negative value.
1383 int consumer_close_metadata(struct consumer_socket
*socket
,
1384 uint64_t metadata_key
)
1387 struct lttcomm_consumer_msg msg
;
1391 DBG2("Consumer close metadata channel key %" PRIu64
, metadata_key
);
1393 memset(&msg
, 0, sizeof(msg
));
1394 msg
.cmd_type
= LTTNG_CONSUMER_CLOSE_METADATA
;
1395 msg
.u
.close_metadata
.key
= metadata_key
;
1397 pthread_mutex_lock(socket
->lock
);
1398 health_code_update();
1400 ret
= consumer_send_msg(socket
, &msg
);
1406 health_code_update();
1407 pthread_mutex_unlock(socket
->lock
);
1412 * Send a setup metdata command to consumer using the given channel key.
1414 * Return 0 on success else a negative value.
1416 int consumer_setup_metadata(struct consumer_socket
*socket
,
1417 uint64_t metadata_key
)
1420 struct lttcomm_consumer_msg msg
;
1424 DBG2("Consumer setup metadata channel key %" PRIu64
, metadata_key
);
1426 memset(&msg
, 0, sizeof(msg
));
1427 msg
.cmd_type
= LTTNG_CONSUMER_SETUP_METADATA
;
1428 msg
.u
.setup_metadata
.key
= metadata_key
;
1430 pthread_mutex_lock(socket
->lock
);
1431 health_code_update();
1433 ret
= consumer_send_msg(socket
, &msg
);
1439 health_code_update();
1440 pthread_mutex_unlock(socket
->lock
);
1445 * Send metadata string to consumer.
1446 * RCU read-side lock must be held to guarantee existence of socket.
1448 * Return 0 on success else a negative value.
1450 int consumer_push_metadata(struct consumer_socket
*socket
,
1451 uint64_t metadata_key
, char *metadata_str
, size_t len
,
1452 size_t target_offset
, uint64_t version
)
1455 struct lttcomm_consumer_msg msg
;
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
,
1479 ret
= consumer_socket_send(socket
, metadata_str
, len
);
1484 health_code_update();
1485 ret
= consumer_recv_status_reply(socket
);
1491 pthread_mutex_unlock(socket
->lock
);
1492 health_code_update();
1497 * Ask the consumer to snapshot a specific channel using the key.
1499 * Returns LTTNG_OK on success or else an LTTng error code.
1501 enum lttng_error_code
consumer_snapshot_channel(struct consumer_socket
*socket
,
1502 uint64_t key
, const struct consumer_output
*output
, int metadata
,
1503 uid_t uid
, gid_t gid
, const char *channel_path
, int wait
,
1504 uint64_t nb_packets_per_stream
)
1507 enum lttng_error_code status
= LTTNG_OK
;
1508 struct lttcomm_consumer_msg msg
;
1513 DBG("Consumer snapshot channel key %" PRIu64
, key
);
1515 memset(&msg
, 0, sizeof(msg
));
1516 msg
.cmd_type
= LTTNG_CONSUMER_SNAPSHOT_CHANNEL
;
1517 msg
.u
.snapshot_channel
.key
= key
;
1518 msg
.u
.snapshot_channel
.nb_packets_per_stream
= nb_packets_per_stream
;
1519 msg
.u
.snapshot_channel
.metadata
= metadata
;
1521 if (output
->type
== CONSUMER_DST_NET
) {
1522 msg
.u
.snapshot_channel
.relayd_id
=
1523 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
, uint64_t channel_key
,
1565 struct consumer_output
*consumer
, uint64_t *discarded
)
1568 struct consumer_socket
*socket
;
1569 struct lttng_ht_iter iter
;
1570 struct lttcomm_consumer_msg msg
;
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
,
1587 uint64_t consumer_discarded
= 0;
1588 pthread_mutex_lock(socket
->lock
);
1589 ret
= consumer_socket_send(socket
, &msg
, sizeof(msg
));
1591 pthread_mutex_unlock(socket
->lock
);
1596 * No need for a recv reply status because the answer to the
1597 * command is the reply status message.
1599 ret
= consumer_socket_recv(socket
, &consumer_discarded
,
1600 sizeof(consumer_discarded
));
1602 ERR("get discarded events");
1603 pthread_mutex_unlock(socket
->lock
);
1606 pthread_mutex_unlock(socket
->lock
);
1607 *discarded
+= consumer_discarded
;
1610 DBG("Consumer discarded %" PRIu64
" events in session id %" PRIu64
,
1611 *discarded
, session_id
);
1619 * Ask the consumer the number of lost packets for a channel.
1621 int consumer_get_lost_packets(uint64_t session_id
, uint64_t channel_key
,
1622 struct consumer_output
*consumer
, uint64_t *lost
)
1625 struct consumer_socket
*socket
;
1626 struct lttng_ht_iter iter
;
1627 struct lttcomm_consumer_msg msg
;
1631 DBG3("Consumer lost packets id %" PRIu64
, session_id
);
1633 memset(&msg
, 0, sizeof(msg
));
1634 msg
.cmd_type
= LTTNG_CONSUMER_LOST_PACKETS
;
1635 msg
.u
.lost_packets
.session_id
= session_id
;
1636 msg
.u
.lost_packets
.channel_key
= channel_key
;
1640 /* Send command for each consumer */
1642 cds_lfht_for_each_entry(consumer
->socks
->ht
, &iter
.iter
, socket
,
1644 uint64_t consumer_lost
= 0;
1645 pthread_mutex_lock(socket
->lock
);
1646 ret
= consumer_socket_send(socket
, &msg
, sizeof(msg
));
1648 pthread_mutex_unlock(socket
->lock
);
1653 * No need for a recv reply status because the answer to the
1654 * command is the reply status message.
1656 ret
= consumer_socket_recv(socket
, &consumer_lost
,
1657 sizeof(consumer_lost
));
1659 ERR("get lost packets");
1660 pthread_mutex_unlock(socket
->lock
);
1663 pthread_mutex_unlock(socket
->lock
);
1664 *lost
+= consumer_lost
;
1667 DBG("Consumer lost %" PRIu64
" packets in session id %" PRIu64
,
1676 * Ask the consumer to rotate a channel.
1678 * The new_chunk_id is the session->rotate_count that has been incremented
1679 * when the rotation started. On the relay, this allows to keep track in which
1680 * chunk each stream is currently writing to (for the rotate_pending operation).
1682 int consumer_rotate_channel(struct consumer_socket
*socket
, uint64_t key
,
1683 uid_t uid
, gid_t gid
, struct consumer_output
*output
,
1684 bool is_metadata_channel
)
1687 struct lttcomm_consumer_msg msg
;
1691 DBG("Consumer rotate channel key %" PRIu64
, key
);
1693 pthread_mutex_lock(socket
->lock
);
1694 memset(&msg
, 0, sizeof(msg
));
1695 msg
.cmd_type
= LTTNG_CONSUMER_ROTATE_CHANNEL
;
1696 msg
.u
.rotate_channel
.key
= key
;
1697 msg
.u
.rotate_channel
.metadata
= !!is_metadata_channel
;
1699 if (output
->type
== CONSUMER_DST_NET
) {
1700 msg
.u
.rotate_channel
.relayd_id
= output
->net_seq_index
;
1702 msg
.u
.rotate_channel
.relayd_id
= (uint64_t) -1ULL;
1705 health_code_update();
1706 ret
= consumer_send_msg(socket
, &msg
);
1709 case LTTCOMM_CONSUMERD_CHAN_NOT_FOUND
:
1710 ret
= -LTTNG_ERR_CHAN_NOT_FOUND
;
1713 ret
= -LTTNG_ERR_ROTATION_FAIL_CONSUMER
;
1719 pthread_mutex_unlock(socket
->lock
);
1720 health_code_update();
1724 int consumer_clear_channel(struct consumer_socket
*socket
, uint64_t key
)
1727 struct lttcomm_consumer_msg msg
;
1731 DBG("Consumer clear channel %" PRIu64
, key
);
1733 memset(&msg
, 0, sizeof(msg
));
1734 msg
.cmd_type
= LTTNG_CONSUMER_CLEAR_CHANNEL
;
1735 msg
.u
.clear_channel
.key
= key
;
1737 health_code_update();
1739 pthread_mutex_lock(socket
->lock
);
1740 ret
= consumer_send_msg(socket
, &msg
);
1746 pthread_mutex_unlock(socket
->lock
);
1748 health_code_update();
1752 int consumer_init(struct consumer_socket
*socket
,
1753 const lttng_uuid sessiond_uuid
)
1756 struct lttcomm_consumer_msg msg
= {
1757 .cmd_type
= LTTNG_CONSUMER_INIT
,
1762 DBG("Sending consumer initialization command");
1763 lttng_uuid_copy(msg
.u
.init
.sessiond_uuid
, sessiond_uuid
);
1765 health_code_update();
1766 ret
= consumer_send_msg(socket
, &msg
);
1772 health_code_update();
1777 * Ask the consumer to create a new chunk for a given session.
1779 * Called with the consumer socket lock held.
1781 int consumer_create_trace_chunk(struct consumer_socket
*socket
,
1782 uint64_t relayd_id
, uint64_t session_id
,
1783 struct lttng_trace_chunk
*chunk
,
1784 const char *domain_subdir
)
1787 enum lttng_trace_chunk_status chunk_status
;
1788 struct lttng_credentials chunk_credentials
;
1789 const struct lttng_directory_handle
*chunk_directory_handle
= NULL
;
1790 struct lttng_directory_handle
*domain_handle
= NULL
;
1792 const char *chunk_name
;
1793 bool chunk_name_overridden
;
1795 time_t creation_timestamp
;
1796 char creation_timestamp_buffer
[ISO8601_STR_LEN
];
1797 const char *creation_timestamp_str
= "(none)";
1798 const bool chunk_has_local_output
= relayd_id
== -1ULL;
1799 enum lttng_trace_chunk_status tc_status
;
1800 struct lttcomm_consumer_msg msg
= {
1801 .cmd_type
= LTTNG_CONSUMER_CREATE_TRACE_CHUNK
,
1802 .u
.create_trace_chunk
.session_id
= session_id
,
1808 if (relayd_id
!= -1ULL) {
1809 LTTNG_OPTIONAL_SET(&msg
.u
.create_trace_chunk
.relayd_id
,
1813 chunk_status
= lttng_trace_chunk_get_name(chunk
, &chunk_name
,
1814 &chunk_name_overridden
);
1815 if (chunk_status
!= LTTNG_TRACE_CHUNK_STATUS_OK
&&
1816 chunk_status
!= LTTNG_TRACE_CHUNK_STATUS_NONE
) {
1817 ERR("Failed to get name of trace chunk");
1818 ret
= -LTTNG_ERR_FATAL
;
1821 if (chunk_name_overridden
) {
1822 ret
= lttng_strncpy(msg
.u
.create_trace_chunk
.override_name
,
1824 sizeof(msg
.u
.create_trace_chunk
.override_name
));
1826 ERR("Trace chunk name \"%s\" exceeds the maximal length allowed by the consumer protocol",
1828 ret
= -LTTNG_ERR_FATAL
;
1833 chunk_status
= lttng_trace_chunk_get_creation_timestamp(chunk
,
1834 &creation_timestamp
);
1835 if (chunk_status
!= LTTNG_TRACE_CHUNK_STATUS_OK
) {
1836 ret
= -LTTNG_ERR_FATAL
;
1839 msg
.u
.create_trace_chunk
.creation_timestamp
=
1840 (uint64_t) creation_timestamp
;
1841 /* Only used for logging purposes. */
1842 ret
= time_to_iso8601_str(creation_timestamp
,
1843 creation_timestamp_buffer
,
1844 sizeof(creation_timestamp_buffer
));
1845 creation_timestamp_str
= !ret
? creation_timestamp_buffer
:
1846 "(formatting error)";
1848 chunk_status
= lttng_trace_chunk_get_id(chunk
, &chunk_id
);
1849 if (chunk_status
!= LTTNG_TRACE_CHUNK_STATUS_OK
) {
1851 * Anonymous trace chunks should never be transmitted
1852 * to remote peers (consumerd and relayd). They are used
1853 * internally for backward-compatibility purposes.
1855 ret
= -LTTNG_ERR_FATAL
;
1858 msg
.u
.create_trace_chunk
.chunk_id
= chunk_id
;
1860 if (chunk_has_local_output
) {
1861 chunk_status
= lttng_trace_chunk_borrow_chunk_directory_handle(
1862 chunk
, &chunk_directory_handle
);
1863 if (chunk_status
!= LTTNG_TRACE_CHUNK_STATUS_OK
) {
1864 ret
= -LTTNG_ERR_FATAL
;
1867 chunk_status
= lttng_trace_chunk_get_credentials(
1868 chunk
, &chunk_credentials
);
1869 if (chunk_status
!= LTTNG_TRACE_CHUNK_STATUS_OK
) {
1871 * Not associating credentials to a sessiond chunk is a
1872 * fatal internal error.
1874 ret
= -LTTNG_ERR_FATAL
;
1877 tc_status
= lttng_trace_chunk_create_subdirectory(
1878 chunk
, domain_subdir
);
1879 if (tc_status
!= LTTNG_TRACE_CHUNK_STATUS_OK
) {
1880 PERROR("Failed to create chunk domain output directory \"%s\"",
1882 ret
= -LTTNG_ERR_FATAL
;
1885 domain_handle
= lttng_directory_handle_create_from_handle(
1887 chunk_directory_handle
);
1888 if (!domain_handle
) {
1889 ret
= -LTTNG_ERR_FATAL
;
1894 * This will only compile on platforms that support
1895 * dirfd (POSIX.2008). This is fine as the session daemon
1896 * is only built for such platforms.
1898 * The ownership of the chunk directory handle's is maintained
1899 * by the trace chunk.
1901 domain_dirfd
= lttng_directory_handle_get_dirfd(
1903 assert(domain_dirfd
>= 0);
1905 msg
.u
.create_trace_chunk
.credentials
.value
.uid
=
1906 chunk_credentials
.uid
;
1907 msg
.u
.create_trace_chunk
.credentials
.value
.gid
=
1908 chunk_credentials
.gid
;
1909 msg
.u
.create_trace_chunk
.credentials
.is_set
= 1;
1912 DBG("Sending consumer create trace chunk command: relayd_id = %" PRId64
1913 ", session_id = %" PRIu64
", chunk_id = %" PRIu64
1914 ", creation_timestamp = %s",
1915 relayd_id
, session_id
, chunk_id
,
1916 creation_timestamp_str
);
1917 health_code_update();
1918 ret
= consumer_send_msg(socket
, &msg
);
1919 health_code_update();
1921 ERR("Trace chunk creation error on consumer");
1922 ret
= -LTTNG_ERR_CREATE_TRACE_CHUNK_FAIL_CONSUMER
;
1926 if (chunk_has_local_output
) {
1927 DBG("Sending trace chunk domain directory fd to consumer");
1928 health_code_update();
1929 ret
= consumer_send_fds(socket
, &domain_dirfd
, 1);
1930 health_code_update();
1932 ERR("Trace chunk creation error on consumer");
1933 ret
= -LTTNG_ERR_CREATE_TRACE_CHUNK_FAIL_CONSUMER
;
1938 lttng_directory_handle_put(domain_handle
);
1943 * Ask the consumer to close a trace chunk for a given session.
1945 * Called with the consumer socket lock held.
1947 int consumer_close_trace_chunk(struct consumer_socket
*socket
,
1948 uint64_t relayd_id
, uint64_t session_id
,
1949 struct lttng_trace_chunk
*chunk
,
1950 char *closed_trace_chunk_path
)
1953 enum lttng_trace_chunk_status chunk_status
;
1954 struct lttcomm_consumer_msg msg
= {
1955 .cmd_type
= LTTNG_CONSUMER_CLOSE_TRACE_CHUNK
,
1956 .u
.close_trace_chunk
.session_id
= session_id
,
1958 struct lttcomm_consumer_close_trace_chunk_reply reply
;
1960 time_t close_timestamp
;
1961 enum lttng_trace_chunk_command_type close_command
;
1962 const char *close_command_name
= "none";
1963 struct lttng_dynamic_buffer path_reception_buffer
;
1966 lttng_dynamic_buffer_init(&path_reception_buffer
);
1968 if (relayd_id
!= -1ULL) {
1970 &msg
.u
.close_trace_chunk
.relayd_id
, relayd_id
);
1973 chunk_status
= lttng_trace_chunk_get_close_command(
1974 chunk
, &close_command
);
1975 switch (chunk_status
) {
1976 case LTTNG_TRACE_CHUNK_STATUS_OK
:
1977 LTTNG_OPTIONAL_SET(&msg
.u
.close_trace_chunk
.close_command
,
1978 (uint32_t) close_command
);
1980 case LTTNG_TRACE_CHUNK_STATUS_NONE
:
1983 ERR("Failed to get trace chunk close command");
1988 chunk_status
= lttng_trace_chunk_get_id(chunk
, &chunk_id
);
1990 * Anonymous trace chunks should never be transmitted to remote peers
1991 * (consumerd and relayd). They are used internally for
1992 * backward-compatibility purposes.
1994 assert(chunk_status
== LTTNG_TRACE_CHUNK_STATUS_OK
);
1995 msg
.u
.close_trace_chunk
.chunk_id
= chunk_id
;
1997 chunk_status
= lttng_trace_chunk_get_close_timestamp(chunk
,
2000 * A trace chunk should be closed locally before being closed remotely.
2001 * Otherwise, the close timestamp would never be transmitted to the
2004 assert(chunk_status
== LTTNG_TRACE_CHUNK_STATUS_OK
);
2005 msg
.u
.close_trace_chunk
.close_timestamp
= (uint64_t) close_timestamp
;
2007 if (msg
.u
.close_trace_chunk
.close_command
.is_set
) {
2008 close_command_name
= lttng_trace_chunk_command_type_get_name(
2011 DBG("Sending consumer close trace chunk command: relayd_id = %" PRId64
2012 ", session_id = %" PRIu64
", chunk_id = %" PRIu64
2013 ", close command = \"%s\"",
2014 relayd_id
, session_id
, chunk_id
, close_command_name
);
2016 health_code_update();
2017 ret
= consumer_socket_send(socket
, &msg
, sizeof(struct lttcomm_consumer_msg
));
2019 ret
= -LTTNG_ERR_CLOSE_TRACE_CHUNK_FAIL_CONSUMER
;
2022 ret
= consumer_socket_recv(socket
, &reply
, sizeof(reply
));
2024 ret
= -LTTNG_ERR_CLOSE_TRACE_CHUNK_FAIL_CONSUMER
;
2027 if (reply
.path_length
>= LTTNG_PATH_MAX
) {
2028 ERR("Invalid path returned by relay daemon: %" PRIu32
"bytes exceeds maximal allowed length of %d bytes",
2029 reply
.path_length
, LTTNG_PATH_MAX
);
2030 ret
= -LTTNG_ERR_INVALID_PROTOCOL
;
2033 ret
= lttng_dynamic_buffer_set_size(&path_reception_buffer
,
2036 ERR("Failed to allocate reception buffer of path returned by the \"close trace chunk\" command");
2037 ret
= -LTTNG_ERR_NOMEM
;
2040 ret
= consumer_socket_recv(socket
, path_reception_buffer
.data
,
2041 path_reception_buffer
.size
);
2043 ERR("Communication error while receiving path of closed trace chunk");
2044 ret
= -LTTNG_ERR_CLOSE_TRACE_CHUNK_FAIL_CONSUMER
;
2047 if (path_reception_buffer
.data
[path_reception_buffer
.size
- 1] != '\0') {
2048 ERR("Invalid path returned by relay daemon: not null-terminated");
2049 ret
= -LTTNG_ERR_INVALID_PROTOCOL
;
2052 if (closed_trace_chunk_path
) {
2054 * closed_trace_chunk_path is assumed to have a length >=
2057 memcpy(closed_trace_chunk_path
, path_reception_buffer
.data
,
2058 path_reception_buffer
.size
);
2061 lttng_dynamic_buffer_reset(&path_reception_buffer
);
2062 health_code_update();
2067 * Ask the consumer if a trace chunk exists.
2069 * Called with the consumer socket lock held.
2070 * Returns 0 on success, or a negative value on error.
2072 int consumer_trace_chunk_exists(struct consumer_socket
*socket
,
2073 uint64_t relayd_id
, uint64_t session_id
,
2074 struct lttng_trace_chunk
*chunk
,
2075 enum consumer_trace_chunk_exists_status
*result
)
2078 enum lttng_trace_chunk_status chunk_status
;
2079 struct lttcomm_consumer_msg msg
= {
2080 .cmd_type
= LTTNG_CONSUMER_TRACE_CHUNK_EXISTS
,
2081 .u
.trace_chunk_exists
.session_id
= session_id
,
2084 const char *consumer_reply_str
;
2088 if (relayd_id
!= -1ULL) {
2089 LTTNG_OPTIONAL_SET(&msg
.u
.trace_chunk_exists
.relayd_id
,
2093 chunk_status
= lttng_trace_chunk_get_id(chunk
, &chunk_id
);
2094 if (chunk_status
!= LTTNG_TRACE_CHUNK_STATUS_OK
) {
2096 * Anonymous trace chunks should never be transmitted
2097 * to remote peers (consumerd and relayd). They are used
2098 * internally for backward-compatibility purposes.
2100 ret
= -LTTNG_ERR_FATAL
;
2103 msg
.u
.trace_chunk_exists
.chunk_id
= chunk_id
;
2105 DBG("Sending consumer trace chunk exists command: relayd_id = %" PRId64
2106 ", session_id = %" PRIu64
2107 ", chunk_id = %" PRIu64
, relayd_id
, session_id
, chunk_id
);
2109 health_code_update();
2110 ret
= consumer_send_msg(socket
, &msg
);
2112 case LTTCOMM_CONSUMERD_UNKNOWN_TRACE_CHUNK
:
2113 consumer_reply_str
= "unknown trace chunk";
2114 *result
= CONSUMER_TRACE_CHUNK_EXISTS_STATUS_UNKNOWN_CHUNK
;
2116 case LTTCOMM_CONSUMERD_TRACE_CHUNK_EXISTS_LOCAL
:
2117 consumer_reply_str
= "trace chunk exists locally";
2118 *result
= CONSUMER_TRACE_CHUNK_EXISTS_STATUS_EXISTS_LOCAL
;
2120 case LTTCOMM_CONSUMERD_TRACE_CHUNK_EXISTS_REMOTE
:
2121 consumer_reply_str
= "trace chunk exists on remote peer";
2122 *result
= CONSUMER_TRACE_CHUNK_EXISTS_STATUS_EXISTS_REMOTE
;
2125 ERR("Consumer returned an error from TRACE_CHUNK_EXISTS command");
2129 DBG("Consumer reply to TRACE_CHUNK_EXISTS command: %s",
2130 consumer_reply_str
);
2133 health_code_update();