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_data_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
);
546 /* Thread quit pipe has been closed. Killing thread. */
547 ret
= check_thread_quit_pipe(pollfd
, revents
);
553 if (revents
& LPOLLIN
) {
555 * A new connection is requested, therefore a
556 * viewer connection is allocated in this
557 * thread, enqueued to a global queue and
558 * dequeued (and freed) in the worker thread.
561 struct relay_connection
*new_conn
;
562 struct lttcomm_sock
*newsock
;
564 newsock
= live_control_sock
->ops
->accept(live_control_sock
);
566 PERROR("accepting control sock");
569 DBG("Relay viewer connection accepted socket %d", newsock
->fd
);
571 ret
= setsockopt(newsock
->fd
, SOL_SOCKET
, SO_REUSEADDR
, &val
,
574 PERROR("setsockopt inet");
575 lttcomm_destroy_sock(newsock
);
578 new_conn
= connection_create(newsock
, RELAY_CONNECTION_UNKNOWN
);
580 lttcomm_destroy_sock(newsock
);
583 /* Ownership assumed by the connection. */
586 /* Enqueue request for the dispatcher thread. */
587 cds_wfcq_enqueue(&viewer_conn_queue
.head
, &viewer_conn_queue
.tail
,
591 * Wake the dispatch queue futex.
592 * Implicit memory barrier with the
593 * exchange in cds_wfcq_enqueue.
595 futex_nto1_wake(&viewer_conn_queue
.futex
);
596 } else if (revents
& (LPOLLERR
| LPOLLHUP
| LPOLLRDHUP
)) {
597 ERR("socket poll error");
600 ERR("Unexpected poll events %u for sock %d", revents
, pollfd
);
610 lttng_poll_clean(&events
);
612 if (live_control_sock
->fd
>= 0) {
613 ret
= live_control_sock
->ops
->close(live_control_sock
);
618 lttcomm_destroy_sock(live_control_sock
);
622 DBG("Live viewer listener thread exited with error");
624 health_unregister(health_relayd
);
625 DBG("Live viewer listener thread cleanup complete");
626 if (lttng_relay_stop_threads()) {
627 ERR("Error stopping threads");
633 * This thread manages the dispatching of the requests to worker threads
636 void *thread_dispatcher(void *data
)
640 struct cds_wfcq_node
*node
;
641 struct relay_connection
*conn
= NULL
;
643 DBG("[thread] Live viewer relay dispatcher started");
645 health_register(health_relayd
, HEALTH_RELAYD_TYPE_LIVE_DISPATCHER
);
647 if (testpoint(relayd_thread_live_dispatcher
)) {
648 goto error_testpoint
;
651 health_code_update();
654 health_code_update();
656 /* Atomically prepare the queue futex */
657 futex_nto1_prepare(&viewer_conn_queue
.futex
);
659 if (CMM_LOAD_SHARED(live_dispatch_thread_exit
)) {
664 health_code_update();
666 /* Dequeue commands */
667 node
= cds_wfcq_dequeue_blocking(&viewer_conn_queue
.head
,
668 &viewer_conn_queue
.tail
);
670 DBG("Woken up but nothing in the live-viewer "
671 "relay command queue");
672 /* Continue thread execution */
675 conn
= caa_container_of(node
, struct relay_connection
, qnode
);
676 DBG("Dispatching viewer request waiting on sock %d",
680 * Inform worker thread of the new request. This
681 * call is blocking so we can be assured that
682 * the data will be read at some point in time
683 * or wait to the end of the world :)
685 ret
= lttng_write(live_conn_pipe
[1], &conn
, sizeof(conn
));
687 PERROR("write conn pipe");
688 connection_put(conn
);
691 } while (node
!= NULL
);
693 /* Futex wait on queue. Blocking call on futex() */
695 futex_nto1_wait(&viewer_conn_queue
.futex
);
699 /* Normal exit, no error */
706 ERR("Health error occurred in %s", __func__
);
708 health_unregister(health_relayd
);
709 DBG("Live viewer dispatch thread dying");
710 if (lttng_relay_stop_threads()) {
711 ERR("Error stopping threads");
717 * Establish connection with the viewer and check the versions.
719 * Return 0 on success or else negative value.
722 int viewer_connect(struct relay_connection
*conn
)
725 struct lttng_viewer_connect reply
, msg
;
727 conn
->version_check_done
= 1;
729 health_code_update();
731 DBG("Viewer is establishing a connection to the relayd.");
733 ret
= recv_request(conn
->sock
, &msg
, sizeof(msg
));
738 health_code_update();
740 memset(&reply
, 0, sizeof(reply
));
741 reply
.major
= RELAYD_VERSION_COMM_MAJOR
;
742 reply
.minor
= RELAYD_VERSION_COMM_MINOR
;
744 /* Major versions must be the same */
745 if (reply
.major
!= be32toh(msg
.major
)) {
746 DBG("Incompatible major versions ([relayd] %u vs [client] %u)",
747 reply
.major
, be32toh(msg
.major
));
752 conn
->major
= reply
.major
;
753 /* We adapt to the lowest compatible version */
754 if (reply
.minor
<= be32toh(msg
.minor
)) {
755 conn
->minor
= reply
.minor
;
757 conn
->minor
= be32toh(msg
.minor
);
760 if (be32toh(msg
.type
) == LTTNG_VIEWER_CLIENT_COMMAND
) {
761 conn
->type
= RELAY_VIEWER_COMMAND
;
762 } else if (be32toh(msg
.type
) == LTTNG_VIEWER_CLIENT_NOTIFICATION
) {
763 conn
->type
= RELAY_VIEWER_NOTIFICATION
;
765 ERR("Unknown connection type : %u", be32toh(msg
.type
));
770 reply
.major
= htobe32(reply
.major
);
771 reply
.minor
= htobe32(reply
.minor
);
772 if (conn
->type
== RELAY_VIEWER_COMMAND
) {
774 * Increment outside of htobe64 macro, because the argument can
775 * be used more than once within the macro, and thus the
776 * operation may be undefined.
778 pthread_mutex_lock(&last_relay_viewer_session_id_lock
);
779 last_relay_viewer_session_id
++;
780 pthread_mutex_unlock(&last_relay_viewer_session_id_lock
);
781 reply
.viewer_session_id
= htobe64(last_relay_viewer_session_id
);
784 health_code_update();
786 ret
= send_response(conn
->sock
, &reply
, sizeof(reply
));
791 health_code_update();
793 DBG("Version check done using protocol %u.%u", conn
->major
, conn
->minor
);
801 * Send the viewer the list of current sessions.
802 * We need to create a copy of the hash table content because otherwise
803 * we cannot assume the number of entries stays the same between getting
804 * the number of HT elements and iteration over the HT.
806 * Return 0 on success or else a negative value.
809 int viewer_list_sessions(struct relay_connection
*conn
)
812 struct lttng_viewer_list_sessions session_list
;
813 struct lttng_ht_iter iter
;
814 struct relay_session
*session
;
815 struct lttng_viewer_session
*send_session_buf
= NULL
;
816 uint32_t buf_count
= SESSION_BUF_DEFAULT_COUNT
;
819 DBG("List sessions received");
821 send_session_buf
= zmalloc(SESSION_BUF_DEFAULT_COUNT
* sizeof(*send_session_buf
));
822 if (!send_session_buf
) {
827 cds_lfht_for_each_entry(sessions_ht
->ht
, &iter
.iter
, session
,
829 struct lttng_viewer_session
*send_session
;
831 health_code_update();
833 if (session
->connection_closed
) {
834 /* Skip closed session */
838 if (count
>= buf_count
) {
839 struct lttng_viewer_session
*newbuf
;
840 uint32_t new_buf_count
= buf_count
<< 1;
842 newbuf
= realloc(send_session_buf
,
843 new_buf_count
* sizeof(*send_session_buf
));
848 send_session_buf
= newbuf
;
849 buf_count
= new_buf_count
;
851 send_session
= &send_session_buf
[count
];
852 if (lttng_strncpy(send_session
->session_name
,
853 session
->session_name
,
854 sizeof(send_session
->session_name
))) {
858 if (lttng_strncpy(send_session
->hostname
, session
->hostname
,
859 sizeof(send_session
->hostname
))) {
863 send_session
->id
= htobe64(session
->id
);
864 send_session
->live_timer
= htobe32(session
->live_timer
);
865 if (session
->viewer_attached
) {
866 send_session
->clients
= htobe32(1);
868 send_session
->clients
= htobe32(0);
870 send_session
->streams
= htobe32(session
->stream_count
);
878 session_list
.sessions_count
= htobe32(count
);
880 health_code_update();
882 ret
= send_response(conn
->sock
, &session_list
, sizeof(session_list
));
887 health_code_update();
889 ret
= send_response(conn
->sock
, send_session_buf
,
890 count
* sizeof(*send_session_buf
));
894 health_code_update();
898 free(send_session_buf
);
903 * Send the viewer the list of current streams.
906 int viewer_get_new_streams(struct relay_connection
*conn
)
908 int ret
, send_streams
= 0;
909 uint32_t nb_created
= 0, nb_unsent
= 0, nb_streams
= 0, nb_total
= 0;
910 struct lttng_viewer_new_streams_request request
;
911 struct lttng_viewer_new_streams_response response
;
912 struct relay_session
*session
;
918 DBG("Get new streams received");
920 health_code_update();
922 /* Receive the request from the connected client. */
923 ret
= recv_request(conn
->sock
, &request
, sizeof(request
));
927 session_id
= be64toh(request
.session_id
);
929 health_code_update();
931 memset(&response
, 0, sizeof(response
));
933 session
= session_get_by_id(session_id
);
935 DBG("Relay session %" PRIu64
" not found", session_id
);
936 response
.status
= htobe32(LTTNG_VIEWER_NEW_STREAMS_ERR
);
940 if (!viewer_session_is_attached(conn
->viewer_session
, session
)) {
942 response
.status
= htobe32(LTTNG_VIEWER_NEW_STREAMS_ERR
);
947 response
.status
= htobe32(LTTNG_VIEWER_NEW_STREAMS_OK
);
949 ret
= make_viewer_streams(session
, LTTNG_VIEWER_SEEK_LAST
, &nb_total
, &nb_unsent
,
950 &nb_created
, &closed
);
952 goto end_put_session
;
954 /* Only send back the newly created streams with the unsent ones. */
955 nb_streams
= nb_created
+ nb_unsent
;
956 response
.streams_count
= htobe32(nb_streams
);
959 * If the session is closed, HUP when there are no more streams
962 if (closed
&& nb_total
== 0) {
964 response
.streams_count
= 0;
965 response
.status
= htobe32(LTTNG_VIEWER_NEW_STREAMS_HUP
);
970 health_code_update();
971 ret
= send_response(conn
->sock
, &response
, sizeof(response
));
973 goto end_put_session
;
975 health_code_update();
978 * Unknown or empty session, just return gracefully, the viewer
979 * knows what is happening.
981 if (!send_streams
|| !nb_streams
) {
983 goto end_put_session
;
987 * Send stream and *DON'T* ignore the sent flag so every viewer
988 * streams that were not sent from that point will be sent to
991 ret
= send_viewer_streams(conn
->sock
, session
, 0);
993 goto end_put_session
;
998 session_put(session
);
1005 * Send the viewer the list of current sessions.
1008 int viewer_attach_session(struct relay_connection
*conn
)
1010 int send_streams
= 0;
1012 uint32_t nb_streams
= 0;
1013 enum lttng_viewer_seek seek_type
;
1014 struct lttng_viewer_attach_session_request request
;
1015 struct lttng_viewer_attach_session_response response
;
1016 struct relay_session
*session
= NULL
;
1017 bool closed
= false;
1021 health_code_update();
1023 /* Receive the request from the connected client. */
1024 ret
= recv_request(conn
->sock
, &request
, sizeof(request
));
1029 health_code_update();
1031 memset(&response
, 0, sizeof(response
));
1033 if (!conn
->viewer_session
) {
1034 DBG("Client trying to attach before creating a live viewer session");
1035 response
.status
= htobe32(LTTNG_VIEWER_ATTACH_NO_SESSION
);
1039 session
= session_get_by_id(be64toh(request
.session_id
));
1041 DBG("Relay session %" PRIu64
" not found",
1042 (uint64_t) be64toh(request
.session_id
));
1043 response
.status
= htobe32(LTTNG_VIEWER_ATTACH_UNK
);
1046 DBG("Attach session ID %" PRIu64
" received",
1047 (uint64_t) be64toh(request
.session_id
));
1049 if (session
->live_timer
== 0) {
1050 DBG("Not live session");
1051 response
.status
= htobe32(LTTNG_VIEWER_ATTACH_NOT_LIVE
);
1056 ret
= viewer_session_attach(conn
->viewer_session
, session
);
1058 DBG("Already a viewer attached");
1059 response
.status
= htobe32(LTTNG_VIEWER_ATTACH_ALREADY
);
1063 switch (be32toh(request
.seek
)) {
1064 case LTTNG_VIEWER_SEEK_BEGINNING
:
1065 case LTTNG_VIEWER_SEEK_LAST
:
1066 response
.status
= htobe32(LTTNG_VIEWER_ATTACH_OK
);
1067 seek_type
= be32toh(request
.seek
);
1070 ERR("Wrong seek parameter");
1071 response
.status
= htobe32(LTTNG_VIEWER_ATTACH_SEEK_ERR
);
1076 ret
= make_viewer_streams(session
, seek_type
, &nb_streams
, NULL
,
1079 goto end_put_session
;
1081 response
.streams_count
= htobe32(nb_streams
);
1084 * If the session is closed when the viewer is attaching, it
1085 * means some of the streams may have been concurrently removed,
1086 * so we don't allow the viewer to attach, even if there are
1087 * streams available.
1091 response
.streams_count
= 0;
1092 response
.status
= htobe32(LTTNG_VIEWER_ATTACH_UNK
);
1097 health_code_update();
1098 ret
= send_response(conn
->sock
, &response
, sizeof(response
));
1100 goto end_put_session
;
1102 health_code_update();
1105 * Unknown or empty session, just return gracefully, the viewer
1106 * knows what is happening.
1108 if (!send_streams
|| !nb_streams
) {
1110 goto end_put_session
;
1113 /* Send stream and ignore the sent flag. */
1114 ret
= send_viewer_streams(conn
->sock
, session
, 1);
1116 goto end_put_session
;
1121 session_put(session
);
1128 * Open the index file if needed for the given vstream.
1130 * If an index file is successfully opened, the vstream will set it as its
1131 * current index file.
1133 * Return 0 on success, a negative value on error (-ENOENT if not ready yet).
1135 * Called with rstream lock held.
1137 static int try_open_index(struct relay_viewer_stream
*vstream
,
1138 struct relay_stream
*rstream
)
1141 const uint32_t connection_major
= rstream
->trace
->session
->major
;
1142 const uint32_t connection_minor
= rstream
->trace
->session
->minor
;
1144 if (vstream
->index_file
) {
1149 * First time, we open the index file and at least one index is ready.
1151 if (rstream
->index_received_seqcount
== 0) {
1155 vstream
->index_file
= lttng_index_file_create_from_trace_chunk_read_only(
1156 rstream
->trace_chunk
, rstream
->path_name
,
1157 rstream
->channel_name
, rstream
->tracefile_size
,
1158 vstream
->current_tracefile_id
,
1159 lttng_to_index_major(connection_major
, connection_minor
),
1160 lttng_to_index_minor(connection_major
, connection_minor
));
1161 if (!vstream
->index_file
) {
1170 * Check the status of the index for the given stream. This function
1171 * updates the index structure if needed and can put (close) the vstream
1172 * in the HUP situation.
1174 * Return 0 means that we can proceed with the index. A value of 1 means
1175 * that the index has been updated and is ready to be sent to the
1176 * client. A negative value indicates an error that can't be handled.
1178 * Called with rstream lock held.
1180 static int check_index_status(struct relay_viewer_stream
*vstream
,
1181 struct relay_stream
*rstream
, struct ctf_trace
*trace
,
1182 struct lttng_viewer_index
*index
)
1186 if ((trace
->session
->connection_closed
|| rstream
->closed
)
1187 && rstream
->index_received_seqcount
1188 == vstream
->index_sent_seqcount
) {
1190 * Last index sent and session connection or relay
1191 * stream are closed.
1193 index
->status
= htobe32(LTTNG_VIEWER_INDEX_HUP
);
1195 } else if (rstream
->beacon_ts_end
!= -1ULL &&
1196 rstream
->index_received_seqcount
1197 == vstream
->index_sent_seqcount
) {
1199 * We've received a synchronization beacon and the last index
1200 * available has been sent, the index for now is inactive.
1202 * In this case, we have received a beacon which allows us to
1203 * inform the client of a time interval during which we can
1204 * guarantee that there are no events to read (and never will
1207 index
->status
= htobe32(LTTNG_VIEWER_INDEX_INACTIVE
);
1208 index
->timestamp_end
= htobe64(rstream
->beacon_ts_end
);
1209 index
->stream_id
= htobe64(rstream
->ctf_stream_id
);
1211 } else if (rstream
->index_received_seqcount
1212 == vstream
->index_sent_seqcount
) {
1214 * This checks whether received == sent seqcount. In
1215 * this case, we have not received a beacon. Therefore,
1216 * we can only ask the client to retry later.
1218 index
->status
= htobe32(LTTNG_VIEWER_INDEX_RETRY
);
1220 } else if (!tracefile_array_seq_in_file(rstream
->tfa
,
1221 vstream
->current_tracefile_id
,
1222 vstream
->index_sent_seqcount
)) {
1224 * The next index we want to send cannot be read either
1225 * because we need to perform a rotation, or due to
1226 * the producer having overwritten its trace file.
1228 DBG("Viewer stream %" PRIu64
" rotation",
1229 vstream
->stream
->stream_handle
);
1230 ret
= viewer_stream_rotate(vstream
);
1233 } else if (ret
== 1) {
1234 /* EOF across entire stream. */
1235 index
->status
= htobe32(LTTNG_VIEWER_INDEX_HUP
);
1239 * If we have been pushed due to overwrite, it
1240 * necessarily means there is data that can be read in
1241 * the stream. If we rotated because we reached the end
1242 * of a tracefile, it means the following tracefile
1243 * needs to contain at least one index, else we would
1244 * have already returned LTTNG_VIEWER_INDEX_RETRY to the
1245 * viewer. The updated index_sent_seqcount needs to
1246 * point to a readable index entry now.
1248 * In the case where we "rotate" on a single file, we
1249 * can end up in a case where the requested index is
1250 * still unavailable.
1252 if (rstream
->tracefile_count
== 1 &&
1253 !tracefile_array_seq_in_file(
1255 vstream
->current_tracefile_id
,
1256 vstream
->index_sent_seqcount
)) {
1257 index
->status
= htobe32(LTTNG_VIEWER_INDEX_RETRY
);
1260 assert(tracefile_array_seq_in_file(rstream
->tfa
,
1261 vstream
->current_tracefile_id
,
1262 vstream
->index_sent_seqcount
));
1264 /* ret == 0 means successful so we continue. */
1270 viewer_stream_put(vstream
);
1276 * Send the next index for a stream.
1278 * Return 0 on success or else a negative value.
1281 int viewer_get_next_index(struct relay_connection
*conn
)
1284 struct lttng_viewer_get_next_index request_index
;
1285 struct lttng_viewer_index viewer_index
;
1286 struct ctf_packet_index packet_index
;
1287 struct relay_viewer_stream
*vstream
= NULL
;
1288 struct relay_stream
*rstream
= NULL
;
1289 struct ctf_trace
*ctf_trace
= NULL
;
1290 struct relay_viewer_stream
*metadata_viewer_stream
= NULL
;
1294 DBG("Viewer get next index");
1296 memset(&viewer_index
, 0, sizeof(viewer_index
));
1297 health_code_update();
1299 ret
= recv_request(conn
->sock
, &request_index
, sizeof(request_index
));
1303 health_code_update();
1305 vstream
= viewer_stream_get_by_id(be64toh(request_index
.stream_id
));
1307 DBG("Client requested index of unknown stream id %" PRIu64
,
1308 (uint64_t) be64toh(request_index
.stream_id
));
1309 viewer_index
.status
= htobe32(LTTNG_VIEWER_INDEX_ERR
);
1313 /* Use back. ref. Protected by refcounts. */
1314 rstream
= vstream
->stream
;
1315 ctf_trace
= rstream
->trace
;
1317 /* metadata_viewer_stream may be NULL. */
1318 metadata_viewer_stream
=
1319 ctf_trace_get_viewer_metadata_stream(ctf_trace
);
1321 pthread_mutex_lock(&rstream
->lock
);
1324 * The viewer should not ask for index on metadata stream.
1326 if (rstream
->is_metadata
) {
1327 viewer_index
.status
= htobe32(LTTNG_VIEWER_INDEX_HUP
);
1331 /* Try to open an index if one is needed for that stream. */
1332 ret
= try_open_index(vstream
, rstream
);
1334 if (ret
== -ENOENT
) {
1336 * The index is created only when the first data
1337 * packet arrives, it might not be ready at the
1338 * beginning of the session
1340 viewer_index
.status
= htobe32(LTTNG_VIEWER_INDEX_RETRY
);
1342 /* Unhandled error. */
1343 viewer_index
.status
= htobe32(LTTNG_VIEWER_INDEX_ERR
);
1348 ret
= check_index_status(vstream
, rstream
, ctf_trace
, &viewer_index
);
1351 } else if (ret
== 1) {
1353 * We have no index to send and check_index_status has populated
1354 * viewer_index's status.
1358 /* At this point, ret is 0 thus we will be able to read the index. */
1362 * vstream->stream_fd may be NULL if it has been closed by
1363 * tracefile rotation, or if we are at the beginning of the
1364 * stream. We open the data stream file here to protect against
1365 * overwrite caused by tracefile rotation (in association with
1366 * unlink performed before overwrite).
1368 if (!vstream
->stream_file
.fd
) {
1370 char file_path
[LTTNG_PATH_MAX
];
1371 enum lttng_trace_chunk_status status
;
1373 ret
= utils_stream_file_path(rstream
->path_name
,
1374 rstream
->channel_name
, rstream
->tracefile_size
,
1375 vstream
->current_tracefile_id
, NULL
, file_path
,
1381 status
= lttng_trace_chunk_open_file(
1382 vstream
->stream_file
.trace_chunk
,
1383 file_path
, O_RDONLY
, 0, &fd
);
1384 if (status
!= LTTNG_TRACE_CHUNK_STATUS_OK
) {
1385 PERROR("Failed to open trace file for viewer stream");
1388 vstream
->stream_file
.fd
= stream_fd_create(fd
);
1389 if (!vstream
->stream_file
.fd
) {
1391 PERROR("Failed to close viewer stream file");
1397 ret
= check_new_streams(conn
);
1399 viewer_index
.status
= htobe32(LTTNG_VIEWER_INDEX_ERR
);
1401 } else if (ret
== 1) {
1402 viewer_index
.flags
|= LTTNG_VIEWER_FLAG_NEW_STREAM
;
1405 ret
= lttng_index_file_read(vstream
->index_file
, &packet_index
);
1407 ERR("Relay error reading index file %d",
1408 vstream
->index_file
->fd
);
1409 viewer_index
.status
= htobe32(LTTNG_VIEWER_INDEX_ERR
);
1412 viewer_index
.status
= htobe32(LTTNG_VIEWER_INDEX_OK
);
1413 vstream
->index_sent_seqcount
++;
1417 * Indexes are stored in big endian, no need to switch before sending.
1419 DBG("Sending viewer index for stream %" PRIu64
" offset %" PRIu64
,
1420 rstream
->stream_handle
,
1421 (uint64_t) be64toh(packet_index
.offset
));
1422 viewer_index
.offset
= packet_index
.offset
;
1423 viewer_index
.packet_size
= packet_index
.packet_size
;
1424 viewer_index
.content_size
= packet_index
.content_size
;
1425 viewer_index
.timestamp_begin
= packet_index
.timestamp_begin
;
1426 viewer_index
.timestamp_end
= packet_index
.timestamp_end
;
1427 viewer_index
.events_discarded
= packet_index
.events_discarded
;
1428 viewer_index
.stream_id
= packet_index
.stream_id
;
1432 pthread_mutex_unlock(&rstream
->lock
);
1435 if (metadata_viewer_stream
) {
1436 pthread_mutex_lock(&metadata_viewer_stream
->stream
->lock
);
1437 DBG("get next index metadata check: recv %" PRIu64
1439 metadata_viewer_stream
->stream
->metadata_received
,
1440 metadata_viewer_stream
->metadata_sent
);
1441 if (!metadata_viewer_stream
->stream
->metadata_received
||
1442 metadata_viewer_stream
->stream
->metadata_received
>
1443 metadata_viewer_stream
->metadata_sent
) {
1444 viewer_index
.flags
|= LTTNG_VIEWER_FLAG_NEW_METADATA
;
1446 pthread_mutex_unlock(&metadata_viewer_stream
->stream
->lock
);
1449 viewer_index
.flags
= htobe32(viewer_index
.flags
);
1450 health_code_update();
1452 ret
= send_response(conn
->sock
, &viewer_index
, sizeof(viewer_index
));
1456 health_code_update();
1459 DBG("Index %" PRIu64
" for stream %" PRIu64
" sent",
1460 vstream
->index_sent_seqcount
,
1461 vstream
->stream
->stream_handle
);
1464 if (metadata_viewer_stream
) {
1465 viewer_stream_put(metadata_viewer_stream
);
1468 viewer_stream_put(vstream
);
1473 pthread_mutex_unlock(&rstream
->lock
);
1474 if (metadata_viewer_stream
) {
1475 viewer_stream_put(metadata_viewer_stream
);
1477 viewer_stream_put(vstream
);
1482 * Send the next index for a stream
1484 * Return 0 on success or else a negative value.
1487 int viewer_get_packet(struct relay_connection
*conn
)
1492 struct lttng_viewer_get_packet get_packet_info
;
1493 struct lttng_viewer_trace_packet reply_header
;
1494 struct relay_viewer_stream
*vstream
= NULL
;
1495 uint32_t reply_size
= sizeof(reply_header
);
1496 uint32_t packet_data_len
= 0;
1499 DBG2("Relay get data packet");
1501 health_code_update();
1503 ret
= recv_request(conn
->sock
, &get_packet_info
,
1504 sizeof(get_packet_info
));
1508 health_code_update();
1510 /* From this point on, the error label can be reached. */
1511 memset(&reply_header
, 0, sizeof(reply_header
));
1513 vstream
= viewer_stream_get_by_id(be64toh(get_packet_info
.stream_id
));
1515 DBG("Client requested packet of unknown stream id %" PRIu64
,
1516 (uint64_t) be64toh(get_packet_info
.stream_id
));
1517 reply_header
.status
= htobe32(LTTNG_VIEWER_GET_PACKET_ERR
);
1518 goto send_reply_nolock
;
1520 packet_data_len
= be32toh(get_packet_info
.len
);
1521 reply_size
+= packet_data_len
;
1524 reply
= zmalloc(reply_size
);
1526 PERROR("packet reply zmalloc");
1527 reply_size
= sizeof(reply_header
);
1531 pthread_mutex_lock(&vstream
->stream
->lock
);
1532 lseek_ret
= lseek(vstream
->stream_file
.fd
->fd
,
1533 be64toh(get_packet_info
.offset
), SEEK_SET
);
1534 if (lseek_ret
< 0) {
1535 PERROR("lseek fd %d to offset %" PRIu64
,
1536 vstream
->stream_file
.fd
->fd
,
1537 (uint64_t) be64toh(get_packet_info
.offset
));
1540 read_len
= lttng_read(vstream
->stream_file
.fd
->fd
,
1541 reply
+ sizeof(reply_header
), packet_data_len
);
1542 if (read_len
< packet_data_len
) {
1543 PERROR("Relay reading trace file, fd: %d, offset: %" PRIu64
,
1544 vstream
->stream_file
.fd
->fd
,
1545 (uint64_t) be64toh(get_packet_info
.offset
));
1548 reply_header
.status
= htobe32(LTTNG_VIEWER_GET_PACKET_OK
);
1549 reply_header
.len
= htobe32(packet_data_len
);
1553 reply_header
.status
= htobe32(LTTNG_VIEWER_GET_PACKET_ERR
);
1557 pthread_mutex_unlock(&vstream
->stream
->lock
);
1561 health_code_update();
1564 memcpy(reply
, &reply_header
, sizeof(reply_header
));
1565 ret
= send_response(conn
->sock
, reply
, reply_size
);
1567 /* No reply to send. */
1568 ret
= send_response(conn
->sock
, &reply_header
,
1572 health_code_update();
1574 PERROR("sendmsg of packet data failed");
1578 DBG("Sent %u bytes for stream %" PRIu64
, reply_size
,
1579 (uint64_t) be64toh(get_packet_info
.stream_id
));
1585 viewer_stream_put(vstream
);
1591 * Send the session's metadata
1593 * Return 0 on success else a negative value.
1596 int viewer_get_metadata(struct relay_connection
*conn
)
1602 struct lttng_viewer_get_metadata request
;
1603 struct lttng_viewer_metadata_packet reply
;
1604 struct relay_viewer_stream
*vstream
= NULL
;
1608 DBG("Relay get metadata");
1610 health_code_update();
1612 ret
= recv_request(conn
->sock
, &request
, sizeof(request
));
1616 health_code_update();
1618 memset(&reply
, 0, sizeof(reply
));
1620 vstream
= viewer_stream_get_by_id(be64toh(request
.stream_id
));
1623 * The metadata stream can be closed by a CLOSE command
1624 * just before we attach. It can also be closed by
1625 * per-pid tracing during tracing. Therefore, it is
1626 * possible that we cannot find this viewer stream.
1627 * Reply back to the client with an error if we cannot
1630 DBG("Client requested metadata of unknown stream id %" PRIu64
,
1631 (uint64_t) be64toh(request
.stream_id
));
1632 reply
.status
= htobe32(LTTNG_VIEWER_METADATA_ERR
);
1635 pthread_mutex_lock(&vstream
->stream
->lock
);
1636 if (!vstream
->stream
->is_metadata
) {
1637 ERR("Invalid metadata stream");
1641 assert(vstream
->metadata_sent
<= vstream
->stream
->metadata_received
);
1643 len
= vstream
->stream
->metadata_received
- vstream
->metadata_sent
;
1645 reply
.status
= htobe32(LTTNG_VIEWER_NO_NEW_METADATA
);
1649 /* first time, we open the metadata file */
1650 if (!vstream
->stream_file
.fd
) {
1652 char file_path
[LTTNG_PATH_MAX
];
1653 enum lttng_trace_chunk_status status
;
1654 struct relay_stream
*rstream
= vstream
->stream
;
1656 ret
= utils_stream_file_path(rstream
->path_name
,
1657 rstream
->channel_name
, rstream
->tracefile_size
,
1658 vstream
->current_tracefile_id
, NULL
, file_path
,
1664 status
= lttng_trace_chunk_open_file(
1665 vstream
->stream_file
.trace_chunk
,
1666 file_path
, O_RDONLY
, 0, &fd
);
1667 if (status
!= LTTNG_TRACE_CHUNK_STATUS_OK
) {
1668 PERROR("Failed to open metadata file for viewer stream");
1671 vstream
->stream_file
.fd
= stream_fd_create(fd
);
1672 if (!vstream
->stream_file
.fd
) {
1674 PERROR("Failed to close viewer metadata file");
1680 reply
.len
= htobe64(len
);
1681 data
= zmalloc(len
);
1683 PERROR("viewer metadata zmalloc");
1687 read_len
= lttng_read(vstream
->stream_file
.fd
->fd
, data
, len
);
1688 if (read_len
< len
) {
1689 PERROR("Relay reading metadata file");
1692 vstream
->metadata_sent
+= read_len
;
1693 if (vstream
->metadata_sent
== vstream
->stream
->metadata_received
1694 && vstream
->stream
->closed
) {
1695 /* Release ownership for the viewer metadata stream. */
1696 viewer_stream_put(vstream
);
1699 reply
.status
= htobe32(LTTNG_VIEWER_METADATA_OK
);
1704 reply
.status
= htobe32(LTTNG_VIEWER_METADATA_ERR
);
1707 health_code_update();
1709 pthread_mutex_unlock(&vstream
->stream
->lock
);
1711 ret
= send_response(conn
->sock
, &reply
, sizeof(reply
));
1715 health_code_update();
1718 ret
= send_response(conn
->sock
, data
, len
);
1724 DBG("Sent %" PRIu64
" bytes of metadata for stream %" PRIu64
, len
,
1725 (uint64_t) be64toh(request
.stream_id
));
1727 DBG("Metadata sent");
1733 viewer_stream_put(vstream
);
1739 * Create a viewer session.
1741 * Return 0 on success or else a negative value.
1744 int viewer_create_session(struct relay_connection
*conn
)
1747 struct lttng_viewer_create_session_response resp
;
1749 DBG("Viewer create session received");
1751 memset(&resp
, 0, sizeof(resp
));
1752 resp
.status
= htobe32(LTTNG_VIEWER_CREATE_SESSION_OK
);
1753 conn
->viewer_session
= viewer_session_create();
1754 if (!conn
->viewer_session
) {
1755 ERR("Allocation viewer session");
1756 resp
.status
= htobe32(LTTNG_VIEWER_CREATE_SESSION_ERR
);
1761 health_code_update();
1762 ret
= send_response(conn
->sock
, &resp
, sizeof(resp
));
1766 health_code_update();
1774 * Detach a viewer session.
1776 * Return 0 on success or else a negative value.
1779 int viewer_detach_session(struct relay_connection
*conn
)
1782 struct lttng_viewer_detach_session_response response
;
1783 struct lttng_viewer_detach_session_request request
;
1784 struct relay_session
*session
= NULL
;
1785 uint64_t viewer_session_to_close
;
1787 DBG("Viewer detach session received");
1791 health_code_update();
1793 /* Receive the request from the connected client. */
1794 ret
= recv_request(conn
->sock
, &request
, sizeof(request
));
1798 viewer_session_to_close
= be64toh(request
.session_id
);
1800 if (!conn
->viewer_session
) {
1801 DBG("Client trying to detach before creating a live viewer session");
1802 response
.status
= htobe32(LTTNG_VIEWER_DETACH_SESSION_ERR
);
1806 health_code_update();
1808 memset(&response
, 0, sizeof(response
));
1809 DBG("Detaching from session ID %" PRIu64
, viewer_session_to_close
);
1811 session
= session_get_by_id(be64toh(request
.session_id
));
1813 DBG("Relay session %" PRIu64
" not found",
1814 (uint64_t) be64toh(request
.session_id
));
1815 response
.status
= htobe32(LTTNG_VIEWER_DETACH_SESSION_UNK
);
1819 ret
= viewer_session_is_attached(conn
->viewer_session
, session
);
1821 DBG("Not attached to this session");
1822 response
.status
= htobe32(LTTNG_VIEWER_DETACH_SESSION_ERR
);
1823 goto send_reply_put
;
1826 viewer_session_close_one_session(conn
->viewer_session
, session
);
1827 response
.status
= htobe32(LTTNG_VIEWER_DETACH_SESSION_OK
);
1828 DBG("Session %" PRIu64
" detached.", viewer_session_to_close
);
1831 session_put(session
);
1834 health_code_update();
1835 ret
= send_response(conn
->sock
, &response
, sizeof(response
));
1839 health_code_update();
1847 * live_relay_unknown_command: send -1 if received unknown command
1850 void live_relay_unknown_command(struct relay_connection
*conn
)
1852 struct lttcomm_relayd_generic_reply reply
;
1854 memset(&reply
, 0, sizeof(reply
));
1855 reply
.ret_code
= htobe32(LTTNG_ERR_UNK
);
1856 (void) send_response(conn
->sock
, &reply
, sizeof(reply
));
1860 * Process the commands received on the control socket
1863 int process_control(struct lttng_viewer_cmd
*recv_hdr
,
1864 struct relay_connection
*conn
)
1869 msg_value
= be32toh(recv_hdr
->cmd
);
1872 * Make sure we've done the version check before any command other then a
1873 * new client connection.
1875 if (msg_value
!= LTTNG_VIEWER_CONNECT
&& !conn
->version_check_done
) {
1876 ERR("Viewer conn value %" PRIu32
" before version check", msg_value
);
1881 switch (msg_value
) {
1882 case LTTNG_VIEWER_CONNECT
:
1883 ret
= viewer_connect(conn
);
1885 case LTTNG_VIEWER_LIST_SESSIONS
:
1886 ret
= viewer_list_sessions(conn
);
1888 case LTTNG_VIEWER_ATTACH_SESSION
:
1889 ret
= viewer_attach_session(conn
);
1891 case LTTNG_VIEWER_GET_NEXT_INDEX
:
1892 ret
= viewer_get_next_index(conn
);
1894 case LTTNG_VIEWER_GET_PACKET
:
1895 ret
= viewer_get_packet(conn
);
1897 case LTTNG_VIEWER_GET_METADATA
:
1898 ret
= viewer_get_metadata(conn
);
1900 case LTTNG_VIEWER_GET_NEW_STREAMS
:
1901 ret
= viewer_get_new_streams(conn
);
1903 case LTTNG_VIEWER_CREATE_SESSION
:
1904 ret
= viewer_create_session(conn
);
1906 case LTTNG_VIEWER_DETACH_SESSION
:
1907 ret
= viewer_detach_session(conn
);
1910 ERR("Received unknown viewer command (%u)",
1911 be32toh(recv_hdr
->cmd
));
1912 live_relay_unknown_command(conn
);
1922 void cleanup_connection_pollfd(struct lttng_poll_event
*events
, int pollfd
)
1926 (void) lttng_poll_del(events
, pollfd
);
1928 ret
= close(pollfd
);
1930 ERR("Closing pollfd %d", pollfd
);
1935 * This thread does the actual work
1938 void *thread_worker(void *data
)
1942 struct lttng_poll_event events
;
1943 struct lttng_ht
*viewer_connections_ht
;
1944 struct lttng_ht_iter iter
;
1945 struct lttng_viewer_cmd recv_hdr
;
1946 struct relay_connection
*destroy_conn
;
1948 DBG("[thread] Live viewer relay worker started");
1950 rcu_register_thread();
1952 health_register(health_relayd
, HEALTH_RELAYD_TYPE_LIVE_WORKER
);
1954 if (testpoint(relayd_thread_live_worker
)) {
1955 goto error_testpoint
;
1958 /* table of connections indexed on socket */
1959 viewer_connections_ht
= lttng_ht_new(0, LTTNG_HT_TYPE_ULONG
);
1960 if (!viewer_connections_ht
) {
1961 goto viewer_connections_ht_error
;
1964 ret
= create_thread_poll_set(&events
, 2);
1966 goto error_poll_create
;
1969 ret
= lttng_poll_add(&events
, live_conn_pipe
[0], LPOLLIN
| LPOLLRDHUP
);
1978 health_code_update();
1980 /* Infinite blocking call, waiting for transmission */
1981 DBG3("Relayd live viewer worker thread polling...");
1982 health_poll_entry();
1983 ret
= lttng_poll_wait(&events
, -1);
1987 * Restart interrupted system call.
1989 if (errno
== EINTR
) {
1998 * Process control. The control connection is prioritised so we don't
1999 * starve it with high throughput tracing data on the data
2002 for (i
= 0; i
< nb_fd
; i
++) {
2003 /* Fetch once the poll data */
2004 uint32_t revents
= LTTNG_POLL_GETEV(&events
, i
);
2005 int pollfd
= LTTNG_POLL_GETFD(&events
, i
);
2007 health_code_update();
2009 /* Thread quit pipe has been closed. Killing thread. */
2010 ret
= check_thread_quit_pipe(pollfd
, revents
);
2016 /* Inspect the relay conn pipe for new connection. */
2017 if (pollfd
== live_conn_pipe
[0]) {
2018 if (revents
& LPOLLIN
) {
2019 struct relay_connection
*conn
;
2021 ret
= lttng_read(live_conn_pipe
[0],
2022 &conn
, sizeof(conn
));
2026 lttng_poll_add(&events
, conn
->sock
->fd
,
2027 LPOLLIN
| LPOLLRDHUP
);
2028 connection_ht_add(viewer_connections_ht
, conn
);
2029 DBG("Connection socket %d added to poll", conn
->sock
->fd
);
2030 } else if (revents
& (LPOLLERR
| LPOLLHUP
| LPOLLRDHUP
)) {
2031 ERR("Relay live pipe error");
2034 ERR("Unexpected poll events %u for sock %d", revents
, pollfd
);
2038 /* Connection activity. */
2039 struct relay_connection
*conn
;
2041 conn
= connection_get_by_sock(viewer_connections_ht
, pollfd
);
2046 if (revents
& LPOLLIN
) {
2047 ret
= conn
->sock
->ops
->recvmsg(conn
->sock
, &recv_hdr
,
2048 sizeof(recv_hdr
), 0);
2050 /* Connection closed. */
2051 cleanup_connection_pollfd(&events
, pollfd
);
2052 /* Put "create" ownership reference. */
2053 connection_put(conn
);
2054 DBG("Viewer control conn closed with %d", pollfd
);
2056 ret
= process_control(&recv_hdr
, conn
);
2058 /* Clear the session on error. */
2059 cleanup_connection_pollfd(&events
, pollfd
);
2060 /* Put "create" ownership reference. */
2061 connection_put(conn
);
2062 DBG("Viewer connection closed with %d", pollfd
);
2065 } else if (revents
& (LPOLLERR
| LPOLLHUP
| LPOLLRDHUP
)) {
2066 cleanup_connection_pollfd(&events
, pollfd
);
2067 /* Put "create" ownership reference. */
2068 connection_put(conn
);
2070 ERR("Unexpected poll events %u for sock %d", revents
, pollfd
);
2071 connection_put(conn
);
2074 /* Put local "get_by_sock" reference. */
2075 connection_put(conn
);
2082 lttng_poll_clean(&events
);
2084 /* Cleanup remaining connection object. */
2086 cds_lfht_for_each_entry(viewer_connections_ht
->ht
, &iter
.iter
,
2089 health_code_update();
2090 connection_put(destroy_conn
);
2094 lttng_ht_destroy(viewer_connections_ht
);
2095 viewer_connections_ht_error
:
2096 /* Close relay conn pipes */
2097 utils_close_pipe(live_conn_pipe
);
2099 DBG("Viewer worker thread exited with error");
2101 DBG("Viewer worker thread cleanup complete");
2105 ERR("Health error occurred in %s", __func__
);
2107 health_unregister(health_relayd
);
2108 if (lttng_relay_stop_threads()) {
2109 ERR("Error stopping threads");
2111 rcu_unregister_thread();
2116 * Create the relay command pipe to wake thread_manage_apps.
2117 * Closed in cleanup().
2119 static int create_conn_pipe(void)
2121 return utils_create_pipe_cloexec(live_conn_pipe
);
2124 int relayd_live_join(void)
2126 int ret
, retval
= 0;
2129 ret
= pthread_join(live_listener_thread
, &status
);
2132 PERROR("pthread_join live listener");
2136 ret
= pthread_join(live_worker_thread
, &status
);
2139 PERROR("pthread_join live worker");
2143 ret
= pthread_join(live_dispatcher_thread
, &status
);
2146 PERROR("pthread_join live dispatcher");
2150 cleanup_relayd_live();
2158 int relayd_live_create(struct lttng_uri
*uri
)
2160 int ret
= 0, retval
= 0;
2166 goto exit_init_data
;
2170 /* Check if daemon is UID = 0 */
2171 is_root
= !getuid();
2174 if (live_uri
->port
< 1024) {
2175 ERR("Need to be root to use ports < 1024");
2177 goto exit_init_data
;
2181 /* Setup the thread apps communication pipe. */
2182 if (create_conn_pipe()) {
2184 goto exit_init_data
;
2187 /* Init relay command queue. */
2188 cds_wfcq_init(&viewer_conn_queue
.head
, &viewer_conn_queue
.tail
);
2190 /* Set up max poll set size */
2191 if (lttng_poll_set_max_size()) {
2193 goto exit_init_data
;
2196 /* Setup the dispatcher thread */
2197 ret
= pthread_create(&live_dispatcher_thread
, default_pthread_attr(),
2198 thread_dispatcher
, (void *) NULL
);
2201 PERROR("pthread_create viewer dispatcher");
2203 goto exit_dispatcher_thread
;
2206 /* Setup the worker thread */
2207 ret
= pthread_create(&live_worker_thread
, default_pthread_attr(),
2208 thread_worker
, NULL
);
2211 PERROR("pthread_create viewer worker");
2213 goto exit_worker_thread
;
2216 /* Setup the listener thread */
2217 ret
= pthread_create(&live_listener_thread
, default_pthread_attr(),
2218 thread_listener
, (void *) NULL
);
2221 PERROR("pthread_create viewer listener");
2223 goto exit_listener_thread
;
2227 * All OK, started all threads.
2232 * Join on the live_listener_thread should anything be added after
2233 * the live_listener thread's creation.
2236 exit_listener_thread
:
2238 ret
= pthread_join(live_worker_thread
, &status
);
2241 PERROR("pthread_join live worker");
2246 ret
= pthread_join(live_dispatcher_thread
, &status
);
2249 PERROR("pthread_join live dispatcher");
2252 exit_dispatcher_thread
:
2255 cleanup_relayd_live();