2 * Copyright (C) 2012 - David Goulet <dgoulet@efficios.com>
4 * This program is free software; you can redistribute it and/or modify it
5 * under the terms of the GNU General Public License, version 2 only, as
6 * published by the Free Software Foundation.
8 * This program is distributed in the hope that it will be useful, but WITHOUT
9 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
10 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
13 * You should have received a copy of the GNU General Public License along with
14 * this program; if not, write to the Free Software Foundation, Inc., 51
15 * Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
24 #include <sys/types.h>
28 #include <common/common.h>
29 #include <common/defaults.h>
30 #include <common/uri.h>
38 * Receive a reply command status message from the consumer. Consumer socket
39 * lock MUST be acquired before calling this function.
41 * Return 0 on success, -1 on recv error or a negative lttng error code which
42 * was possibly returned by the consumer.
44 int consumer_recv_status_reply(struct consumer_socket
*sock
)
47 struct lttcomm_consumer_status_msg reply
;
51 ret
= lttcomm_recv_unix_sock(sock
->fd
, &reply
, sizeof(reply
));
54 /* Orderly shutdown. Don't return 0 which means success. */
57 /* The above call will print a PERROR on error. */
58 DBG("Fail to receive status reply on sock %d", sock
->fd
);
62 if (reply
.ret_code
== LTTNG_OK
) {
66 ret
= -reply
.ret_code
;
67 DBG("Consumer ret code %d", ret
);
75 * Once the ASK_CHANNEL command is sent to the consumer, the channel
76 * information are sent back. This call receives that data and populates key
79 * On success return 0 and both key and stream_count are set. On error, a
80 * negative value is sent back and both parameters are untouched.
82 int consumer_recv_status_channel(struct consumer_socket
*sock
,
83 uint64_t *key
, unsigned int *stream_count
)
86 struct lttcomm_consumer_status_channel reply
;
92 ret
= lttcomm_recv_unix_sock(sock
->fd
, &reply
, sizeof(reply
));
95 /* Orderly shutdown. Don't return 0 which means success. */
98 /* The above call will print a PERROR on error. */
99 DBG("Fail to receive status reply on sock %d", sock
->fd
);
103 /* An error is possible so don't touch the key and stream_count. */
104 if (reply
.ret_code
!= LTTNG_OK
) {
110 *stream_count
= reply
.stream_count
;
117 * Send destroy relayd command to consumer.
119 * On success return positive value. On error, negative value.
121 int consumer_send_destroy_relayd(struct consumer_socket
*sock
,
122 struct consumer_output
*consumer
)
125 struct lttcomm_consumer_msg msg
;
130 DBG2("Sending destroy relayd command to consumer sock %d", sock
->fd
);
132 /* Bail out if consumer is disabled */
133 if (!consumer
->enabled
) {
135 DBG3("Consumer is disabled");
139 msg
.cmd_type
= LTTNG_CONSUMER_DESTROY_RELAYD
;
140 msg
.u
.destroy_relayd
.net_seq_idx
= consumer
->net_seq_index
;
142 pthread_mutex_lock(sock
->lock
);
143 ret
= lttcomm_send_unix_sock(sock
->fd
, &msg
, sizeof(msg
));
145 /* Indicate that the consumer is probably closing at this point. */
146 DBG("send consumer destroy relayd command");
150 /* Don't check the return value. The caller will do it. */
151 ret
= consumer_recv_status_reply(sock
);
153 DBG2("Consumer send destroy relayd command done");
156 pthread_mutex_unlock(sock
->lock
);
162 * For each consumer socket in the consumer output object, send a destroy
165 void consumer_output_send_destroy_relayd(struct consumer_output
*consumer
)
167 struct lttng_ht_iter iter
;
168 struct consumer_socket
*socket
;
172 /* Destroy any relayd connection */
173 if (consumer
->type
== CONSUMER_DST_NET
) {
175 cds_lfht_for_each_entry(consumer
->socks
->ht
, &iter
.iter
, socket
,
179 /* Send destroy relayd command */
180 ret
= consumer_send_destroy_relayd(socket
, consumer
);
182 DBG("Unable to send destroy relayd command to consumer");
183 /* Continue since we MUST delete everything at this point. */
191 * From a consumer_data structure, allocate and add a consumer socket to the
194 * Return 0 on success, else negative value on error
196 int consumer_create_socket(struct consumer_data
*data
,
197 struct consumer_output
*output
)
200 struct consumer_socket
*socket
;
204 if (output
== NULL
|| data
->cmd_sock
< 0) {
206 * Not an error. Possible there is simply not spawned consumer or it's
207 * disabled for the tracing session asking the socket.
213 socket
= consumer_find_socket(data
->cmd_sock
, output
);
215 if (socket
== NULL
) {
216 socket
= consumer_allocate_socket(data
->cmd_sock
);
217 if (socket
== NULL
) {
222 socket
->registered
= 0;
223 socket
->lock
= &data
->lock
;
225 consumer_add_socket(socket
, output
);
229 socket
->type
= data
->type
;
231 DBG3("Consumer socket created (fd: %d) and added to output",
239 * Return the consumer socket from the given consumer output with the right
240 * bitness. On error, returns NULL.
242 * The caller MUST acquire a rcu read side lock and keep it until the socket
243 * object reference is not needed anymore.
245 struct consumer_socket
*consumer_find_socket_by_bitness(int bits
,
246 struct consumer_output
*consumer
)
249 struct consumer_socket
*socket
= NULL
;
253 consumer_fd
= uatomic_read(&ust_consumerd64_fd
);
256 consumer_fd
= uatomic_read(&ust_consumerd32_fd
);
263 socket
= consumer_find_socket(consumer_fd
, consumer
);
265 ERR("Consumer socket fd %d not found in consumer obj %p",
266 consumer_fd
, consumer
);
274 * Find a consumer_socket in a consumer_output hashtable. Read side lock must
275 * be acquired before calling this function and across use of the
276 * returned consumer_socket.
278 struct consumer_socket
*consumer_find_socket(int key
,
279 struct consumer_output
*consumer
)
281 struct lttng_ht_iter iter
;
282 struct lttng_ht_node_ulong
*node
;
283 struct consumer_socket
*socket
= NULL
;
285 /* Negative keys are lookup failures */
286 if (key
< 0 || consumer
== NULL
) {
290 lttng_ht_lookup(consumer
->socks
, (void *)((unsigned long) key
),
292 node
= lttng_ht_iter_get_node_ulong(&iter
);
294 socket
= caa_container_of(node
, struct consumer_socket
, node
);
301 * Allocate a new consumer_socket and return the pointer.
303 struct consumer_socket
*consumer_allocate_socket(int fd
)
305 struct consumer_socket
*socket
= NULL
;
307 socket
= zmalloc(sizeof(struct consumer_socket
));
308 if (socket
== NULL
) {
309 PERROR("zmalloc consumer socket");
314 lttng_ht_node_init_ulong(&socket
->node
, fd
);
321 * Add consumer socket to consumer output object. Read side lock must be
322 * acquired before calling this function.
324 void consumer_add_socket(struct consumer_socket
*sock
,
325 struct consumer_output
*consumer
)
330 lttng_ht_add_unique_ulong(consumer
->socks
, &sock
->node
);
334 * Delte consumer socket to consumer output object. Read side lock must be
335 * acquired before calling this function.
337 void consumer_del_socket(struct consumer_socket
*sock
,
338 struct consumer_output
*consumer
)
341 struct lttng_ht_iter iter
;
346 iter
.iter
.node
= &sock
->node
.node
;
347 ret
= lttng_ht_del(consumer
->socks
, &iter
);
352 * RCU destroy call function.
354 static void destroy_socket_rcu(struct rcu_head
*head
)
356 struct lttng_ht_node_ulong
*node
=
357 caa_container_of(head
, struct lttng_ht_node_ulong
, head
);
358 struct consumer_socket
*socket
=
359 caa_container_of(node
, struct consumer_socket
, node
);
365 * Destroy and free socket pointer in a call RCU. Read side lock must be
366 * acquired before calling this function.
368 void consumer_destroy_socket(struct consumer_socket
*sock
)
373 * We DO NOT close the file descriptor here since it is global to the
374 * session daemon and is closed only if the consumer dies or a custom
375 * consumer was registered,
377 if (sock
->registered
) {
378 DBG3("Consumer socket was registered. Closing fd %d", sock
->fd
);
379 lttcomm_close_unix_sock(sock
->fd
);
382 call_rcu(&sock
->node
.head
, destroy_socket_rcu
);
386 * Allocate and assign data to a consumer_output object.
388 * Return pointer to structure.
390 struct consumer_output
*consumer_create_output(enum consumer_dst_type type
)
392 struct consumer_output
*output
= NULL
;
394 output
= zmalloc(sizeof(struct consumer_output
));
395 if (output
== NULL
) {
396 PERROR("zmalloc consumer_output");
400 /* By default, consumer output is enabled */
403 output
->net_seq_index
= (uint64_t) -1ULL;
405 output
->socks
= lttng_ht_new(0, LTTNG_HT_TYPE_ULONG
);
412 * Iterate over the consumer output socket hash table and destroy them. The
413 * socket file descriptor are only closed if the consumer output was
414 * registered meaning it's an external consumer.
416 void consumer_destroy_output_sockets(struct consumer_output
*obj
)
418 struct lttng_ht_iter iter
;
419 struct consumer_socket
*socket
;
426 cds_lfht_for_each_entry(obj
->socks
->ht
, &iter
.iter
, socket
, node
.node
) {
427 consumer_del_socket(socket
, obj
);
428 consumer_destroy_socket(socket
);
434 * Delete the consumer_output object from the list and free the ptr.
436 * Should *NOT* be called with RCU read-side lock held.
438 void consumer_destroy_output(struct consumer_output
*obj
)
444 consumer_destroy_output_sockets(obj
);
447 /* Finally destroy HT */
448 ht_cleanup_push(obj
->socks
);
455 * Copy consumer output and returned the newly allocated copy.
457 * Should *NOT* be called with RCU read-side lock held.
459 struct consumer_output
*consumer_copy_output(struct consumer_output
*obj
)
462 struct lttng_ht
*tmp_ht_ptr
;
463 struct consumer_output
*output
;
467 output
= consumer_create_output(obj
->type
);
468 if (output
== NULL
) {
471 /* Avoid losing the HT reference after the memcpy() */
472 tmp_ht_ptr
= output
->socks
;
474 memcpy(output
, obj
, sizeof(struct consumer_output
));
476 /* Putting back the HT pointer and start copying socket(s). */
477 output
->socks
= tmp_ht_ptr
;
479 ret
= consumer_copy_sockets(output
, obj
);
488 consumer_destroy_output(output
);
493 * Copy consumer sockets from src to dst.
495 * Return 0 on success or else a negative value.
497 int consumer_copy_sockets(struct consumer_output
*dst
,
498 struct consumer_output
*src
)
501 struct lttng_ht_iter iter
;
502 struct consumer_socket
*socket
, *copy_sock
;
508 cds_lfht_for_each_entry(src
->socks
->ht
, &iter
.iter
, socket
, node
.node
) {
509 /* Ignore socket that are already there. */
510 copy_sock
= consumer_find_socket(socket
->fd
, dst
);
515 /* Create new socket object. */
516 copy_sock
= consumer_allocate_socket(socket
->fd
);
517 if (copy_sock
== NULL
) {
523 copy_sock
->registered
= socket
->registered
;
525 * This is valid because this lock is shared accross all consumer
526 * object being the global lock of the consumer data structure of the
529 copy_sock
->lock
= socket
->lock
;
530 consumer_add_socket(copy_sock
, dst
);
539 * Set network URI to the consumer output object.
541 * Return 0 on success. Return 1 if the URI were equal. Else, negative value on
544 int consumer_set_network_uri(struct consumer_output
*obj
,
545 struct lttng_uri
*uri
)
548 char tmp_path
[PATH_MAX
];
549 char hostname
[HOST_NAME_MAX
];
550 struct lttng_uri
*dst_uri
= NULL
;
552 /* Code flow error safety net. */
556 switch (uri
->stype
) {
557 case LTTNG_STREAM_CONTROL
:
558 dst_uri
= &obj
->dst
.net
.control
;
559 obj
->dst
.net
.control_isset
= 1;
560 if (uri
->port
== 0) {
561 /* Assign default port. */
562 uri
->port
= DEFAULT_NETWORK_CONTROL_PORT
;
564 if (obj
->dst
.net
.data_isset
&& uri
->port
==
565 obj
->dst
.net
.data
.port
) {
566 ret
= -LTTNG_ERR_INVALID
;
570 DBG3("Consumer control URI set with port %d", uri
->port
);
572 case LTTNG_STREAM_DATA
:
573 dst_uri
= &obj
->dst
.net
.data
;
574 obj
->dst
.net
.data_isset
= 1;
575 if (uri
->port
== 0) {
576 /* Assign default port. */
577 uri
->port
= DEFAULT_NETWORK_DATA_PORT
;
579 if (obj
->dst
.net
.control_isset
&& uri
->port
==
580 obj
->dst
.net
.control
.port
) {
581 ret
= -LTTNG_ERR_INVALID
;
585 DBG3("Consumer data URI set with port %d", uri
->port
);
588 ERR("Set network uri type unknown %d", uri
->stype
);
589 ret
= -LTTNG_ERR_INVALID
;
593 ret
= uri_compare(dst_uri
, uri
);
595 /* Same URI, don't touch it and return success. */
596 DBG3("URI network compare are the same");
600 /* URIs were not equal, replacing it. */
601 memset(dst_uri
, 0, sizeof(struct lttng_uri
));
602 memcpy(dst_uri
, uri
, sizeof(struct lttng_uri
));
603 obj
->type
= CONSUMER_DST_NET
;
605 /* Handle subdir and add hostname in front. */
606 if (dst_uri
->stype
== LTTNG_STREAM_CONTROL
) {
607 /* Get hostname to append it in the pathname */
608 ret
= gethostname(hostname
, sizeof(hostname
));
610 PERROR("gethostname. Fallback on default localhost");
611 strncpy(hostname
, "localhost", sizeof(hostname
));
613 hostname
[sizeof(hostname
) - 1] = '\0';
615 /* Setup consumer subdir if none present in the control URI */
616 if (strlen(dst_uri
->subdir
) == 0) {
617 ret
= snprintf(tmp_path
, sizeof(tmp_path
), "%s/%s",
618 hostname
, obj
->subdir
);
620 ret
= snprintf(tmp_path
, sizeof(tmp_path
), "%s/%s",
621 hostname
, dst_uri
->subdir
);
624 PERROR("snprintf set consumer uri subdir");
625 ret
= -LTTNG_ERR_NOMEM
;
629 strncpy(obj
->subdir
, tmp_path
, sizeof(obj
->subdir
));
630 DBG3("Consumer set network uri subdir path %s", tmp_path
);
641 * Send file descriptor to consumer via sock.
643 int consumer_send_fds(struct consumer_socket
*sock
, int *fds
, size_t nb_fd
)
651 ret
= lttcomm_send_fds_unix_sock(sock
->fd
, fds
, nb_fd
);
653 /* The above call will print a PERROR on error. */
654 DBG("Error when sending consumer fds on sock %d", sock
->fd
);
658 ret
= consumer_recv_status_reply(sock
);
665 * Consumer send communication message structure to consumer.
667 int consumer_send_msg(struct consumer_socket
*sock
,
668 struct lttcomm_consumer_msg
*msg
)
674 assert(sock
->fd
>= 0);
676 ret
= lttcomm_send_unix_sock(sock
->fd
, msg
,
677 sizeof(struct lttcomm_consumer_msg
));
679 /* The above call will print a PERROR on error. */
680 DBG("Error when sending consumer channel on sock %d", sock
->fd
);
684 ret
= consumer_recv_status_reply(sock
);
691 * Consumer send channel communication message structure to consumer.
693 int consumer_send_channel(struct consumer_socket
*sock
,
694 struct lttcomm_consumer_msg
*msg
)
700 assert(sock
->fd
>= 0);
702 ret
= lttcomm_send_unix_sock(sock
->fd
, msg
,
703 sizeof(struct lttcomm_consumer_msg
));
705 /* The above call will print a PERROR on error. */
706 DBG("Error when sending consumer channel on sock %d", sock
->fd
);
710 ret
= consumer_recv_status_reply(sock
);
717 * Populate the given consumer msg structure with the ask_channel command
720 void consumer_init_ask_channel_comm_msg(struct lttcomm_consumer_msg
*msg
,
721 uint64_t subbuf_size
,
724 unsigned int switch_timer_interval
,
725 unsigned int read_timer_interval
,
729 const char *pathname
,
737 uint64_t tracefile_size
,
738 uint64_t tracefile_count
,
739 uint64_t session_id_per_pid
,
740 unsigned int monitor
)
744 /* Zeroed structure */
745 memset(msg
, 0, sizeof(struct lttcomm_consumer_msg
));
747 msg
->cmd_type
= LTTNG_CONSUMER_ASK_CHANNEL_CREATION
;
748 msg
->u
.ask_channel
.subbuf_size
= subbuf_size
;
749 msg
->u
.ask_channel
.num_subbuf
= num_subbuf
;
750 msg
->u
.ask_channel
.overwrite
= overwrite
;
751 msg
->u
.ask_channel
.switch_timer_interval
= switch_timer_interval
;
752 msg
->u
.ask_channel
.read_timer_interval
= read_timer_interval
;
753 msg
->u
.ask_channel
.output
= output
;
754 msg
->u
.ask_channel
.type
= type
;
755 msg
->u
.ask_channel
.session_id
= session_id
;
756 msg
->u
.ask_channel
.session_id_per_pid
= session_id_per_pid
;
757 msg
->u
.ask_channel
.uid
= uid
;
758 msg
->u
.ask_channel
.gid
= gid
;
759 msg
->u
.ask_channel
.relayd_id
= relayd_id
;
760 msg
->u
.ask_channel
.key
= key
;
761 msg
->u
.ask_channel
.chan_id
= chan_id
;
762 msg
->u
.ask_channel
.tracefile_size
= tracefile_size
;
763 msg
->u
.ask_channel
.tracefile_count
= tracefile_count
;
764 msg
->u
.ask_channel
.monitor
= monitor
;
766 memcpy(msg
->u
.ask_channel
.uuid
, uuid
, sizeof(msg
->u
.ask_channel
.uuid
));
769 strncpy(msg
->u
.ask_channel
.pathname
, pathname
,
770 sizeof(msg
->u
.ask_channel
.pathname
));
771 msg
->u
.ask_channel
.pathname
[sizeof(msg
->u
.ask_channel
.pathname
)-1] = '\0';
774 strncpy(msg
->u
.ask_channel
.name
, name
, sizeof(msg
->u
.ask_channel
.name
));
775 msg
->u
.ask_channel
.name
[sizeof(msg
->u
.ask_channel
.name
) - 1] = '\0';
779 * Init channel communication message structure.
781 void consumer_init_channel_comm_msg(struct lttcomm_consumer_msg
*msg
,
782 enum lttng_consumer_command cmd
,
783 uint64_t channel_key
,
785 const char *pathname
,
790 unsigned int nb_init_streams
,
791 enum lttng_event_output output
,
793 uint64_t tracefile_size
,
794 uint64_t tracefile_count
,
795 unsigned int monitor
)
799 /* Zeroed structure */
800 memset(msg
, 0, sizeof(struct lttcomm_consumer_msg
));
804 msg
->u
.channel
.channel_key
= channel_key
;
805 msg
->u
.channel
.session_id
= session_id
;
806 msg
->u
.channel
.uid
= uid
;
807 msg
->u
.channel
.gid
= gid
;
808 msg
->u
.channel
.relayd_id
= relayd_id
;
809 msg
->u
.channel
.nb_init_streams
= nb_init_streams
;
810 msg
->u
.channel
.output
= output
;
811 msg
->u
.channel
.type
= type
;
812 msg
->u
.channel
.tracefile_size
= tracefile_size
;
813 msg
->u
.channel
.tracefile_count
= tracefile_count
;
814 msg
->u
.channel
.monitor
= monitor
;
816 strncpy(msg
->u
.channel
.pathname
, pathname
,
817 sizeof(msg
->u
.channel
.pathname
));
818 msg
->u
.channel
.pathname
[sizeof(msg
->u
.channel
.pathname
) - 1] = '\0';
820 strncpy(msg
->u
.channel
.name
, name
, sizeof(msg
->u
.channel
.name
));
821 msg
->u
.channel
.name
[sizeof(msg
->u
.channel
.name
) - 1] = '\0';
825 * Init stream communication message structure.
827 void consumer_init_stream_comm_msg(struct lttcomm_consumer_msg
*msg
,
828 enum lttng_consumer_command cmd
,
829 uint64_t channel_key
,
835 memset(msg
, 0, sizeof(struct lttcomm_consumer_msg
));
838 msg
->u
.stream
.channel_key
= channel_key
;
839 msg
->u
.stream
.stream_key
= stream_key
;
840 msg
->u
.stream
.cpu
= cpu
;
844 * Send stream communication structure to the consumer.
846 int consumer_send_stream(struct consumer_socket
*sock
,
847 struct consumer_output
*dst
, struct lttcomm_consumer_msg
*msg
,
848 int *fds
, size_t nb_fd
)
858 ret
= lttcomm_send_unix_sock(sock
->fd
, msg
,
859 sizeof(struct lttcomm_consumer_msg
));
861 /* The above call will print a PERROR on error. */
862 DBG("Error when sending consumer stream on sock %d", sock
->fd
);
866 ret
= consumer_recv_status_reply(sock
);
871 ret
= consumer_send_fds(sock
, fds
, nb_fd
);
881 * Send relayd socket to consumer associated with a session name.
883 * On success return positive value. On error, negative value.
885 int consumer_send_relayd_socket(struct consumer_socket
*consumer_sock
,
886 struct lttcomm_relayd_sock
*rsock
, struct consumer_output
*consumer
,
887 enum lttng_stream_type type
, uint64_t session_id
)
890 struct lttcomm_consumer_msg msg
;
892 /* Code flow error. Safety net. */
895 assert(consumer_sock
);
897 /* Bail out if consumer is disabled */
898 if (!consumer
->enabled
) {
903 msg
.cmd_type
= LTTNG_CONSUMER_ADD_RELAYD_SOCKET
;
905 * Assign network consumer output index using the temporary consumer since
906 * this call should only be made from within a set_consumer_uri() function
907 * call in the session daemon.
909 msg
.u
.relayd_sock
.net_index
= consumer
->net_seq_index
;
910 msg
.u
.relayd_sock
.type
= type
;
911 msg
.u
.relayd_sock
.session_id
= session_id
;
912 memcpy(&msg
.u
.relayd_sock
.sock
, rsock
, sizeof(msg
.u
.relayd_sock
.sock
));
914 DBG3("Sending relayd sock info to consumer on %d", consumer_sock
->fd
);
915 ret
= lttcomm_send_unix_sock(consumer_sock
->fd
, &msg
, sizeof(msg
));
917 /* The above call will print a PERROR on error. */
918 DBG("Error when sending relayd sockets on sock %d", rsock
->sock
.fd
);
922 ret
= consumer_recv_status_reply(consumer_sock
);
927 DBG3("Sending relayd socket file descriptor to consumer");
928 ret
= consumer_send_fds(consumer_sock
, &rsock
->sock
.fd
, 1);
933 DBG2("Consumer relayd socket sent");
940 * Set consumer subdirectory using the session name and a generated datetime if
941 * needed. This is appended to the current subdirectory.
943 int consumer_set_subdir(struct consumer_output
*consumer
,
944 const char *session_name
)
947 unsigned int have_default_name
= 0;
948 char datetime
[16], tmp_path
[PATH_MAX
];
953 assert(session_name
);
955 memset(tmp_path
, 0, sizeof(tmp_path
));
957 /* Flag if we have a default session. */
958 if (strncmp(session_name
, DEFAULT_SESSION_NAME
"-",
959 strlen(DEFAULT_SESSION_NAME
) + 1) == 0) {
960 have_default_name
= 1;
962 /* Get date and time for session path */
964 timeinfo
= localtime(&rawtime
);
965 strftime(datetime
, sizeof(datetime
), "%Y%m%d-%H%M%S", timeinfo
);
968 if (have_default_name
) {
969 ret
= snprintf(tmp_path
, sizeof(tmp_path
),
970 "%s/%s", consumer
->subdir
, session_name
);
972 ret
= snprintf(tmp_path
, sizeof(tmp_path
),
973 "%s/%s-%s/", consumer
->subdir
, session_name
, datetime
);
976 PERROR("snprintf session name date");
980 strncpy(consumer
->subdir
, tmp_path
, sizeof(consumer
->subdir
));
981 DBG2("Consumer subdir set to %s", consumer
->subdir
);
988 * Ask the consumer if the data is ready to read (NOT pending) for the specific
991 * This function has a different behavior with the consumer i.e. that it waits
992 * for a reply from the consumer if yes or no the data is pending.
994 int consumer_is_data_pending(uint64_t session_id
,
995 struct consumer_output
*consumer
)
998 int32_t ret_code
= 0; /* Default is that the data is NOT pending */
999 struct consumer_socket
*socket
;
1000 struct lttng_ht_iter iter
;
1001 struct lttcomm_consumer_msg msg
;
1005 msg
.cmd_type
= LTTNG_CONSUMER_DATA_PENDING
;
1007 msg
.u
.data_pending
.session_id
= session_id
;
1009 DBG3("Consumer data pending for id %" PRIu64
, session_id
);
1011 /* Send command for each consumer */
1013 cds_lfht_for_each_entry(consumer
->socks
->ht
, &iter
.iter
, socket
,
1015 /* Code flow error */
1016 assert(socket
->fd
>= 0);
1018 pthread_mutex_lock(socket
->lock
);
1020 ret
= lttcomm_send_unix_sock(socket
->fd
, &msg
, sizeof(msg
));
1022 /* The above call will print a PERROR on error. */
1023 DBG("Error on consumer is data pending on sock %d", socket
->fd
);
1024 pthread_mutex_unlock(socket
->lock
);
1029 * No need for a recv reply status because the answer to the command is
1030 * the reply status message.
1033 ret
= lttcomm_recv_unix_sock(socket
->fd
, &ret_code
, sizeof(ret_code
));
1036 /* Orderly shutdown. Don't return 0 which means success. */
1039 /* The above call will print a PERROR on error. */
1040 DBG("Error on recv consumer is data pending on sock %d", socket
->fd
);
1041 pthread_mutex_unlock(socket
->lock
);
1045 pthread_mutex_unlock(socket
->lock
);
1047 if (ret_code
== 1) {
1053 DBG("Consumer data is %s pending for session id %" PRIu64
,
1054 ret_code
== 1 ? "" : "NOT", session_id
);
1063 * Send a flush command to consumer using the given channel key.
1065 * Return 0 on success else a negative value.
1067 int consumer_flush_channel(struct consumer_socket
*socket
, uint64_t key
)
1070 struct lttcomm_consumer_msg msg
;
1073 assert(socket
->fd
>= 0);
1075 DBG2("Consumer flush channel key %" PRIu64
, key
);
1077 msg
.cmd_type
= LTTNG_CONSUMER_FLUSH_CHANNEL
;
1078 msg
.u
.flush_channel
.key
= key
;
1080 pthread_mutex_lock(socket
->lock
);
1081 health_code_update();
1083 ret
= consumer_send_msg(socket
, &msg
);
1089 health_code_update();
1090 pthread_mutex_unlock(socket
->lock
);
1095 * Send a close metdata command to consumer using the given channel key.
1097 * Return 0 on success else a negative value.
1099 int consumer_close_metadata(struct consumer_socket
*socket
,
1100 uint64_t metadata_key
)
1103 struct lttcomm_consumer_msg msg
;
1106 assert(socket
->fd
>= 0);
1108 DBG2("Consumer close metadata channel key %" PRIu64
, metadata_key
);
1110 msg
.cmd_type
= LTTNG_CONSUMER_CLOSE_METADATA
;
1111 msg
.u
.close_metadata
.key
= metadata_key
;
1113 pthread_mutex_lock(socket
->lock
);
1114 health_code_update();
1116 ret
= consumer_send_msg(socket
, &msg
);
1122 health_code_update();
1123 pthread_mutex_unlock(socket
->lock
);
1128 * Send a setup metdata command to consumer using the given channel key.
1130 * Return 0 on success else a negative value.
1132 int consumer_setup_metadata(struct consumer_socket
*socket
,
1133 uint64_t metadata_key
)
1136 struct lttcomm_consumer_msg msg
;
1139 assert(socket
->fd
>= 0);
1141 DBG2("Consumer setup metadata channel key %" PRIu64
, metadata_key
);
1143 msg
.cmd_type
= LTTNG_CONSUMER_SETUP_METADATA
;
1144 msg
.u
.setup_metadata
.key
= metadata_key
;
1146 pthread_mutex_lock(socket
->lock
);
1147 health_code_update();
1149 ret
= consumer_send_msg(socket
, &msg
);
1155 health_code_update();
1156 pthread_mutex_unlock(socket
->lock
);
1161 * Send metadata string to consumer. Socket lock MUST be acquired.
1163 * Return 0 on success else a negative value.
1165 int consumer_push_metadata(struct consumer_socket
*socket
,
1166 uint64_t metadata_key
, char *metadata_str
, size_t len
,
1167 size_t target_offset
)
1170 struct lttcomm_consumer_msg msg
;
1173 assert(socket
->fd
>= 0);
1175 DBG2("Consumer push metadata to consumer socket %d", socket
->fd
);
1177 msg
.cmd_type
= LTTNG_CONSUMER_PUSH_METADATA
;
1178 msg
.u
.push_metadata
.key
= metadata_key
;
1179 msg
.u
.push_metadata
.target_offset
= target_offset
;
1180 msg
.u
.push_metadata
.len
= len
;
1182 health_code_update();
1183 ret
= consumer_send_msg(socket
, &msg
);
1184 if (ret
< 0 || len
== 0) {
1188 DBG3("Consumer pushing metadata on sock %d of len %zu", socket
->fd
, len
);
1190 ret
= lttcomm_send_unix_sock(socket
->fd
, metadata_str
, len
);
1195 health_code_update();
1196 ret
= consumer_recv_status_reply(socket
);
1202 health_code_update();
1207 * Ask the consumer to snapshot a specific channel using the key.
1209 * Return 0 on success or else a negative error.
1211 int consumer_snapshot_channel(struct consumer_socket
*socket
, uint64_t key
,
1212 struct snapshot_output
*output
, int metadata
, uid_t uid
, gid_t gid
,
1213 const char *session_path
, int wait
, int max_stream_size
)
1216 struct lttcomm_consumer_msg msg
;
1219 assert(socket
->fd
>= 0);
1221 assert(output
->consumer
);
1223 DBG("Consumer snapshot channel key %" PRIu64
, key
);
1225 memset(&msg
, 0, sizeof(msg
));
1226 msg
.cmd_type
= LTTNG_CONSUMER_SNAPSHOT_CHANNEL
;
1227 msg
.u
.snapshot_channel
.key
= key
;
1228 msg
.u
.snapshot_channel
.max_stream_size
= max_stream_size
;
1229 msg
.u
.snapshot_channel
.metadata
= metadata
;
1231 if (output
->consumer
->type
== CONSUMER_DST_NET
) {
1232 msg
.u
.snapshot_channel
.relayd_id
= output
->consumer
->net_seq_index
;
1233 msg
.u
.snapshot_channel
.use_relayd
= 1;
1234 ret
= snprintf(msg
.u
.snapshot_channel
.pathname
,
1235 sizeof(msg
.u
.snapshot_channel
.pathname
),
1236 "%s/%s-%s-%" PRIu64
"%s", output
->consumer
->subdir
,
1237 output
->name
, output
->datetime
, output
->nb_snapshot
,
1240 ret
= -LTTNG_ERR_NOMEM
;
1244 ret
= snprintf(msg
.u
.snapshot_channel
.pathname
,
1245 sizeof(msg
.u
.snapshot_channel
.pathname
),
1246 "%s/%s-%s-%" PRIu64
"%s", output
->consumer
->dst
.trace_path
,
1247 output
->name
, output
->datetime
, output
->nb_snapshot
,
1250 ret
= -LTTNG_ERR_NOMEM
;
1253 msg
.u
.snapshot_channel
.relayd_id
= (uint64_t) -1ULL;
1255 /* Create directory. Ignore if exist. */
1256 ret
= run_as_mkdir_recursive(msg
.u
.snapshot_channel
.pathname
,
1257 S_IRWXU
| S_IRWXG
, uid
, gid
);
1259 if (ret
!= -EEXIST
) {
1260 ERR("Trace directory creation error");
1266 health_code_update();
1267 ret
= consumer_send_msg(socket
, &msg
);
1273 health_code_update();