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.
31 #include <sys/mount.h>
32 #include <sys/resource.h>
33 #include <sys/socket.h>
35 #include <sys/types.h>
38 #include <urcu/futex.h>
39 #include <urcu/uatomic.h>
40 #include <urcu/rculist.h>
45 #include <lttng/lttng.h>
46 #include <common/common.h>
47 #include <common/compat/poll.h>
48 #include <common/compat/socket.h>
49 #include <common/compat/endian.h>
50 #include <common/defaults.h>
51 #include <common/futex.h>
52 #include <common/index/index.h>
53 #include <common/sessiond-comm/sessiond-comm.h>
54 #include <common/sessiond-comm/inet.h>
55 #include <common/sessiond-comm/relayd.h>
56 #include <common/uri.h>
57 #include <common/utils.h>
61 #include "lttng-relayd.h"
63 #include "health-relayd.h"
64 #include "testpoint.h"
65 #include "viewer-stream.h"
68 #include "ctf-trace.h"
69 #include "connection.h"
70 #include "viewer-session.h"
72 #define SESSION_BUF_DEFAULT_COUNT 16
74 static struct lttng_uri
*live_uri
;
77 * This pipe is used to inform the worker thread that a command is queued and
78 * ready to be processed.
80 static int live_conn_pipe
[2] = { -1, -1 };
82 /* Shared between threads */
83 static int live_dispatch_thread_exit
;
85 static pthread_t live_listener_thread
;
86 static pthread_t live_dispatcher_thread
;
87 static pthread_t live_worker_thread
;
90 * Relay command queue.
92 * The live_thread_listener and live_thread_dispatcher communicate with this
95 static struct relay_conn_queue viewer_conn_queue
;
97 static uint64_t last_relay_viewer_session_id
;
98 static pthread_mutex_t last_relay_viewer_session_id_lock
=
99 PTHREAD_MUTEX_INITIALIZER
;
105 void cleanup_relayd_live(void)
113 * Receive a request buffer using a given socket, destination allocated buffer
116 * Return the size of the received message or else a negative value on error
117 * with errno being set by recvmsg() syscall.
120 ssize_t
recv_request(struct lttcomm_sock
*sock
, void *buf
, size_t size
)
124 ret
= sock
->ops
->recvmsg(sock
, buf
, size
, 0);
125 if (ret
< 0 || ret
!= size
) {
127 /* Orderly shutdown. Not necessary to print an error. */
128 DBG("Socket %d did an orderly shutdown", sock
->fd
);
130 ERR("Relay failed to receive request.");
139 * Send a response buffer using a given socket, source allocated buffer of
142 * Return the size of the sent message or else a negative value on error with
143 * errno being set by sendmsg() syscall.
146 ssize_t
send_response(struct lttcomm_sock
*sock
, void *buf
, size_t size
)
150 ret
= sock
->ops
->sendmsg(sock
, buf
, size
, 0);
152 ERR("Relayd failed to send response.");
159 * Atomically check if new streams got added in one of the sessions attached
160 * and reset the flag to 0.
162 * Returns 1 if new streams got added, 0 if nothing changed, a negative value
166 int check_new_streams(struct relay_connection
*conn
)
168 struct relay_session
*session
;
169 unsigned long current_val
;
172 if (!conn
->viewer_session
) {
176 cds_list_for_each_entry_rcu(session
,
177 &conn
->viewer_session
->session_list
,
178 viewer_session_node
) {
179 if (!session_get(session
)) {
182 current_val
= uatomic_cmpxchg(&session
->new_streams
, 1, 0);
184 session_put(session
);
195 * Send viewer streams to the given socket. The ignore_sent_flag indicates if
196 * this function should ignore the sent flag or not.
198 * Return 0 on success or else a negative value.
201 ssize_t
send_viewer_streams(struct lttcomm_sock
*sock
,
202 struct relay_session
*session
, unsigned int ignore_sent_flag
)
205 struct lttng_viewer_stream send_stream
;
206 struct lttng_ht_iter iter
;
207 struct relay_viewer_stream
*vstream
;
211 cds_lfht_for_each_entry(viewer_streams_ht
->ht
, &iter
.iter
, vstream
,
213 struct ctf_trace
*ctf_trace
;
215 health_code_update();
217 if (!viewer_stream_get(vstream
)) {
221 pthread_mutex_lock(&vstream
->stream
->lock
);
222 /* Ignore if not the same session. */
223 if (vstream
->stream
->trace
->session
->id
!= session
->id
||
224 (!ignore_sent_flag
&& vstream
->sent_flag
)) {
225 pthread_mutex_unlock(&vstream
->stream
->lock
);
226 viewer_stream_put(vstream
);
230 ctf_trace
= vstream
->stream
->trace
;
231 send_stream
.id
= htobe64(vstream
->stream
->stream_handle
);
232 send_stream
.ctf_trace_id
= htobe64(ctf_trace
->id
);
233 send_stream
.metadata_flag
= htobe32(
234 vstream
->stream
->is_metadata
);
235 if (lttng_strncpy(send_stream
.path_name
, vstream
->path_name
,
236 sizeof(send_stream
.path_name
))) {
237 pthread_mutex_unlock(&vstream
->stream
->lock
);
238 viewer_stream_put(vstream
);
239 ret
= -1; /* Error. */
242 if (lttng_strncpy(send_stream
.channel_name
,
243 vstream
->channel_name
,
244 sizeof(send_stream
.channel_name
))) {
245 pthread_mutex_unlock(&vstream
->stream
->lock
);
246 viewer_stream_put(vstream
);
247 ret
= -1; /* Error. */
251 DBG("Sending stream %" PRIu64
" to viewer",
252 vstream
->stream
->stream_handle
);
253 vstream
->sent_flag
= 1;
254 pthread_mutex_unlock(&vstream
->stream
->lock
);
256 ret
= send_response(sock
, &send_stream
, sizeof(send_stream
));
257 viewer_stream_put(vstream
);
271 * Create every viewer stream possible for the given session with the seek
272 * type. Three counters *can* be return which are in order the total amount of
273 * viewer stream of the session, the number of unsent stream and the number of
274 * stream created. Those counters can be NULL and thus will be ignored.
276 * Return 0 on success or else a negative value.
279 int make_viewer_streams(struct relay_session
*session
,
280 enum lttng_viewer_seek seek_t
, uint32_t *nb_total
, uint32_t *nb_unsent
,
281 uint32_t *nb_created
, bool *closed
)
284 struct lttng_ht_iter iter
;
285 struct ctf_trace
*ctf_trace
;
290 * Hold the session lock to ensure that we see either none or
291 * all initial streams for a session, but no intermediate state.
293 pthread_mutex_lock(&session
->lock
);
295 if (session
->connection_closed
) {
300 * Create viewer streams for relay streams that are ready to be
301 * used for a the given session id only.
304 cds_lfht_for_each_entry(session
->ctf_traces_ht
->ht
, &iter
.iter
, ctf_trace
,
306 struct relay_stream
*stream
;
308 health_code_update();
310 if (!ctf_trace_get(ctf_trace
)) {
314 cds_list_for_each_entry_rcu(stream
, &ctf_trace
->stream_list
, stream_node
) {
315 struct relay_viewer_stream
*vstream
;
317 if (!stream_get(stream
)) {
321 * stream published is protected by the session lock.
323 if (!stream
->published
) {
326 vstream
= viewer_stream_get_by_id(stream
->stream_handle
);
328 vstream
= viewer_stream_create(stream
, seek_t
);
331 ctf_trace_put(ctf_trace
);
337 /* Update number of created stream counter. */
341 * Ensure a self-reference is preserved even
342 * after we have put our local reference.
344 viewer_stream_get(vstream
);
346 if (!vstream
->sent_flag
&& nb_unsent
) {
347 /* Update number of unsent stream counter. */
351 /* Update number of total stream counter. */
353 if (stream
->is_metadata
) {
354 if (!stream
->closed
||
355 stream
->metadata_received
> vstream
->metadata_sent
) {
359 if (!stream
->closed
||
360 !(((int64_t) (stream
->prev_seq
- stream
->last_net_seq_num
)) >= 0)) {
366 /* Put local reference. */
367 viewer_stream_put(vstream
);
371 ctf_trace_put(ctf_trace
);
378 pthread_mutex_unlock(&session
->lock
);
382 int relayd_live_stop(void)
384 /* Stop dispatch thread */
385 CMM_STORE_SHARED(live_dispatch_thread_exit
, 1);
386 futex_nto1_wake(&viewer_conn_queue
.futex
);
391 * Create a poll set with O_CLOEXEC and add the thread quit pipe to the set.
394 int create_thread_poll_set(struct lttng_poll_event
*events
, int size
)
398 if (events
== NULL
|| size
== 0) {
403 ret
= lttng_poll_create(events
, size
, LTTNG_CLOEXEC
);
409 ret
= lttng_poll_add(events
, thread_quit_pipe
[0], LPOLLIN
| LPOLLERR
);
421 * Check if the thread quit pipe was triggered.
423 * Return 1 if it was triggered else 0;
426 int check_thread_quit_pipe(int fd
, uint32_t events
)
428 if (fd
== thread_quit_pipe
[0] && (events
& LPOLLIN
)) {
436 * Create and init socket from uri.
439 struct lttcomm_sock
*init_socket(struct lttng_uri
*uri
)
442 struct lttcomm_sock
*sock
= NULL
;
444 sock
= lttcomm_alloc_sock_from_uri(uri
);
446 ERR("Allocating socket");
450 ret
= lttcomm_create_sock(sock
);
454 DBG("Listening on sock %d for live", sock
->fd
);
456 ret
= sock
->ops
->bind(sock
);
461 ret
= sock
->ops
->listen(sock
, -1);
471 lttcomm_destroy_sock(sock
);
477 * This thread manages the listening for new connections on the network
480 void *thread_listener(void *data
)
482 int i
, ret
, pollfd
, err
= -1;
483 uint32_t revents
, nb_fd
;
484 struct lttng_poll_event events
;
485 struct lttcomm_sock
*live_control_sock
;
487 DBG("[thread] Relay live listener started");
489 health_register(health_relayd
, HEALTH_RELAYD_TYPE_LIVE_LISTENER
);
491 health_code_update();
493 live_control_sock
= init_socket(live_uri
);
494 if (!live_control_sock
) {
495 goto error_sock_control
;
498 /* Pass 2 as size here for the thread quit pipe and control sockets. */
499 ret
= create_thread_poll_set(&events
, 2);
501 goto error_create_poll
;
504 /* Add the control socket */
505 ret
= lttng_poll_add(&events
, live_control_sock
->fd
, LPOLLIN
| LPOLLRDHUP
);
510 lttng_relay_notify_ready();
512 if (testpoint(relayd_thread_live_listener
)) {
513 goto error_testpoint
;
517 health_code_update();
519 DBG("Listener accepting live viewers connections");
523 ret
= lttng_poll_wait(&events
, -1);
527 * Restart interrupted system call.
529 if (errno
== EINTR
) {
536 DBG("Relay new viewer connection received");
537 for (i
= 0; i
< nb_fd
; i
++) {
538 health_code_update();
540 /* Fetch once the poll data */
541 revents
= LTTNG_POLL_GETEV(&events
, i
);
542 pollfd
= LTTNG_POLL_GETFD(&events
, i
);
545 /* No activity for this FD (poll implementation). */
549 /* Thread quit pipe has been closed. Killing thread. */
550 ret
= check_thread_quit_pipe(pollfd
, revents
);
556 if (revents
& LPOLLIN
) {
558 * A new connection is requested, therefore a
559 * viewer connection is allocated in this
560 * thread, enqueued to a global queue and
561 * dequeued (and freed) in the worker thread.
564 struct relay_connection
*new_conn
;
565 struct lttcomm_sock
*newsock
;
567 newsock
= live_control_sock
->ops
->accept(live_control_sock
);
569 PERROR("accepting control sock");
572 DBG("Relay viewer connection accepted socket %d", newsock
->fd
);
574 ret
= setsockopt(newsock
->fd
, SOL_SOCKET
, SO_REUSEADDR
, &val
,
577 PERROR("setsockopt inet");
578 lttcomm_destroy_sock(newsock
);
581 new_conn
= connection_create(newsock
, RELAY_CONNECTION_UNKNOWN
);
583 lttcomm_destroy_sock(newsock
);
586 /* Ownership assumed by the connection. */
589 /* Enqueue request for the dispatcher thread. */
590 cds_wfcq_enqueue(&viewer_conn_queue
.head
, &viewer_conn_queue
.tail
,
594 * Wake the dispatch queue futex.
595 * Implicit memory barrier with the
596 * exchange in cds_wfcq_enqueue.
598 futex_nto1_wake(&viewer_conn_queue
.futex
);
599 } else if (revents
& (LPOLLERR
| LPOLLHUP
| LPOLLRDHUP
)) {
600 ERR("socket poll error");
603 ERR("Unexpected poll events %u for sock %d", revents
, pollfd
);
613 lttng_poll_clean(&events
);
615 if (live_control_sock
->fd
>= 0) {
616 ret
= live_control_sock
->ops
->close(live_control_sock
);
621 lttcomm_destroy_sock(live_control_sock
);
625 DBG("Live viewer listener thread exited with error");
627 health_unregister(health_relayd
);
628 DBG("Live viewer listener thread cleanup complete");
629 if (lttng_relay_stop_threads()) {
630 ERR("Error stopping threads");
636 * This thread manages the dispatching of the requests to worker threads
639 void *thread_dispatcher(void *data
)
643 struct cds_wfcq_node
*node
;
644 struct relay_connection
*conn
= NULL
;
646 DBG("[thread] Live viewer relay dispatcher started");
648 health_register(health_relayd
, HEALTH_RELAYD_TYPE_LIVE_DISPATCHER
);
650 if (testpoint(relayd_thread_live_dispatcher
)) {
651 goto error_testpoint
;
654 health_code_update();
656 while (!CMM_LOAD_SHARED(live_dispatch_thread_exit
)) {
657 health_code_update();
659 /* Atomically prepare the queue futex */
660 futex_nto1_prepare(&viewer_conn_queue
.futex
);
663 health_code_update();
665 /* Dequeue commands */
666 node
= cds_wfcq_dequeue_blocking(&viewer_conn_queue
.head
,
667 &viewer_conn_queue
.tail
);
669 DBG("Woken up but nothing in the live-viewer "
670 "relay command queue");
671 /* Continue thread execution */
674 conn
= caa_container_of(node
, struct relay_connection
, qnode
);
675 DBG("Dispatching viewer request waiting on sock %d",
679 * Inform worker thread of the new request. This
680 * call is blocking so we can be assured that
681 * the data will be read at some point in time
682 * or wait to the end of the world :)
684 ret
= lttng_write(live_conn_pipe
[1], &conn
, sizeof(conn
));
686 PERROR("write conn pipe");
687 connection_put(conn
);
690 } while (node
!= NULL
);
692 /* Futex wait on queue. Blocking call on futex() */
694 futex_nto1_wait(&viewer_conn_queue
.futex
);
698 /* Normal exit, no error */
705 ERR("Health error occurred in %s", __func__
);
707 health_unregister(health_relayd
);
708 DBG("Live viewer dispatch thread dying");
709 if (lttng_relay_stop_threads()) {
710 ERR("Error stopping threads");
716 * Establish connection with the viewer and check the versions.
718 * Return 0 on success or else negative value.
721 int viewer_connect(struct relay_connection
*conn
)
724 struct lttng_viewer_connect reply
, msg
;
726 conn
->version_check_done
= 1;
728 health_code_update();
730 DBG("Viewer is establishing a connection to the relayd.");
732 ret
= recv_request(conn
->sock
, &msg
, sizeof(msg
));
737 health_code_update();
739 memset(&reply
, 0, sizeof(reply
));
740 reply
.major
= RELAYD_VERSION_COMM_MAJOR
;
741 reply
.minor
= RELAYD_VERSION_COMM_MINOR
;
743 /* Major versions must be the same */
744 if (reply
.major
!= be32toh(msg
.major
)) {
745 DBG("Incompatible major versions ([relayd] %u vs [client] %u)",
746 reply
.major
, be32toh(msg
.major
));
751 conn
->major
= reply
.major
;
752 /* We adapt to the lowest compatible version */
753 if (reply
.minor
<= be32toh(msg
.minor
)) {
754 conn
->minor
= reply
.minor
;
756 conn
->minor
= be32toh(msg
.minor
);
759 if (be32toh(msg
.type
) == LTTNG_VIEWER_CLIENT_COMMAND
) {
760 conn
->type
= RELAY_VIEWER_COMMAND
;
761 } else if (be32toh(msg
.type
) == LTTNG_VIEWER_CLIENT_NOTIFICATION
) {
762 conn
->type
= RELAY_VIEWER_NOTIFICATION
;
764 ERR("Unknown connection type : %u", be32toh(msg
.type
));
769 reply
.major
= htobe32(reply
.major
);
770 reply
.minor
= htobe32(reply
.minor
);
771 if (conn
->type
== RELAY_VIEWER_COMMAND
) {
773 * Increment outside of htobe64 macro, because the argument can
774 * be used more than once within the macro, and thus the
775 * operation may be undefined.
777 pthread_mutex_lock(&last_relay_viewer_session_id_lock
);
778 last_relay_viewer_session_id
++;
779 pthread_mutex_unlock(&last_relay_viewer_session_id_lock
);
780 reply
.viewer_session_id
= htobe64(last_relay_viewer_session_id
);
783 health_code_update();
785 ret
= send_response(conn
->sock
, &reply
, sizeof(reply
));
790 health_code_update();
792 DBG("Version check done using protocol %u.%u", conn
->major
, conn
->minor
);
800 * Send the viewer the list of current sessions.
801 * We need to create a copy of the hash table content because otherwise
802 * we cannot assume the number of entries stays the same between getting
803 * the number of HT elements and iteration over the HT.
805 * Return 0 on success or else a negative value.
808 int viewer_list_sessions(struct relay_connection
*conn
)
811 struct lttng_viewer_list_sessions session_list
;
812 struct lttng_ht_iter iter
;
813 struct relay_session
*session
;
814 struct lttng_viewer_session
*send_session_buf
= NULL
;
815 uint32_t buf_count
= SESSION_BUF_DEFAULT_COUNT
;
818 DBG("List sessions received");
820 send_session_buf
= zmalloc(SESSION_BUF_DEFAULT_COUNT
* sizeof(*send_session_buf
));
821 if (!send_session_buf
) {
826 cds_lfht_for_each_entry(sessions_ht
->ht
, &iter
.iter
, session
,
828 struct lttng_viewer_session
*send_session
;
830 health_code_update();
832 if (count
>= buf_count
) {
833 struct lttng_viewer_session
*newbuf
;
834 uint32_t new_buf_count
= buf_count
<< 1;
836 newbuf
= realloc(send_session_buf
,
837 new_buf_count
* sizeof(*send_session_buf
));
843 send_session_buf
= newbuf
;
844 buf_count
= new_buf_count
;
846 send_session
= &send_session_buf
[count
];
847 if (lttng_strncpy(send_session
->session_name
,
848 session
->session_name
,
849 sizeof(send_session
->session_name
))) {
854 if (lttng_strncpy(send_session
->hostname
, session
->hostname
,
855 sizeof(send_session
->hostname
))) {
860 send_session
->id
= htobe64(session
->id
);
861 send_session
->live_timer
= htobe32(session
->live_timer
);
862 if (session
->viewer_attached
) {
863 send_session
->clients
= htobe32(1);
865 send_session
->clients
= htobe32(0);
867 send_session
->streams
= htobe32(session
->stream_count
);
872 session_list
.sessions_count
= htobe32(count
);
874 health_code_update();
876 ret
= send_response(conn
->sock
, &session_list
, sizeof(session_list
));
881 health_code_update();
883 ret
= send_response(conn
->sock
, send_session_buf
,
884 count
* sizeof(*send_session_buf
));
888 health_code_update();
892 free(send_session_buf
);
897 * Send the viewer the list of current streams.
900 int viewer_get_new_streams(struct relay_connection
*conn
)
902 int ret
, send_streams
= 0;
903 uint32_t nb_created
= 0, nb_unsent
= 0, nb_streams
= 0, nb_total
= 0;
904 struct lttng_viewer_new_streams_request request
;
905 struct lttng_viewer_new_streams_response response
;
906 struct relay_session
*session
;
912 DBG("Get new streams received");
914 health_code_update();
916 /* Receive the request from the connected client. */
917 ret
= recv_request(conn
->sock
, &request
, sizeof(request
));
921 session_id
= be64toh(request
.session_id
);
923 health_code_update();
925 memset(&response
, 0, sizeof(response
));
927 session
= session_get_by_id(session_id
);
929 DBG("Relay session %" PRIu64
" not found", session_id
);
930 response
.status
= htobe32(LTTNG_VIEWER_NEW_STREAMS_ERR
);
934 if (!viewer_session_is_attached(conn
->viewer_session
, session
)) {
936 response
.status
= htobe32(LTTNG_VIEWER_NEW_STREAMS_ERR
);
941 response
.status
= htobe32(LTTNG_VIEWER_NEW_STREAMS_OK
);
943 ret
= make_viewer_streams(session
, LTTNG_VIEWER_SEEK_LAST
, &nb_total
, &nb_unsent
,
944 &nb_created
, &closed
);
946 goto end_put_session
;
948 /* Only send back the newly created streams with the unsent ones. */
949 nb_streams
= nb_created
+ nb_unsent
;
950 response
.streams_count
= htobe32(nb_streams
);
953 * If the session is closed, HUP when there are no more streams
956 if (closed
&& nb_total
== 0) {
958 response
.streams_count
= 0;
959 response
.status
= htobe32(LTTNG_VIEWER_NEW_STREAMS_HUP
);
964 health_code_update();
965 ret
= send_response(conn
->sock
, &response
, sizeof(response
));
967 goto end_put_session
;
969 health_code_update();
972 * Unknown or empty session, just return gracefully, the viewer
973 * knows what is happening.
975 if (!send_streams
|| !nb_streams
) {
977 goto end_put_session
;
981 * Send stream and *DON'T* ignore the sent flag so every viewer
982 * streams that were not sent from that point will be sent to
985 ret
= send_viewer_streams(conn
->sock
, session
, 0);
987 goto end_put_session
;
992 session_put(session
);
999 * Send the viewer the list of current sessions.
1002 int viewer_attach_session(struct relay_connection
*conn
)
1004 int send_streams
= 0;
1006 uint32_t nb_streams
= 0;
1007 enum lttng_viewer_seek seek_type
;
1008 struct lttng_viewer_attach_session_request request
;
1009 struct lttng_viewer_attach_session_response response
;
1010 struct relay_session
*session
= NULL
;
1011 bool closed
= false;
1015 health_code_update();
1017 /* Receive the request from the connected client. */
1018 ret
= recv_request(conn
->sock
, &request
, sizeof(request
));
1023 health_code_update();
1025 memset(&response
, 0, sizeof(response
));
1027 if (!conn
->viewer_session
) {
1028 DBG("Client trying to attach before creating a live viewer session");
1029 response
.status
= htobe32(LTTNG_VIEWER_ATTACH_NO_SESSION
);
1033 session
= session_get_by_id(be64toh(request
.session_id
));
1035 DBG("Relay session %" PRIu64
" not found",
1036 be64toh(request
.session_id
));
1037 response
.status
= htobe32(LTTNG_VIEWER_ATTACH_UNK
);
1040 DBG("Attach session ID %" PRIu64
" received",
1041 be64toh(request
.session_id
));
1043 if (session
->live_timer
== 0) {
1044 DBG("Not live session");
1045 response
.status
= htobe32(LTTNG_VIEWER_ATTACH_NOT_LIVE
);
1050 ret
= viewer_session_attach(conn
->viewer_session
, session
);
1052 DBG("Already a viewer attached");
1053 response
.status
= htobe32(LTTNG_VIEWER_ATTACH_ALREADY
);
1057 switch (be32toh(request
.seek
)) {
1058 case LTTNG_VIEWER_SEEK_BEGINNING
:
1059 case LTTNG_VIEWER_SEEK_LAST
:
1060 response
.status
= htobe32(LTTNG_VIEWER_ATTACH_OK
);
1061 seek_type
= be32toh(request
.seek
);
1064 ERR("Wrong seek parameter");
1065 response
.status
= htobe32(LTTNG_VIEWER_ATTACH_SEEK_ERR
);
1070 ret
= make_viewer_streams(session
, seek_type
, &nb_streams
, NULL
,
1073 goto end_put_session
;
1075 response
.streams_count
= htobe32(nb_streams
);
1078 * If the session is closed when the viewer is attaching, it
1079 * means some of the streams may have been concurrently removed,
1080 * so we don't allow the viewer to attach, even if there are
1081 * streams available.
1085 response
.streams_count
= 0;
1086 response
.status
= htobe32(LTTNG_VIEWER_NEW_STREAMS_HUP
);
1091 health_code_update();
1092 ret
= send_response(conn
->sock
, &response
, sizeof(response
));
1094 goto end_put_session
;
1096 health_code_update();
1099 * Unknown or empty session, just return gracefully, the viewer
1100 * knows what is happening.
1102 if (!send_streams
|| !nb_streams
) {
1104 goto end_put_session
;
1107 /* Send stream and ignore the sent flag. */
1108 ret
= send_viewer_streams(conn
->sock
, session
, 1);
1110 goto end_put_session
;
1115 session_put(session
);
1122 * Open the index file if needed for the given vstream.
1124 * If an index file is successfully opened, the vstream index_fd set with
1127 * Return 0 on success, a negative value on error (-ENOENT if not ready yet).
1129 * Called with rstream lock held.
1131 static int try_open_index(struct relay_viewer_stream
*vstream
,
1132 struct relay_stream
*rstream
)
1136 if (vstream
->index_fd
) {
1141 * First time, we open the index file and at least one index is ready.
1143 if (rstream
->index_received_seqcount
== 0) {
1147 ret
= index_open(vstream
->path_name
, vstream
->channel_name
,
1148 vstream
->stream
->tracefile_count
,
1149 vstream
->current_tracefile_id
);
1151 vstream
->index_fd
= stream_fd_create(ret
);
1152 if (!vstream
->index_fd
) {
1168 * Check the status of the index for the given stream. This function
1169 * updates the index structure if needed and can put (close) the vstream
1170 * in the HUP situation.
1172 * Return 0 means that we can proceed with the index. A value of 1 means
1173 * that the index has been updated and is ready to be sent to the
1174 * client. A negative value indicates an error that can't be handled.
1176 * Called with rstream lock held.
1178 static int check_index_status(struct relay_viewer_stream
*vstream
,
1179 struct relay_stream
*rstream
, struct ctf_trace
*trace
,
1180 struct lttng_viewer_index
*index
)
1184 if (trace
->session
->connection_closed
1185 && rstream
->index_received_seqcount
1186 == vstream
->index_sent_seqcount
) {
1187 /* Last index sent and session connection is closed. */
1188 index
->status
= htobe32(LTTNG_VIEWER_INDEX_HUP
);
1190 } else if (rstream
->beacon_ts_end
!= -1ULL &&
1191 rstream
->index_received_seqcount
1192 == vstream
->index_sent_seqcount
) {
1194 * We've received a synchronization beacon and the last index
1195 * available has been sent, the index for now is inactive.
1197 * In this case, we have received a beacon which allows us to
1198 * inform the client of a time interval during which we can
1199 * guarantee that there are no events to read (and never will
1202 index
->status
= htobe32(LTTNG_VIEWER_INDEX_INACTIVE
);
1203 index
->timestamp_end
= htobe64(rstream
->beacon_ts_end
);
1204 index
->stream_id
= htobe64(rstream
->ctf_stream_id
);
1206 } else if (rstream
->index_received_seqcount
1207 == vstream
->index_sent_seqcount
) {
1209 * This checks whether received == sent seqcount. In
1210 * this case, we have not received a beacon. Therefore,
1211 * we can only ask the client to retry later.
1213 index
->status
= htobe32(LTTNG_VIEWER_INDEX_RETRY
);
1215 } else if (!tracefile_array_seq_in_file(rstream
->tfa
,
1216 vstream
->current_tracefile_id
,
1217 vstream
->index_sent_seqcount
)) {
1219 * The next index we want to send cannot be read either
1220 * because we need to perform a rotation, or due to
1221 * the producer having overwritten its trace file.
1223 DBG("Viewer stream %" PRIu64
" rotation",
1224 vstream
->stream
->stream_handle
);
1225 ret
= viewer_stream_rotate(vstream
);
1228 } else if (ret
== 1) {
1229 /* EOF across entire stream. */
1230 index
->status
= htobe32(LTTNG_VIEWER_INDEX_HUP
);
1234 * If we have been pushed due to overwrite, it
1235 * necessarily means there is data that can be read in
1236 * the stream. If we rotated because we reached the end
1237 * of a tracefile, it means the following tracefile
1238 * needs to contain at least one index, else we would
1239 * have already returned LTTNG_VIEWER_INDEX_RETRY to the
1240 * viewer. The updated index_sent_seqcount needs to
1241 * point to a readable index entry now.
1243 * In the case where we "rotate" on a single file, we
1244 * can end up in a case where the requested index is
1245 * still unavailable.
1247 if (rstream
->tracefile_count
== 1 &&
1248 !tracefile_array_seq_in_file(
1250 vstream
->current_tracefile_id
,
1251 vstream
->index_sent_seqcount
)) {
1252 index
->status
= htobe32(LTTNG_VIEWER_INDEX_RETRY
);
1255 assert(tracefile_array_seq_in_file(rstream
->tfa
,
1256 vstream
->current_tracefile_id
,
1257 vstream
->index_sent_seqcount
));
1259 /* ret == 0 means successful so we continue. */
1265 viewer_stream_put(vstream
);
1271 * Send the next index for a stream.
1273 * Return 0 on success or else a negative value.
1276 int viewer_get_next_index(struct relay_connection
*conn
)
1280 struct lttng_viewer_get_next_index request_index
;
1281 struct lttng_viewer_index viewer_index
;
1282 struct ctf_packet_index packet_index
;
1283 struct relay_viewer_stream
*vstream
= NULL
;
1284 struct relay_stream
*rstream
= NULL
;
1285 struct ctf_trace
*ctf_trace
= NULL
;
1286 struct relay_viewer_stream
*metadata_viewer_stream
= NULL
;
1290 DBG("Viewer get next index");
1292 memset(&viewer_index
, 0, sizeof(viewer_index
));
1293 health_code_update();
1295 ret
= recv_request(conn
->sock
, &request_index
, sizeof(request_index
));
1299 health_code_update();
1301 vstream
= viewer_stream_get_by_id(be64toh(request_index
.stream_id
));
1303 DBG("Client requested index of unknown stream id %" PRIu64
,
1304 be64toh(request_index
.stream_id
));
1305 viewer_index
.status
= htobe32(LTTNG_VIEWER_INDEX_ERR
);
1309 /* Use back. ref. Protected by refcounts. */
1310 rstream
= vstream
->stream
;
1311 ctf_trace
= rstream
->trace
;
1313 /* metadata_viewer_stream may be NULL. */
1314 metadata_viewer_stream
=
1315 ctf_trace_get_viewer_metadata_stream(ctf_trace
);
1317 pthread_mutex_lock(&rstream
->lock
);
1320 * The viewer should not ask for index on metadata stream.
1322 if (rstream
->is_metadata
) {
1323 viewer_index
.status
= htobe32(LTTNG_VIEWER_INDEX_HUP
);
1327 /* Try to open an index if one is needed for that stream. */
1328 ret
= try_open_index(vstream
, rstream
);
1330 if (ret
== -ENOENT
) {
1332 * The index is created only when the first data
1333 * packet arrives, it might not be ready at the
1334 * beginning of the session
1336 viewer_index
.status
= htobe32(LTTNG_VIEWER_INDEX_RETRY
);
1338 /* Unhandled error. */
1339 viewer_index
.status
= htobe32(LTTNG_VIEWER_INDEX_ERR
);
1344 ret
= check_index_status(vstream
, rstream
, ctf_trace
, &viewer_index
);
1347 } else if (ret
== 1) {
1349 * We have no index to send and check_index_status has populated
1350 * viewer_index's status.
1354 /* At this point, ret is 0 thus we will be able to read the index. */
1358 * vstream->stream_fd may be NULL if it has been closed by
1359 * tracefile rotation, or if we are at the beginning of the
1360 * stream. We open the data stream file here to protect against
1361 * overwrite caused by tracefile rotation (in association with
1362 * unlink performed before overwrite).
1364 if (!vstream
->stream_fd
) {
1365 char fullpath
[PATH_MAX
];
1367 if (vstream
->stream
->tracefile_count
> 0) {
1368 ret
= snprintf(fullpath
, PATH_MAX
, "%s/%s_%" PRIu64
,
1370 vstream
->channel_name
,
1371 vstream
->current_tracefile_id
);
1373 ret
= snprintf(fullpath
, PATH_MAX
, "%s/%s",
1375 vstream
->channel_name
);
1380 ret
= open(fullpath
, O_RDONLY
);
1382 PERROR("Relay opening trace file");
1385 vstream
->stream_fd
= stream_fd_create(ret
);
1386 if (!vstream
->stream_fd
) {
1394 ret
= check_new_streams(conn
);
1396 viewer_index
.status
= htobe32(LTTNG_VIEWER_INDEX_ERR
);
1398 } else if (ret
== 1) {
1399 viewer_index
.flags
|= LTTNG_VIEWER_FLAG_NEW_STREAM
;
1402 read_ret
= lttng_read(vstream
->index_fd
->fd
, &packet_index
,
1403 sizeof(packet_index
));
1404 if (read_ret
< sizeof(packet_index
)) {
1405 ERR("Relay reading index file %d returned %zd",
1406 vstream
->index_fd
->fd
, read_ret
);
1407 viewer_index
.status
= htobe32(LTTNG_VIEWER_INDEX_ERR
);
1410 viewer_index
.status
= htobe32(LTTNG_VIEWER_INDEX_OK
);
1411 vstream
->index_sent_seqcount
++;
1415 * Indexes are stored in big endian, no need to switch before sending.
1417 DBG("Sending viewer index for stream %" PRIu64
" offset %" PRIu64
,
1418 rstream
->stream_handle
,
1419 be64toh(packet_index
.offset
));
1420 viewer_index
.offset
= packet_index
.offset
;
1421 viewer_index
.packet_size
= packet_index
.packet_size
;
1422 viewer_index
.content_size
= packet_index
.content_size
;
1423 viewer_index
.timestamp_begin
= packet_index
.timestamp_begin
;
1424 viewer_index
.timestamp_end
= packet_index
.timestamp_end
;
1425 viewer_index
.events_discarded
= packet_index
.events_discarded
;
1426 viewer_index
.stream_id
= packet_index
.stream_id
;
1430 pthread_mutex_unlock(&rstream
->lock
);
1433 if (metadata_viewer_stream
) {
1434 pthread_mutex_lock(&metadata_viewer_stream
->stream
->lock
);
1435 DBG("get next index metadata check: recv %" PRIu64
1437 metadata_viewer_stream
->stream
->metadata_received
,
1438 metadata_viewer_stream
->metadata_sent
);
1439 if (!metadata_viewer_stream
->stream
->metadata_received
||
1440 metadata_viewer_stream
->stream
->metadata_received
>
1441 metadata_viewer_stream
->metadata_sent
) {
1442 viewer_index
.flags
|= LTTNG_VIEWER_FLAG_NEW_METADATA
;
1444 pthread_mutex_unlock(&metadata_viewer_stream
->stream
->lock
);
1447 viewer_index
.flags
= htobe32(viewer_index
.flags
);
1448 health_code_update();
1450 ret
= send_response(conn
->sock
, &viewer_index
, sizeof(viewer_index
));
1454 health_code_update();
1457 DBG("Index %" PRIu64
" for stream %" PRIu64
" sent",
1458 vstream
->index_sent_seqcount
,
1459 vstream
->stream
->stream_handle
);
1462 if (metadata_viewer_stream
) {
1463 viewer_stream_put(metadata_viewer_stream
);
1466 viewer_stream_put(vstream
);
1471 pthread_mutex_unlock(&rstream
->lock
);
1472 if (metadata_viewer_stream
) {
1473 viewer_stream_put(metadata_viewer_stream
);
1475 viewer_stream_put(vstream
);
1480 * Send the next index for a stream
1482 * Return 0 on success or else a negative value.
1485 int viewer_get_packet(struct relay_connection
*conn
)
1487 int ret
, send_data
= 0;
1491 struct lttng_viewer_get_packet get_packet_info
;
1492 struct lttng_viewer_trace_packet reply
;
1493 struct relay_viewer_stream
*vstream
= NULL
;
1495 DBG2("Relay get data packet");
1497 health_code_update();
1499 ret
= recv_request(conn
->sock
, &get_packet_info
,
1500 sizeof(get_packet_info
));
1504 health_code_update();
1506 /* From this point on, the error label can be reached. */
1507 memset(&reply
, 0, sizeof(reply
));
1509 vstream
= viewer_stream_get_by_id(be64toh(get_packet_info
.stream_id
));
1511 DBG("Client requested packet of unknown stream id %" PRIu64
,
1512 be64toh(get_packet_info
.stream_id
));
1513 reply
.status
= htobe32(LTTNG_VIEWER_GET_PACKET_ERR
);
1514 goto send_reply_nolock
;
1517 pthread_mutex_lock(&vstream
->stream
->lock
);
1519 len
= be32toh(get_packet_info
.len
);
1520 data
= zmalloc(len
);
1522 PERROR("relay data zmalloc");
1526 ret
= lseek(vstream
->stream_fd
->fd
, be64toh(get_packet_info
.offset
),
1529 PERROR("lseek fd %d to offset %" PRIu64
, vstream
->stream_fd
->fd
,
1530 be64toh(get_packet_info
.offset
));
1533 read_len
= lttng_read(vstream
->stream_fd
->fd
, data
, len
);
1534 if (read_len
< len
) {
1535 PERROR("Relay reading trace file, fd: %d, offset: %" PRIu64
,
1536 vstream
->stream_fd
->fd
,
1537 be64toh(get_packet_info
.offset
));
1540 reply
.status
= htobe32(LTTNG_VIEWER_GET_PACKET_OK
);
1541 reply
.len
= htobe32(len
);
1546 reply
.status
= htobe32(LTTNG_VIEWER_GET_PACKET_ERR
);
1550 pthread_mutex_unlock(&vstream
->stream
->lock
);
1553 reply
.flags
= htobe32(reply
.flags
);
1555 health_code_update();
1557 ret
= send_response(conn
->sock
, &reply
, sizeof(reply
));
1561 health_code_update();
1564 health_code_update();
1565 ret
= send_response(conn
->sock
, data
, len
);
1569 health_code_update();
1572 DBG("Sent %u bytes for stream %" PRIu64
, len
,
1573 be64toh(get_packet_info
.stream_id
));
1579 viewer_stream_put(vstream
);
1585 * Send the session's metadata
1587 * Return 0 on success else a negative value.
1590 int viewer_get_metadata(struct relay_connection
*conn
)
1596 struct lttng_viewer_get_metadata request
;
1597 struct lttng_viewer_metadata_packet reply
;
1598 struct relay_viewer_stream
*vstream
= NULL
;
1602 DBG("Relay get metadata");
1604 health_code_update();
1606 ret
= recv_request(conn
->sock
, &request
, sizeof(request
));
1610 health_code_update();
1612 memset(&reply
, 0, sizeof(reply
));
1614 vstream
= viewer_stream_get_by_id(be64toh(request
.stream_id
));
1617 * The metadata stream can be closed by a CLOSE command
1618 * just before we attach. It can also be closed by
1619 * per-pid tracing during tracing. Therefore, it is
1620 * possible that we cannot find this viewer stream.
1621 * Reply back to the client with an error if we cannot
1624 DBG("Client requested metadata of unknown stream id %" PRIu64
,
1625 be64toh(request
.stream_id
));
1626 reply
.status
= htobe32(LTTNG_VIEWER_METADATA_ERR
);
1629 pthread_mutex_lock(&vstream
->stream
->lock
);
1630 if (!vstream
->stream
->is_metadata
) {
1631 ERR("Invalid metadata stream");
1635 assert(vstream
->metadata_sent
<= vstream
->stream
->metadata_received
);
1637 len
= vstream
->stream
->metadata_received
- vstream
->metadata_sent
;
1639 reply
.status
= htobe32(LTTNG_VIEWER_NO_NEW_METADATA
);
1643 /* first time, we open the metadata file */
1644 if (!vstream
->stream_fd
) {
1645 char fullpath
[PATH_MAX
];
1647 ret
= snprintf(fullpath
, PATH_MAX
, "%s/%s", vstream
->path_name
,
1648 vstream
->channel_name
);
1652 ret
= open(fullpath
, O_RDONLY
);
1654 PERROR("Relay opening metadata file");
1657 vstream
->stream_fd
= stream_fd_create(ret
);
1658 if (!vstream
->stream_fd
) {
1666 reply
.len
= htobe64(len
);
1667 data
= zmalloc(len
);
1669 PERROR("viewer metadata zmalloc");
1673 read_len
= lttng_read(vstream
->stream_fd
->fd
, data
, len
);
1674 if (read_len
< len
) {
1675 PERROR("Relay reading metadata file");
1678 vstream
->metadata_sent
+= read_len
;
1679 if (vstream
->metadata_sent
== vstream
->stream
->metadata_received
1680 && vstream
->stream
->closed
) {
1681 /* Release ownership for the viewer metadata stream. */
1682 viewer_stream_put(vstream
);
1685 reply
.status
= htobe32(LTTNG_VIEWER_METADATA_OK
);
1690 reply
.status
= htobe32(LTTNG_VIEWER_METADATA_ERR
);
1693 health_code_update();
1695 pthread_mutex_unlock(&vstream
->stream
->lock
);
1697 ret
= send_response(conn
->sock
, &reply
, sizeof(reply
));
1701 health_code_update();
1704 ret
= send_response(conn
->sock
, data
, len
);
1710 DBG("Sent %" PRIu64
" bytes of metadata for stream %" PRIu64
, len
,
1711 be64toh(request
.stream_id
));
1713 DBG("Metadata sent");
1719 viewer_stream_put(vstream
);
1725 * Create a viewer session.
1727 * Return 0 on success or else a negative value.
1730 int viewer_create_session(struct relay_connection
*conn
)
1733 struct lttng_viewer_create_session_response resp
;
1735 DBG("Viewer create session received");
1737 memset(&resp
, 0, sizeof(resp
));
1738 resp
.status
= htobe32(LTTNG_VIEWER_CREATE_SESSION_OK
);
1739 conn
->viewer_session
= viewer_session_create();
1740 if (!conn
->viewer_session
) {
1741 ERR("Allocation viewer session");
1742 resp
.status
= htobe32(LTTNG_VIEWER_CREATE_SESSION_ERR
);
1747 health_code_update();
1748 ret
= send_response(conn
->sock
, &resp
, sizeof(resp
));
1752 health_code_update();
1761 * live_relay_unknown_command: send -1 if received unknown command
1764 void live_relay_unknown_command(struct relay_connection
*conn
)
1766 struct lttcomm_relayd_generic_reply reply
;
1768 memset(&reply
, 0, sizeof(reply
));
1769 reply
.ret_code
= htobe32(LTTNG_ERR_UNK
);
1770 (void) send_response(conn
->sock
, &reply
, sizeof(reply
));
1774 * Process the commands received on the control socket
1777 int process_control(struct lttng_viewer_cmd
*recv_hdr
,
1778 struct relay_connection
*conn
)
1783 msg_value
= be32toh(recv_hdr
->cmd
);
1786 * Make sure we've done the version check before any command other then a
1787 * new client connection.
1789 if (msg_value
!= LTTNG_VIEWER_CONNECT
&& !conn
->version_check_done
) {
1790 ERR("Viewer conn value %" PRIu32
" before version check", msg_value
);
1795 switch (msg_value
) {
1796 case LTTNG_VIEWER_CONNECT
:
1797 ret
= viewer_connect(conn
);
1799 case LTTNG_VIEWER_LIST_SESSIONS
:
1800 ret
= viewer_list_sessions(conn
);
1802 case LTTNG_VIEWER_ATTACH_SESSION
:
1803 ret
= viewer_attach_session(conn
);
1805 case LTTNG_VIEWER_GET_NEXT_INDEX
:
1806 ret
= viewer_get_next_index(conn
);
1808 case LTTNG_VIEWER_GET_PACKET
:
1809 ret
= viewer_get_packet(conn
);
1811 case LTTNG_VIEWER_GET_METADATA
:
1812 ret
= viewer_get_metadata(conn
);
1814 case LTTNG_VIEWER_GET_NEW_STREAMS
:
1815 ret
= viewer_get_new_streams(conn
);
1817 case LTTNG_VIEWER_CREATE_SESSION
:
1818 ret
= viewer_create_session(conn
);
1821 ERR("Received unknown viewer command (%u)",
1822 be32toh(recv_hdr
->cmd
));
1823 live_relay_unknown_command(conn
);
1833 void cleanup_connection_pollfd(struct lttng_poll_event
*events
, int pollfd
)
1837 (void) lttng_poll_del(events
, pollfd
);
1839 ret
= close(pollfd
);
1841 ERR("Closing pollfd %d", pollfd
);
1846 * This thread does the actual work
1849 void *thread_worker(void *data
)
1853 struct lttng_poll_event events
;
1854 struct lttng_ht
*viewer_connections_ht
;
1855 struct lttng_ht_iter iter
;
1856 struct lttng_viewer_cmd recv_hdr
;
1857 struct relay_connection
*destroy_conn
;
1859 DBG("[thread] Live viewer relay worker started");
1861 rcu_register_thread();
1863 health_register(health_relayd
, HEALTH_RELAYD_TYPE_LIVE_WORKER
);
1865 if (testpoint(relayd_thread_live_worker
)) {
1866 goto error_testpoint
;
1869 /* table of connections indexed on socket */
1870 viewer_connections_ht
= lttng_ht_new(0, LTTNG_HT_TYPE_ULONG
);
1871 if (!viewer_connections_ht
) {
1872 goto viewer_connections_ht_error
;
1875 ret
= create_thread_poll_set(&events
, 2);
1877 goto error_poll_create
;
1880 ret
= lttng_poll_add(&events
, live_conn_pipe
[0], LPOLLIN
| LPOLLRDHUP
);
1889 health_code_update();
1891 /* Infinite blocking call, waiting for transmission */
1892 DBG3("Relayd live viewer worker thread polling...");
1893 health_poll_entry();
1894 ret
= lttng_poll_wait(&events
, -1);
1898 * Restart interrupted system call.
1900 if (errno
== EINTR
) {
1909 * Process control. The control connection is prioritised so we don't
1910 * starve it with high throughput tracing data on the data
1913 for (i
= 0; i
< nb_fd
; i
++) {
1914 /* Fetch once the poll data */
1915 uint32_t revents
= LTTNG_POLL_GETEV(&events
, i
);
1916 int pollfd
= LTTNG_POLL_GETFD(&events
, i
);
1918 health_code_update();
1921 /* No activity for this FD (poll implementation). */
1925 /* Thread quit pipe has been closed. Killing thread. */
1926 ret
= check_thread_quit_pipe(pollfd
, revents
);
1932 /* Inspect the relay conn pipe for new connection. */
1933 if (pollfd
== live_conn_pipe
[0]) {
1934 if (revents
& LPOLLIN
) {
1935 struct relay_connection
*conn
;
1937 ret
= lttng_read(live_conn_pipe
[0],
1938 &conn
, sizeof(conn
));
1942 lttng_poll_add(&events
, conn
->sock
->fd
,
1943 LPOLLIN
| LPOLLRDHUP
);
1944 connection_ht_add(viewer_connections_ht
, conn
);
1945 DBG("Connection socket %d added to poll", conn
->sock
->fd
);
1946 } else if (revents
& (LPOLLERR
| LPOLLHUP
| LPOLLRDHUP
)) {
1947 ERR("Relay live pipe error");
1950 ERR("Unexpected poll events %u for sock %d", revents
, pollfd
);
1954 /* Connection activity. */
1955 struct relay_connection
*conn
;
1957 conn
= connection_get_by_sock(viewer_connections_ht
, pollfd
);
1962 if (revents
& LPOLLIN
) {
1963 ret
= conn
->sock
->ops
->recvmsg(conn
->sock
, &recv_hdr
,
1964 sizeof(recv_hdr
), 0);
1966 /* Connection closed. */
1967 cleanup_connection_pollfd(&events
, pollfd
);
1968 /* Put "create" ownership reference. */
1969 connection_put(conn
);
1970 DBG("Viewer control conn closed with %d", pollfd
);
1972 ret
= process_control(&recv_hdr
, conn
);
1974 /* Clear the session on error. */
1975 cleanup_connection_pollfd(&events
, pollfd
);
1976 /* Put "create" ownership reference. */
1977 connection_put(conn
);
1978 DBG("Viewer connection closed with %d", pollfd
);
1981 } else if (revents
& (LPOLLERR
| LPOLLHUP
| LPOLLRDHUP
)) {
1982 cleanup_connection_pollfd(&events
, pollfd
);
1983 /* Put "create" ownership reference. */
1984 connection_put(conn
);
1986 ERR("Unexpected poll events %u for sock %d", revents
, pollfd
);
1987 connection_put(conn
);
1990 /* Put local "get_by_sock" reference. */
1991 connection_put(conn
);
1998 lttng_poll_clean(&events
);
2000 /* Cleanup reamaining connection object. */
2002 cds_lfht_for_each_entry(viewer_connections_ht
->ht
, &iter
.iter
,
2005 health_code_update();
2006 connection_put(destroy_conn
);
2010 lttng_ht_destroy(viewer_connections_ht
);
2011 viewer_connections_ht_error
:
2012 /* Close relay conn pipes */
2013 utils_close_pipe(live_conn_pipe
);
2015 DBG("Viewer worker thread exited with error");
2017 DBG("Viewer worker thread cleanup complete");
2021 ERR("Health error occurred in %s", __func__
);
2023 health_unregister(health_relayd
);
2024 if (lttng_relay_stop_threads()) {
2025 ERR("Error stopping threads");
2027 rcu_unregister_thread();
2032 * Create the relay command pipe to wake thread_manage_apps.
2033 * Closed in cleanup().
2035 static int create_conn_pipe(void)
2037 return utils_create_pipe_cloexec(live_conn_pipe
);
2040 int relayd_live_join(void)
2042 int ret
, retval
= 0;
2045 ret
= pthread_join(live_listener_thread
, &status
);
2048 PERROR("pthread_join live listener");
2052 ret
= pthread_join(live_worker_thread
, &status
);
2055 PERROR("pthread_join live worker");
2059 ret
= pthread_join(live_dispatcher_thread
, &status
);
2062 PERROR("pthread_join live dispatcher");
2066 cleanup_relayd_live();
2074 int relayd_live_create(struct lttng_uri
*uri
)
2076 int ret
= 0, retval
= 0;
2082 goto exit_init_data
;
2086 /* Check if daemon is UID = 0 */
2087 is_root
= !getuid();
2090 if (live_uri
->port
< 1024) {
2091 ERR("Need to be root to use ports < 1024");
2093 goto exit_init_data
;
2097 /* Setup the thread apps communication pipe. */
2098 if (create_conn_pipe()) {
2100 goto exit_init_data
;
2103 /* Init relay command queue. */
2104 cds_wfcq_init(&viewer_conn_queue
.head
, &viewer_conn_queue
.tail
);
2106 /* Set up max poll set size */
2107 if (lttng_poll_set_max_size()) {
2109 goto exit_init_data
;
2112 /* Setup the dispatcher thread */
2113 ret
= pthread_create(&live_dispatcher_thread
, NULL
,
2114 thread_dispatcher
, (void *) NULL
);
2117 PERROR("pthread_create viewer dispatcher");
2119 goto exit_dispatcher_thread
;
2122 /* Setup the worker thread */
2123 ret
= pthread_create(&live_worker_thread
, NULL
,
2124 thread_worker
, NULL
);
2127 PERROR("pthread_create viewer worker");
2129 goto exit_worker_thread
;
2132 /* Setup the listener thread */
2133 ret
= pthread_create(&live_listener_thread
, NULL
,
2134 thread_listener
, (void *) NULL
);
2137 PERROR("pthread_create viewer listener");
2139 goto exit_listener_thread
;
2143 * All OK, started all threads.
2148 * Join on the live_listener_thread should anything be added after
2149 * the live_listener thread's creation.
2152 exit_listener_thread
:
2154 ret
= pthread_join(live_worker_thread
, &status
);
2157 PERROR("pthread_join live worker");
2162 ret
= pthread_join(live_dispatcher_thread
, &status
);
2165 PERROR("pthread_join live dispatcher");
2168 exit_dispatcher_thread
:
2171 cleanup_relayd_live();