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"
59 #include "health-relayd.h"
61 static struct lttng_uri
*live_uri
;
64 * Quit pipe for all threads. This permits a single cancellation point
65 * for all threads when receiving an event on the pipe.
67 static int live_thread_quit_pipe
[2] = { -1, -1 };
70 * This pipe is used to inform the worker thread that a command is queued and
71 * ready to be processed.
73 static int live_relay_cmd_pipe
[2] = { -1, -1 };
75 /* Shared between threads */
76 static int live_dispatch_thread_exit
;
78 static pthread_t live_listener_thread
;
79 static pthread_t live_dispatcher_thread
;
80 static pthread_t live_worker_thread
;
83 * Relay command queue.
85 * The live_thread_listener and live_thread_dispatcher communicate with this
88 static struct relay_cmd_queue viewer_cmd_queue
;
90 static uint64_t last_relay_viewer_session_id
;
104 * Write to writable pipe used to notify a thread.
107 int notify_thread_pipe(int wpipe
)
112 ret
= write(wpipe
, "!", 1);
113 } while (ret
< 0 && errno
== EINTR
);
114 if (ret
< 0 || ret
!= 1) {
115 PERROR("write poll pipe");
122 * Stop all threads by closing the thread quit pipe.
125 void stop_threads(void)
129 /* Stopping all threads */
130 DBG("Terminating all live threads");
131 ret
= notify_thread_pipe(live_thread_quit_pipe
[1]);
133 ERR("write error on thread quit pipe");
136 /* Dispatch thread */
137 CMM_STORE_SHARED(live_dispatch_thread_exit
, 1);
138 futex_nto1_wake(&viewer_cmd_queue
.futex
);
142 * Create a poll set with O_CLOEXEC and add the thread quit pipe to the set.
145 int create_thread_poll_set(struct lttng_poll_event
*events
, int size
)
149 if (events
== NULL
|| size
== 0) {
154 ret
= lttng_poll_create(events
, size
, LTTNG_CLOEXEC
);
160 ret
= lttng_poll_add(events
, live_thread_quit_pipe
[0], LPOLLIN
);
172 * Check if the thread quit pipe was triggered.
174 * Return 1 if it was triggered else 0;
177 int check_thread_quit_pipe(int fd
, uint32_t events
)
179 if (fd
== live_thread_quit_pipe
[0] && (events
& LPOLLIN
)) {
187 * Create and init socket from uri.
190 struct lttcomm_sock
*init_socket(struct lttng_uri
*uri
)
193 struct lttcomm_sock
*sock
= NULL
;
195 sock
= lttcomm_alloc_sock_from_uri(uri
);
197 ERR("Allocating socket");
201 ret
= lttcomm_create_sock(sock
);
205 DBG("Listening on sock %d for live", sock
->fd
);
207 ret
= sock
->ops
->bind(sock
);
212 ret
= sock
->ops
->listen(sock
, -1);
222 lttcomm_destroy_sock(sock
);
228 * This thread manages the listening for new connections on the network
231 void *thread_listener(void *data
)
233 int i
, ret
, pollfd
, err
= -1;
235 uint32_t revents
, nb_fd
;
236 struct lttng_poll_event events
;
237 struct lttcomm_sock
*live_control_sock
;
239 DBG("[thread] Relay live listener started");
241 health_register(health_relayd
, HEALTH_RELAYD_TYPE_LIVE_LISTENER
);
243 health_code_update();
245 live_control_sock
= init_socket(live_uri
);
246 if (!live_control_sock
) {
247 goto error_sock_control
;
251 * Pass 3 as size here for the thread quit pipe, control and data socket.
253 ret
= create_thread_poll_set(&events
, 2);
255 goto error_create_poll
;
258 /* Add the control socket */
259 ret
= lttng_poll_add(&events
, live_control_sock
->fd
, LPOLLIN
| LPOLLRDHUP
);
265 health_code_update();
267 DBG("Listener accepting live viewers connections");
271 ret
= lttng_poll_wait(&events
, -1);
275 * Restart interrupted system call.
277 if (errno
== EINTR
) {
284 DBG("Relay new viewer connection received");
285 for (i
= 0; i
< nb_fd
; i
++) {
286 health_code_update();
288 /* Fetch once the poll data */
289 revents
= LTTNG_POLL_GETEV(&events
, i
);
290 pollfd
= LTTNG_POLL_GETFD(&events
, i
);
292 /* Thread quit pipe has been closed. Killing thread. */
293 ret
= check_thread_quit_pipe(pollfd
, revents
);
299 if (revents
& (LPOLLERR
| LPOLLHUP
| LPOLLRDHUP
)) {
300 ERR("socket poll error");
302 } else if (revents
& LPOLLIN
) {
304 * Get allocated in this thread, enqueued to a global queue,
305 * dequeued and freed in the worker thread.
307 struct relay_command
*relay_cmd
;
308 struct lttcomm_sock
*newsock
;
310 relay_cmd
= zmalloc(sizeof(*relay_cmd
));
312 PERROR("relay command zmalloc");
316 assert(pollfd
== live_control_sock
->fd
);
317 newsock
= live_control_sock
->ops
->accept(live_control_sock
);
319 PERROR("accepting control sock");
323 DBG("Relay viewer connection accepted socket %d", newsock
->fd
);
324 ret
= setsockopt(newsock
->fd
, SOL_SOCKET
, SO_REUSEADDR
, &val
,
327 PERROR("setsockopt inet");
328 lttcomm_destroy_sock(newsock
);
332 relay_cmd
->sock
= newsock
;
335 * Lock free enqueue the request.
337 cds_wfq_enqueue(&viewer_cmd_queue
.queue
, &relay_cmd
->node
);
340 * Wake the dispatch queue futex. Implicit memory
341 * barrier with the exchange in cds_wfq_enqueue.
343 futex_nto1_wake(&viewer_cmd_queue
.futex
);
351 lttng_poll_clean(&events
);
353 if (live_control_sock
->fd
>= 0) {
354 ret
= live_control_sock
->ops
->close(live_control_sock
);
359 lttcomm_destroy_sock(live_control_sock
);
363 DBG("Live viewer listener thread exited with error");
365 health_unregister(health_relayd
);
366 DBG("Live viewer listener thread cleanup complete");
372 * This thread manages the dispatching of the requests to worker threads
375 void *thread_dispatcher(void *data
)
378 struct cds_wfq_node
*node
;
379 struct relay_command
*relay_cmd
= NULL
;
381 DBG("[thread] Live viewer relay dispatcher started");
383 health_register(health_relayd
, HEALTH_RELAYD_TYPE_LIVE_DISPATCHER
);
385 health_code_update();
387 while (!CMM_LOAD_SHARED(live_dispatch_thread_exit
)) {
388 health_code_update();
390 /* Atomically prepare the queue futex */
391 futex_nto1_prepare(&viewer_cmd_queue
.futex
);
394 health_code_update();
396 /* Dequeue commands */
397 node
= cds_wfq_dequeue_blocking(&viewer_cmd_queue
.queue
);
399 DBG("Woken up but nothing in the live-viewer "
400 "relay command queue");
401 /* Continue thread execution */
405 relay_cmd
= caa_container_of(node
, struct relay_command
, node
);
406 DBG("Dispatching viewer request waiting on sock %d",
407 relay_cmd
->sock
->fd
);
410 * Inform worker thread of the new request. This call is blocking
411 * so we can be assured that the data will be read at some point in
412 * time or wait to the end of the world :)
415 ret
= write(live_relay_cmd_pipe
[1], relay_cmd
,
417 } while (ret
< 0 && errno
== EINTR
);
419 if (ret
< 0 || ret
!= sizeof(struct relay_command
)) {
420 PERROR("write cmd pipe");
423 } while (node
!= NULL
);
425 /* Futex wait on queue. Blocking call on futex() */
427 futex_nto1_wait(&viewer_cmd_queue
.futex
);
431 /* Normal exit, no error */
437 ERR("Health error occurred in %s", __func__
);
439 health_unregister(health_relayd
);
440 DBG("Live viewer dispatch thread dying");
446 * Establish connection with the viewer and check the versions.
448 * Return 0 on success or else negative value.
451 int viewer_connect(struct relay_command
*cmd
)
454 struct lttng_viewer_connect reply
, msg
;
458 cmd
->version_check_done
= 1;
460 health_code_update();
462 /* Get version from the other side. */
463 ret
= cmd
->sock
->ops
->recvmsg(cmd
->sock
, &msg
, sizeof(msg
), 0);
464 if (ret
< 0 || ret
!= sizeof(msg
)) {
466 /* Orderly shutdown. Not necessary to print an error. */
467 DBG("Socket %d did an orderly shutdown", cmd
->sock
->fd
);
469 ERR("Relay failed to receive the version values.");
475 health_code_update();
477 reply
.major
= RELAYD_VERSION_COMM_MAJOR
;
478 reply
.minor
= RELAYD_VERSION_COMM_MINOR
;
480 /* Major versions must be the same */
481 if (reply
.major
!= be32toh(msg
.major
)) {
482 DBG("Incompatible major versions (%u vs %u)", reply
.major
,
488 cmd
->major
= reply
.major
;
489 /* We adapt to the lowest compatible version */
490 if (reply
.minor
<= be32toh(msg
.minor
)) {
491 cmd
->minor
= reply
.minor
;
493 cmd
->minor
= be32toh(msg
.minor
);
496 if (be32toh(msg
.type
) == VIEWER_CLIENT_COMMAND
) {
497 cmd
->type
= RELAY_VIEWER_COMMAND
;
498 } else if (be32toh(msg
.type
) == VIEWER_CLIENT_NOTIFICATION
) {
499 cmd
->type
= RELAY_VIEWER_NOTIFICATION
;
501 ERR("Unknown connection type : %u", be32toh(msg
.type
));
506 reply
.major
= htobe32(reply
.major
);
507 reply
.minor
= htobe32(reply
.minor
);
508 if (cmd
->type
== RELAY_VIEWER_COMMAND
) {
509 reply
.viewer_session_id
= htobe64(++last_relay_viewer_session_id
);
512 health_code_update();
514 ret
= cmd
->sock
->ops
->sendmsg(cmd
->sock
, &reply
,
515 sizeof(struct lttng_viewer_connect
), 0);
517 ERR("Relay sending version");
520 health_code_update();
522 DBG("Version check done using protocol %u.%u", cmd
->major
, cmd
->minor
);
530 * Send the viewer the list of current sessions.
532 * Return 0 on success or else a negative value.
535 int viewer_list_sessions(struct relay_command
*cmd
,
536 struct lttng_ht
*sessions_ht
)
539 struct lttng_viewer_list_sessions session_list
;
541 long approx_before
, approx_after
;
542 struct lttng_ht_node_ulong
*node
;
543 struct lttng_ht_iter iter
;
544 struct lttng_viewer_session send_session
;
545 struct relay_session
*session
;
547 DBG("List sessions received");
549 if (cmd
->version_check_done
== 0) {
550 ERR("Trying to list sessions before version check");
556 cds_lfht_count_nodes(sessions_ht
->ht
, &approx_before
, &count
, &approx_after
);
557 session_list
.sessions_count
= htobe32(count
);
559 health_code_update();
561 ret
= cmd
->sock
->ops
->sendmsg(cmd
->sock
, &session_list
,
562 sizeof(session_list
), 0);
564 ERR("Relay sending sessions list");
568 health_code_update();
570 cds_lfht_for_each_entry(sessions_ht
->ht
, &iter
.iter
, node
, node
) {
571 health_code_update();
573 node
= lttng_ht_iter_get_node_ulong(&iter
);
577 session
= caa_container_of(node
, struct relay_session
, session_n
);
579 strncpy(send_session
.session_name
, session
->session_name
,
580 sizeof(send_session
.session_name
));
581 strncpy(send_session
.hostname
, session
->hostname
,
582 sizeof(send_session
.hostname
));
583 send_session
.id
= htobe64(session
->id
);
584 send_session
.live_timer
= htobe32(session
->live_timer
);
585 send_session
.clients
= htobe32(session
->viewer_attached
);
586 send_session
.streams
= htobe32(session
->stream_count
);
588 health_code_update();
590 ret
= cmd
->sock
->ops
->sendmsg(cmd
->sock
, &send_session
,
591 sizeof(send_session
), 0);
593 ERR("Relay sending session info");
597 health_code_update();
612 * Allocate and init a new viewer_stream.
614 * Copies the values from the stream passed in parameter and insert the new
615 * stream in the viewer_streams_ht.
617 * MUST be called with rcu_read_lock held.
619 * Returns 0 on success or a negative value on error.
622 int init_viewer_stream(struct relay_stream
*stream
)
625 struct relay_viewer_stream
*viewer_stream
;
629 viewer_stream
= zmalloc(sizeof(*viewer_stream
));
630 if (!viewer_stream
) {
631 PERROR("relay viewer stream zmalloc");
636 viewer_stream
->read_fd
= -1;
637 viewer_stream
->index_read_fd
= -1;
638 viewer_stream
->session_id
= stream
->session
->id
;
639 viewer_stream
->stream_handle
= stream
->stream_handle
;
640 viewer_stream
->path_name
= strndup(stream
->path_name
,
641 LTTNG_VIEWER_PATH_MAX
);
642 viewer_stream
->channel_name
= strndup(stream
->channel_name
,
643 LTTNG_VIEWER_NAME_MAX
);
644 viewer_stream
->total_index_received
= stream
->total_index_received
;
645 viewer_stream
->tracefile_size
= stream
->tracefile_size
;
646 viewer_stream
->tracefile_count
= stream
->tracefile_count
;
647 viewer_stream
->metadata_flag
= stream
->metadata_flag
;
650 * This is to avoid a race between the initialization of this object and
651 * the close of the given stream. If the stream is unable to find this
652 * viewer stream when closing, this copy will at least take the latest
655 viewer_stream
->total_index_received
= stream
->total_index_received
;
658 * The deletion of this ctf_trace object is only done in a call RCU of the
659 * relay stream making it valid as long as we have the read side lock.
661 viewer_stream
->ctf_trace
= stream
->ctf_trace
;
662 uatomic_inc(&viewer_stream
->ctf_trace
->refcount
);
664 lttng_ht_node_init_u64(&viewer_stream
->stream_n
, stream
->stream_handle
);
665 lttng_ht_add_unique_u64(viewer_streams_ht
, &viewer_stream
->stream_n
);
674 * Send the viewer the list of current sessions.
677 int viewer_attach_session(struct relay_command
*cmd
,
678 struct lttng_ht
*sessions_ht
)
680 int ret
, send_streams
= 0, nb_streams
= 0;
681 struct lttng_viewer_attach_session_request request
;
682 struct lttng_viewer_attach_session_response response
;
683 struct lttng_viewer_stream send_stream
;
684 struct relay_stream
*stream
;
685 struct relay_viewer_stream
*viewer_stream
;
686 struct lttng_ht_node_ulong
*node
;
687 struct lttng_ht_node_u64
*node64
;
688 struct lttng_ht_iter iter
;
689 struct relay_session
*session
;
694 DBG("Attach session received");
696 if (cmd
->version_check_done
== 0) {
697 ERR("Trying to attach session before version check");
702 health_code_update();
704 ret
= cmd
->sock
->ops
->recvmsg(cmd
->sock
, &request
, sizeof(request
), 0);
705 if (ret
< 0 || ret
!= sizeof(request
)) {
707 /* Orderly shutdown. Not necessary to print an error. */
708 DBG("Socket %d did an orderly shutdown", cmd
->sock
->fd
);
710 ERR("Relay failed to receive the attach parameters.");
716 health_code_update();
719 lttng_ht_lookup(sessions_ht
,
720 (void *)((unsigned long) be64toh(request
.session_id
)), &iter
);
721 node
= lttng_ht_iter_get_node_ulong(&iter
);
723 DBG("Relay session %" PRIu64
" not found",
724 be64toh(request
.session_id
));
725 response
.status
= htobe32(VIEWER_ATTACH_UNK
);
729 session
= caa_container_of(node
, struct relay_session
, session_n
);
730 if (cmd
->session_id
== session
->id
) {
731 /* Same viewer already attached, just send the stream list. */
733 response
.status
= htobe32(VIEWER_ATTACH_OK
);
734 } else if (session
->viewer_attached
!= 0) {
735 DBG("Already a viewer attached");
736 response
.status
= htobe32(VIEWER_ATTACH_ALREADY
);
738 } else if (session
->live_timer
== 0) {
739 DBG("Not live session");
740 response
.status
= htobe32(VIEWER_ATTACH_NOT_LIVE
);
743 session
->viewer_attached
++;
745 response
.status
= htobe32(VIEWER_ATTACH_OK
);
746 cmd
->session_id
= session
->id
;
747 cmd
->session
= session
;
750 switch (be32toh(request
.seek
)) {
751 case VIEWER_SEEK_BEGINNING
:
752 /* Default behaviour. */
754 case VIEWER_SEEK_LAST
:
758 ERR("Wrong seek parameter");
759 response
.status
= htobe32(VIEWER_ATTACH_SEEK_ERR
);
765 /* We should only be there if we have a session to attach to. */
769 * Fill the viewer_streams_ht to count the number of streams
770 * ready to be sent and avoid concurrency issues on the
771 * relay_streams_ht and don't rely on a total session stream count.
773 cds_lfht_for_each_entry(relay_streams_ht
->ht
, &iter
.iter
, node
, node
) {
774 struct relay_viewer_stream
*vstream
;
776 health_code_update();
778 node
= lttng_ht_iter_get_node_ulong(&iter
);
782 stream
= caa_container_of(node
, struct relay_stream
, stream_n
);
783 if (stream
->session
!= cmd
->session
) {
788 * Don't send streams with no ctf_trace, they are not ready to be
791 if (!stream
->ctf_trace
) {
795 vstream
= live_find_viewer_stream_by_id(stream
->stream_handle
);
797 ret
= init_viewer_stream(stream
);
804 response
.streams_count
= htobe32(nb_streams
);
808 health_code_update();
809 ret
= cmd
->sock
->ops
->sendmsg(cmd
->sock
, &response
, sizeof(response
), 0);
811 ERR("Relay sending viewer attach response");
814 health_code_update();
817 * Unknown or busy session, just return gracefully, the viewer knows what
825 /* We should only be there if we have a session to attach to. */
827 cds_lfht_for_each_entry(viewer_streams_ht
->ht
, &iter
.iter
, node
, node
) {
828 health_code_update();
830 node64
= lttng_ht_iter_get_node_u64(&iter
);
834 viewer_stream
= caa_container_of(node64
, struct relay_viewer_stream
,
836 if (viewer_stream
->session_id
!= cmd
->session
->id
) {
840 send_stream
.id
= htobe64(viewer_stream
->stream_handle
);
841 send_stream
.ctf_trace_id
= htobe64(viewer_stream
->ctf_trace
->id
);
842 send_stream
.metadata_flag
= htobe32(viewer_stream
->metadata_flag
);
843 strncpy(send_stream
.path_name
, viewer_stream
->path_name
,
844 sizeof(send_stream
.path_name
));
845 strncpy(send_stream
.channel_name
, viewer_stream
->channel_name
,
846 sizeof(send_stream
.channel_name
));
848 ret
= cmd
->sock
->ops
->sendmsg(cmd
->sock
, &send_stream
,
849 sizeof(send_stream
), 0);
851 ERR("Relay sending stream %" PRIu64
, viewer_stream
->stream_handle
);
854 DBG("Sent stream %" PRIu64
" to viewer", viewer_stream
->stream_handle
);
866 * Open index file using a given viewer stream.
868 * Return 0 on success or else a negative value.
870 static int open_index(struct relay_viewer_stream
*stream
)
873 char fullpath
[PATH_MAX
];
874 struct lttng_packet_index_file_hdr hdr
;
876 if (stream
->tracefile_size
> 0) {
877 /* For now we don't support on-disk ring buffer. */
881 ret
= snprintf(fullpath
, sizeof(fullpath
), "%s/" DEFAULT_INDEX_DIR
882 "/%s" DEFAULT_INDEX_FILE_SUFFIX
,
883 stream
->path_name
, stream
->channel_name
);
885 PERROR("snprintf index path");
890 DBG("Opening index file %s in read only", fullpath
);
891 ret
= open(fullpath
, O_RDONLY
);
893 if (errno
== ENOENT
) {
897 PERROR("opening index in read-only");
901 stream
->index_read_fd
= ret
;
902 DBG("Opening index file %s in read only, (fd: %d)", fullpath
, ret
);
905 health_code_update();
906 ret
= read(stream
->index_read_fd
, &hdr
, sizeof(hdr
));
907 } while (ret
< 0 && errno
== EINTR
);
909 PERROR("Reading index header");
912 if (strncmp(hdr
.magic
, INDEX_MAGIC
, sizeof(hdr
.magic
)) != 0) {
913 ERR("Invalid header magic");
917 if (be32toh(hdr
.index_major
) != INDEX_MAJOR
||
918 be32toh(hdr
.index_minor
) != INDEX_MINOR
) {
919 ERR("Invalid header version");
931 * Get viewer stream from stream id.
933 * RCU read side lock MUST be acquired.
935 struct relay_viewer_stream
*live_find_viewer_stream_by_id(uint64_t stream_id
)
937 struct lttng_ht_node_u64
*node
;
938 struct lttng_ht_iter iter
;
939 struct relay_viewer_stream
*stream
= NULL
;
941 lttng_ht_lookup(viewer_streams_ht
, &stream_id
, &iter
);
942 node
= lttng_ht_iter_get_node_u64(&iter
);
944 DBG("Relay viewer stream %" PRIu64
" not found", stream_id
);
947 stream
= caa_container_of(node
, struct relay_viewer_stream
, stream_n
);
954 * Send the next index for a stream.
956 * Return 0 on success or else a negative value.
959 int viewer_get_next_index(struct relay_command
*cmd
,
960 struct lttng_ht
*sessions_ht
)
963 struct lttng_viewer_get_next_index request_index
;
964 struct lttng_viewer_index viewer_index
;
965 struct lttng_packet_index packet_index
;
966 struct relay_viewer_stream
*vstream
;
967 struct relay_stream
*rstream
;
972 DBG("Viewer get next index");
974 if (cmd
->version_check_done
== 0) {
975 ERR("Trying to request index before version check");
980 health_code_update();
981 ret
= cmd
->sock
->ops
->recvmsg(cmd
->sock
, &request_index
,
982 sizeof(request_index
), 0);
983 if (ret
< 0 || ret
!= sizeof(request_index
)) {
985 ERR("Relay didn't receive the whole packet");
988 health_code_update();
991 vstream
= live_find_viewer_stream_by_id(be64toh(request_index
.stream_id
));
997 memset(&viewer_index
, 0, sizeof(viewer_index
));
1000 * The viewer should not ask for index on metadata stream.
1002 if (vstream
->metadata_flag
) {
1003 viewer_index
.status
= htobe32(VIEWER_INDEX_HUP
);
1007 /* First time, we open the index file */
1008 if (vstream
->index_read_fd
< 0) {
1009 ret
= open_index(vstream
);
1010 if (ret
== ENOENT
) {
1012 * The index is created only when the first data packet arrives, it
1013 * might not be ready at the beginning of the session
1015 viewer_index
.status
= htobe32(VIEWER_INDEX_RETRY
);
1017 } else if (ret
< 0) {
1018 viewer_index
.status
= htobe32(VIEWER_INDEX_ERR
);
1023 rstream
= relay_stream_find_by_id(vstream
->stream_handle
);
1025 if (rstream
->beacon_ts_end
!= -1ULL &&
1026 vstream
->last_sent_index
== rstream
->total_index_received
) {
1027 viewer_index
.status
= htobe32(VIEWER_INDEX_INACTIVE
);
1028 viewer_index
.timestamp_end
= htobe64(rstream
->beacon_ts_end
);
1032 if (rstream
->total_index_received
<= vstream
->last_sent_index
) {
1033 /* No new index to send, retry later. */
1034 viewer_index
.status
= htobe32(VIEWER_INDEX_RETRY
);
1037 } else if (!rstream
&&
1038 vstream
->total_index_received
== vstream
->last_sent_index
) {
1039 /* Last index sent and stream closed */
1040 viewer_index
.status
= htobe32(VIEWER_INDEX_HUP
);
1044 if (!vstream
->ctf_trace
->metadata_received
||
1045 vstream
->ctf_trace
->metadata_received
>
1046 vstream
->ctf_trace
->metadata_sent
) {
1047 viewer_index
.flags
|= LTTNG_VIEWER_FLAG_NEW_METADATA
;
1051 health_code_update();
1052 ret
= read(vstream
->index_read_fd
, &packet_index
,
1053 sizeof(packet_index
));
1054 } while (ret
< 0 && errno
== EINTR
);
1055 if (ret
< sizeof(packet_index
)) {
1056 PERROR("Relay reading index file");
1057 viewer_index
.status
= htobe32(VIEWER_INDEX_ERR
);
1059 viewer_index
.status
= htobe32(VIEWER_INDEX_OK
);
1060 vstream
->last_sent_index
++;
1064 * Indexes are stored in big endian, no need to switch before sending.
1066 viewer_index
.offset
= packet_index
.offset
;
1067 viewer_index
.packet_size
= packet_index
.packet_size
;
1068 viewer_index
.content_size
= packet_index
.content_size
;
1069 viewer_index
.timestamp_begin
= packet_index
.timestamp_begin
;
1070 viewer_index
.timestamp_end
= packet_index
.timestamp_end
;
1071 viewer_index
.events_discarded
= packet_index
.events_discarded
;
1072 viewer_index
.stream_id
= packet_index
.stream_id
;
1075 viewer_index
.flags
= htobe32(viewer_index
.flags
);
1076 health_code_update();
1077 ret
= cmd
->sock
->ops
->sendmsg(cmd
->sock
, &viewer_index
,
1078 sizeof(viewer_index
), 0);
1080 ERR("Relay index to viewer");
1083 health_code_update();
1085 DBG("Index %" PRIu64
"for stream %" PRIu64
"sent",
1086 vstream
->last_sent_index
, vstream
->stream_handle
);
1097 * Send the next index for a stream
1099 * Return 0 on success or else a negative value.
1102 int viewer_get_packet(struct relay_command
*cmd
)
1104 int ret
, send_data
= 0;
1108 struct lttng_viewer_get_packet get_packet_info
;
1109 struct lttng_viewer_trace_packet reply
;
1110 struct relay_viewer_stream
*stream
;
1114 DBG2("Relay get data packet");
1116 if (cmd
->version_check_done
== 0) {
1117 ERR("Trying to get packet before version check");
1122 health_code_update();
1123 ret
= cmd
->sock
->ops
->recvmsg(cmd
->sock
, &get_packet_info
,
1124 sizeof(get_packet_info
), 0);
1125 if (ret
< 0 || ret
!= sizeof(get_packet_info
)) {
1127 ERR("Relay didn't receive the whole packet");
1130 health_code_update();
1132 /* From this point on, the error label can be reached. */
1133 memset(&reply
, 0, sizeof(reply
));
1136 stream
= live_find_viewer_stream_by_id(be64toh(get_packet_info
.stream_id
));
1140 assert(stream
->ctf_trace
);
1143 * First time we read this stream, we need open the tracefile, we should
1144 * only arrive here if an index has already been sent to the viewer, so the
1145 * tracefile must exist, if it does not it is a fatal error.
1147 if (stream
->read_fd
< 0) {
1148 char fullpath
[PATH_MAX
];
1150 ret
= snprintf(fullpath
, PATH_MAX
, "%s/%s", stream
->path_name
,
1151 stream
->channel_name
);
1155 ret
= open(fullpath
, O_RDONLY
);
1157 PERROR("Relay opening trace file");
1160 stream
->read_fd
= ret
;
1163 if (!stream
->ctf_trace
->metadata_received
||
1164 stream
->ctf_trace
->metadata_received
>
1165 stream
->ctf_trace
->metadata_sent
) {
1166 reply
.status
= htobe32(VIEWER_GET_PACKET_ERR
);
1167 reply
.flags
|= LTTNG_VIEWER_FLAG_NEW_METADATA
;
1171 len
= be32toh(get_packet_info
.len
);
1172 data
= zmalloc(len
);
1174 PERROR("relay data zmalloc");
1178 ret
= lseek(stream
->read_fd
, be64toh(get_packet_info
.offset
), SEEK_SET
);
1183 read_len
= read(stream
->read_fd
, data
, len
);
1184 if (read_len
< (ssize_t
) len
) {
1185 PERROR("Relay reading trace file, fd: %d, offset: %" PRIu64
,
1186 stream
->read_fd
, be64toh(get_packet_info
.offset
));
1189 reply
.status
= htobe32(VIEWER_GET_PACKET_OK
);
1190 reply
.len
= htobe32(len
);
1195 reply
.status
= htobe32(VIEWER_GET_PACKET_ERR
);
1198 reply
.flags
= htobe32(reply
.flags
);
1200 health_code_update();
1201 ret
= cmd
->sock
->ops
->sendmsg(cmd
->sock
, &reply
, sizeof(reply
), 0);
1203 ERR("Relay data header to viewer");
1206 health_code_update();
1209 health_code_update();
1210 ret
= cmd
->sock
->ops
->sendmsg(cmd
->sock
, data
, len
, 0);
1212 ERR("Relay send data to viewer");
1215 health_code_update();
1218 DBG("Sent %u bytes for stream %" PRIu64
, len
,
1219 be64toh(get_packet_info
.stream_id
));
1230 * Send the session's metadata
1232 * Return 0 on success else a negative value.
1235 int viewer_get_metadata(struct relay_command
*cmd
)
1241 struct lttng_viewer_get_metadata request
;
1242 struct lttng_viewer_metadata_packet reply
;
1243 struct relay_viewer_stream
*stream
;
1247 DBG("Relay get metadata");
1249 if (cmd
->version_check_done
== 0) {
1250 ERR("Trying to get metadata before version check");
1255 health_code_update();
1256 ret
= cmd
->sock
->ops
->recvmsg(cmd
->sock
, &request
,
1257 sizeof(request
), 0);
1258 if (ret
< 0 || ret
!= sizeof(request
)) {
1260 ERR("Relay didn't receive the whole packet");
1263 health_code_update();
1266 stream
= live_find_viewer_stream_by_id(be64toh(request
.stream_id
));
1267 if (!stream
|| !stream
->metadata_flag
) {
1268 ERR("Invalid metadata stream");
1271 assert(stream
->ctf_trace
);
1272 assert(stream
->ctf_trace
->metadata_sent
<=
1273 stream
->ctf_trace
->metadata_received
);
1275 len
= stream
->ctf_trace
->metadata_received
-
1276 stream
->ctf_trace
->metadata_sent
;
1278 reply
.status
= htobe32(VIEWER_NO_NEW_METADATA
);
1282 /* first time, we open the metadata file */
1283 if (stream
->read_fd
< 0) {
1284 char fullpath
[PATH_MAX
];
1286 ret
= snprintf(fullpath
, PATH_MAX
, "%s/%s", stream
->path_name
,
1287 stream
->channel_name
);
1291 ret
= open(fullpath
, O_RDONLY
);
1293 PERROR("Relay opening metadata file");
1296 stream
->read_fd
= ret
;
1299 reply
.len
= htobe64(len
);
1300 data
= zmalloc(len
);
1302 PERROR("viewer metadata zmalloc");
1306 read_len
= read(stream
->read_fd
, data
, len
);
1307 if (read_len
< (ssize_t
) len
) {
1308 PERROR("Relay reading metadata file");
1311 stream
->ctf_trace
->metadata_sent
+= read_len
;
1312 reply
.status
= htobe32(VIEWER_METADATA_OK
);
1316 reply
.status
= htobe32(VIEWER_METADATA_ERR
);
1319 health_code_update();
1320 ret
= cmd
->sock
->ops
->sendmsg(cmd
->sock
, &reply
, sizeof(reply
), 0);
1322 ERR("Relay data header to viewer");
1325 health_code_update();
1328 ret
= cmd
->sock
->ops
->sendmsg(cmd
->sock
, data
, len
, 0);
1330 ERR("Relay send data to viewer");
1335 DBG("Sent %" PRIu64
" bytes of metadata for stream %" PRIu64
, len
,
1336 be64toh(request
.stream_id
));
1338 DBG("Metadata sent");
1348 * live_relay_unknown_command: send -1 if received unknown command
1351 void live_relay_unknown_command(struct relay_command
*cmd
)
1353 struct lttcomm_relayd_generic_reply reply
;
1356 reply
.ret_code
= htobe32(LTTNG_ERR_UNK
);
1357 ret
= cmd
->sock
->ops
->sendmsg(cmd
->sock
, &reply
,
1358 sizeof(struct lttcomm_relayd_generic_reply
), 0);
1360 ERR("Relay sending unknown command");
1365 * Process the commands received on the control socket
1368 int process_control(struct lttng_viewer_cmd
*recv_hdr
,
1369 struct relay_command
*cmd
, struct lttng_ht
*sessions_ht
)
1373 switch (be32toh(recv_hdr
->cmd
)) {
1374 case VIEWER_CONNECT
:
1375 ret
= viewer_connect(cmd
);
1377 case VIEWER_LIST_SESSIONS
:
1378 ret
= viewer_list_sessions(cmd
, sessions_ht
);
1380 case VIEWER_ATTACH_SESSION
:
1381 ret
= viewer_attach_session(cmd
, sessions_ht
);
1383 case VIEWER_GET_NEXT_INDEX
:
1384 ret
= viewer_get_next_index(cmd
, sessions_ht
);
1386 case VIEWER_GET_PACKET
:
1387 ret
= viewer_get_packet(cmd
);
1389 case VIEWER_GET_METADATA
:
1390 ret
= viewer_get_metadata(cmd
);
1393 ERR("Received unknown viewer command (%u)", be32toh(recv_hdr
->cmd
));
1394 live_relay_unknown_command(cmd
);
1404 void cleanup_poll_connection(struct lttng_poll_event
*events
, int pollfd
)
1410 lttng_poll_del(events
, pollfd
);
1412 ret
= close(pollfd
);
1414 ERR("Closing pollfd %d", pollfd
);
1419 * Create and add connection to the given hash table.
1421 * Return poll add value or else -1 on error.
1424 int add_connection(int fd
, struct lttng_poll_event
*events
,
1425 struct lttng_ht
*relay_connections_ht
)
1428 struct relay_command
*relay_connection
;
1431 assert(relay_connections_ht
);
1433 relay_connection
= zmalloc(sizeof(struct relay_command
));
1434 if (relay_connection
== NULL
) {
1435 PERROR("Relay command zmalloc");
1440 health_code_update();
1441 ret
= read(fd
, relay_connection
, sizeof(*relay_connection
));
1442 } while (ret
< 0 && errno
== EINTR
);
1443 if (ret
< 0 || ret
< sizeof(*relay_connection
)) {
1444 PERROR("read relay cmd pipe");
1448 lttng_ht_node_init_ulong(&relay_connection
->sock_n
,
1449 (unsigned long) relay_connection
->sock
->fd
);
1451 lttng_ht_add_unique_ulong(relay_connections_ht
,
1452 &relay_connection
->sock_n
);
1455 return lttng_poll_add(events
, relay_connection
->sock
->fd
,
1456 LPOLLIN
| LPOLLRDHUP
);
1459 free(relay_connection
);
1465 void deferred_free_connection(struct rcu_head
*head
)
1467 struct relay_command
*relay_connection
=
1468 caa_container_of(head
, struct relay_command
, rcu_node
);
1470 if (relay_connection
->session
&&
1471 relay_connection
->session
->viewer_attached
> 0) {
1472 relay_connection
->session
->viewer_attached
--;
1474 lttcomm_destroy_sock(relay_connection
->sock
);
1475 free(relay_connection
);
1479 void deferred_free_viewer_stream(struct rcu_head
*head
)
1481 struct relay_viewer_stream
*stream
=
1482 caa_container_of(head
, struct relay_viewer_stream
, rcu_node
);
1484 if (stream
->ctf_trace
) {
1485 uatomic_dec(&stream
->ctf_trace
->refcount
);
1486 assert(uatomic_read(&stream
->ctf_trace
->refcount
) >= 0);
1487 if (uatomic_read(&stream
->ctf_trace
->refcount
) == 0) {
1488 DBG("Freeing ctf_trace %" PRIu64
, stream
->ctf_trace
->id
);
1489 free(stream
->ctf_trace
);
1493 free(stream
->path_name
);
1494 free(stream
->channel_name
);
1499 void viewer_del_streams(uint64_t session_id
)
1502 struct relay_viewer_stream
*stream
;
1503 struct lttng_ht_node_u64
*node
;
1504 struct lttng_ht_iter iter
;
1507 cds_lfht_for_each_entry(viewer_streams_ht
->ht
, &iter
.iter
, node
, node
) {
1508 health_code_update();
1510 node
= lttng_ht_iter_get_node_u64(&iter
);
1515 stream
= caa_container_of(node
, struct relay_viewer_stream
, stream_n
);
1516 if (stream
->session_id
!= session_id
) {
1520 if (stream
->read_fd
> 0) {
1521 ret
= close(stream
->read_fd
);
1523 PERROR("close read_fd");
1526 if (stream
->index_read_fd
> 0) {
1527 ret
= close(stream
->index_read_fd
);
1529 PERROR("close index_read_fd");
1532 if (stream
->metadata_flag
&& stream
->ctf_trace
) {
1533 stream
->ctf_trace
->metadata_sent
= 0;
1535 ret
= lttng_ht_del(viewer_streams_ht
, &iter
);
1537 call_rcu(&stream
->rcu_node
, deferred_free_viewer_stream
);
1543 * Delete and free a connection.
1545 * RCU read side lock MUST be acquired.
1548 void del_connection(struct lttng_ht
*relay_connections_ht
,
1549 struct lttng_ht_iter
*iter
, struct relay_command
*relay_connection
)
1553 assert(relay_connections_ht
);
1555 assert(relay_connection
);
1557 ret
= lttng_ht_del(relay_connections_ht
, iter
);
1560 viewer_del_streams(relay_connection
->session_id
);
1562 call_rcu(&relay_connection
->rcu_node
, deferred_free_connection
);
1566 * This thread does the actual work
1569 void *thread_worker(void *data
)
1573 struct relay_command
*relay_connection
;
1574 struct lttng_poll_event events
;
1575 struct lttng_ht
*relay_connections_ht
;
1576 struct lttng_ht_node_ulong
*node
;
1577 struct lttng_ht_iter iter
;
1578 struct lttng_viewer_cmd recv_hdr
;
1579 struct relay_local_data
*relay_ctx
= (struct relay_local_data
*) data
;
1580 struct lttng_ht
*sessions_ht
= relay_ctx
->sessions_ht
;
1582 DBG("[thread] Live viewer relay worker started");
1584 rcu_register_thread();
1586 health_register(health_relayd
, HEALTH_RELAYD_TYPE_LIVE_WORKER
);
1588 /* table of connections indexed on socket */
1589 relay_connections_ht
= lttng_ht_new(0, LTTNG_HT_TYPE_ULONG
);
1590 if (!relay_connections_ht
) {
1591 goto relay_connections_ht_error
;
1594 ret
= create_thread_poll_set(&events
, 2);
1596 goto error_poll_create
;
1599 ret
= lttng_poll_add(&events
, live_relay_cmd_pipe
[0], LPOLLIN
| LPOLLRDHUP
);
1608 health_code_update();
1610 /* Infinite blocking call, waiting for transmission */
1611 DBG3("Relayd live viewer worker thread polling...");
1612 health_poll_entry();
1613 ret
= lttng_poll_wait(&events
, -1);
1617 * Restart interrupted system call.
1619 if (errno
== EINTR
) {
1628 * Process control. The control connection is prioritised so we don't
1629 * starve it with high throughput tracing data on the data
1632 for (i
= 0; i
< nb_fd
; i
++) {
1633 /* Fetch once the poll data */
1634 uint32_t revents
= LTTNG_POLL_GETEV(&events
, i
);
1635 int pollfd
= LTTNG_POLL_GETFD(&events
, i
);
1637 health_code_update();
1639 /* Thread quit pipe has been closed. Killing thread. */
1640 ret
= check_thread_quit_pipe(pollfd
, revents
);
1646 /* Inspect the relay cmd pipe for new connection */
1647 if (pollfd
== live_relay_cmd_pipe
[0]) {
1648 if (revents
& (LPOLLERR
| LPOLLHUP
| LPOLLRDHUP
)) {
1649 ERR("Relay live pipe error");
1651 } else if (revents
& LPOLLIN
) {
1652 DBG("Relay live viewer command received");
1653 ret
= add_connection(live_relay_cmd_pipe
[0],
1654 &events
, relay_connections_ht
);
1659 } else if (revents
) {
1661 lttng_ht_lookup(relay_connections_ht
,
1662 (void *)((unsigned long) pollfd
), &iter
);
1663 node
= lttng_ht_iter_get_node_ulong(&iter
);
1665 DBG2("Relay viewer sock %d not found", pollfd
);
1669 relay_connection
= caa_container_of(node
, struct relay_command
,
1672 if (revents
& (LPOLLERR
)) {
1673 cleanup_poll_connection(&events
, pollfd
);
1674 del_connection(relay_connections_ht
, &iter
,
1676 } else if (revents
& (LPOLLHUP
| LPOLLRDHUP
)) {
1677 DBG("Viewer socket %d hung up", pollfd
);
1678 cleanup_poll_connection(&events
, pollfd
);
1679 del_connection(relay_connections_ht
, &iter
,
1681 } else if (revents
& LPOLLIN
) {
1682 ret
= relay_connection
->sock
->ops
->recvmsg(
1683 relay_connection
->sock
, &recv_hdr
,
1684 sizeof(struct lttng_viewer_cmd
),
1686 /* connection closed */
1688 cleanup_poll_connection(&events
, pollfd
);
1689 del_connection( relay_connections_ht
, &iter
,
1691 DBG("Viewer control connection closed with %d",
1694 if (relay_connection
->session
) {
1695 DBG2("Relay viewer worker receiving data for "
1696 "session: %" PRIu64
,
1697 relay_connection
->session
->id
);
1699 ret
= process_control(&recv_hdr
, relay_connection
,
1702 /* Clear the session on error. */
1703 cleanup_poll_connection(&events
, pollfd
);
1704 del_connection(relay_connections_ht
, &iter
,
1706 DBG("Viewer connection closed with %d", pollfd
);
1717 lttng_poll_clean(&events
);
1719 /* empty the hash table and free the memory */
1721 cds_lfht_for_each_entry(relay_connections_ht
->ht
, &iter
.iter
, node
, node
) {
1722 health_code_update();
1724 node
= lttng_ht_iter_get_node_ulong(&iter
);
1729 relay_connection
= caa_container_of(node
, struct relay_command
,
1731 del_connection(relay_connections_ht
, &iter
, relay_connection
);
1735 lttng_ht_destroy(relay_connections_ht
);
1736 relay_connections_ht_error
:
1737 /* Close relay cmd pipes */
1738 utils_close_pipe(live_relay_cmd_pipe
);
1740 DBG("Viewer worker thread exited with error");
1742 DBG("Viewer worker thread cleanup complete");
1745 ERR("Health error occurred in %s", __func__
);
1747 health_unregister(health_relayd
);
1749 rcu_unregister_thread();
1754 * Create the relay command pipe to wake thread_manage_apps.
1755 * Closed in cleanup().
1757 static int create_relay_cmd_pipe(void)
1761 ret
= utils_create_pipe_cloexec(live_relay_cmd_pipe
);
1766 void live_stop_threads()
1773 ret
= pthread_join(live_listener_thread
, &status
);
1775 PERROR("pthread_join live listener");
1776 goto error
; /* join error, exit without cleanup */
1779 ret
= pthread_join(live_worker_thread
, &status
);
1781 PERROR("pthread_join live worker");
1782 goto error
; /* join error, exit without cleanup */
1785 ret
= pthread_join(live_dispatcher_thread
, &status
);
1787 PERROR("pthread_join live dispatcher");
1788 goto error
; /* join error, exit without cleanup */
1800 int live_start_threads(struct lttng_uri
*uri
,
1801 struct relay_local_data
*relay_ctx
, int quit_pipe
[2])
1810 live_thread_quit_pipe
[0] = quit_pipe
[0];
1811 live_thread_quit_pipe
[1] = quit_pipe
[1];
1813 /* Check if daemon is UID = 0 */
1814 is_root
= !getuid();
1817 if (live_uri
->port
< 1024) {
1818 ERR("Need to be root to use ports < 1024");
1824 /* Setup the thread apps communication pipe. */
1825 if ((ret
= create_relay_cmd_pipe()) < 0) {
1829 /* Init relay command queue. */
1830 cds_wfq_init(&viewer_cmd_queue
.queue
);
1832 /* Set up max poll set size */
1833 lttng_poll_set_max_size();
1835 /* Setup the dispatcher thread */
1836 ret
= pthread_create(&live_dispatcher_thread
, NULL
,
1837 thread_dispatcher
, (void *) NULL
);
1839 PERROR("pthread_create viewer dispatcher");
1840 goto exit_dispatcher
;
1843 /* Setup the worker thread */
1844 ret
= pthread_create(&live_worker_thread
, NULL
,
1845 thread_worker
, relay_ctx
);
1847 PERROR("pthread_create viewer worker");
1851 /* Setup the listener thread */
1852 ret
= pthread_create(&live_listener_thread
, NULL
,
1853 thread_listener
, (void *) NULL
);
1855 PERROR("pthread_create viewer listener");
1863 ret
= pthread_join(live_listener_thread
, &status
);
1865 PERROR("pthread_join live listener");
1866 goto error
; /* join error, exit without cleanup */
1870 ret
= pthread_join(live_worker_thread
, &status
);
1872 PERROR("pthread_join live worker");
1873 goto error
; /* join error, exit without cleanup */
1877 ret
= pthread_join(live_dispatcher_thread
, &status
);
1879 PERROR("pthread_join live dispatcher");
1880 goto error
; /* join error, exit without cleanup */