fbc70303106e72bd3aefd8be55d985bbd0a8f229
[lttng-tools.git] / src / bin / lttng-relayd / live.c
1 /*
2 * Copyright (C) 2013 - Julien Desfossez <jdesfossez@efficios.com>
3 * David Goulet <dgoulet@efficios.com>
4 * 2015 - Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
5 *
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.
9 *
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
13 * more details.
14 *
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.
18 */
19
20 #define _LGPL_SOURCE
21 #include <getopt.h>
22 #include <grp.h>
23 #include <limits.h>
24 #include <pthread.h>
25 #include <signal.h>
26 #include <stdio.h>
27 #include <stdlib.h>
28 #include <string.h>
29 #include <sys/mman.h>
30 #include <sys/mount.h>
31 #include <sys/resource.h>
32 #include <sys/socket.h>
33 #include <sys/stat.h>
34 #include <sys/types.h>
35 #include <sys/wait.h>
36 #include <inttypes.h>
37 #include <urcu/futex.h>
38 #include <urcu/uatomic.h>
39 #include <urcu/rculist.h>
40 #include <unistd.h>
41 #include <fcntl.h>
42
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>
56 #include <common/fd-tracker/utils.h>
57
58 #include "cmd.h"
59 #include "live.h"
60 #include "lttng-relayd.h"
61 #include "utils.h"
62 #include "health-relayd.h"
63 #include "testpoint.h"
64 #include "viewer-stream.h"
65 #include "stream.h"
66 #include "session.h"
67 #include "ctf-trace.h"
68 #include "connection.h"
69 #include "viewer-session.h"
70
71 #define SESSION_BUF_DEFAULT_COUNT 16
72
73 static struct lttng_uri *live_uri;
74
75 /*
76 * This pipe is used to inform the worker thread that a command is queued and
77 * ready to be processed.
78 */
79 static int live_conn_pipe[2] = { -1, -1 };
80
81 /* Shared between threads */
82 static int live_dispatch_thread_exit;
83
84 static pthread_t live_listener_thread;
85 static pthread_t live_dispatcher_thread;
86 static pthread_t live_worker_thread;
87
88 /*
89 * Relay command queue.
90 *
91 * The live_thread_listener and live_thread_dispatcher communicate with this
92 * queue.
93 */
94 static struct relay_conn_queue viewer_conn_queue;
95
96 static uint64_t last_relay_viewer_session_id;
97 static pthread_mutex_t last_relay_viewer_session_id_lock =
98 PTHREAD_MUTEX_INITIALIZER;
99
100 /*
101 * Cleanup the daemon
102 */
103 static
104 void cleanup_relayd_live(void)
105 {
106 DBG("Cleaning up");
107
108 free(live_uri);
109 }
110
111 /*
112 * Receive a request buffer using a given socket, destination allocated buffer
113 * of length size.
114 *
115 * Return the size of the received message or else a negative value on error
116 * with errno being set by recvmsg() syscall.
117 */
118 static
119 ssize_t recv_request(struct lttcomm_sock *sock, void *buf, size_t size)
120 {
121 ssize_t ret;
122
123 ret = sock->ops->recvmsg(sock, buf, size, 0);
124 if (ret < 0 || ret != size) {
125 if (ret == 0) {
126 /* Orderly shutdown. Not necessary to print an error. */
127 DBG("Socket %d did an orderly shutdown", sock->fd);
128 } else {
129 ERR("Relay failed to receive request.");
130 }
131 ret = -1;
132 }
133
134 return ret;
135 }
136
137 /*
138 * Send a response buffer using a given socket, source allocated buffer of
139 * length size.
140 *
141 * Return the size of the sent message or else a negative value on error with
142 * errno being set by sendmsg() syscall.
143 */
144 static
145 ssize_t send_response(struct lttcomm_sock *sock, void *buf, size_t size)
146 {
147 ssize_t ret;
148
149 ret = sock->ops->sendmsg(sock, buf, size, 0);
150 if (ret < 0) {
151 ERR("Relayd failed to send response.");
152 }
153
154 return ret;
155 }
156
157 /*
158 * Atomically check if new streams got added in one of the sessions attached
159 * and reset the flag to 0.
160 *
161 * Returns 1 if new streams got added, 0 if nothing changed, a negative value
162 * on error.
163 */
164 static
165 int check_new_streams(struct relay_connection *conn)
166 {
167 struct relay_session *session;
168 unsigned long current_val;
169 int ret = 0;
170
171 if (!conn->viewer_session) {
172 goto end;
173 }
174 rcu_read_lock();
175 cds_list_for_each_entry_rcu(session,
176 &conn->viewer_session->session_list,
177 viewer_session_node) {
178 if (!session_get(session)) {
179 continue;
180 }
181 current_val = uatomic_cmpxchg(&session->new_streams, 1, 0);
182 ret = current_val;
183 session_put(session);
184 if (ret == 1) {
185 goto end;
186 }
187 }
188 end:
189 rcu_read_unlock();
190 return ret;
191 }
192
193 /*
194 * Send viewer streams to the given socket. The ignore_sent_flag indicates if
195 * this function should ignore the sent flag or not.
196 *
197 * Return 0 on success or else a negative value.
198 */
199 static
200 ssize_t send_viewer_streams(struct lttcomm_sock *sock,
201 uint64_t session_id, unsigned int ignore_sent_flag)
202 {
203 ssize_t ret;
204 struct lttng_viewer_stream send_stream;
205 struct lttng_ht_iter iter;
206 struct relay_viewer_stream *vstream;
207
208 rcu_read_lock();
209
210 cds_lfht_for_each_entry(viewer_streams_ht->ht, &iter.iter, vstream,
211 stream_n.node) {
212 struct ctf_trace *ctf_trace;
213
214 health_code_update();
215
216 if (!viewer_stream_get(vstream)) {
217 continue;
218 }
219
220 pthread_mutex_lock(&vstream->stream->lock);
221 /* Ignore if not the same session. */
222 if (vstream->stream->trace->session->id != session_id ||
223 (!ignore_sent_flag && vstream->sent_flag)) {
224 pthread_mutex_unlock(&vstream->stream->lock);
225 viewer_stream_put(vstream);
226 continue;
227 }
228
229 ctf_trace = vstream->stream->trace;
230 send_stream.id = htobe64(vstream->stream->stream_handle);
231 send_stream.ctf_trace_id = htobe64(ctf_trace->id);
232 send_stream.metadata_flag = htobe32(
233 vstream->stream->is_metadata);
234 if (lttng_strncpy(send_stream.path_name, vstream->path_name,
235 sizeof(send_stream.path_name))) {
236 pthread_mutex_unlock(&vstream->stream->lock);
237 viewer_stream_put(vstream);
238 ret = -1; /* Error. */
239 goto end_unlock;
240 }
241 if (lttng_strncpy(send_stream.channel_name,
242 vstream->channel_name,
243 sizeof(send_stream.channel_name))) {
244 pthread_mutex_unlock(&vstream->stream->lock);
245 viewer_stream_put(vstream);
246 ret = -1; /* Error. */
247 goto end_unlock;
248 }
249
250 DBG("Sending stream %" PRIu64 " to viewer",
251 vstream->stream->stream_handle);
252 vstream->sent_flag = 1;
253 pthread_mutex_unlock(&vstream->stream->lock);
254
255 ret = send_response(sock, &send_stream, sizeof(send_stream));
256 viewer_stream_put(vstream);
257 if (ret < 0) {
258 goto end_unlock;
259 }
260 }
261
262 ret = 0;
263
264 end_unlock:
265 rcu_read_unlock();
266 return ret;
267 }
268
269 /*
270 * Create every viewer stream possible for the given session with the seek
271 * type. Three counters *can* be return which are in order the total amount of
272 * viewer stream of the session, the number of unsent stream and the number of
273 * stream created. Those counters can be NULL and thus will be ignored.
274 *
275 * session must be locked to ensure that we see either none or all initial
276 * streams for a session, but no intermediate state..
277 *
278 * Return 0 on success or else a negative value.
279 */
280 static int make_viewer_streams(struct relay_session *session,
281 struct lttng_trace_chunk *viewer_trace_chunk,
282 enum lttng_viewer_seek seek_t,
283 uint32_t *nb_total,
284 uint32_t *nb_unsent,
285 uint32_t *nb_created,
286 bool *closed)
287 {
288 int ret;
289 struct lttng_ht_iter iter;
290 struct ctf_trace *ctf_trace;
291
292 assert(session);
293 ASSERT_LOCKED(session->lock);
294
295 if (!viewer_trace_chunk) {
296 ERR("Internal error: viewer session associated with session \"%s\" has a NULL trace chunk",
297 session->session_name);
298 ret = -1;
299 goto error;
300 }
301
302 if (session->connection_closed) {
303 *closed = true;
304 }
305
306 /*
307 * Create viewer streams for relay streams that are ready to be
308 * used for a the given session id only.
309 */
310 rcu_read_lock();
311 cds_lfht_for_each_entry(session->ctf_traces_ht->ht, &iter.iter, ctf_trace,
312 node.node) {
313 bool trace_has_metadata_stream = false;
314 struct relay_stream *stream;
315
316 health_code_update();
317
318 if (!ctf_trace_get(ctf_trace)) {
319 continue;
320 }
321
322 /*
323 * Iterate over all the streams of the trace to see if we have a
324 * metadata stream.
325 */
326 cds_list_for_each_entry_rcu(
327 stream, &ctf_trace->stream_list, stream_node)
328 {
329 if (stream->is_metadata) {
330 trace_has_metadata_stream = true;
331 break;
332 }
333 }
334
335 /*
336 * If there is no metadata stream in this trace at the moment
337 * and we never sent one to the viewer, skip the trace. We
338 * accept that the viewer will not see this trace at all.
339 */
340 if (!trace_has_metadata_stream &&
341 !ctf_trace->metadata_stream_sent_to_viewer) {
342 ctf_trace_put(ctf_trace);
343 continue;
344 }
345
346 cds_list_for_each_entry_rcu(stream, &ctf_trace->stream_list, stream_node) {
347 struct relay_viewer_stream *vstream;
348
349 if (!stream_get(stream)) {
350 continue;
351 }
352 /*
353 * stream published is protected by the session lock.
354 */
355 if (!stream->published) {
356 goto next;
357 }
358 vstream = viewer_stream_get_by_id(stream->stream_handle);
359 if (!vstream) {
360 /*
361 * Save that we sent the metadata stream to the
362 * viewer. So that we know what trace the viewer
363 * is aware of.
364 */
365 if (stream->is_metadata) {
366 ctf_trace->metadata_stream_sent_to_viewer =
367 true;
368 }
369 vstream = viewer_stream_create(stream,
370 viewer_trace_chunk, seek_t);
371 if (!vstream) {
372 ret = -1;
373 ctf_trace_put(ctf_trace);
374 stream_put(stream);
375 goto error_unlock;
376 }
377
378 if (nb_created) {
379 /* Update number of created stream counter. */
380 (*nb_created)++;
381 }
382 /*
383 * Ensure a self-reference is preserved even
384 * after we have put our local reference.
385 */
386 if (!viewer_stream_get(vstream)) {
387 ERR("Unable to get self-reference on viewer stream, logic error.");
388 abort();
389 }
390 } else {
391 if (!vstream->sent_flag && nb_unsent) {
392 /* Update number of unsent stream counter. */
393 (*nb_unsent)++;
394 }
395 }
396 /* Update number of total stream counter. */
397 if (nb_total) {
398 if (stream->is_metadata) {
399 if (!stream->closed ||
400 stream->metadata_received > vstream->metadata_sent) {
401 (*nb_total)++;
402 }
403 } else {
404 if (!stream->closed ||
405 !(((int64_t) (stream->prev_data_seq - stream->last_net_seq_num)) >= 0)) {
406
407 (*nb_total)++;
408 }
409 }
410 }
411 /* Put local reference. */
412 viewer_stream_put(vstream);
413 next:
414 stream_put(stream);
415 }
416 ctf_trace_put(ctf_trace);
417 }
418
419 ret = 0;
420
421 error_unlock:
422 rcu_read_unlock();
423 error:
424 return ret;
425 }
426
427 int relayd_live_stop(void)
428 {
429 /* Stop dispatch thread */
430 CMM_STORE_SHARED(live_dispatch_thread_exit, 1);
431 futex_nto1_wake(&viewer_conn_queue.futex);
432 return 0;
433 }
434
435 /*
436 * Create a poll set with O_CLOEXEC and add the thread quit pipe to the set.
437 */
438 static
439 int create_thread_poll_set(struct lttng_poll_event *events, int size)
440 {
441 int ret;
442
443 if (events == NULL || size == 0) {
444 ret = -1;
445 goto error;
446 }
447
448 ret = lttng_poll_create(events, size, LTTNG_CLOEXEC);
449 if (ret < 0) {
450 goto error;
451 }
452
453 /* Add quit pipe */
454 ret = lttng_poll_add(events, thread_quit_pipe[0], LPOLLIN | LPOLLERR);
455 if (ret < 0) {
456 goto error;
457 }
458
459 return 0;
460
461 error:
462 return ret;
463 }
464
465 /*
466 * Check if the thread quit pipe was triggered.
467 *
468 * Return 1 if it was triggered else 0;
469 */
470 static
471 int check_thread_quit_pipe(int fd, uint32_t events)
472 {
473 if (fd == thread_quit_pipe[0] && (events & LPOLLIN)) {
474 return 1;
475 }
476
477 return 0;
478 }
479
480 /*
481 * Create and init socket from uri.
482 */
483 static
484 struct lttcomm_sock *init_socket(struct lttng_uri *uri)
485 {
486 int ret;
487 struct lttcomm_sock *sock = NULL;
488
489 sock = lttcomm_alloc_sock_from_uri(uri);
490 if (sock == NULL) {
491 ERR("Allocating socket");
492 goto error;
493 }
494
495 ret = lttcomm_create_sock(sock);
496 if (ret < 0) {
497 goto error;
498 }
499 DBG("Listening on sock %d for lttng-live", sock->fd);
500
501 ret = sock->ops->bind(sock);
502 if (ret < 0) {
503 PERROR("Failed to bind lttng-live socket");
504 goto error;
505 }
506
507 ret = sock->ops->listen(sock, -1);
508 if (ret < 0) {
509 goto error;
510
511 }
512
513 return sock;
514
515 error:
516 if (sock) {
517 lttcomm_destroy_sock(sock);
518 }
519 return NULL;
520 }
521
522 /*
523 * This thread manages the listening for new connections on the network
524 */
525 static
526 void *thread_listener(void *data)
527 {
528 int i, ret, pollfd, err = -1;
529 uint32_t revents, nb_fd;
530 struct lttng_poll_event events;
531 struct lttcomm_sock *live_control_sock;
532
533 DBG("[thread] Relay live listener started");
534
535 health_register(health_relayd, HEALTH_RELAYD_TYPE_LIVE_LISTENER);
536
537 health_code_update();
538
539 live_control_sock = init_socket(live_uri);
540 if (!live_control_sock) {
541 goto error_sock_control;
542 }
543
544 /* Pass 2 as size here for the thread quit pipe and control sockets. */
545 ret = create_thread_poll_set(&events, 2);
546 if (ret < 0) {
547 goto error_create_poll;
548 }
549
550 /* Add the control socket */
551 ret = lttng_poll_add(&events, live_control_sock->fd, LPOLLIN | LPOLLRDHUP);
552 if (ret < 0) {
553 goto error_poll_add;
554 }
555
556 lttng_relay_notify_ready();
557
558 if (testpoint(relayd_thread_live_listener)) {
559 goto error_testpoint;
560 }
561
562 while (1) {
563 health_code_update();
564
565 DBG("Listener accepting live viewers connections");
566
567 restart:
568 health_poll_entry();
569 ret = lttng_poll_wait(&events, -1);
570 health_poll_exit();
571 if (ret < 0) {
572 /*
573 * Restart interrupted system call.
574 */
575 if (errno == EINTR) {
576 goto restart;
577 }
578 goto error;
579 }
580 nb_fd = ret;
581
582 DBG("Relay new viewer connection received");
583 for (i = 0; i < nb_fd; i++) {
584 health_code_update();
585
586 /* Fetch once the poll data */
587 revents = LTTNG_POLL_GETEV(&events, i);
588 pollfd = LTTNG_POLL_GETFD(&events, i);
589
590 /* Thread quit pipe has been closed. Killing thread. */
591 ret = check_thread_quit_pipe(pollfd, revents);
592 if (ret) {
593 err = 0;
594 goto exit;
595 }
596
597 if (revents & LPOLLIN) {
598 /*
599 * A new connection is requested, therefore a
600 * viewer connection is allocated in this
601 * thread, enqueued to a global queue and
602 * dequeued (and freed) in the worker thread.
603 */
604 int val = 1;
605 struct relay_connection *new_conn;
606 struct lttcomm_sock *newsock;
607
608 newsock = live_control_sock->ops->accept(live_control_sock);
609 if (!newsock) {
610 PERROR("accepting control sock");
611 goto error;
612 }
613 DBG("Relay viewer connection accepted socket %d", newsock->fd);
614
615 ret = setsockopt(newsock->fd, SOL_SOCKET, SO_REUSEADDR, &val,
616 sizeof(val));
617 if (ret < 0) {
618 PERROR("setsockopt inet");
619 lttcomm_destroy_sock(newsock);
620 goto error;
621 }
622 new_conn = connection_create(newsock, RELAY_CONNECTION_UNKNOWN);
623 if (!new_conn) {
624 lttcomm_destroy_sock(newsock);
625 goto error;
626 }
627 /* Ownership assumed by the connection. */
628 newsock = NULL;
629
630 /* Enqueue request for the dispatcher thread. */
631 cds_wfcq_enqueue(&viewer_conn_queue.head, &viewer_conn_queue.tail,
632 &new_conn->qnode);
633
634 /*
635 * Wake the dispatch queue futex.
636 * Implicit memory barrier with the
637 * exchange in cds_wfcq_enqueue.
638 */
639 futex_nto1_wake(&viewer_conn_queue.futex);
640 } else if (revents & (LPOLLERR | LPOLLHUP | LPOLLRDHUP)) {
641 ERR("socket poll error");
642 goto error;
643 } else {
644 ERR("Unexpected poll events %u for sock %d", revents, pollfd);
645 goto error;
646 }
647 }
648 }
649
650 exit:
651 error:
652 error_poll_add:
653 error_testpoint:
654 lttng_poll_clean(&events);
655 error_create_poll:
656 if (live_control_sock->fd >= 0) {
657 ret = live_control_sock->ops->close(live_control_sock);
658 if (ret) {
659 PERROR("close");
660 }
661 }
662 lttcomm_destroy_sock(live_control_sock);
663 error_sock_control:
664 if (err) {
665 health_error();
666 DBG("Live viewer listener thread exited with error");
667 }
668 health_unregister(health_relayd);
669 DBG("Live viewer listener thread cleanup complete");
670 if (lttng_relay_stop_threads()) {
671 ERR("Error stopping threads");
672 }
673 return NULL;
674 }
675
676 /*
677 * This thread manages the dispatching of the requests to worker threads
678 */
679 static
680 void *thread_dispatcher(void *data)
681 {
682 int err = -1;
683 ssize_t ret;
684 struct cds_wfcq_node *node;
685 struct relay_connection *conn = NULL;
686
687 DBG("[thread] Live viewer relay dispatcher started");
688
689 health_register(health_relayd, HEALTH_RELAYD_TYPE_LIVE_DISPATCHER);
690
691 if (testpoint(relayd_thread_live_dispatcher)) {
692 goto error_testpoint;
693 }
694
695 health_code_update();
696
697 for (;;) {
698 health_code_update();
699
700 /* Atomically prepare the queue futex */
701 futex_nto1_prepare(&viewer_conn_queue.futex);
702
703 if (CMM_LOAD_SHARED(live_dispatch_thread_exit)) {
704 break;
705 }
706
707 do {
708 health_code_update();
709
710 /* Dequeue commands */
711 node = cds_wfcq_dequeue_blocking(&viewer_conn_queue.head,
712 &viewer_conn_queue.tail);
713 if (node == NULL) {
714 DBG("Woken up but nothing in the live-viewer "
715 "relay command queue");
716 /* Continue thread execution */
717 break;
718 }
719 conn = caa_container_of(node, struct relay_connection, qnode);
720 DBG("Dispatching viewer request waiting on sock %d",
721 conn->sock->fd);
722
723 /*
724 * Inform worker thread of the new request. This
725 * call is blocking so we can be assured that
726 * the data will be read at some point in time
727 * or wait to the end of the world :)
728 */
729 ret = lttng_write(live_conn_pipe[1], &conn, sizeof(conn));
730 if (ret < 0) {
731 PERROR("write conn pipe");
732 connection_put(conn);
733 goto error;
734 }
735 } while (node != NULL);
736
737 /* Futex wait on queue. Blocking call on futex() */
738 health_poll_entry();
739 futex_nto1_wait(&viewer_conn_queue.futex);
740 health_poll_exit();
741 }
742
743 /* Normal exit, no error */
744 err = 0;
745
746 error:
747 error_testpoint:
748 if (err) {
749 health_error();
750 ERR("Health error occurred in %s", __func__);
751 }
752 health_unregister(health_relayd);
753 DBG("Live viewer dispatch thread dying");
754 if (lttng_relay_stop_threads()) {
755 ERR("Error stopping threads");
756 }
757 return NULL;
758 }
759
760 /*
761 * Establish connection with the viewer and check the versions.
762 *
763 * Return 0 on success or else negative value.
764 */
765 static
766 int viewer_connect(struct relay_connection *conn)
767 {
768 int ret;
769 struct lttng_viewer_connect reply, msg;
770
771 conn->version_check_done = 1;
772
773 health_code_update();
774
775 DBG("Viewer is establishing a connection to the relayd.");
776
777 ret = recv_request(conn->sock, &msg, sizeof(msg));
778 if (ret < 0) {
779 goto end;
780 }
781
782 health_code_update();
783
784 memset(&reply, 0, sizeof(reply));
785 reply.major = RELAYD_VERSION_COMM_MAJOR;
786 reply.minor = RELAYD_VERSION_COMM_MINOR;
787
788 /* Major versions must be the same */
789 if (reply.major != be32toh(msg.major)) {
790 DBG("Incompatible major versions ([relayd] %u vs [client] %u)",
791 reply.major, be32toh(msg.major));
792 ret = -1;
793 goto end;
794 }
795
796 conn->major = reply.major;
797 /* We adapt to the lowest compatible version */
798 if (reply.minor <= be32toh(msg.minor)) {
799 conn->minor = reply.minor;
800 } else {
801 conn->minor = be32toh(msg.minor);
802 }
803
804 if (be32toh(msg.type) == LTTNG_VIEWER_CLIENT_COMMAND) {
805 conn->type = RELAY_VIEWER_COMMAND;
806 } else if (be32toh(msg.type) == LTTNG_VIEWER_CLIENT_NOTIFICATION) {
807 conn->type = RELAY_VIEWER_NOTIFICATION;
808 } else {
809 ERR("Unknown connection type : %u", be32toh(msg.type));
810 ret = -1;
811 goto end;
812 }
813
814 reply.major = htobe32(reply.major);
815 reply.minor = htobe32(reply.minor);
816 if (conn->type == RELAY_VIEWER_COMMAND) {
817 /*
818 * Increment outside of htobe64 macro, because the argument can
819 * be used more than once within the macro, and thus the
820 * operation may be undefined.
821 */
822 pthread_mutex_lock(&last_relay_viewer_session_id_lock);
823 last_relay_viewer_session_id++;
824 pthread_mutex_unlock(&last_relay_viewer_session_id_lock);
825 reply.viewer_session_id = htobe64(last_relay_viewer_session_id);
826 }
827
828 health_code_update();
829
830 ret = send_response(conn->sock, &reply, sizeof(reply));
831 if (ret < 0) {
832 goto end;
833 }
834
835 health_code_update();
836
837 DBG("Version check done using protocol %u.%u", conn->major, conn->minor);
838 ret = 0;
839
840 end:
841 return ret;
842 }
843
844 /*
845 * Send the viewer the list of current sessions.
846 * We need to create a copy of the hash table content because otherwise
847 * we cannot assume the number of entries stays the same between getting
848 * the number of HT elements and iteration over the HT.
849 *
850 * Return 0 on success or else a negative value.
851 */
852 static
853 int viewer_list_sessions(struct relay_connection *conn)
854 {
855 int ret = 0;
856 struct lttng_viewer_list_sessions session_list;
857 struct lttng_ht_iter iter;
858 struct relay_session *session;
859 struct lttng_viewer_session *send_session_buf = NULL;
860 uint32_t buf_count = SESSION_BUF_DEFAULT_COUNT;
861 uint32_t count = 0;
862
863 DBG("List sessions received");
864
865 send_session_buf = zmalloc(SESSION_BUF_DEFAULT_COUNT * sizeof(*send_session_buf));
866 if (!send_session_buf) {
867 return -1;
868 }
869
870 rcu_read_lock();
871 cds_lfht_for_each_entry(sessions_ht->ht, &iter.iter, session,
872 session_n.node) {
873 struct lttng_viewer_session *send_session;
874
875 health_code_update();
876
877 pthread_mutex_lock(&session->lock);
878 if (session->connection_closed) {
879 /* Skip closed session */
880 goto next_session;
881 }
882 if (!session->current_trace_chunk) {
883 /*
884 * Skip un-attachable session. It is either
885 * being destroyed or has not had a trace
886 * chunk created against it yet.
887 */
888 goto next_session;
889 }
890
891 if (count >= buf_count) {
892 struct lttng_viewer_session *newbuf;
893 uint32_t new_buf_count = buf_count << 1;
894
895 newbuf = realloc(send_session_buf,
896 new_buf_count * sizeof(*send_session_buf));
897 if (!newbuf) {
898 ret = -1;
899 goto break_loop;
900 }
901 send_session_buf = newbuf;
902 buf_count = new_buf_count;
903 }
904 send_session = &send_session_buf[count];
905 if (lttng_strncpy(send_session->session_name,
906 session->session_name,
907 sizeof(send_session->session_name))) {
908 ret = -1;
909 goto break_loop;
910 }
911 if (lttng_strncpy(send_session->hostname, session->hostname,
912 sizeof(send_session->hostname))) {
913 ret = -1;
914 goto break_loop;
915 }
916 send_session->id = htobe64(session->id);
917 send_session->live_timer = htobe32(session->live_timer);
918 if (session->viewer_attached) {
919 send_session->clients = htobe32(1);
920 } else {
921 send_session->clients = htobe32(0);
922 }
923 send_session->streams = htobe32(session->stream_count);
924 count++;
925 next_session:
926 pthread_mutex_unlock(&session->lock);
927 continue;
928 break_loop:
929 pthread_mutex_unlock(&session->lock);
930 break;
931 }
932 rcu_read_unlock();
933 if (ret < 0) {
934 goto end_free;
935 }
936
937 session_list.sessions_count = htobe32(count);
938
939 health_code_update();
940
941 ret = send_response(conn->sock, &session_list, sizeof(session_list));
942 if (ret < 0) {
943 goto end_free;
944 }
945
946 health_code_update();
947
948 ret = send_response(conn->sock, send_session_buf,
949 count * sizeof(*send_session_buf));
950 if (ret < 0) {
951 goto end_free;
952 }
953 health_code_update();
954
955 ret = 0;
956 end_free:
957 free(send_session_buf);
958 return ret;
959 }
960
961 /*
962 * Send the viewer the list of current streams.
963 */
964 static
965 int viewer_get_new_streams(struct relay_connection *conn)
966 {
967 int ret, send_streams = 0;
968 uint32_t nb_created = 0, nb_unsent = 0, nb_streams = 0, nb_total = 0;
969 struct lttng_viewer_new_streams_request request;
970 struct lttng_viewer_new_streams_response response;
971 struct relay_session *session = NULL;
972 uint64_t session_id;
973 bool closed = false;
974
975 assert(conn);
976
977 DBG("Get new streams received");
978
979 health_code_update();
980
981 /* Receive the request from the connected client. */
982 ret = recv_request(conn->sock, &request, sizeof(request));
983 if (ret < 0) {
984 goto error;
985 }
986 session_id = be64toh(request.session_id);
987
988 health_code_update();
989
990 memset(&response, 0, sizeof(response));
991
992 session = session_get_by_id(session_id);
993 if (!session) {
994 DBG("Relay session %" PRIu64 " not found", session_id);
995 response.status = htobe32(LTTNG_VIEWER_NEW_STREAMS_ERR);
996 goto send_reply;
997 }
998
999 if (!viewer_session_is_attached(conn->viewer_session, session)) {
1000 response.status = htobe32(LTTNG_VIEWER_NEW_STREAMS_ERR);
1001 goto send_reply;
1002 }
1003
1004 pthread_mutex_lock(&session->lock);
1005 ret = make_viewer_streams(session,
1006 conn->viewer_session->current_trace_chunk,
1007 LTTNG_VIEWER_SEEK_LAST, &nb_total, &nb_unsent,
1008 &nb_created, &closed);
1009 if (ret < 0) {
1010 goto error_unlock_session;
1011 }
1012 send_streams = 1;
1013 response.status = htobe32(LTTNG_VIEWER_NEW_STREAMS_OK);
1014
1015 /* Only send back the newly created streams with the unsent ones. */
1016 nb_streams = nb_created + nb_unsent;
1017 response.streams_count = htobe32(nb_streams);
1018
1019 /*
1020 * If the session is closed, HUP when there are no more streams
1021 * with data.
1022 */
1023 if (closed && nb_total == 0) {
1024 send_streams = 0;
1025 response.streams_count = 0;
1026 response.status = htobe32(LTTNG_VIEWER_NEW_STREAMS_HUP);
1027 goto send_reply_unlock;
1028 }
1029 send_reply_unlock:
1030 pthread_mutex_unlock(&session->lock);
1031
1032 send_reply:
1033 health_code_update();
1034 ret = send_response(conn->sock, &response, sizeof(response));
1035 if (ret < 0) {
1036 goto end_put_session;
1037 }
1038 health_code_update();
1039
1040 /*
1041 * Unknown or empty session, just return gracefully, the viewer
1042 * knows what is happening.
1043 */
1044 if (!send_streams || !nb_streams) {
1045 ret = 0;
1046 goto end_put_session;
1047 }
1048
1049 /*
1050 * Send stream and *DON'T* ignore the sent flag so every viewer
1051 * streams that were not sent from that point will be sent to
1052 * the viewer.
1053 */
1054 ret = send_viewer_streams(conn->sock, session_id, 0);
1055 if (ret < 0) {
1056 goto end_put_session;
1057 }
1058
1059 end_put_session:
1060 if (session) {
1061 session_put(session);
1062 }
1063 error:
1064 return ret;
1065 error_unlock_session:
1066 pthread_mutex_unlock(&session->lock);
1067 session_put(session);
1068 return ret;
1069 }
1070
1071 /*
1072 * Send the viewer the list of current sessions.
1073 */
1074 static
1075 int viewer_attach_session(struct relay_connection *conn)
1076 {
1077 int send_streams = 0;
1078 ssize_t ret;
1079 uint32_t nb_streams = 0;
1080 enum lttng_viewer_seek seek_type;
1081 struct lttng_viewer_attach_session_request request;
1082 struct lttng_viewer_attach_session_response response;
1083 struct relay_session *session = NULL;
1084 enum lttng_viewer_attach_return_code viewer_attach_status;
1085 bool closed = false;
1086 uint64_t session_id;
1087
1088 assert(conn);
1089
1090 health_code_update();
1091
1092 /* Receive the request from the connected client. */
1093 ret = recv_request(conn->sock, &request, sizeof(request));
1094 if (ret < 0) {
1095 goto error;
1096 }
1097
1098 session_id = be64toh(request.session_id);
1099 health_code_update();
1100
1101 memset(&response, 0, sizeof(response));
1102
1103 if (!conn->viewer_session) {
1104 DBG("Client trying to attach before creating a live viewer session");
1105 response.status = htobe32(LTTNG_VIEWER_ATTACH_NO_SESSION);
1106 goto send_reply;
1107 }
1108
1109 session = session_get_by_id(session_id);
1110 if (!session) {
1111 DBG("Relay session %" PRIu64 " not found", session_id);
1112 response.status = htobe32(LTTNG_VIEWER_ATTACH_UNK);
1113 goto send_reply;
1114 }
1115 DBG("Attach session ID %" PRIu64 " received", session_id);
1116
1117 pthread_mutex_lock(&session->lock);
1118 if (!session->current_trace_chunk) {
1119 /*
1120 * Session is either being destroyed or it never had a trace
1121 * chunk created against it.
1122 */
1123 DBG("Session requested by live client has no current trace chunk, returning unknown session");
1124 response.status = htobe32(LTTNG_VIEWER_ATTACH_UNK);
1125 goto send_reply;
1126 }
1127 if (session->live_timer == 0) {
1128 DBG("Not live session");
1129 response.status = htobe32(LTTNG_VIEWER_ATTACH_NOT_LIVE);
1130 goto send_reply;
1131 }
1132
1133 send_streams = 1;
1134 viewer_attach_status = viewer_session_attach(conn->viewer_session,
1135 session);
1136 if (viewer_attach_status != LTTNG_VIEWER_ATTACH_OK) {
1137 response.status = htobe32(viewer_attach_status);
1138 goto send_reply;
1139 }
1140
1141 switch (be32toh(request.seek)) {
1142 case LTTNG_VIEWER_SEEK_BEGINNING:
1143 case LTTNG_VIEWER_SEEK_LAST:
1144 response.status = htobe32(LTTNG_VIEWER_ATTACH_OK);
1145 seek_type = be32toh(request.seek);
1146 break;
1147 default:
1148 ERR("Wrong seek parameter");
1149 response.status = htobe32(LTTNG_VIEWER_ATTACH_SEEK_ERR);
1150 send_streams = 0;
1151 goto send_reply;
1152 }
1153
1154 ret = make_viewer_streams(session,
1155 conn->viewer_session->current_trace_chunk, seek_type,
1156 &nb_streams, NULL, NULL, &closed);
1157 if (ret < 0) {
1158 goto end_put_session;
1159 }
1160 pthread_mutex_unlock(&session->lock);
1161 session_put(session);
1162 session = NULL;
1163
1164 response.streams_count = htobe32(nb_streams);
1165 /*
1166 * If the session is closed when the viewer is attaching, it
1167 * means some of the streams may have been concurrently removed,
1168 * so we don't allow the viewer to attach, even if there are
1169 * streams available.
1170 */
1171 if (closed) {
1172 send_streams = 0;
1173 response.streams_count = 0;
1174 response.status = htobe32(LTTNG_VIEWER_ATTACH_UNK);
1175 goto send_reply;
1176 }
1177
1178 send_reply:
1179 health_code_update();
1180 ret = send_response(conn->sock, &response, sizeof(response));
1181 if (ret < 0) {
1182 goto end_put_session;
1183 }
1184 health_code_update();
1185
1186 /*
1187 * Unknown or empty session, just return gracefully, the viewer
1188 * knows what is happening.
1189 */
1190 if (!send_streams || !nb_streams) {
1191 ret = 0;
1192 goto end_put_session;
1193 }
1194
1195 /* Send stream and ignore the sent flag. */
1196 ret = send_viewer_streams(conn->sock, session_id, 1);
1197 if (ret < 0) {
1198 goto end_put_session;
1199 }
1200
1201 end_put_session:
1202 if (session) {
1203 pthread_mutex_unlock(&session->lock);
1204 session_put(session);
1205 }
1206 error:
1207 return ret;
1208 }
1209
1210 /*
1211 * Open the index file if needed for the given vstream.
1212 *
1213 * If an index file is successfully opened, the vstream will set it as its
1214 * current index file.
1215 *
1216 * Return 0 on success, a negative value on error (-ENOENT if not ready yet).
1217 *
1218 * Called with rstream lock held.
1219 */
1220 static int try_open_index(struct relay_viewer_stream *vstream,
1221 struct relay_stream *rstream)
1222 {
1223 int ret = 0;
1224 const uint32_t connection_major = rstream->trace->session->major;
1225 const uint32_t connection_minor = rstream->trace->session->minor;
1226 enum lttng_trace_chunk_status chunk_status;
1227
1228 if (vstream->index_file) {
1229 goto end;
1230 }
1231
1232 /*
1233 * First time, we open the index file and at least one index is ready.
1234 */
1235 if (rstream->index_received_seqcount == 0) {
1236 ret = -ENOENT;
1237 goto end;
1238 }
1239 chunk_status = lttng_index_file_create_from_trace_chunk_read_only(
1240 vstream->stream_file.trace_chunk, rstream->path_name,
1241 rstream->channel_name, rstream->tracefile_size,
1242 vstream->current_tracefile_id,
1243 lttng_to_index_major(connection_major, connection_minor),
1244 lttng_to_index_minor(connection_major, connection_minor),
1245 true, &vstream->index_file);
1246 if (chunk_status != LTTNG_TRACE_CHUNK_STATUS_OK) {
1247 if (chunk_status == LTTNG_TRACE_CHUNK_STATUS_NO_FILE) {
1248 ret = -ENOENT;
1249 } else {
1250 ret = -1;
1251 }
1252 }
1253
1254 end:
1255 return ret;
1256 }
1257
1258 /*
1259 * Check the status of the index for the given stream. This function
1260 * updates the index structure if needed and can put (close) the vstream
1261 * in the HUP situation.
1262 *
1263 * Return 0 means that we can proceed with the index. A value of 1 means
1264 * that the index has been updated and is ready to be sent to the
1265 * client. A negative value indicates an error that can't be handled.
1266 *
1267 * Called with rstream lock held.
1268 */
1269 static int check_index_status(struct relay_viewer_stream *vstream,
1270 struct relay_stream *rstream, struct ctf_trace *trace,
1271 struct lttng_viewer_index *index)
1272 {
1273 int ret;
1274
1275 DBG("Check index status: index_received_seqcount %" PRIu64 " "
1276 "index_sent_seqcount %" PRIu64 " "
1277 "for stream %" PRIu64,
1278 rstream->index_received_seqcount,
1279 vstream->index_sent_seqcount,
1280 vstream->stream->stream_handle);
1281 if ((trace->session->connection_closed || rstream->closed)
1282 && rstream->index_received_seqcount
1283 == vstream->index_sent_seqcount) {
1284 /*
1285 * Last index sent and session connection or relay
1286 * stream are closed.
1287 */
1288 index->status = htobe32(LTTNG_VIEWER_INDEX_HUP);
1289 goto hup;
1290 } else if (rstream->beacon_ts_end != -1ULL &&
1291 (rstream->index_received_seqcount == 0 ||
1292 (vstream->index_sent_seqcount != 0 &&
1293 rstream->index_received_seqcount
1294 <= vstream->index_sent_seqcount))) {
1295 /*
1296 * We've received a synchronization beacon and the last index
1297 * available has been sent, the index for now is inactive.
1298 *
1299 * In this case, we have received a beacon which allows us to
1300 * inform the client of a time interval during which we can
1301 * guarantee that there are no events to read (and never will
1302 * be).
1303 *
1304 * The sent seqcount can grow higher than receive seqcount on
1305 * clear because the rotation performed by clear will push
1306 * the index_sent_seqcount ahead (see
1307 * viewer_stream_sync_tracefile_array_tail) and skip over
1308 * packet sequence numbers.
1309 */
1310 index->status = htobe32(LTTNG_VIEWER_INDEX_INACTIVE);
1311 index->timestamp_end = htobe64(rstream->beacon_ts_end);
1312 index->stream_id = htobe64(rstream->ctf_stream_id);
1313 DBG("Check index status: inactive with beacon, for stream %" PRIu64,
1314 vstream->stream->stream_handle);
1315 goto index_ready;
1316 } else if (rstream->index_received_seqcount == 0 ||
1317 (vstream->index_sent_seqcount != 0 &&
1318 rstream->index_received_seqcount
1319 <= vstream->index_sent_seqcount)) {
1320 /*
1321 * This checks whether received <= sent seqcount. In
1322 * this case, we have not received a beacon. Therefore,
1323 * we can only ask the client to retry later.
1324 *
1325 * The sent seqcount can grow higher than receive seqcount on
1326 * clear because the rotation performed by clear will push
1327 * the index_sent_seqcount ahead (see
1328 * viewer_stream_sync_tracefile_array_tail) and skip over
1329 * packet sequence numbers.
1330 */
1331 index->status = htobe32(LTTNG_VIEWER_INDEX_RETRY);
1332 DBG("Check index status: retry for stream %" PRIu64,
1333 vstream->stream->stream_handle);
1334 goto index_ready;
1335 } else if (!tracefile_array_seq_in_file(rstream->tfa,
1336 vstream->current_tracefile_id,
1337 vstream->index_sent_seqcount)) {
1338 /*
1339 * The next index we want to send cannot be read either
1340 * because we need to perform a rotation, or due to
1341 * the producer having overwritten its trace file.
1342 */
1343 DBG("Viewer stream %" PRIu64 " rotation",
1344 vstream->stream->stream_handle);
1345 ret = viewer_stream_rotate(vstream);
1346 if (ret == 1) {
1347 /* EOF across entire stream. */
1348 index->status = htobe32(LTTNG_VIEWER_INDEX_HUP);
1349 goto hup;
1350 }
1351 /*
1352 * If we have been pushed due to overwrite, it
1353 * necessarily means there is data that can be read in
1354 * the stream. If we rotated because we reached the end
1355 * of a tracefile, it means the following tracefile
1356 * needs to contain at least one index, else we would
1357 * have already returned LTTNG_VIEWER_INDEX_RETRY to the
1358 * viewer. The updated index_sent_seqcount needs to
1359 * point to a readable index entry now.
1360 *
1361 * In the case where we "rotate" on a single file, we
1362 * can end up in a case where the requested index is
1363 * still unavailable.
1364 */
1365 if (rstream->tracefile_count == 1 &&
1366 !tracefile_array_seq_in_file(
1367 rstream->tfa,
1368 vstream->current_tracefile_id,
1369 vstream->index_sent_seqcount)) {
1370 index->status = htobe32(LTTNG_VIEWER_INDEX_RETRY);
1371 DBG("Check index status: retry: "
1372 "tracefile array sequence number %" PRIu64
1373 " not in file for stream %" PRIu64,
1374 vstream->index_sent_seqcount,
1375 vstream->stream->stream_handle);
1376 goto index_ready;
1377 }
1378 assert(tracefile_array_seq_in_file(rstream->tfa,
1379 vstream->current_tracefile_id,
1380 vstream->index_sent_seqcount));
1381 }
1382 /* ret == 0 means successful so we continue. */
1383 ret = 0;
1384 return ret;
1385
1386 hup:
1387 viewer_stream_put(vstream);
1388 index_ready:
1389 return 1;
1390 }
1391
1392 /*
1393 * Send the next index for a stream.
1394 *
1395 * Return 0 on success or else a negative value.
1396 */
1397 static
1398 int viewer_get_next_index(struct relay_connection *conn)
1399 {
1400 int ret;
1401 struct lttng_viewer_get_next_index request_index;
1402 struct lttng_viewer_index viewer_index;
1403 struct ctf_packet_index packet_index;
1404 struct relay_viewer_stream *vstream = NULL;
1405 struct relay_stream *rstream = NULL;
1406 struct ctf_trace *ctf_trace = NULL;
1407 struct relay_viewer_stream *metadata_viewer_stream = NULL;
1408
1409 assert(conn);
1410
1411 DBG("Viewer get next index");
1412
1413 memset(&viewer_index, 0, sizeof(viewer_index));
1414 health_code_update();
1415
1416 ret = recv_request(conn->sock, &request_index, sizeof(request_index));
1417 if (ret < 0) {
1418 goto end;
1419 }
1420 health_code_update();
1421
1422 vstream = viewer_stream_get_by_id(be64toh(request_index.stream_id));
1423 if (!vstream) {
1424 DBG("Client requested index of unknown stream id %" PRIu64,
1425 (uint64_t) be64toh(request_index.stream_id));
1426 viewer_index.status = htobe32(LTTNG_VIEWER_INDEX_ERR);
1427 goto send_reply;
1428 }
1429
1430 /* Use back. ref. Protected by refcounts. */
1431 rstream = vstream->stream;
1432 ctf_trace = rstream->trace;
1433
1434 /* metadata_viewer_stream may be NULL. */
1435 metadata_viewer_stream =
1436 ctf_trace_get_viewer_metadata_stream(ctf_trace);
1437
1438 pthread_mutex_lock(&rstream->lock);
1439
1440 /*
1441 * The viewer should not ask for index on metadata stream.
1442 */
1443 if (rstream->is_metadata) {
1444 viewer_index.status = htobe32(LTTNG_VIEWER_INDEX_HUP);
1445 goto send_reply;
1446 }
1447
1448 if (rstream->ongoing_rotation.is_set) {
1449 /* Rotation is ongoing, try again later. */
1450 viewer_index.status = htobe32(LTTNG_VIEWER_INDEX_RETRY);
1451 goto send_reply;
1452 }
1453
1454 if (rstream->trace->session->ongoing_rotation) {
1455 /* Rotation is ongoing, try again later. */
1456 viewer_index.status = htobe32(LTTNG_VIEWER_INDEX_RETRY);
1457 goto send_reply;
1458 }
1459
1460 if (rstream->trace_chunk) {
1461 uint64_t rchunk_id, vchunk_id;
1462
1463 /*
1464 * If the relay stream is not yet closed, ensure the viewer
1465 * chunk matches the relay chunk after clear.
1466 */
1467 if (lttng_trace_chunk_get_id(rstream->trace_chunk,
1468 &rchunk_id) != LTTNG_TRACE_CHUNK_STATUS_OK) {
1469 viewer_index.status = htobe32(LTTNG_VIEWER_INDEX_ERR);
1470 goto send_reply;
1471 }
1472 if (lttng_trace_chunk_get_id(
1473 conn->viewer_session->current_trace_chunk,
1474 &vchunk_id) != LTTNG_TRACE_CHUNK_STATUS_OK) {
1475 viewer_index.status = htobe32(LTTNG_VIEWER_INDEX_ERR);
1476 goto send_reply;
1477 }
1478
1479 if (rchunk_id != vchunk_id) {
1480 DBG("Relay and viewer chunk ids differ: "
1481 "rchunk_id %" PRIu64 " vchunk_id %" PRIu64,
1482 rchunk_id, vchunk_id);
1483
1484 lttng_trace_chunk_put(
1485 conn->viewer_session->current_trace_chunk);
1486 conn->viewer_session->current_trace_chunk = NULL;
1487 ret = viewer_session_set_trace_chunk_copy(
1488 conn->viewer_session,
1489 rstream->trace_chunk);
1490 if (ret) {
1491 viewer_index.status =
1492 htobe32(LTTNG_VIEWER_INDEX_ERR);
1493 goto send_reply;
1494 }
1495 }
1496 }
1497 if (conn->viewer_session->current_trace_chunk !=
1498 vstream->stream_file.trace_chunk) {
1499 bool acquired_reference;
1500
1501 DBG("Viewer session and viewer stream chunk differ: "
1502 "vsession chunk %p vstream chunk %p",
1503 conn->viewer_session->current_trace_chunk,
1504 vstream->stream_file.trace_chunk);
1505 lttng_trace_chunk_put(vstream->stream_file.trace_chunk);
1506 acquired_reference = lttng_trace_chunk_get(conn->viewer_session->current_trace_chunk);
1507 assert(acquired_reference);
1508 vstream->stream_file.trace_chunk =
1509 conn->viewer_session->current_trace_chunk;
1510 viewer_stream_sync_tracefile_array_tail(vstream);
1511 viewer_stream_close_files(vstream);
1512 }
1513
1514 ret = check_index_status(vstream, rstream, ctf_trace, &viewer_index);
1515 if (ret < 0) {
1516 goto error_put;
1517 } else if (ret == 1) {
1518 /*
1519 * We have no index to send and check_index_status has populated
1520 * viewer_index's status.
1521 */
1522 goto send_reply;
1523 }
1524 /* At this point, ret is 0 thus we will be able to read the index. */
1525 assert(!ret);
1526
1527 /* Try to open an index if one is needed for that stream. */
1528 ret = try_open_index(vstream, rstream);
1529 if (ret == -ENOENT) {
1530 if (rstream->closed) {
1531 viewer_index.status = htobe32(LTTNG_VIEWER_INDEX_HUP);
1532 goto send_reply;
1533 } else {
1534 viewer_index.status = htobe32(LTTNG_VIEWER_INDEX_RETRY);
1535 goto send_reply;
1536 }
1537 }
1538 if (ret < 0) {
1539 viewer_index.status = htobe32(LTTNG_VIEWER_INDEX_ERR);
1540 goto send_reply;
1541 }
1542
1543 /*
1544 * vstream->stream_fd may be NULL if it has been closed by
1545 * tracefile rotation, or if we are at the beginning of the
1546 * stream. We open the data stream file here to protect against
1547 * overwrite caused by tracefile rotation (in association with
1548 * unlink performed before overwrite).
1549 */
1550 if (!vstream->stream_file.fd) {
1551 int fd;
1552 char file_path[LTTNG_PATH_MAX];
1553 enum lttng_trace_chunk_status status;
1554
1555 ret = utils_stream_file_path(rstream->path_name,
1556 rstream->channel_name, rstream->tracefile_size,
1557 vstream->current_tracefile_id, NULL, file_path,
1558 sizeof(file_path));
1559 if (ret < 0) {
1560 goto error_put;
1561 }
1562
1563 /*
1564 * It is possible the the file we are trying to open is
1565 * missing if the stream has been closed (application exits with
1566 * per-pid buffers) and a clear command has been performed.
1567 */
1568 status = lttng_trace_chunk_open_file(
1569 vstream->stream_file.trace_chunk,
1570 file_path, O_RDONLY, 0, &fd, true);
1571 if (status != LTTNG_TRACE_CHUNK_STATUS_OK) {
1572 if (status == LTTNG_TRACE_CHUNK_STATUS_NO_FILE &&
1573 rstream->closed) {
1574 viewer_index.status = htobe32(LTTNG_VIEWER_INDEX_HUP);
1575 goto send_reply;
1576 }
1577 PERROR("Failed to open trace file for viewer stream");
1578 goto error_put;
1579 }
1580 vstream->stream_file.fd = stream_fd_create(fd);
1581 if (!vstream->stream_file.fd) {
1582 if (close(fd)) {
1583 PERROR("Failed to close viewer stream file");
1584 }
1585 goto error_put;
1586 }
1587 }
1588
1589 ret = check_new_streams(conn);
1590 if (ret < 0) {
1591 viewer_index.status = htobe32(LTTNG_VIEWER_INDEX_ERR);
1592 goto send_reply;
1593 } else if (ret == 1) {
1594 viewer_index.flags |= LTTNG_VIEWER_FLAG_NEW_STREAM;
1595 }
1596
1597 ret = lttng_index_file_read(vstream->index_file, &packet_index);
1598 if (ret) {
1599 ERR("Relay error reading index file %d",
1600 vstream->index_file->fd);
1601 viewer_index.status = htobe32(LTTNG_VIEWER_INDEX_ERR);
1602 goto send_reply;
1603 } else {
1604 viewer_index.status = htobe32(LTTNG_VIEWER_INDEX_OK);
1605 vstream->index_sent_seqcount++;
1606 }
1607
1608 /*
1609 * Indexes are stored in big endian, no need to switch before sending.
1610 */
1611 DBG("Sending viewer index for stream %" PRIu64 " offset %" PRIu64,
1612 rstream->stream_handle,
1613 (uint64_t) be64toh(packet_index.offset));
1614 viewer_index.offset = packet_index.offset;
1615 viewer_index.packet_size = packet_index.packet_size;
1616 viewer_index.content_size = packet_index.content_size;
1617 viewer_index.timestamp_begin = packet_index.timestamp_begin;
1618 viewer_index.timestamp_end = packet_index.timestamp_end;
1619 viewer_index.events_discarded = packet_index.events_discarded;
1620 viewer_index.stream_id = packet_index.stream_id;
1621
1622 send_reply:
1623 if (rstream) {
1624 pthread_mutex_unlock(&rstream->lock);
1625 }
1626
1627 if (metadata_viewer_stream) {
1628 pthread_mutex_lock(&metadata_viewer_stream->stream->lock);
1629 DBG("get next index metadata check: recv %" PRIu64
1630 " sent %" PRIu64,
1631 metadata_viewer_stream->stream->metadata_received,
1632 metadata_viewer_stream->metadata_sent);
1633 if (!metadata_viewer_stream->stream->metadata_received ||
1634 metadata_viewer_stream->stream->metadata_received >
1635 metadata_viewer_stream->metadata_sent) {
1636 viewer_index.flags |= LTTNG_VIEWER_FLAG_NEW_METADATA;
1637 }
1638 pthread_mutex_unlock(&metadata_viewer_stream->stream->lock);
1639 }
1640
1641 viewer_index.flags = htobe32(viewer_index.flags);
1642 health_code_update();
1643
1644 ret = send_response(conn->sock, &viewer_index, sizeof(viewer_index));
1645 if (ret < 0) {
1646 goto end;
1647 }
1648 health_code_update();
1649
1650 if (vstream) {
1651 DBG("Index %" PRIu64 " for stream %" PRIu64 " sent",
1652 vstream->index_sent_seqcount,
1653 vstream->stream->stream_handle);
1654 }
1655 end:
1656 if (metadata_viewer_stream) {
1657 viewer_stream_put(metadata_viewer_stream);
1658 }
1659 if (vstream) {
1660 viewer_stream_put(vstream);
1661 }
1662 return ret;
1663
1664 error_put:
1665 pthread_mutex_unlock(&rstream->lock);
1666 if (metadata_viewer_stream) {
1667 viewer_stream_put(metadata_viewer_stream);
1668 }
1669 viewer_stream_put(vstream);
1670 return ret;
1671 }
1672
1673 /*
1674 * Send the next index for a stream
1675 *
1676 * Return 0 on success or else a negative value.
1677 */
1678 static
1679 int viewer_get_packet(struct relay_connection *conn)
1680 {
1681 int ret;
1682 off_t lseek_ret;
1683 char *reply = NULL;
1684 struct lttng_viewer_get_packet get_packet_info;
1685 struct lttng_viewer_trace_packet reply_header;
1686 struct relay_viewer_stream *vstream = NULL;
1687 uint32_t reply_size = sizeof(reply_header);
1688 uint32_t packet_data_len = 0;
1689 ssize_t read_len;
1690
1691 DBG2("Relay get data packet");
1692
1693 health_code_update();
1694
1695 ret = recv_request(conn->sock, &get_packet_info,
1696 sizeof(get_packet_info));
1697 if (ret < 0) {
1698 goto end;
1699 }
1700 health_code_update();
1701
1702 /* From this point on, the error label can be reached. */
1703 memset(&reply_header, 0, sizeof(reply_header));
1704
1705 vstream = viewer_stream_get_by_id(be64toh(get_packet_info.stream_id));
1706 if (!vstream) {
1707 DBG("Client requested packet of unknown stream id %" PRIu64,
1708 (uint64_t) be64toh(get_packet_info.stream_id));
1709 reply_header.status = htobe32(LTTNG_VIEWER_GET_PACKET_ERR);
1710 goto send_reply_nolock;
1711 } else {
1712 packet_data_len = be32toh(get_packet_info.len);
1713 reply_size += packet_data_len;
1714 }
1715
1716 reply = zmalloc(reply_size);
1717 if (!reply) {
1718 PERROR("packet reply zmalloc");
1719 reply_size = sizeof(reply_header);
1720 goto error;
1721 }
1722
1723 pthread_mutex_lock(&vstream->stream->lock);
1724 lseek_ret = lseek(vstream->stream_file.fd->fd,
1725 be64toh(get_packet_info.offset), SEEK_SET);
1726 if (lseek_ret < 0) {
1727 PERROR("lseek fd %d to offset %" PRIu64,
1728 vstream->stream_file.fd->fd,
1729 (uint64_t) be64toh(get_packet_info.offset));
1730 goto error;
1731 }
1732 read_len = lttng_read(vstream->stream_file.fd->fd,
1733 reply + sizeof(reply_header), packet_data_len);
1734 if (read_len < packet_data_len) {
1735 PERROR("Relay reading trace file, fd: %d, offset: %" PRIu64,
1736 vstream->stream_file.fd->fd,
1737 (uint64_t) be64toh(get_packet_info.offset));
1738 goto error;
1739 }
1740 reply_header.status = htobe32(LTTNG_VIEWER_GET_PACKET_OK);
1741 reply_header.len = htobe32(packet_data_len);
1742 goto send_reply;
1743
1744 error:
1745 reply_header.status = htobe32(LTTNG_VIEWER_GET_PACKET_ERR);
1746
1747 send_reply:
1748 if (vstream) {
1749 pthread_mutex_unlock(&vstream->stream->lock);
1750 }
1751 send_reply_nolock:
1752
1753 health_code_update();
1754
1755 if (reply) {
1756 memcpy(reply, &reply_header, sizeof(reply_header));
1757 ret = send_response(conn->sock, reply, reply_size);
1758 } else {
1759 /* No reply to send. */
1760 ret = send_response(conn->sock, &reply_header,
1761 reply_size);
1762 }
1763
1764 health_code_update();
1765 if (ret < 0) {
1766 PERROR("sendmsg of packet data failed");
1767 goto end_free;
1768 }
1769
1770 DBG("Sent %u bytes for stream %" PRIu64, reply_size,
1771 (uint64_t) be64toh(get_packet_info.stream_id));
1772
1773 end_free:
1774 free(reply);
1775 end:
1776 if (vstream) {
1777 viewer_stream_put(vstream);
1778 }
1779 return ret;
1780 }
1781
1782 /*
1783 * Send the session's metadata
1784 *
1785 * Return 0 on success else a negative value.
1786 */
1787 static
1788 int viewer_get_metadata(struct relay_connection *conn)
1789 {
1790 int ret = 0;
1791 ssize_t read_len;
1792 uint64_t len = 0;
1793 char *data = NULL;
1794 struct lttng_viewer_get_metadata request;
1795 struct lttng_viewer_metadata_packet reply;
1796 struct relay_viewer_stream *vstream = NULL;
1797
1798 assert(conn);
1799
1800 DBG("Relay get metadata");
1801
1802 health_code_update();
1803
1804 ret = recv_request(conn->sock, &request, sizeof(request));
1805 if (ret < 0) {
1806 goto end;
1807 }
1808 health_code_update();
1809
1810 memset(&reply, 0, sizeof(reply));
1811
1812 vstream = viewer_stream_get_by_id(be64toh(request.stream_id));
1813 if (!vstream) {
1814 /*
1815 * The metadata stream can be closed by a CLOSE command
1816 * just before we attach. It can also be closed by
1817 * per-pid tracing during tracing. Therefore, it is
1818 * possible that we cannot find this viewer stream.
1819 * Reply back to the client with an error if we cannot
1820 * find it.
1821 */
1822 DBG("Client requested metadata of unknown stream id %" PRIu64,
1823 (uint64_t) be64toh(request.stream_id));
1824 reply.status = htobe32(LTTNG_VIEWER_METADATA_ERR);
1825 goto send_reply;
1826 }
1827 pthread_mutex_lock(&vstream->stream->lock);
1828 if (!vstream->stream->is_metadata) {
1829 ERR("Invalid metadata stream");
1830 goto error;
1831 }
1832
1833 if (vstream->metadata_sent >= vstream->stream->metadata_received) {
1834 /*
1835 * The live viewers expect to receive a NO_NEW_METADATA
1836 * status before a stream disappears, otherwise they abort the
1837 * entire live connection when receiving an error status.
1838 *
1839 * Clear feature resets the metadata_sent to 0 until the
1840 * same metadata is received again.
1841 */
1842 reply.status = htobe32(LTTNG_VIEWER_NO_NEW_METADATA);
1843 /*
1844 * The live viewer considers a closed 0 byte metadata stream as
1845 * an error.
1846 */
1847 if (vstream->metadata_sent > 0) {
1848 vstream->stream->no_new_metadata_notified = true;
1849 if (vstream->stream->closed) {
1850 /* Release ownership for the viewer metadata stream. */
1851 viewer_stream_put(vstream);
1852 }
1853 }
1854 goto send_reply;
1855 }
1856
1857 len = vstream->stream->metadata_received - vstream->metadata_sent;
1858
1859 /* first time, we open the metadata file */
1860 if (!vstream->stream_file.fd) {
1861 int fd;
1862 char file_path[LTTNG_PATH_MAX];
1863 enum lttng_trace_chunk_status status;
1864 struct relay_stream *rstream = vstream->stream;
1865
1866 ret = utils_stream_file_path(rstream->path_name,
1867 rstream->channel_name, rstream->tracefile_size,
1868 vstream->current_tracefile_id, NULL, file_path,
1869 sizeof(file_path));
1870 if (ret < 0) {
1871 goto error;
1872 }
1873
1874 /*
1875 * It is possible the the metadata file we are trying to open is
1876 * missing if the stream has been closed (application exits with
1877 * per-pid buffers) and a clear command has been performed.
1878 */
1879 status = lttng_trace_chunk_open_file(
1880 vstream->stream_file.trace_chunk,
1881 file_path, O_RDONLY, 0, &fd, true);
1882 if (status != LTTNG_TRACE_CHUNK_STATUS_OK) {
1883 if (status == LTTNG_TRACE_CHUNK_STATUS_NO_FILE) {
1884 reply.status = htobe32(LTTNG_VIEWER_NO_NEW_METADATA);
1885 len = 0;
1886 if (vstream->stream->closed) {
1887 viewer_stream_put(vstream);
1888 }
1889 goto send_reply;
1890 }
1891 PERROR("Failed to open metadata file for viewer stream");
1892 goto error;
1893 }
1894 vstream->stream_file.fd = stream_fd_create(fd);
1895 if (!vstream->stream_file.fd) {
1896 if (close(fd)) {
1897 PERROR("Failed to close viewer metadata file");
1898 }
1899 goto error;
1900 }
1901 }
1902
1903 reply.len = htobe64(len);
1904 data = zmalloc(len);
1905 if (!data) {
1906 PERROR("viewer metadata zmalloc");
1907 goto error;
1908 }
1909
1910 read_len = lttng_read(vstream->stream_file.fd->fd, data, len);
1911 if (read_len < len) {
1912 PERROR("Relay reading metadata file");
1913 goto error;
1914 }
1915 vstream->metadata_sent += read_len;
1916 reply.status = htobe32(LTTNG_VIEWER_METADATA_OK);
1917
1918 goto send_reply;
1919
1920 error:
1921 reply.status = htobe32(LTTNG_VIEWER_METADATA_ERR);
1922
1923 send_reply:
1924 health_code_update();
1925 if (vstream) {
1926 pthread_mutex_unlock(&vstream->stream->lock);
1927 }
1928 ret = send_response(conn->sock, &reply, sizeof(reply));
1929 if (ret < 0) {
1930 goto end_free;
1931 }
1932 health_code_update();
1933
1934 if (len > 0) {
1935 ret = send_response(conn->sock, data, len);
1936 if (ret < 0) {
1937 goto end_free;
1938 }
1939 }
1940
1941 DBG("Sent %" PRIu64 " bytes of metadata for stream %" PRIu64, len,
1942 (uint64_t) be64toh(request.stream_id));
1943
1944 DBG("Metadata sent");
1945
1946 end_free:
1947 free(data);
1948 end:
1949 if (vstream) {
1950 viewer_stream_put(vstream);
1951 }
1952 return ret;
1953 }
1954
1955 /*
1956 * Create a viewer session.
1957 *
1958 * Return 0 on success or else a negative value.
1959 */
1960 static
1961 int viewer_create_session(struct relay_connection *conn)
1962 {
1963 int ret;
1964 struct lttng_viewer_create_session_response resp;
1965
1966 DBG("Viewer create session received");
1967
1968 memset(&resp, 0, sizeof(resp));
1969 resp.status = htobe32(LTTNG_VIEWER_CREATE_SESSION_OK);
1970 conn->viewer_session = viewer_session_create();
1971 if (!conn->viewer_session) {
1972 ERR("Allocation viewer session");
1973 resp.status = htobe32(LTTNG_VIEWER_CREATE_SESSION_ERR);
1974 goto send_reply;
1975 }
1976
1977 send_reply:
1978 health_code_update();
1979 ret = send_response(conn->sock, &resp, sizeof(resp));
1980 if (ret < 0) {
1981 goto end;
1982 }
1983 health_code_update();
1984 ret = 0;
1985
1986 end:
1987 return ret;
1988 }
1989
1990 /*
1991 * Detach a viewer session.
1992 *
1993 * Return 0 on success or else a negative value.
1994 */
1995 static
1996 int viewer_detach_session(struct relay_connection *conn)
1997 {
1998 int ret;
1999 struct lttng_viewer_detach_session_response response;
2000 struct lttng_viewer_detach_session_request request;
2001 struct relay_session *session = NULL;
2002 uint64_t viewer_session_to_close;
2003
2004 DBG("Viewer detach session received");
2005
2006 assert(conn);
2007
2008 health_code_update();
2009
2010 /* Receive the request from the connected client. */
2011 ret = recv_request(conn->sock, &request, sizeof(request));
2012 if (ret < 0) {
2013 goto end;
2014 }
2015 viewer_session_to_close = be64toh(request.session_id);
2016
2017 if (!conn->viewer_session) {
2018 DBG("Client trying to detach before creating a live viewer session");
2019 response.status = htobe32(LTTNG_VIEWER_DETACH_SESSION_ERR);
2020 goto send_reply;
2021 }
2022
2023 health_code_update();
2024
2025 memset(&response, 0, sizeof(response));
2026 DBG("Detaching from session ID %" PRIu64, viewer_session_to_close);
2027
2028 session = session_get_by_id(be64toh(request.session_id));
2029 if (!session) {
2030 DBG("Relay session %" PRIu64 " not found",
2031 (uint64_t) be64toh(request.session_id));
2032 response.status = htobe32(LTTNG_VIEWER_DETACH_SESSION_UNK);
2033 goto send_reply;
2034 }
2035
2036 ret = viewer_session_is_attached(conn->viewer_session, session);
2037 if (ret != 1) {
2038 DBG("Not attached to this session");
2039 response.status = htobe32(LTTNG_VIEWER_DETACH_SESSION_ERR);
2040 goto send_reply_put;
2041 }
2042
2043 viewer_session_close_one_session(conn->viewer_session, session);
2044 response.status = htobe32(LTTNG_VIEWER_DETACH_SESSION_OK);
2045 DBG("Session %" PRIu64 " detached.", viewer_session_to_close);
2046
2047 send_reply_put:
2048 session_put(session);
2049
2050 send_reply:
2051 health_code_update();
2052 ret = send_response(conn->sock, &response, sizeof(response));
2053 if (ret < 0) {
2054 goto end;
2055 }
2056 health_code_update();
2057 ret = 0;
2058
2059 end:
2060 return ret;
2061 }
2062
2063 /*
2064 * live_relay_unknown_command: send -1 if received unknown command
2065 */
2066 static
2067 void live_relay_unknown_command(struct relay_connection *conn)
2068 {
2069 struct lttcomm_relayd_generic_reply reply;
2070
2071 memset(&reply, 0, sizeof(reply));
2072 reply.ret_code = htobe32(LTTNG_ERR_UNK);
2073 (void) send_response(conn->sock, &reply, sizeof(reply));
2074 }
2075
2076 /*
2077 * Process the commands received on the control socket
2078 */
2079 static
2080 int process_control(struct lttng_viewer_cmd *recv_hdr,
2081 struct relay_connection *conn)
2082 {
2083 int ret = 0;
2084 uint32_t msg_value;
2085
2086 msg_value = be32toh(recv_hdr->cmd);
2087
2088 /*
2089 * Make sure we've done the version check before any command other then a
2090 * new client connection.
2091 */
2092 if (msg_value != LTTNG_VIEWER_CONNECT && !conn->version_check_done) {
2093 ERR("Viewer conn value %" PRIu32 " before version check", msg_value);
2094 ret = -1;
2095 goto end;
2096 }
2097
2098 switch (msg_value) {
2099 case LTTNG_VIEWER_CONNECT:
2100 ret = viewer_connect(conn);
2101 break;
2102 case LTTNG_VIEWER_LIST_SESSIONS:
2103 ret = viewer_list_sessions(conn);
2104 break;
2105 case LTTNG_VIEWER_ATTACH_SESSION:
2106 ret = viewer_attach_session(conn);
2107 break;
2108 case LTTNG_VIEWER_GET_NEXT_INDEX:
2109 ret = viewer_get_next_index(conn);
2110 break;
2111 case LTTNG_VIEWER_GET_PACKET:
2112 ret = viewer_get_packet(conn);
2113 break;
2114 case LTTNG_VIEWER_GET_METADATA:
2115 ret = viewer_get_metadata(conn);
2116 break;
2117 case LTTNG_VIEWER_GET_NEW_STREAMS:
2118 ret = viewer_get_new_streams(conn);
2119 break;
2120 case LTTNG_VIEWER_CREATE_SESSION:
2121 ret = viewer_create_session(conn);
2122 break;
2123 case LTTNG_VIEWER_DETACH_SESSION:
2124 ret = viewer_detach_session(conn);
2125 break;
2126 default:
2127 ERR("Received unknown viewer command (%u)",
2128 be32toh(recv_hdr->cmd));
2129 live_relay_unknown_command(conn);
2130 ret = -1;
2131 goto end;
2132 }
2133
2134 end:
2135 return ret;
2136 }
2137
2138 static
2139 void cleanup_connection_pollfd(struct lttng_poll_event *events, int pollfd)
2140 {
2141 int ret;
2142
2143 (void) lttng_poll_del(events, pollfd);
2144
2145 ret = close(pollfd);
2146 if (ret < 0) {
2147 ERR("Closing pollfd %d", pollfd);
2148 }
2149 }
2150
2151 /*
2152 * This thread does the actual work
2153 */
2154 static
2155 void *thread_worker(void *data)
2156 {
2157 int ret, err = -1;
2158 uint32_t nb_fd;
2159 struct lttng_poll_event events;
2160 struct lttng_ht *viewer_connections_ht;
2161 struct lttng_ht_iter iter;
2162 struct lttng_viewer_cmd recv_hdr;
2163 struct relay_connection *destroy_conn;
2164
2165 DBG("[thread] Live viewer relay worker started");
2166
2167 rcu_register_thread();
2168
2169 health_register(health_relayd, HEALTH_RELAYD_TYPE_LIVE_WORKER);
2170
2171 if (testpoint(relayd_thread_live_worker)) {
2172 goto error_testpoint;
2173 }
2174
2175 /* table of connections indexed on socket */
2176 viewer_connections_ht = lttng_ht_new(0, LTTNG_HT_TYPE_ULONG);
2177 if (!viewer_connections_ht) {
2178 goto viewer_connections_ht_error;
2179 }
2180
2181 ret = create_thread_poll_set(&events, 2);
2182 if (ret < 0) {
2183 goto error_poll_create;
2184 }
2185
2186 ret = lttng_poll_add(&events, live_conn_pipe[0], LPOLLIN | LPOLLRDHUP);
2187 if (ret < 0) {
2188 goto error;
2189 }
2190
2191 restart:
2192 while (1) {
2193 int i;
2194
2195 health_code_update();
2196
2197 /* Infinite blocking call, waiting for transmission */
2198 DBG3("Relayd live viewer worker thread polling...");
2199 health_poll_entry();
2200 ret = lttng_poll_wait(&events, -1);
2201 health_poll_exit();
2202 if (ret < 0) {
2203 /*
2204 * Restart interrupted system call.
2205 */
2206 if (errno == EINTR) {
2207 goto restart;
2208 }
2209 goto error;
2210 }
2211
2212 nb_fd = ret;
2213
2214 /*
2215 * Process control. The control connection is prioritised so we don't
2216 * starve it with high throughput tracing data on the data
2217 * connection.
2218 */
2219 for (i = 0; i < nb_fd; i++) {
2220 /* Fetch once the poll data */
2221 uint32_t revents = LTTNG_POLL_GETEV(&events, i);
2222 int pollfd = LTTNG_POLL_GETFD(&events, i);
2223
2224 health_code_update();
2225
2226 /* Thread quit pipe has been closed. Killing thread. */
2227 ret = check_thread_quit_pipe(pollfd, revents);
2228 if (ret) {
2229 err = 0;
2230 goto exit;
2231 }
2232
2233 /* Inspect the relay conn pipe for new connection. */
2234 if (pollfd == live_conn_pipe[0]) {
2235 if (revents & LPOLLIN) {
2236 struct relay_connection *conn;
2237
2238 ret = lttng_read(live_conn_pipe[0],
2239 &conn, sizeof(conn));
2240 if (ret < 0) {
2241 goto error;
2242 }
2243 ret = lttng_poll_add(&events,
2244 conn->sock->fd,
2245 LPOLLIN | LPOLLRDHUP);
2246 if (ret) {
2247 ERR("Failed to add new live connection file descriptor to poll set");
2248 goto error;
2249 }
2250 connection_ht_add(viewer_connections_ht, conn);
2251 DBG("Connection socket %d added to poll", conn->sock->fd);
2252 } else if (revents & (LPOLLERR | LPOLLHUP | LPOLLRDHUP)) {
2253 ERR("Relay live pipe error");
2254 goto error;
2255 } else {
2256 ERR("Unexpected poll events %u for sock %d", revents, pollfd);
2257 goto error;
2258 }
2259 } else {
2260 /* Connection activity. */
2261 struct relay_connection *conn;
2262
2263 conn = connection_get_by_sock(viewer_connections_ht, pollfd);
2264 if (!conn) {
2265 continue;
2266 }
2267
2268 if (revents & LPOLLIN) {
2269 ret = conn->sock->ops->recvmsg(conn->sock, &recv_hdr,
2270 sizeof(recv_hdr), 0);
2271 if (ret <= 0) {
2272 /* Connection closed. */
2273 cleanup_connection_pollfd(&events, pollfd);
2274 /* Put "create" ownership reference. */
2275 connection_put(conn);
2276 DBG("Viewer control conn closed with %d", pollfd);
2277 } else {
2278 ret = process_control(&recv_hdr, conn);
2279 if (ret < 0) {
2280 /* Clear the session on error. */
2281 cleanup_connection_pollfd(&events, pollfd);
2282 /* Put "create" ownership reference. */
2283 connection_put(conn);
2284 DBG("Viewer connection closed with %d", pollfd);
2285 }
2286 }
2287 } else if (revents & (LPOLLERR | LPOLLHUP | LPOLLRDHUP)) {
2288 cleanup_connection_pollfd(&events, pollfd);
2289 /* Put "create" ownership reference. */
2290 connection_put(conn);
2291 } else {
2292 ERR("Unexpected poll events %u for sock %d", revents, pollfd);
2293 connection_put(conn);
2294 goto error;
2295 }
2296 /* Put local "get_by_sock" reference. */
2297 connection_put(conn);
2298 }
2299 }
2300 }
2301
2302 exit:
2303 error:
2304 lttng_poll_clean(&events);
2305
2306 /* Cleanup remaining connection object. */
2307 rcu_read_lock();
2308 cds_lfht_for_each_entry(viewer_connections_ht->ht, &iter.iter,
2309 destroy_conn,
2310 sock_n.node) {
2311 health_code_update();
2312 connection_put(destroy_conn);
2313 }
2314 rcu_read_unlock();
2315 error_poll_create:
2316 lttng_ht_destroy(viewer_connections_ht);
2317 viewer_connections_ht_error:
2318 /* Close relay conn pipes */
2319 (void) fd_tracker_util_pipe_close(the_fd_tracker, live_conn_pipe);
2320 if (err) {
2321 DBG("Viewer worker thread exited with error");
2322 }
2323 DBG("Viewer worker thread cleanup complete");
2324 error_testpoint:
2325 if (err) {
2326 health_error();
2327 ERR("Health error occurred in %s", __func__);
2328 }
2329 health_unregister(health_relayd);
2330 if (lttng_relay_stop_threads()) {
2331 ERR("Error stopping threads");
2332 }
2333 rcu_unregister_thread();
2334 return NULL;
2335 }
2336
2337 /*
2338 * Create the relay command pipe to wake thread_manage_apps.
2339 * Closed in cleanup().
2340 */
2341 static int create_conn_pipe(void)
2342 {
2343 return fd_tracker_util_pipe_open_cloexec(the_fd_tracker,
2344 "Live connection pipe", live_conn_pipe);
2345 }
2346
2347 int relayd_live_join(void)
2348 {
2349 int ret, retval = 0;
2350 void *status;
2351
2352 ret = pthread_join(live_listener_thread, &status);
2353 if (ret) {
2354 errno = ret;
2355 PERROR("pthread_join live listener");
2356 retval = -1;
2357 }
2358
2359 ret = pthread_join(live_worker_thread, &status);
2360 if (ret) {
2361 errno = ret;
2362 PERROR("pthread_join live worker");
2363 retval = -1;
2364 }
2365
2366 ret = pthread_join(live_dispatcher_thread, &status);
2367 if (ret) {
2368 errno = ret;
2369 PERROR("pthread_join live dispatcher");
2370 retval = -1;
2371 }
2372
2373 cleanup_relayd_live();
2374
2375 return retval;
2376 }
2377
2378 /*
2379 * main
2380 */
2381 int relayd_live_create(struct lttng_uri *uri)
2382 {
2383 int ret = 0, retval = 0;
2384 void *status;
2385 int is_root;
2386
2387 if (!uri) {
2388 retval = -1;
2389 goto exit_init_data;
2390 }
2391 live_uri = uri;
2392
2393 /* Check if daemon is UID = 0 */
2394 is_root = !getuid();
2395
2396 if (!is_root) {
2397 if (live_uri->port < 1024) {
2398 ERR("Need to be root to use ports < 1024");
2399 retval = -1;
2400 goto exit_init_data;
2401 }
2402 }
2403
2404 /* Setup the thread apps communication pipe. */
2405 if (create_conn_pipe()) {
2406 retval = -1;
2407 goto exit_init_data;
2408 }
2409
2410 /* Init relay command queue. */
2411 cds_wfcq_init(&viewer_conn_queue.head, &viewer_conn_queue.tail);
2412
2413 /* Set up max poll set size */
2414 if (lttng_poll_set_max_size()) {
2415 retval = -1;
2416 goto exit_init_data;
2417 }
2418
2419 /* Setup the dispatcher thread */
2420 ret = pthread_create(&live_dispatcher_thread, default_pthread_attr(),
2421 thread_dispatcher, (void *) NULL);
2422 if (ret) {
2423 errno = ret;
2424 PERROR("pthread_create viewer dispatcher");
2425 retval = -1;
2426 goto exit_dispatcher_thread;
2427 }
2428
2429 /* Setup the worker thread */
2430 ret = pthread_create(&live_worker_thread, default_pthread_attr(),
2431 thread_worker, NULL);
2432 if (ret) {
2433 errno = ret;
2434 PERROR("pthread_create viewer worker");
2435 retval = -1;
2436 goto exit_worker_thread;
2437 }
2438
2439 /* Setup the listener thread */
2440 ret = pthread_create(&live_listener_thread, default_pthread_attr(),
2441 thread_listener, (void *) NULL);
2442 if (ret) {
2443 errno = ret;
2444 PERROR("pthread_create viewer listener");
2445 retval = -1;
2446 goto exit_listener_thread;
2447 }
2448
2449 /*
2450 * All OK, started all threads.
2451 */
2452 return retval;
2453
2454 /*
2455 * Join on the live_listener_thread should anything be added after
2456 * the live_listener thread's creation.
2457 */
2458
2459 exit_listener_thread:
2460
2461 ret = pthread_join(live_worker_thread, &status);
2462 if (ret) {
2463 errno = ret;
2464 PERROR("pthread_join live worker");
2465 retval = -1;
2466 }
2467 exit_worker_thread:
2468
2469 ret = pthread_join(live_dispatcher_thread, &status);
2470 if (ret) {
2471 errno = ret;
2472 PERROR("pthread_join live dispatcher");
2473 retval = -1;
2474 }
2475 exit_dispatcher_thread:
2476
2477 exit_init_data:
2478 cleanup_relayd_live();
2479
2480 return retval;
2481 }
This page took 0.125394 seconds and 4 git commands to generate.