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>
31 #include <common/utils.h>
39 * Receive a reply command status message from the consumer. Consumer socket
40 * lock MUST be acquired before calling this function.
42 * Return 0 on success, -1 on recv error or a negative lttng error code which
43 * was possibly returned by the consumer.
45 int consumer_recv_status_reply(struct consumer_socket
*sock
)
48 struct lttcomm_consumer_status_msg reply
;
52 ret
= lttcomm_recv_unix_sock(sock
->fd
, &reply
, sizeof(reply
));
55 /* Orderly shutdown. Don't return 0 which means success. */
58 /* The above call will print a PERROR on error. */
59 DBG("Fail to receive status reply on sock %d", sock
->fd
);
63 if (reply
.ret_code
== LTTNG_OK
) {
67 ret
= -reply
.ret_code
;
68 DBG("Consumer ret code %d", ret
);
76 * Once the ASK_CHANNEL command is sent to the consumer, the channel
77 * information are sent back. This call receives that data and populates key
80 * On success return 0 and both key and stream_count are set. On error, a
81 * negative value is sent back and both parameters are untouched.
83 int consumer_recv_status_channel(struct consumer_socket
*sock
,
84 uint64_t *key
, unsigned int *stream_count
)
87 struct lttcomm_consumer_status_channel reply
;
93 ret
= lttcomm_recv_unix_sock(sock
->fd
, &reply
, sizeof(reply
));
96 /* Orderly shutdown. Don't return 0 which means success. */
99 /* The above call will print a PERROR on error. */
100 DBG("Fail to receive status reply on sock %d", sock
->fd
);
104 /* An error is possible so don't touch the key and stream_count. */
105 if (reply
.ret_code
!= LTTNG_OK
) {
111 *stream_count
= reply
.stream_count
;
118 * Send destroy relayd command to consumer.
120 * On success return positive value. On error, negative value.
122 int consumer_send_destroy_relayd(struct consumer_socket
*sock
,
123 struct consumer_output
*consumer
)
126 struct lttcomm_consumer_msg msg
;
131 DBG2("Sending destroy relayd command to consumer sock %d", sock
->fd
);
133 /* Bail out if consumer is disabled */
134 if (!consumer
->enabled
) {
136 DBG3("Consumer is disabled");
140 msg
.cmd_type
= LTTNG_CONSUMER_DESTROY_RELAYD
;
141 msg
.u
.destroy_relayd
.net_seq_idx
= consumer
->net_seq_index
;
143 pthread_mutex_lock(sock
->lock
);
144 ret
= lttcomm_send_unix_sock(sock
->fd
, &msg
, sizeof(msg
));
146 /* Indicate that the consumer is probably closing at this point. */
147 DBG("send consumer destroy relayd command");
151 /* Don't check the return value. The caller will do it. */
152 ret
= consumer_recv_status_reply(sock
);
154 DBG2("Consumer send destroy relayd command done");
157 pthread_mutex_unlock(sock
->lock
);
163 * For each consumer socket in the consumer output object, send a destroy
166 void consumer_output_send_destroy_relayd(struct consumer_output
*consumer
)
168 struct lttng_ht_iter iter
;
169 struct consumer_socket
*socket
;
173 /* Destroy any relayd connection */
174 if (consumer
->type
== CONSUMER_DST_NET
) {
176 cds_lfht_for_each_entry(consumer
->socks
->ht
, &iter
.iter
, socket
,
180 /* Send destroy relayd command */
181 ret
= consumer_send_destroy_relayd(socket
, consumer
);
183 DBG("Unable to send destroy relayd command to consumer");
184 /* Continue since we MUST delete everything at this point. */
192 * From a consumer_data structure, allocate and add a consumer socket to the
195 * Return 0 on success, else negative value on error
197 int consumer_create_socket(struct consumer_data
*data
,
198 struct consumer_output
*output
)
201 struct consumer_socket
*socket
;
205 if (output
== NULL
|| data
->cmd_sock
< 0) {
207 * Not an error. Possible there is simply not spawned consumer or it's
208 * disabled for the tracing session asking the socket.
214 socket
= consumer_find_socket(data
->cmd_sock
, output
);
216 if (socket
== NULL
) {
217 socket
= consumer_allocate_socket(data
->cmd_sock
);
218 if (socket
== NULL
) {
223 socket
->registered
= 0;
224 socket
->lock
= &data
->lock
;
226 consumer_add_socket(socket
, output
);
230 socket
->type
= data
->type
;
232 DBG3("Consumer socket created (fd: %d) and added to output",
240 * Return the consumer socket from the given consumer output with the right
241 * bitness. On error, returns NULL.
243 * The caller MUST acquire a rcu read side lock and keep it until the socket
244 * object reference is not needed anymore.
246 struct consumer_socket
*consumer_find_socket_by_bitness(int bits
,
247 struct consumer_output
*consumer
)
250 struct consumer_socket
*socket
= NULL
;
254 consumer_fd
= uatomic_read(&ust_consumerd64_fd
);
257 consumer_fd
= uatomic_read(&ust_consumerd32_fd
);
264 socket
= consumer_find_socket(consumer_fd
, consumer
);
266 ERR("Consumer socket fd %d not found in consumer obj %p",
267 consumer_fd
, consumer
);
275 * Find a consumer_socket in a consumer_output hashtable. Read side lock must
276 * be acquired before calling this function and across use of the
277 * returned consumer_socket.
279 struct consumer_socket
*consumer_find_socket(int key
,
280 struct consumer_output
*consumer
)
282 struct lttng_ht_iter iter
;
283 struct lttng_ht_node_ulong
*node
;
284 struct consumer_socket
*socket
= NULL
;
286 /* Negative keys are lookup failures */
287 if (key
< 0 || consumer
== NULL
) {
291 lttng_ht_lookup(consumer
->socks
, (void *)((unsigned long) key
),
293 node
= lttng_ht_iter_get_node_ulong(&iter
);
295 socket
= caa_container_of(node
, struct consumer_socket
, node
);
302 * Allocate a new consumer_socket and return the pointer.
304 struct consumer_socket
*consumer_allocate_socket(int fd
)
306 struct consumer_socket
*socket
= NULL
;
308 socket
= zmalloc(sizeof(struct consumer_socket
));
309 if (socket
== NULL
) {
310 PERROR("zmalloc consumer socket");
315 lttng_ht_node_init_ulong(&socket
->node
, fd
);
322 * Add consumer socket to consumer output object. Read side lock must be
323 * acquired before calling this function.
325 void consumer_add_socket(struct consumer_socket
*sock
,
326 struct consumer_output
*consumer
)
331 lttng_ht_add_unique_ulong(consumer
->socks
, &sock
->node
);
335 * Delte consumer socket to consumer output object. Read side lock must be
336 * acquired before calling this function.
338 void consumer_del_socket(struct consumer_socket
*sock
,
339 struct consumer_output
*consumer
)
342 struct lttng_ht_iter iter
;
347 iter
.iter
.node
= &sock
->node
.node
;
348 ret
= lttng_ht_del(consumer
->socks
, &iter
);
353 * RCU destroy call function.
355 static void destroy_socket_rcu(struct rcu_head
*head
)
357 struct lttng_ht_node_ulong
*node
=
358 caa_container_of(head
, struct lttng_ht_node_ulong
, head
);
359 struct consumer_socket
*socket
=
360 caa_container_of(node
, struct consumer_socket
, node
);
366 * Destroy and free socket pointer in a call RCU. Read side lock must be
367 * acquired before calling this function.
369 void consumer_destroy_socket(struct consumer_socket
*sock
)
374 * We DO NOT close the file descriptor here since it is global to the
375 * session daemon and is closed only if the consumer dies or a custom
376 * consumer was registered,
378 if (sock
->registered
) {
379 DBG3("Consumer socket was registered. Closing fd %d", sock
->fd
);
380 lttcomm_close_unix_sock(sock
->fd
);
383 call_rcu(&sock
->node
.head
, destroy_socket_rcu
);
387 * Allocate and assign data to a consumer_output object.
389 * Return pointer to structure.
391 struct consumer_output
*consumer_create_output(enum consumer_dst_type type
)
393 struct consumer_output
*output
= NULL
;
395 output
= zmalloc(sizeof(struct consumer_output
));
396 if (output
== NULL
) {
397 PERROR("zmalloc consumer_output");
401 /* By default, consumer output is enabled */
404 output
->net_seq_index
= (uint64_t) -1ULL;
406 output
->socks
= lttng_ht_new(0, LTTNG_HT_TYPE_ULONG
);
413 * Delete the consumer_output object from the list and free the ptr.
415 * Should *NOT* be called with RCU read-side lock held.
417 void consumer_destroy_output(struct consumer_output
*obj
)
424 struct lttng_ht_iter iter
;
425 struct consumer_socket
*socket
;
428 cds_lfht_for_each_entry(obj
->socks
->ht
, &iter
.iter
, socket
, node
.node
) {
429 consumer_del_socket(socket
, obj
);
430 consumer_destroy_socket(socket
);
434 /* Finally destroy HT */
435 ht_cleanup_push(obj
->socks
);
442 * Copy consumer output and returned the newly allocated copy.
444 * Should *NOT* be called with RCU read-side lock held.
446 struct consumer_output
*consumer_copy_output(struct consumer_output
*obj
)
449 struct lttng_ht
*tmp_ht_ptr
;
450 struct consumer_output
*output
;
454 output
= consumer_create_output(obj
->type
);
455 if (output
== NULL
) {
458 /* Avoid losing the HT reference after the memcpy() */
459 tmp_ht_ptr
= output
->socks
;
461 memcpy(output
, obj
, sizeof(struct consumer_output
));
463 /* Putting back the HT pointer and start copying socket(s). */
464 output
->socks
= tmp_ht_ptr
;
466 ret
= consumer_copy_sockets(output
, obj
);
475 consumer_destroy_output(output
);
480 * Copy consumer sockets from src to dst.
482 * Return 0 on success or else a negative value.
484 int consumer_copy_sockets(struct consumer_output
*dst
,
485 struct consumer_output
*src
)
488 struct lttng_ht_iter iter
;
489 struct consumer_socket
*socket
, *copy_sock
;
495 cds_lfht_for_each_entry(src
->socks
->ht
, &iter
.iter
, socket
, node
.node
) {
496 /* Ignore socket that are already there. */
497 copy_sock
= consumer_find_socket(socket
->fd
, dst
);
502 /* Create new socket object. */
503 copy_sock
= consumer_allocate_socket(socket
->fd
);
504 if (copy_sock
== NULL
) {
510 copy_sock
->registered
= socket
->registered
;
512 * This is valid because this lock is shared accross all consumer
513 * object being the global lock of the consumer data structure of the
516 copy_sock
->lock
= socket
->lock
;
517 consumer_add_socket(copy_sock
, dst
);
526 * Set network URI to the consumer output object.
528 * Return 0 on success. Return 1 if the URI were equal. Else, negative value on
531 int consumer_set_network_uri(struct consumer_output
*obj
,
532 struct lttng_uri
*uri
)
535 char tmp_path
[PATH_MAX
];
536 char hostname
[HOST_NAME_MAX
];
537 struct lttng_uri
*dst_uri
= NULL
;
539 /* Code flow error safety net. */
543 switch (uri
->stype
) {
544 case LTTNG_STREAM_CONTROL
:
545 dst_uri
= &obj
->dst
.net
.control
;
546 obj
->dst
.net
.control_isset
= 1;
547 if (uri
->port
== 0) {
548 /* Assign default port. */
549 uri
->port
= DEFAULT_NETWORK_CONTROL_PORT
;
551 if (obj
->dst
.net
.data_isset
&& uri
->port
==
552 obj
->dst
.net
.data
.port
) {
553 ret
= -LTTNG_ERR_INVALID
;
557 DBG3("Consumer control URI set with port %d", uri
->port
);
559 case LTTNG_STREAM_DATA
:
560 dst_uri
= &obj
->dst
.net
.data
;
561 obj
->dst
.net
.data_isset
= 1;
562 if (uri
->port
== 0) {
563 /* Assign default port. */
564 uri
->port
= DEFAULT_NETWORK_DATA_PORT
;
566 if (obj
->dst
.net
.control_isset
&& uri
->port
==
567 obj
->dst
.net
.control
.port
) {
568 ret
= -LTTNG_ERR_INVALID
;
572 DBG3("Consumer data URI set with port %d", uri
->port
);
575 ERR("Set network uri type unknown %d", uri
->stype
);
576 ret
= -LTTNG_ERR_INVALID
;
580 ret
= uri_compare(dst_uri
, uri
);
582 /* Same URI, don't touch it and return success. */
583 DBG3("URI network compare are the same");
587 /* URIs were not equal, replacing it. */
588 memset(dst_uri
, 0, sizeof(struct lttng_uri
));
589 memcpy(dst_uri
, uri
, sizeof(struct lttng_uri
));
590 obj
->type
= CONSUMER_DST_NET
;
592 /* Handle subdir and add hostname in front. */
593 if (dst_uri
->stype
== LTTNG_STREAM_CONTROL
) {
594 /* Get hostname to append it in the pathname */
595 ret
= gethostname(hostname
, sizeof(hostname
));
597 PERROR("gethostname. Fallback on default localhost");
598 strncpy(hostname
, "localhost", sizeof(hostname
));
600 hostname
[sizeof(hostname
) - 1] = '\0';
602 /* Setup consumer subdir if none present in the control URI */
603 if (strlen(dst_uri
->subdir
) == 0) {
604 ret
= snprintf(tmp_path
, sizeof(tmp_path
), "%s/%s",
605 hostname
, obj
->subdir
);
607 ret
= snprintf(tmp_path
, sizeof(tmp_path
), "%s/%s",
608 hostname
, dst_uri
->subdir
);
611 PERROR("snprintf set consumer uri subdir");
612 ret
= -LTTNG_ERR_NOMEM
;
616 strncpy(obj
->subdir
, tmp_path
, sizeof(obj
->subdir
));
617 DBG3("Consumer set network uri subdir path %s", tmp_path
);
628 * Send file descriptor to consumer via sock.
630 int consumer_send_fds(struct consumer_socket
*sock
, int *fds
, size_t nb_fd
)
638 ret
= lttcomm_send_fds_unix_sock(sock
->fd
, fds
, nb_fd
);
640 /* The above call will print a PERROR on error. */
641 DBG("Error when sending consumer fds on sock %d", sock
->fd
);
645 ret
= consumer_recv_status_reply(sock
);
652 * Consumer send communication message structure to consumer.
654 int consumer_send_msg(struct consumer_socket
*sock
,
655 struct lttcomm_consumer_msg
*msg
)
661 assert(sock
->fd
>= 0);
663 ret
= lttcomm_send_unix_sock(sock
->fd
, msg
,
664 sizeof(struct lttcomm_consumer_msg
));
666 /* The above call will print a PERROR on error. */
667 DBG("Error when sending consumer channel on sock %d", sock
->fd
);
671 ret
= consumer_recv_status_reply(sock
);
678 * Consumer send channel communication message structure to consumer.
680 int consumer_send_channel(struct consumer_socket
*sock
,
681 struct lttcomm_consumer_msg
*msg
)
687 assert(sock
->fd
>= 0);
689 ret
= lttcomm_send_unix_sock(sock
->fd
, msg
,
690 sizeof(struct lttcomm_consumer_msg
));
692 /* The above call will print a PERROR on error. */
693 DBG("Error when sending consumer channel on sock %d", sock
->fd
);
697 ret
= consumer_recv_status_reply(sock
);
704 * Populate the given consumer msg structure with the ask_channel command
707 void consumer_init_ask_channel_comm_msg(struct lttcomm_consumer_msg
*msg
,
708 uint64_t subbuf_size
,
711 unsigned int switch_timer_interval
,
712 unsigned int read_timer_interval
,
716 const char *pathname
,
724 uint64_t tracefile_size
,
725 uint64_t tracefile_count
,
726 uint64_t session_id_per_pid
,
727 unsigned int monitor
)
731 /* Zeroed structure */
732 memset(msg
, 0, sizeof(struct lttcomm_consumer_msg
));
734 msg
->cmd_type
= LTTNG_CONSUMER_ASK_CHANNEL_CREATION
;
735 msg
->u
.ask_channel
.subbuf_size
= subbuf_size
;
736 msg
->u
.ask_channel
.num_subbuf
= num_subbuf
;
737 msg
->u
.ask_channel
.overwrite
= overwrite
;
738 msg
->u
.ask_channel
.switch_timer_interval
= switch_timer_interval
;
739 msg
->u
.ask_channel
.read_timer_interval
= read_timer_interval
;
740 msg
->u
.ask_channel
.output
= output
;
741 msg
->u
.ask_channel
.type
= type
;
742 msg
->u
.ask_channel
.session_id
= session_id
;
743 msg
->u
.ask_channel
.session_id_per_pid
= session_id_per_pid
;
744 msg
->u
.ask_channel
.uid
= uid
;
745 msg
->u
.ask_channel
.gid
= gid
;
746 msg
->u
.ask_channel
.relayd_id
= relayd_id
;
747 msg
->u
.ask_channel
.key
= key
;
748 msg
->u
.ask_channel
.chan_id
= chan_id
;
749 msg
->u
.ask_channel
.tracefile_size
= tracefile_size
;
750 msg
->u
.ask_channel
.tracefile_count
= tracefile_count
;
751 msg
->u
.ask_channel
.monitor
= monitor
;
753 memcpy(msg
->u
.ask_channel
.uuid
, uuid
, sizeof(msg
->u
.ask_channel
.uuid
));
755 strncpy(msg
->u
.ask_channel
.pathname
, pathname
,
756 sizeof(msg
->u
.ask_channel
.pathname
));
757 msg
->u
.ask_channel
.pathname
[sizeof(msg
->u
.ask_channel
.pathname
)-1] = '\0';
759 strncpy(msg
->u
.ask_channel
.name
, name
, sizeof(msg
->u
.ask_channel
.name
));
760 msg
->u
.ask_channel
.name
[sizeof(msg
->u
.ask_channel
.name
) - 1] = '\0';
764 * Init channel communication message structure.
766 void consumer_init_channel_comm_msg(struct lttcomm_consumer_msg
*msg
,
767 enum lttng_consumer_command cmd
,
768 uint64_t channel_key
,
770 const char *pathname
,
775 unsigned int nb_init_streams
,
776 enum lttng_event_output output
,
778 uint64_t tracefile_size
,
779 uint64_t tracefile_count
,
780 unsigned int monitor
)
784 /* Zeroed structure */
785 memset(msg
, 0, sizeof(struct lttcomm_consumer_msg
));
789 msg
->u
.channel
.channel_key
= channel_key
;
790 msg
->u
.channel
.session_id
= session_id
;
791 msg
->u
.channel
.uid
= uid
;
792 msg
->u
.channel
.gid
= gid
;
793 msg
->u
.channel
.relayd_id
= relayd_id
;
794 msg
->u
.channel
.nb_init_streams
= nb_init_streams
;
795 msg
->u
.channel
.output
= output
;
796 msg
->u
.channel
.type
= type
;
797 msg
->u
.channel
.tracefile_size
= tracefile_size
;
798 msg
->u
.channel
.tracefile_count
= tracefile_count
;
799 msg
->u
.channel
.monitor
= monitor
;
801 strncpy(msg
->u
.channel
.pathname
, pathname
,
802 sizeof(msg
->u
.channel
.pathname
));
803 msg
->u
.channel
.pathname
[sizeof(msg
->u
.channel
.pathname
) - 1] = '\0';
805 strncpy(msg
->u
.channel
.name
, name
, sizeof(msg
->u
.channel
.name
));
806 msg
->u
.channel
.name
[sizeof(msg
->u
.channel
.name
) - 1] = '\0';
810 * Init stream communication message structure.
812 void consumer_init_stream_comm_msg(struct lttcomm_consumer_msg
*msg
,
813 enum lttng_consumer_command cmd
,
814 uint64_t channel_key
,
820 memset(msg
, 0, sizeof(struct lttcomm_consumer_msg
));
823 msg
->u
.stream
.channel_key
= channel_key
;
824 msg
->u
.stream
.stream_key
= stream_key
;
825 msg
->u
.stream
.cpu
= cpu
;
829 * Send stream communication structure to the consumer.
831 int consumer_send_stream(struct consumer_socket
*sock
,
832 struct consumer_output
*dst
, struct lttcomm_consumer_msg
*msg
,
833 int *fds
, size_t nb_fd
)
843 ret
= lttcomm_send_unix_sock(sock
->fd
, msg
,
844 sizeof(struct lttcomm_consumer_msg
));
846 /* The above call will print a PERROR on error. */
847 DBG("Error when sending consumer stream on sock %d", sock
->fd
);
851 ret
= consumer_recv_status_reply(sock
);
856 ret
= consumer_send_fds(sock
, fds
, nb_fd
);
866 * Send relayd socket to consumer associated with a session name.
868 * On success return positive value. On error, negative value.
870 int consumer_send_relayd_socket(struct consumer_socket
*consumer_sock
,
871 struct lttcomm_relayd_sock
*rsock
, struct consumer_output
*consumer
,
872 enum lttng_stream_type type
, uint64_t session_id
)
875 struct lttcomm_consumer_msg msg
;
877 /* Code flow error. Safety net. */
880 assert(consumer_sock
);
882 /* Bail out if consumer is disabled */
883 if (!consumer
->enabled
) {
888 msg
.cmd_type
= LTTNG_CONSUMER_ADD_RELAYD_SOCKET
;
890 * Assign network consumer output index using the temporary consumer since
891 * this call should only be made from within a set_consumer_uri() function
892 * call in the session daemon.
894 msg
.u
.relayd_sock
.net_index
= consumer
->net_seq_index
;
895 msg
.u
.relayd_sock
.type
= type
;
896 msg
.u
.relayd_sock
.session_id
= session_id
;
897 memcpy(&msg
.u
.relayd_sock
.sock
, rsock
, sizeof(msg
.u
.relayd_sock
.sock
));
899 DBG3("Sending relayd sock info to consumer on %d", consumer_sock
->fd
);
900 ret
= lttcomm_send_unix_sock(consumer_sock
->fd
, &msg
, sizeof(msg
));
902 /* The above call will print a PERROR on error. */
903 DBG("Error when sending relayd sockets on sock %d", rsock
->sock
.fd
);
907 ret
= consumer_recv_status_reply(consumer_sock
);
912 DBG3("Sending relayd socket file descriptor to consumer");
913 ret
= consumer_send_fds(consumer_sock
, &rsock
->sock
.fd
, 1);
918 DBG2("Consumer relayd socket sent");
925 * Set consumer subdirectory using the session name and a generated datetime if
926 * needed. This is appended to the current subdirectory.
928 int consumer_set_subdir(struct consumer_output
*consumer
,
929 const char *session_name
)
932 unsigned int have_default_name
= 0;
933 char datetime
[16], tmp_path
[PATH_MAX
];
938 assert(session_name
);
940 memset(tmp_path
, 0, sizeof(tmp_path
));
942 /* Flag if we have a default session. */
943 if (strncmp(session_name
, DEFAULT_SESSION_NAME
"-",
944 strlen(DEFAULT_SESSION_NAME
) + 1) == 0) {
945 have_default_name
= 1;
947 /* Get date and time for session path */
949 timeinfo
= localtime(&rawtime
);
950 strftime(datetime
, sizeof(datetime
), "%Y%m%d-%H%M%S", timeinfo
);
953 if (have_default_name
) {
954 ret
= snprintf(tmp_path
, sizeof(tmp_path
),
955 "%s/%s", consumer
->subdir
, session_name
);
957 ret
= snprintf(tmp_path
, sizeof(tmp_path
),
958 "%s/%s-%s/", consumer
->subdir
, session_name
, datetime
);
961 PERROR("snprintf session name date");
965 strncpy(consumer
->subdir
, tmp_path
, sizeof(consumer
->subdir
));
966 DBG2("Consumer subdir set to %s", consumer
->subdir
);
973 * Ask the consumer if the data is ready to read (NOT pending) for the specific
976 * This function has a different behavior with the consumer i.e. that it waits
977 * for a reply from the consumer if yes or no the data is pending.
979 int consumer_is_data_pending(uint64_t session_id
,
980 struct consumer_output
*consumer
)
983 int32_t ret_code
= 0; /* Default is that the data is NOT pending */
984 struct consumer_socket
*socket
;
985 struct lttng_ht_iter iter
;
986 struct lttcomm_consumer_msg msg
;
990 msg
.cmd_type
= LTTNG_CONSUMER_DATA_PENDING
;
992 msg
.u
.data_pending
.session_id
= session_id
;
994 DBG3("Consumer data pending for id %" PRIu64
, session_id
);
996 /* Send command for each consumer */
998 cds_lfht_for_each_entry(consumer
->socks
->ht
, &iter
.iter
, socket
,
1000 /* Code flow error */
1001 assert(socket
->fd
>= 0);
1003 pthread_mutex_lock(socket
->lock
);
1005 ret
= lttcomm_send_unix_sock(socket
->fd
, &msg
, sizeof(msg
));
1007 /* The above call will print a PERROR on error. */
1008 DBG("Error on consumer is data pending on sock %d", socket
->fd
);
1009 pthread_mutex_unlock(socket
->lock
);
1014 * No need for a recv reply status because the answer to the command is
1015 * the reply status message.
1018 ret
= lttcomm_recv_unix_sock(socket
->fd
, &ret_code
, sizeof(ret_code
));
1021 /* Orderly shutdown. Don't return 0 which means success. */
1024 /* The above call will print a PERROR on error. */
1025 DBG("Error on recv consumer is data pending on sock %d", socket
->fd
);
1026 pthread_mutex_unlock(socket
->lock
);
1030 pthread_mutex_unlock(socket
->lock
);
1032 if (ret_code
== 1) {
1038 DBG("Consumer data is %s pending for session id %" PRIu64
,
1039 ret_code
== 1 ? "" : "NOT", session_id
);
1048 * Send a flush command to consumer using the given channel key.
1050 * Return 0 on success else a negative value.
1052 int consumer_flush_channel(struct consumer_socket
*socket
, uint64_t key
)
1055 struct lttcomm_consumer_msg msg
;
1058 assert(socket
->fd
>= 0);
1060 DBG2("Consumer flush channel key %" PRIu64
, key
);
1062 msg
.cmd_type
= LTTNG_CONSUMER_FLUSH_CHANNEL
;
1063 msg
.u
.flush_channel
.key
= key
;
1065 pthread_mutex_lock(socket
->lock
);
1066 health_code_update();
1068 ret
= consumer_send_msg(socket
, &msg
);
1074 health_code_update();
1075 pthread_mutex_unlock(socket
->lock
);
1080 * Send a close metdata command to consumer using the given channel key.
1082 * Return 0 on success else a negative value.
1084 int consumer_close_metadata(struct consumer_socket
*socket
,
1085 uint64_t metadata_key
)
1088 struct lttcomm_consumer_msg msg
;
1091 assert(socket
->fd
>= 0);
1093 DBG2("Consumer close metadata channel key %" PRIu64
, metadata_key
);
1095 msg
.cmd_type
= LTTNG_CONSUMER_CLOSE_METADATA
;
1096 msg
.u
.close_metadata
.key
= metadata_key
;
1098 pthread_mutex_lock(socket
->lock
);
1099 health_code_update();
1101 ret
= consumer_send_msg(socket
, &msg
);
1107 health_code_update();
1108 pthread_mutex_unlock(socket
->lock
);
1113 * Send a setup metdata command to consumer using the given channel key.
1115 * Return 0 on success else a negative value.
1117 int consumer_setup_metadata(struct consumer_socket
*socket
,
1118 uint64_t metadata_key
)
1121 struct lttcomm_consumer_msg msg
;
1124 assert(socket
->fd
>= 0);
1126 DBG2("Consumer setup metadata channel key %" PRIu64
, metadata_key
);
1128 msg
.cmd_type
= LTTNG_CONSUMER_SETUP_METADATA
;
1129 msg
.u
.setup_metadata
.key
= metadata_key
;
1131 pthread_mutex_lock(socket
->lock
);
1132 health_code_update();
1134 ret
= consumer_send_msg(socket
, &msg
);
1140 health_code_update();
1141 pthread_mutex_unlock(socket
->lock
);
1146 * Send metadata string to consumer. Socket lock MUST be acquired.
1148 * Return 0 on success else a negative value.
1150 int consumer_push_metadata(struct consumer_socket
*socket
,
1151 uint64_t metadata_key
, char *metadata_str
, size_t len
,
1152 size_t target_offset
)
1155 struct lttcomm_consumer_msg msg
;
1158 assert(socket
->fd
>= 0);
1160 DBG2("Consumer push metadata to consumer socket %d", socket
->fd
);
1162 msg
.cmd_type
= LTTNG_CONSUMER_PUSH_METADATA
;
1163 msg
.u
.push_metadata
.key
= metadata_key
;
1164 msg
.u
.push_metadata
.target_offset
= target_offset
;
1165 msg
.u
.push_metadata
.len
= len
;
1167 health_code_update();
1168 ret
= consumer_send_msg(socket
, &msg
);
1169 if (ret
< 0 || len
== 0) {
1173 DBG3("Consumer pushing metadata on sock %d of len %zu", socket
->fd
, len
);
1175 ret
= lttcomm_send_unix_sock(socket
->fd
, metadata_str
, len
);
1180 health_code_update();
1181 ret
= consumer_recv_status_reply(socket
);
1187 health_code_update();
1192 * Ask the consumer to snapshot a specific channel using the key.
1194 * Return 0 on success or else a negative error.
1196 int consumer_snapshot_channel(struct consumer_socket
*socket
, uint64_t key
,
1197 struct snapshot_output
*output
, int metadata
, uid_t uid
, gid_t gid
,
1198 const char *session_path
, int wait
)
1202 struct lttcomm_consumer_msg msg
;
1205 assert(socket
->fd
>= 0);
1207 assert(output
->consumer
);
1209 DBG("Consumer snapshot channel key %" PRIu64
, key
);
1211 ret
= utils_get_current_time_str("%Y%m%d-%H%M%S", datetime
,
1218 memset(&msg
, 0, sizeof(msg
));
1219 msg
.cmd_type
= LTTNG_CONSUMER_SNAPSHOT_CHANNEL
;
1220 msg
.u
.snapshot_channel
.key
= key
;
1221 msg
.u
.snapshot_channel
.max_size
= output
->max_size
;
1222 msg
.u
.snapshot_channel
.metadata
= metadata
;
1224 if (output
->consumer
->type
== CONSUMER_DST_NET
) {
1225 msg
.u
.snapshot_channel
.relayd_id
= output
->consumer
->net_seq_index
;
1226 msg
.u
.snapshot_channel
.use_relayd
= 1;
1227 ret
= snprintf(msg
.u
.snapshot_channel
.pathname
,
1228 sizeof(msg
.u
.snapshot_channel
.pathname
), "%s/%s-%s%s",
1229 output
->consumer
->subdir
, output
->name
, datetime
,
1232 ret
= -LTTNG_ERR_NOMEM
;
1236 ret
= snprintf(msg
.u
.snapshot_channel
.pathname
,
1237 sizeof(msg
.u
.snapshot_channel
.pathname
), "%s/%s-%s%s",
1238 output
->consumer
->dst
.trace_path
, output
->name
, datetime
,
1241 ret
= -LTTNG_ERR_NOMEM
;
1244 msg
.u
.snapshot_channel
.relayd_id
= (uint64_t) -1ULL;
1246 /* Create directory. Ignore if exist. */
1247 ret
= run_as_mkdir_recursive(msg
.u
.snapshot_channel
.pathname
,
1248 S_IRWXU
| S_IRWXG
, uid
, gid
);
1250 if (ret
!= -EEXIST
) {
1251 ERR("Trace directory creation error");
1257 health_code_update();
1258 ret
= consumer_send_msg(socket
, &msg
);
1264 health_code_update();