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
));
49 PERROR("recv consumer status msg");
53 if (reply
.ret_code
== LTTNG_OK
) {
57 ret
= -reply
.ret_code
;
58 ERR("Consumer ret code %d", reply
.ret_code
);
66 * Send destroy relayd command to consumer.
68 * On success return positive value. On error, negative value.
70 int consumer_send_destroy_relayd(struct consumer_socket
*sock
,
71 struct consumer_output
*consumer
)
74 struct lttcomm_consumer_msg msg
;
79 DBG2("Sending destroy relayd command to consumer...");
81 /* Bail out if consumer is disabled */
82 if (!consumer
->enabled
) {
84 DBG3("Consumer is disabled");
88 msg
.cmd_type
= LTTNG_CONSUMER_DESTROY_RELAYD
;
89 msg
.u
.destroy_relayd
.net_seq_idx
= consumer
->net_seq_index
;
91 pthread_mutex_lock(sock
->lock
);
92 ret
= lttcomm_send_unix_sock(sock
->fd
, &msg
, sizeof(msg
));
94 /* Indicate that the consumer is probably closing at this point. */
95 DBG("send consumer destroy relayd command");
99 /* Don't check the return value. The caller will do it. */
100 ret
= consumer_recv_status_reply(sock
);
102 DBG2("Consumer send destroy relayd command done");
105 pthread_mutex_unlock(sock
->lock
);
111 * For each consumer socket in the consumer output object, send a destroy
114 void consumer_output_send_destroy_relayd(struct consumer_output
*consumer
)
116 struct lttng_ht_iter iter
;
117 struct consumer_socket
*socket
;
121 /* Destroy any relayd connection */
122 if (consumer
&& consumer
->type
== CONSUMER_DST_NET
) {
124 cds_lfht_for_each_entry(consumer
->socks
->ht
, &iter
.iter
, socket
,
128 /* Send destroy relayd command */
129 ret
= consumer_send_destroy_relayd(socket
, consumer
);
131 DBG("Unable to send destroy relayd command to consumer");
132 /* Continue since we MUST delete everything at this point. */
140 * From a consumer_data structure, allocate and add a consumer socket to the
143 * Return 0 on success, else negative value on error
145 int consumer_create_socket(struct consumer_data
*data
,
146 struct consumer_output
*output
)
149 struct consumer_socket
*socket
;
153 if (output
== NULL
|| data
->cmd_sock
< 0) {
155 * Not an error. Possible there is simply not spawned consumer or it's
156 * disabled for the tracing session asking the socket.
162 socket
= consumer_find_socket(data
->cmd_sock
, output
);
164 if (socket
== NULL
) {
165 socket
= consumer_allocate_socket(data
->cmd_sock
);
166 if (socket
== NULL
) {
171 socket
->registered
= 0;
172 socket
->lock
= &data
->lock
;
174 consumer_add_socket(socket
, output
);
178 DBG3("Consumer socket created (fd: %d) and added to output",
186 * Find a consumer_socket in a consumer_output hashtable. Read side lock must
187 * be acquired before calling this function and across use of the
188 * returned consumer_socket.
190 struct consumer_socket
*consumer_find_socket(int key
,
191 struct consumer_output
*consumer
)
193 struct lttng_ht_iter iter
;
194 struct lttng_ht_node_ulong
*node
;
195 struct consumer_socket
*socket
= NULL
;
197 /* Negative keys are lookup failures */
198 if (key
< 0 || consumer
== NULL
) {
202 lttng_ht_lookup(consumer
->socks
, (void *)((unsigned long) key
),
204 node
= lttng_ht_iter_get_node_ulong(&iter
);
206 socket
= caa_container_of(node
, struct consumer_socket
, node
);
213 * Allocate a new consumer_socket and return the pointer.
215 struct consumer_socket
*consumer_allocate_socket(int fd
)
217 struct consumer_socket
*socket
= NULL
;
219 socket
= zmalloc(sizeof(struct consumer_socket
));
220 if (socket
== NULL
) {
221 PERROR("zmalloc consumer socket");
226 lttng_ht_node_init_ulong(&socket
->node
, fd
);
233 * Add consumer socket to consumer output object. Read side lock must be
234 * acquired before calling this function.
236 void consumer_add_socket(struct consumer_socket
*sock
,
237 struct consumer_output
*consumer
)
242 lttng_ht_add_unique_ulong(consumer
->socks
, &sock
->node
);
246 * Delte consumer socket to consumer output object. Read side lock must be
247 * acquired before calling this function.
249 void consumer_del_socket(struct consumer_socket
*sock
,
250 struct consumer_output
*consumer
)
253 struct lttng_ht_iter iter
;
258 iter
.iter
.node
= &sock
->node
.node
;
259 ret
= lttng_ht_del(consumer
->socks
, &iter
);
264 * RCU destroy call function.
266 static void destroy_socket_rcu(struct rcu_head
*head
)
268 struct lttng_ht_node_ulong
*node
=
269 caa_container_of(head
, struct lttng_ht_node_ulong
, head
);
270 struct consumer_socket
*socket
=
271 caa_container_of(node
, struct consumer_socket
, node
);
277 * Destroy and free socket pointer in a call RCU. Read side lock must be
278 * acquired before calling this function.
280 void consumer_destroy_socket(struct consumer_socket
*sock
)
285 * We DO NOT close the file descriptor here since it is global to the
286 * session daemon and is closed only if the consumer dies or a custom
287 * consumer was registered,
289 if (sock
->registered
) {
290 DBG3("Consumer socket was registered. Closing fd %d", sock
->fd
);
291 lttcomm_close_unix_sock(sock
->fd
);
294 call_rcu(&sock
->node
.head
, destroy_socket_rcu
);
298 * Allocate and assign data to a consumer_output object.
300 * Return pointer to structure.
302 struct consumer_output
*consumer_create_output(enum consumer_dst_type type
)
304 struct consumer_output
*output
= NULL
;
306 output
= zmalloc(sizeof(struct consumer_output
));
307 if (output
== NULL
) {
308 PERROR("zmalloc consumer_output");
312 /* By default, consumer output is enabled */
315 output
->net_seq_index
= -1;
317 output
->socks
= lttng_ht_new(0, LTTNG_HT_TYPE_ULONG
);
324 * Delete the consumer_output object from the list and free the ptr.
326 void consumer_destroy_output(struct consumer_output
*obj
)
333 struct lttng_ht_iter iter
;
334 struct consumer_socket
*socket
;
337 cds_lfht_for_each_entry(obj
->socks
->ht
, &iter
.iter
, socket
, node
.node
) {
338 consumer_del_socket(socket
, obj
);
339 consumer_destroy_socket(socket
);
343 /* Finally destroy HT */
344 lttng_ht_destroy(obj
->socks
);
351 * Copy consumer output and returned the newly allocated copy.
353 struct consumer_output
*consumer_copy_output(struct consumer_output
*obj
)
355 struct lttng_ht
*tmp_ht_ptr
;
356 struct lttng_ht_iter iter
;
357 struct consumer_socket
*socket
, *copy_sock
;
358 struct consumer_output
*output
;
362 output
= consumer_create_output(obj
->type
);
363 if (output
== NULL
) {
366 /* Avoid losing the HT reference after the memcpy() */
367 tmp_ht_ptr
= output
->socks
;
369 memcpy(output
, obj
, sizeof(struct consumer_output
));
371 /* Putting back the HT pointer and start copying socket(s). */
372 output
->socks
= tmp_ht_ptr
;
374 cds_lfht_for_each_entry(obj
->socks
->ht
, &iter
.iter
, socket
, node
.node
) {
375 /* Create new socket object. */
376 copy_sock
= consumer_allocate_socket(socket
->fd
);
377 if (copy_sock
== NULL
) {
381 copy_sock
->registered
= socket
->registered
;
382 copy_sock
->lock
= socket
->lock
;
383 consumer_add_socket(copy_sock
, output
);
390 consumer_destroy_output(output
);
395 * Set network URI to the consumer output object.
397 * Return 0 on success. Return 1 if the URI were equal. Else, negative value on
400 int consumer_set_network_uri(struct consumer_output
*obj
,
401 struct lttng_uri
*uri
)
404 char tmp_path
[PATH_MAX
];
405 char hostname
[HOST_NAME_MAX
];
406 struct lttng_uri
*dst_uri
= NULL
;
408 /* Code flow error safety net. */
412 switch (uri
->stype
) {
413 case LTTNG_STREAM_CONTROL
:
414 dst_uri
= &obj
->dst
.net
.control
;
415 obj
->dst
.net
.control_isset
= 1;
416 if (uri
->port
== 0) {
417 /* Assign default port. */
418 uri
->port
= DEFAULT_NETWORK_CONTROL_PORT
;
420 DBG3("Consumer control URI set with port %d", uri
->port
);
422 case LTTNG_STREAM_DATA
:
423 dst_uri
= &obj
->dst
.net
.data
;
424 obj
->dst
.net
.data_isset
= 1;
425 if (uri
->port
== 0) {
426 /* Assign default port. */
427 uri
->port
= DEFAULT_NETWORK_DATA_PORT
;
429 DBG3("Consumer data URI set with port %d", uri
->port
);
432 ERR("Set network uri type unknown %d", uri
->stype
);
436 ret
= uri_compare(dst_uri
, uri
);
438 /* Same URI, don't touch it and return success. */
439 DBG3("URI network compare are the same");
443 /* URIs were not equal, replacing it. */
444 memset(dst_uri
, 0, sizeof(struct lttng_uri
));
445 memcpy(dst_uri
, uri
, sizeof(struct lttng_uri
));
446 obj
->type
= CONSUMER_DST_NET
;
448 /* Handle subdir and add hostname in front. */
449 if (dst_uri
->stype
== LTTNG_STREAM_CONTROL
) {
450 /* Get hostname to append it in the pathname */
451 ret
= gethostname(hostname
, sizeof(hostname
));
453 PERROR("gethostname. Fallback on default localhost");
454 strncpy(hostname
, "localhost", sizeof(hostname
));
456 hostname
[sizeof(hostname
) - 1] = '\0';
458 /* Setup consumer subdir if none present in the control URI */
459 if (strlen(dst_uri
->subdir
) == 0) {
460 ret
= snprintf(tmp_path
, sizeof(tmp_path
), "%s/%s",
461 hostname
, obj
->subdir
);
463 ret
= snprintf(tmp_path
, sizeof(tmp_path
), "%s/%s",
464 hostname
, dst_uri
->subdir
);
467 PERROR("snprintf set consumer uri subdir");
471 strncpy(obj
->subdir
, tmp_path
, sizeof(obj
->subdir
));
472 DBG3("Consumer set network uri subdir path %s", tmp_path
);
483 * Send file descriptor to consumer via sock.
485 int consumer_send_fds(struct consumer_socket
*sock
, int *fds
, size_t nb_fd
)
493 ret
= lttcomm_send_fds_unix_sock(sock
->fd
, fds
, nb_fd
);
495 PERROR("send consumer fds");
499 ret
= consumer_recv_status_reply(sock
);
506 * Consumer send channel communication message structure to consumer.
508 int consumer_send_channel(struct consumer_socket
*sock
,
509 struct lttcomm_consumer_msg
*msg
)
515 assert(sock
->fd
>= 0);
517 ret
= lttcomm_send_unix_sock(sock
->fd
, msg
,
518 sizeof(struct lttcomm_consumer_msg
));
520 PERROR("send consumer channel");
524 ret
= consumer_recv_status_reply(sock
);
531 * Init channel communication message structure.
533 void consumer_init_channel_comm_msg(struct lttcomm_consumer_msg
*msg
,
534 enum lttng_consumer_command cmd
,
536 uint64_t max_sb_size
,
539 unsigned int nb_init_streams
)
543 /* TODO: Args validation */
545 /* Zeroed structure */
546 memset(msg
, 0, sizeof(struct lttcomm_consumer_msg
));
550 msg
->u
.channel
.channel_key
= channel_key
;
551 msg
->u
.channel
.max_sb_size
= max_sb_size
;
552 msg
->u
.channel
.mmap_len
= mmap_len
;
553 msg
->u
.channel
.nb_init_streams
= nb_init_streams
;
557 * Init stream communication message structure.
559 void consumer_init_stream_comm_msg(struct lttcomm_consumer_msg
*msg
,
560 enum lttng_consumer_command cmd
,
564 enum lttng_event_output output
,
569 unsigned int metadata_flag
,
571 const char *pathname
,
572 unsigned int session_id
)
576 memset(msg
, 0, sizeof(struct lttcomm_consumer_msg
));
578 /* TODO: Args validation */
581 msg
->u
.stream
.channel_key
= channel_key
;
582 msg
->u
.stream
.stream_key
= stream_key
;
583 msg
->u
.stream
.state
= state
;
584 msg
->u
.stream
.output
= output
;
585 msg
->u
.stream
.mmap_len
= mmap_len
;
586 msg
->u
.stream
.uid
= uid
;
587 msg
->u
.stream
.gid
= gid
;
588 msg
->u
.stream
.net_index
= net_index
;
589 msg
->u
.stream
.metadata_flag
= metadata_flag
;
590 msg
->u
.stream
.session_id
= (uint64_t) session_id
;
591 strncpy(msg
->u
.stream
.name
, name
, sizeof(msg
->u
.stream
.name
));
592 msg
->u
.stream
.name
[sizeof(msg
->u
.stream
.name
) - 1] = '\0';
593 strncpy(msg
->u
.stream
.path_name
, pathname
,
594 sizeof(msg
->u
.stream
.path_name
));
595 msg
->u
.stream
.path_name
[sizeof(msg
->u
.stream
.path_name
) - 1] = '\0';
599 * Send stream communication structure to the consumer.
601 int consumer_send_stream(struct consumer_socket
*sock
,
602 struct consumer_output
*dst
, struct lttcomm_consumer_msg
*msg
,
603 int *fds
, size_t nb_fd
)
612 case CONSUMER_DST_NET
:
613 /* Consumer should send the stream on the network. */
614 msg
->u
.stream
.net_index
= dst
->net_seq_index
;
616 case CONSUMER_DST_LOCAL
:
617 /* Add stream file name to stream path */
618 strncat(msg
->u
.stream
.path_name
, "/",
619 sizeof(msg
->u
.stream
.path_name
) -
620 strlen(msg
->u
.stream
.path_name
) - 1);
621 strncat(msg
->u
.stream
.path_name
, msg
->u
.stream
.name
,
622 sizeof(msg
->u
.stream
.path_name
) -
623 strlen(msg
->u
.stream
.path_name
) - 1);
624 msg
->u
.stream
.path_name
[sizeof(msg
->u
.stream
.path_name
) - 1] = '\0';
625 /* Indicate that the stream is NOT network */
626 msg
->u
.stream
.net_index
= -1;
629 ERR("Consumer unknown output type (%d)", dst
->type
);
635 ret
= lttcomm_send_unix_sock(sock
->fd
, msg
,
636 sizeof(struct lttcomm_consumer_msg
));
638 PERROR("send consumer stream");
642 ret
= consumer_recv_status_reply(sock
);
647 ret
= consumer_send_fds(sock
, fds
, nb_fd
);
657 * Send relayd socket to consumer associated with a session name.
659 * On success return positive value. On error, negative value.
661 int consumer_send_relayd_socket(struct consumer_socket
*consumer_sock
,
662 struct lttcomm_sock
*sock
, struct consumer_output
*consumer
,
663 enum lttng_stream_type type
, unsigned int session_id
)
666 struct lttcomm_consumer_msg msg
;
668 /* Code flow error. Safety net. */
671 assert(consumer_sock
);
673 /* Bail out if consumer is disabled */
674 if (!consumer
->enabled
) {
679 msg
.cmd_type
= LTTNG_CONSUMER_ADD_RELAYD_SOCKET
;
681 * Assign network consumer output index using the temporary consumer since
682 * this call should only be made from within a set_consumer_uri() function
683 * call in the session daemon.
685 msg
.u
.relayd_sock
.net_index
= consumer
->net_seq_index
;
686 msg
.u
.relayd_sock
.type
= type
;
687 msg
.u
.relayd_sock
.session_id
= session_id
;
688 memcpy(&msg
.u
.relayd_sock
.sock
, sock
, sizeof(msg
.u
.relayd_sock
.sock
));
690 DBG3("Sending relayd sock info to consumer on %d", consumer_sock
->fd
);
691 ret
= lttcomm_send_unix_sock(consumer_sock
->fd
, &msg
, sizeof(msg
));
693 PERROR("send consumer relayd socket info");
697 ret
= consumer_recv_status_reply(consumer_sock
);
702 DBG3("Sending relayd socket file descriptor to consumer");
703 ret
= consumer_send_fds(consumer_sock
, &sock
->fd
, 1);
708 DBG2("Consumer relayd socket sent");
715 * Set consumer subdirectory using the session name and a generated datetime if
716 * needed. This is appended to the current subdirectory.
718 int consumer_set_subdir(struct consumer_output
*consumer
,
719 const char *session_name
)
722 unsigned int have_default_name
= 0;
723 char datetime
[16], tmp_path
[PATH_MAX
];
728 assert(session_name
);
730 memset(tmp_path
, 0, sizeof(tmp_path
));
732 /* Flag if we have a default session. */
733 if (strncmp(session_name
, DEFAULT_SESSION_NAME
"-",
734 strlen(DEFAULT_SESSION_NAME
) + 1) == 0) {
735 have_default_name
= 1;
737 /* Get date and time for session path */
739 timeinfo
= localtime(&rawtime
);
740 strftime(datetime
, sizeof(datetime
), "%Y%m%d-%H%M%S", timeinfo
);
743 if (have_default_name
) {
744 ret
= snprintf(tmp_path
, sizeof(tmp_path
),
745 "%s/%s", consumer
->subdir
, session_name
);
747 ret
= snprintf(tmp_path
, sizeof(tmp_path
),
748 "%s/%s-%s/", consumer
->subdir
, session_name
, datetime
);
751 PERROR("snprintf session name date");
755 strncpy(consumer
->subdir
, tmp_path
, sizeof(consumer
->subdir
));
756 DBG2("Consumer subdir set to %s", consumer
->subdir
);
763 * Ask the consumer if the data is ready to read (NOT pending) for the specific
766 * This function has a different behavior with the consumer i.e. that it waits
767 * for a reply from the consumer if yes or no the data is pending.
769 int consumer_is_data_pending(unsigned int id
,
770 struct consumer_output
*consumer
)
773 int32_t ret_code
= 0; /* Default is that the data is NOT pending */
774 struct consumer_socket
*socket
;
775 struct lttng_ht_iter iter
;
776 struct lttcomm_consumer_msg msg
;
780 msg
.cmd_type
= LTTNG_CONSUMER_DATA_PENDING
;
782 msg
.u
.data_pending
.session_id
= (uint64_t) id
;
784 DBG3("Consumer data pending for id %u", id
);
786 /* Send command for each consumer */
787 cds_lfht_for_each_entry(consumer
->socks
->ht
, &iter
.iter
, socket
,
789 /* Code flow error */
790 assert(socket
->fd
>= 0);
792 pthread_mutex_lock(socket
->lock
);
794 ret
= lttcomm_send_unix_sock(socket
->fd
, &msg
, sizeof(msg
));
796 PERROR("send consumer data pending command");
797 pthread_mutex_unlock(socket
->lock
);
802 * No need for a recv reply status because the answer to the command is
803 * the reply status message.
806 ret
= lttcomm_recv_unix_sock(socket
->fd
, &ret_code
, sizeof(ret_code
));
808 PERROR("recv consumer data pending status");
809 pthread_mutex_unlock(socket
->lock
);
813 pthread_mutex_unlock(socket
->lock
);
820 DBG("Consumer data pending ret %d", ret_code
);