2 * Copyright (C) 2013 - Julien Desfossez <jdesfossez@efficios.com>
3 * David Goulet <dgoulet@efficios.com>
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License, version 2 only,
7 * as published by the Free Software Foundation.
9 * This program is distributed in the hope that it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
14 * You should have received a copy of the GNU General Public License along
15 * with this program; if not, write to the Free Software Foundation, Inc.,
16 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
29 #include <sys/mount.h>
30 #include <sys/resource.h>
31 #include <sys/socket.h>
33 #include <sys/types.h>
36 #include <urcu/futex.h>
37 #include <urcu/uatomic.h>
42 #include <lttng/lttng.h>
43 #include <common/common.h>
44 #include <common/compat/poll.h>
45 #include <common/compat/socket.h>
46 #include <common/defaults.h>
47 #include <common/futex.h>
48 #include <common/sessiond-comm/sessiond-comm.h>
49 #include <common/sessiond-comm/inet.h>
50 #include <common/sessiond-comm/relayd.h>
51 #include <common/uri.h>
52 #include <common/utils.h>
56 #include "lttng-relayd.h"
57 #include "lttng-viewer.h"
60 static struct lttng_uri
*live_uri
;
63 * Quit pipe for all threads. This permits a single cancellation point
64 * for all threads when receiving an event on the pipe.
66 static int live_thread_quit_pipe
[2] = { -1, -1 };
69 * This pipe is used to inform the worker thread that a command is queued and
70 * ready to be processed.
72 static int live_relay_cmd_pipe
[2] = { -1, -1 };
74 /* Shared between threads */
75 static int live_dispatch_thread_exit
;
77 static pthread_t live_listener_thread
;
78 static pthread_t live_dispatcher_thread
;
79 static pthread_t live_worker_thread
;
82 * Relay command queue.
84 * The live_thread_listener and live_thread_dispatcher communicate with this
87 static struct relay_cmd_queue viewer_cmd_queue
;
89 static uint64_t last_relay_viewer_session_id
;
99 /* Close thread quit pipes */
100 utils_close_pipe(live_thread_quit_pipe
);
105 * Write to writable pipe used to notify a thread.
108 int notify_thread_pipe(int wpipe
)
113 ret
= write(wpipe
, "!", 1);
114 } while (ret
< 0 && errno
== EINTR
);
115 if (ret
< 0 || ret
!= 1) {
116 PERROR("write poll pipe");
123 * Stop all threads by closing the thread quit pipe.
126 void stop_threads(void)
130 /* Stopping all threads */
131 DBG("Terminating all live threads");
132 ret
= notify_thread_pipe(live_thread_quit_pipe
[1]);
134 ERR("write error on thread quit pipe");
137 /* Dispatch thread */
138 CMM_STORE_SHARED(live_dispatch_thread_exit
, 1);
139 futex_nto1_wake(&viewer_cmd_queue
.futex
);
143 * Init thread quit pipe.
145 * Return -1 on error or 0 if all pipes are created.
148 int init_thread_quit_pipe(void)
152 ret
= utils_create_pipe_cloexec(live_thread_quit_pipe
);
158 * Create a poll set with O_CLOEXEC and add the thread quit pipe to the set.
161 int create_thread_poll_set(struct lttng_poll_event
*events
, int size
)
165 if (events
== NULL
|| size
== 0) {
170 ret
= lttng_poll_create(events
, size
, LTTNG_CLOEXEC
);
176 ret
= lttng_poll_add(events
, live_thread_quit_pipe
[0], LPOLLIN
);
188 * Check if the thread quit pipe was triggered.
190 * Return 1 if it was triggered else 0;
193 int check_thread_quit_pipe(int fd
, uint32_t events
)
195 if (fd
== live_thread_quit_pipe
[0] && (events
& LPOLLIN
)) {
203 * Create and init socket from uri.
206 struct lttcomm_sock
*init_socket(struct lttng_uri
*uri
)
209 struct lttcomm_sock
*sock
= NULL
;
211 sock
= lttcomm_alloc_sock_from_uri(uri
);
213 ERR("Allocating socket");
217 ret
= lttcomm_create_sock(sock
);
221 DBG("Listening on sock %d for live", sock
->fd
);
223 ret
= sock
->ops
->bind(sock
);
228 ret
= sock
->ops
->listen(sock
, -1);
238 lttcomm_destroy_sock(sock
);
244 * This thread manages the listening for new connections on the network
247 void *thread_listener(void *data
)
249 int i
, ret
, pollfd
, err
= -1;
251 uint32_t revents
, nb_fd
;
252 struct lttng_poll_event events
;
253 struct lttcomm_sock
*live_control_sock
;
255 DBG("[thread] Relay live listener started");
257 live_control_sock
= init_socket(live_uri
);
258 if (!live_control_sock
) {
259 goto error_sock_control
;
263 * Pass 3 as size here for the thread quit pipe, control and data socket.
265 ret
= create_thread_poll_set(&events
, 2);
267 goto error_create_poll
;
270 /* Add the control socket */
271 ret
= lttng_poll_add(&events
, live_control_sock
->fd
, LPOLLIN
| LPOLLRDHUP
);
277 DBG("Listener accepting live viewers connections");
280 ret
= lttng_poll_wait(&events
, -1);
283 * Restart interrupted system call.
285 if (errno
== EINTR
) {
292 DBG("Relay new viewer connection received");
293 for (i
= 0; i
< nb_fd
; i
++) {
294 /* Fetch once the poll data */
295 revents
= LTTNG_POLL_GETEV(&events
, i
);
296 pollfd
= LTTNG_POLL_GETFD(&events
, i
);
298 /* Thread quit pipe has been closed. Killing thread. */
299 ret
= check_thread_quit_pipe(pollfd
, revents
);
305 if (revents
& (LPOLLERR
| LPOLLHUP
| LPOLLRDHUP
)) {
306 ERR("socket poll error");
308 } else if (revents
& LPOLLIN
) {
310 * Get allocated in this thread, enqueued to a global queue,
311 * dequeued and freed in the worker thread.
313 struct relay_command
*relay_cmd
;
314 struct lttcomm_sock
*newsock
;
316 relay_cmd
= zmalloc(sizeof(*relay_cmd
));
318 PERROR("relay command zmalloc");
322 assert(pollfd
== live_control_sock
->fd
);
323 newsock
= live_control_sock
->ops
->accept(live_control_sock
);
325 PERROR("accepting control sock");
329 DBG("Relay viewer connection accepted socket %d", newsock
->fd
);
330 ret
= setsockopt(newsock
->fd
, SOL_SOCKET
, SO_REUSEADDR
, &val
,
333 PERROR("setsockopt inet");
334 lttcomm_destroy_sock(newsock
);
338 relay_cmd
->sock
= newsock
;
341 * Lock free enqueue the request.
343 cds_wfq_enqueue(&viewer_cmd_queue
.queue
, &relay_cmd
->node
);
346 * Wake the dispatch queue futex. Implicit memory
347 * barrier with the exchange in cds_wfq_enqueue.
349 futex_nto1_wake(&viewer_cmd_queue
.futex
);
357 lttng_poll_clean(&events
);
359 if (live_control_sock
->fd
>= 0) {
360 ret
= live_control_sock
->ops
->close(live_control_sock
);
365 lttcomm_destroy_sock(live_control_sock
);
368 DBG("Live viewer listener thread exited with error");
370 DBG("Live viewer listener thread cleanup complete");
376 * This thread manages the dispatching of the requests to worker threads
379 void *thread_dispatcher(void *data
)
382 struct cds_wfq_node
*node
;
383 struct relay_command
*relay_cmd
= NULL
;
385 DBG("[thread] Live viewer relay dispatcher started");
387 while (!CMM_LOAD_SHARED(live_dispatch_thread_exit
)) {
388 /* Atomically prepare the queue futex */
389 futex_nto1_prepare(&viewer_cmd_queue
.futex
);
392 /* Dequeue commands */
393 node
= cds_wfq_dequeue_blocking(&viewer_cmd_queue
.queue
);
395 DBG("Woken up but nothing in the live-viewer "
396 "relay command queue");
397 /* Continue thread execution */
401 relay_cmd
= caa_container_of(node
, struct relay_command
, node
);
402 DBG("Dispatching viewer request waiting on sock %d",
403 relay_cmd
->sock
->fd
);
406 * Inform worker thread of the new request. This call is blocking
407 * so we can be assured that the data will be read at some point in
408 * time or wait to the end of the world :)
411 ret
= write(live_relay_cmd_pipe
[1], relay_cmd
,
413 } while (ret
< 0 && errno
== EINTR
);
415 if (ret
< 0 || ret
!= sizeof(struct relay_command
)) {
416 PERROR("write cmd pipe");
419 } while (node
!= NULL
);
421 /* Futex wait on queue. Blocking call on futex() */
422 futex_nto1_wait(&viewer_cmd_queue
.futex
);
426 DBG("Live viewer dispatch thread dying");
432 * Establish connection with the viewer and check the versions.
434 * Return 0 on success or else negative value.
437 int viewer_connect(struct relay_command
*cmd
)
440 struct lttng_viewer_connect reply
, msg
;
444 cmd
->version_check_done
= 1;
446 /* Get version from the other side. */
447 ret
= cmd
->sock
->ops
->recvmsg(cmd
->sock
, &msg
, sizeof(msg
), 0);
448 if (ret
< 0 || ret
!= sizeof(msg
)) {
450 /* Orderly shutdown. Not necessary to print an error. */
451 DBG("Socket %d did an orderly shutdown", cmd
->sock
->fd
);
453 ERR("Relay failed to receive the version values.");
459 reply
.major
= RELAYD_VERSION_COMM_MAJOR
;
460 reply
.minor
= RELAYD_VERSION_COMM_MINOR
;
462 /* Major versions must be the same */
463 if (reply
.major
!= be32toh(msg
.major
)) {
464 DBG("Incompatible major versions (%u vs %u)", reply
.major
,
470 cmd
->major
= reply
.major
;
471 /* We adapt to the lowest compatible version */
472 if (reply
.minor
<= be32toh(msg
.minor
)) {
473 cmd
->minor
= reply
.minor
;
475 cmd
->minor
= be32toh(msg
.minor
);
478 if (be32toh(msg
.type
) == VIEWER_CLIENT_COMMAND
) {
479 cmd
->type
= RELAY_VIEWER_COMMAND
;
480 } else if (be32toh(msg
.type
) == VIEWER_CLIENT_NOTIFICATION
) {
481 cmd
->type
= RELAY_VIEWER_NOTIFICATION
;
483 ERR("Unknown connection type : %u", be32toh(msg
.type
));
488 reply
.major
= htobe32(reply
.major
);
489 reply
.minor
= htobe32(reply
.minor
);
490 if (cmd
->type
== RELAY_VIEWER_COMMAND
) {
491 reply
.viewer_session_id
= htobe64(++last_relay_viewer_session_id
);
493 ret
= cmd
->sock
->ops
->sendmsg(cmd
->sock
, &reply
,
494 sizeof(struct lttng_viewer_connect
), 0);
496 ERR("Relay sending version");
499 DBG("Version check done using protocol %u.%u", cmd
->major
, cmd
->minor
);
507 * Send the viewer the list of current sessions.
509 * Return 0 on success or else a negative value.
512 int viewer_list_sessions(struct relay_command
*cmd
,
513 struct lttng_ht
*sessions_ht
)
516 struct lttng_viewer_list_sessions session_list
;
518 long approx_before
, approx_after
;
519 struct lttng_ht_node_ulong
*node
;
520 struct lttng_ht_iter iter
;
521 struct lttng_viewer_session send_session
;
522 struct relay_session
*session
;
524 DBG("List sessions received");
526 if (cmd
->version_check_done
== 0) {
527 ERR("Trying to list sessions before version check");
533 cds_lfht_count_nodes(sessions_ht
->ht
, &approx_before
, &count
, &approx_after
);
534 session_list
.sessions_count
= htobe32(count
);
536 ret
= cmd
->sock
->ops
->sendmsg(cmd
->sock
, &session_list
,
537 sizeof(session_list
), 0);
539 ERR("Relay sending sessions list");
543 cds_lfht_for_each_entry(sessions_ht
->ht
, &iter
.iter
, node
, node
) {
544 node
= lttng_ht_iter_get_node_ulong(&iter
);
548 session
= caa_container_of(node
, struct relay_session
, session_n
);
550 strncpy(send_session
.session_name
, session
->session_name
,
551 sizeof(send_session
.session_name
));
552 strncpy(send_session
.hostname
, session
->hostname
,
553 sizeof(send_session
.hostname
));
554 send_session
.id
= htobe64(session
->id
);
555 send_session
.live_timer
= htobe32(session
->live_timer
);
556 send_session
.clients
= htobe32(session
->viewer_attached
);
558 ret
= cmd
->sock
->ops
->sendmsg(cmd
->sock
, &send_session
,
559 sizeof(send_session
), 0);
561 ERR("Relay sending session info");
578 * Allocate and init a new viewer_stream.
580 * Copies the values from the stream passed in parameter and insert the new
581 * stream in the viewer_streams_ht.
583 * MUST be called with rcu_read_lock held.
585 * Returns 0 on success or a negative value on error.
588 int init_viewer_stream(struct relay_stream
*stream
)
591 struct relay_viewer_stream
*viewer_stream
;
595 viewer_stream
= zmalloc(sizeof(*viewer_stream
));
596 if (!viewer_stream
) {
597 PERROR("relay viewer stream zmalloc");
602 viewer_stream
->read_fd
= -1;
603 viewer_stream
->index_read_fd
= -1;
604 viewer_stream
->session_id
= stream
->session
->id
;
605 viewer_stream
->stream_handle
= stream
->stream_handle
;
606 viewer_stream
->path_name
= strndup(stream
->path_name
,
607 LTTNG_VIEWER_PATH_MAX
);
608 viewer_stream
->channel_name
= strndup(stream
->channel_name
,
609 LTTNG_VIEWER_NAME_MAX
);
610 viewer_stream
->total_index_received
= stream
->total_index_received
;
611 viewer_stream
->tracefile_size
= stream
->tracefile_size
;
612 viewer_stream
->tracefile_count
= stream
->tracefile_count
;
613 viewer_stream
->metadata_flag
= stream
->metadata_flag
;
616 * This is to avoid a race between the initialization of this object and
617 * the close of the given stream. If the stream is unable to find this
618 * viewer stream when closing, this copy will at least take the latest
621 viewer_stream
->total_index_received
= stream
->total_index_received
;
624 * The deletion of this ctf_trace object is only done in a call RCU of the
625 * relay stream making it valid as long as we have the read side lock.
627 viewer_stream
->ctf_trace
= stream
->ctf_trace
;
628 uatomic_inc(&viewer_stream
->ctf_trace
->refcount
);
630 lttng_ht_node_init_u64(&viewer_stream
->stream_n
, stream
->stream_handle
);
631 lttng_ht_add_unique_u64(viewer_streams_ht
, &viewer_stream
->stream_n
);
640 * Send the viewer the list of current sessions.
643 int viewer_attach_session(struct relay_command
*cmd
,
644 struct lttng_ht
*sessions_ht
)
646 int ret
, send_streams
= 0, nb_streams
= 0;
647 struct lttng_viewer_attach_session_request request
;
648 struct lttng_viewer_attach_session_response response
;
649 struct lttng_viewer_stream send_stream
;
650 struct relay_stream
*stream
;
651 struct relay_viewer_stream
*viewer_stream
;
652 struct lttng_ht_node_ulong
*node
;
653 struct lttng_ht_node_u64
*node64
;
654 struct lttng_ht_iter iter
;
655 struct relay_session
*session
;
660 DBG("Attach session received");
662 if (cmd
->version_check_done
== 0) {
663 ERR("Trying to attach session before version check");
668 ret
= cmd
->sock
->ops
->recvmsg(cmd
->sock
, &request
, sizeof(request
), 0);
669 if (ret
< 0 || ret
!= sizeof(request
)) {
671 /* Orderly shutdown. Not necessary to print an error. */
672 DBG("Socket %d did an orderly shutdown", cmd
->sock
->fd
);
674 ERR("Relay failed to receive the attach parameters.");
681 lttng_ht_lookup(sessions_ht
,
682 (void *)((unsigned long) be64toh(request
.session_id
)), &iter
);
683 node
= lttng_ht_iter_get_node_ulong(&iter
);
685 DBG("Relay session %" PRIu64
" not found",
686 be64toh(request
.session_id
));
687 response
.status
= htobe32(VIEWER_ATTACH_UNK
);
691 session
= caa_container_of(node
, struct relay_session
, session_n
);
692 if (cmd
->session
== session
) {
693 /* Same viewer already attached, just send the stream list. */
695 response
.status
= htobe32(VIEWER_ATTACH_OK
);
696 } else if (session
->viewer_attached
!= 0) {
697 DBG("Already a viewer attached");
698 response
.status
= htobe32(VIEWER_ATTACH_ALREADY
);
700 } else if (session
->live_timer
== 0) {
701 DBG("Not live session");
702 response
.status
= htobe32(VIEWER_ATTACH_NOT_LIVE
);
705 session
->viewer_attached
++;
707 response
.status
= htobe32(VIEWER_ATTACH_OK
);
708 cmd
->session
= session
;
711 switch (be32toh(request
.seek
)) {
712 case VIEWER_SEEK_BEGINNING
:
713 /* Default behaviour. */
715 case VIEWER_SEEK_LAST
:
719 ERR("Wrong seek parameter");
720 response
.status
= htobe32(VIEWER_ATTACH_SEEK_ERR
);
726 /* We should only be there if we have a session to attach to. */
730 * Fill the viewer_streams_ht to count the number of streams
731 * ready to be sent and avoid concurrency issues on the
732 * relay_streams_ht and don't rely on a total session stream count.
734 cds_lfht_for_each_entry(relay_streams_ht
->ht
, &iter
.iter
, node
, node
) {
735 struct relay_viewer_stream
*vstream
;
737 node
= lttng_ht_iter_get_node_ulong(&iter
);
741 stream
= caa_container_of(node
, struct relay_stream
, stream_n
);
742 if (stream
->session
!= cmd
->session
) {
747 * Don't send streams with no ctf_trace, they are not ready to be
750 if (!stream
->ctf_trace
) {
754 vstream
= live_find_viewer_stream_by_id(stream
->stream_handle
);
756 ret
= init_viewer_stream(stream
);
763 response
.streams_count
= htobe32(nb_streams
);
767 ret
= cmd
->sock
->ops
->sendmsg(cmd
->sock
, &response
, sizeof(response
), 0);
769 ERR("Relay sending viewer attach response");
774 * Unknown or busy session, just return gracefully, the viewer knows what
782 /* We should only be there if we have a session to attach to. */
784 cds_lfht_for_each_entry(viewer_streams_ht
->ht
, &iter
.iter
, node
, node
) {
785 node64
= lttng_ht_iter_get_node_u64(&iter
);
789 viewer_stream
= caa_container_of(node64
, struct relay_viewer_stream
,
791 if (viewer_stream
->session_id
!= cmd
->session
->id
) {
795 send_stream
.id
= htobe64(viewer_stream
->stream_handle
);
796 send_stream
.ctf_trace_id
= htobe64(viewer_stream
->ctf_trace
->id
);
797 send_stream
.metadata_flag
= htobe32(viewer_stream
->metadata_flag
);
798 strncpy(send_stream
.path_name
, viewer_stream
->path_name
,
799 sizeof(send_stream
.path_name
));
800 strncpy(send_stream
.channel_name
, viewer_stream
->channel_name
,
801 sizeof(send_stream
.channel_name
));
803 ret
= cmd
->sock
->ops
->sendmsg(cmd
->sock
, &send_stream
,
804 sizeof(send_stream
), 0);
806 ERR("Relay sending stream %" PRIu64
, viewer_stream
->stream_handle
);
809 DBG("Sent stream %" PRIu64
" to viewer", viewer_stream
->stream_handle
);
821 * Open index file using a given viewer stream.
823 * Return 0 on success or else a negative value.
825 static int open_index(struct relay_viewer_stream
*stream
)
828 char fullpath
[PATH_MAX
];
829 struct lttng_packet_index_file_hdr hdr
;
831 if (stream
->tracefile_size
> 0) {
832 /* For now we don't support on-disk ring buffer. */
836 ret
= snprintf(fullpath
, sizeof(fullpath
), "%s/" DEFAULT_INDEX_DIR
837 "/%s" DEFAULT_INDEX_FILE_SUFFIX
,
838 stream
->path_name
, stream
->channel_name
);
840 PERROR("snprintf index path");
845 DBG("Opening index file %s in read only", fullpath
);
846 ret
= open(fullpath
, O_RDONLY
);
848 if (errno
== ENOENT
) {
852 PERROR("opening index in read-only");
856 stream
->index_read_fd
= ret
;
857 DBG("Opening index file %s in read only, (fd: %d)", fullpath
, ret
);
860 ret
= read(stream
->index_read_fd
, &hdr
, sizeof(hdr
));
861 } while (ret
< 0 && errno
== EINTR
);
863 PERROR("Reading index header");
866 if (strncmp(hdr
.magic
, INDEX_MAGIC
, sizeof(hdr
.magic
)) != 0) {
867 ERR("Invalid header magic");
871 if (be32toh(hdr
.index_major
) != INDEX_MAJOR
||
872 be32toh(hdr
.index_minor
) != INDEX_MINOR
) {
873 ERR("Invalid header version");
885 * Get viewer stream from stream id.
887 * RCU read side lock MUST be acquired.
889 struct relay_viewer_stream
*live_find_viewer_stream_by_id(uint64_t stream_id
)
891 struct lttng_ht_node_u64
*node
;
892 struct lttng_ht_iter iter
;
893 struct relay_viewer_stream
*stream
= NULL
;
895 lttng_ht_lookup(viewer_streams_ht
, &stream_id
, &iter
);
896 node
= lttng_ht_iter_get_node_u64(&iter
);
898 DBG("Relay viewer stream %" PRIu64
" not found", stream_id
);
901 stream
= caa_container_of(node
, struct relay_viewer_stream
, stream_n
);
908 * Send the next index for a stream.
910 * Return 0 on success or else a negative value.
913 int viewer_get_next_index(struct relay_command
*cmd
,
914 struct lttng_ht
*sessions_ht
)
917 struct lttng_viewer_get_next_index request_index
;
918 struct lttng_viewer_index viewer_index
;
919 struct lttng_packet_index packet_index
;
920 struct relay_viewer_stream
*vstream
;
921 struct relay_stream
*rstream
;
926 DBG("Viewer get next index");
928 if (cmd
->version_check_done
== 0) {
929 ERR("Trying to request index before version check");
934 ret
= cmd
->sock
->ops
->recvmsg(cmd
->sock
, &request_index
,
935 sizeof(request_index
), 0);
936 if (ret
< 0 || ret
!= sizeof(request_index
)) {
938 ERR("Relay didn't receive the whole packet");
943 vstream
= live_find_viewer_stream_by_id(be64toh(request_index
.stream_id
));
949 memset(&viewer_index
, 0, sizeof(viewer_index
));
952 * The viewer should not ask for index on metadata stream.
954 if (vstream
->metadata_flag
) {
955 viewer_index
.status
= htobe32(VIEWER_INDEX_HUP
);
959 /* First time, we open the index file */
960 if (vstream
->index_read_fd
< 0) {
961 ret
= open_index(vstream
);
964 * The index is created only when the first data packet arrives, it
965 * might not be ready at the beginning of the session
967 viewer_index
.status
= htobe32(VIEWER_INDEX_RETRY
);
969 } else if (ret
< 0) {
970 viewer_index
.status
= htobe32(VIEWER_INDEX_ERR
);
975 rstream
= relay_stream_find_by_id(vstream
->stream_handle
);
977 if (rstream
->beacon_ts_end
!= -1ULL &&
978 vstream
->last_sent_index
== rstream
->total_index_received
) {
979 viewer_index
.status
= htobe32(VIEWER_INDEX_INACTIVE
);
980 viewer_index
.timestamp_end
= htobe64(rstream
->beacon_ts_end
);
984 if (rstream
->total_index_received
<= vstream
->last_sent_index
) {
985 /* No new index to send, retry later. */
986 viewer_index
.status
= htobe32(VIEWER_INDEX_RETRY
);
989 } else if (!rstream
&&
990 vstream
->total_index_received
== vstream
->last_sent_index
) {
991 /* Last index sent and stream closed */
992 viewer_index
.status
= htobe32(VIEWER_INDEX_HUP
);
996 if (!vstream
->ctf_trace
->metadata_received
||
997 vstream
->ctf_trace
->metadata_received
>
998 vstream
->ctf_trace
->metadata_sent
) {
999 viewer_index
.flags
|= LTTNG_VIEWER_FLAG_NEW_METADATA
;
1003 ret
= read(vstream
->index_read_fd
, &packet_index
,
1004 sizeof(packet_index
));
1005 } while (ret
< 0 && errno
== EINTR
);
1006 if (ret
< sizeof(packet_index
)) {
1007 PERROR("Relay reading index file");
1008 viewer_index
.status
= htobe32(VIEWER_INDEX_ERR
);
1010 viewer_index
.status
= htobe32(VIEWER_INDEX_OK
);
1011 vstream
->last_sent_index
++;
1015 * Indexes are stored in big endian, no need to switch before sending.
1017 viewer_index
.offset
= packet_index
.offset
;
1018 viewer_index
.packet_size
= packet_index
.packet_size
;
1019 viewer_index
.content_size
= packet_index
.content_size
;
1020 viewer_index
.timestamp_begin
= packet_index
.timestamp_begin
;
1021 viewer_index
.timestamp_end
= packet_index
.timestamp_end
;
1022 viewer_index
.events_discarded
= packet_index
.events_discarded
;
1023 viewer_index
.stream_id
= packet_index
.stream_id
;
1026 viewer_index
.flags
= htobe32(viewer_index
.flags
);
1027 ret
= cmd
->sock
->ops
->sendmsg(cmd
->sock
, &viewer_index
,
1028 sizeof(viewer_index
), 0);
1030 ERR("Relay index to viewer");
1034 DBG("Index %" PRIu64
"for stream %" PRIu64
"sent",
1035 vstream
->last_sent_index
, vstream
->stream_handle
);
1046 * Send the next index for a stream
1048 * Return 0 on success or else a negative value.
1051 int viewer_get_packet(struct relay_command
*cmd
)
1053 int ret
, send_data
= 0;
1057 struct lttng_viewer_get_packet get_packet_info
;
1058 struct lttng_viewer_trace_packet reply
;
1059 struct relay_viewer_stream
*stream
;
1063 DBG2("Relay get data packet");
1065 if (cmd
->version_check_done
== 0) {
1066 ERR("Trying to get packet before version check");
1071 ret
= cmd
->sock
->ops
->recvmsg(cmd
->sock
, &get_packet_info
,
1072 sizeof(get_packet_info
), 0);
1073 if (ret
< 0 || ret
!= sizeof(get_packet_info
)) {
1075 ERR("Relay didn't receive the whole packet");
1080 stream
= live_find_viewer_stream_by_id(be64toh(get_packet_info
.stream_id
));
1084 assert(stream
->ctf_trace
);
1087 * First time we read this stream, we need open the tracefile, we should
1088 * only arrive here if an index has already been sent to the viewer, so the
1089 * tracefile must exist, if it does not it is a fatal error.
1091 if (stream
->read_fd
< 0) {
1092 char fullpath
[PATH_MAX
];
1094 ret
= snprintf(fullpath
, PATH_MAX
, "%s/%s", stream
->path_name
,
1095 stream
->channel_name
);
1099 ret
= open(fullpath
, O_RDONLY
);
1101 PERROR("Relay opening trace file");
1104 stream
->read_fd
= ret
;
1107 memset(&reply
, 0, sizeof(reply
));
1109 if (!stream
->ctf_trace
->metadata_received
||
1110 stream
->ctf_trace
->metadata_received
>
1111 stream
->ctf_trace
->metadata_sent
) {
1112 reply
.status
= htobe32(VIEWER_GET_PACKET_ERR
);
1113 reply
.flags
|= LTTNG_VIEWER_FLAG_NEW_METADATA
;
1118 len
= be32toh(get_packet_info
.len
);
1119 data
= zmalloc(len
);
1121 PERROR("relay data zmalloc");
1125 ret
= lseek(stream
->read_fd
, be64toh(get_packet_info
.offset
), SEEK_SET
);
1130 read_len
= read(stream
->read_fd
, data
, len
);
1131 if (read_len
< (ssize_t
) len
) {
1132 PERROR("Relay reading trace file, fd: %d, offset: %" PRIu64
,
1133 stream
->read_fd
, be64toh(get_packet_info
.offset
));
1136 reply
.status
= htobe32(VIEWER_GET_PACKET_OK
);
1137 reply
.len
= htobe32(len
);
1142 reply
.status
= htobe32(VIEWER_GET_PACKET_ERR
);
1145 reply
.flags
= htobe32(reply
.flags
);
1146 ret
= cmd
->sock
->ops
->sendmsg(cmd
->sock
, &reply
, sizeof(reply
), 0);
1148 ERR("Relay data header to viewer");
1153 ret
= cmd
->sock
->ops
->sendmsg(cmd
->sock
, data
, len
, 0);
1155 ERR("Relay send data to viewer");
1160 DBG("Sent %u bytes for stream %" PRIu64
, len
,
1161 be64toh(get_packet_info
.stream_id
));
1172 * Send the session's metadata
1174 * Return 0 on success else a negative value.
1177 int viewer_get_metadata(struct relay_command
*cmd
)
1183 struct lttng_viewer_get_metadata request
;
1184 struct lttng_viewer_metadata_packet reply
;
1185 struct relay_viewer_stream
*stream
;
1189 DBG("Relay get metadata");
1191 if (cmd
->version_check_done
== 0) {
1192 ERR("Trying to get metadata before version check");
1197 ret
= cmd
->sock
->ops
->recvmsg(cmd
->sock
, &request
,
1198 sizeof(request
), 0);
1199 if (ret
< 0 || ret
!= sizeof(request
)) {
1201 ERR("Relay didn't receive the whole packet");
1206 stream
= live_find_viewer_stream_by_id(be64toh(request
.stream_id
));
1207 if (!stream
|| !stream
->metadata_flag
) {
1208 ERR("Invalid metadata stream");
1211 assert(stream
->ctf_trace
);
1212 assert(stream
->ctf_trace
->metadata_sent
<=
1213 stream
->ctf_trace
->metadata_received
);
1215 len
= stream
->ctf_trace
->metadata_received
-
1216 stream
->ctf_trace
->metadata_sent
;
1218 reply
.status
= htobe32(VIEWER_NO_NEW_METADATA
);
1222 /* first time, we open the metadata file */
1223 if (stream
->read_fd
< 0) {
1224 char fullpath
[PATH_MAX
];
1226 ret
= snprintf(fullpath
, PATH_MAX
, "%s/%s", stream
->path_name
,
1227 stream
->channel_name
);
1231 ret
= open(fullpath
, O_RDONLY
);
1233 PERROR("Relay opening metadata file");
1236 stream
->read_fd
= ret
;
1239 reply
.len
= htobe64(len
);
1240 data
= zmalloc(len
);
1242 PERROR("viewer metadata zmalloc");
1246 read_len
= read(stream
->read_fd
, data
, len
);
1247 if (read_len
< (ssize_t
) len
) {
1248 PERROR("Relay reading metadata file");
1251 stream
->ctf_trace
->metadata_sent
+= read_len
;
1252 reply
.status
= htobe32(VIEWER_METADATA_OK
);
1256 reply
.status
= htobe32(VIEWER_METADATA_ERR
);
1259 ret
= cmd
->sock
->ops
->sendmsg(cmd
->sock
, &reply
, sizeof(reply
), 0);
1261 ERR("Relay data header to viewer");
1266 ret
= cmd
->sock
->ops
->sendmsg(cmd
->sock
, data
, len
, 0);
1268 ERR("Relay send data to viewer");
1273 DBG("Sent %" PRIu64
" bytes of metadata for stream %" PRIu64
, len
,
1274 be64toh(request
.stream_id
));
1276 DBG("Metadata sent");
1286 * live_relay_unknown_command: send -1 if received unknown command
1289 void live_relay_unknown_command(struct relay_command
*cmd
)
1291 struct lttcomm_relayd_generic_reply reply
;
1294 reply
.ret_code
= htobe32(LTTNG_ERR_UNK
);
1295 ret
= cmd
->sock
->ops
->sendmsg(cmd
->sock
, &reply
,
1296 sizeof(struct lttcomm_relayd_generic_reply
), 0);
1298 ERR("Relay sending unknown command");
1303 * Process the commands received on the control socket
1306 int process_control(struct lttng_viewer_cmd
*recv_hdr
,
1307 struct relay_command
*cmd
, struct lttng_ht
*sessions_ht
)
1311 switch (be32toh(recv_hdr
->cmd
)) {
1312 case VIEWER_CONNECT
:
1313 ret
= viewer_connect(cmd
);
1315 case VIEWER_LIST_SESSIONS
:
1316 ret
= viewer_list_sessions(cmd
, sessions_ht
);
1318 case VIEWER_ATTACH_SESSION
:
1319 ret
= viewer_attach_session(cmd
, sessions_ht
);
1321 case VIEWER_GET_NEXT_INDEX
:
1322 ret
= viewer_get_next_index(cmd
, sessions_ht
);
1324 case VIEWER_GET_PACKET
:
1325 ret
= viewer_get_packet(cmd
);
1327 case VIEWER_GET_METADATA
:
1328 ret
= viewer_get_metadata(cmd
);
1331 ERR("Received unknown viewer command (%u)", be32toh(recv_hdr
->cmd
));
1332 live_relay_unknown_command(cmd
);
1342 void cleanup_poll_connection(struct lttng_poll_event
*events
, int pollfd
)
1348 lttng_poll_del(events
, pollfd
);
1350 ret
= close(pollfd
);
1352 ERR("Closing pollfd %d", pollfd
);
1357 * Create and add connection to the given hash table.
1359 * Return poll add value or else -1 on error.
1362 int add_connection(int fd
, struct lttng_poll_event
*events
,
1363 struct lttng_ht
*relay_connections_ht
)
1366 struct relay_command
*relay_connection
;
1369 assert(relay_connections_ht
);
1371 relay_connection
= zmalloc(sizeof(struct relay_command
));
1372 if (relay_connection
== NULL
) {
1373 PERROR("Relay command zmalloc");
1378 ret
= read(fd
, relay_connection
, sizeof(*relay_connection
));
1379 } while (ret
< 0 && errno
== EINTR
);
1380 if (ret
< 0 || ret
< sizeof(*relay_connection
)) {
1381 PERROR("read relay cmd pipe");
1385 lttng_ht_node_init_ulong(&relay_connection
->sock_n
,
1386 (unsigned long) relay_connection
->sock
->fd
);
1388 lttng_ht_add_unique_ulong(relay_connections_ht
,
1389 &relay_connection
->sock_n
);
1392 return lttng_poll_add(events
, relay_connection
->sock
->fd
,
1393 LPOLLIN
| LPOLLRDHUP
);
1396 free(relay_connection
);
1402 void deferred_free_connection(struct rcu_head
*head
)
1404 struct relay_command
*relay_connection
=
1405 caa_container_of(head
, struct relay_command
, rcu_node
);
1407 if (relay_connection
->session
&&
1408 relay_connection
->session
->viewer_attached
> 0) {
1409 relay_connection
->session
->viewer_attached
--;
1411 lttcomm_destroy_sock(relay_connection
->sock
);
1412 free(relay_connection
);
1416 void deferred_free_viewer_stream(struct rcu_head
*head
)
1418 struct relay_viewer_stream
*stream
=
1419 caa_container_of(head
, struct relay_viewer_stream
, rcu_node
);
1421 if (stream
->ctf_trace
) {
1422 uatomic_dec(&stream
->ctf_trace
->refcount
);
1423 assert(uatomic_read(&stream
->ctf_trace
->refcount
) >= 0);
1424 if (uatomic_read(&stream
->ctf_trace
->refcount
) == 0) {
1425 DBG("Freeing ctf_trace %" PRIu64
, stream
->ctf_trace
->id
);
1426 free(stream
->ctf_trace
);
1430 free(stream
->path_name
);
1431 free(stream
->channel_name
);
1436 void viewer_del_streams(struct relay_session
*session
)
1439 struct relay_viewer_stream
*stream
;
1440 struct lttng_ht_node_u64
*node
;
1441 struct lttng_ht_iter iter
;
1446 cds_lfht_for_each_entry(viewer_streams_ht
->ht
, &iter
.iter
, node
, node
) {
1447 node
= lttng_ht_iter_get_node_u64(&iter
);
1452 stream
= caa_container_of(node
, struct relay_viewer_stream
, stream_n
);
1453 if (stream
->session_id
!= session
->id
) {
1457 if (stream
->read_fd
> 0) {
1458 ret
= close(stream
->read_fd
);
1460 PERROR("close read_fd");
1463 if (stream
->index_read_fd
> 0) {
1464 ret
= close(stream
->index_read_fd
);
1466 PERROR("close index_read_fd");
1469 if (stream
->metadata_flag
&& stream
->ctf_trace
) {
1470 stream
->ctf_trace
->metadata_sent
= 0;
1472 ret
= lttng_ht_del(viewer_streams_ht
, &iter
);
1474 call_rcu(&stream
->rcu_node
, deferred_free_viewer_stream
);
1480 * Delete and free a connection.
1482 * RCU read side lock MUST be acquired.
1485 void del_connection(struct lttng_ht
*relay_connections_ht
,
1486 struct lttng_ht_iter
*iter
, struct relay_command
*relay_connection
)
1490 assert(relay_connections_ht
);
1492 assert(relay_connection
);
1494 ret
= lttng_ht_del(relay_connections_ht
, iter
);
1497 if (relay_connection
->session
) {
1498 viewer_del_streams(relay_connection
->session
);
1501 call_rcu(&relay_connection
->rcu_node
, deferred_free_connection
);
1505 * This thread does the actual work
1508 void *thread_worker(void *data
)
1512 struct relay_command
*relay_connection
;
1513 struct lttng_poll_event events
;
1514 struct lttng_ht
*relay_connections_ht
;
1515 struct lttng_ht_node_ulong
*node
;
1516 struct lttng_ht_iter iter
;
1517 struct lttng_viewer_cmd recv_hdr
;
1518 struct relay_local_data
*relay_ctx
= (struct relay_local_data
*) data
;
1519 struct lttng_ht
*sessions_ht
= relay_ctx
->sessions_ht
;
1521 DBG("[thread] Live viewer relay worker started");
1523 rcu_register_thread();
1525 /* table of connections indexed on socket */
1526 relay_connections_ht
= lttng_ht_new(0, LTTNG_HT_TYPE_ULONG
);
1527 if (!relay_connections_ht
) {
1528 goto relay_connections_ht_error
;
1531 ret
= create_thread_poll_set(&events
, 2);
1533 goto error_poll_create
;
1536 ret
= lttng_poll_add(&events
, live_relay_cmd_pipe
[0], LPOLLIN
| LPOLLRDHUP
);
1545 /* Infinite blocking call, waiting for transmission */
1546 DBG3("Relayd live viewer worker thread polling...");
1547 ret
= lttng_poll_wait(&events
, -1);
1550 * Restart interrupted system call.
1552 if (errno
== EINTR
) {
1561 * Process control. The control connection is prioritised so we don't
1562 * starve it with high throughput tracing data on the data
1565 for (i
= 0; i
< nb_fd
; i
++) {
1566 /* Fetch once the poll data */
1567 uint32_t revents
= LTTNG_POLL_GETEV(&events
, i
);
1568 int pollfd
= LTTNG_POLL_GETFD(&events
, i
);
1570 /* Thread quit pipe has been closed. Killing thread. */
1571 ret
= check_thread_quit_pipe(pollfd
, revents
);
1577 /* Inspect the relay cmd pipe for new connection */
1578 if (pollfd
== live_relay_cmd_pipe
[0]) {
1579 if (revents
& (LPOLLERR
| LPOLLHUP
| LPOLLRDHUP
)) {
1580 ERR("Relay live pipe error");
1582 } else if (revents
& LPOLLIN
) {
1583 DBG("Relay live viewer command received");
1584 ret
= add_connection(live_relay_cmd_pipe
[0],
1585 &events
, relay_connections_ht
);
1590 } else if (revents
) {
1592 lttng_ht_lookup(relay_connections_ht
,
1593 (void *)((unsigned long) pollfd
), &iter
);
1594 node
= lttng_ht_iter_get_node_ulong(&iter
);
1596 DBG2("Relay viewer sock %d not found", pollfd
);
1600 relay_connection
= caa_container_of(node
, struct relay_command
,
1603 if (revents
& (LPOLLERR
)) {
1604 ERR("VIEWER POLL ERROR");
1605 cleanup_poll_connection(&events
, pollfd
);
1606 del_connection(relay_connections_ht
, &iter
,
1608 } else if (revents
& (LPOLLHUP
| LPOLLRDHUP
)) {
1609 DBG("Viewer socket %d hung up", pollfd
);
1610 cleanup_poll_connection(&events
, pollfd
);
1611 del_connection(relay_connections_ht
, &iter
,
1613 } else if (revents
& LPOLLIN
) {
1614 ret
= relay_connection
->sock
->ops
->recvmsg(
1615 relay_connection
->sock
, &recv_hdr
,
1616 sizeof(struct lttng_viewer_cmd
),
1618 /* connection closed */
1620 cleanup_poll_connection(&events
, pollfd
);
1621 del_connection( relay_connections_ht
, &iter
,
1623 DBG("Viewer control connection closed with %d",
1626 if (relay_connection
->session
) {
1627 DBG2("Relay viewer worker receiving data for "
1628 "session: %" PRIu64
,
1629 relay_connection
->session
->id
);
1631 ret
= process_control(&recv_hdr
, relay_connection
,
1634 /* Clear the session on error. */
1635 cleanup_poll_connection(&events
, pollfd
);
1636 del_connection(relay_connections_ht
, &iter
,
1638 DBG("Viewer connection closed with %d", pollfd
);
1649 lttng_poll_clean(&events
);
1651 /* empty the hash table and free the memory */
1653 cds_lfht_for_each_entry(relay_connections_ht
->ht
, &iter
.iter
, node
, node
) {
1654 node
= lttng_ht_iter_get_node_ulong(&iter
);
1659 relay_connection
= caa_container_of(node
, struct relay_command
,
1661 del_connection(relay_connections_ht
, &iter
, relay_connection
);
1665 lttng_ht_destroy(relay_connections_ht
);
1666 relay_connections_ht_error
:
1667 /* Close relay cmd pipes */
1668 utils_close_pipe(live_relay_cmd_pipe
);
1670 DBG("Viewer worker thread exited with error");
1672 DBG("Viewer worker thread cleanup complete");
1674 rcu_unregister_thread();
1679 * Create the relay command pipe to wake thread_manage_apps.
1680 * Closed in cleanup().
1682 static int create_relay_cmd_pipe(void)
1686 ret
= utils_create_pipe_cloexec(live_relay_cmd_pipe
);
1691 void live_stop_threads()
1698 ret
= pthread_join(live_listener_thread
, &status
);
1700 PERROR("pthread_join live listener");
1701 goto error
; /* join error, exit without cleanup */
1704 ret
= pthread_join(live_worker_thread
, &status
);
1706 PERROR("pthread_join live worker");
1707 goto error
; /* join error, exit without cleanup */
1710 ret
= pthread_join(live_dispatcher_thread
, &status
);
1712 PERROR("pthread_join live dispatcher");
1713 goto error
; /* join error, exit without cleanup */
1725 int live_start_threads(struct lttng_uri
*uri
,
1726 struct relay_local_data
*relay_ctx
)
1735 /* Create thread quit pipe */
1736 if ((ret
= init_thread_quit_pipe()) < 0) {
1740 /* Check if daemon is UID = 0 */
1741 is_root
= !getuid();
1744 if (live_uri
->port
< 1024) {
1745 ERR("Need to be root to use ports < 1024");
1751 /* Setup the thread apps communication pipe. */
1752 if ((ret
= create_relay_cmd_pipe()) < 0) {
1756 /* Init relay command queue. */
1757 cds_wfq_init(&viewer_cmd_queue
.queue
);
1759 /* Set up max poll set size */
1760 lttng_poll_set_max_size();
1762 /* Setup the dispatcher thread */
1763 ret
= pthread_create(&live_dispatcher_thread
, NULL
,
1764 thread_dispatcher
, (void *) NULL
);
1766 PERROR("pthread_create viewer dispatcher");
1767 goto exit_dispatcher
;
1770 /* Setup the worker thread */
1771 ret
= pthread_create(&live_worker_thread
, NULL
,
1772 thread_worker
, relay_ctx
);
1774 PERROR("pthread_create viewer worker");
1778 /* Setup the listener thread */
1779 ret
= pthread_create(&live_listener_thread
, NULL
,
1780 thread_listener
, (void *) NULL
);
1782 PERROR("pthread_create viewer listener");
1790 ret
= pthread_join(live_listener_thread
, &status
);
1792 PERROR("pthread_join live listener");
1793 goto error
; /* join error, exit without cleanup */
1797 ret
= pthread_join(live_worker_thread
, &status
);
1799 PERROR("pthread_join live worker");
1800 goto error
; /* join error, exit without cleanup */
1804 ret
= pthread_join(live_dispatcher_thread
, &status
);
1806 PERROR("pthread_join live dispatcher");
1807 goto error
; /* join error, exit without cleanup */