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 memcpy(&output
->dst
, &src
->dst
, sizeof(output
->dst
));
630 ret
= consumer_copy_sockets(output
, src
);
638 consumer_output_put(output
);
643 * Copy consumer sockets from src to dst.
645 * Return 0 on success or else a negative value.
647 int consumer_copy_sockets(struct consumer_output
*dst
,
648 struct consumer_output
*src
)
651 struct lttng_ht_iter iter
;
652 struct consumer_socket
*socket
, *copy_sock
;
658 cds_lfht_for_each_entry(src
->socks
->ht
, &iter
.iter
, socket
, node
.node
) {
659 /* Ignore socket that are already there. */
660 copy_sock
= consumer_find_socket(*socket
->fd_ptr
, dst
);
665 /* Create new socket object. */
666 copy_sock
= consumer_allocate_socket(socket
->fd_ptr
);
667 if (copy_sock
== NULL
) {
673 copy_sock
->registered
= socket
->registered
;
675 * This is valid because this lock is shared accross all consumer
676 * object being the global lock of the consumer data structure of the
679 copy_sock
->lock
= socket
->lock
;
680 consumer_add_socket(copy_sock
, dst
);
689 * Set network URI to the consumer output.
691 * Return 0 on success. Return 1 if the URI were equal. Else, negative value on
694 int consumer_set_network_uri(const struct ltt_session
*session
,
695 struct consumer_output
*output
,
696 struct lttng_uri
*uri
)
699 struct lttng_uri
*dst_uri
= NULL
;
701 /* Code flow error safety net. */
705 switch (uri
->stype
) {
706 case LTTNG_STREAM_CONTROL
:
707 dst_uri
= &output
->dst
.net
.control
;
708 output
->dst
.net
.control_isset
= 1;
709 if (uri
->port
== 0) {
710 /* Assign default port. */
711 uri
->port
= DEFAULT_NETWORK_CONTROL_PORT
;
713 if (output
->dst
.net
.data_isset
&& uri
->port
==
714 output
->dst
.net
.data
.port
) {
715 ret
= -LTTNG_ERR_INVALID
;
719 DBG3("Consumer control URI set with port %d", uri
->port
);
721 case LTTNG_STREAM_DATA
:
722 dst_uri
= &output
->dst
.net
.data
;
723 output
->dst
.net
.data_isset
= 1;
724 if (uri
->port
== 0) {
725 /* Assign default port. */
726 uri
->port
= DEFAULT_NETWORK_DATA_PORT
;
728 if (output
->dst
.net
.control_isset
&& uri
->port
==
729 output
->dst
.net
.control
.port
) {
730 ret
= -LTTNG_ERR_INVALID
;
734 DBG3("Consumer data URI set with port %d", uri
->port
);
737 ERR("Set network uri type unknown %d", uri
->stype
);
738 ret
= -LTTNG_ERR_INVALID
;
742 ret
= uri_compare(dst_uri
, uri
);
744 /* Same URI, don't touch it and return success. */
745 DBG3("URI network compare are the same");
749 /* URIs were not equal, replacing it. */
750 memcpy(dst_uri
, uri
, sizeof(struct lttng_uri
));
751 output
->type
= CONSUMER_DST_NET
;
752 if (dst_uri
->stype
!= LTTNG_STREAM_CONTROL
) {
753 /* Only the control uri needs to contain the path. */
758 * If the user has specified a subdir as part of the control
759 * URL, the session's base output directory is:
760 * /RELAYD_OUTPUT_PATH/HOSTNAME/USER_SPECIFIED_DIR
762 * Hence, the "base_dir" from which all stream files and
763 * session rotation chunks are created takes the form
764 * /HOSTNAME/USER_SPECIFIED_DIR
766 * If the user has not specified an output directory as part of
767 * the control URL, the base output directory has the form:
768 * /RELAYD_OUTPUT_PATH/HOSTNAME/SESSION_NAME-CREATION_TIME
770 * Hence, the "base_dir" from which all stream files and
771 * session rotation chunks are created takes the form
772 * /HOSTNAME/SESSION_NAME-CREATION_TIME
774 * Note that automatically generated session names already
775 * contain the session's creation time. In that case, the
776 * creation time is omitted to prevent it from being duplicated
777 * in the final directory hierarchy.
780 if (strstr(uri
->subdir
, "../")) {
781 ERR("Network URI subdirs are not allowed to walk up the path hierarchy");
782 ret
= -LTTNG_ERR_INVALID
;
785 ret
= snprintf(output
->dst
.net
.base_dir
,
786 sizeof(output
->dst
.net
.base_dir
),
787 "/%s/%s/", session
->hostname
, uri
->subdir
);
789 if (session
->has_auto_generated_name
) {
790 ret
= snprintf(output
->dst
.net
.base_dir
,
791 sizeof(output
->dst
.net
.base_dir
),
792 "/%s/%s/", session
->hostname
,
795 char session_creation_datetime
[16];
799 timeinfo
= localtime(&session
->creation_time
);
801 ret
= -LTTNG_ERR_FATAL
;
804 strftime_ret
= strftime(session_creation_datetime
,
805 sizeof(session_creation_datetime
),
806 "%Y%m%d-%H%M%S", timeinfo
);
807 if (strftime_ret
== 0) {
808 ERR("Failed to format session creation timestamp while setting network URI");
809 ret
= -LTTNG_ERR_FATAL
;
812 ret
= snprintf(output
->dst
.net
.base_dir
,
813 sizeof(output
->dst
.net
.base_dir
),
814 "/%s/%s-%s/", session
->hostname
,
816 session_creation_datetime
);
819 if (ret
>= sizeof(output
->dst
.net
.base_dir
)) {
820 ret
= -LTTNG_ERR_INVALID
;
821 ERR("Truncation occurred while setting network output base directory");
823 } else if (ret
== -1) {
824 ret
= -LTTNG_ERR_INVALID
;
825 PERROR("Error occurred while setting network output base directory");
829 DBG3("Consumer set network uri base_dir path %s",
830 output
->dst
.net
.base_dir
);
841 * Send file descriptor to consumer via sock.
843 * The consumer socket lock must be held by the caller.
845 int consumer_send_fds(struct consumer_socket
*sock
, const int *fds
,
853 assert(pthread_mutex_trylock(sock
->lock
) == EBUSY
);
855 ret
= lttcomm_send_fds_unix_sock(*sock
->fd_ptr
, fds
, nb_fd
);
857 /* The above call will print a PERROR on error. */
858 DBG("Error when sending consumer fds on sock %d", *sock
->fd_ptr
);
862 ret
= consumer_recv_status_reply(sock
);
868 * Consumer send communication message structure to consumer.
870 * The consumer socket lock must be held by the caller.
872 int consumer_send_msg(struct consumer_socket
*sock
,
873 struct lttcomm_consumer_msg
*msg
)
879 assert(pthread_mutex_trylock(sock
->lock
) == EBUSY
);
881 ret
= consumer_socket_send(sock
, msg
, sizeof(struct lttcomm_consumer_msg
));
886 ret
= consumer_recv_status_reply(sock
);
893 * Consumer send channel communication message structure to consumer.
895 * The consumer socket lock must be held by the caller.
897 int consumer_send_channel(struct consumer_socket
*sock
,
898 struct lttcomm_consumer_msg
*msg
)
905 ret
= consumer_send_msg(sock
, msg
);
915 * Populate the given consumer msg structure with the ask_channel command
918 void consumer_init_ask_channel_comm_msg(struct lttcomm_consumer_msg
*msg
,
919 uint64_t subbuf_size
,
922 unsigned int switch_timer_interval
,
923 unsigned int read_timer_interval
,
924 unsigned int live_timer_interval
,
925 unsigned int monitor_timer_interval
,
929 const char *pathname
,
935 uint64_t tracefile_size
,
936 uint64_t tracefile_count
,
937 uint64_t session_id_per_pid
,
938 unsigned int monitor
,
939 uint32_t ust_app_uid
,
940 int64_t blocking_timeout
,
941 const char *root_shm_path
,
942 const char *shm_path
,
943 struct lttng_trace_chunk
*trace_chunk
,
944 const struct lttng_credentials
*buffer_credentials
)
948 /* Zeroed structure */
949 memset(msg
, 0, sizeof(struct lttcomm_consumer_msg
));
950 msg
->u
.ask_channel
.buffer_credentials
.uid
= UINT32_MAX
;
951 msg
->u
.ask_channel
.buffer_credentials
.gid
= UINT32_MAX
;
955 enum lttng_trace_chunk_status chunk_status
;
957 chunk_status
= lttng_trace_chunk_get_id(trace_chunk
, &chunk_id
);
958 assert(chunk_status
== LTTNG_TRACE_CHUNK_STATUS_OK
);
959 LTTNG_OPTIONAL_SET(&msg
->u
.ask_channel
.chunk_id
, chunk_id
);
961 msg
->u
.ask_channel
.buffer_credentials
.uid
= buffer_credentials
->uid
;
962 msg
->u
.ask_channel
.buffer_credentials
.gid
= buffer_credentials
->gid
;
964 msg
->cmd_type
= LTTNG_CONSUMER_ASK_CHANNEL_CREATION
;
965 msg
->u
.ask_channel
.subbuf_size
= subbuf_size
;
966 msg
->u
.ask_channel
.num_subbuf
= num_subbuf
;
967 msg
->u
.ask_channel
.overwrite
= overwrite
;
968 msg
->u
.ask_channel
.switch_timer_interval
= switch_timer_interval
;
969 msg
->u
.ask_channel
.read_timer_interval
= read_timer_interval
;
970 msg
->u
.ask_channel
.live_timer_interval
= live_timer_interval
;
971 msg
->u
.ask_channel
.monitor_timer_interval
= monitor_timer_interval
;
972 msg
->u
.ask_channel
.output
= output
;
973 msg
->u
.ask_channel
.type
= type
;
974 msg
->u
.ask_channel
.session_id
= session_id
;
975 msg
->u
.ask_channel
.session_id_per_pid
= session_id_per_pid
;
976 msg
->u
.ask_channel
.relayd_id
= relayd_id
;
977 msg
->u
.ask_channel
.key
= key
;
978 msg
->u
.ask_channel
.chan_id
= chan_id
;
979 msg
->u
.ask_channel
.tracefile_size
= tracefile_size
;
980 msg
->u
.ask_channel
.tracefile_count
= tracefile_count
;
981 msg
->u
.ask_channel
.monitor
= monitor
;
982 msg
->u
.ask_channel
.ust_app_uid
= ust_app_uid
;
983 msg
->u
.ask_channel
.blocking_timeout
= blocking_timeout
;
985 memcpy(msg
->u
.ask_channel
.uuid
, uuid
, sizeof(msg
->u
.ask_channel
.uuid
));
988 strncpy(msg
->u
.ask_channel
.pathname
, pathname
,
989 sizeof(msg
->u
.ask_channel
.pathname
));
990 msg
->u
.ask_channel
.pathname
[sizeof(msg
->u
.ask_channel
.pathname
)-1] = '\0';
993 strncpy(msg
->u
.ask_channel
.name
, name
, sizeof(msg
->u
.ask_channel
.name
));
994 msg
->u
.ask_channel
.name
[sizeof(msg
->u
.ask_channel
.name
) - 1] = '\0';
997 strncpy(msg
->u
.ask_channel
.root_shm_path
, root_shm_path
,
998 sizeof(msg
->u
.ask_channel
.root_shm_path
));
999 msg
->u
.ask_channel
.root_shm_path
[sizeof(msg
->u
.ask_channel
.root_shm_path
) - 1] = '\0';
1002 strncpy(msg
->u
.ask_channel
.shm_path
, shm_path
,
1003 sizeof(msg
->u
.ask_channel
.shm_path
));
1004 msg
->u
.ask_channel
.shm_path
[sizeof(msg
->u
.ask_channel
.shm_path
) - 1] = '\0';
1009 * Init channel communication message structure.
1011 void consumer_init_add_channel_comm_msg(struct lttcomm_consumer_msg
*msg
,
1012 uint64_t channel_key
,
1013 uint64_t session_id
,
1014 const char *pathname
,
1019 unsigned int nb_init_streams
,
1020 enum lttng_event_output output
,
1022 uint64_t tracefile_size
,
1023 uint64_t tracefile_count
,
1024 unsigned int monitor
,
1025 unsigned int live_timer_interval
,
1026 unsigned int monitor_timer_interval
,
1027 struct lttng_trace_chunk
*trace_chunk
)
1031 /* Zeroed structure */
1032 memset(msg
, 0, sizeof(struct lttcomm_consumer_msg
));
1036 enum lttng_trace_chunk_status chunk_status
;
1038 chunk_status
= lttng_trace_chunk_get_id(trace_chunk
, &chunk_id
);
1039 assert(chunk_status
== LTTNG_TRACE_CHUNK_STATUS_OK
);
1040 LTTNG_OPTIONAL_SET(&msg
->u
.channel
.chunk_id
, chunk_id
);
1044 msg
->cmd_type
= LTTNG_CONSUMER_ADD_CHANNEL
;
1045 msg
->u
.channel
.channel_key
= channel_key
;
1046 msg
->u
.channel
.session_id
= session_id
;
1047 msg
->u
.channel
.relayd_id
= relayd_id
;
1048 msg
->u
.channel
.nb_init_streams
= nb_init_streams
;
1049 msg
->u
.channel
.output
= output
;
1050 msg
->u
.channel
.type
= type
;
1051 msg
->u
.channel
.tracefile_size
= tracefile_size
;
1052 msg
->u
.channel
.tracefile_count
= tracefile_count
;
1053 msg
->u
.channel
.monitor
= monitor
;
1054 msg
->u
.channel
.live_timer_interval
= live_timer_interval
;
1055 msg
->u
.channel
.monitor_timer_interval
= monitor_timer_interval
;
1057 strncpy(msg
->u
.channel
.pathname
, pathname
,
1058 sizeof(msg
->u
.channel
.pathname
));
1059 msg
->u
.channel
.pathname
[sizeof(msg
->u
.channel
.pathname
) - 1] = '\0';
1061 strncpy(msg
->u
.channel
.name
, name
, sizeof(msg
->u
.channel
.name
));
1062 msg
->u
.channel
.name
[sizeof(msg
->u
.channel
.name
) - 1] = '\0';
1066 * Init stream communication message structure.
1068 void consumer_init_add_stream_comm_msg(struct lttcomm_consumer_msg
*msg
,
1069 uint64_t channel_key
,
1070 uint64_t stream_key
,
1075 memset(msg
, 0, sizeof(struct lttcomm_consumer_msg
));
1077 msg
->cmd_type
= LTTNG_CONSUMER_ADD_STREAM
;
1078 msg
->u
.stream
.channel_key
= channel_key
;
1079 msg
->u
.stream
.stream_key
= stream_key
;
1080 msg
->u
.stream
.cpu
= cpu
;
1083 void consumer_init_streams_sent_comm_msg(struct lttcomm_consumer_msg
*msg
,
1084 enum lttng_consumer_command cmd
,
1085 uint64_t channel_key
, uint64_t net_seq_idx
)
1089 memset(msg
, 0, sizeof(struct lttcomm_consumer_msg
));
1091 msg
->cmd_type
= cmd
;
1092 msg
->u
.sent_streams
.channel_key
= channel_key
;
1093 msg
->u
.sent_streams
.net_seq_idx
= net_seq_idx
;
1097 * Send stream communication structure to the consumer.
1099 int consumer_send_stream(struct consumer_socket
*sock
,
1100 struct consumer_output
*dst
, struct lttcomm_consumer_msg
*msg
,
1101 const int *fds
, size_t nb_fd
)
1110 ret
= consumer_send_msg(sock
, msg
);
1115 ret
= consumer_send_fds(sock
, fds
, nb_fd
);
1125 * Send relayd socket to consumer associated with a session name.
1127 * The consumer socket lock must be held by the caller.
1129 * On success return positive value. On error, negative value.
1131 int consumer_send_relayd_socket(struct consumer_socket
*consumer_sock
,
1132 struct lttcomm_relayd_sock
*rsock
, struct consumer_output
*consumer
,
1133 enum lttng_stream_type type
, uint64_t session_id
,
1134 const char *session_name
, const char *hostname
,
1135 const char *base_path
, int session_live_timer
,
1136 const uint64_t *current_chunk_id
, time_t session_creation_time
,
1137 bool session_name_contains_creation_time
)
1140 struct lttcomm_consumer_msg msg
;
1142 /* Code flow error. Safety net. */
1145 assert(consumer_sock
);
1147 memset(&msg
, 0, sizeof(msg
));
1148 /* Bail out if consumer is disabled */
1149 if (!consumer
->enabled
) {
1154 if (type
== LTTNG_STREAM_CONTROL
) {
1155 char output_path
[LTTNG_PATH_MAX
] = {};
1156 uint64_t relayd_session_id
;
1158 ret
= relayd_create_session(rsock
,
1160 session_name
, hostname
, base_path
,
1162 consumer
->snapshot
, session_id
,
1163 sessiond_uuid
, current_chunk_id
,
1164 session_creation_time
,
1165 session_name_contains_creation_time
,
1168 /* Close the control socket. */
1169 (void) relayd_close(rsock
);
1172 msg
.u
.relayd_sock
.relayd_session_id
= relayd_session_id
;
1173 DBG("Created session on relay, output path reply: %s",
1177 msg
.cmd_type
= LTTNG_CONSUMER_ADD_RELAYD_SOCKET
;
1179 * Assign network consumer output index using the temporary consumer since
1180 * this call should only be made from within a set_consumer_uri() function
1181 * call in the session daemon.
1183 msg
.u
.relayd_sock
.net_index
= consumer
->net_seq_index
;
1184 msg
.u
.relayd_sock
.type
= type
;
1185 msg
.u
.relayd_sock
.session_id
= session_id
;
1186 memcpy(&msg
.u
.relayd_sock
.sock
, rsock
, sizeof(msg
.u
.relayd_sock
.sock
));
1188 DBG3("Sending relayd sock info to consumer on %d", *consumer_sock
->fd_ptr
);
1189 ret
= consumer_send_msg(consumer_sock
, &msg
);
1194 DBG3("Sending relayd socket file descriptor to consumer");
1195 ret
= consumer_send_fds(consumer_sock
, ALIGNED_CONST_PTR(rsock
->sock
.fd
), 1);
1200 DBG2("Consumer relayd socket sent");
1207 int consumer_send_pipe(struct consumer_socket
*consumer_sock
,
1208 enum lttng_consumer_command cmd
, int pipe
)
1211 struct lttcomm_consumer_msg msg
;
1212 const char *pipe_name
;
1213 const char *command_name
;
1216 case LTTNG_CONSUMER_SET_CHANNEL_MONITOR_PIPE
:
1217 pipe_name
= "channel monitor";
1218 command_name
= "SET_CHANNEL_MONITOR_PIPE";
1221 ERR("Unexpected command received in %s (cmd = %d)", __func__
,
1226 /* Code flow error. Safety net. */
1228 memset(&msg
, 0, sizeof(msg
));
1231 pthread_mutex_lock(consumer_sock
->lock
);
1232 DBG3("Sending %s command to consumer", command_name
);
1233 ret
= consumer_send_msg(consumer_sock
, &msg
);
1238 DBG3("Sending %s pipe %d to consumer on socket %d",
1240 pipe
, *consumer_sock
->fd_ptr
);
1241 ret
= consumer_send_fds(consumer_sock
, &pipe
, 1);
1246 DBG2("%s pipe successfully sent", pipe_name
);
1248 pthread_mutex_unlock(consumer_sock
->lock
);
1252 int consumer_send_channel_monitor_pipe(struct consumer_socket
*consumer_sock
,
1255 return consumer_send_pipe(consumer_sock
,
1256 LTTNG_CONSUMER_SET_CHANNEL_MONITOR_PIPE
, pipe
);
1260 * Ask the consumer if the data is pending for the specific session id.
1261 * Returns 1 if data is pending, 0 otherwise, or < 0 on error.
1263 int consumer_is_data_pending(uint64_t session_id
,
1264 struct consumer_output
*consumer
)
1267 int32_t ret_code
= 0; /* Default is that the data is NOT pending */
1268 struct consumer_socket
*socket
;
1269 struct lttng_ht_iter iter
;
1270 struct lttcomm_consumer_msg msg
;
1274 DBG3("Consumer data pending for id %" PRIu64
, session_id
);
1276 memset(&msg
, 0, sizeof(msg
));
1277 msg
.cmd_type
= LTTNG_CONSUMER_DATA_PENDING
;
1278 msg
.u
.data_pending
.session_id
= session_id
;
1280 /* Send command for each consumer */
1282 cds_lfht_for_each_entry(consumer
->socks
->ht
, &iter
.iter
, socket
,
1284 pthread_mutex_lock(socket
->lock
);
1285 ret
= consumer_socket_send(socket
, &msg
, sizeof(msg
));
1287 pthread_mutex_unlock(socket
->lock
);
1292 * No need for a recv reply status because the answer to the command is
1293 * the reply status message.
1296 ret
= consumer_socket_recv(socket
, &ret_code
, sizeof(ret_code
));
1298 pthread_mutex_unlock(socket
->lock
);
1301 pthread_mutex_unlock(socket
->lock
);
1303 if (ret_code
== 1) {
1309 DBG("Consumer data is %s pending for session id %" PRIu64
,
1310 ret_code
== 1 ? "" : "NOT", session_id
);
1319 * Send a flush command to consumer using the given channel key.
1321 * Return 0 on success else a negative value.
1323 int consumer_flush_channel(struct consumer_socket
*socket
, uint64_t key
)
1326 struct lttcomm_consumer_msg msg
;
1330 DBG2("Consumer flush channel key %" PRIu64
, key
);
1332 memset(&msg
, 0, sizeof(msg
));
1333 msg
.cmd_type
= LTTNG_CONSUMER_FLUSH_CHANNEL
;
1334 msg
.u
.flush_channel
.key
= key
;
1336 pthread_mutex_lock(socket
->lock
);
1337 health_code_update();
1339 ret
= consumer_send_msg(socket
, &msg
);
1345 health_code_update();
1346 pthread_mutex_unlock(socket
->lock
);
1351 * Send a clear quiescent command to consumer using the given channel key.
1353 * Return 0 on success else a negative value.
1355 int consumer_clear_quiescent_channel(struct consumer_socket
*socket
, uint64_t key
)
1358 struct lttcomm_consumer_msg msg
;
1362 DBG2("Consumer clear quiescent channel key %" PRIu64
, key
);
1364 memset(&msg
, 0, sizeof(msg
));
1365 msg
.cmd_type
= LTTNG_CONSUMER_CLEAR_QUIESCENT_CHANNEL
;
1366 msg
.u
.clear_quiescent_channel
.key
= key
;
1368 pthread_mutex_lock(socket
->lock
);
1369 health_code_update();
1371 ret
= consumer_send_msg(socket
, &msg
);
1377 health_code_update();
1378 pthread_mutex_unlock(socket
->lock
);
1383 * Send a close metadata command to consumer using the given channel key.
1384 * Called with registry lock held.
1386 * Return 0 on success else a negative value.
1388 int consumer_close_metadata(struct consumer_socket
*socket
,
1389 uint64_t metadata_key
)
1392 struct lttcomm_consumer_msg msg
;
1396 DBG2("Consumer close metadata channel key %" PRIu64
, metadata_key
);
1398 memset(&msg
, 0, sizeof(msg
));
1399 msg
.cmd_type
= LTTNG_CONSUMER_CLOSE_METADATA
;
1400 msg
.u
.close_metadata
.key
= metadata_key
;
1402 pthread_mutex_lock(socket
->lock
);
1403 health_code_update();
1405 ret
= consumer_send_msg(socket
, &msg
);
1411 health_code_update();
1412 pthread_mutex_unlock(socket
->lock
);
1417 * Send a setup metdata command to consumer using the given channel key.
1419 * Return 0 on success else a negative value.
1421 int consumer_setup_metadata(struct consumer_socket
*socket
,
1422 uint64_t metadata_key
)
1425 struct lttcomm_consumer_msg msg
;
1429 DBG2("Consumer setup metadata channel key %" PRIu64
, metadata_key
);
1431 memset(&msg
, 0, sizeof(msg
));
1432 msg
.cmd_type
= LTTNG_CONSUMER_SETUP_METADATA
;
1433 msg
.u
.setup_metadata
.key
= metadata_key
;
1435 pthread_mutex_lock(socket
->lock
);
1436 health_code_update();
1438 ret
= consumer_send_msg(socket
, &msg
);
1444 health_code_update();
1445 pthread_mutex_unlock(socket
->lock
);
1450 * Send metadata string to consumer.
1451 * RCU read-side lock must be held to guarantee existence of socket.
1453 * Return 0 on success else a negative value.
1455 int consumer_push_metadata(struct consumer_socket
*socket
,
1456 uint64_t metadata_key
, char *metadata_str
, size_t len
,
1457 size_t target_offset
, uint64_t version
)
1460 struct lttcomm_consumer_msg msg
;
1464 DBG2("Consumer push metadata to consumer socket %d", *socket
->fd_ptr
);
1466 pthread_mutex_lock(socket
->lock
);
1468 memset(&msg
, 0, sizeof(msg
));
1469 msg
.cmd_type
= LTTNG_CONSUMER_PUSH_METADATA
;
1470 msg
.u
.push_metadata
.key
= metadata_key
;
1471 msg
.u
.push_metadata
.target_offset
= target_offset
;
1472 msg
.u
.push_metadata
.len
= len
;
1473 msg
.u
.push_metadata
.version
= version
;
1475 health_code_update();
1476 ret
= consumer_send_msg(socket
, &msg
);
1477 if (ret
< 0 || len
== 0) {
1481 DBG3("Consumer pushing metadata on sock %d of len %zu", *socket
->fd_ptr
,
1484 ret
= consumer_socket_send(socket
, metadata_str
, len
);
1489 health_code_update();
1490 ret
= consumer_recv_status_reply(socket
);
1496 pthread_mutex_unlock(socket
->lock
);
1497 health_code_update();
1502 * Ask the consumer to snapshot a specific channel using the key.
1504 * Returns LTTNG_OK on success or else an LTTng error code.
1506 enum lttng_error_code
consumer_snapshot_channel(struct consumer_socket
*socket
,
1507 uint64_t key
, const struct consumer_output
*output
, int metadata
,
1508 uid_t uid
, gid_t gid
, const char *channel_path
, int wait
,
1509 uint64_t nb_packets_per_stream
)
1512 enum lttng_error_code status
= LTTNG_OK
;
1513 struct lttcomm_consumer_msg msg
;
1518 DBG("Consumer snapshot channel key %" PRIu64
, key
);
1520 memset(&msg
, 0, sizeof(msg
));
1521 msg
.cmd_type
= LTTNG_CONSUMER_SNAPSHOT_CHANNEL
;
1522 msg
.u
.snapshot_channel
.key
= key
;
1523 msg
.u
.snapshot_channel
.nb_packets_per_stream
= nb_packets_per_stream
;
1524 msg
.u
.snapshot_channel
.metadata
= metadata
;
1526 if (output
->type
== CONSUMER_DST_NET
) {
1527 msg
.u
.snapshot_channel
.relayd_id
=
1528 output
->net_seq_index
;
1529 msg
.u
.snapshot_channel
.use_relayd
= 1;
1531 msg
.u
.snapshot_channel
.relayd_id
= (uint64_t) -1ULL;
1533 ret
= lttng_strncpy(msg
.u
.snapshot_channel
.pathname
,
1535 sizeof(msg
.u
.snapshot_channel
.pathname
));
1537 ERR("Snapshot path exceeds the maximal allowed length of %zu bytes (%zu bytes required) with path \"%s\"",
1538 sizeof(msg
.u
.snapshot_channel
.pathname
),
1539 strlen(channel_path
),
1541 status
= LTTNG_ERR_SNAPSHOT_FAIL
;
1545 health_code_update();
1546 pthread_mutex_lock(socket
->lock
);
1547 ret
= consumer_send_msg(socket
, &msg
);
1548 pthread_mutex_unlock(socket
->lock
);
1551 case LTTCOMM_CONSUMERD_CHAN_NOT_FOUND
:
1552 status
= LTTNG_ERR_CHAN_NOT_FOUND
;
1555 status
= LTTNG_ERR_SNAPSHOT_FAIL
;
1562 health_code_update();
1567 * Ask the consumer the number of discarded events for a channel.
1569 int consumer_get_discarded_events(uint64_t session_id
, uint64_t channel_key
,
1570 struct consumer_output
*consumer
, uint64_t *discarded
)
1573 struct consumer_socket
*socket
;
1574 struct lttng_ht_iter iter
;
1575 struct lttcomm_consumer_msg msg
;
1579 DBG3("Consumer discarded events id %" PRIu64
, session_id
);
1581 memset(&msg
, 0, sizeof(msg
));
1582 msg
.cmd_type
= LTTNG_CONSUMER_DISCARDED_EVENTS
;
1583 msg
.u
.discarded_events
.session_id
= session_id
;
1584 msg
.u
.discarded_events
.channel_key
= channel_key
;
1588 /* Send command for each consumer */
1590 cds_lfht_for_each_entry(consumer
->socks
->ht
, &iter
.iter
, socket
,
1592 uint64_t consumer_discarded
= 0;
1593 pthread_mutex_lock(socket
->lock
);
1594 ret
= consumer_socket_send(socket
, &msg
, sizeof(msg
));
1596 pthread_mutex_unlock(socket
->lock
);
1601 * No need for a recv reply status because the answer to the
1602 * command is the reply status message.
1604 ret
= consumer_socket_recv(socket
, &consumer_discarded
,
1605 sizeof(consumer_discarded
));
1607 ERR("get discarded events");
1608 pthread_mutex_unlock(socket
->lock
);
1611 pthread_mutex_unlock(socket
->lock
);
1612 *discarded
+= consumer_discarded
;
1615 DBG("Consumer discarded %" PRIu64
" events in session id %" PRIu64
,
1616 *discarded
, session_id
);
1624 * Ask the consumer the number of lost packets for a channel.
1626 int consumer_get_lost_packets(uint64_t session_id
, uint64_t channel_key
,
1627 struct consumer_output
*consumer
, uint64_t *lost
)
1630 struct consumer_socket
*socket
;
1631 struct lttng_ht_iter iter
;
1632 struct lttcomm_consumer_msg msg
;
1636 DBG3("Consumer lost packets id %" PRIu64
, session_id
);
1638 memset(&msg
, 0, sizeof(msg
));
1639 msg
.cmd_type
= LTTNG_CONSUMER_LOST_PACKETS
;
1640 msg
.u
.lost_packets
.session_id
= session_id
;
1641 msg
.u
.lost_packets
.channel_key
= channel_key
;
1645 /* Send command for each consumer */
1647 cds_lfht_for_each_entry(consumer
->socks
->ht
, &iter
.iter
, socket
,
1649 uint64_t consumer_lost
= 0;
1650 pthread_mutex_lock(socket
->lock
);
1651 ret
= consumer_socket_send(socket
, &msg
, sizeof(msg
));
1653 pthread_mutex_unlock(socket
->lock
);
1658 * No need for a recv reply status because the answer to the
1659 * command is the reply status message.
1661 ret
= consumer_socket_recv(socket
, &consumer_lost
,
1662 sizeof(consumer_lost
));
1664 ERR("get lost packets");
1665 pthread_mutex_unlock(socket
->lock
);
1668 pthread_mutex_unlock(socket
->lock
);
1669 *lost
+= consumer_lost
;
1672 DBG("Consumer lost %" PRIu64
" packets in session id %" PRIu64
,
1681 * Ask the consumer to rotate a channel.
1683 * The new_chunk_id is the session->rotate_count that has been incremented
1684 * when the rotation started. On the relay, this allows to keep track in which
1685 * chunk each stream is currently writing to (for the rotate_pending operation).
1687 int consumer_rotate_channel(struct consumer_socket
*socket
, uint64_t key
,
1688 uid_t uid
, gid_t gid
, struct consumer_output
*output
,
1689 bool is_metadata_channel
)
1692 struct lttcomm_consumer_msg msg
;
1696 DBG("Consumer rotate channel key %" PRIu64
, key
);
1698 pthread_mutex_lock(socket
->lock
);
1699 memset(&msg
, 0, sizeof(msg
));
1700 msg
.cmd_type
= LTTNG_CONSUMER_ROTATE_CHANNEL
;
1701 msg
.u
.rotate_channel
.key
= key
;
1702 msg
.u
.rotate_channel
.metadata
= !!is_metadata_channel
;
1704 if (output
->type
== CONSUMER_DST_NET
) {
1705 msg
.u
.rotate_channel
.relayd_id
= output
->net_seq_index
;
1707 msg
.u
.rotate_channel
.relayd_id
= (uint64_t) -1ULL;
1710 health_code_update();
1711 ret
= consumer_send_msg(socket
, &msg
);
1714 case LTTCOMM_CONSUMERD_CHAN_NOT_FOUND
:
1715 ret
= -LTTNG_ERR_CHAN_NOT_FOUND
;
1718 ret
= -LTTNG_ERR_ROTATION_FAIL_CONSUMER
;
1724 pthread_mutex_unlock(socket
->lock
);
1725 health_code_update();
1729 int consumer_init(struct consumer_socket
*socket
,
1730 const lttng_uuid sessiond_uuid
)
1733 struct lttcomm_consumer_msg msg
= {
1734 .cmd_type
= LTTNG_CONSUMER_INIT
,
1739 DBG("Sending consumer initialization command");
1740 lttng_uuid_copy(msg
.u
.init
.sessiond_uuid
, sessiond_uuid
);
1742 health_code_update();
1743 ret
= consumer_send_msg(socket
, &msg
);
1749 health_code_update();
1754 * Ask the consumer to create a new chunk for a given session.
1756 * Called with the consumer socket lock held.
1758 int consumer_create_trace_chunk(struct consumer_socket
*socket
,
1759 uint64_t relayd_id
, uint64_t session_id
,
1760 struct lttng_trace_chunk
*chunk
,
1761 const char *domain_subdir
)
1764 enum lttng_trace_chunk_status chunk_status
;
1765 struct lttng_credentials chunk_credentials
;
1766 const struct lttng_directory_handle
*chunk_directory_handle
= NULL
;
1767 struct lttng_directory_handle
*domain_handle
= NULL
;
1769 const char *chunk_name
;
1770 bool chunk_name_overridden
;
1772 time_t creation_timestamp
;
1773 char creation_timestamp_buffer
[ISO8601_STR_LEN
];
1774 const char *creation_timestamp_str
= "(none)";
1775 const bool chunk_has_local_output
= relayd_id
== -1ULL;
1776 struct lttcomm_consumer_msg msg
= {
1777 .cmd_type
= LTTNG_CONSUMER_CREATE_TRACE_CHUNK
,
1778 .u
.create_trace_chunk
.session_id
= session_id
,
1784 if (relayd_id
!= -1ULL) {
1785 LTTNG_OPTIONAL_SET(&msg
.u
.create_trace_chunk
.relayd_id
,
1789 chunk_status
= lttng_trace_chunk_get_name(chunk
, &chunk_name
,
1790 &chunk_name_overridden
);
1791 if (chunk_status
!= LTTNG_TRACE_CHUNK_STATUS_OK
&&
1792 chunk_status
!= LTTNG_TRACE_CHUNK_STATUS_NONE
) {
1793 ERR("Failed to get name of trace chunk");
1794 ret
= -LTTNG_ERR_FATAL
;
1797 if (chunk_name_overridden
) {
1798 ret
= lttng_strncpy(msg
.u
.create_trace_chunk
.override_name
,
1800 sizeof(msg
.u
.create_trace_chunk
.override_name
));
1802 ERR("Trace chunk name \"%s\" exceeds the maximal length allowed by the consumer protocol",
1804 ret
= -LTTNG_ERR_FATAL
;
1809 chunk_status
= lttng_trace_chunk_get_creation_timestamp(chunk
,
1810 &creation_timestamp
);
1811 if (chunk_status
!= LTTNG_TRACE_CHUNK_STATUS_OK
) {
1812 ret
= -LTTNG_ERR_FATAL
;
1815 msg
.u
.create_trace_chunk
.creation_timestamp
=
1816 (uint64_t) creation_timestamp
;
1817 /* Only used for logging purposes. */
1818 ret
= time_to_iso8601_str(creation_timestamp
,
1819 creation_timestamp_buffer
,
1820 sizeof(creation_timestamp_buffer
));
1821 creation_timestamp_str
= !ret
? creation_timestamp_buffer
:
1822 "(formatting error)";
1824 chunk_status
= lttng_trace_chunk_get_id(chunk
, &chunk_id
);
1825 if (chunk_status
!= LTTNG_TRACE_CHUNK_STATUS_OK
) {
1827 * Anonymous trace chunks should never be transmitted
1828 * to remote peers (consumerd and relayd). They are used
1829 * internally for backward-compatibility purposes.
1831 ret
= -LTTNG_ERR_FATAL
;
1834 msg
.u
.create_trace_chunk
.chunk_id
= chunk_id
;
1836 if (chunk_has_local_output
) {
1837 chunk_status
= lttng_trace_chunk_borrow_chunk_directory_handle(
1838 chunk
, &chunk_directory_handle
);
1839 if (chunk_status
!= LTTNG_TRACE_CHUNK_STATUS_OK
) {
1840 ret
= -LTTNG_ERR_FATAL
;
1843 chunk_status
= lttng_trace_chunk_get_credentials(
1844 chunk
, &chunk_credentials
);
1845 if (chunk_status
!= LTTNG_TRACE_CHUNK_STATUS_OK
) {
1847 * Not associating credentials to a sessiond chunk is a
1848 * fatal internal error.
1850 ret
= -LTTNG_ERR_FATAL
;
1853 ret
= lttng_directory_handle_create_subdirectory_as_user(
1854 chunk_directory_handle
,
1857 &chunk_credentials
);
1859 PERROR("Failed to create chunk domain output directory \"%s\"",
1861 ret
= -LTTNG_ERR_FATAL
;
1864 domain_handle
= lttng_directory_handle_create_from_handle(
1866 chunk_directory_handle
);
1867 if (!domain_handle
) {
1868 ret
= -LTTNG_ERR_FATAL
;
1873 * This will only compile on platforms that support
1874 * dirfd (POSIX.2008). This is fine as the session daemon
1875 * is only built for such platforms.
1877 * The ownership of the chunk directory handle's is maintained
1878 * by the trace chunk.
1880 domain_dirfd
= lttng_directory_handle_get_dirfd(
1882 assert(domain_dirfd
>= 0);
1884 msg
.u
.create_trace_chunk
.credentials
.value
.uid
=
1885 chunk_credentials
.uid
;
1886 msg
.u
.create_trace_chunk
.credentials
.value
.gid
=
1887 chunk_credentials
.gid
;
1888 msg
.u
.create_trace_chunk
.credentials
.is_set
= 1;
1891 DBG("Sending consumer create trace chunk command: relayd_id = %" PRId64
1892 ", session_id = %" PRIu64
", chunk_id = %" PRIu64
1893 ", creation_timestamp = %s",
1894 relayd_id
, session_id
, chunk_id
,
1895 creation_timestamp_str
);
1896 health_code_update();
1897 ret
= consumer_send_msg(socket
, &msg
);
1898 health_code_update();
1900 ERR("Trace chunk creation error on consumer");
1901 ret
= -LTTNG_ERR_CREATE_TRACE_CHUNK_FAIL_CONSUMER
;
1905 if (chunk_has_local_output
) {
1906 DBG("Sending trace chunk domain directory fd to consumer");
1907 health_code_update();
1908 ret
= consumer_send_fds(socket
, &domain_dirfd
, 1);
1909 health_code_update();
1911 ERR("Trace chunk creation error on consumer");
1912 ret
= -LTTNG_ERR_CREATE_TRACE_CHUNK_FAIL_CONSUMER
;
1917 lttng_directory_handle_put(domain_handle
);
1922 * Ask the consumer to close a trace chunk for a given session.
1924 * Called with the consumer socket lock held.
1926 int consumer_close_trace_chunk(struct consumer_socket
*socket
,
1927 uint64_t relayd_id
, uint64_t session_id
,
1928 struct lttng_trace_chunk
*chunk
,
1929 char *closed_trace_chunk_path
)
1932 enum lttng_trace_chunk_status chunk_status
;
1933 struct lttcomm_consumer_msg msg
= {
1934 .cmd_type
= LTTNG_CONSUMER_CLOSE_TRACE_CHUNK
,
1935 .u
.close_trace_chunk
.session_id
= session_id
,
1937 struct lttcomm_consumer_close_trace_chunk_reply reply
;
1939 time_t close_timestamp
;
1940 enum lttng_trace_chunk_command_type close_command
;
1941 const char *close_command_name
= "none";
1942 struct lttng_dynamic_buffer path_reception_buffer
;
1945 lttng_dynamic_buffer_init(&path_reception_buffer
);
1947 if (relayd_id
!= -1ULL) {
1949 &msg
.u
.close_trace_chunk
.relayd_id
, relayd_id
);
1952 chunk_status
= lttng_trace_chunk_get_close_command(
1953 chunk
, &close_command
);
1954 switch (chunk_status
) {
1955 case LTTNG_TRACE_CHUNK_STATUS_OK
:
1956 LTTNG_OPTIONAL_SET(&msg
.u
.close_trace_chunk
.close_command
,
1957 (uint32_t) close_command
);
1959 case LTTNG_TRACE_CHUNK_STATUS_NONE
:
1962 ERR("Failed to get trace chunk close command");
1967 chunk_status
= lttng_trace_chunk_get_id(chunk
, &chunk_id
);
1969 * Anonymous trace chunks should never be transmitted to remote peers
1970 * (consumerd and relayd). They are used internally for
1971 * backward-compatibility purposes.
1973 assert(chunk_status
== LTTNG_TRACE_CHUNK_STATUS_OK
);
1974 msg
.u
.close_trace_chunk
.chunk_id
= chunk_id
;
1976 chunk_status
= lttng_trace_chunk_get_close_timestamp(chunk
,
1979 * A trace chunk should be closed locally before being closed remotely.
1980 * Otherwise, the close timestamp would never be transmitted to the
1983 assert(chunk_status
== LTTNG_TRACE_CHUNK_STATUS_OK
);
1984 msg
.u
.close_trace_chunk
.close_timestamp
= (uint64_t) close_timestamp
;
1986 if (msg
.u
.close_trace_chunk
.close_command
.is_set
) {
1987 close_command_name
= lttng_trace_chunk_command_type_get_name(
1990 DBG("Sending consumer close trace chunk command: relayd_id = %" PRId64
1991 ", session_id = %" PRIu64
", chunk_id = %" PRIu64
1992 ", close command = \"%s\"",
1993 relayd_id
, session_id
, chunk_id
, close_command_name
);
1995 health_code_update();
1996 ret
= consumer_socket_send(socket
, &msg
, sizeof(struct lttcomm_consumer_msg
));
1998 ret
= -LTTNG_ERR_CLOSE_TRACE_CHUNK_FAIL_CONSUMER
;
2001 ret
= consumer_socket_recv(socket
, &reply
, sizeof(reply
));
2003 ret
= -LTTNG_ERR_CLOSE_TRACE_CHUNK_FAIL_CONSUMER
;
2006 if (reply
.path_length
>= LTTNG_PATH_MAX
) {
2007 ERR("Invalid path returned by relay daemon: %" PRIu32
"bytes exceeds maximal allowed length of %d bytes",
2008 reply
.path_length
, LTTNG_PATH_MAX
);
2009 ret
= -LTTNG_ERR_INVALID_PROTOCOL
;
2012 ret
= lttng_dynamic_buffer_set_size(&path_reception_buffer
,
2015 ERR("Failed to allocate reception buffer of path returned by the \"close trace chunk\" command");
2016 ret
= -LTTNG_ERR_NOMEM
;
2019 ret
= consumer_socket_recv(socket
, path_reception_buffer
.data
,
2020 path_reception_buffer
.size
);
2022 ERR("Communication error while receiving path of closed trace chunk");
2023 ret
= -LTTNG_ERR_CLOSE_TRACE_CHUNK_FAIL_CONSUMER
;
2026 if (path_reception_buffer
.data
[path_reception_buffer
.size
- 1] != '\0') {
2027 ERR("Invalid path returned by relay daemon: not null-terminated");
2028 ret
= -LTTNG_ERR_INVALID_PROTOCOL
;
2031 if (closed_trace_chunk_path
) {
2033 * closed_trace_chunk_path is assumed to have a length >=
2036 memcpy(closed_trace_chunk_path
, path_reception_buffer
.data
,
2037 path_reception_buffer
.size
);
2040 lttng_dynamic_buffer_reset(&path_reception_buffer
);
2041 health_code_update();
2046 * Ask the consumer if a trace chunk exists.
2048 * Called with the consumer socket lock held.
2049 * Returns 0 on success, or a negative value on error.
2051 int consumer_trace_chunk_exists(struct consumer_socket
*socket
,
2052 uint64_t relayd_id
, uint64_t session_id
,
2053 struct lttng_trace_chunk
*chunk
,
2054 enum consumer_trace_chunk_exists_status
*result
)
2057 enum lttng_trace_chunk_status chunk_status
;
2058 struct lttcomm_consumer_msg msg
= {
2059 .cmd_type
= LTTNG_CONSUMER_TRACE_CHUNK_EXISTS
,
2060 .u
.trace_chunk_exists
.session_id
= session_id
,
2063 const char *consumer_reply_str
;
2067 if (relayd_id
!= -1ULL) {
2068 LTTNG_OPTIONAL_SET(&msg
.u
.trace_chunk_exists
.relayd_id
,
2072 chunk_status
= lttng_trace_chunk_get_id(chunk
, &chunk_id
);
2073 if (chunk_status
!= LTTNG_TRACE_CHUNK_STATUS_OK
) {
2075 * Anonymous trace chunks should never be transmitted
2076 * to remote peers (consumerd and relayd). They are used
2077 * internally for backward-compatibility purposes.
2079 ret
= -LTTNG_ERR_FATAL
;
2082 msg
.u
.trace_chunk_exists
.chunk_id
= chunk_id
;
2084 DBG("Sending consumer trace chunk exists command: relayd_id = %" PRId64
2085 ", session_id = %" PRIu64
2086 ", chunk_id = %" PRIu64
, relayd_id
, session_id
, chunk_id
);
2088 health_code_update();
2089 ret
= consumer_send_msg(socket
, &msg
);
2091 case LTTCOMM_CONSUMERD_UNKNOWN_TRACE_CHUNK
:
2092 consumer_reply_str
= "unknown trace chunk";
2093 *result
= CONSUMER_TRACE_CHUNK_EXISTS_STATUS_UNKNOWN_CHUNK
;
2095 case LTTCOMM_CONSUMERD_TRACE_CHUNK_EXISTS_LOCAL
:
2096 consumer_reply_str
= "trace chunk exists locally";
2097 *result
= CONSUMER_TRACE_CHUNK_EXISTS_STATUS_EXISTS_LOCAL
;
2099 case LTTCOMM_CONSUMERD_TRACE_CHUNK_EXISTS_REMOTE
:
2100 consumer_reply_str
= "trace chunk exists on remote peer";
2101 *result
= CONSUMER_TRACE_CHUNK_EXISTS_STATUS_EXISTS_REMOTE
;
2104 ERR("Consumer returned an error from TRACE_CHUNK_EXISTS command");
2108 DBG("Consumer reply to TRACE_CHUNK_EXISTS command: %s",
2109 consumer_reply_str
);
2112 health_code_update();