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 PERROR("send consumer destroy relayd command");
98 /* Don't check the return value. The caller will do it. */
99 ret
= consumer_recv_status_reply(sock
);
101 DBG2("Consumer send destroy relayd command done");
104 pthread_mutex_unlock(sock
->lock
);
110 * For each consumer socket in the consumer output object, send a destroy
113 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
,
126 /* Send destroy relayd command */
127 ret
= consumer_send_destroy_relayd(socket
, consumer
);
129 ERR("Unable to send destroy relayd command to consumer");
130 /* Continue since we MUST delete everything at this point. */
138 * From a consumer_data structure, allocate and add a consumer socket to the
141 * Return 0 on success, else negative value on error
143 int consumer_create_socket(struct consumer_data
*data
,
144 struct consumer_output
*output
)
147 struct consumer_socket
*socket
;
151 if (output
== NULL
|| data
->cmd_sock
< 0) {
153 * Not an error. Possible there is simply not spawned consumer or it's
154 * disabled for the tracing session asking the socket.
160 socket
= consumer_find_socket(data
->cmd_sock
, output
);
162 if (socket
== NULL
) {
163 socket
= consumer_allocate_socket(data
->cmd_sock
);
164 if (socket
== NULL
) {
169 socket
->registered
= 0;
170 socket
->lock
= &data
->lock
;
172 consumer_add_socket(socket
, output
);
176 DBG3("Consumer socket created (fd: %d) and added to output",
184 * Find a consumer_socket in a consumer_output hashtable. Read side lock must
185 * be acquired before calling this function and across use of the
186 * returned consumer_socket.
188 struct consumer_socket
*consumer_find_socket(int key
,
189 struct consumer_output
*consumer
)
191 struct lttng_ht_iter iter
;
192 struct lttng_ht_node_ulong
*node
;
193 struct consumer_socket
*socket
= NULL
;
195 /* Negative keys are lookup failures */
196 if (key
< 0 || consumer
== NULL
) {
200 lttng_ht_lookup(consumer
->socks
, (void *)((unsigned long) key
),
202 node
= lttng_ht_iter_get_node_ulong(&iter
);
204 socket
= caa_container_of(node
, struct consumer_socket
, node
);
211 * Allocate a new consumer_socket and return the pointer.
213 struct consumer_socket
*consumer_allocate_socket(int fd
)
215 struct consumer_socket
*socket
= NULL
;
217 socket
= zmalloc(sizeof(struct consumer_socket
));
218 if (socket
== NULL
) {
219 PERROR("zmalloc consumer socket");
224 lttng_ht_node_init_ulong(&socket
->node
, fd
);
231 * Add consumer socket to consumer output object. Read side lock must be
232 * acquired before calling this function.
234 void consumer_add_socket(struct consumer_socket
*sock
,
235 struct consumer_output
*consumer
)
240 lttng_ht_add_unique_ulong(consumer
->socks
, &sock
->node
);
244 * Delte consumer socket to consumer output object. Read side lock must be
245 * acquired before calling this function.
247 void consumer_del_socket(struct consumer_socket
*sock
,
248 struct consumer_output
*consumer
)
251 struct lttng_ht_iter iter
;
256 iter
.iter
.node
= &sock
->node
.node
;
257 ret
= lttng_ht_del(consumer
->socks
, &iter
);
262 * RCU destroy call function.
264 static void destroy_socket_rcu(struct rcu_head
*head
)
266 struct lttng_ht_node_ulong
*node
=
267 caa_container_of(head
, struct lttng_ht_node_ulong
, head
);
268 struct consumer_socket
*socket
=
269 caa_container_of(node
, struct consumer_socket
, node
);
275 * Destroy and free socket pointer in a call RCU. Read side lock must be
276 * acquired before calling this function.
278 void consumer_destroy_socket(struct consumer_socket
*sock
)
283 * We DO NOT close the file descriptor here since it is global to the
284 * session daemon and is closed only if the consumer dies or a custom
285 * consumer was registered,
287 if (sock
->registered
) {
288 DBG3("Consumer socket was registered. Closing fd %d", sock
->fd
);
289 lttcomm_close_unix_sock(sock
->fd
);
292 call_rcu(&sock
->node
.head
, destroy_socket_rcu
);
296 * Allocate and assign data to a consumer_output object.
298 * Return pointer to structure.
300 struct consumer_output
*consumer_create_output(enum consumer_dst_type type
)
302 struct consumer_output
*output
= NULL
;
304 output
= zmalloc(sizeof(struct consumer_output
));
305 if (output
== NULL
) {
306 PERROR("zmalloc consumer_output");
310 /* By default, consumer output is enabled */
313 output
->net_seq_index
= -1;
315 output
->socks
= lttng_ht_new(0, LTTNG_HT_TYPE_ULONG
);
322 * Delete the consumer_output object from the list and free the ptr.
324 void consumer_destroy_output(struct consumer_output
*obj
)
331 struct lttng_ht_iter iter
;
332 struct consumer_socket
*socket
;
335 cds_lfht_for_each_entry(obj
->socks
->ht
, &iter
.iter
, socket
, node
.node
) {
336 consumer_del_socket(socket
, obj
);
337 consumer_destroy_socket(socket
);
341 /* Finally destroy HT */
342 lttng_ht_destroy(obj
->socks
);
349 * Copy consumer output and returned the newly allocated copy.
351 struct consumer_output
*consumer_copy_output(struct consumer_output
*obj
)
353 struct lttng_ht
*tmp_ht_ptr
;
354 struct lttng_ht_iter iter
;
355 struct consumer_socket
*socket
, *copy_sock
;
356 struct consumer_output
*output
;
360 output
= consumer_create_output(obj
->type
);
361 if (output
== NULL
) {
364 /* Avoid losing the HT reference after the memcpy() */
365 tmp_ht_ptr
= output
->socks
;
367 memcpy(output
, obj
, sizeof(struct consumer_output
));
369 /* Putting back the HT pointer and start copying socket(s). */
370 output
->socks
= tmp_ht_ptr
;
372 cds_lfht_for_each_entry(obj
->socks
->ht
, &iter
.iter
, socket
, node
.node
) {
373 /* Create new socket object. */
374 copy_sock
= consumer_allocate_socket(socket
->fd
);
375 if (copy_sock
== NULL
) {
379 copy_sock
->registered
= socket
->registered
;
380 copy_sock
->lock
= socket
->lock
;
381 consumer_add_socket(copy_sock
, output
);
388 consumer_destroy_output(output
);
393 * Set network URI to the consumer output object.
395 * Return 0 on success. Return 1 if the URI were equal. Else, negative value on
398 int consumer_set_network_uri(struct consumer_output
*obj
,
399 struct lttng_uri
*uri
)
402 char tmp_path
[PATH_MAX
];
403 char hostname
[HOST_NAME_MAX
];
404 struct lttng_uri
*dst_uri
= NULL
;
406 /* Code flow error safety net. */
410 switch (uri
->stype
) {
411 case LTTNG_STREAM_CONTROL
:
412 dst_uri
= &obj
->dst
.net
.control
;
413 obj
->dst
.net
.control_isset
= 1;
414 if (uri
->port
== 0) {
415 /* Assign default port. */
416 uri
->port
= DEFAULT_NETWORK_CONTROL_PORT
;
418 DBG3("Consumer control URI set with port %d", uri
->port
);
420 case LTTNG_STREAM_DATA
:
421 dst_uri
= &obj
->dst
.net
.data
;
422 obj
->dst
.net
.data_isset
= 1;
423 if (uri
->port
== 0) {
424 /* Assign default port. */
425 uri
->port
= DEFAULT_NETWORK_DATA_PORT
;
427 DBG3("Consumer data URI set with port %d", uri
->port
);
430 ERR("Set network uri type unknown %d", uri
->stype
);
434 ret
= uri_compare(dst_uri
, uri
);
436 /* Same URI, don't touch it and return success. */
437 DBG3("URI network compare are the same");
441 /* URIs were not equal, replacing it. */
442 memset(dst_uri
, 0, sizeof(struct lttng_uri
));
443 memcpy(dst_uri
, uri
, sizeof(struct lttng_uri
));
444 obj
->type
= CONSUMER_DST_NET
;
446 /* Handle subdir and add hostname in front. */
447 if (dst_uri
->stype
== LTTNG_STREAM_CONTROL
) {
448 /* Get hostname to append it in the pathname */
449 ret
= gethostname(hostname
, sizeof(hostname
));
451 PERROR("gethostname. Fallback on default localhost");
452 strncpy(hostname
, "localhost", sizeof(hostname
));
454 hostname
[sizeof(hostname
) - 1] = '\0';
456 /* Setup consumer subdir if none present in the control URI */
457 if (strlen(dst_uri
->subdir
) == 0) {
458 ret
= snprintf(tmp_path
, sizeof(tmp_path
), "%s/%s",
459 hostname
, obj
->subdir
);
461 ret
= snprintf(tmp_path
, sizeof(tmp_path
), "%s/%s",
462 hostname
, dst_uri
->subdir
);
465 PERROR("snprintf set consumer uri subdir");
469 strncpy(obj
->subdir
, tmp_path
, sizeof(obj
->subdir
));
470 DBG3("Consumer set network uri subdir path %s", tmp_path
);
481 * Send file descriptor to consumer via sock.
483 int consumer_send_fds(struct consumer_socket
*sock
, int *fds
, size_t nb_fd
)
491 ret
= lttcomm_send_fds_unix_sock(sock
->fd
, fds
, nb_fd
);
493 PERROR("send consumer fds");
497 ret
= consumer_recv_status_reply(sock
);
504 * Consumer send channel communication message structure to consumer.
506 int consumer_send_channel(struct consumer_socket
*sock
,
507 struct lttcomm_consumer_msg
*msg
)
513 assert(sock
->fd
>= 0);
515 ret
= lttcomm_send_unix_sock(sock
->fd
, msg
,
516 sizeof(struct lttcomm_consumer_msg
));
518 PERROR("send consumer channel");
522 ret
= consumer_recv_status_reply(sock
);
529 * Init channel communication message structure.
531 void consumer_init_channel_comm_msg(struct lttcomm_consumer_msg
*msg
,
532 enum lttng_consumer_command cmd
,
534 uint64_t max_sb_size
,
537 unsigned int nb_init_streams
)
541 /* TODO: Args validation */
543 /* Zeroed structure */
544 memset(msg
, 0, sizeof(struct lttcomm_consumer_msg
));
548 msg
->u
.channel
.channel_key
= channel_key
;
549 msg
->u
.channel
.max_sb_size
= max_sb_size
;
550 msg
->u
.channel
.mmap_len
= mmap_len
;
551 msg
->u
.channel
.nb_init_streams
= nb_init_streams
;
555 * Init stream communication message structure.
557 void consumer_init_stream_comm_msg(struct lttcomm_consumer_msg
*msg
,
558 enum lttng_consumer_command cmd
,
562 enum lttng_event_output output
,
567 unsigned int metadata_flag
,
569 const char *pathname
,
570 unsigned int session_id
)
574 memset(msg
, 0, sizeof(struct lttcomm_consumer_msg
));
576 /* TODO: Args validation */
579 msg
->u
.stream
.channel_key
= channel_key
;
580 msg
->u
.stream
.stream_key
= stream_key
;
581 msg
->u
.stream
.state
= state
;
582 msg
->u
.stream
.output
= output
;
583 msg
->u
.stream
.mmap_len
= mmap_len
;
584 msg
->u
.stream
.uid
= uid
;
585 msg
->u
.stream
.gid
= gid
;
586 msg
->u
.stream
.net_index
= net_index
;
587 msg
->u
.stream
.metadata_flag
= metadata_flag
;
588 msg
->u
.stream
.session_id
= (uint64_t) session_id
;
589 strncpy(msg
->u
.stream
.name
, name
, sizeof(msg
->u
.stream
.name
));
590 msg
->u
.stream
.name
[sizeof(msg
->u
.stream
.name
) - 1] = '\0';
591 strncpy(msg
->u
.stream
.path_name
, pathname
,
592 sizeof(msg
->u
.stream
.path_name
));
593 msg
->u
.stream
.path_name
[sizeof(msg
->u
.stream
.path_name
) - 1] = '\0';
597 * Send stream communication structure to the consumer.
599 int consumer_send_stream(struct consumer_socket
*sock
,
600 struct consumer_output
*dst
, struct lttcomm_consumer_msg
*msg
,
601 int *fds
, size_t nb_fd
)
610 case CONSUMER_DST_NET
:
611 /* Consumer should send the stream on the network. */
612 msg
->u
.stream
.net_index
= dst
->net_seq_index
;
614 case CONSUMER_DST_LOCAL
:
615 /* Add stream file name to stream path */
616 strncat(msg
->u
.stream
.path_name
, "/",
617 sizeof(msg
->u
.stream
.path_name
) -
618 strlen(msg
->u
.stream
.path_name
) - 1);
619 strncat(msg
->u
.stream
.path_name
, msg
->u
.stream
.name
,
620 sizeof(msg
->u
.stream
.path_name
) -
621 strlen(msg
->u
.stream
.path_name
) - 1);
622 msg
->u
.stream
.path_name
[sizeof(msg
->u
.stream
.path_name
) - 1] = '\0';
623 /* Indicate that the stream is NOT network */
624 msg
->u
.stream
.net_index
= -1;
627 ERR("Consumer unknown output type (%d)", dst
->type
);
633 ret
= lttcomm_send_unix_sock(sock
->fd
, msg
,
634 sizeof(struct lttcomm_consumer_msg
));
636 PERROR("send consumer stream");
640 ret
= consumer_recv_status_reply(sock
);
645 ret
= consumer_send_fds(sock
, fds
, nb_fd
);
655 * Send relayd socket to consumer associated with a session name.
657 * On success return positive value. On error, negative value.
659 int consumer_send_relayd_socket(struct consumer_socket
*consumer_sock
,
660 struct lttcomm_sock
*sock
, struct consumer_output
*consumer
,
661 enum lttng_stream_type type
)
664 struct lttcomm_consumer_msg msg
;
666 /* Code flow error. Safety net. */
669 assert(consumer_sock
);
671 /* Bail out if consumer is disabled */
672 if (!consumer
->enabled
) {
677 msg
.cmd_type
= LTTNG_CONSUMER_ADD_RELAYD_SOCKET
;
679 * Assign network consumer output index using the temporary consumer since
680 * this call should only be made from within a set_consumer_uri() function
681 * call in the session daemon.
683 msg
.u
.relayd_sock
.net_index
= consumer
->net_seq_index
;
684 msg
.u
.relayd_sock
.type
= type
;
685 memcpy(&msg
.u
.relayd_sock
.sock
, sock
, sizeof(msg
.u
.relayd_sock
.sock
));
687 DBG3("Sending relayd sock info to consumer on %d", consumer_sock
->fd
);
688 ret
= lttcomm_send_unix_sock(consumer_sock
->fd
, &msg
, sizeof(msg
));
690 PERROR("send consumer relayd socket info");
694 ret
= consumer_recv_status_reply(consumer_sock
);
699 DBG3("Sending relayd socket file descriptor to consumer");
700 ret
= consumer_send_fds(consumer_sock
, &sock
->fd
, 1);
705 DBG2("Consumer relayd socket sent");
712 * Set consumer subdirectory using the session name and a generated datetime if
713 * needed. This is appended to the current subdirectory.
715 int consumer_set_subdir(struct consumer_output
*consumer
,
716 const char *session_name
)
719 unsigned int have_default_name
= 0;
720 char datetime
[16], tmp_path
[PATH_MAX
];
725 assert(session_name
);
727 memset(tmp_path
, 0, sizeof(tmp_path
));
729 /* Flag if we have a default session. */
730 if (strncmp(session_name
, DEFAULT_SESSION_NAME
"-",
731 strlen(DEFAULT_SESSION_NAME
) + 1) == 0) {
732 have_default_name
= 1;
734 /* Get date and time for session path */
736 timeinfo
= localtime(&rawtime
);
737 strftime(datetime
, sizeof(datetime
), "%Y%m%d-%H%M%S", timeinfo
);
740 if (have_default_name
) {
741 ret
= snprintf(tmp_path
, sizeof(tmp_path
),
742 "%s/%s", consumer
->subdir
, session_name
);
744 ret
= snprintf(tmp_path
, sizeof(tmp_path
),
745 "%s/%s-%s/", consumer
->subdir
, session_name
, datetime
);
748 PERROR("snprintf session name date");
752 strncpy(consumer
->subdir
, tmp_path
, sizeof(consumer
->subdir
));
753 DBG2("Consumer subdir set to %s", consumer
->subdir
);
760 * Ask the consumer if the data is ready to read (NOT pending) for the specific
763 * This function has a different behavior with the consumer i.e. that it waits
764 * for a reply from the consumer if yes or no the data is pending.
766 int consumer_is_data_pending(unsigned int id
,
767 struct consumer_output
*consumer
)
770 int32_t ret_code
= 0; /* Default is that the data is NOT pending */
771 struct consumer_socket
*socket
;
772 struct lttng_ht_iter iter
;
773 struct lttcomm_consumer_msg msg
;
777 msg
.cmd_type
= LTTNG_CONSUMER_DATA_PENDING
;
779 msg
.u
.data_pending
.session_id
= (uint64_t) id
;
781 DBG3("Consumer data pending for id %u", id
);
783 /* Send command for each consumer */
784 cds_lfht_for_each_entry(consumer
->socks
->ht
, &iter
.iter
, socket
,
786 /* Code flow error */
787 assert(socket
->fd
>= 0);
789 pthread_mutex_lock(socket
->lock
);
791 ret
= lttcomm_send_unix_sock(socket
->fd
, &msg
, sizeof(msg
));
793 PERROR("send consumer data pending command");
794 pthread_mutex_unlock(socket
->lock
);
799 * No need for a recv reply status because the answer to the command is
800 * the reply status message.
803 ret
= lttcomm_recv_unix_sock(socket
->fd
, &ret_code
, sizeof(ret_code
));
805 PERROR("recv consumer data pending status");
806 pthread_mutex_unlock(socket
->lock
);
810 pthread_mutex_unlock(socket
->lock
);
817 DBG("Consumer data pending ret %d", ret_code
);