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
;
379 cds_lfht_for_each_entry(obj
->socks
->ht
, &iter
.iter
, socket
, node
.node
) {
380 /* Create new socket object. */
381 copy_sock
= consumer_allocate_socket(socket
->fd
);
382 if (copy_sock
== NULL
) {
386 copy_sock
->registered
= socket
->registered
;
387 copy_sock
->lock
= socket
->lock
;
388 consumer_add_socket(copy_sock
, output
);
395 consumer_destroy_output(output
);
400 * Set network URI to the consumer output object.
402 * Return 0 on success. Return 1 if the URI were equal. Else, negative value on
405 int consumer_set_network_uri(struct consumer_output
*obj
,
406 struct lttng_uri
*uri
)
409 char tmp_path
[PATH_MAX
];
410 char hostname
[HOST_NAME_MAX
];
411 struct lttng_uri
*dst_uri
= NULL
;
413 /* Code flow error safety net. */
417 switch (uri
->stype
) {
418 case LTTNG_STREAM_CONTROL
:
419 dst_uri
= &obj
->dst
.net
.control
;
420 obj
->dst
.net
.control_isset
= 1;
421 if (uri
->port
== 0) {
422 /* Assign default port. */
423 uri
->port
= DEFAULT_NETWORK_CONTROL_PORT
;
425 DBG3("Consumer control URI set with port %d", uri
->port
);
427 case LTTNG_STREAM_DATA
:
428 dst_uri
= &obj
->dst
.net
.data
;
429 obj
->dst
.net
.data_isset
= 1;
430 if (uri
->port
== 0) {
431 /* Assign default port. */
432 uri
->port
= DEFAULT_NETWORK_DATA_PORT
;
434 DBG3("Consumer data URI set with port %d", uri
->port
);
437 ERR("Set network uri type unknown %d", uri
->stype
);
441 ret
= uri_compare(dst_uri
, uri
);
443 /* Same URI, don't touch it and return success. */
444 DBG3("URI network compare are the same");
448 /* URIs were not equal, replacing it. */
449 memset(dst_uri
, 0, sizeof(struct lttng_uri
));
450 memcpy(dst_uri
, uri
, sizeof(struct lttng_uri
));
451 obj
->type
= CONSUMER_DST_NET
;
453 /* Handle subdir and add hostname in front. */
454 if (dst_uri
->stype
== LTTNG_STREAM_CONTROL
) {
455 /* Get hostname to append it in the pathname */
456 ret
= gethostname(hostname
, sizeof(hostname
));
458 PERROR("gethostname. Fallback on default localhost");
459 strncpy(hostname
, "localhost", sizeof(hostname
));
461 hostname
[sizeof(hostname
) - 1] = '\0';
463 /* Setup consumer subdir if none present in the control URI */
464 if (strlen(dst_uri
->subdir
) == 0) {
465 ret
= snprintf(tmp_path
, sizeof(tmp_path
), "%s/%s",
466 hostname
, obj
->subdir
);
468 ret
= snprintf(tmp_path
, sizeof(tmp_path
), "%s/%s",
469 hostname
, dst_uri
->subdir
);
472 PERROR("snprintf set consumer uri subdir");
476 strncpy(obj
->subdir
, tmp_path
, sizeof(obj
->subdir
));
477 DBG3("Consumer set network uri subdir path %s", tmp_path
);
488 * Send file descriptor to consumer via sock.
490 int consumer_send_fds(struct consumer_socket
*sock
, int *fds
, size_t nb_fd
)
498 ret
= lttcomm_send_fds_unix_sock(sock
->fd
, fds
, nb_fd
);
500 /* The above call will print a PERROR on error. */
501 DBG("Error when sending consumer fds on sock %d", sock
->fd
);
505 ret
= consumer_recv_status_reply(sock
);
512 * Consumer send channel communication message structure to consumer.
514 int consumer_send_channel(struct consumer_socket
*sock
,
515 struct lttcomm_consumer_msg
*msg
)
521 assert(sock
->fd
>= 0);
523 ret
= lttcomm_send_unix_sock(sock
->fd
, msg
,
524 sizeof(struct lttcomm_consumer_msg
));
526 /* The above call will print a PERROR on error. */
527 DBG("Error when sending consumer channel on sock %d", sock
->fd
);
531 ret
= consumer_recv_status_reply(sock
);
538 * Init channel communication message structure.
540 void consumer_init_channel_comm_msg(struct lttcomm_consumer_msg
*msg
,
541 enum lttng_consumer_command cmd
,
543 uint64_t max_sb_size
,
546 unsigned int nb_init_streams
)
550 /* TODO: Args validation */
552 /* Zeroed structure */
553 memset(msg
, 0, sizeof(struct lttcomm_consumer_msg
));
557 msg
->u
.channel
.channel_key
= channel_key
;
558 msg
->u
.channel
.max_sb_size
= max_sb_size
;
559 msg
->u
.channel
.mmap_len
= mmap_len
;
560 msg
->u
.channel
.nb_init_streams
= nb_init_streams
;
564 * Init stream communication message structure.
566 void consumer_init_stream_comm_msg(struct lttcomm_consumer_msg
*msg
,
567 enum lttng_consumer_command cmd
,
571 enum lttng_event_output output
,
576 unsigned int metadata_flag
,
578 const char *pathname
,
579 unsigned int session_id
)
583 memset(msg
, 0, sizeof(struct lttcomm_consumer_msg
));
585 /* TODO: Args validation */
588 msg
->u
.stream
.channel_key
= channel_key
;
589 msg
->u
.stream
.stream_key
= stream_key
;
590 msg
->u
.stream
.state
= state
;
591 msg
->u
.stream
.output
= output
;
592 msg
->u
.stream
.mmap_len
= mmap_len
;
593 msg
->u
.stream
.uid
= uid
;
594 msg
->u
.stream
.gid
= gid
;
595 msg
->u
.stream
.net_index
= net_index
;
596 msg
->u
.stream
.metadata_flag
= metadata_flag
;
597 msg
->u
.stream
.session_id
= (uint64_t) session_id
;
598 strncpy(msg
->u
.stream
.name
, name
, sizeof(msg
->u
.stream
.name
));
599 msg
->u
.stream
.name
[sizeof(msg
->u
.stream
.name
) - 1] = '\0';
600 strncpy(msg
->u
.stream
.path_name
, pathname
,
601 sizeof(msg
->u
.stream
.path_name
));
602 msg
->u
.stream
.path_name
[sizeof(msg
->u
.stream
.path_name
) - 1] = '\0';
606 * Send stream communication structure to the consumer.
608 int consumer_send_stream(struct consumer_socket
*sock
,
609 struct consumer_output
*dst
, struct lttcomm_consumer_msg
*msg
,
610 int *fds
, size_t nb_fd
)
619 case CONSUMER_DST_NET
:
620 /* Consumer should send the stream on the network. */
621 msg
->u
.stream
.net_index
= dst
->net_seq_index
;
623 case CONSUMER_DST_LOCAL
:
624 /* Add stream file name to stream path */
625 strncat(msg
->u
.stream
.path_name
, "/",
626 sizeof(msg
->u
.stream
.path_name
) -
627 strlen(msg
->u
.stream
.path_name
) - 1);
628 strncat(msg
->u
.stream
.path_name
, msg
->u
.stream
.name
,
629 sizeof(msg
->u
.stream
.path_name
) -
630 strlen(msg
->u
.stream
.path_name
) - 1);
631 msg
->u
.stream
.path_name
[sizeof(msg
->u
.stream
.path_name
) - 1] = '\0';
632 /* Indicate that the stream is NOT network */
633 msg
->u
.stream
.net_index
= -1;
636 ERR("Consumer unknown output type (%d)", dst
->type
);
642 ret
= lttcomm_send_unix_sock(sock
->fd
, msg
,
643 sizeof(struct lttcomm_consumer_msg
));
645 /* The above call will print a PERROR on error. */
646 DBG("Error when sending consumer stream on sock %d", sock
->fd
);
650 ret
= consumer_recv_status_reply(sock
);
655 ret
= consumer_send_fds(sock
, fds
, nb_fd
);
665 * Send relayd socket to consumer associated with a session name.
667 * On success return positive value. On error, negative value.
669 int consumer_send_relayd_socket(struct consumer_socket
*consumer_sock
,
670 struct lttcomm_sock
*sock
, struct consumer_output
*consumer
,
671 enum lttng_stream_type type
, unsigned int session_id
)
674 struct lttcomm_consumer_msg msg
;
676 /* Code flow error. Safety net. */
679 assert(consumer_sock
);
681 /* Bail out if consumer is disabled */
682 if (!consumer
->enabled
) {
687 msg
.cmd_type
= LTTNG_CONSUMER_ADD_RELAYD_SOCKET
;
689 * Assign network consumer output index using the temporary consumer since
690 * this call should only be made from within a set_consumer_uri() function
691 * call in the session daemon.
693 msg
.u
.relayd_sock
.net_index
= consumer
->net_seq_index
;
694 msg
.u
.relayd_sock
.type
= type
;
695 msg
.u
.relayd_sock
.session_id
= session_id
;
696 memcpy(&msg
.u
.relayd_sock
.sock
, sock
, sizeof(msg
.u
.relayd_sock
.sock
));
698 DBG3("Sending relayd sock info to consumer on %d", consumer_sock
->fd
);
699 ret
= lttcomm_send_unix_sock(consumer_sock
->fd
, &msg
, sizeof(msg
));
701 /* The above call will print a PERROR on error. */
702 DBG("Error when sending relayd sockets on sock %d", sock
->fd
);
706 ret
= consumer_recv_status_reply(consumer_sock
);
711 DBG3("Sending relayd socket file descriptor to consumer");
712 ret
= consumer_send_fds(consumer_sock
, &sock
->fd
, 1);
717 DBG2("Consumer relayd socket sent");
724 * Set consumer subdirectory using the session name and a generated datetime if
725 * needed. This is appended to the current subdirectory.
727 int consumer_set_subdir(struct consumer_output
*consumer
,
728 const char *session_name
)
731 unsigned int have_default_name
= 0;
732 char datetime
[16], tmp_path
[PATH_MAX
];
737 assert(session_name
);
739 memset(tmp_path
, 0, sizeof(tmp_path
));
741 /* Flag if we have a default session. */
742 if (strncmp(session_name
, DEFAULT_SESSION_NAME
"-",
743 strlen(DEFAULT_SESSION_NAME
) + 1) == 0) {
744 have_default_name
= 1;
746 /* Get date and time for session path */
748 timeinfo
= localtime(&rawtime
);
749 strftime(datetime
, sizeof(datetime
), "%Y%m%d-%H%M%S", timeinfo
);
752 if (have_default_name
) {
753 ret
= snprintf(tmp_path
, sizeof(tmp_path
),
754 "%s/%s", consumer
->subdir
, session_name
);
756 ret
= snprintf(tmp_path
, sizeof(tmp_path
),
757 "%s/%s-%s/", consumer
->subdir
, session_name
, datetime
);
760 PERROR("snprintf session name date");
764 strncpy(consumer
->subdir
, tmp_path
, sizeof(consumer
->subdir
));
765 DBG2("Consumer subdir set to %s", consumer
->subdir
);
772 * Ask the consumer if the data is ready to read (NOT pending) for the specific
775 * This function has a different behavior with the consumer i.e. that it waits
776 * for a reply from the consumer if yes or no the data is pending.
778 int consumer_is_data_pending(unsigned int id
,
779 struct consumer_output
*consumer
)
782 int32_t ret_code
= 0; /* Default is that the data is NOT pending */
783 struct consumer_socket
*socket
;
784 struct lttng_ht_iter iter
;
785 struct lttcomm_consumer_msg msg
;
789 msg
.cmd_type
= LTTNG_CONSUMER_DATA_PENDING
;
791 msg
.u
.data_pending
.session_id
= (uint64_t) id
;
793 DBG3("Consumer data pending for id %u", id
);
795 /* Send command for each consumer */
796 cds_lfht_for_each_entry(consumer
->socks
->ht
, &iter
.iter
, socket
,
798 /* Code flow error */
799 assert(socket
->fd
>= 0);
801 pthread_mutex_lock(socket
->lock
);
803 ret
= lttcomm_send_unix_sock(socket
->fd
, &msg
, sizeof(msg
));
805 /* The above call will print a PERROR on error. */
806 DBG("Error on consumer is data pending on sock %d", socket
->fd
);
807 pthread_mutex_unlock(socket
->lock
);
812 * No need for a recv reply status because the answer to the command is
813 * the reply status message.
816 ret
= lttcomm_recv_unix_sock(socket
->fd
, &ret_code
, sizeof(ret_code
));
819 /* Orderly shutdown. Don't return 0 which means success. */
822 /* The above call will print a PERROR on error. */
823 DBG("Error on recv consumer is data pending on sock %d", socket
->fd
);
824 pthread_mutex_unlock(socket
->lock
);
828 pthread_mutex_unlock(socket
->lock
);
835 DBG("Consumer data is %s pending for session id %u",
836 ret_code
== 1 ? "" : "NOT", id
);