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
;
103 * Write to writable pipe used to notify a thread.
106 int notify_thread_pipe(int wpipe
)
111 ret
= write(wpipe
, "!", 1);
112 } while (ret
< 0 && errno
== EINTR
);
113 if (ret
< 0 || ret
!= 1) {
114 PERROR("write poll pipe");
121 * Stop all threads by closing the thread quit pipe.
124 void stop_threads(void)
128 /* Stopping all threads */
129 DBG("Terminating all live threads");
130 ret
= notify_thread_pipe(live_thread_quit_pipe
[1]);
132 ERR("write error on thread quit pipe");
135 /* Dispatch thread */
136 CMM_STORE_SHARED(live_dispatch_thread_exit
, 1);
137 futex_nto1_wake(&viewer_cmd_queue
.futex
);
141 * Create a poll set with O_CLOEXEC and add the thread quit pipe to the set.
144 int create_thread_poll_set(struct lttng_poll_event
*events
, int size
)
148 if (events
== NULL
|| size
== 0) {
153 ret
= lttng_poll_create(events
, size
, LTTNG_CLOEXEC
);
159 ret
= lttng_poll_add(events
, live_thread_quit_pipe
[0], LPOLLIN
);
171 * Check if the thread quit pipe was triggered.
173 * Return 1 if it was triggered else 0;
176 int check_thread_quit_pipe(int fd
, uint32_t events
)
178 if (fd
== live_thread_quit_pipe
[0] && (events
& LPOLLIN
)) {
186 * Create and init socket from uri.
189 struct lttcomm_sock
*init_socket(struct lttng_uri
*uri
)
192 struct lttcomm_sock
*sock
= NULL
;
194 sock
= lttcomm_alloc_sock_from_uri(uri
);
196 ERR("Allocating socket");
200 ret
= lttcomm_create_sock(sock
);
204 DBG("Listening on sock %d for live", sock
->fd
);
206 ret
= sock
->ops
->bind(sock
);
211 ret
= sock
->ops
->listen(sock
, -1);
221 lttcomm_destroy_sock(sock
);
227 * This thread manages the listening for new connections on the network
230 void *thread_listener(void *data
)
232 int i
, ret
, pollfd
, err
= -1;
234 uint32_t revents
, nb_fd
;
235 struct lttng_poll_event events
;
236 struct lttcomm_sock
*live_control_sock
;
238 DBG("[thread] Relay live listener started");
240 live_control_sock
= init_socket(live_uri
);
241 if (!live_control_sock
) {
242 goto error_sock_control
;
246 * Pass 3 as size here for the thread quit pipe, control and data socket.
248 ret
= create_thread_poll_set(&events
, 2);
250 goto error_create_poll
;
253 /* Add the control socket */
254 ret
= lttng_poll_add(&events
, live_control_sock
->fd
, LPOLLIN
| LPOLLRDHUP
);
260 DBG("Listener accepting live viewers connections");
263 ret
= lttng_poll_wait(&events
, -1);
266 * Restart interrupted system call.
268 if (errno
== EINTR
) {
275 DBG("Relay new viewer connection received");
276 for (i
= 0; i
< nb_fd
; i
++) {
277 /* Fetch once the poll data */
278 revents
= LTTNG_POLL_GETEV(&events
, i
);
279 pollfd
= LTTNG_POLL_GETFD(&events
, i
);
281 /* Thread quit pipe has been closed. Killing thread. */
282 ret
= check_thread_quit_pipe(pollfd
, revents
);
288 if (revents
& (LPOLLERR
| LPOLLHUP
| LPOLLRDHUP
)) {
289 ERR("socket poll error");
291 } else if (revents
& LPOLLIN
) {
293 * Get allocated in this thread, enqueued to a global queue,
294 * dequeued and freed in the worker thread.
296 struct relay_command
*relay_cmd
;
297 struct lttcomm_sock
*newsock
;
299 relay_cmd
= zmalloc(sizeof(*relay_cmd
));
301 PERROR("relay command zmalloc");
305 assert(pollfd
== live_control_sock
->fd
);
306 newsock
= live_control_sock
->ops
->accept(live_control_sock
);
308 PERROR("accepting control sock");
312 DBG("Relay viewer connection accepted socket %d", newsock
->fd
);
313 ret
= setsockopt(newsock
->fd
, SOL_SOCKET
, SO_REUSEADDR
, &val
,
316 PERROR("setsockopt inet");
317 lttcomm_destroy_sock(newsock
);
321 relay_cmd
->sock
= newsock
;
324 * Lock free enqueue the request.
326 cds_wfq_enqueue(&viewer_cmd_queue
.queue
, &relay_cmd
->node
);
329 * Wake the dispatch queue futex. Implicit memory
330 * barrier with the exchange in cds_wfq_enqueue.
332 futex_nto1_wake(&viewer_cmd_queue
.futex
);
340 lttng_poll_clean(&events
);
342 if (live_control_sock
->fd
>= 0) {
343 ret
= live_control_sock
->ops
->close(live_control_sock
);
348 lttcomm_destroy_sock(live_control_sock
);
351 DBG("Live viewer listener thread exited with error");
353 DBG("Live viewer listener thread cleanup complete");
359 * This thread manages the dispatching of the requests to worker threads
362 void *thread_dispatcher(void *data
)
365 struct cds_wfq_node
*node
;
366 struct relay_command
*relay_cmd
= NULL
;
368 DBG("[thread] Live viewer relay dispatcher started");
370 while (!CMM_LOAD_SHARED(live_dispatch_thread_exit
)) {
371 /* Atomically prepare the queue futex */
372 futex_nto1_prepare(&viewer_cmd_queue
.futex
);
375 /* Dequeue commands */
376 node
= cds_wfq_dequeue_blocking(&viewer_cmd_queue
.queue
);
378 DBG("Woken up but nothing in the live-viewer "
379 "relay command queue");
380 /* Continue thread execution */
384 relay_cmd
= caa_container_of(node
, struct relay_command
, node
);
385 DBG("Dispatching viewer request waiting on sock %d",
386 relay_cmd
->sock
->fd
);
389 * Inform worker thread of the new request. This call is blocking
390 * so we can be assured that the data will be read at some point in
391 * time or wait to the end of the world :)
394 ret
= write(live_relay_cmd_pipe
[1], relay_cmd
,
396 } while (ret
< 0 && errno
== EINTR
);
398 if (ret
< 0 || ret
!= sizeof(struct relay_command
)) {
399 PERROR("write cmd pipe");
402 } while (node
!= NULL
);
404 /* Futex wait on queue. Blocking call on futex() */
405 futex_nto1_wait(&viewer_cmd_queue
.futex
);
409 DBG("Live viewer dispatch thread dying");
415 * Establish connection with the viewer and check the versions.
417 * Return 0 on success or else negative value.
420 int viewer_connect(struct relay_command
*cmd
)
423 struct lttng_viewer_connect reply
, msg
;
427 cmd
->version_check_done
= 1;
429 /* Get version from the other side. */
430 ret
= cmd
->sock
->ops
->recvmsg(cmd
->sock
, &msg
, sizeof(msg
), 0);
431 if (ret
< 0 || ret
!= sizeof(msg
)) {
433 /* Orderly shutdown. Not necessary to print an error. */
434 DBG("Socket %d did an orderly shutdown", cmd
->sock
->fd
);
436 ERR("Relay failed to receive the version values.");
442 reply
.major
= RELAYD_VERSION_COMM_MAJOR
;
443 reply
.minor
= RELAYD_VERSION_COMM_MINOR
;
445 /* Major versions must be the same */
446 if (reply
.major
!= be32toh(msg
.major
)) {
447 DBG("Incompatible major versions (%u vs %u)", reply
.major
,
453 cmd
->major
= reply
.major
;
454 /* We adapt to the lowest compatible version */
455 if (reply
.minor
<= be32toh(msg
.minor
)) {
456 cmd
->minor
= reply
.minor
;
458 cmd
->minor
= be32toh(msg
.minor
);
461 if (be32toh(msg
.type
) == VIEWER_CLIENT_COMMAND
) {
462 cmd
->type
= RELAY_VIEWER_COMMAND
;
463 } else if (be32toh(msg
.type
) == VIEWER_CLIENT_NOTIFICATION
) {
464 cmd
->type
= RELAY_VIEWER_NOTIFICATION
;
466 ERR("Unknown connection type : %u", be32toh(msg
.type
));
471 reply
.major
= htobe32(reply
.major
);
472 reply
.minor
= htobe32(reply
.minor
);
473 if (cmd
->type
== RELAY_VIEWER_COMMAND
) {
474 reply
.viewer_session_id
= htobe64(++last_relay_viewer_session_id
);
476 ret
= cmd
->sock
->ops
->sendmsg(cmd
->sock
, &reply
,
477 sizeof(struct lttng_viewer_connect
), 0);
479 ERR("Relay sending version");
482 DBG("Version check done using protocol %u.%u", cmd
->major
, cmd
->minor
);
490 * Send the viewer the list of current sessions.
492 * Return 0 on success or else a negative value.
495 int viewer_list_sessions(struct relay_command
*cmd
,
496 struct lttng_ht
*sessions_ht
)
499 struct lttng_viewer_list_sessions session_list
;
501 long approx_before
, approx_after
;
502 struct lttng_ht_node_ulong
*node
;
503 struct lttng_ht_iter iter
;
504 struct lttng_viewer_session send_session
;
505 struct relay_session
*session
;
507 DBG("List sessions received");
509 if (cmd
->version_check_done
== 0) {
510 ERR("Trying to list sessions before version check");
516 cds_lfht_count_nodes(sessions_ht
->ht
, &approx_before
, &count
, &approx_after
);
517 session_list
.sessions_count
= htobe32(count
);
519 ret
= cmd
->sock
->ops
->sendmsg(cmd
->sock
, &session_list
,
520 sizeof(session_list
), 0);
522 ERR("Relay sending sessions list");
526 cds_lfht_for_each_entry(sessions_ht
->ht
, &iter
.iter
, node
, node
) {
527 node
= lttng_ht_iter_get_node_ulong(&iter
);
531 session
= caa_container_of(node
, struct relay_session
, session_n
);
533 strncpy(send_session
.session_name
, session
->session_name
,
534 sizeof(send_session
.session_name
));
535 strncpy(send_session
.hostname
, session
->hostname
,
536 sizeof(send_session
.hostname
));
537 send_session
.id
= htobe64(session
->id
);
538 send_session
.live_timer
= htobe32(session
->live_timer
);
539 send_session
.clients
= htobe32(session
->viewer_attached
);
541 ret
= cmd
->sock
->ops
->sendmsg(cmd
->sock
, &send_session
,
542 sizeof(send_session
), 0);
544 ERR("Relay sending session info");
561 * Allocate and init a new viewer_stream.
563 * Copies the values from the stream passed in parameter and insert the new
564 * stream in the viewer_streams_ht.
566 * MUST be called with rcu_read_lock held.
568 * Returns 0 on success or a negative value on error.
571 int init_viewer_stream(struct relay_stream
*stream
)
574 struct relay_viewer_stream
*viewer_stream
;
578 viewer_stream
= zmalloc(sizeof(*viewer_stream
));
579 if (!viewer_stream
) {
580 PERROR("relay viewer stream zmalloc");
585 viewer_stream
->read_fd
= -1;
586 viewer_stream
->index_read_fd
= -1;
587 viewer_stream
->session_id
= stream
->session
->id
;
588 viewer_stream
->stream_handle
= stream
->stream_handle
;
589 viewer_stream
->path_name
= strndup(stream
->path_name
,
590 LTTNG_VIEWER_PATH_MAX
);
591 viewer_stream
->channel_name
= strndup(stream
->channel_name
,
592 LTTNG_VIEWER_NAME_MAX
);
593 viewer_stream
->total_index_received
= stream
->total_index_received
;
594 viewer_stream
->tracefile_size
= stream
->tracefile_size
;
595 viewer_stream
->tracefile_count
= stream
->tracefile_count
;
596 viewer_stream
->metadata_flag
= stream
->metadata_flag
;
599 * This is to avoid a race between the initialization of this object and
600 * the close of the given stream. If the stream is unable to find this
601 * viewer stream when closing, this copy will at least take the latest
604 viewer_stream
->total_index_received
= stream
->total_index_received
;
607 * The deletion of this ctf_trace object is only done in a call RCU of the
608 * relay stream making it valid as long as we have the read side lock.
610 viewer_stream
->ctf_trace
= stream
->ctf_trace
;
611 uatomic_inc(&viewer_stream
->ctf_trace
->refcount
);
613 lttng_ht_node_init_u64(&viewer_stream
->stream_n
, stream
->stream_handle
);
614 lttng_ht_add_unique_u64(viewer_streams_ht
, &viewer_stream
->stream_n
);
623 * Send the viewer the list of current sessions.
626 int viewer_attach_session(struct relay_command
*cmd
,
627 struct lttng_ht
*sessions_ht
)
629 int ret
, send_streams
= 0, nb_streams
= 0;
630 struct lttng_viewer_attach_session_request request
;
631 struct lttng_viewer_attach_session_response response
;
632 struct lttng_viewer_stream send_stream
;
633 struct relay_stream
*stream
;
634 struct relay_viewer_stream
*viewer_stream
;
635 struct lttng_ht_node_ulong
*node
;
636 struct lttng_ht_node_u64
*node64
;
637 struct lttng_ht_iter iter
;
638 struct relay_session
*session
;
643 DBG("Attach session received");
645 if (cmd
->version_check_done
== 0) {
646 ERR("Trying to attach session before version check");
651 ret
= cmd
->sock
->ops
->recvmsg(cmd
->sock
, &request
, sizeof(request
), 0);
652 if (ret
< 0 || ret
!= sizeof(request
)) {
654 /* Orderly shutdown. Not necessary to print an error. */
655 DBG("Socket %d did an orderly shutdown", cmd
->sock
->fd
);
657 ERR("Relay failed to receive the attach parameters.");
664 lttng_ht_lookup(sessions_ht
,
665 (void *)((unsigned long) be64toh(request
.session_id
)), &iter
);
666 node
= lttng_ht_iter_get_node_ulong(&iter
);
668 DBG("Relay session %" PRIu64
" not found",
669 be64toh(request
.session_id
));
670 response
.status
= htobe32(VIEWER_ATTACH_UNK
);
674 session
= caa_container_of(node
, struct relay_session
, session_n
);
675 if (cmd
->session_id
== session
->id
) {
676 /* Same viewer already attached, just send the stream list. */
678 response
.status
= htobe32(VIEWER_ATTACH_OK
);
679 } else if (session
->viewer_attached
!= 0) {
680 DBG("Already a viewer attached");
681 response
.status
= htobe32(VIEWER_ATTACH_ALREADY
);
683 } else if (session
->live_timer
== 0) {
684 DBG("Not live session");
685 response
.status
= htobe32(VIEWER_ATTACH_NOT_LIVE
);
688 session
->viewer_attached
++;
690 response
.status
= htobe32(VIEWER_ATTACH_OK
);
691 cmd
->session_id
= session
->id
;
692 cmd
->session
= session
;
695 switch (be32toh(request
.seek
)) {
696 case VIEWER_SEEK_BEGINNING
:
697 /* Default behaviour. */
699 case VIEWER_SEEK_LAST
:
703 ERR("Wrong seek parameter");
704 response
.status
= htobe32(VIEWER_ATTACH_SEEK_ERR
);
710 /* We should only be there if we have a session to attach to. */
714 * Fill the viewer_streams_ht to count the number of streams
715 * ready to be sent and avoid concurrency issues on the
716 * relay_streams_ht and don't rely on a total session stream count.
718 cds_lfht_for_each_entry(relay_streams_ht
->ht
, &iter
.iter
, node
, node
) {
719 struct relay_viewer_stream
*vstream
;
721 node
= lttng_ht_iter_get_node_ulong(&iter
);
725 stream
= caa_container_of(node
, struct relay_stream
, stream_n
);
726 if (stream
->session
!= cmd
->session
) {
731 * Don't send streams with no ctf_trace, they are not ready to be
734 if (!stream
->ctf_trace
) {
738 vstream
= live_find_viewer_stream_by_id(stream
->stream_handle
);
740 ret
= init_viewer_stream(stream
);
747 response
.streams_count
= htobe32(nb_streams
);
751 ret
= cmd
->sock
->ops
->sendmsg(cmd
->sock
, &response
, sizeof(response
), 0);
753 ERR("Relay sending viewer attach response");
758 * Unknown or busy session, just return gracefully, the viewer knows what
766 /* We should only be there if we have a session to attach to. */
768 cds_lfht_for_each_entry(viewer_streams_ht
->ht
, &iter
.iter
, node
, node
) {
769 node64
= lttng_ht_iter_get_node_u64(&iter
);
773 viewer_stream
= caa_container_of(node64
, struct relay_viewer_stream
,
775 if (viewer_stream
->session_id
!= cmd
->session
->id
) {
779 send_stream
.id
= htobe64(viewer_stream
->stream_handle
);
780 send_stream
.ctf_trace_id
= htobe64(viewer_stream
->ctf_trace
->id
);
781 send_stream
.metadata_flag
= htobe32(viewer_stream
->metadata_flag
);
782 strncpy(send_stream
.path_name
, viewer_stream
->path_name
,
783 sizeof(send_stream
.path_name
));
784 strncpy(send_stream
.channel_name
, viewer_stream
->channel_name
,
785 sizeof(send_stream
.channel_name
));
787 ret
= cmd
->sock
->ops
->sendmsg(cmd
->sock
, &send_stream
,
788 sizeof(send_stream
), 0);
790 ERR("Relay sending stream %" PRIu64
, viewer_stream
->stream_handle
);
793 DBG("Sent stream %" PRIu64
" to viewer", viewer_stream
->stream_handle
);
805 * Open index file using a given viewer stream.
807 * Return 0 on success or else a negative value.
809 static int open_index(struct relay_viewer_stream
*stream
)
812 char fullpath
[PATH_MAX
];
813 struct lttng_packet_index_file_hdr hdr
;
815 if (stream
->tracefile_size
> 0) {
816 /* For now we don't support on-disk ring buffer. */
820 ret
= snprintf(fullpath
, sizeof(fullpath
), "%s/" DEFAULT_INDEX_DIR
821 "/%s" DEFAULT_INDEX_FILE_SUFFIX
,
822 stream
->path_name
, stream
->channel_name
);
824 PERROR("snprintf index path");
829 DBG("Opening index file %s in read only", fullpath
);
830 ret
= open(fullpath
, O_RDONLY
);
832 if (errno
== ENOENT
) {
836 PERROR("opening index in read-only");
840 stream
->index_read_fd
= ret
;
841 DBG("Opening index file %s in read only, (fd: %d)", fullpath
, ret
);
844 ret
= read(stream
->index_read_fd
, &hdr
, sizeof(hdr
));
845 } while (ret
< 0 && errno
== EINTR
);
847 PERROR("Reading index header");
850 if (strncmp(hdr
.magic
, INDEX_MAGIC
, sizeof(hdr
.magic
)) != 0) {
851 ERR("Invalid header magic");
855 if (be32toh(hdr
.index_major
) != INDEX_MAJOR
||
856 be32toh(hdr
.index_minor
) != INDEX_MINOR
) {
857 ERR("Invalid header version");
869 * Get viewer stream from stream id.
871 * RCU read side lock MUST be acquired.
873 struct relay_viewer_stream
*live_find_viewer_stream_by_id(uint64_t stream_id
)
875 struct lttng_ht_node_u64
*node
;
876 struct lttng_ht_iter iter
;
877 struct relay_viewer_stream
*stream
= NULL
;
879 lttng_ht_lookup(viewer_streams_ht
, &stream_id
, &iter
);
880 node
= lttng_ht_iter_get_node_u64(&iter
);
882 DBG("Relay viewer stream %" PRIu64
" not found", stream_id
);
885 stream
= caa_container_of(node
, struct relay_viewer_stream
, stream_n
);
892 * Send the next index for a stream.
894 * Return 0 on success or else a negative value.
897 int viewer_get_next_index(struct relay_command
*cmd
,
898 struct lttng_ht
*sessions_ht
)
901 struct lttng_viewer_get_next_index request_index
;
902 struct lttng_viewer_index viewer_index
;
903 struct lttng_packet_index packet_index
;
904 struct relay_viewer_stream
*vstream
;
905 struct relay_stream
*rstream
;
910 DBG("Viewer get next index");
912 if (cmd
->version_check_done
== 0) {
913 ERR("Trying to request index before version check");
918 ret
= cmd
->sock
->ops
->recvmsg(cmd
->sock
, &request_index
,
919 sizeof(request_index
), 0);
920 if (ret
< 0 || ret
!= sizeof(request_index
)) {
922 ERR("Relay didn't receive the whole packet");
927 vstream
= live_find_viewer_stream_by_id(be64toh(request_index
.stream_id
));
933 memset(&viewer_index
, 0, sizeof(viewer_index
));
936 * The viewer should not ask for index on metadata stream.
938 if (vstream
->metadata_flag
) {
939 viewer_index
.status
= htobe32(VIEWER_INDEX_HUP
);
943 /* First time, we open the index file */
944 if (vstream
->index_read_fd
< 0) {
945 ret
= open_index(vstream
);
948 * The index is created only when the first data packet arrives, it
949 * might not be ready at the beginning of the session
951 viewer_index
.status
= htobe32(VIEWER_INDEX_RETRY
);
953 } else if (ret
< 0) {
954 viewer_index
.status
= htobe32(VIEWER_INDEX_ERR
);
959 rstream
= relay_stream_find_by_id(vstream
->stream_handle
);
961 if (rstream
->beacon_ts_end
!= -1ULL &&
962 vstream
->last_sent_index
== rstream
->total_index_received
) {
963 viewer_index
.status
= htobe32(VIEWER_INDEX_INACTIVE
);
964 viewer_index
.timestamp_end
= htobe64(rstream
->beacon_ts_end
);
968 if (rstream
->total_index_received
<= vstream
->last_sent_index
) {
969 /* No new index to send, retry later. */
970 viewer_index
.status
= htobe32(VIEWER_INDEX_RETRY
);
973 } else if (!rstream
&&
974 vstream
->total_index_received
== vstream
->last_sent_index
) {
975 /* Last index sent and stream closed */
976 viewer_index
.status
= htobe32(VIEWER_INDEX_HUP
);
980 if (!vstream
->ctf_trace
->metadata_received
||
981 vstream
->ctf_trace
->metadata_received
>
982 vstream
->ctf_trace
->metadata_sent
) {
983 viewer_index
.flags
|= LTTNG_VIEWER_FLAG_NEW_METADATA
;
987 ret
= read(vstream
->index_read_fd
, &packet_index
,
988 sizeof(packet_index
));
989 } while (ret
< 0 && errno
== EINTR
);
990 if (ret
< sizeof(packet_index
)) {
991 PERROR("Relay reading index file");
992 viewer_index
.status
= htobe32(VIEWER_INDEX_ERR
);
994 viewer_index
.status
= htobe32(VIEWER_INDEX_OK
);
995 vstream
->last_sent_index
++;
999 * Indexes are stored in big endian, no need to switch before sending.
1001 viewer_index
.offset
= packet_index
.offset
;
1002 viewer_index
.packet_size
= packet_index
.packet_size
;
1003 viewer_index
.content_size
= packet_index
.content_size
;
1004 viewer_index
.timestamp_begin
= packet_index
.timestamp_begin
;
1005 viewer_index
.timestamp_end
= packet_index
.timestamp_end
;
1006 viewer_index
.events_discarded
= packet_index
.events_discarded
;
1007 viewer_index
.stream_id
= packet_index
.stream_id
;
1010 viewer_index
.flags
= htobe32(viewer_index
.flags
);
1011 ret
= cmd
->sock
->ops
->sendmsg(cmd
->sock
, &viewer_index
,
1012 sizeof(viewer_index
), 0);
1014 ERR("Relay index to viewer");
1018 DBG("Index %" PRIu64
"for stream %" PRIu64
"sent",
1019 vstream
->last_sent_index
, vstream
->stream_handle
);
1030 * Send the next index for a stream
1032 * Return 0 on success or else a negative value.
1035 int viewer_get_packet(struct relay_command
*cmd
)
1037 int ret
, send_data
= 0;
1041 struct lttng_viewer_get_packet get_packet_info
;
1042 struct lttng_viewer_trace_packet reply
;
1043 struct relay_viewer_stream
*stream
;
1047 DBG2("Relay get data packet");
1049 if (cmd
->version_check_done
== 0) {
1050 ERR("Trying to get packet before version check");
1055 ret
= cmd
->sock
->ops
->recvmsg(cmd
->sock
, &get_packet_info
,
1056 sizeof(get_packet_info
), 0);
1057 if (ret
< 0 || ret
!= sizeof(get_packet_info
)) {
1059 ERR("Relay didn't receive the whole packet");
1064 stream
= live_find_viewer_stream_by_id(be64toh(get_packet_info
.stream_id
));
1068 assert(stream
->ctf_trace
);
1071 * First time we read this stream, we need open the tracefile, we should
1072 * only arrive here if an index has already been sent to the viewer, so the
1073 * tracefile must exist, if it does not it is a fatal error.
1075 if (stream
->read_fd
< 0) {
1076 char fullpath
[PATH_MAX
];
1078 ret
= snprintf(fullpath
, PATH_MAX
, "%s/%s", stream
->path_name
,
1079 stream
->channel_name
);
1083 ret
= open(fullpath
, O_RDONLY
);
1085 PERROR("Relay opening trace file");
1088 stream
->read_fd
= ret
;
1091 memset(&reply
, 0, sizeof(reply
));
1093 if (!stream
->ctf_trace
->metadata_received
||
1094 stream
->ctf_trace
->metadata_received
>
1095 stream
->ctf_trace
->metadata_sent
) {
1096 reply
.status
= htobe32(VIEWER_GET_PACKET_ERR
);
1097 reply
.flags
|= LTTNG_VIEWER_FLAG_NEW_METADATA
;
1102 len
= be32toh(get_packet_info
.len
);
1103 data
= zmalloc(len
);
1105 PERROR("relay data zmalloc");
1109 ret
= lseek(stream
->read_fd
, be64toh(get_packet_info
.offset
), SEEK_SET
);
1114 read_len
= read(stream
->read_fd
, data
, len
);
1115 if (read_len
< (ssize_t
) len
) {
1116 PERROR("Relay reading trace file, fd: %d, offset: %" PRIu64
,
1117 stream
->read_fd
, be64toh(get_packet_info
.offset
));
1120 reply
.status
= htobe32(VIEWER_GET_PACKET_OK
);
1121 reply
.len
= htobe32(len
);
1126 reply
.status
= htobe32(VIEWER_GET_PACKET_ERR
);
1129 reply
.flags
= htobe32(reply
.flags
);
1130 ret
= cmd
->sock
->ops
->sendmsg(cmd
->sock
, &reply
, sizeof(reply
), 0);
1132 ERR("Relay data header to viewer");
1137 ret
= cmd
->sock
->ops
->sendmsg(cmd
->sock
, data
, len
, 0);
1139 ERR("Relay send data to viewer");
1144 DBG("Sent %u bytes for stream %" PRIu64
, len
,
1145 be64toh(get_packet_info
.stream_id
));
1156 * Send the session's metadata
1158 * Return 0 on success else a negative value.
1161 int viewer_get_metadata(struct relay_command
*cmd
)
1167 struct lttng_viewer_get_metadata request
;
1168 struct lttng_viewer_metadata_packet reply
;
1169 struct relay_viewer_stream
*stream
;
1173 DBG("Relay get metadata");
1175 if (cmd
->version_check_done
== 0) {
1176 ERR("Trying to get metadata before version check");
1181 ret
= cmd
->sock
->ops
->recvmsg(cmd
->sock
, &request
,
1182 sizeof(request
), 0);
1183 if (ret
< 0 || ret
!= sizeof(request
)) {
1185 ERR("Relay didn't receive the whole packet");
1190 stream
= live_find_viewer_stream_by_id(be64toh(request
.stream_id
));
1191 if (!stream
|| !stream
->metadata_flag
) {
1192 ERR("Invalid metadata stream");
1195 assert(stream
->ctf_trace
);
1196 assert(stream
->ctf_trace
->metadata_sent
<=
1197 stream
->ctf_trace
->metadata_received
);
1199 len
= stream
->ctf_trace
->metadata_received
-
1200 stream
->ctf_trace
->metadata_sent
;
1202 reply
.status
= htobe32(VIEWER_NO_NEW_METADATA
);
1206 /* first time, we open the metadata file */
1207 if (stream
->read_fd
< 0) {
1208 char fullpath
[PATH_MAX
];
1210 ret
= snprintf(fullpath
, PATH_MAX
, "%s/%s", stream
->path_name
,
1211 stream
->channel_name
);
1215 ret
= open(fullpath
, O_RDONLY
);
1217 PERROR("Relay opening metadata file");
1220 stream
->read_fd
= ret
;
1223 reply
.len
= htobe64(len
);
1224 data
= zmalloc(len
);
1226 PERROR("viewer metadata zmalloc");
1230 read_len
= read(stream
->read_fd
, data
, len
);
1231 if (read_len
< (ssize_t
) len
) {
1232 PERROR("Relay reading metadata file");
1235 stream
->ctf_trace
->metadata_sent
+= read_len
;
1236 reply
.status
= htobe32(VIEWER_METADATA_OK
);
1240 reply
.status
= htobe32(VIEWER_METADATA_ERR
);
1243 ret
= cmd
->sock
->ops
->sendmsg(cmd
->sock
, &reply
, sizeof(reply
), 0);
1245 ERR("Relay data header to viewer");
1250 ret
= cmd
->sock
->ops
->sendmsg(cmd
->sock
, data
, len
, 0);
1252 ERR("Relay send data to viewer");
1257 DBG("Sent %" PRIu64
" bytes of metadata for stream %" PRIu64
, len
,
1258 be64toh(request
.stream_id
));
1260 DBG("Metadata sent");
1270 * live_relay_unknown_command: send -1 if received unknown command
1273 void live_relay_unknown_command(struct relay_command
*cmd
)
1275 struct lttcomm_relayd_generic_reply reply
;
1278 reply
.ret_code
= htobe32(LTTNG_ERR_UNK
);
1279 ret
= cmd
->sock
->ops
->sendmsg(cmd
->sock
, &reply
,
1280 sizeof(struct lttcomm_relayd_generic_reply
), 0);
1282 ERR("Relay sending unknown command");
1287 * Process the commands received on the control socket
1290 int process_control(struct lttng_viewer_cmd
*recv_hdr
,
1291 struct relay_command
*cmd
, struct lttng_ht
*sessions_ht
)
1295 switch (be32toh(recv_hdr
->cmd
)) {
1296 case VIEWER_CONNECT
:
1297 ret
= viewer_connect(cmd
);
1299 case VIEWER_LIST_SESSIONS
:
1300 ret
= viewer_list_sessions(cmd
, sessions_ht
);
1302 case VIEWER_ATTACH_SESSION
:
1303 ret
= viewer_attach_session(cmd
, sessions_ht
);
1305 case VIEWER_GET_NEXT_INDEX
:
1306 ret
= viewer_get_next_index(cmd
, sessions_ht
);
1308 case VIEWER_GET_PACKET
:
1309 ret
= viewer_get_packet(cmd
);
1311 case VIEWER_GET_METADATA
:
1312 ret
= viewer_get_metadata(cmd
);
1315 ERR("Received unknown viewer command (%u)", be32toh(recv_hdr
->cmd
));
1316 live_relay_unknown_command(cmd
);
1326 void cleanup_poll_connection(struct lttng_poll_event
*events
, int pollfd
)
1332 lttng_poll_del(events
, pollfd
);
1334 ret
= close(pollfd
);
1336 ERR("Closing pollfd %d", pollfd
);
1341 * Create and add connection to the given hash table.
1343 * Return poll add value or else -1 on error.
1346 int add_connection(int fd
, struct lttng_poll_event
*events
,
1347 struct lttng_ht
*relay_connections_ht
)
1350 struct relay_command
*relay_connection
;
1353 assert(relay_connections_ht
);
1355 relay_connection
= zmalloc(sizeof(struct relay_command
));
1356 if (relay_connection
== NULL
) {
1357 PERROR("Relay command zmalloc");
1362 ret
= read(fd
, relay_connection
, sizeof(*relay_connection
));
1363 } while (ret
< 0 && errno
== EINTR
);
1364 if (ret
< 0 || ret
< sizeof(*relay_connection
)) {
1365 PERROR("read relay cmd pipe");
1369 lttng_ht_node_init_ulong(&relay_connection
->sock_n
,
1370 (unsigned long) relay_connection
->sock
->fd
);
1372 lttng_ht_add_unique_ulong(relay_connections_ht
,
1373 &relay_connection
->sock_n
);
1376 return lttng_poll_add(events
, relay_connection
->sock
->fd
,
1377 LPOLLIN
| LPOLLRDHUP
);
1380 free(relay_connection
);
1386 void deferred_free_connection(struct rcu_head
*head
)
1388 struct relay_command
*relay_connection
=
1389 caa_container_of(head
, struct relay_command
, rcu_node
);
1391 if (relay_connection
->session
&&
1392 relay_connection
->session
->viewer_attached
> 0) {
1393 relay_connection
->session
->viewer_attached
--;
1395 lttcomm_destroy_sock(relay_connection
->sock
);
1396 free(relay_connection
);
1400 void deferred_free_viewer_stream(struct rcu_head
*head
)
1402 struct relay_viewer_stream
*stream
=
1403 caa_container_of(head
, struct relay_viewer_stream
, rcu_node
);
1405 if (stream
->ctf_trace
) {
1406 uatomic_dec(&stream
->ctf_trace
->refcount
);
1407 assert(uatomic_read(&stream
->ctf_trace
->refcount
) >= 0);
1408 if (uatomic_read(&stream
->ctf_trace
->refcount
) == 0) {
1409 DBG("Freeing ctf_trace %" PRIu64
, stream
->ctf_trace
->id
);
1410 free(stream
->ctf_trace
);
1414 free(stream
->path_name
);
1415 free(stream
->channel_name
);
1420 void viewer_del_streams(uint64_t session_id
)
1423 struct relay_viewer_stream
*stream
;
1424 struct lttng_ht_node_u64
*node
;
1425 struct lttng_ht_iter iter
;
1428 cds_lfht_for_each_entry(viewer_streams_ht
->ht
, &iter
.iter
, node
, node
) {
1429 node
= lttng_ht_iter_get_node_u64(&iter
);
1434 stream
= caa_container_of(node
, struct relay_viewer_stream
, stream_n
);
1435 if (stream
->session_id
!= session_id
) {
1439 if (stream
->read_fd
> 0) {
1440 ret
= close(stream
->read_fd
);
1442 PERROR("close read_fd");
1445 if (stream
->index_read_fd
> 0) {
1446 ret
= close(stream
->index_read_fd
);
1448 PERROR("close index_read_fd");
1451 if (stream
->metadata_flag
&& stream
->ctf_trace
) {
1452 stream
->ctf_trace
->metadata_sent
= 0;
1454 ret
= lttng_ht_del(viewer_streams_ht
, &iter
);
1456 call_rcu(&stream
->rcu_node
, deferred_free_viewer_stream
);
1462 * Delete and free a connection.
1464 * RCU read side lock MUST be acquired.
1467 void del_connection(struct lttng_ht
*relay_connections_ht
,
1468 struct lttng_ht_iter
*iter
, struct relay_command
*relay_connection
)
1472 assert(relay_connections_ht
);
1474 assert(relay_connection
);
1476 ret
= lttng_ht_del(relay_connections_ht
, iter
);
1479 viewer_del_streams(relay_connection
->session_id
);
1481 call_rcu(&relay_connection
->rcu_node
, deferred_free_connection
);
1485 * This thread does the actual work
1488 void *thread_worker(void *data
)
1492 struct relay_command
*relay_connection
;
1493 struct lttng_poll_event events
;
1494 struct lttng_ht
*relay_connections_ht
;
1495 struct lttng_ht_node_ulong
*node
;
1496 struct lttng_ht_iter iter
;
1497 struct lttng_viewer_cmd recv_hdr
;
1498 struct relay_local_data
*relay_ctx
= (struct relay_local_data
*) data
;
1499 struct lttng_ht
*sessions_ht
= relay_ctx
->sessions_ht
;
1501 DBG("[thread] Live viewer relay worker started");
1503 rcu_register_thread();
1505 /* table of connections indexed on socket */
1506 relay_connections_ht
= lttng_ht_new(0, LTTNG_HT_TYPE_ULONG
);
1507 if (!relay_connections_ht
) {
1508 goto relay_connections_ht_error
;
1511 ret
= create_thread_poll_set(&events
, 2);
1513 goto error_poll_create
;
1516 ret
= lttng_poll_add(&events
, live_relay_cmd_pipe
[0], LPOLLIN
| LPOLLRDHUP
);
1525 /* Infinite blocking call, waiting for transmission */
1526 DBG3("Relayd live viewer worker thread polling...");
1527 ret
= lttng_poll_wait(&events
, -1);
1530 * Restart interrupted system call.
1532 if (errno
== EINTR
) {
1541 * Process control. The control connection is prioritised so we don't
1542 * starve it with high throughput tracing data on the data
1545 for (i
= 0; i
< nb_fd
; i
++) {
1546 /* Fetch once the poll data */
1547 uint32_t revents
= LTTNG_POLL_GETEV(&events
, i
);
1548 int pollfd
= LTTNG_POLL_GETFD(&events
, i
);
1550 /* Thread quit pipe has been closed. Killing thread. */
1551 ret
= check_thread_quit_pipe(pollfd
, revents
);
1557 /* Inspect the relay cmd pipe for new connection */
1558 if (pollfd
== live_relay_cmd_pipe
[0]) {
1559 if (revents
& (LPOLLERR
| LPOLLHUP
| LPOLLRDHUP
)) {
1560 ERR("Relay live pipe error");
1562 } else if (revents
& LPOLLIN
) {
1563 DBG("Relay live viewer command received");
1564 ret
= add_connection(live_relay_cmd_pipe
[0],
1565 &events
, relay_connections_ht
);
1570 } else if (revents
) {
1572 lttng_ht_lookup(relay_connections_ht
,
1573 (void *)((unsigned long) pollfd
), &iter
);
1574 node
= lttng_ht_iter_get_node_ulong(&iter
);
1576 DBG2("Relay viewer sock %d not found", pollfd
);
1580 relay_connection
= caa_container_of(node
, struct relay_command
,
1583 if (revents
& (LPOLLERR
)) {
1584 cleanup_poll_connection(&events
, pollfd
);
1585 del_connection(relay_connections_ht
, &iter
,
1587 } else if (revents
& (LPOLLHUP
| LPOLLRDHUP
)) {
1588 DBG("Viewer socket %d hung up", pollfd
);
1589 cleanup_poll_connection(&events
, pollfd
);
1590 del_connection(relay_connections_ht
, &iter
,
1592 } else if (revents
& LPOLLIN
) {
1593 ret
= relay_connection
->sock
->ops
->recvmsg(
1594 relay_connection
->sock
, &recv_hdr
,
1595 sizeof(struct lttng_viewer_cmd
),
1597 /* connection closed */
1599 cleanup_poll_connection(&events
, pollfd
);
1600 del_connection( relay_connections_ht
, &iter
,
1602 DBG("Viewer control connection closed with %d",
1605 if (relay_connection
->session
) {
1606 DBG2("Relay viewer worker receiving data for "
1607 "session: %" PRIu64
,
1608 relay_connection
->session
->id
);
1610 ret
= process_control(&recv_hdr
, relay_connection
,
1613 /* Clear the session on error. */
1614 cleanup_poll_connection(&events
, pollfd
);
1615 del_connection(relay_connections_ht
, &iter
,
1617 DBG("Viewer connection closed with %d", pollfd
);
1628 lttng_poll_clean(&events
);
1630 /* empty the hash table and free the memory */
1632 cds_lfht_for_each_entry(relay_connections_ht
->ht
, &iter
.iter
, node
, node
) {
1633 node
= lttng_ht_iter_get_node_ulong(&iter
);
1638 relay_connection
= caa_container_of(node
, struct relay_command
,
1640 del_connection(relay_connections_ht
, &iter
, relay_connection
);
1644 lttng_ht_destroy(relay_connections_ht
);
1645 relay_connections_ht_error
:
1646 /* Close relay cmd pipes */
1647 utils_close_pipe(live_relay_cmd_pipe
);
1649 DBG("Viewer worker thread exited with error");
1651 DBG("Viewer worker thread cleanup complete");
1653 rcu_unregister_thread();
1658 * Create the relay command pipe to wake thread_manage_apps.
1659 * Closed in cleanup().
1661 static int create_relay_cmd_pipe(void)
1665 ret
= utils_create_pipe_cloexec(live_relay_cmd_pipe
);
1670 void live_stop_threads()
1677 ret
= pthread_join(live_listener_thread
, &status
);
1679 PERROR("pthread_join live listener");
1680 goto error
; /* join error, exit without cleanup */
1683 ret
= pthread_join(live_worker_thread
, &status
);
1685 PERROR("pthread_join live worker");
1686 goto error
; /* join error, exit without cleanup */
1689 ret
= pthread_join(live_dispatcher_thread
, &status
);
1691 PERROR("pthread_join live dispatcher");
1692 goto error
; /* join error, exit without cleanup */
1704 int live_start_threads(struct lttng_uri
*uri
,
1705 struct relay_local_data
*relay_ctx
, int quit_pipe
[2])
1714 live_thread_quit_pipe
[0] = quit_pipe
[0];
1715 live_thread_quit_pipe
[1] = quit_pipe
[1];
1717 /* Check if daemon is UID = 0 */
1718 is_root
= !getuid();
1721 if (live_uri
->port
< 1024) {
1722 ERR("Need to be root to use ports < 1024");
1728 /* Setup the thread apps communication pipe. */
1729 if ((ret
= create_relay_cmd_pipe()) < 0) {
1733 /* Init relay command queue. */
1734 cds_wfq_init(&viewer_cmd_queue
.queue
);
1736 /* Set up max poll set size */
1737 lttng_poll_set_max_size();
1739 /* Setup the dispatcher thread */
1740 ret
= pthread_create(&live_dispatcher_thread
, NULL
,
1741 thread_dispatcher
, (void *) NULL
);
1743 PERROR("pthread_create viewer dispatcher");
1744 goto exit_dispatcher
;
1747 /* Setup the worker thread */
1748 ret
= pthread_create(&live_worker_thread
, NULL
,
1749 thread_worker
, relay_ctx
);
1751 PERROR("pthread_create viewer worker");
1755 /* Setup the listener thread */
1756 ret
= pthread_create(&live_listener_thread
, NULL
,
1757 thread_listener
, (void *) NULL
);
1759 PERROR("pthread_create viewer listener");
1767 ret
= pthread_join(live_listener_thread
, &status
);
1769 PERROR("pthread_join live listener");
1770 goto error
; /* join error, exit without cleanup */
1774 ret
= pthread_join(live_worker_thread
, &status
);
1776 PERROR("pthread_join live worker");
1777 goto error
; /* join error, exit without cleanup */
1781 ret
= pthread_join(live_dispatcher_thread
, &status
);
1783 PERROR("pthread_join live dispatcher");
1784 goto error
; /* join error, exit without cleanup */