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>
27 #include <common/common.h>
28 #include <common/defaults.h>
29 #include <common/uri.h>
34 * Receive a reply command status message from the consumer. Consumer socket
35 * lock MUST be acquired before calling this function.
37 * Return 0 on success, -1 on recv error or a negative lttng error code which
38 * was possibly returned by the consumer.
40 int consumer_recv_status_reply(struct consumer_socket
*sock
)
43 struct lttcomm_consumer_status_msg reply
;
47 ret
= lttcomm_recv_unix_sock(sock
->fd
, &reply
, sizeof(reply
));
50 /* Orderly shutdown. Don't return 0 which means success. */
53 /* The above call will print a PERROR on error. */
54 DBG("Fail to receive status reply on sock %d", sock
->fd
);
58 if (reply
.ret_code
== LTTNG_OK
) {
62 ret
= -reply
.ret_code
;
63 DBG("Consumer ret code %d", reply
.ret_code
);
71 * Send destroy relayd command to consumer.
73 * On success return positive value. On error, negative value.
75 int consumer_send_destroy_relayd(struct consumer_socket
*sock
,
76 struct consumer_output
*consumer
)
79 struct lttcomm_consumer_msg msg
;
84 DBG2("Sending destroy relayd command to consumer...");
86 /* Bail out if consumer is disabled */
87 if (!consumer
->enabled
) {
89 DBG3("Consumer is disabled");
93 msg
.cmd_type
= LTTNG_CONSUMER_DESTROY_RELAYD
;
94 msg
.u
.destroy_relayd
.net_seq_idx
= consumer
->net_seq_index
;
96 pthread_mutex_lock(sock
->lock
);
97 ret
= lttcomm_send_unix_sock(sock
->fd
, &msg
, sizeof(msg
));
99 /* Indicate that the consumer is probably closing at this point. */
100 DBG("send consumer destroy relayd command");
104 /* Don't check the return value. The caller will do it. */
105 ret
= consumer_recv_status_reply(sock
);
107 DBG2("Consumer send destroy relayd command done");
110 pthread_mutex_unlock(sock
->lock
);
116 * For each consumer socket in the consumer output object, send a destroy
119 void consumer_output_send_destroy_relayd(struct consumer_output
*consumer
)
121 struct lttng_ht_iter iter
;
122 struct consumer_socket
*socket
;
126 /* Destroy any relayd connection */
127 if (consumer
&& consumer
->type
== CONSUMER_DST_NET
) {
129 cds_lfht_for_each_entry(consumer
->socks
->ht
, &iter
.iter
, socket
,
133 /* Send destroy relayd command */
134 ret
= consumer_send_destroy_relayd(socket
, consumer
);
136 DBG("Unable to send destroy relayd command to consumer");
137 /* Continue since we MUST delete everything at this point. */
145 * From a consumer_data structure, allocate and add a consumer socket to the
148 * Return 0 on success, else negative value on error
150 int consumer_create_socket(struct consumer_data
*data
,
151 struct consumer_output
*output
)
154 struct consumer_socket
*socket
;
158 if (output
== NULL
|| data
->cmd_sock
< 0) {
160 * Not an error. Possible there is simply not spawned consumer or it's
161 * disabled for the tracing session asking the socket.
167 socket
= consumer_find_socket(data
->cmd_sock
, output
);
169 if (socket
== NULL
) {
170 socket
= consumer_allocate_socket(data
->cmd_sock
);
171 if (socket
== NULL
) {
176 socket
->registered
= 0;
177 socket
->lock
= &data
->lock
;
179 consumer_add_socket(socket
, output
);
183 DBG3("Consumer socket created (fd: %d) and added to output",
191 * Find a consumer_socket in a consumer_output hashtable. Read side lock must
192 * be acquired before calling this function and across use of the
193 * returned consumer_socket.
195 struct consumer_socket
*consumer_find_socket(int key
,
196 struct consumer_output
*consumer
)
198 struct lttng_ht_iter iter
;
199 struct lttng_ht_node_ulong
*node
;
200 struct consumer_socket
*socket
= NULL
;
202 /* Negative keys are lookup failures */
203 if (key
< 0 || consumer
== NULL
) {
207 lttng_ht_lookup(consumer
->socks
, (void *)((unsigned long) key
),
209 node
= lttng_ht_iter_get_node_ulong(&iter
);
211 socket
= caa_container_of(node
, struct consumer_socket
, node
);
218 * Allocate a new consumer_socket and return the pointer.
220 struct consumer_socket
*consumer_allocate_socket(int fd
)
222 struct consumer_socket
*socket
= NULL
;
224 socket
= zmalloc(sizeof(struct consumer_socket
));
225 if (socket
== NULL
) {
226 PERROR("zmalloc consumer socket");
231 lttng_ht_node_init_ulong(&socket
->node
, fd
);
238 * Add consumer socket to consumer output object. Read side lock must be
239 * acquired before calling this function.
241 void consumer_add_socket(struct consumer_socket
*sock
,
242 struct consumer_output
*consumer
)
247 lttng_ht_add_unique_ulong(consumer
->socks
, &sock
->node
);
251 * Delte consumer socket to consumer output object. Read side lock must be
252 * acquired before calling this function.
254 void consumer_del_socket(struct consumer_socket
*sock
,
255 struct consumer_output
*consumer
)
258 struct lttng_ht_iter iter
;
263 iter
.iter
.node
= &sock
->node
.node
;
264 ret
= lttng_ht_del(consumer
->socks
, &iter
);
269 * RCU destroy call function.
271 static void destroy_socket_rcu(struct rcu_head
*head
)
273 struct lttng_ht_node_ulong
*node
=
274 caa_container_of(head
, struct lttng_ht_node_ulong
, head
);
275 struct consumer_socket
*socket
=
276 caa_container_of(node
, struct consumer_socket
, node
);
282 * Destroy and free socket pointer in a call RCU. Read side lock must be
283 * acquired before calling this function.
285 void consumer_destroy_socket(struct consumer_socket
*sock
)
290 * We DO NOT close the file descriptor here since it is global to the
291 * session daemon and is closed only if the consumer dies or a custom
292 * consumer was registered,
294 if (sock
->registered
) {
295 DBG3("Consumer socket was registered. Closing fd %d", sock
->fd
);
296 lttcomm_close_unix_sock(sock
->fd
);
299 call_rcu(&sock
->node
.head
, destroy_socket_rcu
);
303 * Allocate and assign data to a consumer_output object.
305 * Return pointer to structure.
307 struct consumer_output
*consumer_create_output(enum consumer_dst_type type
)
309 struct consumer_output
*output
= NULL
;
311 output
= zmalloc(sizeof(struct consumer_output
));
312 if (output
== NULL
) {
313 PERROR("zmalloc consumer_output");
317 /* By default, consumer output is enabled */
320 output
->net_seq_index
= -1;
322 output
->socks
= lttng_ht_new(0, LTTNG_HT_TYPE_ULONG
);
329 * Delete the consumer_output object from the list and free the ptr.
331 void consumer_destroy_output(struct consumer_output
*obj
)
338 struct lttng_ht_iter iter
;
339 struct consumer_socket
*socket
;
342 cds_lfht_for_each_entry(obj
->socks
->ht
, &iter
.iter
, socket
, node
.node
) {
343 consumer_del_socket(socket
, obj
);
344 consumer_destroy_socket(socket
);
348 /* Finally destroy HT */
349 lttng_ht_destroy(obj
->socks
);
356 * Copy consumer output and returned the newly allocated copy.
358 struct consumer_output
*consumer_copy_output(struct consumer_output
*obj
)
360 struct lttng_ht
*tmp_ht_ptr
;
361 struct lttng_ht_iter iter
;
362 struct consumer_socket
*socket
, *copy_sock
;
363 struct consumer_output
*output
;
367 output
= consumer_create_output(obj
->type
);
368 if (output
== NULL
) {
371 /* Avoid losing the HT reference after the memcpy() */
372 tmp_ht_ptr
= output
->socks
;
374 memcpy(output
, obj
, sizeof(struct consumer_output
));
376 /* Putting back the HT pointer and start copying socket(s). */
377 output
->socks
= tmp_ht_ptr
;
380 cds_lfht_for_each_entry(obj
->socks
->ht
, &iter
.iter
, socket
, node
.node
) {
381 /* Create new socket object. */
382 copy_sock
= consumer_allocate_socket(socket
->fd
);
383 if (copy_sock
== NULL
) {
388 copy_sock
->registered
= socket
->registered
;
389 copy_sock
->lock
= socket
->lock
;
390 consumer_add_socket(copy_sock
, output
);
398 consumer_destroy_output(output
);
403 * Set network URI to the consumer output object.
405 * Return 0 on success. Return 1 if the URI were equal. Else, negative value on
408 int consumer_set_network_uri(struct consumer_output
*obj
,
409 struct lttng_uri
*uri
)
412 char tmp_path
[PATH_MAX
];
413 char hostname
[HOST_NAME_MAX
];
414 struct lttng_uri
*dst_uri
= NULL
;
416 /* Code flow error safety net. */
420 switch (uri
->stype
) {
421 case LTTNG_STREAM_CONTROL
:
422 dst_uri
= &obj
->dst
.net
.control
;
423 obj
->dst
.net
.control_isset
= 1;
424 if (uri
->port
== 0) {
425 /* Assign default port. */
426 uri
->port
= DEFAULT_NETWORK_CONTROL_PORT
;
428 DBG3("Consumer control URI set with port %d", uri
->port
);
430 case LTTNG_STREAM_DATA
:
431 dst_uri
= &obj
->dst
.net
.data
;
432 obj
->dst
.net
.data_isset
= 1;
433 if (uri
->port
== 0) {
434 /* Assign default port. */
435 uri
->port
= DEFAULT_NETWORK_DATA_PORT
;
437 DBG3("Consumer data URI set with port %d", uri
->port
);
440 ERR("Set network uri type unknown %d", uri
->stype
);
444 ret
= uri_compare(dst_uri
, uri
);
446 /* Same URI, don't touch it and return success. */
447 DBG3("URI network compare are the same");
451 /* URIs were not equal, replacing it. */
452 memset(dst_uri
, 0, sizeof(struct lttng_uri
));
453 memcpy(dst_uri
, uri
, sizeof(struct lttng_uri
));
454 obj
->type
= CONSUMER_DST_NET
;
456 /* Handle subdir and add hostname in front. */
457 if (dst_uri
->stype
== LTTNG_STREAM_CONTROL
) {
458 /* Get hostname to append it in the pathname */
459 ret
= gethostname(hostname
, sizeof(hostname
));
461 PERROR("gethostname. Fallback on default localhost");
462 strncpy(hostname
, "localhost", sizeof(hostname
));
464 hostname
[sizeof(hostname
) - 1] = '\0';
466 /* Setup consumer subdir if none present in the control URI */
467 if (strlen(dst_uri
->subdir
) == 0) {
468 ret
= snprintf(tmp_path
, sizeof(tmp_path
), "%s/%s",
469 hostname
, obj
->subdir
);
471 ret
= snprintf(tmp_path
, sizeof(tmp_path
), "%s/%s",
472 hostname
, dst_uri
->subdir
);
475 PERROR("snprintf set consumer uri subdir");
479 strncpy(obj
->subdir
, tmp_path
, sizeof(obj
->subdir
));
480 DBG3("Consumer set network uri subdir path %s", tmp_path
);
491 * Send file descriptor to consumer via sock.
493 int consumer_send_fds(struct consumer_socket
*sock
, int *fds
, size_t nb_fd
)
501 ret
= lttcomm_send_fds_unix_sock(sock
->fd
, fds
, nb_fd
);
503 /* The above call will print a PERROR on error. */
504 DBG("Error when sending consumer fds on sock %d", sock
->fd
);
508 ret
= consumer_recv_status_reply(sock
);
515 * Consumer send channel communication message structure to consumer.
517 int consumer_send_channel(struct consumer_socket
*sock
,
518 struct lttcomm_consumer_msg
*msg
)
524 assert(sock
->fd
>= 0);
526 ret
= lttcomm_send_unix_sock(sock
->fd
, msg
,
527 sizeof(struct lttcomm_consumer_msg
));
529 /* The above call will print a PERROR on error. */
530 DBG("Error when sending consumer channel on sock %d", sock
->fd
);
534 ret
= consumer_recv_status_reply(sock
);
541 * Init channel communication message structure.
543 void consumer_init_channel_comm_msg(struct lttcomm_consumer_msg
*msg
,
544 enum lttng_consumer_command cmd
,
546 uint64_t max_sb_size
,
549 unsigned int nb_init_streams
)
553 /* TODO: Args validation */
555 /* Zeroed structure */
556 memset(msg
, 0, sizeof(struct lttcomm_consumer_msg
));
560 msg
->u
.channel
.channel_key
= channel_key
;
561 msg
->u
.channel
.max_sb_size
= max_sb_size
;
562 msg
->u
.channel
.mmap_len
= mmap_len
;
563 msg
->u
.channel
.nb_init_streams
= nb_init_streams
;
567 * Init stream communication message structure.
569 void consumer_init_stream_comm_msg(struct lttcomm_consumer_msg
*msg
,
570 enum lttng_consumer_command cmd
,
574 enum lttng_event_output output
,
579 unsigned int metadata_flag
,
581 const char *pathname
,
582 unsigned int session_id
)
586 memset(msg
, 0, sizeof(struct lttcomm_consumer_msg
));
588 /* TODO: Args validation */
591 msg
->u
.stream
.channel_key
= channel_key
;
592 msg
->u
.stream
.stream_key
= stream_key
;
593 msg
->u
.stream
.state
= state
;
594 msg
->u
.stream
.output
= output
;
595 msg
->u
.stream
.mmap_len
= mmap_len
;
596 msg
->u
.stream
.uid
= uid
;
597 msg
->u
.stream
.gid
= gid
;
598 msg
->u
.stream
.net_index
= net_index
;
599 msg
->u
.stream
.metadata_flag
= metadata_flag
;
600 msg
->u
.stream
.session_id
= (uint64_t) session_id
;
601 strncpy(msg
->u
.stream
.name
, name
, sizeof(msg
->u
.stream
.name
));
602 msg
->u
.stream
.name
[sizeof(msg
->u
.stream
.name
) - 1] = '\0';
603 strncpy(msg
->u
.stream
.path_name
, pathname
,
604 sizeof(msg
->u
.stream
.path_name
));
605 msg
->u
.stream
.path_name
[sizeof(msg
->u
.stream
.path_name
) - 1] = '\0';
609 * Send stream communication structure to the consumer.
611 int consumer_send_stream(struct consumer_socket
*sock
,
612 struct consumer_output
*dst
, struct lttcomm_consumer_msg
*msg
,
613 int *fds
, size_t nb_fd
)
622 case CONSUMER_DST_NET
:
623 /* Consumer should send the stream on the network. */
624 msg
->u
.stream
.net_index
= dst
->net_seq_index
;
626 case CONSUMER_DST_LOCAL
:
627 /* Add stream file name to stream path */
628 strncat(msg
->u
.stream
.path_name
, "/",
629 sizeof(msg
->u
.stream
.path_name
) -
630 strlen(msg
->u
.stream
.path_name
) - 1);
631 strncat(msg
->u
.stream
.path_name
, msg
->u
.stream
.name
,
632 sizeof(msg
->u
.stream
.path_name
) -
633 strlen(msg
->u
.stream
.path_name
) - 1);
634 msg
->u
.stream
.path_name
[sizeof(msg
->u
.stream
.path_name
) - 1] = '\0';
635 /* Indicate that the stream is NOT network */
636 msg
->u
.stream
.net_index
= -1;
639 ERR("Consumer unknown output type (%d)", dst
->type
);
645 ret
= lttcomm_send_unix_sock(sock
->fd
, msg
,
646 sizeof(struct lttcomm_consumer_msg
));
648 /* The above call will print a PERROR on error. */
649 DBG("Error when sending consumer stream on sock %d", sock
->fd
);
653 ret
= consumer_recv_status_reply(sock
);
658 ret
= consumer_send_fds(sock
, fds
, nb_fd
);
668 * Send relayd socket to consumer associated with a session name.
670 * On success return positive value. On error, negative value.
672 int consumer_send_relayd_socket(struct consumer_socket
*consumer_sock
,
673 struct lttcomm_sock
*sock
, struct consumer_output
*consumer
,
674 enum lttng_stream_type type
, unsigned int session_id
)
677 struct lttcomm_consumer_msg msg
;
679 /* Code flow error. Safety net. */
682 assert(consumer_sock
);
684 /* Bail out if consumer is disabled */
685 if (!consumer
->enabled
) {
690 msg
.cmd_type
= LTTNG_CONSUMER_ADD_RELAYD_SOCKET
;
692 * Assign network consumer output index using the temporary consumer since
693 * this call should only be made from within a set_consumer_uri() function
694 * call in the session daemon.
696 msg
.u
.relayd_sock
.net_index
= consumer
->net_seq_index
;
697 msg
.u
.relayd_sock
.type
= type
;
698 msg
.u
.relayd_sock
.session_id
= session_id
;
699 memcpy(&msg
.u
.relayd_sock
.sock
, sock
, sizeof(msg
.u
.relayd_sock
.sock
));
701 DBG3("Sending relayd sock info to consumer on %d", consumer_sock
->fd
);
702 ret
= lttcomm_send_unix_sock(consumer_sock
->fd
, &msg
, sizeof(msg
));
704 /* The above call will print a PERROR on error. */
705 DBG("Error when sending relayd sockets on sock %d", sock
->fd
);
709 ret
= consumer_recv_status_reply(consumer_sock
);
714 DBG3("Sending relayd socket file descriptor to consumer");
715 ret
= consumer_send_fds(consumer_sock
, &sock
->fd
, 1);
720 DBG2("Consumer relayd socket sent");
727 * Set consumer subdirectory using the session name and a generated datetime if
728 * needed. This is appended to the current subdirectory.
730 int consumer_set_subdir(struct consumer_output
*consumer
,
731 const char *session_name
)
734 unsigned int have_default_name
= 0;
735 char datetime
[16], tmp_path
[PATH_MAX
];
740 assert(session_name
);
742 memset(tmp_path
, 0, sizeof(tmp_path
));
744 /* Flag if we have a default session. */
745 if (strncmp(session_name
, DEFAULT_SESSION_NAME
"-",
746 strlen(DEFAULT_SESSION_NAME
) + 1) == 0) {
747 have_default_name
= 1;
749 /* Get date and time for session path */
751 timeinfo
= localtime(&rawtime
);
752 strftime(datetime
, sizeof(datetime
), "%Y%m%d-%H%M%S", timeinfo
);
755 if (have_default_name
) {
756 ret
= snprintf(tmp_path
, sizeof(tmp_path
),
757 "%s/%s", consumer
->subdir
, session_name
);
759 ret
= snprintf(tmp_path
, sizeof(tmp_path
),
760 "%s/%s-%s/", consumer
->subdir
, session_name
, datetime
);
763 PERROR("snprintf session name date");
767 strncpy(consumer
->subdir
, tmp_path
, sizeof(consumer
->subdir
));
768 DBG2("Consumer subdir set to %s", consumer
->subdir
);
775 * Ask the consumer if the data is ready to read (NOT pending) for the specific
778 * This function has a different behavior with the consumer i.e. that it waits
779 * for a reply from the consumer if yes or no the data is pending.
781 int consumer_is_data_pending(unsigned int id
,
782 struct consumer_output
*consumer
)
785 int32_t ret_code
= 0; /* Default is that the data is NOT pending */
786 struct consumer_socket
*socket
;
787 struct lttng_ht_iter iter
;
788 struct lttcomm_consumer_msg msg
;
792 msg
.cmd_type
= LTTNG_CONSUMER_DATA_PENDING
;
794 msg
.u
.data_pending
.session_id
= (uint64_t) id
;
796 DBG3("Consumer data pending for id %u", id
);
798 /* Send command for each consumer */
800 cds_lfht_for_each_entry(consumer
->socks
->ht
, &iter
.iter
, socket
,
802 /* Code flow error */
803 assert(socket
->fd
>= 0);
805 pthread_mutex_lock(socket
->lock
);
807 ret
= lttcomm_send_unix_sock(socket
->fd
, &msg
, sizeof(msg
));
809 /* The above call will print a PERROR on error. */
810 DBG("Error on consumer is data pending on sock %d", socket
->fd
);
811 pthread_mutex_unlock(socket
->lock
);
816 * No need for a recv reply status because the answer to the command is
817 * the reply status message.
820 ret
= lttcomm_recv_unix_sock(socket
->fd
, &ret_code
, sizeof(ret_code
));
823 /* Orderly shutdown. Don't return 0 which means success. */
826 /* The above call will print a PERROR on error. */
827 DBG("Error on recv consumer is data pending on sock %d", socket
->fd
);
828 pthread_mutex_unlock(socket
->lock
);
832 pthread_mutex_unlock(socket
->lock
);
840 DBG("Consumer data is %s pending for session id %u",
841 ret_code
== 1 ? "" : "NOT", id
);