2 * Copyright (C) 2013 - Julien Desfossez <jdesfossez@efficios.com>
3 * David Goulet <dgoulet@efficios.com>
4 * 2015 - Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License, version 2 only,
8 * as published by the Free Software Foundation.
10 * This program is distributed in the hope that it will be useful, but WITHOUT
11 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
12 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
30 #include <sys/mount.h>
31 #include <sys/resource.h>
32 #include <sys/socket.h>
34 #include <sys/types.h>
37 #include <urcu/futex.h>
38 #include <urcu/uatomic.h>
39 #include <urcu/rculist.h>
43 #include <lttng/lttng.h>
44 #include <common/common.h>
45 #include <common/compat/poll.h>
46 #include <common/compat/socket.h>
47 #include <common/compat/endian.h>
48 #include <common/defaults.h>
49 #include <common/futex.h>
50 #include <common/index/index.h>
51 #include <common/sessiond-comm/sessiond-comm.h>
52 #include <common/sessiond-comm/inet.h>
53 #include <common/sessiond-comm/relayd.h>
54 #include <common/uri.h>
55 #include <common/utils.h>
59 #include "lttng-relayd.h"
61 #include "health-relayd.h"
62 #include "testpoint.h"
63 #include "viewer-stream.h"
66 #include "ctf-trace.h"
67 #include "connection.h"
68 #include "viewer-session.h"
70 #define SESSION_BUF_DEFAULT_COUNT 16
72 static struct lttng_uri
*live_uri
;
75 * This pipe is used to inform the worker thread that a command is queued and
76 * ready to be processed.
78 static int live_conn_pipe
[2] = { -1, -1 };
80 /* Shared between threads */
81 static int live_dispatch_thread_exit
;
83 static pthread_t live_listener_thread
;
84 static pthread_t live_dispatcher_thread
;
85 static pthread_t live_worker_thread
;
88 * Relay command queue.
90 * The live_thread_listener and live_thread_dispatcher communicate with this
93 static struct relay_conn_queue viewer_conn_queue
;
95 static uint64_t last_relay_viewer_session_id
;
96 static pthread_mutex_t last_relay_viewer_session_id_lock
=
97 PTHREAD_MUTEX_INITIALIZER
;
103 void cleanup_relayd_live(void)
111 * Receive a request buffer using a given socket, destination allocated buffer
114 * Return the size of the received message or else a negative value on error
115 * with errno being set by recvmsg() syscall.
118 ssize_t
recv_request(struct lttcomm_sock
*sock
, void *buf
, size_t size
)
122 ret
= sock
->ops
->recvmsg(sock
, buf
, size
, 0);
123 if (ret
< 0 || ret
!= size
) {
125 /* Orderly shutdown. Not necessary to print an error. */
126 DBG("Socket %d did an orderly shutdown", sock
->fd
);
128 ERR("Relay failed to receive request.");
137 * Send a response buffer using a given socket, source allocated buffer of
140 * Return the size of the sent message or else a negative value on error with
141 * errno being set by sendmsg() syscall.
144 ssize_t
send_response(struct lttcomm_sock
*sock
, void *buf
, size_t size
)
148 ret
= sock
->ops
->sendmsg(sock
, buf
, size
, 0);
150 ERR("Relayd failed to send response.");
157 * Atomically check if new streams got added in one of the sessions attached
158 * and reset the flag to 0.
160 * Returns 1 if new streams got added, 0 if nothing changed, a negative value
164 int check_new_streams(struct relay_connection
*conn
)
166 struct relay_session
*session
;
167 unsigned long current_val
;
170 if (!conn
->viewer_session
) {
174 cds_list_for_each_entry_rcu(session
,
175 &conn
->viewer_session
->session_list
,
176 viewer_session_node
) {
177 if (!session_get(session
)) {
180 current_val
= uatomic_cmpxchg(&session
->new_streams
, 1, 0);
182 session_put(session
);
193 * Send viewer streams to the given socket. The ignore_sent_flag indicates if
194 * this function should ignore the sent flag or not.
196 * Return 0 on success or else a negative value.
199 ssize_t
send_viewer_streams(struct lttcomm_sock
*sock
,
200 struct relay_session
*session
, unsigned int ignore_sent_flag
)
203 struct lttng_viewer_stream send_stream
;
204 struct lttng_ht_iter iter
;
205 struct relay_viewer_stream
*vstream
;
209 cds_lfht_for_each_entry(viewer_streams_ht
->ht
, &iter
.iter
, vstream
,
211 struct ctf_trace
*ctf_trace
;
213 health_code_update();
215 if (!viewer_stream_get(vstream
)) {
219 pthread_mutex_lock(&vstream
->stream
->lock
);
220 /* Ignore if not the same session. */
221 if (vstream
->stream
->trace
->session
->id
!= session
->id
||
222 (!ignore_sent_flag
&& vstream
->sent_flag
)) {
223 pthread_mutex_unlock(&vstream
->stream
->lock
);
224 viewer_stream_put(vstream
);
228 ctf_trace
= vstream
->stream
->trace
;
229 send_stream
.id
= htobe64(vstream
->stream
->stream_handle
);
230 send_stream
.ctf_trace_id
= htobe64(ctf_trace
->id
);
231 send_stream
.metadata_flag
= htobe32(
232 vstream
->stream
->is_metadata
);
233 if (lttng_strncpy(send_stream
.path_name
, vstream
->path_name
,
234 sizeof(send_stream
.path_name
))) {
235 pthread_mutex_unlock(&vstream
->stream
->lock
);
236 viewer_stream_put(vstream
);
237 ret
= -1; /* Error. */
240 if (lttng_strncpy(send_stream
.channel_name
,
241 vstream
->channel_name
,
242 sizeof(send_stream
.channel_name
))) {
243 pthread_mutex_unlock(&vstream
->stream
->lock
);
244 viewer_stream_put(vstream
);
245 ret
= -1; /* Error. */
249 DBG("Sending stream %" PRIu64
" to viewer",
250 vstream
->stream
->stream_handle
);
251 vstream
->sent_flag
= 1;
252 pthread_mutex_unlock(&vstream
->stream
->lock
);
254 ret
= send_response(sock
, &send_stream
, sizeof(send_stream
));
255 viewer_stream_put(vstream
);
269 * Create every viewer stream possible for the given session with the seek
270 * type. Three counters *can* be return which are in order the total amount of
271 * viewer stream of the session, the number of unsent stream and the number of
272 * stream created. Those counters can be NULL and thus will be ignored.
274 * Return 0 on success or else a negative value.
277 int make_viewer_streams(struct relay_session
*session
,
278 enum lttng_viewer_seek seek_t
, uint32_t *nb_total
, uint32_t *nb_unsent
,
279 uint32_t *nb_created
, bool *closed
)
282 struct lttng_ht_iter iter
;
283 struct ctf_trace
*ctf_trace
;
288 * Hold the session lock to ensure that we see either none or
289 * all initial streams for a session, but no intermediate state.
291 pthread_mutex_lock(&session
->lock
);
293 if (session
->connection_closed
) {
298 * Create viewer streams for relay streams that are ready to be
299 * used for a the given session id only.
302 cds_lfht_for_each_entry(session
->ctf_traces_ht
->ht
, &iter
.iter
, ctf_trace
,
304 struct relay_stream
*stream
;
306 health_code_update();
308 if (!ctf_trace_get(ctf_trace
)) {
312 cds_list_for_each_entry_rcu(stream
, &ctf_trace
->stream_list
, stream_node
) {
313 struct relay_viewer_stream
*vstream
;
315 if (!stream_get(stream
)) {
319 * stream published is protected by the session lock.
321 if (!stream
->published
) {
324 vstream
= viewer_stream_get_by_id(stream
->stream_handle
);
326 vstream
= viewer_stream_create(stream
, seek_t
);
329 ctf_trace_put(ctf_trace
);
335 /* Update number of created stream counter. */
339 * Ensure a self-reference is preserved even
340 * after we have put our local reference.
342 if (!viewer_stream_get(vstream
)) {
343 ERR("Unable to get self-reference on viewer stream, logic error.");
347 if (!vstream
->sent_flag
&& nb_unsent
) {
348 /* Update number of unsent stream counter. */
352 /* Update number of total stream counter. */
354 if (stream
->is_metadata
) {
355 if (!stream
->closed
||
356 stream
->metadata_received
> vstream
->metadata_sent
) {
360 if (!stream
->closed
||
361 !(((int64_t) (stream
->prev_seq
- stream
->last_net_seq_num
)) >= 0)) {
367 /* Put local reference. */
368 viewer_stream_put(vstream
);
372 ctf_trace_put(ctf_trace
);
379 pthread_mutex_unlock(&session
->lock
);
383 int relayd_live_stop(void)
385 /* Stop dispatch thread */
386 CMM_STORE_SHARED(live_dispatch_thread_exit
, 1);
387 futex_nto1_wake(&viewer_conn_queue
.futex
);
392 * Create a poll set with O_CLOEXEC and add the thread quit pipe to the set.
395 int create_thread_poll_set(struct lttng_poll_event
*events
, int size
)
399 if (events
== NULL
|| size
== 0) {
404 ret
= lttng_poll_create(events
, size
, LTTNG_CLOEXEC
);
410 ret
= lttng_poll_add(events
, thread_quit_pipe
[0], LPOLLIN
| LPOLLERR
);
422 * Check if the thread quit pipe was triggered.
424 * Return 1 if it was triggered else 0;
427 int check_thread_quit_pipe(int fd
, uint32_t events
)
429 if (fd
== thread_quit_pipe
[0] && (events
& LPOLLIN
)) {
437 * Create and init socket from uri.
440 struct lttcomm_sock
*init_socket(struct lttng_uri
*uri
)
443 struct lttcomm_sock
*sock
= NULL
;
445 sock
= lttcomm_alloc_sock_from_uri(uri
);
447 ERR("Allocating socket");
451 ret
= lttcomm_create_sock(sock
);
455 DBG("Listening on sock %d for lttng-live", sock
->fd
);
457 ret
= sock
->ops
->bind(sock
);
459 PERROR("Failed to bind lttng-live socket");
463 ret
= sock
->ops
->listen(sock
, -1);
473 lttcomm_destroy_sock(sock
);
479 * This thread manages the listening for new connections on the network
482 void *thread_listener(void *data
)
484 int i
, ret
, pollfd
, err
= -1;
485 uint32_t revents
, nb_fd
;
486 struct lttng_poll_event events
;
487 struct lttcomm_sock
*live_control_sock
;
489 DBG("[thread] Relay live listener started");
491 health_register(health_relayd
, HEALTH_RELAYD_TYPE_LIVE_LISTENER
);
493 health_code_update();
495 live_control_sock
= init_socket(live_uri
);
496 if (!live_control_sock
) {
497 goto error_sock_control
;
500 /* Pass 2 as size here for the thread quit pipe and control sockets. */
501 ret
= create_thread_poll_set(&events
, 2);
503 goto error_create_poll
;
506 /* Add the control socket */
507 ret
= lttng_poll_add(&events
, live_control_sock
->fd
, LPOLLIN
| LPOLLRDHUP
);
512 lttng_relay_notify_ready();
514 if (testpoint(relayd_thread_live_listener
)) {
515 goto error_testpoint
;
519 health_code_update();
521 DBG("Listener accepting live viewers connections");
525 ret
= lttng_poll_wait(&events
, -1);
529 * Restart interrupted system call.
531 if (errno
== EINTR
) {
538 DBG("Relay new viewer connection received");
539 for (i
= 0; i
< nb_fd
; i
++) {
540 health_code_update();
542 /* Fetch once the poll data */
543 revents
= LTTNG_POLL_GETEV(&events
, i
);
544 pollfd
= LTTNG_POLL_GETFD(&events
, i
);
547 /* No activity for this FD (poll implementation). */
551 /* Thread quit pipe has been closed. Killing thread. */
552 ret
= check_thread_quit_pipe(pollfd
, revents
);
558 if (revents
& LPOLLIN
) {
560 * A new connection is requested, therefore a
561 * viewer connection is allocated in this
562 * thread, enqueued to a global queue and
563 * dequeued (and freed) in the worker thread.
566 struct relay_connection
*new_conn
;
567 struct lttcomm_sock
*newsock
;
569 newsock
= live_control_sock
->ops
->accept(live_control_sock
);
571 PERROR("accepting control sock");
574 DBG("Relay viewer connection accepted socket %d", newsock
->fd
);
576 ret
= setsockopt(newsock
->fd
, SOL_SOCKET
, SO_REUSEADDR
, &val
,
579 PERROR("setsockopt inet");
580 lttcomm_destroy_sock(newsock
);
583 new_conn
= connection_create(newsock
, RELAY_CONNECTION_UNKNOWN
);
585 lttcomm_destroy_sock(newsock
);
588 /* Ownership assumed by the connection. */
591 /* Enqueue request for the dispatcher thread. */
592 cds_wfcq_enqueue(&viewer_conn_queue
.head
, &viewer_conn_queue
.tail
,
596 * Wake the dispatch queue futex.
597 * Implicit memory barrier with the
598 * exchange in cds_wfcq_enqueue.
600 futex_nto1_wake(&viewer_conn_queue
.futex
);
601 } else if (revents
& (LPOLLERR
| LPOLLHUP
| LPOLLRDHUP
)) {
602 ERR("socket poll error");
605 ERR("Unexpected poll events %u for sock %d", revents
, pollfd
);
615 lttng_poll_clean(&events
);
617 if (live_control_sock
->fd
>= 0) {
618 ret
= live_control_sock
->ops
->close(live_control_sock
);
623 lttcomm_destroy_sock(live_control_sock
);
627 DBG("Live viewer listener thread exited with error");
629 health_unregister(health_relayd
);
630 DBG("Live viewer listener thread cleanup complete");
631 if (lttng_relay_stop_threads()) {
632 ERR("Error stopping threads");
638 * This thread manages the dispatching of the requests to worker threads
641 void *thread_dispatcher(void *data
)
645 struct cds_wfcq_node
*node
;
646 struct relay_connection
*conn
= NULL
;
648 DBG("[thread] Live viewer relay dispatcher started");
650 health_register(health_relayd
, HEALTH_RELAYD_TYPE_LIVE_DISPATCHER
);
652 if (testpoint(relayd_thread_live_dispatcher
)) {
653 goto error_testpoint
;
656 health_code_update();
659 health_code_update();
661 /* Atomically prepare the queue futex */
662 futex_nto1_prepare(&viewer_conn_queue
.futex
);
664 if (CMM_LOAD_SHARED(live_dispatch_thread_exit
)) {
669 health_code_update();
671 /* Dequeue commands */
672 node
= cds_wfcq_dequeue_blocking(&viewer_conn_queue
.head
,
673 &viewer_conn_queue
.tail
);
675 DBG("Woken up but nothing in the live-viewer "
676 "relay command queue");
677 /* Continue thread execution */
680 conn
= caa_container_of(node
, struct relay_connection
, qnode
);
681 DBG("Dispatching viewer request waiting on sock %d",
685 * Inform worker thread of the new request. This
686 * call is blocking so we can be assured that
687 * the data will be read at some point in time
688 * or wait to the end of the world :)
690 ret
= lttng_write(live_conn_pipe
[1], &conn
, sizeof(conn
));
692 PERROR("write conn pipe");
693 connection_put(conn
);
696 } while (node
!= NULL
);
698 /* Futex wait on queue. Blocking call on futex() */
700 futex_nto1_wait(&viewer_conn_queue
.futex
);
704 /* Normal exit, no error */
711 ERR("Health error occurred in %s", __func__
);
713 health_unregister(health_relayd
);
714 DBG("Live viewer dispatch thread dying");
715 if (lttng_relay_stop_threads()) {
716 ERR("Error stopping threads");
722 * Establish connection with the viewer and check the versions.
724 * Return 0 on success or else negative value.
727 int viewer_connect(struct relay_connection
*conn
)
730 struct lttng_viewer_connect reply
, msg
;
732 conn
->version_check_done
= 1;
734 health_code_update();
736 DBG("Viewer is establishing a connection to the relayd.");
738 ret
= recv_request(conn
->sock
, &msg
, sizeof(msg
));
743 health_code_update();
745 memset(&reply
, 0, sizeof(reply
));
746 reply
.major
= RELAYD_VERSION_COMM_MAJOR
;
747 reply
.minor
= RELAYD_VERSION_COMM_MINOR
;
749 /* Major versions must be the same */
750 if (reply
.major
!= be32toh(msg
.major
)) {
751 DBG("Incompatible major versions ([relayd] %u vs [client] %u)",
752 reply
.major
, be32toh(msg
.major
));
757 conn
->major
= reply
.major
;
758 /* We adapt to the lowest compatible version */
759 if (reply
.minor
<= be32toh(msg
.minor
)) {
760 conn
->minor
= reply
.minor
;
762 conn
->minor
= be32toh(msg
.minor
);
765 if (be32toh(msg
.type
) == LTTNG_VIEWER_CLIENT_COMMAND
) {
766 conn
->type
= RELAY_VIEWER_COMMAND
;
767 } else if (be32toh(msg
.type
) == LTTNG_VIEWER_CLIENT_NOTIFICATION
) {
768 conn
->type
= RELAY_VIEWER_NOTIFICATION
;
770 ERR("Unknown connection type : %u", be32toh(msg
.type
));
775 reply
.major
= htobe32(reply
.major
);
776 reply
.minor
= htobe32(reply
.minor
);
777 if (conn
->type
== RELAY_VIEWER_COMMAND
) {
779 * Increment outside of htobe64 macro, because the argument can
780 * be used more than once within the macro, and thus the
781 * operation may be undefined.
783 pthread_mutex_lock(&last_relay_viewer_session_id_lock
);
784 last_relay_viewer_session_id
++;
785 pthread_mutex_unlock(&last_relay_viewer_session_id_lock
);
786 reply
.viewer_session_id
= htobe64(last_relay_viewer_session_id
);
789 health_code_update();
791 ret
= send_response(conn
->sock
, &reply
, sizeof(reply
));
796 health_code_update();
798 DBG("Version check done using protocol %u.%u", conn
->major
, conn
->minor
);
806 * Send the viewer the list of current sessions.
807 * We need to create a copy of the hash table content because otherwise
808 * we cannot assume the number of entries stays the same between getting
809 * the number of HT elements and iteration over the HT.
811 * Return 0 on success or else a negative value.
814 int viewer_list_sessions(struct relay_connection
*conn
)
817 struct lttng_viewer_list_sessions session_list
;
818 struct lttng_ht_iter iter
;
819 struct relay_session
*session
;
820 struct lttng_viewer_session
*send_session_buf
= NULL
;
821 uint32_t buf_count
= SESSION_BUF_DEFAULT_COUNT
;
824 DBG("List sessions received");
826 send_session_buf
= zmalloc(SESSION_BUF_DEFAULT_COUNT
* sizeof(*send_session_buf
));
827 if (!send_session_buf
) {
832 cds_lfht_for_each_entry(sessions_ht
->ht
, &iter
.iter
, session
,
834 struct lttng_viewer_session
*send_session
;
836 health_code_update();
838 if (session
->connection_closed
) {
839 /* Skip closed session */
843 if (count
>= buf_count
) {
844 struct lttng_viewer_session
*newbuf
;
845 uint32_t new_buf_count
= buf_count
<< 1;
847 newbuf
= realloc(send_session_buf
,
848 new_buf_count
* sizeof(*send_session_buf
));
853 send_session_buf
= newbuf
;
854 buf_count
= new_buf_count
;
856 send_session
= &send_session_buf
[count
];
857 if (lttng_strncpy(send_session
->session_name
,
858 session
->session_name
,
859 sizeof(send_session
->session_name
))) {
863 if (lttng_strncpy(send_session
->hostname
, session
->hostname
,
864 sizeof(send_session
->hostname
))) {
868 send_session
->id
= htobe64(session
->id
);
869 send_session
->live_timer
= htobe32(session
->live_timer
);
870 if (session
->viewer_attached
) {
871 send_session
->clients
= htobe32(1);
873 send_session
->clients
= htobe32(0);
875 send_session
->streams
= htobe32(session
->stream_count
);
883 session_list
.sessions_count
= htobe32(count
);
885 health_code_update();
887 ret
= send_response(conn
->sock
, &session_list
, sizeof(session_list
));
892 health_code_update();
894 ret
= send_response(conn
->sock
, send_session_buf
,
895 count
* sizeof(*send_session_buf
));
899 health_code_update();
903 free(send_session_buf
);
908 * Send the viewer the list of current streams.
911 int viewer_get_new_streams(struct relay_connection
*conn
)
913 int ret
, send_streams
= 0;
914 uint32_t nb_created
= 0, nb_unsent
= 0, nb_streams
= 0, nb_total
= 0;
915 struct lttng_viewer_new_streams_request request
;
916 struct lttng_viewer_new_streams_response response
;
917 struct relay_session
*session
;
923 DBG("Get new streams received");
925 health_code_update();
927 /* Receive the request from the connected client. */
928 ret
= recv_request(conn
->sock
, &request
, sizeof(request
));
932 session_id
= be64toh(request
.session_id
);
934 health_code_update();
936 memset(&response
, 0, sizeof(response
));
938 session
= session_get_by_id(session_id
);
940 DBG("Relay session %" PRIu64
" not found", session_id
);
941 response
.status
= htobe32(LTTNG_VIEWER_NEW_STREAMS_ERR
);
945 if (!viewer_session_is_attached(conn
->viewer_session
, session
)) {
947 response
.status
= htobe32(LTTNG_VIEWER_NEW_STREAMS_ERR
);
952 response
.status
= htobe32(LTTNG_VIEWER_NEW_STREAMS_OK
);
954 ret
= make_viewer_streams(session
, LTTNG_VIEWER_SEEK_LAST
, &nb_total
, &nb_unsent
,
955 &nb_created
, &closed
);
957 goto end_put_session
;
959 /* Only send back the newly created streams with the unsent ones. */
960 nb_streams
= nb_created
+ nb_unsent
;
961 response
.streams_count
= htobe32(nb_streams
);
964 * If the session is closed, HUP when there are no more streams
967 if (closed
&& nb_total
== 0) {
969 response
.streams_count
= 0;
970 response
.status
= htobe32(LTTNG_VIEWER_NEW_STREAMS_HUP
);
975 health_code_update();
976 ret
= send_response(conn
->sock
, &response
, sizeof(response
));
978 goto end_put_session
;
980 health_code_update();
983 * Unknown or empty session, just return gracefully, the viewer
984 * knows what is happening.
986 if (!send_streams
|| !nb_streams
) {
988 goto end_put_session
;
992 * Send stream and *DON'T* ignore the sent flag so every viewer
993 * streams that were not sent from that point will be sent to
996 ret
= send_viewer_streams(conn
->sock
, session
, 0);
998 goto end_put_session
;
1003 session_put(session
);
1010 * Send the viewer the list of current sessions.
1013 int viewer_attach_session(struct relay_connection
*conn
)
1015 int send_streams
= 0;
1017 uint32_t nb_streams
= 0;
1018 enum lttng_viewer_seek seek_type
;
1019 struct lttng_viewer_attach_session_request request
;
1020 struct lttng_viewer_attach_session_response response
;
1021 struct relay_session
*session
= NULL
;
1022 bool closed
= false;
1026 health_code_update();
1028 /* Receive the request from the connected client. */
1029 ret
= recv_request(conn
->sock
, &request
, sizeof(request
));
1034 health_code_update();
1036 memset(&response
, 0, sizeof(response
));
1038 if (!conn
->viewer_session
) {
1039 DBG("Client trying to attach before creating a live viewer session");
1040 response
.status
= htobe32(LTTNG_VIEWER_ATTACH_NO_SESSION
);
1044 session
= session_get_by_id(be64toh(request
.session_id
));
1046 DBG("Relay session %" PRIu64
" not found",
1047 (uint64_t) be64toh(request
.session_id
));
1048 response
.status
= htobe32(LTTNG_VIEWER_ATTACH_UNK
);
1051 DBG("Attach session ID %" PRIu64
" received",
1052 (uint64_t) be64toh(request
.session_id
));
1054 if (session
->live_timer
== 0) {
1055 DBG("Not live session");
1056 response
.status
= htobe32(LTTNG_VIEWER_ATTACH_NOT_LIVE
);
1061 ret
= viewer_session_attach(conn
->viewer_session
, session
);
1063 DBG("Already a viewer attached");
1064 response
.status
= htobe32(LTTNG_VIEWER_ATTACH_ALREADY
);
1068 switch (be32toh(request
.seek
)) {
1069 case LTTNG_VIEWER_SEEK_BEGINNING
:
1070 case LTTNG_VIEWER_SEEK_LAST
:
1071 response
.status
= htobe32(LTTNG_VIEWER_ATTACH_OK
);
1072 seek_type
= be32toh(request
.seek
);
1075 ERR("Wrong seek parameter");
1076 response
.status
= htobe32(LTTNG_VIEWER_ATTACH_SEEK_ERR
);
1081 ret
= make_viewer_streams(session
, seek_type
, &nb_streams
, NULL
,
1084 goto end_put_session
;
1086 response
.streams_count
= htobe32(nb_streams
);
1089 * If the session is closed when the viewer is attaching, it
1090 * means some of the streams may have been concurrently removed,
1091 * so we don't allow the viewer to attach, even if there are
1092 * streams available.
1096 response
.streams_count
= 0;
1097 response
.status
= htobe32(LTTNG_VIEWER_ATTACH_UNK
);
1101 if (testpoint(relayd_viewer_session_attach
)) {
1102 ERR("Viewer session attach testpoint failed");
1104 response
.streams_count
= 0;
1105 response
.status
= htobe32(LTTNG_VIEWER_ATTACH_UNK
);
1111 health_code_update();
1112 ret
= send_response(conn
->sock
, &response
, sizeof(response
));
1114 goto end_put_session
;
1116 health_code_update();
1119 * Unknown or empty session, just return gracefully, the viewer
1120 * knows what is happening.
1122 if (!send_streams
|| !nb_streams
) {
1124 goto end_put_session
;
1127 /* Send stream and ignore the sent flag. */
1128 ret
= send_viewer_streams(conn
->sock
, session
, 1);
1130 goto end_put_session
;
1135 session_put(session
);
1142 * Open the index file if needed for the given vstream.
1144 * If an index file is successfully opened, the vstream will set it as its
1145 * current index file.
1147 * Return 0 on success, a negative value on error (-ENOENT if not ready yet).
1149 * Called with rstream lock held.
1151 static int try_open_index(struct relay_viewer_stream
*vstream
,
1152 struct relay_stream
*rstream
)
1156 if (vstream
->index_file
) {
1161 * Deal with streams that have not received any index so far (or after clear).
1163 if (rstream
->index_received_seqcount
<= rstream
->clear_position_index_seqcount
) {
1167 vstream
->index_file
= lttng_index_file_open(vstream
->path_name
,
1168 vstream
->channel_name
,
1169 vstream
->stream
->tracefile_count
,
1170 vstream
->current_tracefile_id
);
1171 if (!vstream
->index_file
) {
1180 * Check the status of the index for the given stream. This function
1181 * updates the index structure if needed and can put (close) the vstream
1182 * in the HUP situation.
1184 * Return 0 means that we can proceed with the index. A value of 1 means
1185 * that the index has been updated and is ready to be sent to the
1186 * client. A negative value indicates an error that can't be handled.
1188 * Called with rstream lock held.
1190 static int check_index_status(struct relay_viewer_stream
*vstream
,
1191 struct relay_stream
*rstream
, struct ctf_trace
*trace
,
1192 struct lttng_viewer_index
*index
)
1196 DBG("check status: recv %" PRIu64
" sent %" PRIu64
" clear index %" PRIu64
" clear data %" PRIu64
" for stream %" PRIu64
,
1197 rstream
->index_received_seqcount
,
1198 vstream
->index_sent_seqcount
,
1199 rstream
->clear_position_index_seqcount
,
1200 rstream
->clear_position_data_seqcount
, vstream
->stream
->stream_handle
);
1201 if ((trace
->session
->connection_closed
|| rstream
->closed
)
1202 && rstream
->index_received_seqcount
1203 == vstream
->index_sent_seqcount
) {
1205 * Last index sent and session connection or relay
1206 * stream are closed.
1208 index
->status
= htobe32(LTTNG_VIEWER_INDEX_HUP
);
1210 } else if (rstream
->beacon_ts_end
!= -1ULL &&
1211 rstream
->index_received_seqcount
1212 <= vstream
->index_sent_seqcount
) {
1214 * We've received a synchronization beacon and the last index
1215 * available has been sent, the index for now is inactive.
1217 * In this case, we have received a beacon which allows us to
1218 * inform the client of a time interval during which we can
1219 * guarantee that there are no events to read (and never will
1222 * The sent seqcount can grow higher than receive seqcount on clear.
1224 index
->status
= htobe32(LTTNG_VIEWER_INDEX_INACTIVE
);
1225 index
->timestamp_end
= htobe64(rstream
->beacon_ts_end
);
1226 index
->stream_id
= htobe64(rstream
->ctf_stream_id
);
1227 DBG("INACTIVE: with beacon, r v recv eq for stream %" PRIu64
, vstream
->stream
->stream_handle
);
1229 } else if (rstream
->index_received_seqcount
1230 <= vstream
->index_sent_seqcount
) {
1232 * This checks whether received <= sent seqcount. In
1233 * this case, we have not received a beacon. Therefore,
1234 * we can only ask the client to retry later.
1236 * The sent seqcount can grow higher than receive seqcount on clear.
1238 index
->status
= htobe32(LTTNG_VIEWER_INDEX_RETRY
);
1239 DBG("RETRY: r v recv leq for stream %" PRIu64
, vstream
->stream
->stream_handle
);
1241 } else if (rstream
->index_received_seqcount
<= rstream
->clear_position_index_seqcount
||
1242 rstream
->index_received_seqcount
<= rstream
->clear_position_data_seqcount
) {
1244 * Send "retry" reply if a clear operation is in progress.
1246 index
->status
= htobe32(LTTNG_VIEWER_INDEX_RETRY
);
1247 DBG("RETRY: r <= clear pos for stream %" PRIu64
, vstream
->stream
->stream_handle
);
1249 } else if (!tracefile_array_seq_in_file(rstream
->tfa
,
1250 vstream
->current_tracefile_id
,
1251 vstream
->index_sent_seqcount
)) {
1253 * The next index we want to send cannot be read either
1254 * because we need to perform a rotation, or due to
1255 * the producer having overwritten its trace file.
1257 DBG("Viewer stream %" PRIu64
" rotation",
1258 vstream
->stream
->stream_handle
);
1259 ret
= viewer_stream_rotate(vstream
);
1262 } else if (ret
== 1) {
1263 /* EOF across entire stream. */
1264 index
->status
= htobe32(LTTNG_VIEWER_INDEX_HUP
);
1268 * If we have been pushed due to overwrite, it
1269 * necessarily means there is data that can be read in
1270 * the stream. If we rotated because we reached the end
1271 * of a tracefile, it means the following tracefile
1272 * needs to contain at least one index, else we would
1273 * have already returned LTTNG_VIEWER_INDEX_RETRY to the
1274 * viewer. The updated index_sent_seqcount needs to
1275 * point to a readable index entry now.
1277 * In the case where we "rotate" on a single file, we
1278 * can end up in a case where the requested index is
1279 * still unavailable.
1281 if (rstream
->tracefile_count
== 1 &&
1282 !tracefile_array_seq_in_file(
1284 vstream
->current_tracefile_id
,
1285 vstream
->index_sent_seqcount
)) {
1286 index
->status
= htobe32(LTTNG_VIEWER_INDEX_RETRY
);
1287 DBG("RETRY: !tfs seq in file for stream %" PRIu64
, vstream
->stream
->stream_handle
);
1290 assert(tracefile_array_seq_in_file(rstream
->tfa
,
1291 vstream
->current_tracefile_id
,
1292 vstream
->index_sent_seqcount
));
1294 /* ret == 0 means successful so we continue. */
1300 viewer_stream_put(vstream
);
1306 * Send the next index for a stream.
1308 * Return 0 on success or else a negative value.
1311 int viewer_get_next_index(struct relay_connection
*conn
)
1314 struct lttng_viewer_get_next_index request_index
;
1315 struct lttng_viewer_index viewer_index
;
1316 struct ctf_packet_index packet_index
;
1317 struct relay_viewer_stream
*vstream
= NULL
;
1318 struct relay_stream
*rstream
= NULL
;
1319 struct ctf_trace
*ctf_trace
= NULL
;
1320 struct relay_viewer_stream
*metadata_viewer_stream
= NULL
;
1324 DBG("Viewer get next index");
1326 memset(&viewer_index
, 0, sizeof(viewer_index
));
1327 health_code_update();
1329 ret
= recv_request(conn
->sock
, &request_index
, sizeof(request_index
));
1333 health_code_update();
1335 vstream
= viewer_stream_get_by_id(be64toh(request_index
.stream_id
));
1337 DBG("Client requested index of unknown stream id %" PRIu64
,
1338 (uint64_t) be64toh(request_index
.stream_id
));
1339 viewer_index
.status
= htobe32(LTTNG_VIEWER_INDEX_ERR
);
1343 /* Use back. ref. Protected by refcounts. */
1344 rstream
= vstream
->stream
;
1345 ctf_trace
= rstream
->trace
;
1347 /* metadata_viewer_stream may be NULL. */
1348 metadata_viewer_stream
=
1349 ctf_trace_get_viewer_metadata_stream(ctf_trace
);
1351 pthread_mutex_lock(&rstream
->lock
);
1354 * The viewer should not ask for index on metadata stream.
1356 if (rstream
->is_metadata
) {
1357 viewer_index
.status
= htobe32(LTTNG_VIEWER_INDEX_HUP
);
1362 * In case the stream has been cleared, we need to push the viewer
1363 * stream index sent seqcount forward. Note that this can temporarily
1364 * bring the index_sent position beyond the index received position.
1366 if (rstream
->clear_position_index_seqcount
>= vstream
->index_sent_seqcount
) {
1367 vstream
->index_sent_seqcount
= rstream
->clear_position_index_seqcount
;
1369 * Close the currently open index and data files to ensure we
1370 * sync up with the receive side.
1372 if (vstream
->index_file
) {
1373 lttng_index_file_put(vstream
->index_file
);
1374 vstream
->index_file
= NULL
;
1376 if (vstream
->stream_fd
) {
1377 stream_fd_put(vstream
->stream_fd
);
1378 vstream
->stream_fd
= NULL
;
1381 * In tracefile rotation, we reset the current tracefile to 0.
1383 vstream
->current_tracefile_id
= 0;
1386 /* Try to open an index if one is needed for that stream. */
1387 ret
= try_open_index(vstream
, rstream
);
1389 if (ret
== -ENOENT
) {
1391 * The index is created only when the first data
1392 * packet arrives, it might not be ready at the
1393 * beginning of the session. Let check_index_status
1394 * deal with inactivity beacons.
1398 /* Unhandled error. */
1399 viewer_index
.status
= htobe32(LTTNG_VIEWER_INDEX_ERR
);
1405 ret
= check_index_status(vstream
, rstream
, ctf_trace
, &viewer_index
);
1408 } else if (ret
== 1) {
1410 * We have no index to send and check_index_status has populated
1411 * viewer_index's status.
1415 /* At this point, ret is 0 thus we will be able to read the index. */
1419 * vstream->stream_fd may be NULL if it has been closed by
1420 * tracefile rotation, or if we are at the beginning of the
1421 * stream. We open the data stream file here to protect against
1422 * overwrite caused by tracefile rotation (in association with
1423 * unlink performed before overwrite).
1425 if (!vstream
->stream_fd
) {
1426 char fullpath
[PATH_MAX
];
1428 if (vstream
->stream
->tracefile_count
> 0) {
1429 ret
= snprintf(fullpath
, PATH_MAX
, "%s/%s_%" PRIu64
,
1431 vstream
->channel_name
,
1432 vstream
->current_tracefile_id
);
1434 ret
= snprintf(fullpath
, PATH_MAX
, "%s/%s",
1436 vstream
->channel_name
);
1441 ret
= open(fullpath
, O_RDONLY
);
1443 PERROR("Relay opening trace file");
1446 vstream
->stream_fd
= stream_fd_create(ret
);
1447 if (!vstream
->stream_fd
) {
1455 ret
= check_new_streams(conn
);
1457 viewer_index
.status
= htobe32(LTTNG_VIEWER_INDEX_ERR
);
1459 } else if (ret
== 1) {
1460 viewer_index
.flags
|= LTTNG_VIEWER_FLAG_NEW_STREAM
;
1463 ret
= lttng_index_file_read(vstream
->index_file
, &packet_index
);
1465 ERR("Relay error reading index file %d",
1466 vstream
->index_file
->fd
);
1467 viewer_index
.status
= htobe32(LTTNG_VIEWER_INDEX_ERR
);
1470 viewer_index
.status
= htobe32(LTTNG_VIEWER_INDEX_OK
);
1471 vstream
->index_sent_seqcount
++;
1475 * Indexes are stored in big endian, no need to switch before sending.
1477 DBG("Sending viewer index for stream %" PRIu64
" offset %" PRIu64
,
1478 rstream
->stream_handle
,
1479 (uint64_t) be64toh(packet_index
.offset
));
1480 viewer_index
.offset
= packet_index
.offset
;
1481 viewer_index
.packet_size
= packet_index
.packet_size
;
1482 viewer_index
.content_size
= packet_index
.content_size
;
1483 viewer_index
.timestamp_begin
= packet_index
.timestamp_begin
;
1484 viewer_index
.timestamp_end
= packet_index
.timestamp_end
;
1485 viewer_index
.events_discarded
= packet_index
.events_discarded
;
1486 viewer_index
.stream_id
= packet_index
.stream_id
;
1490 pthread_mutex_unlock(&rstream
->lock
);
1493 if (metadata_viewer_stream
) {
1494 pthread_mutex_lock(&metadata_viewer_stream
->stream
->lock
);
1495 DBG("get next index metadata check: recv %" PRIu64
1497 metadata_viewer_stream
->stream
->metadata_received
,
1498 metadata_viewer_stream
->metadata_sent
);
1499 if (!metadata_viewer_stream
->stream
->metadata_received
||
1500 metadata_viewer_stream
->stream
->metadata_received
>
1501 metadata_viewer_stream
->metadata_sent
) {
1502 viewer_index
.flags
|= LTTNG_VIEWER_FLAG_NEW_METADATA
;
1504 pthread_mutex_unlock(&metadata_viewer_stream
->stream
->lock
);
1507 viewer_index
.flags
= htobe32(viewer_index
.flags
);
1508 health_code_update();
1510 ret
= send_response(conn
->sock
, &viewer_index
, sizeof(viewer_index
));
1514 health_code_update();
1517 DBG("Index %" PRIu64
" for stream %" PRIu64
" sent",
1518 vstream
->index_sent_seqcount
,
1519 vstream
->stream
->stream_handle
);
1522 if (metadata_viewer_stream
) {
1523 viewer_stream_put(metadata_viewer_stream
);
1526 viewer_stream_put(vstream
);
1531 pthread_mutex_unlock(&rstream
->lock
);
1532 if (metadata_viewer_stream
) {
1533 viewer_stream_put(metadata_viewer_stream
);
1535 viewer_stream_put(vstream
);
1540 * Send the next index for a stream
1542 * Return 0 on success or else a negative value.
1545 int viewer_get_packet(struct relay_connection
*conn
)
1550 struct lttng_viewer_get_packet get_packet_info
;
1551 struct lttng_viewer_trace_packet reply_header
;
1552 struct relay_viewer_stream
*vstream
= NULL
;
1553 uint32_t reply_size
= sizeof(reply_header
);
1554 uint32_t packet_data_len
= 0;
1557 DBG2("Relay get data packet");
1559 health_code_update();
1561 ret
= recv_request(conn
->sock
, &get_packet_info
,
1562 sizeof(get_packet_info
));
1566 health_code_update();
1568 /* From this point on, the error label can be reached. */
1569 memset(&reply_header
, 0, sizeof(reply_header
));
1571 vstream
= viewer_stream_get_by_id(be64toh(get_packet_info
.stream_id
));
1573 DBG("Client requested packet of unknown stream id %" PRIu64
,
1574 (uint64_t) be64toh(get_packet_info
.stream_id
));
1575 reply_header
.status
= htobe32(LTTNG_VIEWER_GET_PACKET_ERR
);
1576 goto send_reply_nolock
;
1578 packet_data_len
= be32toh(get_packet_info
.len
);
1579 reply_size
+= packet_data_len
;
1582 reply
= zmalloc(reply_size
);
1584 PERROR("packet reply zmalloc");
1585 reply_size
= sizeof(reply_header
);
1589 pthread_mutex_lock(&vstream
->stream
->lock
);
1590 lseek_ret
= lseek(vstream
->stream_fd
->fd
, be64toh(get_packet_info
.offset
),
1592 if (lseek_ret
< 0) {
1593 PERROR("lseek fd %d to offset %" PRIu64
, vstream
->stream_fd
->fd
,
1594 (uint64_t) be64toh(get_packet_info
.offset
));
1597 read_len
= lttng_read(vstream
->stream_fd
->fd
,
1598 reply
+ sizeof(reply_header
),
1600 if (read_len
< packet_data_len
) {
1601 PERROR("Relay reading trace file, fd: %d, offset: %" PRIu64
,
1602 vstream
->stream_fd
->fd
,
1603 (uint64_t) be64toh(get_packet_info
.offset
));
1606 reply_header
.status
= htobe32(LTTNG_VIEWER_GET_PACKET_OK
);
1607 reply_header
.len
= htobe32(packet_data_len
);
1611 reply_header
.status
= htobe32(LTTNG_VIEWER_GET_PACKET_ERR
);
1615 pthread_mutex_unlock(&vstream
->stream
->lock
);
1619 health_code_update();
1622 memcpy(reply
, &reply_header
, sizeof(reply_header
));
1623 ret
= send_response(conn
->sock
, reply
, reply_size
);
1625 /* No reply to send. */
1626 ret
= send_response(conn
->sock
, &reply_header
,
1630 health_code_update();
1632 PERROR("sendmsg of packet data failed");
1636 DBG("Sent %u bytes for stream %" PRIu64
, reply_size
,
1637 (uint64_t) be64toh(get_packet_info
.stream_id
));
1643 viewer_stream_put(vstream
);
1649 * Send the session's metadata
1651 * Return 0 on success else a negative value.
1654 int viewer_get_metadata(struct relay_connection
*conn
)
1660 struct lttng_viewer_get_metadata request
;
1661 struct lttng_viewer_metadata_packet reply
;
1662 struct relay_viewer_stream
*vstream
= NULL
;
1666 DBG("Relay get metadata");
1668 health_code_update();
1670 ret
= recv_request(conn
->sock
, &request
, sizeof(request
));
1674 health_code_update();
1676 memset(&reply
, 0, sizeof(reply
));
1678 vstream
= viewer_stream_get_by_id(be64toh(request
.stream_id
));
1681 * The metadata stream can be closed by a CLOSE command
1682 * just before we attach. It can also be closed by
1683 * per-pid tracing during tracing. Therefore, it is
1684 * possible that we cannot find this viewer stream.
1685 * Reply back to the client with an error if we cannot
1688 DBG("Client requested metadata of unknown stream id %" PRIu64
,
1689 (uint64_t) be64toh(request
.stream_id
));
1690 reply
.status
= htobe32(LTTNG_VIEWER_METADATA_ERR
);
1693 pthread_mutex_lock(&vstream
->stream
->lock
);
1694 if (!vstream
->stream
->is_metadata
) {
1695 ERR("Invalid metadata stream");
1699 assert(vstream
->metadata_sent
<= vstream
->stream
->metadata_received
);
1701 len
= vstream
->stream
->metadata_received
- vstream
->metadata_sent
;
1703 reply
.status
= htobe32(LTTNG_VIEWER_NO_NEW_METADATA
);
1707 /* first time, we open the metadata file */
1708 if (!vstream
->stream_fd
) {
1709 char fullpath
[PATH_MAX
];
1711 ret
= snprintf(fullpath
, PATH_MAX
, "%s/%s", vstream
->path_name
,
1712 vstream
->channel_name
);
1716 ret
= open(fullpath
, O_RDONLY
);
1718 PERROR("Relay opening metadata file");
1721 vstream
->stream_fd
= stream_fd_create(ret
);
1722 if (!vstream
->stream_fd
) {
1730 reply
.len
= htobe64(len
);
1731 data
= zmalloc(len
);
1733 PERROR("viewer metadata zmalloc");
1737 read_len
= lttng_read(vstream
->stream_fd
->fd
, data
, len
);
1738 if (read_len
< len
) {
1739 PERROR("Relay reading metadata file");
1742 vstream
->metadata_sent
+= read_len
;
1743 if (vstream
->metadata_sent
== vstream
->stream
->metadata_received
1744 && vstream
->stream
->closed
) {
1745 /* Release ownership for the viewer metadata stream. */
1746 viewer_stream_put(vstream
);
1749 reply
.status
= htobe32(LTTNG_VIEWER_METADATA_OK
);
1754 reply
.status
= htobe32(LTTNG_VIEWER_METADATA_ERR
);
1757 health_code_update();
1759 pthread_mutex_unlock(&vstream
->stream
->lock
);
1761 ret
= send_response(conn
->sock
, &reply
, sizeof(reply
));
1765 health_code_update();
1768 ret
= send_response(conn
->sock
, data
, len
);
1774 DBG("Sent %" PRIu64
" bytes of metadata for stream %" PRIu64
, len
,
1775 (uint64_t) be64toh(request
.stream_id
));
1777 DBG("Metadata sent");
1783 viewer_stream_put(vstream
);
1789 * Create a viewer session.
1791 * Return 0 on success or else a negative value.
1794 int viewer_create_session(struct relay_connection
*conn
)
1797 struct lttng_viewer_create_session_response resp
;
1799 DBG("Viewer create session received");
1801 memset(&resp
, 0, sizeof(resp
));
1802 resp
.status
= htobe32(LTTNG_VIEWER_CREATE_SESSION_OK
);
1803 conn
->viewer_session
= viewer_session_create();
1804 if (!conn
->viewer_session
) {
1805 ERR("Allocation viewer session");
1806 resp
.status
= htobe32(LTTNG_VIEWER_CREATE_SESSION_ERR
);
1811 health_code_update();
1812 ret
= send_response(conn
->sock
, &resp
, sizeof(resp
));
1816 health_code_update();
1824 * Detach a viewer session.
1826 * Return 0 on success or else a negative value.
1829 int viewer_detach_session(struct relay_connection
*conn
)
1832 struct lttng_viewer_detach_session_response response
;
1833 struct lttng_viewer_detach_session_request request
;
1834 struct relay_session
*session
= NULL
;
1835 uint64_t viewer_session_to_close
;
1837 DBG("Viewer detach session received");
1841 health_code_update();
1843 /* Receive the request from the connected client. */
1844 ret
= recv_request(conn
->sock
, &request
, sizeof(request
));
1848 viewer_session_to_close
= be64toh(request
.session_id
);
1850 if (!conn
->viewer_session
) {
1851 DBG("Client trying to detach before creating a live viewer session");
1852 response
.status
= htobe32(LTTNG_VIEWER_DETACH_SESSION_ERR
);
1856 health_code_update();
1858 memset(&response
, 0, sizeof(response
));
1859 DBG("Detaching from session ID %" PRIu64
, viewer_session_to_close
);
1861 session
= session_get_by_id(be64toh(request
.session_id
));
1863 DBG("Relay session %" PRIu64
" not found",
1864 (uint64_t) be64toh(request
.session_id
));
1865 response
.status
= htobe32(LTTNG_VIEWER_DETACH_SESSION_UNK
);
1869 ret
= viewer_session_is_attached(conn
->viewer_session
, session
);
1871 DBG("Not attached to this session");
1872 response
.status
= htobe32(LTTNG_VIEWER_DETACH_SESSION_ERR
);
1873 goto send_reply_put
;
1876 viewer_session_close_one_session(conn
->viewer_session
, session
);
1877 response
.status
= htobe32(LTTNG_VIEWER_DETACH_SESSION_OK
);
1878 DBG("Session %" PRIu64
" detached.", viewer_session_to_close
);
1881 session_put(session
);
1884 health_code_update();
1885 ret
= send_response(conn
->sock
, &response
, sizeof(response
));
1889 health_code_update();
1897 * live_relay_unknown_command: send -1 if received unknown command
1900 void live_relay_unknown_command(struct relay_connection
*conn
)
1902 struct lttcomm_relayd_generic_reply reply
;
1904 memset(&reply
, 0, sizeof(reply
));
1905 reply
.ret_code
= htobe32(LTTNG_ERR_UNK
);
1906 (void) send_response(conn
->sock
, &reply
, sizeof(reply
));
1910 * Process the commands received on the control socket
1913 int process_control(struct lttng_viewer_cmd
*recv_hdr
,
1914 struct relay_connection
*conn
)
1919 msg_value
= be32toh(recv_hdr
->cmd
);
1922 * Make sure we've done the version check before any command other then a
1923 * new client connection.
1925 if (msg_value
!= LTTNG_VIEWER_CONNECT
&& !conn
->version_check_done
) {
1926 ERR("Viewer conn value %" PRIu32
" before version check", msg_value
);
1931 switch (msg_value
) {
1932 case LTTNG_VIEWER_CONNECT
:
1933 ret
= viewer_connect(conn
);
1935 case LTTNG_VIEWER_LIST_SESSIONS
:
1936 ret
= viewer_list_sessions(conn
);
1938 case LTTNG_VIEWER_ATTACH_SESSION
:
1939 ret
= viewer_attach_session(conn
);
1941 case LTTNG_VIEWER_GET_NEXT_INDEX
:
1942 ret
= viewer_get_next_index(conn
);
1944 case LTTNG_VIEWER_GET_PACKET
:
1945 ret
= viewer_get_packet(conn
);
1947 case LTTNG_VIEWER_GET_METADATA
:
1948 ret
= viewer_get_metadata(conn
);
1950 case LTTNG_VIEWER_GET_NEW_STREAMS
:
1951 ret
= viewer_get_new_streams(conn
);
1953 case LTTNG_VIEWER_CREATE_SESSION
:
1954 ret
= viewer_create_session(conn
);
1956 case LTTNG_VIEWER_DETACH_SESSION
:
1957 ret
= viewer_detach_session(conn
);
1960 ERR("Received unknown viewer command (%u)",
1961 be32toh(recv_hdr
->cmd
));
1962 live_relay_unknown_command(conn
);
1972 void cleanup_connection_pollfd(struct lttng_poll_event
*events
, int pollfd
)
1976 (void) lttng_poll_del(events
, pollfd
);
1978 ret
= close(pollfd
);
1980 ERR("Closing pollfd %d", pollfd
);
1985 * This thread does the actual work
1988 void *thread_worker(void *data
)
1992 struct lttng_poll_event events
;
1993 struct lttng_ht
*viewer_connections_ht
;
1994 struct lttng_ht_iter iter
;
1995 struct lttng_viewer_cmd recv_hdr
;
1996 struct relay_connection
*destroy_conn
;
1998 DBG("[thread] Live viewer relay worker started");
2000 rcu_register_thread();
2002 health_register(health_relayd
, HEALTH_RELAYD_TYPE_LIVE_WORKER
);
2004 if (testpoint(relayd_thread_live_worker
)) {
2005 goto error_testpoint
;
2008 /* table of connections indexed on socket */
2009 viewer_connections_ht
= lttng_ht_new(0, LTTNG_HT_TYPE_ULONG
);
2010 if (!viewer_connections_ht
) {
2011 goto viewer_connections_ht_error
;
2014 ret
= create_thread_poll_set(&events
, 2);
2016 goto error_poll_create
;
2019 ret
= lttng_poll_add(&events
, live_conn_pipe
[0], LPOLLIN
| LPOLLRDHUP
);
2028 health_code_update();
2030 /* Infinite blocking call, waiting for transmission */
2031 DBG3("Relayd live viewer worker thread polling...");
2032 health_poll_entry();
2033 ret
= lttng_poll_wait(&events
, -1);
2037 * Restart interrupted system call.
2039 if (errno
== EINTR
) {
2048 * Process control. The control connection is prioritised so we don't
2049 * starve it with high throughput tracing data on the data
2052 for (i
= 0; i
< nb_fd
; i
++) {
2053 /* Fetch once the poll data */
2054 uint32_t revents
= LTTNG_POLL_GETEV(&events
, i
);
2055 int pollfd
= LTTNG_POLL_GETFD(&events
, i
);
2057 health_code_update();
2060 /* No activity for this FD (poll implementation). */
2064 /* Thread quit pipe has been closed. Killing thread. */
2065 ret
= check_thread_quit_pipe(pollfd
, revents
);
2071 /* Inspect the relay conn pipe for new connection. */
2072 if (pollfd
== live_conn_pipe
[0]) {
2073 if (revents
& LPOLLIN
) {
2074 struct relay_connection
*conn
;
2076 ret
= lttng_read(live_conn_pipe
[0],
2077 &conn
, sizeof(conn
));
2081 lttng_poll_add(&events
, conn
->sock
->fd
,
2082 LPOLLIN
| LPOLLRDHUP
);
2083 connection_ht_add(viewer_connections_ht
, conn
);
2084 DBG("Connection socket %d added to poll", conn
->sock
->fd
);
2085 } else if (revents
& (LPOLLERR
| LPOLLHUP
| LPOLLRDHUP
)) {
2086 ERR("Relay live pipe error");
2089 ERR("Unexpected poll events %u for sock %d", revents
, pollfd
);
2093 /* Connection activity. */
2094 struct relay_connection
*conn
;
2096 conn
= connection_get_by_sock(viewer_connections_ht
, pollfd
);
2101 if (revents
& LPOLLIN
) {
2102 ret
= conn
->sock
->ops
->recvmsg(conn
->sock
, &recv_hdr
,
2103 sizeof(recv_hdr
), 0);
2105 /* Connection closed. */
2106 cleanup_connection_pollfd(&events
, pollfd
);
2107 /* Put "create" ownership reference. */
2108 connection_put(conn
);
2109 DBG("Viewer control conn closed with %d", pollfd
);
2111 ret
= process_control(&recv_hdr
, conn
);
2113 /* Clear the session on error. */
2114 cleanup_connection_pollfd(&events
, pollfd
);
2115 /* Put "create" ownership reference. */
2116 connection_put(conn
);
2117 DBG("Viewer connection closed with %d", pollfd
);
2120 } else if (revents
& (LPOLLERR
| LPOLLHUP
| LPOLLRDHUP
)) {
2121 cleanup_connection_pollfd(&events
, pollfd
);
2122 /* Put "create" ownership reference. */
2123 connection_put(conn
);
2125 ERR("Unexpected poll events %u for sock %d", revents
, pollfd
);
2126 connection_put(conn
);
2129 /* Put local "get_by_sock" reference. */
2130 connection_put(conn
);
2137 lttng_poll_clean(&events
);
2139 /* Cleanup reamaining connection object. */
2141 cds_lfht_for_each_entry(viewer_connections_ht
->ht
, &iter
.iter
,
2144 health_code_update();
2145 connection_put(destroy_conn
);
2149 lttng_ht_destroy(viewer_connections_ht
);
2150 viewer_connections_ht_error
:
2151 /* Close relay conn pipes */
2152 utils_close_pipe(live_conn_pipe
);
2154 DBG("Viewer worker thread exited with error");
2156 DBG("Viewer worker thread cleanup complete");
2160 ERR("Health error occurred in %s", __func__
);
2162 health_unregister(health_relayd
);
2163 if (lttng_relay_stop_threads()) {
2164 ERR("Error stopping threads");
2166 rcu_unregister_thread();
2171 * Create the relay command pipe to wake thread_manage_apps.
2172 * Closed in cleanup().
2174 static int create_conn_pipe(void)
2176 return utils_create_pipe_cloexec(live_conn_pipe
);
2179 int relayd_live_join(void)
2181 int ret
, retval
= 0;
2184 ret
= pthread_join(live_listener_thread
, &status
);
2187 PERROR("pthread_join live listener");
2191 ret
= pthread_join(live_worker_thread
, &status
);
2194 PERROR("pthread_join live worker");
2198 ret
= pthread_join(live_dispatcher_thread
, &status
);
2201 PERROR("pthread_join live dispatcher");
2205 cleanup_relayd_live();
2213 int relayd_live_create(struct lttng_uri
*uri
)
2215 int ret
= 0, retval
= 0;
2221 goto exit_init_data
;
2225 /* Check if daemon is UID = 0 */
2226 is_root
= !getuid();
2229 if (live_uri
->port
< 1024) {
2230 ERR("Need to be root to use ports < 1024");
2232 goto exit_init_data
;
2236 /* Setup the thread apps communication pipe. */
2237 if (create_conn_pipe()) {
2239 goto exit_init_data
;
2242 /* Init relay command queue. */
2243 cds_wfcq_init(&viewer_conn_queue
.head
, &viewer_conn_queue
.tail
);
2245 /* Set up max poll set size */
2246 if (lttng_poll_set_max_size()) {
2248 goto exit_init_data
;
2251 /* Setup the dispatcher thread */
2252 ret
= pthread_create(&live_dispatcher_thread
, default_pthread_attr(),
2253 thread_dispatcher
, (void *) NULL
);
2256 PERROR("pthread_create viewer dispatcher");
2258 goto exit_dispatcher_thread
;
2261 /* Setup the worker thread */
2262 ret
= pthread_create(&live_worker_thread
, default_pthread_attr(),
2263 thread_worker
, NULL
);
2266 PERROR("pthread_create viewer worker");
2268 goto exit_worker_thread
;
2271 /* Setup the listener thread */
2272 ret
= pthread_create(&live_listener_thread
, default_pthread_attr(),
2273 thread_listener
, (void *) NULL
);
2276 PERROR("pthread_create viewer listener");
2278 goto exit_listener_thread
;
2282 * All OK, started all threads.
2287 * Join on the live_listener_thread should anything be added after
2288 * the live_listener thread's creation.
2291 exit_listener_thread
:
2293 ret
= pthread_join(live_worker_thread
, &status
);
2296 PERROR("pthread_join live worker");
2301 ret
= pthread_join(live_dispatcher_thread
, &status
);
2304 PERROR("pthread_join live dispatcher");
2307 exit_dispatcher_thread
:
2310 cleanup_relayd_live();