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
)
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
,
77 ret
= snprintf(pathname
, LTTNG_PATH_MAX
, "%s%s",
78 consumer
->domain_subdir
, session_path
);
80 DBG3("Consumer trace path relative to current trace chunk: \"%s\"",
83 PERROR("Failed to format channel path");
85 } else if (ret
>= LTTNG_PATH_MAX
) {
86 ERR("Truncation occurred while formatting channel path");
97 * Send a data payload using a given consumer socket of size len.
99 * The consumer socket lock MUST be acquired before calling this since this
100 * function can change the fd value.
102 * Return 0 on success else a negative value on error.
104 int consumer_socket_send(struct consumer_socket
*socket
, void *msg
, size_t len
)
110 assert(socket
->fd_ptr
);
113 /* Consumer socket is invalid. Stopping. */
114 fd
= *socket
->fd_ptr
;
119 size
= lttcomm_send_unix_sock(fd
, msg
, len
);
121 /* The above call will print a PERROR on error. */
122 DBG("Error when sending data to consumer on sock %d", fd
);
124 * At this point, the socket is not usable anymore thus closing it and
125 * setting the file descriptor to -1 so it is not reused.
128 /* This call will PERROR on error. */
129 (void) lttcomm_close_unix_sock(fd
);
130 *socket
->fd_ptr
= -1;
141 * Receive a data payload using a given consumer socket of size len.
143 * The consumer socket lock MUST be acquired before calling this since this
144 * function can change the fd value.
146 * Return 0 on success else a negative value on error.
148 int consumer_socket_recv(struct consumer_socket
*socket
, void *msg
, size_t len
)
154 assert(socket
->fd_ptr
);
157 /* Consumer socket is invalid. Stopping. */
158 fd
= *socket
->fd_ptr
;
163 size
= lttcomm_recv_unix_sock(fd
, msg
, len
);
165 /* The above call will print a PERROR on error. */
166 DBG("Error when receiving data from the consumer socket %d", fd
);
168 * At this point, the socket is not usable anymore thus closing it and
169 * setting the file descriptor to -1 so it is not reused.
172 /* This call will PERROR on error. */
173 (void) lttcomm_close_unix_sock(fd
);
174 *socket
->fd_ptr
= -1;
185 * Receive a reply command status message from the consumer. Consumer socket
186 * lock MUST be acquired before calling this function.
188 * Return 0 on success, -1 on recv error or a negative lttng error code which
189 * was possibly returned by the consumer.
191 int consumer_recv_status_reply(struct consumer_socket
*sock
)
194 struct lttcomm_consumer_status_msg reply
;
198 ret
= consumer_socket_recv(sock
, &reply
, sizeof(reply
));
203 if (reply
.ret_code
== LTTCOMM_CONSUMERD_SUCCESS
) {
207 ret
= -reply
.ret_code
;
208 DBG("Consumer ret code %d", ret
);
216 * Once the ASK_CHANNEL command is sent to the consumer, the channel
217 * information are sent back. This call receives that data and populates key
220 * On success return 0 and both key and stream_count are set. On error, a
221 * negative value is sent back and both parameters are untouched.
223 int consumer_recv_status_channel(struct consumer_socket
*sock
,
224 uint64_t *key
, unsigned int *stream_count
)
227 struct lttcomm_consumer_status_channel reply
;
230 assert(stream_count
);
233 ret
= consumer_socket_recv(sock
, &reply
, sizeof(reply
));
238 /* An error is possible so don't touch the key and stream_count. */
239 if (reply
.ret_code
!= LTTCOMM_CONSUMERD_SUCCESS
) {
245 *stream_count
= reply
.stream_count
;
253 * Send destroy relayd command to consumer.
255 * On success return positive value. On error, negative value.
257 int consumer_send_destroy_relayd(struct consumer_socket
*sock
,
258 struct consumer_output
*consumer
)
261 struct lttcomm_consumer_msg msg
;
266 DBG2("Sending destroy relayd command to consumer sock %d", *sock
->fd_ptr
);
268 memset(&msg
, 0, sizeof(msg
));
269 msg
.cmd_type
= LTTNG_CONSUMER_DESTROY_RELAYD
;
270 msg
.u
.destroy_relayd
.net_seq_idx
= consumer
->net_seq_index
;
272 pthread_mutex_lock(sock
->lock
);
273 ret
= consumer_socket_send(sock
, &msg
, sizeof(msg
));
278 /* Don't check the return value. The caller will do it. */
279 ret
= consumer_recv_status_reply(sock
);
281 DBG2("Consumer send destroy relayd command done");
284 pthread_mutex_unlock(sock
->lock
);
289 * For each consumer socket in the consumer output object, send a destroy
292 void consumer_output_send_destroy_relayd(struct consumer_output
*consumer
)
294 struct lttng_ht_iter iter
;
295 struct consumer_socket
*socket
;
299 /* Destroy any relayd connection */
300 if (consumer
->type
== CONSUMER_DST_NET
) {
302 cds_lfht_for_each_entry(consumer
->socks
->ht
, &iter
.iter
, socket
,
306 /* Send destroy relayd command */
307 ret
= consumer_send_destroy_relayd(socket
, consumer
);
309 DBG("Unable to send destroy relayd command to consumer");
310 /* Continue since we MUST delete everything at this point. */
318 * From a consumer_data structure, allocate and add a consumer socket to the
321 * Return 0 on success, else negative value on error
323 int consumer_create_socket(struct consumer_data
*data
,
324 struct consumer_output
*output
)
327 struct consumer_socket
*socket
;
331 if (output
== NULL
|| data
->cmd_sock
< 0) {
333 * Not an error. Possible there is simply not spawned consumer or it's
334 * disabled for the tracing session asking the socket.
340 socket
= consumer_find_socket(data
->cmd_sock
, output
);
342 if (socket
== NULL
) {
343 socket
= consumer_allocate_socket(&data
->cmd_sock
);
344 if (socket
== NULL
) {
349 socket
->registered
= 0;
350 socket
->lock
= &data
->lock
;
352 consumer_add_socket(socket
, output
);
356 socket
->type
= data
->type
;
358 DBG3("Consumer socket created (fd: %d) and added to output",
366 * Return the consumer socket from the given consumer output with the right
367 * bitness. On error, returns NULL.
369 * The caller MUST acquire a rcu read side lock and keep it until the socket
370 * object reference is not needed anymore.
372 struct consumer_socket
*consumer_find_socket_by_bitness(int bits
,
373 const struct consumer_output
*consumer
)
376 struct consumer_socket
*socket
= NULL
;
380 consumer_fd
= uatomic_read(&ust_consumerd64_fd
);
383 consumer_fd
= uatomic_read(&ust_consumerd32_fd
);
390 socket
= consumer_find_socket(consumer_fd
, consumer
);
392 ERR("Consumer socket fd %d not found in consumer obj %p",
393 consumer_fd
, consumer
);
401 * Find a consumer_socket in a consumer_output hashtable. Read side lock must
402 * be acquired before calling this function and across use of the
403 * returned consumer_socket.
405 struct consumer_socket
*consumer_find_socket(int key
,
406 const struct consumer_output
*consumer
)
408 struct lttng_ht_iter iter
;
409 struct lttng_ht_node_ulong
*node
;
410 struct consumer_socket
*socket
= NULL
;
412 /* Negative keys are lookup failures */
413 if (key
< 0 || consumer
== NULL
) {
417 lttng_ht_lookup(consumer
->socks
, (void *)((unsigned long) key
),
419 node
= lttng_ht_iter_get_node_ulong(&iter
);
421 socket
= caa_container_of(node
, struct consumer_socket
, node
);
428 * Allocate a new consumer_socket and return the pointer.
430 struct consumer_socket
*consumer_allocate_socket(int *fd
)
432 struct consumer_socket
*socket
= NULL
;
436 socket
= zmalloc(sizeof(struct consumer_socket
));
437 if (socket
== NULL
) {
438 PERROR("zmalloc consumer socket");
443 lttng_ht_node_init_ulong(&socket
->node
, *fd
);
450 * Add consumer socket to consumer output object. Read side lock must be
451 * acquired before calling this function.
453 void consumer_add_socket(struct consumer_socket
*sock
,
454 struct consumer_output
*consumer
)
459 lttng_ht_add_unique_ulong(consumer
->socks
, &sock
->node
);
463 * Delete consumer socket to consumer output object. Read side lock must be
464 * acquired before calling this function.
466 void consumer_del_socket(struct consumer_socket
*sock
,
467 struct consumer_output
*consumer
)
470 struct lttng_ht_iter iter
;
475 iter
.iter
.node
= &sock
->node
.node
;
476 ret
= lttng_ht_del(consumer
->socks
, &iter
);
481 * RCU destroy call function.
483 static void destroy_socket_rcu(struct rcu_head
*head
)
485 struct lttng_ht_node_ulong
*node
=
486 caa_container_of(head
, struct lttng_ht_node_ulong
, head
);
487 struct consumer_socket
*socket
=
488 caa_container_of(node
, struct consumer_socket
, node
);
494 * Destroy and free socket pointer in a call RCU. Read side lock must be
495 * acquired before calling this function.
497 void consumer_destroy_socket(struct consumer_socket
*sock
)
502 * We DO NOT close the file descriptor here since it is global to the
503 * session daemon and is closed only if the consumer dies or a custom
504 * consumer was registered,
506 if (sock
->registered
) {
507 DBG3("Consumer socket was registered. Closing fd %d", *sock
->fd_ptr
);
508 lttcomm_close_unix_sock(*sock
->fd_ptr
);
511 call_rcu(&sock
->node
.head
, destroy_socket_rcu
);
515 * Allocate and assign data to a consumer_output object.
517 * Return pointer to structure.
519 struct consumer_output
*consumer_create_output(enum consumer_dst_type type
)
521 struct consumer_output
*output
= NULL
;
523 output
= zmalloc(sizeof(struct consumer_output
));
524 if (output
== NULL
) {
525 PERROR("zmalloc consumer_output");
529 /* By default, consumer output is enabled */
532 output
->net_seq_index
= (uint64_t) -1ULL;
533 urcu_ref_init(&output
->ref
);
535 output
->socks
= lttng_ht_new(0, LTTNG_HT_TYPE_ULONG
);
542 * Iterate over the consumer output socket hash table and destroy them. The
543 * socket file descriptor are only closed if the consumer output was
544 * registered meaning it's an external consumer.
546 void consumer_destroy_output_sockets(struct consumer_output
*obj
)
548 struct lttng_ht_iter iter
;
549 struct consumer_socket
*socket
;
556 cds_lfht_for_each_entry(obj
->socks
->ht
, &iter
.iter
, socket
, node
.node
) {
557 consumer_del_socket(socket
, obj
);
558 consumer_destroy_socket(socket
);
564 * Delete the consumer_output object from the list and free the ptr.
566 * Should *NOT* be called with RCU read-side lock held.
568 static void consumer_release_output(struct urcu_ref
*ref
)
570 struct consumer_output
*obj
=
571 caa_container_of(ref
, struct consumer_output
, ref
);
573 consumer_destroy_output_sockets(obj
);
576 /* Finally destroy HT */
577 ht_cleanup_push(obj
->socks
);
584 * Get the consumer_output object.
586 void consumer_output_get(struct consumer_output
*obj
)
588 urcu_ref_get(&obj
->ref
);
592 * Put the consumer_output object.
594 * Should *NOT* be called with RCU read-side lock held.
596 void consumer_output_put(struct consumer_output
*obj
)
601 urcu_ref_put(&obj
->ref
, consumer_release_output
);
605 * Copy consumer output and returned the newly allocated copy.
607 * Should *NOT* be called with RCU read-side lock held.
609 struct consumer_output
*consumer_copy_output(struct consumer_output
*src
)
612 struct consumer_output
*output
;
616 output
= consumer_create_output(src
->type
);
617 if (output
== NULL
) {
620 output
->enabled
= src
->enabled
;
621 output
->net_seq_index
= src
->net_seq_index
;
622 memcpy(output
->domain_subdir
, src
->domain_subdir
,
623 sizeof(output
->domain_subdir
));
624 output
->snapshot
= src
->snapshot
;
625 output
->relay_major_version
= src
->relay_major_version
;
626 output
->relay_minor_version
= src
->relay_minor_version
;
627 memcpy(&output
->dst
, &src
->dst
, sizeof(output
->dst
));
628 ret
= consumer_copy_sockets(output
, src
);
636 consumer_output_put(output
);
641 * Copy consumer sockets from src to dst.
643 * Return 0 on success or else a negative value.
645 int consumer_copy_sockets(struct consumer_output
*dst
,
646 struct consumer_output
*src
)
649 struct lttng_ht_iter iter
;
650 struct consumer_socket
*socket
, *copy_sock
;
656 cds_lfht_for_each_entry(src
->socks
->ht
, &iter
.iter
, socket
, node
.node
) {
657 /* Ignore socket that are already there. */
658 copy_sock
= consumer_find_socket(*socket
->fd_ptr
, dst
);
663 /* Create new socket object. */
664 copy_sock
= consumer_allocate_socket(socket
->fd_ptr
);
665 if (copy_sock
== NULL
) {
671 copy_sock
->registered
= socket
->registered
;
673 * This is valid because this lock is shared accross all consumer
674 * object being the global lock of the consumer data structure of the
677 copy_sock
->lock
= socket
->lock
;
678 consumer_add_socket(copy_sock
, dst
);
687 * Set network URI to the consumer output.
689 * Return 0 on success. Return 1 if the URI were equal. Else, negative value on
692 int consumer_set_network_uri(const struct ltt_session
*session
,
693 struct consumer_output
*output
,
694 struct lttng_uri
*uri
)
697 struct lttng_uri
*dst_uri
= NULL
;
699 /* Code flow error safety net. */
703 switch (uri
->stype
) {
704 case LTTNG_STREAM_CONTROL
:
705 dst_uri
= &output
->dst
.net
.control
;
706 output
->dst
.net
.control_isset
= 1;
707 if (uri
->port
== 0) {
708 /* Assign default port. */
709 uri
->port
= DEFAULT_NETWORK_CONTROL_PORT
;
711 if (output
->dst
.net
.data_isset
&& uri
->port
==
712 output
->dst
.net
.data
.port
) {
713 ret
= -LTTNG_ERR_INVALID
;
717 DBG3("Consumer control URI set with port %d", uri
->port
);
719 case LTTNG_STREAM_DATA
:
720 dst_uri
= &output
->dst
.net
.data
;
721 output
->dst
.net
.data_isset
= 1;
722 if (uri
->port
== 0) {
723 /* Assign default port. */
724 uri
->port
= DEFAULT_NETWORK_DATA_PORT
;
726 if (output
->dst
.net
.control_isset
&& uri
->port
==
727 output
->dst
.net
.control
.port
) {
728 ret
= -LTTNG_ERR_INVALID
;
732 DBG3("Consumer data URI set with port %d", uri
->port
);
735 ERR("Set network uri type unknown %d", uri
->stype
);
736 ret
= -LTTNG_ERR_INVALID
;
740 ret
= uri_compare(dst_uri
, uri
);
742 /* Same URI, don't touch it and return success. */
743 DBG3("URI network compare are the same");
747 /* URIs were not equal, replacing it. */
748 memcpy(dst_uri
, uri
, sizeof(struct lttng_uri
));
749 output
->type
= CONSUMER_DST_NET
;
750 if (dst_uri
->stype
!= LTTNG_STREAM_CONTROL
) {
751 /* Only the control uri needs to contain the path. */
756 * If the user has specified a subdir as part of the control
757 * URL, the session's base output directory is:
758 * /RELAYD_OUTPUT_PATH/HOSTNAME/USER_SPECIFIED_DIR
760 * Hence, the "base_dir" from which all stream files and
761 * session rotation chunks are created takes the form
762 * /HOSTNAME/USER_SPECIFIED_DIR
764 * If the user has not specified an output directory as part of
765 * the control URL, the base output directory has the form:
766 * /RELAYD_OUTPUT_PATH/HOSTNAME/SESSION_NAME-CREATION_TIME
768 * Hence, the "base_dir" from which all stream files and
769 * session rotation chunks are created takes the form
770 * /HOSTNAME/SESSION_NAME-CREATION_TIME
772 * Note that automatically generated session names already
773 * contain the session's creation time. In that case, the
774 * creation time is omitted to prevent it from being duplicated
775 * in the final directory hierarchy.
778 if (strstr(uri
->subdir
, "../")) {
779 ERR("Network URI subdirs are not allowed to walk up the path hierarchy");
780 ret
= -LTTNG_ERR_INVALID
;
783 ret
= snprintf(output
->dst
.net
.base_dir
,
784 sizeof(output
->dst
.net
.base_dir
),
785 "/%s/%s/", session
->hostname
, uri
->subdir
);
787 if (session
->has_auto_generated_name
) {
788 ret
= snprintf(output
->dst
.net
.base_dir
,
789 sizeof(output
->dst
.net
.base_dir
),
790 "/%s/%s/", session
->hostname
,
793 char session_creation_datetime
[16];
797 timeinfo
= localtime(&session
->creation_time
);
799 ret
= -LTTNG_ERR_FATAL
;
802 strftime_ret
= strftime(session_creation_datetime
,
803 sizeof(session_creation_datetime
),
804 "%Y%m%d-%H%M%S", timeinfo
);
805 if (strftime_ret
== 0) {
806 ERR("Failed to format session creation timestamp while setting network URI");
807 ret
= -LTTNG_ERR_FATAL
;
810 ret
= snprintf(output
->dst
.net
.base_dir
,
811 sizeof(output
->dst
.net
.base_dir
),
812 "/%s/%s-%s/", session
->hostname
,
814 session_creation_datetime
);
817 if (ret
>= sizeof(output
->dst
.net
.base_dir
)) {
818 ret
= -LTTNG_ERR_INVALID
;
819 ERR("Truncation occurred while setting network output base directory");
821 } else if (ret
== -1) {
822 ret
= -LTTNG_ERR_INVALID
;
823 PERROR("Error occurred while setting network output base directory");
827 DBG3("Consumer set network uri base_dir path %s",
828 output
->dst
.net
.base_dir
);
839 * Send file descriptor to consumer via sock.
841 * The consumer socket lock must be held by the caller.
843 int consumer_send_fds(struct consumer_socket
*sock
, const int *fds
,
851 assert(pthread_mutex_trylock(sock
->lock
) == EBUSY
);
853 ret
= lttcomm_send_fds_unix_sock(*sock
->fd_ptr
, fds
, nb_fd
);
855 /* The above call will print a PERROR on error. */
856 DBG("Error when sending consumer fds on sock %d", *sock
->fd_ptr
);
860 ret
= consumer_recv_status_reply(sock
);
866 * Consumer send communication message structure to consumer.
868 * The consumer socket lock must be held by the caller.
870 int consumer_send_msg(struct consumer_socket
*sock
,
871 struct lttcomm_consumer_msg
*msg
)
877 assert(pthread_mutex_trylock(sock
->lock
) == EBUSY
);
879 ret
= consumer_socket_send(sock
, msg
, sizeof(struct lttcomm_consumer_msg
));
884 ret
= consumer_recv_status_reply(sock
);
891 * Consumer send channel communication message structure to consumer.
893 * The consumer socket lock must be held by the caller.
895 int consumer_send_channel(struct consumer_socket
*sock
,
896 struct lttcomm_consumer_msg
*msg
)
903 ret
= consumer_send_msg(sock
, msg
);
913 * Populate the given consumer msg structure with the ask_channel command
916 void consumer_init_ask_channel_comm_msg(struct lttcomm_consumer_msg
*msg
,
917 uint64_t subbuf_size
,
920 unsigned int switch_timer_interval
,
921 unsigned int read_timer_interval
,
922 unsigned int live_timer_interval
,
923 unsigned int monitor_timer_interval
,
927 const char *pathname
,
933 uint64_t tracefile_size
,
934 uint64_t tracefile_count
,
935 uint64_t session_id_per_pid
,
936 unsigned int monitor
,
937 uint32_t ust_app_uid
,
938 int64_t blocking_timeout
,
939 const char *root_shm_path
,
940 const char *shm_path
,
941 struct lttng_trace_chunk
*trace_chunk
,
942 const struct lttng_credentials
*buffer_credentials
)
946 /* Zeroed structure */
947 memset(msg
, 0, sizeof(struct lttcomm_consumer_msg
));
948 msg
->u
.ask_channel
.buffer_credentials
.uid
= UINT32_MAX
;
949 msg
->u
.ask_channel
.buffer_credentials
.gid
= UINT32_MAX
;
953 enum lttng_trace_chunk_status chunk_status
;
955 chunk_status
= lttng_trace_chunk_get_id(trace_chunk
, &chunk_id
);
956 assert(chunk_status
== LTTNG_TRACE_CHUNK_STATUS_OK
);
957 LTTNG_OPTIONAL_SET(&msg
->u
.ask_channel
.chunk_id
, chunk_id
);
959 msg
->u
.ask_channel
.buffer_credentials
.uid
= buffer_credentials
->uid
;
960 msg
->u
.ask_channel
.buffer_credentials
.gid
= buffer_credentials
->gid
;
962 msg
->cmd_type
= LTTNG_CONSUMER_ASK_CHANNEL_CREATION
;
963 msg
->u
.ask_channel
.subbuf_size
= subbuf_size
;
964 msg
->u
.ask_channel
.num_subbuf
= num_subbuf
;
965 msg
->u
.ask_channel
.overwrite
= overwrite
;
966 msg
->u
.ask_channel
.switch_timer_interval
= switch_timer_interval
;
967 msg
->u
.ask_channel
.read_timer_interval
= read_timer_interval
;
968 msg
->u
.ask_channel
.live_timer_interval
= live_timer_interval
;
969 msg
->u
.ask_channel
.monitor_timer_interval
= monitor_timer_interval
;
970 msg
->u
.ask_channel
.output
= output
;
971 msg
->u
.ask_channel
.type
= type
;
972 msg
->u
.ask_channel
.session_id
= session_id
;
973 msg
->u
.ask_channel
.session_id_per_pid
= session_id_per_pid
;
974 msg
->u
.ask_channel
.relayd_id
= relayd_id
;
975 msg
->u
.ask_channel
.key
= key
;
976 msg
->u
.ask_channel
.chan_id
= chan_id
;
977 msg
->u
.ask_channel
.tracefile_size
= tracefile_size
;
978 msg
->u
.ask_channel
.tracefile_count
= tracefile_count
;
979 msg
->u
.ask_channel
.monitor
= monitor
;
980 msg
->u
.ask_channel
.ust_app_uid
= ust_app_uid
;
981 msg
->u
.ask_channel
.blocking_timeout
= blocking_timeout
;
983 memcpy(msg
->u
.ask_channel
.uuid
, uuid
, sizeof(msg
->u
.ask_channel
.uuid
));
986 strncpy(msg
->u
.ask_channel
.pathname
, pathname
,
987 sizeof(msg
->u
.ask_channel
.pathname
));
988 msg
->u
.ask_channel
.pathname
[sizeof(msg
->u
.ask_channel
.pathname
)-1] = '\0';
991 strncpy(msg
->u
.ask_channel
.name
, name
, sizeof(msg
->u
.ask_channel
.name
));
992 msg
->u
.ask_channel
.name
[sizeof(msg
->u
.ask_channel
.name
) - 1] = '\0';
995 strncpy(msg
->u
.ask_channel
.root_shm_path
, root_shm_path
,
996 sizeof(msg
->u
.ask_channel
.root_shm_path
));
997 msg
->u
.ask_channel
.root_shm_path
[sizeof(msg
->u
.ask_channel
.root_shm_path
) - 1] = '\0';
1000 strncpy(msg
->u
.ask_channel
.shm_path
, shm_path
,
1001 sizeof(msg
->u
.ask_channel
.shm_path
));
1002 msg
->u
.ask_channel
.shm_path
[sizeof(msg
->u
.ask_channel
.shm_path
) - 1] = '\0';
1007 * Init channel communication message structure.
1009 void consumer_init_add_channel_comm_msg(struct lttcomm_consumer_msg
*msg
,
1010 uint64_t channel_key
,
1011 uint64_t session_id
,
1012 const char *pathname
,
1017 unsigned int nb_init_streams
,
1018 enum lttng_event_output output
,
1020 uint64_t tracefile_size
,
1021 uint64_t tracefile_count
,
1022 unsigned int monitor
,
1023 unsigned int live_timer_interval
,
1024 unsigned int monitor_timer_interval
,
1025 struct lttng_trace_chunk
*trace_chunk
)
1029 /* Zeroed structure */
1030 memset(msg
, 0, sizeof(struct lttcomm_consumer_msg
));
1034 enum lttng_trace_chunk_status chunk_status
;
1036 chunk_status
= lttng_trace_chunk_get_id(trace_chunk
, &chunk_id
);
1037 assert(chunk_status
== LTTNG_TRACE_CHUNK_STATUS_OK
);
1038 LTTNG_OPTIONAL_SET(&msg
->u
.channel
.chunk_id
, chunk_id
);
1042 msg
->cmd_type
= LTTNG_CONSUMER_ADD_CHANNEL
;
1043 msg
->u
.channel
.channel_key
= channel_key
;
1044 msg
->u
.channel
.session_id
= session_id
;
1045 msg
->u
.channel
.relayd_id
= relayd_id
;
1046 msg
->u
.channel
.nb_init_streams
= nb_init_streams
;
1047 msg
->u
.channel
.output
= output
;
1048 msg
->u
.channel
.type
= type
;
1049 msg
->u
.channel
.tracefile_size
= tracefile_size
;
1050 msg
->u
.channel
.tracefile_count
= tracefile_count
;
1051 msg
->u
.channel
.monitor
= monitor
;
1052 msg
->u
.channel
.live_timer_interval
= live_timer_interval
;
1053 msg
->u
.channel
.monitor_timer_interval
= monitor_timer_interval
;
1055 strncpy(msg
->u
.channel
.pathname
, pathname
,
1056 sizeof(msg
->u
.channel
.pathname
));
1057 msg
->u
.channel
.pathname
[sizeof(msg
->u
.channel
.pathname
) - 1] = '\0';
1059 strncpy(msg
->u
.channel
.name
, name
, sizeof(msg
->u
.channel
.name
));
1060 msg
->u
.channel
.name
[sizeof(msg
->u
.channel
.name
) - 1] = '\0';
1064 * Init stream communication message structure.
1066 void consumer_init_add_stream_comm_msg(struct lttcomm_consumer_msg
*msg
,
1067 uint64_t channel_key
,
1068 uint64_t stream_key
,
1073 memset(msg
, 0, sizeof(struct lttcomm_consumer_msg
));
1075 msg
->cmd_type
= LTTNG_CONSUMER_ADD_STREAM
;
1076 msg
->u
.stream
.channel_key
= channel_key
;
1077 msg
->u
.stream
.stream_key
= stream_key
;
1078 msg
->u
.stream
.cpu
= cpu
;
1081 void consumer_init_streams_sent_comm_msg(struct lttcomm_consumer_msg
*msg
,
1082 enum lttng_consumer_command cmd
,
1083 uint64_t channel_key
, uint64_t net_seq_idx
)
1087 memset(msg
, 0, sizeof(struct lttcomm_consumer_msg
));
1089 msg
->cmd_type
= cmd
;
1090 msg
->u
.sent_streams
.channel_key
= channel_key
;
1091 msg
->u
.sent_streams
.net_seq_idx
= net_seq_idx
;
1095 * Send stream communication structure to the consumer.
1097 int consumer_send_stream(struct consumer_socket
*sock
,
1098 struct consumer_output
*dst
, struct lttcomm_consumer_msg
*msg
,
1099 const int *fds
, size_t nb_fd
)
1108 ret
= consumer_send_msg(sock
, msg
);
1113 ret
= consumer_send_fds(sock
, fds
, nb_fd
);
1123 * Send relayd socket to consumer associated with a session name.
1125 * The consumer socket lock must be held by the caller.
1127 * On success return positive value. On error, negative value.
1129 int consumer_send_relayd_socket(struct consumer_socket
*consumer_sock
,
1130 struct lttcomm_relayd_sock
*rsock
, struct consumer_output
*consumer
,
1131 enum lttng_stream_type type
, uint64_t session_id
,
1132 const char *session_name
, const char *hostname
,
1133 const char *base_path
, int session_live_timer
,
1134 const uint64_t *current_chunk_id
, time_t session_creation_time
,
1135 bool session_name_contains_creation_time
)
1138 struct lttcomm_consumer_msg msg
;
1140 /* Code flow error. Safety net. */
1143 assert(consumer_sock
);
1145 memset(&msg
, 0, sizeof(msg
));
1146 /* Bail out if consumer is disabled */
1147 if (!consumer
->enabled
) {
1152 if (type
== LTTNG_STREAM_CONTROL
) {
1153 char output_path
[LTTNG_PATH_MAX
] = {};
1155 ret
= relayd_create_session(rsock
,
1156 &msg
.u
.relayd_sock
.relayd_session_id
,
1157 session_name
, hostname
, base_path
,
1159 consumer
->snapshot
, session_id
,
1160 sessiond_uuid
, current_chunk_id
,
1161 session_creation_time
,
1162 session_name_contains_creation_time
,
1165 /* Close the control socket. */
1166 (void) relayd_close(rsock
);
1169 DBG("Created session on relay, output path reply: %s",
1173 msg
.cmd_type
= LTTNG_CONSUMER_ADD_RELAYD_SOCKET
;
1175 * Assign network consumer output index using the temporary consumer since
1176 * this call should only be made from within a set_consumer_uri() function
1177 * call in the session daemon.
1179 msg
.u
.relayd_sock
.net_index
= consumer
->net_seq_index
;
1180 msg
.u
.relayd_sock
.type
= type
;
1181 msg
.u
.relayd_sock
.session_id
= session_id
;
1182 memcpy(&msg
.u
.relayd_sock
.sock
, rsock
, sizeof(msg
.u
.relayd_sock
.sock
));
1184 DBG3("Sending relayd sock info to consumer on %d", *consumer_sock
->fd_ptr
);
1185 ret
= consumer_send_msg(consumer_sock
, &msg
);
1190 DBG3("Sending relayd socket file descriptor to consumer");
1191 ret
= consumer_send_fds(consumer_sock
, &rsock
->sock
.fd
, 1);
1196 DBG2("Consumer relayd socket sent");
1203 int consumer_send_pipe(struct consumer_socket
*consumer_sock
,
1204 enum lttng_consumer_command cmd
, int pipe
)
1207 struct lttcomm_consumer_msg msg
;
1208 const char *pipe_name
;
1209 const char *command_name
;
1212 case LTTNG_CONSUMER_SET_CHANNEL_MONITOR_PIPE
:
1213 pipe_name
= "channel monitor";
1214 command_name
= "SET_CHANNEL_MONITOR_PIPE";
1217 ERR("Unexpected command received in %s (cmd = %d)", __func__
,
1222 /* Code flow error. Safety net. */
1224 memset(&msg
, 0, sizeof(msg
));
1227 pthread_mutex_lock(consumer_sock
->lock
);
1228 DBG3("Sending %s command to consumer", command_name
);
1229 ret
= consumer_send_msg(consumer_sock
, &msg
);
1234 DBG3("Sending %s pipe %d to consumer on socket %d",
1236 pipe
, *consumer_sock
->fd_ptr
);
1237 ret
= consumer_send_fds(consumer_sock
, &pipe
, 1);
1242 DBG2("%s pipe successfully sent", pipe_name
);
1244 pthread_mutex_unlock(consumer_sock
->lock
);
1248 int consumer_send_channel_monitor_pipe(struct consumer_socket
*consumer_sock
,
1251 return consumer_send_pipe(consumer_sock
,
1252 LTTNG_CONSUMER_SET_CHANNEL_MONITOR_PIPE
, pipe
);
1256 * Ask the consumer if the data is pending for the specific session id.
1257 * Returns 1 if data is pending, 0 otherwise, or < 0 on error.
1259 int consumer_is_data_pending(uint64_t session_id
,
1260 struct consumer_output
*consumer
)
1263 int32_t ret_code
= 0; /* Default is that the data is NOT pending */
1264 struct consumer_socket
*socket
;
1265 struct lttng_ht_iter iter
;
1266 struct lttcomm_consumer_msg msg
;
1270 DBG3("Consumer data pending for id %" PRIu64
, session_id
);
1272 memset(&msg
, 0, sizeof(msg
));
1273 msg
.cmd_type
= LTTNG_CONSUMER_DATA_PENDING
;
1274 msg
.u
.data_pending
.session_id
= session_id
;
1276 /* Send command for each consumer */
1278 cds_lfht_for_each_entry(consumer
->socks
->ht
, &iter
.iter
, socket
,
1280 pthread_mutex_lock(socket
->lock
);
1281 ret
= consumer_socket_send(socket
, &msg
, sizeof(msg
));
1283 pthread_mutex_unlock(socket
->lock
);
1288 * No need for a recv reply status because the answer to the command is
1289 * the reply status message.
1292 ret
= consumer_socket_recv(socket
, &ret_code
, sizeof(ret_code
));
1294 pthread_mutex_unlock(socket
->lock
);
1297 pthread_mutex_unlock(socket
->lock
);
1299 if (ret_code
== 1) {
1305 DBG("Consumer data is %s pending for session id %" PRIu64
,
1306 ret_code
== 1 ? "" : "NOT", session_id
);
1315 * Send a flush command to consumer using the given channel key.
1317 * Return 0 on success else a negative value.
1319 int consumer_flush_channel(struct consumer_socket
*socket
, uint64_t key
)
1322 struct lttcomm_consumer_msg msg
;
1326 DBG2("Consumer flush channel key %" PRIu64
, key
);
1328 memset(&msg
, 0, sizeof(msg
));
1329 msg
.cmd_type
= LTTNG_CONSUMER_FLUSH_CHANNEL
;
1330 msg
.u
.flush_channel
.key
= key
;
1332 pthread_mutex_lock(socket
->lock
);
1333 health_code_update();
1335 ret
= consumer_send_msg(socket
, &msg
);
1341 health_code_update();
1342 pthread_mutex_unlock(socket
->lock
);
1347 * Send a clear quiescent command to consumer using the given channel key.
1349 * Return 0 on success else a negative value.
1351 int consumer_clear_quiescent_channel(struct consumer_socket
*socket
, uint64_t key
)
1354 struct lttcomm_consumer_msg msg
;
1358 DBG2("Consumer clear quiescent channel key %" PRIu64
, key
);
1360 memset(&msg
, 0, sizeof(msg
));
1361 msg
.cmd_type
= LTTNG_CONSUMER_CLEAR_QUIESCENT_CHANNEL
;
1362 msg
.u
.clear_quiescent_channel
.key
= key
;
1364 pthread_mutex_lock(socket
->lock
);
1365 health_code_update();
1367 ret
= consumer_send_msg(socket
, &msg
);
1373 health_code_update();
1374 pthread_mutex_unlock(socket
->lock
);
1379 * Send a close metadata command to consumer using the given channel key.
1380 * Called with registry lock held.
1382 * Return 0 on success else a negative value.
1384 int consumer_close_metadata(struct consumer_socket
*socket
,
1385 uint64_t metadata_key
)
1388 struct lttcomm_consumer_msg msg
;
1392 DBG2("Consumer close metadata channel key %" PRIu64
, metadata_key
);
1394 memset(&msg
, 0, sizeof(msg
));
1395 msg
.cmd_type
= LTTNG_CONSUMER_CLOSE_METADATA
;
1396 msg
.u
.close_metadata
.key
= metadata_key
;
1398 pthread_mutex_lock(socket
->lock
);
1399 health_code_update();
1401 ret
= consumer_send_msg(socket
, &msg
);
1407 health_code_update();
1408 pthread_mutex_unlock(socket
->lock
);
1413 * Send a setup metdata command to consumer using the given channel key.
1415 * Return 0 on success else a negative value.
1417 int consumer_setup_metadata(struct consumer_socket
*socket
,
1418 uint64_t metadata_key
)
1421 struct lttcomm_consumer_msg msg
;
1425 DBG2("Consumer setup metadata channel key %" PRIu64
, metadata_key
);
1427 memset(&msg
, 0, sizeof(msg
));
1428 msg
.cmd_type
= LTTNG_CONSUMER_SETUP_METADATA
;
1429 msg
.u
.setup_metadata
.key
= metadata_key
;
1431 pthread_mutex_lock(socket
->lock
);
1432 health_code_update();
1434 ret
= consumer_send_msg(socket
, &msg
);
1440 health_code_update();
1441 pthread_mutex_unlock(socket
->lock
);
1446 * Send metadata string to consumer.
1447 * RCU read-side lock must be held to guarantee existence of socket.
1449 * Return 0 on success else a negative value.
1451 int consumer_push_metadata(struct consumer_socket
*socket
,
1452 uint64_t metadata_key
, char *metadata_str
, size_t len
,
1453 size_t target_offset
, uint64_t version
)
1456 struct lttcomm_consumer_msg msg
;
1460 DBG2("Consumer push metadata to consumer socket %d", *socket
->fd_ptr
);
1462 pthread_mutex_lock(socket
->lock
);
1464 memset(&msg
, 0, sizeof(msg
));
1465 msg
.cmd_type
= LTTNG_CONSUMER_PUSH_METADATA
;
1466 msg
.u
.push_metadata
.key
= metadata_key
;
1467 msg
.u
.push_metadata
.target_offset
= target_offset
;
1468 msg
.u
.push_metadata
.len
= len
;
1469 msg
.u
.push_metadata
.version
= version
;
1471 health_code_update();
1472 ret
= consumer_send_msg(socket
, &msg
);
1473 if (ret
< 0 || len
== 0) {
1477 DBG3("Consumer pushing metadata on sock %d of len %zu", *socket
->fd_ptr
,
1480 ret
= consumer_socket_send(socket
, metadata_str
, len
);
1485 health_code_update();
1486 ret
= consumer_recv_status_reply(socket
);
1492 pthread_mutex_unlock(socket
->lock
);
1493 health_code_update();
1498 * Ask the consumer to snapshot a specific channel using the key.
1500 * Returns LTTNG_OK on success or else an LTTng error code.
1502 enum lttng_error_code
consumer_snapshot_channel(struct consumer_socket
*socket
,
1503 uint64_t key
, const struct consumer_output
*output
, int metadata
,
1504 uid_t uid
, gid_t gid
, const char *channel_path
, int wait
,
1505 uint64_t nb_packets_per_stream
)
1508 enum lttng_error_code status
= LTTNG_OK
;
1509 struct lttcomm_consumer_msg msg
;
1514 DBG("Consumer snapshot channel key %" PRIu64
, key
);
1516 memset(&msg
, 0, sizeof(msg
));
1517 msg
.cmd_type
= LTTNG_CONSUMER_SNAPSHOT_CHANNEL
;
1518 msg
.u
.snapshot_channel
.key
= key
;
1519 msg
.u
.snapshot_channel
.nb_packets_per_stream
= nb_packets_per_stream
;
1520 msg
.u
.snapshot_channel
.metadata
= metadata
;
1522 if (output
->type
== CONSUMER_DST_NET
) {
1523 msg
.u
.snapshot_channel
.relayd_id
=
1524 output
->net_seq_index
;
1525 msg
.u
.snapshot_channel
.use_relayd
= 1;
1527 msg
.u
.snapshot_channel
.relayd_id
= (uint64_t) -1ULL;
1529 ret
= lttng_strncpy(msg
.u
.snapshot_channel
.pathname
,
1531 sizeof(msg
.u
.snapshot_channel
.pathname
));
1533 ERR("Snapshot path exceeds the maximal allowed length of %zu bytes (%zu bytes required) with path \"%s\"",
1534 sizeof(msg
.u
.snapshot_channel
.pathname
),
1535 strlen(channel_path
),
1537 status
= LTTNG_ERR_SNAPSHOT_FAIL
;
1541 health_code_update();
1542 pthread_mutex_lock(socket
->lock
);
1543 ret
= consumer_send_msg(socket
, &msg
);
1544 pthread_mutex_unlock(socket
->lock
);
1547 case LTTCOMM_CONSUMERD_CHAN_NOT_FOUND
:
1548 status
= LTTNG_ERR_CHAN_NOT_FOUND
;
1551 status
= LTTNG_ERR_SNAPSHOT_FAIL
;
1558 health_code_update();
1563 * Ask the consumer the number of discarded events for a channel.
1565 int consumer_get_discarded_events(uint64_t session_id
, uint64_t channel_key
,
1566 struct consumer_output
*consumer
, uint64_t *discarded
)
1569 struct consumer_socket
*socket
;
1570 struct lttng_ht_iter iter
;
1571 struct lttcomm_consumer_msg msg
;
1575 DBG3("Consumer discarded events id %" PRIu64
, session_id
);
1577 memset(&msg
, 0, sizeof(msg
));
1578 msg
.cmd_type
= LTTNG_CONSUMER_DISCARDED_EVENTS
;
1579 msg
.u
.discarded_events
.session_id
= session_id
;
1580 msg
.u
.discarded_events
.channel_key
= channel_key
;
1584 /* Send command for each consumer */
1586 cds_lfht_for_each_entry(consumer
->socks
->ht
, &iter
.iter
, socket
,
1588 uint64_t consumer_discarded
= 0;
1589 pthread_mutex_lock(socket
->lock
);
1590 ret
= consumer_socket_send(socket
, &msg
, sizeof(msg
));
1592 pthread_mutex_unlock(socket
->lock
);
1597 * No need for a recv reply status because the answer to the
1598 * command is the reply status message.
1600 ret
= consumer_socket_recv(socket
, &consumer_discarded
,
1601 sizeof(consumer_discarded
));
1603 ERR("get discarded events");
1604 pthread_mutex_unlock(socket
->lock
);
1607 pthread_mutex_unlock(socket
->lock
);
1608 *discarded
+= consumer_discarded
;
1611 DBG("Consumer discarded %" PRIu64
" events in session id %" PRIu64
,
1612 *discarded
, session_id
);
1620 * Ask the consumer the number of lost packets for a channel.
1622 int consumer_get_lost_packets(uint64_t session_id
, uint64_t channel_key
,
1623 struct consumer_output
*consumer
, uint64_t *lost
)
1626 struct consumer_socket
*socket
;
1627 struct lttng_ht_iter iter
;
1628 struct lttcomm_consumer_msg msg
;
1632 DBG3("Consumer lost packets id %" PRIu64
, session_id
);
1634 memset(&msg
, 0, sizeof(msg
));
1635 msg
.cmd_type
= LTTNG_CONSUMER_LOST_PACKETS
;
1636 msg
.u
.lost_packets
.session_id
= session_id
;
1637 msg
.u
.lost_packets
.channel_key
= channel_key
;
1641 /* Send command for each consumer */
1643 cds_lfht_for_each_entry(consumer
->socks
->ht
, &iter
.iter
, socket
,
1645 uint64_t consumer_lost
= 0;
1646 pthread_mutex_lock(socket
->lock
);
1647 ret
= consumer_socket_send(socket
, &msg
, sizeof(msg
));
1649 pthread_mutex_unlock(socket
->lock
);
1654 * No need for a recv reply status because the answer to the
1655 * command is the reply status message.
1657 ret
= consumer_socket_recv(socket
, &consumer_lost
,
1658 sizeof(consumer_lost
));
1660 ERR("get lost packets");
1661 pthread_mutex_unlock(socket
->lock
);
1664 pthread_mutex_unlock(socket
->lock
);
1665 *lost
+= consumer_lost
;
1668 DBG("Consumer lost %" PRIu64
" packets in session id %" PRIu64
,
1677 * Ask the consumer to rotate a channel.
1679 * The new_chunk_id is the session->rotate_count that has been incremented
1680 * when the rotation started. On the relay, this allows to keep track in which
1681 * chunk each stream is currently writing to (for the rotate_pending operation).
1683 int consumer_rotate_channel(struct consumer_socket
*socket
, uint64_t key
,
1684 uid_t uid
, gid_t gid
, struct consumer_output
*output
,
1685 bool is_metadata_channel
)
1688 struct lttcomm_consumer_msg msg
;
1692 DBG("Consumer rotate channel key %" PRIu64
, key
);
1694 pthread_mutex_lock(socket
->lock
);
1695 memset(&msg
, 0, sizeof(msg
));
1696 msg
.cmd_type
= LTTNG_CONSUMER_ROTATE_CHANNEL
;
1697 msg
.u
.rotate_channel
.key
= key
;
1698 msg
.u
.rotate_channel
.metadata
= !!is_metadata_channel
;
1700 if (output
->type
== CONSUMER_DST_NET
) {
1701 msg
.u
.rotate_channel
.relayd_id
= output
->net_seq_index
;
1703 msg
.u
.rotate_channel
.relayd_id
= (uint64_t) -1ULL;
1706 health_code_update();
1707 ret
= consumer_send_msg(socket
, &msg
);
1710 case LTTCOMM_CONSUMERD_CHAN_NOT_FOUND
:
1711 ret
= -LTTNG_ERR_CHAN_NOT_FOUND
;
1714 ret
= -LTTNG_ERR_ROTATION_FAIL_CONSUMER
;
1720 pthread_mutex_unlock(socket
->lock
);
1721 health_code_update();
1725 int consumer_init(struct consumer_socket
*socket
,
1726 const lttng_uuid sessiond_uuid
)
1729 struct lttcomm_consumer_msg msg
= {
1730 .cmd_type
= LTTNG_CONSUMER_INIT
,
1735 DBG("Sending consumer initialization command");
1736 lttng_uuid_copy(msg
.u
.init
.sessiond_uuid
, sessiond_uuid
);
1738 health_code_update();
1739 ret
= consumer_send_msg(socket
, &msg
);
1745 health_code_update();
1750 * Ask the consumer to create a new chunk for a given session.
1752 * Called with the consumer socket lock held.
1754 int consumer_create_trace_chunk(struct consumer_socket
*socket
,
1755 uint64_t relayd_id
, uint64_t session_id
,
1756 struct lttng_trace_chunk
*chunk
)
1759 enum lttng_trace_chunk_status chunk_status
;
1760 struct lttng_credentials chunk_credentials
;
1761 const struct lttng_directory_handle
*chunk_directory_handle
;
1763 const char *chunk_name
;
1764 bool chunk_name_overridden
;
1766 time_t creation_timestamp
;
1767 char creation_timestamp_buffer
[ISO8601_STR_LEN
];
1768 const char *creation_timestamp_str
= "(none)";
1769 const bool chunk_has_local_output
= relayd_id
== -1ULL;
1770 struct lttcomm_consumer_msg msg
= {
1771 .cmd_type
= LTTNG_CONSUMER_CREATE_TRACE_CHUNK
,
1772 .u
.create_trace_chunk
.session_id
= session_id
,
1778 if (relayd_id
!= -1ULL) {
1779 LTTNG_OPTIONAL_SET(&msg
.u
.create_trace_chunk
.relayd_id
,
1783 chunk_status
= lttng_trace_chunk_get_name(chunk
, &chunk_name
,
1784 &chunk_name_overridden
);
1785 if (chunk_status
!= LTTNG_TRACE_CHUNK_STATUS_OK
&&
1786 chunk_status
!= LTTNG_TRACE_CHUNK_STATUS_NONE
) {
1787 ERR("Failed to get name of trace chunk");
1788 ret
= -LTTNG_ERR_FATAL
;
1791 if (chunk_name_overridden
) {
1792 ret
= lttng_strncpy(msg
.u
.create_trace_chunk
.override_name
,
1794 sizeof(msg
.u
.create_trace_chunk
.override_name
));
1796 ERR("Trace chunk name \"%s\" exceeds the maximal length allowed by the consumer protocol",
1798 ret
= -LTTNG_ERR_FATAL
;
1803 chunk_status
= lttng_trace_chunk_get_creation_timestamp(chunk
,
1804 &creation_timestamp
);
1805 if (chunk_status
!= LTTNG_TRACE_CHUNK_STATUS_OK
) {
1806 ret
= -LTTNG_ERR_FATAL
;
1809 msg
.u
.create_trace_chunk
.creation_timestamp
=
1810 (uint64_t) creation_timestamp
;
1811 /* Only used for logging purposes. */
1812 ret
= time_to_iso8601_str(creation_timestamp
,
1813 creation_timestamp_buffer
,
1814 sizeof(creation_timestamp_buffer
));
1815 creation_timestamp_str
= !ret
? creation_timestamp_buffer
:
1816 "(formatting error)";
1818 chunk_status
= lttng_trace_chunk_get_id(chunk
, &chunk_id
);
1819 if (chunk_status
!= LTTNG_TRACE_CHUNK_STATUS_OK
) {
1821 * Anonymous trace chunks should never be transmitted
1822 * to remote peers (consumerd and relayd). They are used
1823 * internally for backward-compatibility purposes.
1825 ret
= -LTTNG_ERR_FATAL
;
1828 msg
.u
.create_trace_chunk
.chunk_id
= chunk_id
;
1830 if (chunk_has_local_output
) {
1831 chunk_status
= lttng_trace_chunk_get_chunk_directory_handle(
1832 chunk
, &chunk_directory_handle
);
1833 if (chunk_status
!= LTTNG_TRACE_CHUNK_STATUS_OK
) {
1834 ret
= -LTTNG_ERR_FATAL
;
1839 * This will only compile on platforms that support
1840 * dirfd (POSIX.2008). This is fine as the session daemon
1841 * is only built for such platforms.
1843 * The ownership of the chunk directory handle's is maintained
1844 * by the trace chunk.
1846 chunk_dirfd
= lttng_directory_handle_get_dirfd(
1847 chunk_directory_handle
);
1848 assert(chunk_dirfd
>= 0);
1850 chunk_status
= lttng_trace_chunk_get_credentials(
1851 chunk
, &chunk_credentials
);
1852 if (chunk_status
!= LTTNG_TRACE_CHUNK_STATUS_OK
) {
1854 * Not associating credentials to a sessiond chunk is a
1855 * fatal internal error.
1857 ret
= -LTTNG_ERR_FATAL
;
1860 msg
.u
.create_trace_chunk
.credentials
.value
.uid
=
1861 chunk_credentials
.uid
;
1862 msg
.u
.create_trace_chunk
.credentials
.value
.gid
=
1863 chunk_credentials
.gid
;
1864 msg
.u
.create_trace_chunk
.credentials
.is_set
= 1;
1867 DBG("Sending consumer create trace chunk command: relayd_id = %" PRId64
1868 ", session_id = %" PRIu64
", chunk_id = %" PRIu64
1869 ", creation_timestamp = %s",
1870 relayd_id
, session_id
, chunk_id
,
1871 creation_timestamp_str
);
1872 health_code_update();
1873 ret
= consumer_send_msg(socket
, &msg
);
1874 health_code_update();
1876 ERR("Trace chunk creation error on consumer");
1877 ret
= -LTTNG_ERR_CREATE_TRACE_CHUNK_FAIL_CONSUMER
;
1881 if (chunk_has_local_output
) {
1882 DBG("Sending trace chunk directory fd to consumer");
1883 health_code_update();
1884 ret
= consumer_send_fds(socket
, &chunk_dirfd
, 1);
1885 health_code_update();
1887 ERR("Trace chunk creation error on consumer");
1888 ret
= -LTTNG_ERR_CREATE_TRACE_CHUNK_FAIL_CONSUMER
;
1897 * Ask the consumer to close a trace chunk for a given session.
1899 * Called with the consumer socket lock held.
1901 int consumer_close_trace_chunk(struct consumer_socket
*socket
,
1902 uint64_t relayd_id
, uint64_t session_id
,
1903 struct lttng_trace_chunk
*chunk
,
1904 char *closed_trace_chunk_path
)
1907 enum lttng_trace_chunk_status chunk_status
;
1908 struct lttcomm_consumer_msg msg
= {
1909 .cmd_type
= LTTNG_CONSUMER_CLOSE_TRACE_CHUNK
,
1910 .u
.close_trace_chunk
.session_id
= session_id
,
1912 struct lttcomm_consumer_close_trace_chunk_reply reply
;
1914 time_t close_timestamp
;
1915 enum lttng_trace_chunk_command_type close_command
;
1916 const char *close_command_name
= "none";
1917 struct lttng_dynamic_buffer path_reception_buffer
;
1920 lttng_dynamic_buffer_init(&path_reception_buffer
);
1922 if (relayd_id
!= -1ULL) {
1924 &msg
.u
.close_trace_chunk
.relayd_id
, relayd_id
);
1927 chunk_status
= lttng_trace_chunk_get_close_command(
1928 chunk
, &close_command
);
1929 switch (chunk_status
) {
1930 case LTTNG_TRACE_CHUNK_STATUS_OK
:
1931 LTTNG_OPTIONAL_SET(&msg
.u
.close_trace_chunk
.close_command
,
1932 (uint32_t) close_command
);
1934 case LTTNG_TRACE_CHUNK_STATUS_NONE
:
1937 ERR("Failed to get trace chunk close command");
1942 chunk_status
= lttng_trace_chunk_get_id(chunk
, &chunk_id
);
1944 * Anonymous trace chunks should never be transmitted to remote peers
1945 * (consumerd and relayd). They are used internally for
1946 * backward-compatibility purposes.
1948 assert(chunk_status
== LTTNG_TRACE_CHUNK_STATUS_OK
);
1949 msg
.u
.close_trace_chunk
.chunk_id
= chunk_id
;
1951 chunk_status
= lttng_trace_chunk_get_close_timestamp(chunk
,
1954 * A trace chunk should be closed locally before being closed remotely.
1955 * Otherwise, the close timestamp would never be transmitted to the
1958 assert(chunk_status
== LTTNG_TRACE_CHUNK_STATUS_OK
);
1959 msg
.u
.close_trace_chunk
.close_timestamp
= (uint64_t) close_timestamp
;
1961 if (msg
.u
.close_trace_chunk
.close_command
.is_set
) {
1962 close_command_name
= lttng_trace_chunk_command_type_get_name(
1965 DBG("Sending consumer close trace chunk command: relayd_id = %" PRId64
1966 ", session_id = %" PRIu64
", chunk_id = %" PRIu64
1967 ", close command = \"%s\"",
1968 relayd_id
, session_id
, chunk_id
, close_command_name
);
1970 health_code_update();
1971 ret
= consumer_socket_send(socket
, &msg
, sizeof(struct lttcomm_consumer_msg
));
1973 ret
= -LTTNG_ERR_CLOSE_TRACE_CHUNK_FAIL_CONSUMER
;
1976 ret
= consumer_socket_recv(socket
, &reply
, sizeof(reply
));
1978 ret
= -LTTNG_ERR_CLOSE_TRACE_CHUNK_FAIL_CONSUMER
;
1981 if (reply
.path_length
>= LTTNG_PATH_MAX
) {
1982 ERR("Invalid path returned by relay daemon: %" PRIu32
"bytes exceeds maximal allowed length of %d bytes",
1983 reply
.path_length
, LTTNG_PATH_MAX
);
1984 ret
= -LTTNG_ERR_INVALID_PROTOCOL
;
1987 ret
= lttng_dynamic_buffer_set_size(&path_reception_buffer
,
1990 ERR("Failed to allocate reception buffer of path returned by the \"close trace chunk\" command");
1991 ret
= -LTTNG_ERR_NOMEM
;
1994 ret
= consumer_socket_recv(socket
, path_reception_buffer
.data
,
1995 path_reception_buffer
.size
);
1997 ERR("Communication error while receiving path of closed trace chunk");
1998 ret
= -LTTNG_ERR_CLOSE_TRACE_CHUNK_FAIL_CONSUMER
;
2001 if (path_reception_buffer
.data
[path_reception_buffer
.size
- 1] != '\0') {
2002 ERR("Invalid path returned by relay daemon: not null-terminated");
2003 ret
= -LTTNG_ERR_INVALID_PROTOCOL
;
2006 if (closed_trace_chunk_path
) {
2008 * closed_trace_chunk_path is assumed to have a length >=
2011 memcpy(closed_trace_chunk_path
, path_reception_buffer
.data
,
2012 path_reception_buffer
.size
);
2015 lttng_dynamic_buffer_reset(&path_reception_buffer
);
2016 health_code_update();
2021 * Ask the consumer if a trace chunk exists.
2023 * Called with the consumer socket lock held.
2024 * Returns 0 on success, or a negative value on error.
2026 int consumer_trace_chunk_exists(struct consumer_socket
*socket
,
2027 uint64_t relayd_id
, uint64_t session_id
,
2028 struct lttng_trace_chunk
*chunk
,
2029 enum consumer_trace_chunk_exists_status
*result
)
2032 enum lttng_trace_chunk_status chunk_status
;
2033 struct lttcomm_consumer_msg msg
= {
2034 .cmd_type
= LTTNG_CONSUMER_TRACE_CHUNK_EXISTS
,
2035 .u
.trace_chunk_exists
.session_id
= session_id
,
2038 const char *consumer_reply_str
;
2042 if (relayd_id
!= -1ULL) {
2043 LTTNG_OPTIONAL_SET(&msg
.u
.trace_chunk_exists
.relayd_id
,
2047 chunk_status
= lttng_trace_chunk_get_id(chunk
, &chunk_id
);
2048 if (chunk_status
!= LTTNG_TRACE_CHUNK_STATUS_OK
) {
2050 * Anonymous trace chunks should never be transmitted
2051 * to remote peers (consumerd and relayd). They are used
2052 * internally for backward-compatibility purposes.
2054 ret
= -LTTNG_ERR_FATAL
;
2057 msg
.u
.trace_chunk_exists
.chunk_id
= chunk_id
;
2059 DBG("Sending consumer trace chunk exists command: relayd_id = %" PRId64
2060 ", session_id = %" PRIu64
2061 ", chunk_id = %" PRIu64
, relayd_id
, session_id
, chunk_id
);
2063 health_code_update();
2064 ret
= consumer_send_msg(socket
, &msg
);
2066 case LTTCOMM_CONSUMERD_UNKNOWN_TRACE_CHUNK
:
2067 consumer_reply_str
= "unknown trace chunk";
2068 *result
= CONSUMER_TRACE_CHUNK_EXISTS_STATUS_UNKNOWN_CHUNK
;
2070 case LTTCOMM_CONSUMERD_TRACE_CHUNK_EXISTS_LOCAL
:
2071 consumer_reply_str
= "trace chunk exists locally";
2072 *result
= CONSUMER_TRACE_CHUNK_EXISTS_STATUS_EXISTS_LOCAL
;
2074 case LTTCOMM_CONSUMERD_TRACE_CHUNK_EXISTS_REMOTE
:
2075 consumer_reply_str
= "trace chunk exists on remote peer";
2076 *result
= CONSUMER_TRACE_CHUNK_EXISTS_STATUS_EXISTS_REMOTE
;
2079 ERR("Consumer returned an error from TRACE_CHUNK_EXISTS command");
2083 DBG("Consumer reply to TRACE_CHUNK_EXISTS command: %s",
2084 consumer_reply_str
);
2087 health_code_update();