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");
98 * Send a data payload using a given consumer socket of size len.
100 * The consumer socket lock MUST be acquired before calling this since this
101 * function can change the fd value.
103 * Return 0 on success else a negative value on error.
105 int consumer_socket_send(struct consumer_socket
*socket
, void *msg
, size_t len
)
111 assert(socket
->fd_ptr
);
114 /* Consumer socket is invalid. Stopping. */
115 fd
= *socket
->fd_ptr
;
120 size
= lttcomm_send_unix_sock(fd
, msg
, len
);
122 /* The above call will print a PERROR on error. */
123 DBG("Error when sending data to consumer on sock %d", fd
);
125 * At this point, the socket is not usable anymore thus closing it and
126 * setting the file descriptor to -1 so it is not reused.
129 /* This call will PERROR on error. */
130 (void) lttcomm_close_unix_sock(fd
);
131 *socket
->fd_ptr
= -1;
142 * Receive a data payload using a given consumer socket of size len.
144 * The consumer socket lock MUST be acquired before calling this since this
145 * function can change the fd value.
147 * Return 0 on success else a negative value on error.
149 int consumer_socket_recv(struct consumer_socket
*socket
, void *msg
, size_t len
)
155 assert(socket
->fd_ptr
);
158 /* Consumer socket is invalid. Stopping. */
159 fd
= *socket
->fd_ptr
;
164 size
= lttcomm_recv_unix_sock(fd
, msg
, len
);
166 /* The above call will print a PERROR on error. */
167 DBG("Error when receiving data from the consumer socket %d", fd
);
169 * At this point, the socket is not usable anymore thus closing it and
170 * setting the file descriptor to -1 so it is not reused.
173 /* This call will PERROR on error. */
174 (void) lttcomm_close_unix_sock(fd
);
175 *socket
->fd_ptr
= -1;
186 * Receive a reply command status message from the consumer. Consumer socket
187 * lock MUST be acquired before calling this function.
189 * Return 0 on success, -1 on recv error or a negative lttng error code which
190 * was possibly returned by the consumer.
192 int consumer_recv_status_reply(struct consumer_socket
*sock
)
195 struct lttcomm_consumer_status_msg reply
;
199 ret
= consumer_socket_recv(sock
, &reply
, sizeof(reply
));
204 if (reply
.ret_code
== LTTCOMM_CONSUMERD_SUCCESS
) {
208 ret
= -reply
.ret_code
;
209 DBG("Consumer ret code %d", ret
);
217 * Once the ASK_CHANNEL command is sent to the consumer, the channel
218 * information are sent back. This call receives that data and populates key
221 * On success return 0 and both key and stream_count are set. On error, a
222 * negative value is sent back and both parameters are untouched.
224 int consumer_recv_status_channel(struct consumer_socket
*sock
,
225 uint64_t *key
, unsigned int *stream_count
)
228 struct lttcomm_consumer_status_channel reply
;
231 assert(stream_count
);
234 ret
= consumer_socket_recv(sock
, &reply
, sizeof(reply
));
239 /* An error is possible so don't touch the key and stream_count. */
240 if (reply
.ret_code
!= LTTCOMM_CONSUMERD_SUCCESS
) {
246 *stream_count
= reply
.stream_count
;
254 * Send destroy relayd command to consumer.
256 * On success return positive value. On error, negative value.
258 int consumer_send_destroy_relayd(struct consumer_socket
*sock
,
259 struct consumer_output
*consumer
)
262 struct lttcomm_consumer_msg msg
;
267 DBG2("Sending destroy relayd command to consumer sock %d", *sock
->fd_ptr
);
269 memset(&msg
, 0, sizeof(msg
));
270 msg
.cmd_type
= LTTNG_CONSUMER_DESTROY_RELAYD
;
271 msg
.u
.destroy_relayd
.net_seq_idx
= consumer
->net_seq_index
;
273 pthread_mutex_lock(sock
->lock
);
274 ret
= consumer_socket_send(sock
, &msg
, sizeof(msg
));
279 /* Don't check the return value. The caller will do it. */
280 ret
= consumer_recv_status_reply(sock
);
282 DBG2("Consumer send destroy relayd command done");
285 pthread_mutex_unlock(sock
->lock
);
290 * For each consumer socket in the consumer output object, send a destroy
293 void consumer_output_send_destroy_relayd(struct consumer_output
*consumer
)
295 struct lttng_ht_iter iter
;
296 struct consumer_socket
*socket
;
300 /* Destroy any relayd connection */
301 if (consumer
->type
== CONSUMER_DST_NET
) {
303 cds_lfht_for_each_entry(consumer
->socks
->ht
, &iter
.iter
, socket
,
307 /* Send destroy relayd command */
308 ret
= consumer_send_destroy_relayd(socket
, consumer
);
310 DBG("Unable to send destroy relayd command to consumer");
311 /* Continue since we MUST delete everything at this point. */
319 * From a consumer_data structure, allocate and add a consumer socket to the
322 * Return 0 on success, else negative value on error
324 int consumer_create_socket(struct consumer_data
*data
,
325 struct consumer_output
*output
)
328 struct consumer_socket
*socket
;
332 if (output
== NULL
|| data
->cmd_sock
< 0) {
334 * Not an error. Possible there is simply not spawned consumer or it's
335 * disabled for the tracing session asking the socket.
341 socket
= consumer_find_socket(data
->cmd_sock
, output
);
343 if (socket
== NULL
) {
344 socket
= consumer_allocate_socket(&data
->cmd_sock
);
345 if (socket
== NULL
) {
350 socket
->registered
= 0;
351 socket
->lock
= &data
->lock
;
353 consumer_add_socket(socket
, output
);
357 socket
->type
= data
->type
;
359 DBG3("Consumer socket created (fd: %d) and added to output",
367 * Return the consumer socket from the given consumer output with the right
368 * bitness. On error, returns NULL.
370 * The caller MUST acquire a rcu read side lock and keep it until the socket
371 * object reference is not needed anymore.
373 struct consumer_socket
*consumer_find_socket_by_bitness(int bits
,
374 const struct consumer_output
*consumer
)
377 struct consumer_socket
*socket
= NULL
;
381 consumer_fd
= uatomic_read(&ust_consumerd64_fd
);
384 consumer_fd
= uatomic_read(&ust_consumerd32_fd
);
391 socket
= consumer_find_socket(consumer_fd
, consumer
);
393 ERR("Consumer socket fd %d not found in consumer obj %p",
394 consumer_fd
, consumer
);
402 * Find a consumer_socket in a consumer_output hashtable. Read side lock must
403 * be acquired before calling this function and across use of the
404 * returned consumer_socket.
406 struct consumer_socket
*consumer_find_socket(int key
,
407 const struct consumer_output
*consumer
)
409 struct lttng_ht_iter iter
;
410 struct lttng_ht_node_ulong
*node
;
411 struct consumer_socket
*socket
= NULL
;
413 /* Negative keys are lookup failures */
414 if (key
< 0 || consumer
== NULL
) {
418 lttng_ht_lookup(consumer
->socks
, (void *)((unsigned long) key
),
420 node
= lttng_ht_iter_get_node_ulong(&iter
);
422 socket
= caa_container_of(node
, struct consumer_socket
, node
);
429 * Allocate a new consumer_socket and return the pointer.
431 struct consumer_socket
*consumer_allocate_socket(int *fd
)
433 struct consumer_socket
*socket
= NULL
;
437 socket
= zmalloc(sizeof(struct consumer_socket
));
438 if (socket
== NULL
) {
439 PERROR("zmalloc consumer socket");
444 lttng_ht_node_init_ulong(&socket
->node
, *fd
);
451 * Add consumer socket to consumer output object. Read side lock must be
452 * acquired before calling this function.
454 void consumer_add_socket(struct consumer_socket
*sock
,
455 struct consumer_output
*consumer
)
460 lttng_ht_add_unique_ulong(consumer
->socks
, &sock
->node
);
464 * Delete consumer socket to consumer output object. Read side lock must be
465 * acquired before calling this function.
467 void consumer_del_socket(struct consumer_socket
*sock
,
468 struct consumer_output
*consumer
)
471 struct lttng_ht_iter iter
;
476 iter
.iter
.node
= &sock
->node
.node
;
477 ret
= lttng_ht_del(consumer
->socks
, &iter
);
482 * RCU destroy call function.
484 static void destroy_socket_rcu(struct rcu_head
*head
)
486 struct lttng_ht_node_ulong
*node
=
487 caa_container_of(head
, struct lttng_ht_node_ulong
, head
);
488 struct consumer_socket
*socket
=
489 caa_container_of(node
, struct consumer_socket
, node
);
495 * Destroy and free socket pointer in a call RCU. Read side lock must be
496 * acquired before calling this function.
498 void consumer_destroy_socket(struct consumer_socket
*sock
)
503 * We DO NOT close the file descriptor here since it is global to the
504 * session daemon and is closed only if the consumer dies or a custom
505 * consumer was registered,
507 if (sock
->registered
) {
508 DBG3("Consumer socket was registered. Closing fd %d", *sock
->fd_ptr
);
509 lttcomm_close_unix_sock(*sock
->fd_ptr
);
512 call_rcu(&sock
->node
.head
, destroy_socket_rcu
);
516 * Allocate and assign data to a consumer_output object.
518 * Return pointer to structure.
520 struct consumer_output
*consumer_create_output(enum consumer_dst_type type
)
522 struct consumer_output
*output
= NULL
;
524 output
= zmalloc(sizeof(struct consumer_output
));
525 if (output
== NULL
) {
526 PERROR("zmalloc consumer_output");
530 /* By default, consumer output is enabled */
533 output
->net_seq_index
= (uint64_t) -1ULL;
534 urcu_ref_init(&output
->ref
);
536 output
->socks
= lttng_ht_new(0, LTTNG_HT_TYPE_ULONG
);
543 * Iterate over the consumer output socket hash table and destroy them. The
544 * socket file descriptor are only closed if the consumer output was
545 * registered meaning it's an external consumer.
547 void consumer_destroy_output_sockets(struct consumer_output
*obj
)
549 struct lttng_ht_iter iter
;
550 struct consumer_socket
*socket
;
557 cds_lfht_for_each_entry(obj
->socks
->ht
, &iter
.iter
, socket
, node
.node
) {
558 consumer_del_socket(socket
, obj
);
559 consumer_destroy_socket(socket
);
565 * Delete the consumer_output object from the list and free the ptr.
567 * Should *NOT* be called with RCU read-side lock held.
569 static void consumer_release_output(struct urcu_ref
*ref
)
571 struct consumer_output
*obj
=
572 caa_container_of(ref
, struct consumer_output
, ref
);
574 consumer_destroy_output_sockets(obj
);
577 /* Finally destroy HT */
578 ht_cleanup_push(obj
->socks
);
585 * Get the consumer_output object.
587 void consumer_output_get(struct consumer_output
*obj
)
589 urcu_ref_get(&obj
->ref
);
593 * Put the consumer_output object.
595 * Should *NOT* be called with RCU read-side lock held.
597 void consumer_output_put(struct consumer_output
*obj
)
602 urcu_ref_put(&obj
->ref
, consumer_release_output
);
606 * Copy consumer output and returned the newly allocated copy.
608 * Should *NOT* be called with RCU read-side lock held.
610 struct consumer_output
*consumer_copy_output(struct consumer_output
*src
)
613 struct consumer_output
*output
;
617 output
= consumer_create_output(src
->type
);
618 if (output
== NULL
) {
621 output
->enabled
= src
->enabled
;
622 output
->net_seq_index
= src
->net_seq_index
;
623 memcpy(output
->domain_subdir
, src
->domain_subdir
,
624 sizeof(output
->domain_subdir
));
625 output
->snapshot
= src
->snapshot
;
626 output
->relay_major_version
= src
->relay_major_version
;
627 output
->relay_minor_version
= src
->relay_minor_version
;
628 memcpy(&output
->dst
, &src
->dst
, sizeof(output
->dst
));
629 ret
= consumer_copy_sockets(output
, src
);
637 consumer_output_put(output
);
642 * Copy consumer sockets from src to dst.
644 * Return 0 on success or else a negative value.
646 int consumer_copy_sockets(struct consumer_output
*dst
,
647 struct consumer_output
*src
)
650 struct lttng_ht_iter iter
;
651 struct consumer_socket
*socket
, *copy_sock
;
657 cds_lfht_for_each_entry(src
->socks
->ht
, &iter
.iter
, socket
, node
.node
) {
658 /* Ignore socket that are already there. */
659 copy_sock
= consumer_find_socket(*socket
->fd_ptr
, dst
);
664 /* Create new socket object. */
665 copy_sock
= consumer_allocate_socket(socket
->fd_ptr
);
666 if (copy_sock
== NULL
) {
672 copy_sock
->registered
= socket
->registered
;
674 * This is valid because this lock is shared accross all consumer
675 * object being the global lock of the consumer data structure of the
678 copy_sock
->lock
= socket
->lock
;
679 consumer_add_socket(copy_sock
, dst
);
688 * Set network URI to the consumer output.
690 * Return 0 on success. Return 1 if the URI were equal. Else, negative value on
693 int consumer_set_network_uri(const struct ltt_session
*session
,
694 struct consumer_output
*output
,
695 struct lttng_uri
*uri
)
698 struct lttng_uri
*dst_uri
= NULL
;
700 /* Code flow error safety net. */
704 switch (uri
->stype
) {
705 case LTTNG_STREAM_CONTROL
:
706 dst_uri
= &output
->dst
.net
.control
;
707 output
->dst
.net
.control_isset
= 1;
708 if (uri
->port
== 0) {
709 /* Assign default port. */
710 uri
->port
= DEFAULT_NETWORK_CONTROL_PORT
;
712 if (output
->dst
.net
.data_isset
&& uri
->port
==
713 output
->dst
.net
.data
.port
) {
714 ret
= -LTTNG_ERR_INVALID
;
718 DBG3("Consumer control URI set with port %d", uri
->port
);
720 case LTTNG_STREAM_DATA
:
721 dst_uri
= &output
->dst
.net
.data
;
722 output
->dst
.net
.data_isset
= 1;
723 if (uri
->port
== 0) {
724 /* Assign default port. */
725 uri
->port
= DEFAULT_NETWORK_DATA_PORT
;
727 if (output
->dst
.net
.control_isset
&& uri
->port
==
728 output
->dst
.net
.control
.port
) {
729 ret
= -LTTNG_ERR_INVALID
;
733 DBG3("Consumer data URI set with port %d", uri
->port
);
736 ERR("Set network uri type unknown %d", uri
->stype
);
737 ret
= -LTTNG_ERR_INVALID
;
741 ret
= uri_compare(dst_uri
, uri
);
743 /* Same URI, don't touch it and return success. */
744 DBG3("URI network compare are the same");
748 /* URIs were not equal, replacing it. */
749 memcpy(dst_uri
, uri
, sizeof(struct lttng_uri
));
750 output
->type
= CONSUMER_DST_NET
;
751 if (dst_uri
->stype
!= LTTNG_STREAM_CONTROL
) {
752 /* Only the control uri needs to contain the path. */
757 * If the user has specified a subdir as part of the control
758 * URL, the session's base output directory is:
759 * /RELAYD_OUTPUT_PATH/HOSTNAME/USER_SPECIFIED_DIR
761 * Hence, the "base_dir" from which all stream files and
762 * session rotation chunks are created takes the form
763 * /HOSTNAME/USER_SPECIFIED_DIR
765 * If the user has not specified an output directory as part of
766 * the control URL, the base output directory has the form:
767 * /RELAYD_OUTPUT_PATH/HOSTNAME/SESSION_NAME-CREATION_TIME
769 * Hence, the "base_dir" from which all stream files and
770 * session rotation chunks are created takes the form
771 * /HOSTNAME/SESSION_NAME-CREATION_TIME
773 * Note that automatically generated session names already
774 * contain the session's creation time. In that case, the
775 * creation time is omitted to prevent it from being duplicated
776 * in the final directory hierarchy.
779 if (strstr(uri
->subdir
, "../")) {
780 ERR("Network URI subdirs are not allowed to walk up the path hierarchy");
781 ret
= -LTTNG_ERR_INVALID
;
784 ret
= snprintf(output
->dst
.net
.base_dir
,
785 sizeof(output
->dst
.net
.base_dir
),
786 "/%s/%s/", session
->hostname
, uri
->subdir
);
788 if (session
->has_auto_generated_name
) {
789 ret
= snprintf(output
->dst
.net
.base_dir
,
790 sizeof(output
->dst
.net
.base_dir
),
791 "/%s/%s/", session
->hostname
,
794 char session_creation_datetime
[16];
798 timeinfo
= localtime(&session
->creation_time
);
800 ret
= -LTTNG_ERR_FATAL
;
803 strftime_ret
= strftime(session_creation_datetime
,
804 sizeof(session_creation_datetime
),
805 "%Y%m%d-%H%M%S", timeinfo
);
806 if (strftime_ret
== 0) {
807 ERR("Failed to format session creation timestamp while setting network URI");
808 ret
= -LTTNG_ERR_FATAL
;
811 ret
= snprintf(output
->dst
.net
.base_dir
,
812 sizeof(output
->dst
.net
.base_dir
),
813 "/%s/%s-%s/", session
->hostname
,
815 session_creation_datetime
);
818 if (ret
>= sizeof(output
->dst
.net
.base_dir
)) {
819 ret
= -LTTNG_ERR_INVALID
;
820 ERR("Truncation occurred while setting network output base directory");
822 } else if (ret
== -1) {
823 ret
= -LTTNG_ERR_INVALID
;
824 PERROR("Error occurred while setting network output base directory");
828 DBG3("Consumer set network uri base_dir path %s",
829 output
->dst
.net
.base_dir
);
840 * Send file descriptor to consumer via sock.
842 * The consumer socket lock must be held by the caller.
844 int consumer_send_fds(struct consumer_socket
*sock
, const int *fds
,
852 assert(pthread_mutex_trylock(sock
->lock
) == EBUSY
);
854 ret
= lttcomm_send_fds_unix_sock(*sock
->fd_ptr
, fds
, nb_fd
);
856 /* The above call will print a PERROR on error. */
857 DBG("Error when sending consumer fds on sock %d", *sock
->fd_ptr
);
861 ret
= consumer_recv_status_reply(sock
);
867 * Consumer send communication message structure to consumer.
869 * The consumer socket lock must be held by the caller.
871 int consumer_send_msg(struct consumer_socket
*sock
,
872 struct lttcomm_consumer_msg
*msg
)
878 assert(pthread_mutex_trylock(sock
->lock
) == EBUSY
);
880 ret
= consumer_socket_send(sock
, msg
, sizeof(struct lttcomm_consumer_msg
));
885 ret
= consumer_recv_status_reply(sock
);
892 * Consumer send channel communication message structure to consumer.
894 * The consumer socket lock must be held by the caller.
896 int consumer_send_channel(struct consumer_socket
*sock
,
897 struct lttcomm_consumer_msg
*msg
)
904 ret
= consumer_send_msg(sock
, msg
);
914 * Populate the given consumer msg structure with the ask_channel command
917 void consumer_init_ask_channel_comm_msg(struct lttcomm_consumer_msg
*msg
,
918 uint64_t subbuf_size
,
921 unsigned int switch_timer_interval
,
922 unsigned int read_timer_interval
,
923 unsigned int live_timer_interval
,
924 unsigned int monitor_timer_interval
,
928 const char *pathname
,
934 uint64_t tracefile_size
,
935 uint64_t tracefile_count
,
936 uint64_t session_id_per_pid
,
937 unsigned int monitor
,
938 uint32_t ust_app_uid
,
939 int64_t blocking_timeout
,
940 const char *root_shm_path
,
941 const char *shm_path
,
942 struct lttng_trace_chunk
*trace_chunk
,
943 const struct lttng_credentials
*buffer_credentials
)
947 /* Zeroed structure */
948 memset(msg
, 0, sizeof(struct lttcomm_consumer_msg
));
949 msg
->u
.ask_channel
.buffer_credentials
.uid
= UINT32_MAX
;
950 msg
->u
.ask_channel
.buffer_credentials
.gid
= UINT32_MAX
;
954 enum lttng_trace_chunk_status chunk_status
;
956 chunk_status
= lttng_trace_chunk_get_id(trace_chunk
, &chunk_id
);
957 assert(chunk_status
== LTTNG_TRACE_CHUNK_STATUS_OK
);
958 LTTNG_OPTIONAL_SET(&msg
->u
.ask_channel
.chunk_id
, chunk_id
);
960 msg
->u
.ask_channel
.buffer_credentials
.uid
= buffer_credentials
->uid
;
961 msg
->u
.ask_channel
.buffer_credentials
.gid
= buffer_credentials
->gid
;
963 msg
->cmd_type
= LTTNG_CONSUMER_ASK_CHANNEL_CREATION
;
964 msg
->u
.ask_channel
.subbuf_size
= subbuf_size
;
965 msg
->u
.ask_channel
.num_subbuf
= num_subbuf
;
966 msg
->u
.ask_channel
.overwrite
= overwrite
;
967 msg
->u
.ask_channel
.switch_timer_interval
= switch_timer_interval
;
968 msg
->u
.ask_channel
.read_timer_interval
= read_timer_interval
;
969 msg
->u
.ask_channel
.live_timer_interval
= live_timer_interval
;
970 msg
->u
.ask_channel
.monitor_timer_interval
= monitor_timer_interval
;
971 msg
->u
.ask_channel
.output
= output
;
972 msg
->u
.ask_channel
.type
= type
;
973 msg
->u
.ask_channel
.session_id
= session_id
;
974 msg
->u
.ask_channel
.session_id_per_pid
= session_id_per_pid
;
975 msg
->u
.ask_channel
.relayd_id
= relayd_id
;
976 msg
->u
.ask_channel
.key
= key
;
977 msg
->u
.ask_channel
.chan_id
= chan_id
;
978 msg
->u
.ask_channel
.tracefile_size
= tracefile_size
;
979 msg
->u
.ask_channel
.tracefile_count
= tracefile_count
;
980 msg
->u
.ask_channel
.monitor
= monitor
;
981 msg
->u
.ask_channel
.ust_app_uid
= ust_app_uid
;
982 msg
->u
.ask_channel
.blocking_timeout
= blocking_timeout
;
984 memcpy(msg
->u
.ask_channel
.uuid
, uuid
, sizeof(msg
->u
.ask_channel
.uuid
));
987 strncpy(msg
->u
.ask_channel
.pathname
, pathname
,
988 sizeof(msg
->u
.ask_channel
.pathname
));
989 msg
->u
.ask_channel
.pathname
[sizeof(msg
->u
.ask_channel
.pathname
)-1] = '\0';
992 strncpy(msg
->u
.ask_channel
.name
, name
, sizeof(msg
->u
.ask_channel
.name
));
993 msg
->u
.ask_channel
.name
[sizeof(msg
->u
.ask_channel
.name
) - 1] = '\0';
996 strncpy(msg
->u
.ask_channel
.root_shm_path
, root_shm_path
,
997 sizeof(msg
->u
.ask_channel
.root_shm_path
));
998 msg
->u
.ask_channel
.root_shm_path
[sizeof(msg
->u
.ask_channel
.root_shm_path
) - 1] = '\0';
1001 strncpy(msg
->u
.ask_channel
.shm_path
, shm_path
,
1002 sizeof(msg
->u
.ask_channel
.shm_path
));
1003 msg
->u
.ask_channel
.shm_path
[sizeof(msg
->u
.ask_channel
.shm_path
) - 1] = '\0';
1008 * Init channel communication message structure.
1010 void consumer_init_add_channel_comm_msg(struct lttcomm_consumer_msg
*msg
,
1011 uint64_t channel_key
,
1012 uint64_t session_id
,
1013 const char *pathname
,
1018 unsigned int nb_init_streams
,
1019 enum lttng_event_output output
,
1021 uint64_t tracefile_size
,
1022 uint64_t tracefile_count
,
1023 unsigned int monitor
,
1024 unsigned int live_timer_interval
,
1025 unsigned int monitor_timer_interval
,
1026 struct lttng_trace_chunk
*trace_chunk
)
1030 /* Zeroed structure */
1031 memset(msg
, 0, sizeof(struct lttcomm_consumer_msg
));
1035 enum lttng_trace_chunk_status chunk_status
;
1037 chunk_status
= lttng_trace_chunk_get_id(trace_chunk
, &chunk_id
);
1038 assert(chunk_status
== LTTNG_TRACE_CHUNK_STATUS_OK
);
1039 LTTNG_OPTIONAL_SET(&msg
->u
.channel
.chunk_id
, chunk_id
);
1043 msg
->cmd_type
= LTTNG_CONSUMER_ADD_CHANNEL
;
1044 msg
->u
.channel
.channel_key
= channel_key
;
1045 msg
->u
.channel
.session_id
= session_id
;
1046 msg
->u
.channel
.relayd_id
= relayd_id
;
1047 msg
->u
.channel
.nb_init_streams
= nb_init_streams
;
1048 msg
->u
.channel
.output
= output
;
1049 msg
->u
.channel
.type
= type
;
1050 msg
->u
.channel
.tracefile_size
= tracefile_size
;
1051 msg
->u
.channel
.tracefile_count
= tracefile_count
;
1052 msg
->u
.channel
.monitor
= monitor
;
1053 msg
->u
.channel
.live_timer_interval
= live_timer_interval
;
1054 msg
->u
.channel
.monitor_timer_interval
= monitor_timer_interval
;
1056 strncpy(msg
->u
.channel
.pathname
, pathname
,
1057 sizeof(msg
->u
.channel
.pathname
));
1058 msg
->u
.channel
.pathname
[sizeof(msg
->u
.channel
.pathname
) - 1] = '\0';
1060 strncpy(msg
->u
.channel
.name
, name
, sizeof(msg
->u
.channel
.name
));
1061 msg
->u
.channel
.name
[sizeof(msg
->u
.channel
.name
) - 1] = '\0';
1065 * Init stream communication message structure.
1067 void consumer_init_add_stream_comm_msg(struct lttcomm_consumer_msg
*msg
,
1068 uint64_t channel_key
,
1069 uint64_t stream_key
,
1074 memset(msg
, 0, sizeof(struct lttcomm_consumer_msg
));
1076 msg
->cmd_type
= LTTNG_CONSUMER_ADD_STREAM
;
1077 msg
->u
.stream
.channel_key
= channel_key
;
1078 msg
->u
.stream
.stream_key
= stream_key
;
1079 msg
->u
.stream
.cpu
= cpu
;
1082 void consumer_init_streams_sent_comm_msg(struct lttcomm_consumer_msg
*msg
,
1083 enum lttng_consumer_command cmd
,
1084 uint64_t channel_key
, uint64_t net_seq_idx
)
1088 memset(msg
, 0, sizeof(struct lttcomm_consumer_msg
));
1090 msg
->cmd_type
= cmd
;
1091 msg
->u
.sent_streams
.channel_key
= channel_key
;
1092 msg
->u
.sent_streams
.net_seq_idx
= net_seq_idx
;
1096 * Send stream communication structure to the consumer.
1098 int consumer_send_stream(struct consumer_socket
*sock
,
1099 struct consumer_output
*dst
, struct lttcomm_consumer_msg
*msg
,
1100 const int *fds
, size_t nb_fd
)
1109 ret
= consumer_send_msg(sock
, msg
);
1114 ret
= consumer_send_fds(sock
, fds
, nb_fd
);
1124 * Send relayd socket to consumer associated with a session name.
1126 * The consumer socket lock must be held by the caller.
1128 * On success return positive value. On error, negative value.
1130 int consumer_send_relayd_socket(struct consumer_socket
*consumer_sock
,
1131 struct lttcomm_relayd_sock
*rsock
, struct consumer_output
*consumer
,
1132 enum lttng_stream_type type
, uint64_t session_id
,
1133 const char *session_name
, const char *hostname
,
1134 const char *base_path
, int session_live_timer
,
1135 const uint64_t *current_chunk_id
, time_t session_creation_time
,
1136 bool session_name_contains_creation_time
)
1139 struct lttcomm_consumer_msg msg
;
1141 /* Code flow error. Safety net. */
1144 assert(consumer_sock
);
1146 memset(&msg
, 0, sizeof(msg
));
1147 /* Bail out if consumer is disabled */
1148 if (!consumer
->enabled
) {
1153 if (type
== LTTNG_STREAM_CONTROL
) {
1154 char output_path
[LTTNG_PATH_MAX
] = {};
1156 ret
= relayd_create_session(rsock
,
1157 &msg
.u
.relayd_sock
.relayd_session_id
,
1158 session_name
, hostname
, base_path
,
1160 consumer
->snapshot
, session_id
,
1161 sessiond_uuid
, current_chunk_id
,
1162 session_creation_time
,
1163 session_name_contains_creation_time
,
1166 /* Close the control socket. */
1167 (void) relayd_close(rsock
);
1170 DBG("Created session on relay, output path reply: %s",
1174 msg
.cmd_type
= LTTNG_CONSUMER_ADD_RELAYD_SOCKET
;
1176 * Assign network consumer output index using the temporary consumer since
1177 * this call should only be made from within a set_consumer_uri() function
1178 * call in the session daemon.
1180 msg
.u
.relayd_sock
.net_index
= consumer
->net_seq_index
;
1181 msg
.u
.relayd_sock
.type
= type
;
1182 msg
.u
.relayd_sock
.session_id
= session_id
;
1183 memcpy(&msg
.u
.relayd_sock
.sock
, rsock
, sizeof(msg
.u
.relayd_sock
.sock
));
1185 DBG3("Sending relayd sock info to consumer on %d", *consumer_sock
->fd_ptr
);
1186 ret
= consumer_send_msg(consumer_sock
, &msg
);
1191 DBG3("Sending relayd socket file descriptor to consumer");
1192 ret
= consumer_send_fds(consumer_sock
, &rsock
->sock
.fd
, 1);
1197 DBG2("Consumer relayd socket sent");
1204 int consumer_send_pipe(struct consumer_socket
*consumer_sock
,
1205 enum lttng_consumer_command cmd
, int pipe
)
1208 struct lttcomm_consumer_msg msg
;
1209 const char *pipe_name
;
1210 const char *command_name
;
1213 case LTTNG_CONSUMER_SET_CHANNEL_MONITOR_PIPE
:
1214 pipe_name
= "channel monitor";
1215 command_name
= "SET_CHANNEL_MONITOR_PIPE";
1218 ERR("Unexpected command received in %s (cmd = %d)", __func__
,
1223 /* Code flow error. Safety net. */
1225 memset(&msg
, 0, sizeof(msg
));
1228 pthread_mutex_lock(consumer_sock
->lock
);
1229 DBG3("Sending %s command to consumer", command_name
);
1230 ret
= consumer_send_msg(consumer_sock
, &msg
);
1235 DBG3("Sending %s pipe %d to consumer on socket %d",
1237 pipe
, *consumer_sock
->fd_ptr
);
1238 ret
= consumer_send_fds(consumer_sock
, &pipe
, 1);
1243 DBG2("%s pipe successfully sent", pipe_name
);
1245 pthread_mutex_unlock(consumer_sock
->lock
);
1249 int consumer_send_channel_monitor_pipe(struct consumer_socket
*consumer_sock
,
1252 return consumer_send_pipe(consumer_sock
,
1253 LTTNG_CONSUMER_SET_CHANNEL_MONITOR_PIPE
, pipe
);
1257 * Ask the consumer if the data is pending for the specific session id.
1258 * Returns 1 if data is pending, 0 otherwise, or < 0 on error.
1260 int consumer_is_data_pending(uint64_t session_id
,
1261 struct consumer_output
*consumer
)
1264 int32_t ret_code
= 0; /* Default is that the data is NOT pending */
1265 struct consumer_socket
*socket
;
1266 struct lttng_ht_iter iter
;
1267 struct lttcomm_consumer_msg msg
;
1271 DBG3("Consumer data pending for id %" PRIu64
, session_id
);
1273 memset(&msg
, 0, sizeof(msg
));
1274 msg
.cmd_type
= LTTNG_CONSUMER_DATA_PENDING
;
1275 msg
.u
.data_pending
.session_id
= session_id
;
1277 /* Send command for each consumer */
1279 cds_lfht_for_each_entry(consumer
->socks
->ht
, &iter
.iter
, socket
,
1281 pthread_mutex_lock(socket
->lock
);
1282 ret
= consumer_socket_send(socket
, &msg
, sizeof(msg
));
1284 pthread_mutex_unlock(socket
->lock
);
1289 * No need for a recv reply status because the answer to the command is
1290 * the reply status message.
1293 ret
= consumer_socket_recv(socket
, &ret_code
, sizeof(ret_code
));
1295 pthread_mutex_unlock(socket
->lock
);
1298 pthread_mutex_unlock(socket
->lock
);
1300 if (ret_code
== 1) {
1306 DBG("Consumer data is %s pending for session id %" PRIu64
,
1307 ret_code
== 1 ? "" : "NOT", session_id
);
1316 * Send a flush command to consumer using the given channel key.
1318 * Return 0 on success else a negative value.
1320 int consumer_flush_channel(struct consumer_socket
*socket
, uint64_t key
)
1323 struct lttcomm_consumer_msg msg
;
1327 DBG2("Consumer flush channel key %" PRIu64
, key
);
1329 memset(&msg
, 0, sizeof(msg
));
1330 msg
.cmd_type
= LTTNG_CONSUMER_FLUSH_CHANNEL
;
1331 msg
.u
.flush_channel
.key
= key
;
1333 pthread_mutex_lock(socket
->lock
);
1334 health_code_update();
1336 ret
= consumer_send_msg(socket
, &msg
);
1342 health_code_update();
1343 pthread_mutex_unlock(socket
->lock
);
1348 * Send a clear quiescent command to consumer using the given channel key.
1350 * Return 0 on success else a negative value.
1352 int consumer_clear_quiescent_channel(struct consumer_socket
*socket
, uint64_t key
)
1355 struct lttcomm_consumer_msg msg
;
1359 DBG2("Consumer clear quiescent channel key %" PRIu64
, key
);
1361 memset(&msg
, 0, sizeof(msg
));
1362 msg
.cmd_type
= LTTNG_CONSUMER_CLEAR_QUIESCENT_CHANNEL
;
1363 msg
.u
.clear_quiescent_channel
.key
= key
;
1365 pthread_mutex_lock(socket
->lock
);
1366 health_code_update();
1368 ret
= consumer_send_msg(socket
, &msg
);
1374 health_code_update();
1375 pthread_mutex_unlock(socket
->lock
);
1380 * Send a close metadata command to consumer using the given channel key.
1381 * Called with registry lock held.
1383 * Return 0 on success else a negative value.
1385 int consumer_close_metadata(struct consumer_socket
*socket
,
1386 uint64_t metadata_key
)
1389 struct lttcomm_consumer_msg msg
;
1393 DBG2("Consumer close metadata channel key %" PRIu64
, metadata_key
);
1395 memset(&msg
, 0, sizeof(msg
));
1396 msg
.cmd_type
= LTTNG_CONSUMER_CLOSE_METADATA
;
1397 msg
.u
.close_metadata
.key
= metadata_key
;
1399 pthread_mutex_lock(socket
->lock
);
1400 health_code_update();
1402 ret
= consumer_send_msg(socket
, &msg
);
1408 health_code_update();
1409 pthread_mutex_unlock(socket
->lock
);
1414 * Send a setup metdata command to consumer using the given channel key.
1416 * Return 0 on success else a negative value.
1418 int consumer_setup_metadata(struct consumer_socket
*socket
,
1419 uint64_t metadata_key
)
1422 struct lttcomm_consumer_msg msg
;
1426 DBG2("Consumer setup metadata channel key %" PRIu64
, metadata_key
);
1428 memset(&msg
, 0, sizeof(msg
));
1429 msg
.cmd_type
= LTTNG_CONSUMER_SETUP_METADATA
;
1430 msg
.u
.setup_metadata
.key
= metadata_key
;
1432 pthread_mutex_lock(socket
->lock
);
1433 health_code_update();
1435 ret
= consumer_send_msg(socket
, &msg
);
1441 health_code_update();
1442 pthread_mutex_unlock(socket
->lock
);
1447 * Send metadata string to consumer.
1448 * RCU read-side lock must be held to guarantee existence of socket.
1450 * Return 0 on success else a negative value.
1452 int consumer_push_metadata(struct consumer_socket
*socket
,
1453 uint64_t metadata_key
, char *metadata_str
, size_t len
,
1454 size_t target_offset
, uint64_t version
)
1457 struct lttcomm_consumer_msg msg
;
1461 DBG2("Consumer push metadata to consumer socket %d", *socket
->fd_ptr
);
1463 pthread_mutex_lock(socket
->lock
);
1465 memset(&msg
, 0, sizeof(msg
));
1466 msg
.cmd_type
= LTTNG_CONSUMER_PUSH_METADATA
;
1467 msg
.u
.push_metadata
.key
= metadata_key
;
1468 msg
.u
.push_metadata
.target_offset
= target_offset
;
1469 msg
.u
.push_metadata
.len
= len
;
1470 msg
.u
.push_metadata
.version
= version
;
1472 health_code_update();
1473 ret
= consumer_send_msg(socket
, &msg
);
1474 if (ret
< 0 || len
== 0) {
1478 DBG3("Consumer pushing metadata on sock %d of len %zu", *socket
->fd_ptr
,
1481 ret
= consumer_socket_send(socket
, metadata_str
, len
);
1486 health_code_update();
1487 ret
= consumer_recv_status_reply(socket
);
1493 pthread_mutex_unlock(socket
->lock
);
1494 health_code_update();
1499 * Ask the consumer to snapshot a specific channel using the key.
1501 * Returns LTTNG_OK on success or else an LTTng error code.
1503 enum lttng_error_code
consumer_snapshot_channel(struct consumer_socket
*socket
,
1504 uint64_t key
, const struct consumer_output
*output
, int metadata
,
1505 uid_t uid
, gid_t gid
, const char *channel_path
, int wait
,
1506 uint64_t nb_packets_per_stream
)
1509 enum lttng_error_code status
= LTTNG_OK
;
1510 struct lttcomm_consumer_msg msg
;
1515 DBG("Consumer snapshot channel key %" PRIu64
, key
);
1517 memset(&msg
, 0, sizeof(msg
));
1518 msg
.cmd_type
= LTTNG_CONSUMER_SNAPSHOT_CHANNEL
;
1519 msg
.u
.snapshot_channel
.key
= key
;
1520 msg
.u
.snapshot_channel
.nb_packets_per_stream
= nb_packets_per_stream
;
1521 msg
.u
.snapshot_channel
.metadata
= metadata
;
1523 if (output
->type
== CONSUMER_DST_NET
) {
1524 msg
.u
.snapshot_channel
.relayd_id
=
1525 output
->net_seq_index
;
1526 msg
.u
.snapshot_channel
.use_relayd
= 1;
1528 msg
.u
.snapshot_channel
.relayd_id
= (uint64_t) -1ULL;
1530 ret
= lttng_strncpy(msg
.u
.snapshot_channel
.pathname
,
1532 sizeof(msg
.u
.snapshot_channel
.pathname
));
1534 ERR("Snapshot path exceeds the maximal allowed length of %zu bytes (%zu bytes required) with path \"%s\"",
1535 sizeof(msg
.u
.snapshot_channel
.pathname
),
1536 strlen(channel_path
),
1538 status
= LTTNG_ERR_SNAPSHOT_FAIL
;
1542 health_code_update();
1543 pthread_mutex_lock(socket
->lock
);
1544 ret
= consumer_send_msg(socket
, &msg
);
1545 pthread_mutex_unlock(socket
->lock
);
1548 case LTTCOMM_CONSUMERD_CHAN_NOT_FOUND
:
1549 status
= LTTNG_ERR_CHAN_NOT_FOUND
;
1552 status
= LTTNG_ERR_SNAPSHOT_FAIL
;
1559 health_code_update();
1564 * Ask the consumer the number of discarded events for a channel.
1566 int consumer_get_discarded_events(uint64_t session_id
, uint64_t channel_key
,
1567 struct consumer_output
*consumer
, uint64_t *discarded
)
1570 struct consumer_socket
*socket
;
1571 struct lttng_ht_iter iter
;
1572 struct lttcomm_consumer_msg msg
;
1576 DBG3("Consumer discarded events id %" PRIu64
, session_id
);
1578 memset(&msg
, 0, sizeof(msg
));
1579 msg
.cmd_type
= LTTNG_CONSUMER_DISCARDED_EVENTS
;
1580 msg
.u
.discarded_events
.session_id
= session_id
;
1581 msg
.u
.discarded_events
.channel_key
= channel_key
;
1585 /* Send command for each consumer */
1587 cds_lfht_for_each_entry(consumer
->socks
->ht
, &iter
.iter
, socket
,
1589 uint64_t consumer_discarded
= 0;
1590 pthread_mutex_lock(socket
->lock
);
1591 ret
= consumer_socket_send(socket
, &msg
, sizeof(msg
));
1593 pthread_mutex_unlock(socket
->lock
);
1598 * No need for a recv reply status because the answer to the
1599 * command is the reply status message.
1601 ret
= consumer_socket_recv(socket
, &consumer_discarded
,
1602 sizeof(consumer_discarded
));
1604 ERR("get discarded events");
1605 pthread_mutex_unlock(socket
->lock
);
1608 pthread_mutex_unlock(socket
->lock
);
1609 *discarded
+= consumer_discarded
;
1612 DBG("Consumer discarded %" PRIu64
" events in session id %" PRIu64
,
1613 *discarded
, session_id
);
1621 * Ask the consumer the number of lost packets for a channel.
1623 int consumer_get_lost_packets(uint64_t session_id
, uint64_t channel_key
,
1624 struct consumer_output
*consumer
, uint64_t *lost
)
1627 struct consumer_socket
*socket
;
1628 struct lttng_ht_iter iter
;
1629 struct lttcomm_consumer_msg msg
;
1633 DBG3("Consumer lost packets id %" PRIu64
, session_id
);
1635 memset(&msg
, 0, sizeof(msg
));
1636 msg
.cmd_type
= LTTNG_CONSUMER_LOST_PACKETS
;
1637 msg
.u
.lost_packets
.session_id
= session_id
;
1638 msg
.u
.lost_packets
.channel_key
= channel_key
;
1642 /* Send command for each consumer */
1644 cds_lfht_for_each_entry(consumer
->socks
->ht
, &iter
.iter
, socket
,
1646 uint64_t consumer_lost
= 0;
1647 pthread_mutex_lock(socket
->lock
);
1648 ret
= consumer_socket_send(socket
, &msg
, sizeof(msg
));
1650 pthread_mutex_unlock(socket
->lock
);
1655 * No need for a recv reply status because the answer to the
1656 * command is the reply status message.
1658 ret
= consumer_socket_recv(socket
, &consumer_lost
,
1659 sizeof(consumer_lost
));
1661 ERR("get lost packets");
1662 pthread_mutex_unlock(socket
->lock
);
1665 pthread_mutex_unlock(socket
->lock
);
1666 *lost
+= consumer_lost
;
1669 DBG("Consumer lost %" PRIu64
" packets in session id %" PRIu64
,
1678 * Ask the consumer to rotate a channel.
1680 * The new_chunk_id is the session->rotate_count that has been incremented
1681 * when the rotation started. On the relay, this allows to keep track in which
1682 * chunk each stream is currently writing to (for the rotate_pending operation).
1684 int consumer_rotate_channel(struct consumer_socket
*socket
, uint64_t key
,
1685 uid_t uid
, gid_t gid
, struct consumer_output
*output
,
1686 bool is_metadata_channel
)
1689 struct lttcomm_consumer_msg msg
;
1693 DBG("Consumer rotate channel key %" PRIu64
, key
);
1695 pthread_mutex_lock(socket
->lock
);
1696 memset(&msg
, 0, sizeof(msg
));
1697 msg
.cmd_type
= LTTNG_CONSUMER_ROTATE_CHANNEL
;
1698 msg
.u
.rotate_channel
.key
= key
;
1699 msg
.u
.rotate_channel
.metadata
= !!is_metadata_channel
;
1701 if (output
->type
== CONSUMER_DST_NET
) {
1702 msg
.u
.rotate_channel
.relayd_id
= output
->net_seq_index
;
1704 msg
.u
.rotate_channel
.relayd_id
= (uint64_t) -1ULL;
1707 health_code_update();
1708 ret
= consumer_send_msg(socket
, &msg
);
1711 case LTTCOMM_CONSUMERD_CHAN_NOT_FOUND
:
1712 ret
= -LTTNG_ERR_CHAN_NOT_FOUND
;
1715 ret
= -LTTNG_ERR_ROTATION_FAIL_CONSUMER
;
1721 pthread_mutex_unlock(socket
->lock
);
1722 health_code_update();
1726 int consumer_init(struct consumer_socket
*socket
,
1727 const lttng_uuid sessiond_uuid
)
1730 struct lttcomm_consumer_msg msg
= {
1731 .cmd_type
= LTTNG_CONSUMER_INIT
,
1736 DBG("Sending consumer initialization command");
1737 lttng_uuid_copy(msg
.u
.init
.sessiond_uuid
, sessiond_uuid
);
1739 health_code_update();
1740 ret
= consumer_send_msg(socket
, &msg
);
1746 health_code_update();
1751 * Ask the consumer to create a new chunk for a given session.
1753 * Called with the consumer socket lock held.
1755 int consumer_create_trace_chunk(struct consumer_socket
*socket
,
1756 uint64_t relayd_id
, uint64_t session_id
,
1757 struct lttng_trace_chunk
*chunk
)
1760 enum lttng_trace_chunk_status chunk_status
;
1761 struct lttng_credentials chunk_credentials
;
1762 const struct lttng_directory_handle
*chunk_directory_handle
;
1764 const char *chunk_name
;
1765 bool chunk_name_overridden
;
1767 time_t creation_timestamp
;
1768 char creation_timestamp_buffer
[ISO8601_STR_LEN
];
1769 const char *creation_timestamp_str
= "(none)";
1770 const bool chunk_has_local_output
= relayd_id
== -1ULL;
1771 struct lttcomm_consumer_msg msg
= {
1772 .cmd_type
= LTTNG_CONSUMER_CREATE_TRACE_CHUNK
,
1773 .u
.create_trace_chunk
.session_id
= session_id
,
1779 if (relayd_id
!= -1ULL) {
1780 LTTNG_OPTIONAL_SET(&msg
.u
.create_trace_chunk
.relayd_id
,
1784 chunk_status
= lttng_trace_chunk_get_name(chunk
, &chunk_name
,
1785 &chunk_name_overridden
);
1786 if (chunk_status
!= LTTNG_TRACE_CHUNK_STATUS_OK
&&
1787 chunk_status
!= LTTNG_TRACE_CHUNK_STATUS_NONE
) {
1788 ERR("Failed to get name of trace chunk");
1789 ret
= -LTTNG_ERR_FATAL
;
1792 if (chunk_name_overridden
) {
1793 ret
= lttng_strncpy(msg
.u
.create_trace_chunk
.override_name
,
1795 sizeof(msg
.u
.create_trace_chunk
.override_name
));
1797 ERR("Trace chunk name \"%s\" exceeds the maximal length allowed by the consumer protocol",
1799 ret
= -LTTNG_ERR_FATAL
;
1804 chunk_status
= lttng_trace_chunk_get_creation_timestamp(chunk
,
1805 &creation_timestamp
);
1806 if (chunk_status
!= LTTNG_TRACE_CHUNK_STATUS_OK
) {
1807 ret
= -LTTNG_ERR_FATAL
;
1810 msg
.u
.create_trace_chunk
.creation_timestamp
=
1811 (uint64_t) creation_timestamp
;
1812 /* Only used for logging purposes. */
1813 ret
= time_to_iso8601_str(creation_timestamp
,
1814 creation_timestamp_buffer
,
1815 sizeof(creation_timestamp_buffer
));
1816 creation_timestamp_str
= !ret
? creation_timestamp_buffer
:
1817 "(formatting error)";
1819 chunk_status
= lttng_trace_chunk_get_id(chunk
, &chunk_id
);
1820 if (chunk_status
!= LTTNG_TRACE_CHUNK_STATUS_OK
) {
1822 * Anonymous trace chunks should never be transmitted
1823 * to remote peers (consumerd and relayd). They are used
1824 * internally for backward-compatibility purposes.
1826 ret
= -LTTNG_ERR_FATAL
;
1829 msg
.u
.create_trace_chunk
.chunk_id
= chunk_id
;
1831 if (chunk_has_local_output
) {
1832 chunk_status
= lttng_trace_chunk_get_chunk_directory_handle(
1833 chunk
, &chunk_directory_handle
);
1834 if (chunk_status
!= LTTNG_TRACE_CHUNK_STATUS_OK
) {
1835 ret
= -LTTNG_ERR_FATAL
;
1840 * This will only compile on platforms that support
1841 * dirfd (POSIX.2008). This is fine as the session daemon
1842 * is only built for such platforms.
1844 * The ownership of the chunk directory handle's is maintained
1845 * by the trace chunk.
1847 chunk_dirfd
= lttng_directory_handle_get_dirfd(
1848 chunk_directory_handle
);
1849 assert(chunk_dirfd
>= 0);
1851 chunk_status
= lttng_trace_chunk_get_credentials(
1852 chunk
, &chunk_credentials
);
1853 if (chunk_status
!= LTTNG_TRACE_CHUNK_STATUS_OK
) {
1855 * Not associating credentials to a sessiond chunk is a
1856 * fatal internal error.
1858 ret
= -LTTNG_ERR_FATAL
;
1861 msg
.u
.create_trace_chunk
.credentials
.value
.uid
=
1862 chunk_credentials
.uid
;
1863 msg
.u
.create_trace_chunk
.credentials
.value
.gid
=
1864 chunk_credentials
.gid
;
1865 msg
.u
.create_trace_chunk
.credentials
.is_set
= 1;
1868 DBG("Sending consumer create trace chunk command: relayd_id = %" PRId64
1869 ", session_id = %" PRIu64
", chunk_id = %" PRIu64
1870 ", creation_timestamp = %s",
1871 relayd_id
, session_id
, chunk_id
,
1872 creation_timestamp_str
);
1873 health_code_update();
1874 ret
= consumer_send_msg(socket
, &msg
);
1875 health_code_update();
1877 ERR("Trace chunk creation error on consumer");
1878 ret
= -LTTNG_ERR_CREATE_TRACE_CHUNK_FAIL_CONSUMER
;
1882 if (chunk_has_local_output
) {
1883 DBG("Sending trace chunk directory fd to consumer");
1884 health_code_update();
1885 ret
= consumer_send_fds(socket
, &chunk_dirfd
, 1);
1886 health_code_update();
1888 ERR("Trace chunk creation error on consumer");
1889 ret
= -LTTNG_ERR_CREATE_TRACE_CHUNK_FAIL_CONSUMER
;
1898 * Ask the consumer to close a trace chunk for a given session.
1900 * Called with the consumer socket lock held.
1902 int consumer_close_trace_chunk(struct consumer_socket
*socket
,
1903 uint64_t relayd_id
, uint64_t session_id
,
1904 struct lttng_trace_chunk
*chunk
,
1905 char *closed_trace_chunk_path
)
1908 enum lttng_trace_chunk_status chunk_status
;
1909 struct lttcomm_consumer_msg msg
= {
1910 .cmd_type
= LTTNG_CONSUMER_CLOSE_TRACE_CHUNK
,
1911 .u
.close_trace_chunk
.session_id
= session_id
,
1913 struct lttcomm_consumer_close_trace_chunk_reply reply
;
1915 time_t close_timestamp
;
1916 enum lttng_trace_chunk_command_type close_command
;
1917 const char *close_command_name
= "none";
1918 struct lttng_dynamic_buffer path_reception_buffer
;
1921 lttng_dynamic_buffer_init(&path_reception_buffer
);
1923 if (relayd_id
!= -1ULL) {
1925 &msg
.u
.close_trace_chunk
.relayd_id
, relayd_id
);
1928 chunk_status
= lttng_trace_chunk_get_close_command(
1929 chunk
, &close_command
);
1930 switch (chunk_status
) {
1931 case LTTNG_TRACE_CHUNK_STATUS_OK
:
1932 LTTNG_OPTIONAL_SET(&msg
.u
.close_trace_chunk
.close_command
,
1933 (uint32_t) close_command
);
1935 case LTTNG_TRACE_CHUNK_STATUS_NONE
:
1938 ERR("Failed to get trace chunk close command");
1943 chunk_status
= lttng_trace_chunk_get_id(chunk
, &chunk_id
);
1945 * Anonymous trace chunks should never be transmitted to remote peers
1946 * (consumerd and relayd). They are used internally for
1947 * backward-compatibility purposes.
1949 assert(chunk_status
== LTTNG_TRACE_CHUNK_STATUS_OK
);
1950 msg
.u
.close_trace_chunk
.chunk_id
= chunk_id
;
1952 chunk_status
= lttng_trace_chunk_get_close_timestamp(chunk
,
1955 * A trace chunk should be closed locally before being closed remotely.
1956 * Otherwise, the close timestamp would never be transmitted to the
1959 assert(chunk_status
== LTTNG_TRACE_CHUNK_STATUS_OK
);
1960 msg
.u
.close_trace_chunk
.close_timestamp
= (uint64_t) close_timestamp
;
1962 if (msg
.u
.close_trace_chunk
.close_command
.is_set
) {
1963 close_command_name
= lttng_trace_chunk_command_type_get_name(
1966 DBG("Sending consumer close trace chunk command: relayd_id = %" PRId64
1967 ", session_id = %" PRIu64
", chunk_id = %" PRIu64
1968 ", close command = \"%s\"",
1969 relayd_id
, session_id
, chunk_id
, close_command_name
);
1971 health_code_update();
1972 ret
= consumer_socket_send(socket
, &msg
, sizeof(struct lttcomm_consumer_msg
));
1974 ret
= -LTTNG_ERR_CLOSE_TRACE_CHUNK_FAIL_CONSUMER
;
1977 ret
= consumer_socket_recv(socket
, &reply
, sizeof(reply
));
1979 ret
= -LTTNG_ERR_CLOSE_TRACE_CHUNK_FAIL_CONSUMER
;
1982 if (reply
.path_length
>= LTTNG_PATH_MAX
) {
1983 ERR("Invalid path returned by relay daemon: %" PRIu32
"bytes exceeds maximal allowed length of %d bytes",
1984 reply
.path_length
, LTTNG_PATH_MAX
);
1985 ret
= -LTTNG_ERR_INVALID_PROTOCOL
;
1988 ret
= lttng_dynamic_buffer_set_size(&path_reception_buffer
,
1991 ERR("Failed to allocate reception buffer of path returned by the \"close trace chunk\" command");
1992 ret
= -LTTNG_ERR_NOMEM
;
1995 ret
= consumer_socket_recv(socket
, path_reception_buffer
.data
,
1996 path_reception_buffer
.size
);
1998 ERR("Communication error while receiving path of closed trace chunk");
1999 ret
= -LTTNG_ERR_CLOSE_TRACE_CHUNK_FAIL_CONSUMER
;
2002 if (path_reception_buffer
.data
[path_reception_buffer
.size
- 1] != '\0') {
2003 ERR("Invalid path returned by relay daemon: not null-terminated");
2004 ret
= -LTTNG_ERR_INVALID_PROTOCOL
;
2007 if (closed_trace_chunk_path
) {
2009 * closed_trace_chunk_path is assumed to have a length >=
2012 memcpy(closed_trace_chunk_path
, path_reception_buffer
.data
,
2013 path_reception_buffer
.size
);
2016 lttng_dynamic_buffer_reset(&path_reception_buffer
);
2017 health_code_update();
2022 * Ask the consumer if a trace chunk exists.
2024 * Called with the consumer socket lock held.
2025 * Returns 0 on success, or a negative value on error.
2027 int consumer_trace_chunk_exists(struct consumer_socket
*socket
,
2028 uint64_t relayd_id
, uint64_t session_id
,
2029 struct lttng_trace_chunk
*chunk
,
2030 enum consumer_trace_chunk_exists_status
*result
)
2033 enum lttng_trace_chunk_status chunk_status
;
2034 struct lttcomm_consumer_msg msg
= {
2035 .cmd_type
= LTTNG_CONSUMER_TRACE_CHUNK_EXISTS
,
2036 .u
.trace_chunk_exists
.session_id
= session_id
,
2039 const char *consumer_reply_str
;
2043 if (relayd_id
!= -1ULL) {
2044 LTTNG_OPTIONAL_SET(&msg
.u
.trace_chunk_exists
.relayd_id
,
2048 chunk_status
= lttng_trace_chunk_get_id(chunk
, &chunk_id
);
2049 if (chunk_status
!= LTTNG_TRACE_CHUNK_STATUS_OK
) {
2051 * Anonymous trace chunks should never be transmitted
2052 * to remote peers (consumerd and relayd). They are used
2053 * internally for backward-compatibility purposes.
2055 ret
= -LTTNG_ERR_FATAL
;
2058 msg
.u
.trace_chunk_exists
.chunk_id
= chunk_id
;
2060 DBG("Sending consumer trace chunk exists command: relayd_id = %" PRId64
2061 ", session_id = %" PRIu64
2062 ", chunk_id = %" PRIu64
, relayd_id
, session_id
, chunk_id
);
2064 health_code_update();
2065 ret
= consumer_send_msg(socket
, &msg
);
2067 case LTTCOMM_CONSUMERD_UNKNOWN_TRACE_CHUNK
:
2068 consumer_reply_str
= "unknown trace chunk";
2069 *result
= CONSUMER_TRACE_CHUNK_EXISTS_STATUS_UNKNOWN_CHUNK
;
2071 case LTTCOMM_CONSUMERD_TRACE_CHUNK_EXISTS_LOCAL
:
2072 consumer_reply_str
= "trace chunk exists locally";
2073 *result
= CONSUMER_TRACE_CHUNK_EXISTS_STATUS_EXISTS_LOCAL
;
2075 case LTTCOMM_CONSUMERD_TRACE_CHUNK_EXISTS_REMOTE
:
2076 consumer_reply_str
= "trace chunk exists on remote peer";
2077 *result
= CONSUMER_TRACE_CHUNK_EXISTS_STATUS_EXISTS_REMOTE
;
2080 ERR("Consumer returned an error from TRACE_CHUNK_EXISTS command");
2084 DBG("Consumer reply to TRACE_CHUNK_EXISTS command: %s",
2085 consumer_reply_str
);
2088 health_code_update();