2 * Copyright (C) 2012 - David Goulet <dgoulet@efficios.com>
3 * 2018 - Jérémie Galarneau <jeremie.galarneau@efficios.com>
5 * This program is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License, version 2 only, as
7 * published by the Free Software Foundation.
9 * This program is distributed in the hope that it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
14 * You should have received a copy of the GNU General Public License along with
15 * this program; if not, write to the Free Software Foundation, Inc., 51
16 * Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
25 #include <sys/types.h>
29 #include <common/common.h>
30 #include <common/defaults.h>
31 #include <common/uri.h>
32 #include <common/relayd/relayd.h>
33 #include <common/string-utils/format.h>
36 #include "health-sessiond.h"
39 #include "lttng-sessiond.h"
42 * Return allocated full pathname of the session using the consumer trace path
43 * and subdir if available.
45 * The caller can safely free(3) the returned value. On error, NULL is
48 char *setup_channel_trace_path(struct consumer_output
*consumer
,
49 const char *session_path
, size_t *consumer_path_offset
)
60 * Allocate the string ourself to make sure we never exceed
63 pathname
= zmalloc(LTTNG_PATH_MAX
);
68 /* Get correct path name destination */
69 if (consumer
->type
== CONSUMER_DST_NET
&&
70 consumer
->relay_major_version
== 2 &&
71 consumer
->relay_minor_version
< 11) {
72 ret
= snprintf(pathname
, LTTNG_PATH_MAX
, "%s%s/%s/%s",
73 consumer
->dst
.net
.base_dir
,
74 consumer
->chunk_path
, consumer
->domain_subdir
,
76 *consumer_path_offset
= 0;
78 ret
= snprintf(pathname
, LTTNG_PATH_MAX
, "%s/%s",
79 consumer
->domain_subdir
, session_path
);
80 *consumer_path_offset
= strlen(consumer
->domain_subdir
) + 1;
82 DBG3("Consumer trace path relative to current trace chunk: \"%s\"",
85 PERROR("Failed to format channel path");
87 } else if (ret
>= LTTNG_PATH_MAX
) {
88 ERR("Truncation occurred while formatting channel path");
99 * Send a data payload using a given consumer socket of size len.
101 * The consumer socket lock MUST be acquired before calling this since this
102 * function can change the fd value.
104 * Return 0 on success else a negative value on error.
106 int consumer_socket_send(struct consumer_socket
*socket
, void *msg
, size_t len
)
112 assert(socket
->fd_ptr
);
115 /* Consumer socket is invalid. Stopping. */
116 fd
= *socket
->fd_ptr
;
121 size
= lttcomm_send_unix_sock(fd
, msg
, len
);
123 /* The above call will print a PERROR on error. */
124 DBG("Error when sending data to consumer on sock %d", fd
);
126 * At this point, the socket is not usable anymore thus closing it and
127 * setting the file descriptor to -1 so it is not reused.
130 /* This call will PERROR on error. */
131 (void) lttcomm_close_unix_sock(fd
);
132 *socket
->fd_ptr
= -1;
143 * Receive a data payload using a given consumer socket of size len.
145 * The consumer socket lock MUST be acquired before calling this since this
146 * function can change the fd value.
148 * Return 0 on success else a negative value on error.
150 int consumer_socket_recv(struct consumer_socket
*socket
, void *msg
, size_t len
)
156 assert(socket
->fd_ptr
);
159 /* Consumer socket is invalid. Stopping. */
160 fd
= *socket
->fd_ptr
;
165 size
= lttcomm_recv_unix_sock(fd
, msg
, len
);
167 /* The above call will print a PERROR on error. */
168 DBG("Error when receiving data from the consumer socket %d", fd
);
170 * At this point, the socket is not usable anymore thus closing it and
171 * setting the file descriptor to -1 so it is not reused.
174 /* This call will PERROR on error. */
175 (void) lttcomm_close_unix_sock(fd
);
176 *socket
->fd_ptr
= -1;
187 * Receive a reply command status message from the consumer. Consumer socket
188 * lock MUST be acquired before calling this function.
190 * Return 0 on success, -1 on recv error or a negative lttng error code which
191 * was possibly returned by the consumer.
193 int consumer_recv_status_reply(struct consumer_socket
*sock
)
196 struct lttcomm_consumer_status_msg reply
;
200 ret
= consumer_socket_recv(sock
, &reply
, sizeof(reply
));
205 if (reply
.ret_code
== LTTCOMM_CONSUMERD_SUCCESS
) {
209 ret
= -reply
.ret_code
;
210 DBG("Consumer ret code %d", ret
);
218 * Once the ASK_CHANNEL command is sent to the consumer, the channel
219 * information are sent back. This call receives that data and populates key
222 * On success return 0 and both key and stream_count are set. On error, a
223 * negative value is sent back and both parameters are untouched.
225 int consumer_recv_status_channel(struct consumer_socket
*sock
,
226 uint64_t *key
, unsigned int *stream_count
)
229 struct lttcomm_consumer_status_channel reply
;
232 assert(stream_count
);
235 ret
= consumer_socket_recv(sock
, &reply
, sizeof(reply
));
240 /* An error is possible so don't touch the key and stream_count. */
241 if (reply
.ret_code
!= LTTCOMM_CONSUMERD_SUCCESS
) {
247 *stream_count
= reply
.stream_count
;
255 * Send destroy relayd command to consumer.
257 * On success return positive value. On error, negative value.
259 int consumer_send_destroy_relayd(struct consumer_socket
*sock
,
260 struct consumer_output
*consumer
)
263 struct lttcomm_consumer_msg msg
;
268 DBG2("Sending destroy relayd command to consumer sock %d", *sock
->fd_ptr
);
270 memset(&msg
, 0, sizeof(msg
));
271 msg
.cmd_type
= LTTNG_CONSUMER_DESTROY_RELAYD
;
272 msg
.u
.destroy_relayd
.net_seq_idx
= consumer
->net_seq_index
;
274 pthread_mutex_lock(sock
->lock
);
275 ret
= consumer_socket_send(sock
, &msg
, sizeof(msg
));
280 /* Don't check the return value. The caller will do it. */
281 ret
= consumer_recv_status_reply(sock
);
283 DBG2("Consumer send destroy relayd command done");
286 pthread_mutex_unlock(sock
->lock
);
291 * For each consumer socket in the consumer output object, send a destroy
294 void consumer_output_send_destroy_relayd(struct consumer_output
*consumer
)
296 struct lttng_ht_iter iter
;
297 struct consumer_socket
*socket
;
301 /* Destroy any relayd connection */
302 if (consumer
->type
== CONSUMER_DST_NET
) {
304 cds_lfht_for_each_entry(consumer
->socks
->ht
, &iter
.iter
, socket
,
308 /* Send destroy relayd command */
309 ret
= consumer_send_destroy_relayd(socket
, consumer
);
311 DBG("Unable to send destroy relayd command to consumer");
312 /* Continue since we MUST delete everything at this point. */
320 * From a consumer_data structure, allocate and add a consumer socket to the
323 * Return 0 on success, else negative value on error
325 int consumer_create_socket(struct consumer_data
*data
,
326 struct consumer_output
*output
)
329 struct consumer_socket
*socket
;
333 if (output
== NULL
|| data
->cmd_sock
< 0) {
335 * Not an error. Possible there is simply not spawned consumer or it's
336 * disabled for the tracing session asking the socket.
342 socket
= consumer_find_socket(data
->cmd_sock
, output
);
344 if (socket
== NULL
) {
345 socket
= consumer_allocate_socket(&data
->cmd_sock
);
346 if (socket
== NULL
) {
351 socket
->registered
= 0;
352 socket
->lock
= &data
->lock
;
354 consumer_add_socket(socket
, output
);
358 socket
->type
= data
->type
;
360 DBG3("Consumer socket created (fd: %d) and added to output",
368 * Return the consumer socket from the given consumer output with the right
369 * bitness. On error, returns NULL.
371 * The caller MUST acquire a rcu read side lock and keep it until the socket
372 * object reference is not needed anymore.
374 struct consumer_socket
*consumer_find_socket_by_bitness(int bits
,
375 const struct consumer_output
*consumer
)
378 struct consumer_socket
*socket
= NULL
;
382 consumer_fd
= uatomic_read(&ust_consumerd64_fd
);
385 consumer_fd
= uatomic_read(&ust_consumerd32_fd
);
392 socket
= consumer_find_socket(consumer_fd
, consumer
);
394 ERR("Consumer socket fd %d not found in consumer obj %p",
395 consumer_fd
, consumer
);
403 * Find a consumer_socket in a consumer_output hashtable. Read side lock must
404 * be acquired before calling this function and across use of the
405 * returned consumer_socket.
407 struct consumer_socket
*consumer_find_socket(int key
,
408 const struct consumer_output
*consumer
)
410 struct lttng_ht_iter iter
;
411 struct lttng_ht_node_ulong
*node
;
412 struct consumer_socket
*socket
= NULL
;
414 /* Negative keys are lookup failures */
415 if (key
< 0 || consumer
== NULL
) {
419 lttng_ht_lookup(consumer
->socks
, (void *)((unsigned long) key
),
421 node
= lttng_ht_iter_get_node_ulong(&iter
);
423 socket
= caa_container_of(node
, struct consumer_socket
, node
);
430 * Allocate a new consumer_socket and return the pointer.
432 struct consumer_socket
*consumer_allocate_socket(int *fd
)
434 struct consumer_socket
*socket
= NULL
;
438 socket
= zmalloc(sizeof(struct consumer_socket
));
439 if (socket
== NULL
) {
440 PERROR("zmalloc consumer socket");
445 lttng_ht_node_init_ulong(&socket
->node
, *fd
);
452 * Add consumer socket to consumer output object. Read side lock must be
453 * acquired before calling this function.
455 void consumer_add_socket(struct consumer_socket
*sock
,
456 struct consumer_output
*consumer
)
461 lttng_ht_add_unique_ulong(consumer
->socks
, &sock
->node
);
465 * Delete consumer socket to consumer output object. Read side lock must be
466 * acquired before calling this function.
468 void consumer_del_socket(struct consumer_socket
*sock
,
469 struct consumer_output
*consumer
)
472 struct lttng_ht_iter iter
;
477 iter
.iter
.node
= &sock
->node
.node
;
478 ret
= lttng_ht_del(consumer
->socks
, &iter
);
483 * RCU destroy call function.
485 static void destroy_socket_rcu(struct rcu_head
*head
)
487 struct lttng_ht_node_ulong
*node
=
488 caa_container_of(head
, struct lttng_ht_node_ulong
, head
);
489 struct consumer_socket
*socket
=
490 caa_container_of(node
, struct consumer_socket
, node
);
496 * Destroy and free socket pointer in a call RCU. Read side lock must be
497 * acquired before calling this function.
499 void consumer_destroy_socket(struct consumer_socket
*sock
)
504 * We DO NOT close the file descriptor here since it is global to the
505 * session daemon and is closed only if the consumer dies or a custom
506 * consumer was registered,
508 if (sock
->registered
) {
509 DBG3("Consumer socket was registered. Closing fd %d", *sock
->fd_ptr
);
510 lttcomm_close_unix_sock(*sock
->fd_ptr
);
513 call_rcu(&sock
->node
.head
, destroy_socket_rcu
);
517 * Allocate and assign data to a consumer_output object.
519 * Return pointer to structure.
521 struct consumer_output
*consumer_create_output(enum consumer_dst_type type
)
523 struct consumer_output
*output
= NULL
;
525 output
= zmalloc(sizeof(struct consumer_output
));
526 if (output
== NULL
) {
527 PERROR("zmalloc consumer_output");
531 /* By default, consumer output is enabled */
534 output
->net_seq_index
= (uint64_t) -1ULL;
535 urcu_ref_init(&output
->ref
);
537 output
->socks
= lttng_ht_new(0, LTTNG_HT_TYPE_ULONG
);
544 * Iterate over the consumer output socket hash table and destroy them. The
545 * socket file descriptor are only closed if the consumer output was
546 * registered meaning it's an external consumer.
548 void consumer_destroy_output_sockets(struct consumer_output
*obj
)
550 struct lttng_ht_iter iter
;
551 struct consumer_socket
*socket
;
558 cds_lfht_for_each_entry(obj
->socks
->ht
, &iter
.iter
, socket
, node
.node
) {
559 consumer_del_socket(socket
, obj
);
560 consumer_destroy_socket(socket
);
566 * Delete the consumer_output object from the list and free the ptr.
568 * Should *NOT* be called with RCU read-side lock held.
570 static void consumer_release_output(struct urcu_ref
*ref
)
572 struct consumer_output
*obj
=
573 caa_container_of(ref
, struct consumer_output
, ref
);
575 consumer_destroy_output_sockets(obj
);
578 /* Finally destroy HT */
579 ht_cleanup_push(obj
->socks
);
586 * Get the consumer_output object.
588 void consumer_output_get(struct consumer_output
*obj
)
590 urcu_ref_get(&obj
->ref
);
594 * Put the consumer_output object.
596 * Should *NOT* be called with RCU read-side lock held.
598 void consumer_output_put(struct consumer_output
*obj
)
603 urcu_ref_put(&obj
->ref
, consumer_release_output
);
607 * Copy consumer output and returned the newly allocated copy.
609 * Should *NOT* be called with RCU read-side lock held.
611 struct consumer_output
*consumer_copy_output(struct consumer_output
*src
)
614 struct consumer_output
*output
;
618 output
= consumer_create_output(src
->type
);
619 if (output
== NULL
) {
622 output
->enabled
= src
->enabled
;
623 output
->net_seq_index
= src
->net_seq_index
;
624 memcpy(output
->domain_subdir
, src
->domain_subdir
,
625 sizeof(output
->domain_subdir
));
626 output
->snapshot
= src
->snapshot
;
627 output
->relay_major_version
= src
->relay_major_version
;
628 output
->relay_minor_version
= src
->relay_minor_version
;
629 output
->relay_allows_clear
= src
->relay_allows_clear
;
630 memcpy(&output
->dst
, &src
->dst
, sizeof(output
->dst
));
631 ret
= consumer_copy_sockets(output
, src
);
639 consumer_output_put(output
);
644 * Copy consumer sockets from src to dst.
646 * Return 0 on success or else a negative value.
648 int consumer_copy_sockets(struct consumer_output
*dst
,
649 struct consumer_output
*src
)
652 struct lttng_ht_iter iter
;
653 struct consumer_socket
*socket
, *copy_sock
;
659 cds_lfht_for_each_entry(src
->socks
->ht
, &iter
.iter
, socket
, node
.node
) {
660 /* Ignore socket that are already there. */
661 copy_sock
= consumer_find_socket(*socket
->fd_ptr
, dst
);
666 /* Create new socket object. */
667 copy_sock
= consumer_allocate_socket(socket
->fd_ptr
);
668 if (copy_sock
== NULL
) {
674 copy_sock
->registered
= socket
->registered
;
676 * This is valid because this lock is shared accross all consumer
677 * object being the global lock of the consumer data structure of the
680 copy_sock
->lock
= socket
->lock
;
681 consumer_add_socket(copy_sock
, dst
);
690 * Set network URI to the consumer output.
692 * Return 0 on success. Return 1 if the URI were equal. Else, negative value on
695 int consumer_set_network_uri(const struct ltt_session
*session
,
696 struct consumer_output
*output
,
697 struct lttng_uri
*uri
)
700 struct lttng_uri
*dst_uri
= NULL
;
702 /* Code flow error safety net. */
706 switch (uri
->stype
) {
707 case LTTNG_STREAM_CONTROL
:
708 dst_uri
= &output
->dst
.net
.control
;
709 output
->dst
.net
.control_isset
= 1;
710 if (uri
->port
== 0) {
711 /* Assign default port. */
712 uri
->port
= DEFAULT_NETWORK_CONTROL_PORT
;
714 if (output
->dst
.net
.data_isset
&& uri
->port
==
715 output
->dst
.net
.data
.port
) {
716 ret
= -LTTNG_ERR_INVALID
;
720 DBG3("Consumer control URI set with port %d", uri
->port
);
722 case LTTNG_STREAM_DATA
:
723 dst_uri
= &output
->dst
.net
.data
;
724 output
->dst
.net
.data_isset
= 1;
725 if (uri
->port
== 0) {
726 /* Assign default port. */
727 uri
->port
= DEFAULT_NETWORK_DATA_PORT
;
729 if (output
->dst
.net
.control_isset
&& uri
->port
==
730 output
->dst
.net
.control
.port
) {
731 ret
= -LTTNG_ERR_INVALID
;
735 DBG3("Consumer data URI set with port %d", uri
->port
);
738 ERR("Set network uri type unknown %d", uri
->stype
);
739 ret
= -LTTNG_ERR_INVALID
;
743 ret
= uri_compare(dst_uri
, uri
);
745 /* Same URI, don't touch it and return success. */
746 DBG3("URI network compare are the same");
750 /* URIs were not equal, replacing it. */
751 memcpy(dst_uri
, uri
, sizeof(struct lttng_uri
));
752 output
->type
= CONSUMER_DST_NET
;
753 if (dst_uri
->stype
!= LTTNG_STREAM_CONTROL
) {
754 /* Only the control uri needs to contain the path. */
759 * If the user has specified a subdir as part of the control
760 * URL, the session's base output directory is:
761 * /RELAYD_OUTPUT_PATH/HOSTNAME/USER_SPECIFIED_DIR
763 * Hence, the "base_dir" from which all stream files and
764 * session rotation chunks are created takes the form
765 * /HOSTNAME/USER_SPECIFIED_DIR
767 * If the user has not specified an output directory as part of
768 * the control URL, the base output directory has the form:
769 * /RELAYD_OUTPUT_PATH/HOSTNAME/SESSION_NAME-CREATION_TIME
771 * Hence, the "base_dir" from which all stream files and
772 * session rotation chunks are created takes the form
773 * /HOSTNAME/SESSION_NAME-CREATION_TIME
775 * Note that automatically generated session names already
776 * contain the session's creation time. In that case, the
777 * creation time is omitted to prevent it from being duplicated
778 * in the final directory hierarchy.
781 if (strstr(uri
->subdir
, "../")) {
782 ERR("Network URI subdirs are not allowed to walk up the path hierarchy");
783 ret
= -LTTNG_ERR_INVALID
;
786 ret
= snprintf(output
->dst
.net
.base_dir
,
787 sizeof(output
->dst
.net
.base_dir
),
788 "/%s/%s/", session
->hostname
, uri
->subdir
);
790 if (session
->has_auto_generated_name
) {
791 ret
= snprintf(output
->dst
.net
.base_dir
,
792 sizeof(output
->dst
.net
.base_dir
),
793 "/%s/%s/", session
->hostname
,
796 char session_creation_datetime
[16];
800 timeinfo
= localtime(&session
->creation_time
);
802 ret
= -LTTNG_ERR_FATAL
;
805 strftime_ret
= strftime(session_creation_datetime
,
806 sizeof(session_creation_datetime
),
807 "%Y%m%d-%H%M%S", timeinfo
);
808 if (strftime_ret
== 0) {
809 ERR("Failed to format session creation timestamp while setting network URI");
810 ret
= -LTTNG_ERR_FATAL
;
813 ret
= snprintf(output
->dst
.net
.base_dir
,
814 sizeof(output
->dst
.net
.base_dir
),
815 "/%s/%s-%s/", session
->hostname
,
817 session_creation_datetime
);
820 if (ret
>= sizeof(output
->dst
.net
.base_dir
)) {
821 ret
= -LTTNG_ERR_INVALID
;
822 ERR("Truncation occurred while setting network output base directory");
824 } else if (ret
== -1) {
825 ret
= -LTTNG_ERR_INVALID
;
826 PERROR("Error occurred while setting network output base directory");
830 DBG3("Consumer set network uri base_dir path %s",
831 output
->dst
.net
.base_dir
);
842 * Send file descriptor to consumer via sock.
844 * The consumer socket lock must be held by the caller.
846 int consumer_send_fds(struct consumer_socket
*sock
, const int *fds
,
854 assert(pthread_mutex_trylock(sock
->lock
) == EBUSY
);
856 ret
= lttcomm_send_fds_unix_sock(*sock
->fd_ptr
, fds
, nb_fd
);
858 /* The above call will print a PERROR on error. */
859 DBG("Error when sending consumer fds on sock %d", *sock
->fd_ptr
);
863 ret
= consumer_recv_status_reply(sock
);
869 * Consumer send communication message structure to consumer.
871 * The consumer socket lock must be held by the caller.
873 int consumer_send_msg(struct consumer_socket
*sock
,
874 struct lttcomm_consumer_msg
*msg
)
880 assert(pthread_mutex_trylock(sock
->lock
) == EBUSY
);
882 ret
= consumer_socket_send(sock
, msg
, sizeof(struct lttcomm_consumer_msg
));
887 ret
= consumer_recv_status_reply(sock
);
894 * Consumer send channel communication message structure to consumer.
896 * The consumer socket lock must be held by the caller.
898 int consumer_send_channel(struct consumer_socket
*sock
,
899 struct lttcomm_consumer_msg
*msg
)
906 ret
= consumer_send_msg(sock
, msg
);
916 * Populate the given consumer msg structure with the ask_channel command
919 void consumer_init_ask_channel_comm_msg(struct lttcomm_consumer_msg
*msg
,
920 uint64_t subbuf_size
,
923 unsigned int switch_timer_interval
,
924 unsigned int read_timer_interval
,
925 unsigned int live_timer_interval
,
926 unsigned int monitor_timer_interval
,
930 const char *pathname
,
936 uint64_t tracefile_size
,
937 uint64_t tracefile_count
,
938 uint64_t session_id_per_pid
,
939 unsigned int monitor
,
940 uint32_t ust_app_uid
,
941 int64_t blocking_timeout
,
942 const char *root_shm_path
,
943 const char *shm_path
,
944 struct lttng_trace_chunk
*trace_chunk
,
945 const struct lttng_credentials
*buffer_credentials
)
949 /* Zeroed structure */
950 memset(msg
, 0, sizeof(struct lttcomm_consumer_msg
));
951 msg
->u
.ask_channel
.buffer_credentials
.uid
= UINT32_MAX
;
952 msg
->u
.ask_channel
.buffer_credentials
.gid
= UINT32_MAX
;
956 enum lttng_trace_chunk_status chunk_status
;
958 chunk_status
= lttng_trace_chunk_get_id(trace_chunk
, &chunk_id
);
959 assert(chunk_status
== LTTNG_TRACE_CHUNK_STATUS_OK
);
960 LTTNG_OPTIONAL_SET(&msg
->u
.ask_channel
.chunk_id
, chunk_id
);
962 msg
->u
.ask_channel
.buffer_credentials
.uid
= buffer_credentials
->uid
;
963 msg
->u
.ask_channel
.buffer_credentials
.gid
= buffer_credentials
->gid
;
965 msg
->cmd_type
= LTTNG_CONSUMER_ASK_CHANNEL_CREATION
;
966 msg
->u
.ask_channel
.subbuf_size
= subbuf_size
;
967 msg
->u
.ask_channel
.num_subbuf
= num_subbuf
;
968 msg
->u
.ask_channel
.overwrite
= overwrite
;
969 msg
->u
.ask_channel
.switch_timer_interval
= switch_timer_interval
;
970 msg
->u
.ask_channel
.read_timer_interval
= read_timer_interval
;
971 msg
->u
.ask_channel
.live_timer_interval
= live_timer_interval
;
972 msg
->u
.ask_channel
.monitor_timer_interval
= monitor_timer_interval
;
973 msg
->u
.ask_channel
.output
= output
;
974 msg
->u
.ask_channel
.type
= type
;
975 msg
->u
.ask_channel
.session_id
= session_id
;
976 msg
->u
.ask_channel
.session_id_per_pid
= session_id_per_pid
;
977 msg
->u
.ask_channel
.relayd_id
= relayd_id
;
978 msg
->u
.ask_channel
.key
= key
;
979 msg
->u
.ask_channel
.chan_id
= chan_id
;
980 msg
->u
.ask_channel
.tracefile_size
= tracefile_size
;
981 msg
->u
.ask_channel
.tracefile_count
= tracefile_count
;
982 msg
->u
.ask_channel
.monitor
= monitor
;
983 msg
->u
.ask_channel
.ust_app_uid
= ust_app_uid
;
984 msg
->u
.ask_channel
.blocking_timeout
= blocking_timeout
;
986 memcpy(msg
->u
.ask_channel
.uuid
, uuid
, sizeof(msg
->u
.ask_channel
.uuid
));
989 strncpy(msg
->u
.ask_channel
.pathname
, pathname
,
990 sizeof(msg
->u
.ask_channel
.pathname
));
991 msg
->u
.ask_channel
.pathname
[sizeof(msg
->u
.ask_channel
.pathname
)-1] = '\0';
994 strncpy(msg
->u
.ask_channel
.name
, name
, sizeof(msg
->u
.ask_channel
.name
));
995 msg
->u
.ask_channel
.name
[sizeof(msg
->u
.ask_channel
.name
) - 1] = '\0';
998 strncpy(msg
->u
.ask_channel
.root_shm_path
, root_shm_path
,
999 sizeof(msg
->u
.ask_channel
.root_shm_path
));
1000 msg
->u
.ask_channel
.root_shm_path
[sizeof(msg
->u
.ask_channel
.root_shm_path
) - 1] = '\0';
1003 strncpy(msg
->u
.ask_channel
.shm_path
, shm_path
,
1004 sizeof(msg
->u
.ask_channel
.shm_path
));
1005 msg
->u
.ask_channel
.shm_path
[sizeof(msg
->u
.ask_channel
.shm_path
) - 1] = '\0';
1010 * Init channel communication message structure.
1012 void consumer_init_add_channel_comm_msg(struct lttcomm_consumer_msg
*msg
,
1013 uint64_t channel_key
,
1014 uint64_t session_id
,
1015 const char *pathname
,
1020 unsigned int nb_init_streams
,
1021 enum lttng_event_output output
,
1023 uint64_t tracefile_size
,
1024 uint64_t tracefile_count
,
1025 unsigned int monitor
,
1026 unsigned int live_timer_interval
,
1027 unsigned int monitor_timer_interval
,
1028 struct lttng_trace_chunk
*trace_chunk
)
1032 /* Zeroed structure */
1033 memset(msg
, 0, sizeof(struct lttcomm_consumer_msg
));
1037 enum lttng_trace_chunk_status chunk_status
;
1039 chunk_status
= lttng_trace_chunk_get_id(trace_chunk
, &chunk_id
);
1040 assert(chunk_status
== LTTNG_TRACE_CHUNK_STATUS_OK
);
1041 LTTNG_OPTIONAL_SET(&msg
->u
.channel
.chunk_id
, chunk_id
);
1045 msg
->cmd_type
= LTTNG_CONSUMER_ADD_CHANNEL
;
1046 msg
->u
.channel
.channel_key
= channel_key
;
1047 msg
->u
.channel
.session_id
= session_id
;
1048 msg
->u
.channel
.relayd_id
= relayd_id
;
1049 msg
->u
.channel
.nb_init_streams
= nb_init_streams
;
1050 msg
->u
.channel
.output
= output
;
1051 msg
->u
.channel
.type
= type
;
1052 msg
->u
.channel
.tracefile_size
= tracefile_size
;
1053 msg
->u
.channel
.tracefile_count
= tracefile_count
;
1054 msg
->u
.channel
.monitor
= monitor
;
1055 msg
->u
.channel
.live_timer_interval
= live_timer_interval
;
1056 msg
->u
.channel
.monitor_timer_interval
= monitor_timer_interval
;
1058 strncpy(msg
->u
.channel
.pathname
, pathname
,
1059 sizeof(msg
->u
.channel
.pathname
));
1060 msg
->u
.channel
.pathname
[sizeof(msg
->u
.channel
.pathname
) - 1] = '\0';
1062 strncpy(msg
->u
.channel
.name
, name
, sizeof(msg
->u
.channel
.name
));
1063 msg
->u
.channel
.name
[sizeof(msg
->u
.channel
.name
) - 1] = '\0';
1067 * Init stream communication message structure.
1069 void consumer_init_add_stream_comm_msg(struct lttcomm_consumer_msg
*msg
,
1070 uint64_t channel_key
,
1071 uint64_t stream_key
,
1076 memset(msg
, 0, sizeof(struct lttcomm_consumer_msg
));
1078 msg
->cmd_type
= LTTNG_CONSUMER_ADD_STREAM
;
1079 msg
->u
.stream
.channel_key
= channel_key
;
1080 msg
->u
.stream
.stream_key
= stream_key
;
1081 msg
->u
.stream
.cpu
= cpu
;
1084 void consumer_init_streams_sent_comm_msg(struct lttcomm_consumer_msg
*msg
,
1085 enum lttng_consumer_command cmd
,
1086 uint64_t channel_key
, uint64_t net_seq_idx
)
1090 memset(msg
, 0, sizeof(struct lttcomm_consumer_msg
));
1092 msg
->cmd_type
= cmd
;
1093 msg
->u
.sent_streams
.channel_key
= channel_key
;
1094 msg
->u
.sent_streams
.net_seq_idx
= net_seq_idx
;
1098 * Send stream communication structure to the consumer.
1100 int consumer_send_stream(struct consumer_socket
*sock
,
1101 struct consumer_output
*dst
, struct lttcomm_consumer_msg
*msg
,
1102 const int *fds
, size_t nb_fd
)
1111 ret
= consumer_send_msg(sock
, msg
);
1116 ret
= consumer_send_fds(sock
, fds
, nb_fd
);
1126 * Send relayd socket to consumer associated with a session name.
1128 * The consumer socket lock must be held by the caller.
1130 * On success return positive value. On error, negative value.
1132 int consumer_send_relayd_socket(struct consumer_socket
*consumer_sock
,
1133 struct lttcomm_relayd_sock
*rsock
, struct consumer_output
*consumer
,
1134 enum lttng_stream_type type
, uint64_t session_id
,
1135 const char *session_name
, const char *hostname
,
1136 const char *base_path
, int session_live_timer
,
1137 const uint64_t *current_chunk_id
, time_t session_creation_time
,
1138 bool session_name_contains_creation_time
)
1141 struct lttcomm_consumer_msg msg
;
1143 /* Code flow error. Safety net. */
1146 assert(consumer_sock
);
1148 memset(&msg
, 0, sizeof(msg
));
1149 /* Bail out if consumer is disabled */
1150 if (!consumer
->enabled
) {
1155 if (type
== LTTNG_STREAM_CONTROL
) {
1156 char output_path
[LTTNG_PATH_MAX
] = {};
1157 uint64_t relayd_session_id
;
1159 ret
= relayd_create_session(rsock
,
1161 session_name
, hostname
, base_path
,
1163 consumer
->snapshot
, session_id
,
1164 sessiond_uuid
, current_chunk_id
,
1165 session_creation_time
,
1166 session_name_contains_creation_time
,
1169 /* Close the control socket. */
1170 (void) relayd_close(rsock
);
1173 msg
.u
.relayd_sock
.relayd_session_id
= relayd_session_id
;
1174 DBG("Created session on relay, output path reply: %s",
1178 msg
.cmd_type
= LTTNG_CONSUMER_ADD_RELAYD_SOCKET
;
1180 * Assign network consumer output index using the temporary consumer since
1181 * this call should only be made from within a set_consumer_uri() function
1182 * call in the session daemon.
1184 msg
.u
.relayd_sock
.net_index
= consumer
->net_seq_index
;
1185 msg
.u
.relayd_sock
.type
= type
;
1186 msg
.u
.relayd_sock
.session_id
= session_id
;
1187 memcpy(&msg
.u
.relayd_sock
.sock
, rsock
, sizeof(msg
.u
.relayd_sock
.sock
));
1189 DBG3("Sending relayd sock info to consumer on %d", *consumer_sock
->fd_ptr
);
1190 ret
= consumer_send_msg(consumer_sock
, &msg
);
1195 DBG3("Sending relayd socket file descriptor to consumer");
1196 ret
= consumer_send_fds(consumer_sock
, ALIGNED_CONST_PTR(rsock
->sock
.fd
), 1);
1201 DBG2("Consumer relayd socket sent");
1208 int consumer_send_pipe(struct consumer_socket
*consumer_sock
,
1209 enum lttng_consumer_command cmd
, int pipe
)
1212 struct lttcomm_consumer_msg msg
;
1213 const char *pipe_name
;
1214 const char *command_name
;
1217 case LTTNG_CONSUMER_SET_CHANNEL_MONITOR_PIPE
:
1218 pipe_name
= "channel monitor";
1219 command_name
= "SET_CHANNEL_MONITOR_PIPE";
1222 ERR("Unexpected command received in %s (cmd = %d)", __func__
,
1227 /* Code flow error. Safety net. */
1229 memset(&msg
, 0, sizeof(msg
));
1232 pthread_mutex_lock(consumer_sock
->lock
);
1233 DBG3("Sending %s command to consumer", command_name
);
1234 ret
= consumer_send_msg(consumer_sock
, &msg
);
1239 DBG3("Sending %s pipe %d to consumer on socket %d",
1241 pipe
, *consumer_sock
->fd_ptr
);
1242 ret
= consumer_send_fds(consumer_sock
, &pipe
, 1);
1247 DBG2("%s pipe successfully sent", pipe_name
);
1249 pthread_mutex_unlock(consumer_sock
->lock
);
1253 int consumer_send_channel_monitor_pipe(struct consumer_socket
*consumer_sock
,
1256 return consumer_send_pipe(consumer_sock
,
1257 LTTNG_CONSUMER_SET_CHANNEL_MONITOR_PIPE
, pipe
);
1261 * Ask the consumer if the data is pending for the specific session id.
1262 * Returns 1 if data is pending, 0 otherwise, or < 0 on error.
1264 int consumer_is_data_pending(uint64_t session_id
,
1265 struct consumer_output
*consumer
)
1268 int32_t ret_code
= 0; /* Default is that the data is NOT pending */
1269 struct consumer_socket
*socket
;
1270 struct lttng_ht_iter iter
;
1271 struct lttcomm_consumer_msg msg
;
1275 DBG3("Consumer data pending for id %" PRIu64
, session_id
);
1277 memset(&msg
, 0, sizeof(msg
));
1278 msg
.cmd_type
= LTTNG_CONSUMER_DATA_PENDING
;
1279 msg
.u
.data_pending
.session_id
= session_id
;
1281 /* Send command for each consumer */
1283 cds_lfht_for_each_entry(consumer
->socks
->ht
, &iter
.iter
, socket
,
1285 pthread_mutex_lock(socket
->lock
);
1286 ret
= consumer_socket_send(socket
, &msg
, sizeof(msg
));
1288 pthread_mutex_unlock(socket
->lock
);
1293 * No need for a recv reply status because the answer to the command is
1294 * the reply status message.
1297 ret
= consumer_socket_recv(socket
, &ret_code
, sizeof(ret_code
));
1299 pthread_mutex_unlock(socket
->lock
);
1302 pthread_mutex_unlock(socket
->lock
);
1304 if (ret_code
== 1) {
1310 DBG("Consumer data is %s pending for session id %" PRIu64
,
1311 ret_code
== 1 ? "" : "NOT", session_id
);
1320 * Send a flush command to consumer using the given channel key.
1322 * Return 0 on success else a negative value.
1324 int consumer_flush_channel(struct consumer_socket
*socket
, uint64_t key
)
1327 struct lttcomm_consumer_msg msg
;
1331 DBG2("Consumer flush channel key %" PRIu64
, key
);
1333 memset(&msg
, 0, sizeof(msg
));
1334 msg
.cmd_type
= LTTNG_CONSUMER_FLUSH_CHANNEL
;
1335 msg
.u
.flush_channel
.key
= key
;
1337 pthread_mutex_lock(socket
->lock
);
1338 health_code_update();
1340 ret
= consumer_send_msg(socket
, &msg
);
1346 health_code_update();
1347 pthread_mutex_unlock(socket
->lock
);
1352 * Send a clear quiescent command to consumer using the given channel key.
1354 * Return 0 on success else a negative value.
1356 int consumer_clear_quiescent_channel(struct consumer_socket
*socket
, uint64_t key
)
1359 struct lttcomm_consumer_msg msg
;
1363 DBG2("Consumer clear quiescent channel key %" PRIu64
, key
);
1365 memset(&msg
, 0, sizeof(msg
));
1366 msg
.cmd_type
= LTTNG_CONSUMER_CLEAR_QUIESCENT_CHANNEL
;
1367 msg
.u
.clear_quiescent_channel
.key
= key
;
1369 pthread_mutex_lock(socket
->lock
);
1370 health_code_update();
1372 ret
= consumer_send_msg(socket
, &msg
);
1378 health_code_update();
1379 pthread_mutex_unlock(socket
->lock
);
1384 * Send a close metadata command to consumer using the given channel key.
1385 * Called with registry lock held.
1387 * Return 0 on success else a negative value.
1389 int consumer_close_metadata(struct consumer_socket
*socket
,
1390 uint64_t metadata_key
)
1393 struct lttcomm_consumer_msg msg
;
1397 DBG2("Consumer close metadata channel key %" PRIu64
, metadata_key
);
1399 memset(&msg
, 0, sizeof(msg
));
1400 msg
.cmd_type
= LTTNG_CONSUMER_CLOSE_METADATA
;
1401 msg
.u
.close_metadata
.key
= metadata_key
;
1403 pthread_mutex_lock(socket
->lock
);
1404 health_code_update();
1406 ret
= consumer_send_msg(socket
, &msg
);
1412 health_code_update();
1413 pthread_mutex_unlock(socket
->lock
);
1418 * Send a setup metdata command to consumer using the given channel key.
1420 * Return 0 on success else a negative value.
1422 int consumer_setup_metadata(struct consumer_socket
*socket
,
1423 uint64_t metadata_key
)
1426 struct lttcomm_consumer_msg msg
;
1430 DBG2("Consumer setup metadata channel key %" PRIu64
, metadata_key
);
1432 memset(&msg
, 0, sizeof(msg
));
1433 msg
.cmd_type
= LTTNG_CONSUMER_SETUP_METADATA
;
1434 msg
.u
.setup_metadata
.key
= metadata_key
;
1436 pthread_mutex_lock(socket
->lock
);
1437 health_code_update();
1439 ret
= consumer_send_msg(socket
, &msg
);
1445 health_code_update();
1446 pthread_mutex_unlock(socket
->lock
);
1451 * Send metadata string to consumer.
1452 * RCU read-side lock must be held to guarantee existence of socket.
1454 * Return 0 on success else a negative value.
1456 int consumer_push_metadata(struct consumer_socket
*socket
,
1457 uint64_t metadata_key
, char *metadata_str
, size_t len
,
1458 size_t target_offset
, uint64_t version
)
1461 struct lttcomm_consumer_msg msg
;
1465 DBG2("Consumer push metadata to consumer socket %d", *socket
->fd_ptr
);
1467 pthread_mutex_lock(socket
->lock
);
1469 memset(&msg
, 0, sizeof(msg
));
1470 msg
.cmd_type
= LTTNG_CONSUMER_PUSH_METADATA
;
1471 msg
.u
.push_metadata
.key
= metadata_key
;
1472 msg
.u
.push_metadata
.target_offset
= target_offset
;
1473 msg
.u
.push_metadata
.len
= len
;
1474 msg
.u
.push_metadata
.version
= version
;
1476 health_code_update();
1477 ret
= consumer_send_msg(socket
, &msg
);
1478 if (ret
< 0 || len
== 0) {
1482 DBG3("Consumer pushing metadata on sock %d of len %zu", *socket
->fd_ptr
,
1485 ret
= consumer_socket_send(socket
, metadata_str
, len
);
1490 health_code_update();
1491 ret
= consumer_recv_status_reply(socket
);
1497 pthread_mutex_unlock(socket
->lock
);
1498 health_code_update();
1503 * Ask the consumer to snapshot a specific channel using the key.
1505 * Returns LTTNG_OK on success or else an LTTng error code.
1507 enum lttng_error_code
consumer_snapshot_channel(struct consumer_socket
*socket
,
1508 uint64_t key
, const struct consumer_output
*output
, int metadata
,
1509 uid_t uid
, gid_t gid
, const char *channel_path
, int wait
,
1510 uint64_t nb_packets_per_stream
)
1513 enum lttng_error_code status
= LTTNG_OK
;
1514 struct lttcomm_consumer_msg msg
;
1519 DBG("Consumer snapshot channel key %" PRIu64
, key
);
1521 memset(&msg
, 0, sizeof(msg
));
1522 msg
.cmd_type
= LTTNG_CONSUMER_SNAPSHOT_CHANNEL
;
1523 msg
.u
.snapshot_channel
.key
= key
;
1524 msg
.u
.snapshot_channel
.nb_packets_per_stream
= nb_packets_per_stream
;
1525 msg
.u
.snapshot_channel
.metadata
= metadata
;
1527 if (output
->type
== CONSUMER_DST_NET
) {
1528 msg
.u
.snapshot_channel
.relayd_id
=
1529 output
->net_seq_index
;
1530 msg
.u
.snapshot_channel
.use_relayd
= 1;
1532 msg
.u
.snapshot_channel
.relayd_id
= (uint64_t) -1ULL;
1534 ret
= lttng_strncpy(msg
.u
.snapshot_channel
.pathname
,
1536 sizeof(msg
.u
.snapshot_channel
.pathname
));
1538 ERR("Snapshot path exceeds the maximal allowed length of %zu bytes (%zu bytes required) with path \"%s\"",
1539 sizeof(msg
.u
.snapshot_channel
.pathname
),
1540 strlen(channel_path
),
1542 status
= LTTNG_ERR_SNAPSHOT_FAIL
;
1546 health_code_update();
1547 pthread_mutex_lock(socket
->lock
);
1548 ret
= consumer_send_msg(socket
, &msg
);
1549 pthread_mutex_unlock(socket
->lock
);
1552 case LTTCOMM_CONSUMERD_CHAN_NOT_FOUND
:
1553 status
= LTTNG_ERR_CHAN_NOT_FOUND
;
1556 status
= LTTNG_ERR_SNAPSHOT_FAIL
;
1563 health_code_update();
1568 * Ask the consumer the number of discarded events for a channel.
1570 int consumer_get_discarded_events(uint64_t session_id
, uint64_t channel_key
,
1571 struct consumer_output
*consumer
, uint64_t *discarded
)
1574 struct consumer_socket
*socket
;
1575 struct lttng_ht_iter iter
;
1576 struct lttcomm_consumer_msg msg
;
1580 DBG3("Consumer discarded events id %" PRIu64
, session_id
);
1582 memset(&msg
, 0, sizeof(msg
));
1583 msg
.cmd_type
= LTTNG_CONSUMER_DISCARDED_EVENTS
;
1584 msg
.u
.discarded_events
.session_id
= session_id
;
1585 msg
.u
.discarded_events
.channel_key
= channel_key
;
1589 /* Send command for each consumer */
1591 cds_lfht_for_each_entry(consumer
->socks
->ht
, &iter
.iter
, socket
,
1593 uint64_t consumer_discarded
= 0;
1594 pthread_mutex_lock(socket
->lock
);
1595 ret
= consumer_socket_send(socket
, &msg
, sizeof(msg
));
1597 pthread_mutex_unlock(socket
->lock
);
1602 * No need for a recv reply status because the answer to the
1603 * command is the reply status message.
1605 ret
= consumer_socket_recv(socket
, &consumer_discarded
,
1606 sizeof(consumer_discarded
));
1608 ERR("get discarded events");
1609 pthread_mutex_unlock(socket
->lock
);
1612 pthread_mutex_unlock(socket
->lock
);
1613 *discarded
+= consumer_discarded
;
1616 DBG("Consumer discarded %" PRIu64
" events in session id %" PRIu64
,
1617 *discarded
, session_id
);
1625 * Ask the consumer the number of lost packets for a channel.
1627 int consumer_get_lost_packets(uint64_t session_id
, uint64_t channel_key
,
1628 struct consumer_output
*consumer
, uint64_t *lost
)
1631 struct consumer_socket
*socket
;
1632 struct lttng_ht_iter iter
;
1633 struct lttcomm_consumer_msg msg
;
1637 DBG3("Consumer lost packets id %" PRIu64
, session_id
);
1639 memset(&msg
, 0, sizeof(msg
));
1640 msg
.cmd_type
= LTTNG_CONSUMER_LOST_PACKETS
;
1641 msg
.u
.lost_packets
.session_id
= session_id
;
1642 msg
.u
.lost_packets
.channel_key
= channel_key
;
1646 /* Send command for each consumer */
1648 cds_lfht_for_each_entry(consumer
->socks
->ht
, &iter
.iter
, socket
,
1650 uint64_t consumer_lost
= 0;
1651 pthread_mutex_lock(socket
->lock
);
1652 ret
= consumer_socket_send(socket
, &msg
, sizeof(msg
));
1654 pthread_mutex_unlock(socket
->lock
);
1659 * No need for a recv reply status because the answer to the
1660 * command is the reply status message.
1662 ret
= consumer_socket_recv(socket
, &consumer_lost
,
1663 sizeof(consumer_lost
));
1665 ERR("get lost packets");
1666 pthread_mutex_unlock(socket
->lock
);
1669 pthread_mutex_unlock(socket
->lock
);
1670 *lost
+= consumer_lost
;
1673 DBG("Consumer lost %" PRIu64
" packets in session id %" PRIu64
,
1682 * Ask the consumer to rotate a channel.
1684 * The new_chunk_id is the session->rotate_count that has been incremented
1685 * when the rotation started. On the relay, this allows to keep track in which
1686 * chunk each stream is currently writing to (for the rotate_pending operation).
1688 int consumer_rotate_channel(struct consumer_socket
*socket
, uint64_t key
,
1689 uid_t uid
, gid_t gid
, struct consumer_output
*output
,
1690 bool is_metadata_channel
)
1693 struct lttcomm_consumer_msg msg
;
1697 DBG("Consumer rotate channel key %" PRIu64
, key
);
1699 pthread_mutex_lock(socket
->lock
);
1700 memset(&msg
, 0, sizeof(msg
));
1701 msg
.cmd_type
= LTTNG_CONSUMER_ROTATE_CHANNEL
;
1702 msg
.u
.rotate_channel
.key
= key
;
1703 msg
.u
.rotate_channel
.metadata
= !!is_metadata_channel
;
1705 if (output
->type
== CONSUMER_DST_NET
) {
1706 msg
.u
.rotate_channel
.relayd_id
= output
->net_seq_index
;
1708 msg
.u
.rotate_channel
.relayd_id
= (uint64_t) -1ULL;
1711 health_code_update();
1712 ret
= consumer_send_msg(socket
, &msg
);
1715 case LTTCOMM_CONSUMERD_CHAN_NOT_FOUND
:
1716 ret
= -LTTNG_ERR_CHAN_NOT_FOUND
;
1719 ret
= -LTTNG_ERR_ROTATION_FAIL_CONSUMER
;
1725 pthread_mutex_unlock(socket
->lock
);
1726 health_code_update();
1730 int consumer_init(struct consumer_socket
*socket
,
1731 const lttng_uuid sessiond_uuid
)
1734 struct lttcomm_consumer_msg msg
= {
1735 .cmd_type
= LTTNG_CONSUMER_INIT
,
1740 DBG("Sending consumer initialization command");
1741 lttng_uuid_copy(msg
.u
.init
.sessiond_uuid
, sessiond_uuid
);
1743 health_code_update();
1744 ret
= consumer_send_msg(socket
, &msg
);
1750 health_code_update();
1755 * Ask the consumer to create a new chunk for a given session.
1757 * Called with the consumer socket lock held.
1759 int consumer_create_trace_chunk(struct consumer_socket
*socket
,
1760 uint64_t relayd_id
, uint64_t session_id
,
1761 struct lttng_trace_chunk
*chunk
,
1762 const char *domain_subdir
)
1765 enum lttng_trace_chunk_status chunk_status
;
1766 struct lttng_credentials chunk_credentials
;
1767 const struct lttng_directory_handle
*chunk_directory_handle
= NULL
;
1768 struct lttng_directory_handle
*domain_handle
= NULL
;
1770 const char *chunk_name
;
1771 bool chunk_name_overridden
;
1773 time_t creation_timestamp
;
1774 char creation_timestamp_buffer
[ISO8601_STR_LEN
];
1775 const char *creation_timestamp_str
= "(none)";
1776 const bool chunk_has_local_output
= relayd_id
== -1ULL;
1777 struct lttcomm_consumer_msg msg
= {
1778 .cmd_type
= LTTNG_CONSUMER_CREATE_TRACE_CHUNK
,
1779 .u
.create_trace_chunk
.session_id
= session_id
,
1785 if (relayd_id
!= -1ULL) {
1786 LTTNG_OPTIONAL_SET(&msg
.u
.create_trace_chunk
.relayd_id
,
1790 chunk_status
= lttng_trace_chunk_get_name(chunk
, &chunk_name
,
1791 &chunk_name_overridden
);
1792 if (chunk_status
!= LTTNG_TRACE_CHUNK_STATUS_OK
&&
1793 chunk_status
!= LTTNG_TRACE_CHUNK_STATUS_NONE
) {
1794 ERR("Failed to get name of trace chunk");
1795 ret
= -LTTNG_ERR_FATAL
;
1798 if (chunk_name_overridden
) {
1799 ret
= lttng_strncpy(msg
.u
.create_trace_chunk
.override_name
,
1801 sizeof(msg
.u
.create_trace_chunk
.override_name
));
1803 ERR("Trace chunk name \"%s\" exceeds the maximal length allowed by the consumer protocol",
1805 ret
= -LTTNG_ERR_FATAL
;
1810 chunk_status
= lttng_trace_chunk_get_creation_timestamp(chunk
,
1811 &creation_timestamp
);
1812 if (chunk_status
!= LTTNG_TRACE_CHUNK_STATUS_OK
) {
1813 ret
= -LTTNG_ERR_FATAL
;
1816 msg
.u
.create_trace_chunk
.creation_timestamp
=
1817 (uint64_t) creation_timestamp
;
1818 /* Only used for logging purposes. */
1819 ret
= time_to_iso8601_str(creation_timestamp
,
1820 creation_timestamp_buffer
,
1821 sizeof(creation_timestamp_buffer
));
1822 creation_timestamp_str
= !ret
? creation_timestamp_buffer
:
1823 "(formatting error)";
1825 chunk_status
= lttng_trace_chunk_get_id(chunk
, &chunk_id
);
1826 if (chunk_status
!= LTTNG_TRACE_CHUNK_STATUS_OK
) {
1828 * Anonymous trace chunks should never be transmitted
1829 * to remote peers (consumerd and relayd). They are used
1830 * internally for backward-compatibility purposes.
1832 ret
= -LTTNG_ERR_FATAL
;
1835 msg
.u
.create_trace_chunk
.chunk_id
= chunk_id
;
1837 if (chunk_has_local_output
) {
1838 chunk_status
= lttng_trace_chunk_borrow_chunk_directory_handle(
1839 chunk
, &chunk_directory_handle
);
1840 if (chunk_status
!= LTTNG_TRACE_CHUNK_STATUS_OK
) {
1841 ret
= -LTTNG_ERR_FATAL
;
1844 chunk_status
= lttng_trace_chunk_get_credentials(
1845 chunk
, &chunk_credentials
);
1846 if (chunk_status
!= LTTNG_TRACE_CHUNK_STATUS_OK
) {
1848 * Not associating credentials to a sessiond chunk is a
1849 * fatal internal error.
1851 ret
= -LTTNG_ERR_FATAL
;
1854 ret
= lttng_directory_handle_create_subdirectory_as_user(
1855 chunk_directory_handle
,
1858 &chunk_credentials
);
1860 PERROR("Failed to create chunk domain output directory \"%s\"",
1862 ret
= -LTTNG_ERR_FATAL
;
1865 domain_handle
= lttng_directory_handle_create_from_handle(
1867 chunk_directory_handle
);
1868 if (!domain_handle
) {
1869 ret
= -LTTNG_ERR_FATAL
;
1874 * This will only compile on platforms that support
1875 * dirfd (POSIX.2008). This is fine as the session daemon
1876 * is only built for such platforms.
1878 * The ownership of the chunk directory handle's is maintained
1879 * by the trace chunk.
1881 domain_dirfd
= lttng_directory_handle_get_dirfd(
1883 assert(domain_dirfd
>= 0);
1885 msg
.u
.create_trace_chunk
.credentials
.value
.uid
=
1886 chunk_credentials
.uid
;
1887 msg
.u
.create_trace_chunk
.credentials
.value
.gid
=
1888 chunk_credentials
.gid
;
1889 msg
.u
.create_trace_chunk
.credentials
.is_set
= 1;
1892 DBG("Sending consumer create trace chunk command: relayd_id = %" PRId64
1893 ", session_id = %" PRIu64
", chunk_id = %" PRIu64
1894 ", creation_timestamp = %s",
1895 relayd_id
, session_id
, chunk_id
,
1896 creation_timestamp_str
);
1897 health_code_update();
1898 ret
= consumer_send_msg(socket
, &msg
);
1899 health_code_update();
1901 ERR("Trace chunk creation error on consumer");
1902 ret
= -LTTNG_ERR_CREATE_TRACE_CHUNK_FAIL_CONSUMER
;
1906 if (chunk_has_local_output
) {
1907 DBG("Sending trace chunk domain directory fd to consumer");
1908 health_code_update();
1909 ret
= consumer_send_fds(socket
, &domain_dirfd
, 1);
1910 health_code_update();
1912 ERR("Trace chunk creation error on consumer");
1913 ret
= -LTTNG_ERR_CREATE_TRACE_CHUNK_FAIL_CONSUMER
;
1918 lttng_directory_handle_put(domain_handle
);
1923 * Ask the consumer to close a trace chunk for a given session.
1925 * Called with the consumer socket lock held.
1927 int consumer_close_trace_chunk(struct consumer_socket
*socket
,
1928 uint64_t relayd_id
, uint64_t session_id
,
1929 struct lttng_trace_chunk
*chunk
,
1930 char *closed_trace_chunk_path
)
1933 enum lttng_trace_chunk_status chunk_status
;
1934 struct lttcomm_consumer_msg msg
= {
1935 .cmd_type
= LTTNG_CONSUMER_CLOSE_TRACE_CHUNK
,
1936 .u
.close_trace_chunk
.session_id
= session_id
,
1938 struct lttcomm_consumer_close_trace_chunk_reply reply
;
1940 time_t close_timestamp
;
1941 enum lttng_trace_chunk_command_type close_command
;
1942 const char *close_command_name
= "none";
1943 struct lttng_dynamic_buffer path_reception_buffer
;
1946 lttng_dynamic_buffer_init(&path_reception_buffer
);
1948 if (relayd_id
!= -1ULL) {
1950 &msg
.u
.close_trace_chunk
.relayd_id
, relayd_id
);
1953 chunk_status
= lttng_trace_chunk_get_close_command(
1954 chunk
, &close_command
);
1955 switch (chunk_status
) {
1956 case LTTNG_TRACE_CHUNK_STATUS_OK
:
1957 LTTNG_OPTIONAL_SET(&msg
.u
.close_trace_chunk
.close_command
,
1958 (uint32_t) close_command
);
1960 case LTTNG_TRACE_CHUNK_STATUS_NONE
:
1963 ERR("Failed to get trace chunk close command");
1968 chunk_status
= lttng_trace_chunk_get_id(chunk
, &chunk_id
);
1970 * Anonymous trace chunks should never be transmitted to remote peers
1971 * (consumerd and relayd). They are used internally for
1972 * backward-compatibility purposes.
1974 assert(chunk_status
== LTTNG_TRACE_CHUNK_STATUS_OK
);
1975 msg
.u
.close_trace_chunk
.chunk_id
= chunk_id
;
1977 chunk_status
= lttng_trace_chunk_get_close_timestamp(chunk
,
1980 * A trace chunk should be closed locally before being closed remotely.
1981 * Otherwise, the close timestamp would never be transmitted to the
1984 assert(chunk_status
== LTTNG_TRACE_CHUNK_STATUS_OK
);
1985 msg
.u
.close_trace_chunk
.close_timestamp
= (uint64_t) close_timestamp
;
1987 if (msg
.u
.close_trace_chunk
.close_command
.is_set
) {
1988 close_command_name
= lttng_trace_chunk_command_type_get_name(
1991 DBG("Sending consumer close trace chunk command: relayd_id = %" PRId64
1992 ", session_id = %" PRIu64
", chunk_id = %" PRIu64
1993 ", close command = \"%s\"",
1994 relayd_id
, session_id
, chunk_id
, close_command_name
);
1996 health_code_update();
1997 ret
= consumer_socket_send(socket
, &msg
, sizeof(struct lttcomm_consumer_msg
));
1999 ret
= -LTTNG_ERR_CLOSE_TRACE_CHUNK_FAIL_CONSUMER
;
2002 ret
= consumer_socket_recv(socket
, &reply
, sizeof(reply
));
2004 ret
= -LTTNG_ERR_CLOSE_TRACE_CHUNK_FAIL_CONSUMER
;
2007 if (reply
.path_length
>= LTTNG_PATH_MAX
) {
2008 ERR("Invalid path returned by relay daemon: %" PRIu32
"bytes exceeds maximal allowed length of %d bytes",
2009 reply
.path_length
, LTTNG_PATH_MAX
);
2010 ret
= -LTTNG_ERR_INVALID_PROTOCOL
;
2013 ret
= lttng_dynamic_buffer_set_size(&path_reception_buffer
,
2016 ERR("Failed to allocate reception buffer of path returned by the \"close trace chunk\" command");
2017 ret
= -LTTNG_ERR_NOMEM
;
2020 ret
= consumer_socket_recv(socket
, path_reception_buffer
.data
,
2021 path_reception_buffer
.size
);
2023 ERR("Communication error while receiving path of closed trace chunk");
2024 ret
= -LTTNG_ERR_CLOSE_TRACE_CHUNK_FAIL_CONSUMER
;
2027 if (path_reception_buffer
.data
[path_reception_buffer
.size
- 1] != '\0') {
2028 ERR("Invalid path returned by relay daemon: not null-terminated");
2029 ret
= -LTTNG_ERR_INVALID_PROTOCOL
;
2032 if (closed_trace_chunk_path
) {
2034 * closed_trace_chunk_path is assumed to have a length >=
2037 memcpy(closed_trace_chunk_path
, path_reception_buffer
.data
,
2038 path_reception_buffer
.size
);
2041 lttng_dynamic_buffer_reset(&path_reception_buffer
);
2042 health_code_update();
2047 * Ask the consumer if a trace chunk exists.
2049 * Called with the consumer socket lock held.
2050 * Returns 0 on success, or a negative value on error.
2052 int consumer_trace_chunk_exists(struct consumer_socket
*socket
,
2053 uint64_t relayd_id
, uint64_t session_id
,
2054 struct lttng_trace_chunk
*chunk
,
2055 enum consumer_trace_chunk_exists_status
*result
)
2058 enum lttng_trace_chunk_status chunk_status
;
2059 struct lttcomm_consumer_msg msg
= {
2060 .cmd_type
= LTTNG_CONSUMER_TRACE_CHUNK_EXISTS
,
2061 .u
.trace_chunk_exists
.session_id
= session_id
,
2064 const char *consumer_reply_str
;
2068 if (relayd_id
!= -1ULL) {
2069 LTTNG_OPTIONAL_SET(&msg
.u
.trace_chunk_exists
.relayd_id
,
2073 chunk_status
= lttng_trace_chunk_get_id(chunk
, &chunk_id
);
2074 if (chunk_status
!= LTTNG_TRACE_CHUNK_STATUS_OK
) {
2076 * Anonymous trace chunks should never be transmitted
2077 * to remote peers (consumerd and relayd). They are used
2078 * internally for backward-compatibility purposes.
2080 ret
= -LTTNG_ERR_FATAL
;
2083 msg
.u
.trace_chunk_exists
.chunk_id
= chunk_id
;
2085 DBG("Sending consumer trace chunk exists command: relayd_id = %" PRId64
2086 ", session_id = %" PRIu64
2087 ", chunk_id = %" PRIu64
, relayd_id
, session_id
, chunk_id
);
2089 health_code_update();
2090 ret
= consumer_send_msg(socket
, &msg
);
2092 case LTTCOMM_CONSUMERD_UNKNOWN_TRACE_CHUNK
:
2093 consumer_reply_str
= "unknown trace chunk";
2094 *result
= CONSUMER_TRACE_CHUNK_EXISTS_STATUS_UNKNOWN_CHUNK
;
2096 case LTTCOMM_CONSUMERD_TRACE_CHUNK_EXISTS_LOCAL
:
2097 consumer_reply_str
= "trace chunk exists locally";
2098 *result
= CONSUMER_TRACE_CHUNK_EXISTS_STATUS_EXISTS_LOCAL
;
2100 case LTTCOMM_CONSUMERD_TRACE_CHUNK_EXISTS_REMOTE
:
2101 consumer_reply_str
= "trace chunk exists on remote peer";
2102 *result
= CONSUMER_TRACE_CHUNK_EXISTS_STATUS_EXISTS_REMOTE
;
2105 ERR("Consumer returned an error from TRACE_CHUNK_EXISTS command");
2109 DBG("Consumer reply to TRACE_CHUNK_EXISTS command: %s",
2110 consumer_reply_str
);
2113 health_code_update();