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