4724a3adaab0bcbfe5d64dfc44245b57c0f202da
[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 static
594 int create_sock(void *data, int *out_fd)
595 {
596 int ret;
597 struct lttcomm_sock *sock = (lttcomm_sock *) data;
598
599 ret = lttcomm_create_sock(sock);
600 if (ret < 0) {
601 goto end;
602 }
603
604 *out_fd = sock->fd;
605 end:
606 return ret;
607 }
608
609 static
610 int close_sock(void *data, int *in_fd __attribute__((unused)))
611 {
612 struct lttcomm_sock *sock = (lttcomm_sock *) data;
613
614 return sock->ops->close(sock);
615 }
616
617 static int accept_sock(void *data, int *out_fd)
618 {
619 int ret = 0;
620 /* Socks is an array of in_sock, out_sock. */
621 struct lttcomm_sock **socks = (lttcomm_sock **) data;
622 struct lttcomm_sock *in_sock = socks[0];
623
624 socks[1] = in_sock->ops->accept(in_sock);
625 if (!socks[1]) {
626 ret = -1;
627 goto end;
628 }
629 *out_fd = socks[1]->fd;
630 end:
631 return ret;
632 }
633
634 static
635 struct lttcomm_sock *accept_live_sock(struct lttcomm_sock *listening_sock,
636 const char *name)
637 {
638 int out_fd, ret;
639 struct lttcomm_sock *socks[2] = { listening_sock, NULL };
640 struct lttcomm_sock *new_sock = NULL;
641
642 ret = fd_tracker_open_unsuspendable_fd(the_fd_tracker, &out_fd,
643 (const char **) &name, 1, accept_sock, &socks);
644 if (ret) {
645 goto end;
646 }
647 new_sock = socks[1];
648 DBG("%s accepted, socket %d", name, new_sock->fd);
649 end:
650 return new_sock;
651 }
652
653 /*
654 * Create and init socket from uri.
655 */
656 static
657 struct lttcomm_sock *init_socket(struct lttng_uri *uri, const char *name)
658 {
659 int ret, sock_fd;
660 struct lttcomm_sock *sock = NULL;
661 char uri_str[LTTNG_PATH_MAX];
662 char *formated_name = NULL;
663
664 sock = lttcomm_alloc_sock_from_uri(uri);
665 if (sock == NULL) {
666 ERR("Allocating socket");
667 goto error;
668 }
669
670 /*
671 * Don't fail to create the socket if the name can't be built as it is
672 * only used for debugging purposes.
673 */
674 ret = uri_to_str_url(uri, uri_str, sizeof(uri_str));
675 uri_str[sizeof(uri_str) - 1] = '\0';
676 if (ret >= 0) {
677 ret = asprintf(&formated_name, "%s socket @ %s", name,
678 uri_str);
679 if (ret < 0) {
680 formated_name = NULL;
681 }
682 }
683
684 ret = fd_tracker_open_unsuspendable_fd(the_fd_tracker, &sock_fd,
685 (const char **) (formated_name ? &formated_name : NULL),
686 1, create_sock, sock);
687 if (ret) {
688 PERROR("Failed to create \"%s\" socket",
689 formated_name ?: "Unknown");
690 goto error;
691 }
692 DBG("Listening on %s socket %d", name, sock->fd);
693
694 ret = sock->ops->bind(sock);
695 if (ret < 0) {
696 PERROR("Failed to bind lttng-live socket");
697 goto error;
698 }
699
700 ret = sock->ops->listen(sock, -1);
701 if (ret < 0) {
702 goto error;
703
704 }
705
706 free(formated_name);
707 return sock;
708
709 error:
710 if (sock) {
711 lttcomm_destroy_sock(sock);
712 }
713 free(formated_name);
714 return NULL;
715 }
716
717 /*
718 * This thread manages the listening for new connections on the network
719 */
720 static
721 void *thread_listener(void *data __attribute__((unused)))
722 {
723 int i, ret, err = -1;
724 uint32_t nb_fd;
725 struct lttng_poll_event events;
726 struct lttcomm_sock *live_control_sock;
727
728 DBG("[thread] Relay live listener started");
729
730 rcu_register_thread();
731 health_register(health_relayd, HEALTH_RELAYD_TYPE_LIVE_LISTENER);
732
733 health_code_update();
734
735 live_control_sock = init_socket(live_uri, "Live listener");
736 if (!live_control_sock) {
737 goto error_sock_control;
738 }
739
740 /* Pass 2 as size here for the thread quit pipe and control sockets. */
741 ret = create_named_thread_poll_set(&events, 2,
742 "Live listener thread epoll");
743 if (ret < 0) {
744 goto error_create_poll;
745 }
746
747 /* Add the control socket */
748 ret = lttng_poll_add(&events, live_control_sock->fd, LPOLLIN | LPOLLRDHUP);
749 if (ret < 0) {
750 goto error_poll_add;
751 }
752
753 lttng_relay_notify_ready();
754
755 if (testpoint(relayd_thread_live_listener)) {
756 goto error_testpoint;
757 }
758
759 while (1) {
760 health_code_update();
761
762 DBG("Listener accepting live viewers connections");
763
764 restart:
765 health_poll_entry();
766 ret = lttng_poll_wait(&events, -1);
767 health_poll_exit();
768 if (ret < 0) {
769 /*
770 * Restart interrupted system call.
771 */
772 if (errno == EINTR) {
773 goto restart;
774 }
775 goto error;
776 }
777 nb_fd = ret;
778
779 DBG("Relay new viewer connection received");
780 for (i = 0; i < nb_fd; i++) {
781 /* Fetch once the poll data */
782 const auto revents = LTTNG_POLL_GETEV(&events, i);
783 const auto pollfd = LTTNG_POLL_GETFD(&events, i);
784
785 health_code_update();
786
787 /* Activity on thread quit pipe, exiting. */
788 if (relayd_is_thread_quit_pipe(pollfd)) {
789 DBG("Activity on thread quit pipe");
790 err = 0;
791 goto exit;
792 }
793
794 if (revents & LPOLLIN) {
795 /*
796 * A new connection is requested, therefore a
797 * viewer connection is allocated in this
798 * thread, enqueued to a global queue and
799 * dequeued (and freed) in the worker thread.
800 */
801 int val = 1;
802 struct relay_connection *new_conn;
803 struct lttcomm_sock *newsock;
804
805 newsock = accept_live_sock(live_control_sock,
806 "Live socket to client");
807 if (!newsock) {
808 PERROR("accepting control sock");
809 goto error;
810 }
811 DBG("Relay viewer connection accepted socket %d", newsock->fd);
812
813 ret = setsockopt(newsock->fd, SOL_SOCKET, SO_REUSEADDR, &val,
814 sizeof(val));
815 if (ret < 0) {
816 PERROR("setsockopt inet");
817 lttcomm_destroy_sock(newsock);
818 goto error;
819 }
820 new_conn = connection_create(newsock, RELAY_CONNECTION_UNKNOWN);
821 if (!new_conn) {
822 lttcomm_destroy_sock(newsock);
823 goto error;
824 }
825 /* Ownership assumed by the connection. */
826 newsock = NULL;
827
828 /* Enqueue request for the dispatcher thread. */
829 cds_wfcq_head_ptr_t head;
830 head.h = &viewer_conn_queue.head;
831 cds_wfcq_enqueue(head, &viewer_conn_queue.tail,
832 &new_conn->qnode);
833
834 /*
835 * Wake the dispatch queue futex.
836 * Implicit memory barrier with the
837 * exchange in cds_wfcq_enqueue.
838 */
839 futex_nto1_wake(&viewer_conn_queue.futex);
840 } else if (revents & (LPOLLERR | LPOLLHUP | LPOLLRDHUP)) {
841 ERR("socket poll error");
842 goto error;
843 } else {
844 ERR("Unexpected poll events %u for sock %d", revents, pollfd);
845 goto error;
846 }
847 }
848 }
849
850 exit:
851 error:
852 error_poll_add:
853 error_testpoint:
854 (void) fd_tracker_util_poll_clean(the_fd_tracker, &events);
855 error_create_poll:
856 if (live_control_sock->fd >= 0) {
857 int sock_fd = live_control_sock->fd;
858
859 ret = fd_tracker_close_unsuspendable_fd(the_fd_tracker,
860 &sock_fd, 1, close_sock,
861 live_control_sock);
862 if (ret) {
863 PERROR("close");
864 }
865 live_control_sock->fd = -1;
866 }
867 lttcomm_destroy_sock(live_control_sock);
868 error_sock_control:
869 if (err) {
870 health_error();
871 DBG("Live viewer listener thread exited with error");
872 }
873 health_unregister(health_relayd);
874 rcu_unregister_thread();
875 DBG("Live viewer listener thread cleanup complete");
876 if (lttng_relay_stop_threads()) {
877 ERR("Error stopping threads");
878 }
879 return NULL;
880 }
881
882 /*
883 * This thread manages the dispatching of the requests to worker threads
884 */
885 static
886 void *thread_dispatcher(void *data __attribute__((unused)))
887 {
888 int err = -1;
889 ssize_t ret;
890 struct cds_wfcq_node *node;
891 struct relay_connection *conn = NULL;
892
893 DBG("[thread] Live viewer relay dispatcher started");
894
895 health_register(health_relayd, HEALTH_RELAYD_TYPE_LIVE_DISPATCHER);
896
897 if (testpoint(relayd_thread_live_dispatcher)) {
898 goto error_testpoint;
899 }
900
901 health_code_update();
902
903 for (;;) {
904 health_code_update();
905
906 /* Atomically prepare the queue futex */
907 futex_nto1_prepare(&viewer_conn_queue.futex);
908
909 if (CMM_LOAD_SHARED(live_dispatch_thread_exit)) {
910 break;
911 }
912
913 do {
914 health_code_update();
915
916 /* Dequeue commands */
917 node = cds_wfcq_dequeue_blocking(&viewer_conn_queue.head,
918 &viewer_conn_queue.tail);
919 if (node == NULL) {
920 DBG("Woken up but nothing in the live-viewer "
921 "relay command queue");
922 /* Continue thread execution */
923 break;
924 }
925 conn = lttng::utils::container_of(node, &relay_connection::qnode);
926 DBG("Dispatching viewer request waiting on sock %d",
927 conn->sock->fd);
928
929 /*
930 * Inform worker thread of the new request. This
931 * call is blocking so we can be assured that
932 * the data will be read at some point in time
933 * or wait to the end of the world :)
934 */
935 ret = lttng_write(live_conn_pipe[1], &conn, sizeof(conn));
936 if (ret < 0) {
937 PERROR("write conn pipe");
938 connection_put(conn);
939 goto error;
940 }
941 } while (node != NULL);
942
943 /* Futex wait on queue. Blocking call on futex() */
944 health_poll_entry();
945 futex_nto1_wait(&viewer_conn_queue.futex);
946 health_poll_exit();
947 }
948
949 /* Normal exit, no error */
950 err = 0;
951
952 error:
953 error_testpoint:
954 if (err) {
955 health_error();
956 ERR("Health error occurred in %s", __func__);
957 }
958 health_unregister(health_relayd);
959 DBG("Live viewer dispatch thread dying");
960 if (lttng_relay_stop_threads()) {
961 ERR("Error stopping threads");
962 }
963 return NULL;
964 }
965
966 /*
967 * Establish connection with the viewer and check the versions.
968 *
969 * Return 0 on success or else negative value.
970 */
971 static
972 int viewer_connect(struct relay_connection *conn)
973 {
974 int ret;
975 struct lttng_viewer_connect reply, msg;
976
977 conn->version_check_done = 1;
978
979 health_code_update();
980
981 ret = recv_request(conn->sock, &msg, sizeof(msg));
982 if (ret < 0) {
983 goto end;
984 }
985
986 health_code_update();
987
988 memset(&reply, 0, sizeof(reply));
989 reply.major = RELAYD_VERSION_COMM_MAJOR;
990 reply.minor = RELAYD_VERSION_COMM_MINOR;
991
992 /* Major versions must be the same */
993 if (reply.major != be32toh(msg.major)) {
994 DBG("Incompatible major versions ([relayd] %u vs [client] %u)",
995 reply.major, be32toh(msg.major));
996 ret = -1;
997 goto end;
998 }
999
1000 conn->major = reply.major;
1001 /* We adapt to the lowest compatible version */
1002 if (reply.minor <= be32toh(msg.minor)) {
1003 conn->minor = reply.minor;
1004 } else {
1005 conn->minor = be32toh(msg.minor);
1006 }
1007
1008 if (be32toh(msg.type) == LTTNG_VIEWER_CLIENT_COMMAND) {
1009 conn->type = RELAY_VIEWER_COMMAND;
1010 } else if (be32toh(msg.type) == LTTNG_VIEWER_CLIENT_NOTIFICATION) {
1011 conn->type = RELAY_VIEWER_NOTIFICATION;
1012 } else {
1013 ERR("Unknown connection type : %u", be32toh(msg.type));
1014 ret = -1;
1015 goto end;
1016 }
1017
1018 reply.major = htobe32(reply.major);
1019 reply.minor = htobe32(reply.minor);
1020 if (conn->type == RELAY_VIEWER_COMMAND) {
1021 /*
1022 * Increment outside of htobe64 macro, because the argument can
1023 * be used more than once within the macro, and thus the
1024 * operation may be undefined.
1025 */
1026 pthread_mutex_lock(&last_relay_viewer_session_id_lock);
1027 last_relay_viewer_session_id++;
1028 pthread_mutex_unlock(&last_relay_viewer_session_id_lock);
1029 reply.viewer_session_id = htobe64(last_relay_viewer_session_id);
1030 }
1031
1032 health_code_update();
1033
1034 ret = send_response(conn->sock, &reply, sizeof(reply));
1035 if (ret < 0) {
1036 goto end;
1037 }
1038
1039 health_code_update();
1040
1041 DBG("Version check done using protocol %u.%u", conn->major, conn->minor);
1042 ret = 0;
1043
1044 end:
1045 return ret;
1046 }
1047
1048 /*
1049 * Send the viewer the list of current sessions.
1050 * We need to create a copy of the hash table content because otherwise
1051 * we cannot assume the number of entries stays the same between getting
1052 * the number of HT elements and iteration over the HT.
1053 *
1054 * Return 0 on success or else a negative value.
1055 */
1056 static
1057 int viewer_list_sessions(struct relay_connection *conn)
1058 {
1059 int ret = 0;
1060 struct lttng_viewer_list_sessions session_list;
1061 struct lttng_ht_iter iter;
1062 struct relay_session *session;
1063 struct lttng_viewer_session *send_session_buf = NULL;
1064 uint32_t buf_count = SESSION_BUF_DEFAULT_COUNT;
1065 uint32_t count = 0;
1066
1067 send_session_buf = calloc<lttng_viewer_session>(SESSION_BUF_DEFAULT_COUNT);
1068 if (!send_session_buf) {
1069 return -1;
1070 }
1071
1072 rcu_read_lock();
1073 cds_lfht_for_each_entry(sessions_ht->ht, &iter.iter, session,
1074 session_n.node) {
1075 struct lttng_viewer_session *send_session;
1076
1077 health_code_update();
1078
1079 pthread_mutex_lock(&session->lock);
1080 if (session->connection_closed) {
1081 /* Skip closed session */
1082 goto next_session;
1083 }
1084
1085 if (count >= buf_count) {
1086 struct lttng_viewer_session *newbuf;
1087 uint32_t new_buf_count = buf_count << 1;
1088
1089 newbuf = (lttng_viewer_session *) realloc(send_session_buf,
1090 new_buf_count * sizeof(*send_session_buf));
1091 if (!newbuf) {
1092 ret = -1;
1093 goto break_loop;
1094 }
1095 send_session_buf = newbuf;
1096 buf_count = new_buf_count;
1097 }
1098 send_session = &send_session_buf[count];
1099 if (lttng_strncpy(send_session->session_name,
1100 session->session_name,
1101 sizeof(send_session->session_name))) {
1102 ret = -1;
1103 goto break_loop;
1104 }
1105 if (lttng_strncpy(send_session->hostname, session->hostname,
1106 sizeof(send_session->hostname))) {
1107 ret = -1;
1108 goto break_loop;
1109 }
1110 send_session->id = htobe64(session->id);
1111 send_session->live_timer = htobe32(session->live_timer);
1112 if (session->viewer_attached) {
1113 send_session->clients = htobe32(1);
1114 } else {
1115 send_session->clients = htobe32(0);
1116 }
1117 send_session->streams = htobe32(session->stream_count);
1118 count++;
1119 next_session:
1120 pthread_mutex_unlock(&session->lock);
1121 continue;
1122 break_loop:
1123 pthread_mutex_unlock(&session->lock);
1124 break;
1125 }
1126 rcu_read_unlock();
1127 if (ret < 0) {
1128 goto end_free;
1129 }
1130
1131 session_list.sessions_count = htobe32(count);
1132
1133 health_code_update();
1134
1135 ret = send_response(conn->sock, &session_list, sizeof(session_list));
1136 if (ret < 0) {
1137 goto end_free;
1138 }
1139
1140 health_code_update();
1141
1142 ret = send_response(conn->sock, send_session_buf,
1143 count * sizeof(*send_session_buf));
1144 if (ret < 0) {
1145 goto end_free;
1146 }
1147 health_code_update();
1148
1149 ret = 0;
1150 end_free:
1151 free(send_session_buf);
1152 return ret;
1153 }
1154
1155 /*
1156 * Send the viewer the list of current streams.
1157 */
1158 static
1159 int viewer_get_new_streams(struct relay_connection *conn)
1160 {
1161 int ret, send_streams = 0;
1162 uint32_t nb_created = 0, nb_unsent = 0, nb_streams = 0, nb_total = 0;
1163 struct lttng_viewer_new_streams_request request;
1164 struct lttng_viewer_new_streams_response response;
1165 struct relay_session *session = NULL;
1166 uint64_t session_id;
1167 bool closed = false;
1168
1169 LTTNG_ASSERT(conn);
1170
1171 health_code_update();
1172
1173 /* Receive the request from the connected client. */
1174 ret = recv_request(conn->sock, &request, sizeof(request));
1175 if (ret < 0) {
1176 goto error;
1177 }
1178 session_id = be64toh(request.session_id);
1179
1180 health_code_update();
1181
1182 memset(&response, 0, sizeof(response));
1183
1184 session = session_get_by_id(session_id);
1185 if (!session) {
1186 DBG("Relay session %" PRIu64 " not found", session_id);
1187 response.status = htobe32(LTTNG_VIEWER_NEW_STREAMS_ERR);
1188 goto send_reply;
1189 }
1190
1191 if (!viewer_session_is_attached(conn->viewer_session, session)) {
1192 response.status = htobe32(LTTNG_VIEWER_NEW_STREAMS_ERR);
1193 goto send_reply;
1194 }
1195
1196 /*
1197 * For any new stream, create it with LTTNG_VIEWER_SEEK_BEGINNING since
1198 * that at this point the client is already attached to the session.Aany
1199 * initial stream will have been created with the seek type at attach
1200 * time (for now most readers use the LTTNG_VIEWER_SEEK_LAST on attach).
1201 * Otherwise any event happening in a new stream between the attach and
1202 * a call to viewer_get_new_streams will be "lost" (never received) from
1203 * the viewer's point of view.
1204 */
1205 pthread_mutex_lock(&session->lock);
1206 /*
1207 * If a session rotation is ongoing, do not attempt to open any
1208 * stream, because the chunk can be in an intermediate state
1209 * due to directory renaming.
1210 */
1211 if (session_has_ongoing_rotation(session)) {
1212 DBG("Relay session %" PRIu64 " rotation ongoing", session_id);
1213 response.status = htobe32(LTTNG_VIEWER_NEW_STREAMS_NO_NEW);
1214 goto send_reply_unlock;
1215 }
1216 ret = make_viewer_streams(session,
1217 conn->viewer_session,
1218 LTTNG_VIEWER_SEEK_BEGINNING, &nb_total, &nb_unsent,
1219 &nb_created, &closed);
1220 if (ret < 0) {
1221 /*
1222 * This is caused by an internal error; propagate the negative
1223 * 'ret' to close the connection.
1224 */
1225 response.status = htobe32(LTTNG_VIEWER_NEW_STREAMS_ERR);
1226 goto send_reply_unlock;
1227 }
1228 send_streams = 1;
1229 response.status = htobe32(LTTNG_VIEWER_NEW_STREAMS_OK);
1230
1231 /* Only send back the newly created streams with the unsent ones. */
1232 nb_streams = nb_created + nb_unsent;
1233 response.streams_count = htobe32(nb_streams);
1234
1235 /*
1236 * If the session is closed, HUP when there are no more streams
1237 * with data.
1238 */
1239 if (closed && nb_total == 0) {
1240 send_streams = 0;
1241 response.streams_count = 0;
1242 response.status = htobe32(LTTNG_VIEWER_NEW_STREAMS_HUP);
1243 goto send_reply_unlock;
1244 }
1245 send_reply_unlock:
1246 pthread_mutex_unlock(&session->lock);
1247
1248 send_reply:
1249 health_code_update();
1250 ret = send_response(conn->sock, &response, sizeof(response));
1251 if (ret < 0) {
1252 goto end_put_session;
1253 }
1254 health_code_update();
1255
1256 /*
1257 * Unknown or empty session, just return gracefully, the viewer
1258 * knows what is happening.
1259 */
1260 if (!send_streams || !nb_streams) {
1261 ret = 0;
1262 goto end_put_session;
1263 }
1264
1265 /*
1266 * Send stream and *DON'T* ignore the sent flag so every viewer
1267 * streams that were not sent from that point will be sent to
1268 * the viewer.
1269 */
1270 ret = send_viewer_streams(conn->sock, session_id, 0);
1271 if (ret < 0) {
1272 goto end_put_session;
1273 }
1274
1275 end_put_session:
1276 if (session) {
1277 session_put(session);
1278 }
1279 error:
1280 return ret;
1281 }
1282
1283 /*
1284 * Send the viewer the list of current sessions.
1285 */
1286 static
1287 int viewer_attach_session(struct relay_connection *conn)
1288 {
1289 int send_streams = 0;
1290 ssize_t ret;
1291 uint32_t nb_streams = 0;
1292 enum lttng_viewer_seek seek_type;
1293 struct lttng_viewer_attach_session_request request;
1294 struct lttng_viewer_attach_session_response response;
1295 struct relay_session *session = NULL;
1296 enum lttng_viewer_attach_return_code viewer_attach_status;
1297 bool closed = false;
1298 uint64_t session_id;
1299
1300 LTTNG_ASSERT(conn);
1301
1302 health_code_update();
1303
1304 /* Receive the request from the connected client. */
1305 ret = recv_request(conn->sock, &request, sizeof(request));
1306 if (ret < 0) {
1307 goto error;
1308 }
1309
1310 session_id = be64toh(request.session_id);
1311
1312 health_code_update();
1313
1314 memset(&response, 0, sizeof(response));
1315
1316 if (!conn->viewer_session) {
1317 viewer_attach_status = LTTNG_VIEWER_ATTACH_NO_SESSION;
1318 DBG("Client trying to attach before creating a live viewer session, returning status=%s",
1319 lttng_viewer_attach_return_code_str(viewer_attach_status));
1320 goto send_reply;
1321 }
1322
1323 session = session_get_by_id(session_id);
1324 if (!session) {
1325 viewer_attach_status = LTTNG_VIEWER_ATTACH_UNK;
1326 DBG("Relay session %" PRIu64 " not found, returning status=%s",
1327 session_id,
1328 lttng_viewer_attach_return_code_str(viewer_attach_status));
1329 goto send_reply;
1330 }
1331 DBG("Attach relay session ID %" PRIu64 " received", session_id);
1332
1333 pthread_mutex_lock(&session->lock);
1334 if (session->live_timer == 0) {
1335 viewer_attach_status = LTTNG_VIEWER_ATTACH_NOT_LIVE;
1336 DBG("Relay session ID %" PRIu64 " is not a live session, returning status=%s",
1337 session_id,
1338 lttng_viewer_attach_return_code_str(viewer_attach_status));
1339 goto send_reply;
1340 }
1341
1342 send_streams = 1;
1343 viewer_attach_status = viewer_session_attach(conn->viewer_session,
1344 session);
1345 if (viewer_attach_status != LTTNG_VIEWER_ATTACH_OK) {
1346 DBG("Error attaching to relay session %" PRIu64 ", returning status=%s",
1347 session_id,
1348 lttng_viewer_attach_return_code_str(viewer_attach_status));
1349 goto send_reply;
1350 }
1351
1352 switch (be32toh(request.seek)) {
1353 case LTTNG_VIEWER_SEEK_BEGINNING:
1354 case LTTNG_VIEWER_SEEK_LAST:
1355 viewer_attach_status = LTTNG_VIEWER_ATTACH_OK;
1356 seek_type = (lttng_viewer_seek) be32toh(request.seek);
1357 break;
1358 default:
1359 ERR("Wrong seek parameter for relay session %" PRIu64
1360 ", returning status=%s", session_id,
1361 lttng_viewer_attach_return_code_str(viewer_attach_status));
1362 viewer_attach_status = LTTNG_VIEWER_ATTACH_SEEK_ERR;
1363 send_streams = 0;
1364 goto send_reply;
1365 }
1366
1367 /*
1368 * If a session rotation is ongoing, do not attempt to open any
1369 * stream, because the chunk can be in an intermediate state
1370 * due to directory renaming.
1371 */
1372 if (session_has_ongoing_rotation(session)) {
1373 DBG("Relay session %" PRIu64 " rotation ongoing", session_id);
1374 send_streams = 0;
1375 goto send_reply;
1376 }
1377
1378 ret = make_viewer_streams(session,
1379 conn->viewer_session, seek_type,
1380 &nb_streams, NULL, NULL, &closed);
1381 if (ret < 0) {
1382 goto end_put_session;
1383 }
1384 pthread_mutex_unlock(&session->lock);
1385 session_put(session);
1386 session = NULL;
1387
1388 response.streams_count = htobe32(nb_streams);
1389 /*
1390 * If the session is closed when the viewer is attaching, it
1391 * means some of the streams may have been concurrently removed,
1392 * so we don't allow the viewer to attach, even if there are
1393 * streams available.
1394 */
1395 if (closed) {
1396 send_streams = 0;
1397 response.streams_count = 0;
1398 viewer_attach_status = LTTNG_VIEWER_ATTACH_UNK;
1399 ERR("Session %" PRIu64 " is closed, returning status=%s",
1400 session_id,
1401 lttng_viewer_attach_return_code_str(viewer_attach_status));
1402 goto send_reply;
1403 }
1404
1405 send_reply:
1406 health_code_update();
1407
1408 response.status = htobe32((uint32_t) viewer_attach_status);
1409
1410 ret = send_response(conn->sock, &response, sizeof(response));
1411 if (ret < 0) {
1412 goto end_put_session;
1413 }
1414 health_code_update();
1415
1416 /*
1417 * Unknown or empty session, just return gracefully, the viewer
1418 * knows what is happening.
1419 */
1420 if (!send_streams || !nb_streams) {
1421 ret = 0;
1422 goto end_put_session;
1423 }
1424
1425 /* Send stream and ignore the sent flag. */
1426 ret = send_viewer_streams(conn->sock, session_id, 1);
1427 if (ret < 0) {
1428 goto end_put_session;
1429 }
1430
1431 end_put_session:
1432 if (session) {
1433 pthread_mutex_unlock(&session->lock);
1434 session_put(session);
1435 }
1436 error:
1437 return ret;
1438 }
1439
1440 /*
1441 * Open the index file if needed for the given vstream.
1442 *
1443 * If an index file is successfully opened, the vstream will set it as its
1444 * current index file.
1445 *
1446 * Return 0 on success, a negative value on error (-ENOENT if not ready yet).
1447 *
1448 * Called with rstream lock held.
1449 */
1450 static int try_open_index(struct relay_viewer_stream *vstream,
1451 struct relay_stream *rstream)
1452 {
1453 int ret = 0;
1454 const uint32_t connection_major = rstream->trace->session->major;
1455 const uint32_t connection_minor = rstream->trace->session->minor;
1456 enum lttng_trace_chunk_status chunk_status;
1457
1458 if (vstream->index_file) {
1459 goto end;
1460 }
1461
1462 /*
1463 * First time, we open the index file and at least one index is ready.
1464 */
1465 if (rstream->index_received_seqcount == 0 ||
1466 !vstream->stream_file.trace_chunk) {
1467 ret = -ENOENT;
1468 goto end;
1469 }
1470
1471 chunk_status = lttng_index_file_create_from_trace_chunk_read_only(
1472 vstream->stream_file.trace_chunk, rstream->path_name,
1473 rstream->channel_name, rstream->tracefile_size,
1474 vstream->current_tracefile_id,
1475 lttng_to_index_major(connection_major, connection_minor),
1476 lttng_to_index_minor(connection_major, connection_minor),
1477 true, &vstream->index_file);
1478 if (chunk_status != LTTNG_TRACE_CHUNK_STATUS_OK) {
1479 if (chunk_status == LTTNG_TRACE_CHUNK_STATUS_NO_FILE) {
1480 ret = -ENOENT;
1481 } else {
1482 ret = -1;
1483 }
1484 }
1485
1486 end:
1487 return ret;
1488 }
1489
1490 /*
1491 * Check the status of the index for the given stream. This function
1492 * updates the index structure if needed and can put (close) the vstream
1493 * in the HUP situation.
1494 *
1495 * Return 0 means that we can proceed with the index. A value of 1 means
1496 * that the index has been updated and is ready to be sent to the
1497 * client. A negative value indicates an error that can't be handled.
1498 *
1499 * Called with rstream lock held.
1500 */
1501 static int check_index_status(struct relay_viewer_stream *vstream,
1502 struct relay_stream *rstream, struct ctf_trace *trace,
1503 struct lttng_viewer_index *index)
1504 {
1505 int ret;
1506
1507 DBG("Check index status: index_received_seqcount %" PRIu64 " "
1508 "index_sent_seqcount %" PRIu64 " "
1509 "for stream %" PRIu64,
1510 rstream->index_received_seqcount,
1511 vstream->index_sent_seqcount,
1512 vstream->stream->stream_handle);
1513 if ((trace->session->connection_closed || rstream->closed)
1514 && rstream->index_received_seqcount
1515 == vstream->index_sent_seqcount) {
1516 /*
1517 * Last index sent and session connection or relay
1518 * stream are closed.
1519 */
1520 index->status = LTTNG_VIEWER_INDEX_HUP;
1521 DBG("Check index status: Connection or stream are closed, stream %" PRIu64
1522 ",connection-closed=%d, relay-stream-closed=%d, returning status=%s",
1523 vstream->stream->stream_handle,
1524 trace->session->connection_closed, rstream->closed,
1525 lttng_viewer_next_index_return_code_str(
1526 (enum lttng_viewer_next_index_return_code) index->status));
1527 goto hup;
1528 } else if (rstream->beacon_ts_end != -1ULL &&
1529 (rstream->index_received_seqcount == 0 ||
1530 (vstream->index_sent_seqcount != 0 &&
1531 rstream->index_received_seqcount
1532 <= vstream->index_sent_seqcount))) {
1533 /*
1534 * We've received a synchronization beacon and the last index
1535 * available has been sent, the index for now is inactive.
1536 *
1537 * In this case, we have received a beacon which allows us to
1538 * inform the client of a time interval during which we can
1539 * guarantee that there are no events to read (and never will
1540 * be).
1541 *
1542 * The sent seqcount can grow higher than receive seqcount on
1543 * clear because the rotation performed by clear will push
1544 * the index_sent_seqcount ahead (see
1545 * viewer_stream_sync_tracefile_array_tail) and skip over
1546 * packet sequence numbers.
1547 */
1548 index->status = LTTNG_VIEWER_INDEX_INACTIVE;
1549 index->timestamp_end = htobe64(rstream->beacon_ts_end);
1550 index->stream_id = htobe64(rstream->ctf_stream_id);
1551 DBG("Check index status: inactive with beacon, for stream %" PRIu64
1552 ", returning status=%s",
1553 vstream->stream->stream_handle,
1554 lttng_viewer_next_index_return_code_str(
1555 (enum lttng_viewer_next_index_return_code) index->status));
1556 goto index_ready;
1557 } else if (rstream->index_received_seqcount == 0 ||
1558 (vstream->index_sent_seqcount != 0 &&
1559 rstream->index_received_seqcount
1560 <= vstream->index_sent_seqcount)) {
1561 /*
1562 * This checks whether received <= sent seqcount. In
1563 * this case, we have not received a beacon. Therefore,
1564 * we can only ask the client to retry later.
1565 *
1566 * The sent seqcount can grow higher than receive seqcount on
1567 * clear because the rotation performed by clear will push
1568 * the index_sent_seqcount ahead (see
1569 * viewer_stream_sync_tracefile_array_tail) and skip over
1570 * packet sequence numbers.
1571 */
1572 index->status = LTTNG_VIEWER_INDEX_RETRY;
1573 DBG("Check index status:"
1574 "did not received beacon for stream %" PRIu64
1575 ", returning status=%s",
1576 vstream->stream->stream_handle,
1577 lttng_viewer_next_index_return_code_str(
1578 (enum lttng_viewer_next_index_return_code) index->status));
1579 goto index_ready;
1580 } else if (!tracefile_array_seq_in_file(rstream->tfa,
1581 vstream->current_tracefile_id,
1582 vstream->index_sent_seqcount)) {
1583 /*
1584 * The next index we want to send cannot be read either
1585 * because we need to perform a rotation, or due to
1586 * the producer having overwritten its trace file.
1587 */
1588 DBG("Viewer stream %" PRIu64 " rotation",
1589 vstream->stream->stream_handle);
1590 ret = viewer_stream_rotate(vstream);
1591 if (ret == 1) {
1592 /* EOF across entire stream. */
1593 index->status = LTTNG_VIEWER_INDEX_HUP;
1594 DBG("Check index status:"
1595 "reached end of file for stream %" PRIu64
1596 ", returning status=%s",
1597 vstream->stream->stream_handle,
1598 lttng_viewer_next_index_return_code_str(
1599 (enum lttng_viewer_next_index_return_code) index->status));
1600 goto hup;
1601 }
1602 /*
1603 * If we have been pushed due to overwrite, it
1604 * necessarily means there is data that can be read in
1605 * the stream. If we rotated because we reached the end
1606 * of a tracefile, it means the following tracefile
1607 * needs to contain at least one index, else we would
1608 * have already returned LTTNG_VIEWER_INDEX_RETRY to the
1609 * viewer. The updated index_sent_seqcount needs to
1610 * point to a readable index entry now.
1611 *
1612 * In the case where we "rotate" on a single file, we
1613 * can end up in a case where the requested index is
1614 * still unavailable.
1615 */
1616 if (rstream->tracefile_count == 1 &&
1617 !tracefile_array_seq_in_file(
1618 rstream->tfa,
1619 vstream->current_tracefile_id,
1620 vstream->index_sent_seqcount)) {
1621 index->status = LTTNG_VIEWER_INDEX_RETRY;
1622 DBG("Check index status:"
1623 "tracefile array sequence number %" PRIu64
1624 " not in file for stream %" PRIu64
1625 ", returning status=%s",
1626 vstream->index_sent_seqcount,
1627 vstream->stream->stream_handle,
1628 lttng_viewer_next_index_return_code_str(
1629 (enum lttng_viewer_next_index_return_code) index->status));
1630 goto index_ready;
1631 }
1632 LTTNG_ASSERT(tracefile_array_seq_in_file(rstream->tfa,
1633 vstream->current_tracefile_id,
1634 vstream->index_sent_seqcount));
1635 }
1636 /* ret == 0 means successful so we continue. */
1637 ret = 0;
1638 return ret;
1639
1640 hup:
1641 viewer_stream_put(vstream);
1642 index_ready:
1643 return 1;
1644 }
1645
1646 static
1647 void viewer_stream_rotate_to_trace_chunk(struct relay_viewer_stream *vstream,
1648 struct lttng_trace_chunk *new_trace_chunk)
1649 {
1650 lttng_trace_chunk_put(vstream->stream_file.trace_chunk);
1651
1652 if (new_trace_chunk) {
1653 const bool acquired_reference = lttng_trace_chunk_get(
1654 new_trace_chunk);
1655
1656 LTTNG_ASSERT(acquired_reference);
1657 }
1658
1659 vstream->stream_file.trace_chunk = new_trace_chunk;
1660 viewer_stream_sync_tracefile_array_tail(vstream);
1661 viewer_stream_close_files(vstream);
1662 }
1663
1664 /*
1665 * Send the next index for a stream.
1666 *
1667 * Return 0 on success or else a negative value.
1668 */
1669 static
1670 int viewer_get_next_index(struct relay_connection *conn)
1671 {
1672 int ret;
1673 struct lttng_viewer_get_next_index request_index;
1674 struct lttng_viewer_index viewer_index;
1675 struct ctf_packet_index packet_index;
1676 struct relay_viewer_stream *vstream = NULL;
1677 struct relay_stream *rstream = NULL;
1678 struct ctf_trace *ctf_trace = NULL;
1679 struct relay_viewer_stream *metadata_viewer_stream = NULL;
1680 bool viewer_stream_and_session_in_same_chunk, viewer_stream_one_rotation_behind;
1681 uint64_t stream_file_chunk_id = -1ULL, viewer_session_chunk_id = -1ULL;
1682 enum lttng_trace_chunk_status status;
1683
1684 LTTNG_ASSERT(conn);
1685
1686 memset(&viewer_index, 0, sizeof(viewer_index));
1687 health_code_update();
1688
1689 ret = recv_request(conn->sock, &request_index, sizeof(request_index));
1690 if (ret < 0) {
1691 goto end;
1692 }
1693 health_code_update();
1694
1695 vstream = viewer_stream_get_by_id(be64toh(request_index.stream_id));
1696 if (!vstream) {
1697 viewer_index.status = LTTNG_VIEWER_INDEX_ERR;
1698 DBG("Client requested index of unknown stream id %" PRIu64", returning status=%s",
1699 (uint64_t) be64toh(request_index.stream_id),
1700 lttng_viewer_next_index_return_code_str(
1701 (enum lttng_viewer_next_index_return_code) viewer_index.status));
1702 goto send_reply;
1703 }
1704
1705 /* Use back. ref. Protected by refcounts. */
1706 rstream = vstream->stream;
1707 ctf_trace = rstream->trace;
1708
1709 /* metadata_viewer_stream may be NULL. */
1710 metadata_viewer_stream =
1711 ctf_trace_get_viewer_metadata_stream(ctf_trace);
1712
1713 /*
1714 * Hold the session lock to protect against concurrent changes
1715 * to the chunk files (e.g. rename done by clear), which are
1716 * protected by the session ongoing rotation state. Those are
1717 * synchronized with the session lock.
1718 */
1719 pthread_mutex_lock(&rstream->trace->session->lock);
1720 pthread_mutex_lock(&rstream->lock);
1721
1722 /*
1723 * The viewer should not ask for index on metadata stream.
1724 */
1725 if (rstream->is_metadata) {
1726 viewer_index.status = LTTNG_VIEWER_INDEX_HUP;
1727 DBG("Client requested index of a metadata stream id %" PRIu64", returning status=%s",
1728 (uint64_t) be64toh(request_index.stream_id),
1729 lttng_viewer_next_index_return_code_str(
1730 (enum lttng_viewer_next_index_return_code) viewer_index.status));
1731 goto send_reply;
1732 }
1733
1734 if (rstream->ongoing_rotation.is_set) {
1735 /* Rotation is ongoing, try again later. */
1736 viewer_index.status = LTTNG_VIEWER_INDEX_RETRY;
1737 DBG("Client requested index for stream id %" PRIu64" while a stream rotation is ongoing, returning status=%s",
1738 (uint64_t) be64toh(request_index.stream_id),
1739 lttng_viewer_next_index_return_code_str(
1740 (enum lttng_viewer_next_index_return_code) viewer_index.status));
1741 goto send_reply;
1742 }
1743
1744 if (session_has_ongoing_rotation(rstream->trace->session)) {
1745 /* Rotation is ongoing, try again later. */
1746 viewer_index.status = LTTNG_VIEWER_INDEX_RETRY;
1747 DBG("Client requested index for stream id %" PRIu64" while a session rotation is ongoing, returning status=%s",
1748 (uint64_t) be64toh(request_index.stream_id),
1749 lttng_viewer_next_index_return_code_str(
1750 (enum lttng_viewer_next_index_return_code) viewer_index.status));
1751 goto send_reply;
1752 }
1753
1754 /*
1755 * Transition the viewer session into the newest trace chunk available.
1756 */
1757 if (!lttng_trace_chunk_ids_equal(
1758 conn->viewer_session->current_trace_chunk,
1759 rstream->trace_chunk)) {
1760 DBG("Relay stream and viewer chunk ids differ");
1761
1762 ret = viewer_session_set_trace_chunk_copy(
1763 conn->viewer_session,
1764 rstream->trace_chunk);
1765 if (ret) {
1766 viewer_index.status = LTTNG_VIEWER_INDEX_ERR;
1767 ERR("Error copying trace chunk for stream id %" PRIu64
1768 ", returning status=%s",
1769 (uint64_t) be64toh(request_index.stream_id),
1770 lttng_viewer_next_index_return_code_str(
1771 (enum lttng_viewer_next_index_return_code) viewer_index.status));
1772 goto send_reply;
1773 }
1774 }
1775
1776 /*
1777 * Transition the viewer stream into the latest trace chunk available.
1778 *
1779 * Note that the stream must _not_ rotate in one precise condition:
1780 * the relay stream has rotated to a NULL trace chunk and the viewer
1781 * stream is consuming the trace chunk that was active just before
1782 * that rotation to NULL.
1783 *
1784 * This allows clients to consume all the packets of a trace chunk
1785 * after a session's destruction.
1786 */
1787 if (vstream->stream_file.trace_chunk) {
1788 status = lttng_trace_chunk_get_id(
1789 vstream->stream_file.trace_chunk,
1790 &stream_file_chunk_id);
1791 LTTNG_ASSERT(status == LTTNG_TRACE_CHUNK_STATUS_OK);
1792 }
1793 if (conn->viewer_session->current_trace_chunk) {
1794 status = lttng_trace_chunk_get_id(
1795 conn->viewer_session->current_trace_chunk,
1796 &viewer_session_chunk_id);
1797 LTTNG_ASSERT(status == LTTNG_TRACE_CHUNK_STATUS_OK);
1798 }
1799
1800 viewer_stream_and_session_in_same_chunk = lttng_trace_chunk_ids_equal(
1801 conn->viewer_session->current_trace_chunk,
1802 vstream->stream_file.trace_chunk);
1803 viewer_stream_one_rotation_behind = rstream->completed_rotation_count ==
1804 vstream->last_seen_rotation_count + 1;
1805
1806 if (viewer_stream_and_session_in_same_chunk) {
1807 DBG("Transition to latest chunk check (%s -> %s): Same chunk, no need to rotate",
1808 vstream->stream_file.trace_chunk ?
1809 std::to_string(stream_file_chunk_id).c_str() :
1810 "None",
1811 conn->viewer_session->current_trace_chunk ?
1812 std::to_string(viewer_session_chunk_id).c_str() :
1813 "None");
1814 } else if (viewer_stream_one_rotation_behind && !rstream->trace_chunk) {
1815 DBG("Transition to latest chunk check (%s -> %s): One chunk behind relay stream which is being destroyed, no need to rotate",
1816 vstream->stream_file.trace_chunk ?
1817 std::to_string(stream_file_chunk_id).c_str() :
1818 "None",
1819 conn->viewer_session->current_trace_chunk ?
1820 std::to_string(viewer_session_chunk_id).c_str() :
1821 "None");
1822 } else {
1823 DBG("Transition to latest chunk check (%s -> %s): Viewer stream chunk ID and viewer session chunk ID differ, rotating viewer stream",
1824 vstream->stream_file.trace_chunk ?
1825 std::to_string(stream_file_chunk_id).c_str() :
1826 "None",
1827 conn->viewer_session->current_trace_chunk ?
1828 std::to_string(viewer_session_chunk_id).c_str() :
1829 "None");
1830
1831 viewer_stream_rotate_to_trace_chunk(vstream,
1832 conn->viewer_session->current_trace_chunk);
1833 vstream->last_seen_rotation_count =
1834 rstream->completed_rotation_count;
1835 }
1836
1837 ret = check_index_status(vstream, rstream, ctf_trace, &viewer_index);
1838 if (ret < 0) {
1839 goto error_put;
1840 } else if (ret == 1) {
1841 /*
1842 * We have no index to send and check_index_status has populated
1843 * viewer_index's status.
1844 */
1845 goto send_reply;
1846 }
1847 /* At this point, ret is 0 thus we will be able to read the index. */
1848 LTTNG_ASSERT(!ret);
1849
1850 /* Try to open an index if one is needed for that stream. */
1851 ret = try_open_index(vstream, rstream);
1852 if (ret == -ENOENT) {
1853 if (rstream->closed) {
1854 viewer_index.status = LTTNG_VIEWER_INDEX_HUP;
1855 DBG("Cannot open index for stream id %" PRIu64
1856 "stream is closed, returning status=%s",
1857 (uint64_t) be64toh(request_index.stream_id),
1858 lttng_viewer_next_index_return_code_str(
1859 (enum lttng_viewer_next_index_return_code) viewer_index.status));
1860 goto send_reply;
1861 } else {
1862 viewer_index.status = LTTNG_VIEWER_INDEX_RETRY;
1863 DBG("Cannot open index for stream id %" PRIu64
1864 ", returning status=%s",
1865 (uint64_t) be64toh(request_index.stream_id),
1866 lttng_viewer_next_index_return_code_str(
1867 (enum lttng_viewer_next_index_return_code) viewer_index.status));
1868 goto send_reply;
1869 }
1870 }
1871 if (ret < 0) {
1872 viewer_index.status = LTTNG_VIEWER_INDEX_ERR;
1873 ERR("Error opening index for stream id %" PRIu64
1874 ", returning status=%s",
1875 (uint64_t) be64toh(request_index.stream_id),
1876 lttng_viewer_next_index_return_code_str(
1877 (enum lttng_viewer_next_index_return_code) viewer_index.status));
1878 goto send_reply;
1879 }
1880
1881 /*
1882 * vstream->stream_fd may be NULL if it has been closed by
1883 * tracefile rotation, or if we are at the beginning of the
1884 * stream. We open the data stream file here to protect against
1885 * overwrite caused by tracefile rotation (in association with
1886 * unlink performed before overwrite).
1887 */
1888 if (!vstream->stream_file.handle) {
1889 char file_path[LTTNG_PATH_MAX];
1890 struct fs_handle *fs_handle;
1891
1892 ret = utils_stream_file_path(rstream->path_name,
1893 rstream->channel_name, rstream->tracefile_size,
1894 vstream->current_tracefile_id, NULL, file_path,
1895 sizeof(file_path));
1896 if (ret < 0) {
1897 goto error_put;
1898 }
1899
1900 /*
1901 * It is possible the the file we are trying to open is
1902 * missing if the stream has been closed (application exits with
1903 * per-pid buffers) and a clear command has been performed.
1904 */
1905 status = lttng_trace_chunk_open_fs_handle(
1906 vstream->stream_file.trace_chunk,
1907 file_path, O_RDONLY, 0, &fs_handle, true);
1908 if (status != LTTNG_TRACE_CHUNK_STATUS_OK) {
1909 if (status == LTTNG_TRACE_CHUNK_STATUS_NO_FILE &&
1910 rstream->closed) {
1911 viewer_index.status = LTTNG_VIEWER_INDEX_HUP;
1912 DBG("Cannot find trace chunk file and stream is closed for stream id %" PRIu64
1913 ", returning status=%s",
1914 (uint64_t) be64toh(request_index.stream_id),
1915 lttng_viewer_next_index_return_code_str(
1916 (enum lttng_viewer_next_index_return_code) viewer_index.status));
1917 goto send_reply;
1918 }
1919 PERROR("Failed to open trace file for viewer stream");
1920 goto error_put;
1921 }
1922 vstream->stream_file.handle = fs_handle;
1923 }
1924
1925 ret = check_new_streams(conn);
1926 if (ret < 0) {
1927 viewer_index.status = LTTNG_VIEWER_INDEX_ERR;
1928 ERR("Error checking for new streams before sending new index to stream id %" PRIu64
1929 ", returning status=%s",
1930 (uint64_t) be64toh(request_index.stream_id),
1931 lttng_viewer_next_index_return_code_str(
1932 (enum lttng_viewer_next_index_return_code) viewer_index.status));
1933 goto send_reply;
1934 } else if (ret == 1) {
1935 viewer_index.flags |= LTTNG_VIEWER_FLAG_NEW_STREAM;
1936 }
1937
1938 ret = lttng_index_file_read(vstream->index_file, &packet_index);
1939 if (ret) {
1940 viewer_index.status = LTTNG_VIEWER_INDEX_ERR;
1941 ERR("Relay error reading index file for stream id %" PRIu64
1942 ", returning status=%s",
1943 (uint64_t) be64toh(request_index.stream_id),
1944 lttng_viewer_next_index_return_code_str(
1945 (enum lttng_viewer_next_index_return_code) viewer_index.status));
1946 goto send_reply;
1947 } else {
1948 viewer_index.status = LTTNG_VIEWER_INDEX_OK;
1949 DBG("Read index file for stream id %" PRIu64
1950 ", returning status=%s",
1951 (uint64_t) be64toh(request_index.stream_id),
1952 lttng_viewer_next_index_return_code_str(
1953 (enum lttng_viewer_next_index_return_code) viewer_index.status));
1954 vstream->index_sent_seqcount++;
1955 }
1956
1957 /*
1958 * Indexes are stored in big endian, no need to switch before sending.
1959 */
1960 DBG("Sending viewer index for stream %" PRIu64 " offset %" PRIu64,
1961 rstream->stream_handle,
1962 (uint64_t) be64toh(packet_index.offset));
1963 viewer_index.offset = packet_index.offset;
1964 viewer_index.packet_size = packet_index.packet_size;
1965 viewer_index.content_size = packet_index.content_size;
1966 viewer_index.timestamp_begin = packet_index.timestamp_begin;
1967 viewer_index.timestamp_end = packet_index.timestamp_end;
1968 viewer_index.events_discarded = packet_index.events_discarded;
1969 viewer_index.stream_id = packet_index.stream_id;
1970
1971 send_reply:
1972 if (rstream) {
1973 pthread_mutex_unlock(&rstream->lock);
1974 pthread_mutex_unlock(&rstream->trace->session->lock);
1975 }
1976
1977 if (metadata_viewer_stream) {
1978 pthread_mutex_lock(&metadata_viewer_stream->stream->lock);
1979 DBG("get next index metadata check: recv %" PRIu64
1980 " sent %" PRIu64,
1981 metadata_viewer_stream->stream->metadata_received,
1982 metadata_viewer_stream->metadata_sent);
1983 if (!metadata_viewer_stream->stream->metadata_received ||
1984 metadata_viewer_stream->stream->metadata_received >
1985 metadata_viewer_stream->metadata_sent) {
1986 viewer_index.flags |= LTTNG_VIEWER_FLAG_NEW_METADATA;
1987 }
1988 pthread_mutex_unlock(&metadata_viewer_stream->stream->lock);
1989 }
1990
1991 viewer_index.flags = htobe32(viewer_index.flags);
1992 viewer_index.status = htobe32(viewer_index.status);
1993 health_code_update();
1994
1995 ret = send_response(conn->sock, &viewer_index, sizeof(viewer_index));
1996 if (ret < 0) {
1997 goto end;
1998 }
1999 health_code_update();
2000
2001 if (vstream) {
2002 DBG("Index %" PRIu64 " for stream %" PRIu64 " sent",
2003 vstream->index_sent_seqcount,
2004 vstream->stream->stream_handle);
2005 }
2006 end:
2007 if (metadata_viewer_stream) {
2008 viewer_stream_put(metadata_viewer_stream);
2009 }
2010 if (vstream) {
2011 viewer_stream_put(vstream);
2012 }
2013 return ret;
2014
2015 error_put:
2016 pthread_mutex_unlock(&rstream->lock);
2017 pthread_mutex_unlock(&rstream->trace->session->lock);
2018 if (metadata_viewer_stream) {
2019 viewer_stream_put(metadata_viewer_stream);
2020 }
2021 viewer_stream_put(vstream);
2022 return ret;
2023 }
2024
2025 /*
2026 * Send the next index for a stream
2027 *
2028 * Return 0 on success or else a negative value.
2029 */
2030 static
2031 int viewer_get_packet(struct relay_connection *conn)
2032 {
2033 int ret;
2034 off_t lseek_ret;
2035 char *reply = NULL;
2036 struct lttng_viewer_get_packet get_packet_info;
2037 struct lttng_viewer_trace_packet reply_header;
2038 struct relay_viewer_stream *vstream = NULL;
2039 uint32_t reply_size = sizeof(reply_header);
2040 uint32_t packet_data_len = 0;
2041 ssize_t read_len;
2042 uint64_t stream_id;
2043 enum lttng_viewer_get_packet_return_code get_packet_status;
2044
2045 health_code_update();
2046
2047 ret = recv_request(conn->sock, &get_packet_info,
2048 sizeof(get_packet_info));
2049 if (ret < 0) {
2050 goto end;
2051 }
2052 health_code_update();
2053
2054 /* From this point on, the error label can be reached. */
2055 memset(&reply_header, 0, sizeof(reply_header));
2056 stream_id = (uint64_t) be64toh(get_packet_info.stream_id);
2057
2058 vstream = viewer_stream_get_by_id(stream_id);
2059 if (!vstream) {
2060 get_packet_status = LTTNG_VIEWER_GET_PACKET_ERR;
2061 DBG("Client requested packet of unknown stream id %" PRIu64
2062 ", returning status=%s", stream_id,
2063 lttng_viewer_get_packet_return_code_str(get_packet_status));
2064 goto send_reply_nolock;
2065 } else {
2066 packet_data_len = be32toh(get_packet_info.len);
2067 reply_size += packet_data_len;
2068 }
2069
2070 reply = zmalloc<char>(reply_size);
2071 if (!reply) {
2072 get_packet_status = LTTNG_VIEWER_GET_PACKET_ERR;
2073 PERROR("Falled to allocate reply, returning status=%s",
2074 lttng_viewer_get_packet_return_code_str(get_packet_status));
2075 goto error;
2076 }
2077
2078 pthread_mutex_lock(&vstream->stream->lock);
2079 lseek_ret = fs_handle_seek(vstream->stream_file.handle,
2080 be64toh(get_packet_info.offset), SEEK_SET);
2081 if (lseek_ret < 0) {
2082 get_packet_status = LTTNG_VIEWER_GET_PACKET_ERR;
2083 PERROR("Failed to seek file system handle of viewer stream %" PRIu64
2084 " to offset %" PRIu64", returning status=%s", stream_id,
2085 (uint64_t) be64toh(get_packet_info.offset),
2086 lttng_viewer_get_packet_return_code_str(get_packet_status));
2087 goto error;
2088 }
2089 read_len = fs_handle_read(vstream->stream_file.handle,
2090 reply + sizeof(reply_header), packet_data_len);
2091 if (read_len < packet_data_len) {
2092 get_packet_status = LTTNG_VIEWER_GET_PACKET_ERR;
2093 PERROR("Failed to read from file system handle of viewer stream id %" PRIu64
2094 ", offset: %" PRIu64 ", returning status=%s", stream_id,
2095 (uint64_t) be64toh(get_packet_info.offset),
2096 lttng_viewer_get_packet_return_code_str(get_packet_status));
2097 goto error;
2098 }
2099
2100 get_packet_status = LTTNG_VIEWER_GET_PACKET_OK;
2101 reply_header.len = htobe32(packet_data_len);
2102 goto send_reply;
2103
2104 error:
2105 /* No payload to send on error. */
2106 reply_size = sizeof(reply_header);
2107
2108 send_reply:
2109 if (vstream) {
2110 pthread_mutex_unlock(&vstream->stream->lock);
2111 }
2112 send_reply_nolock:
2113
2114 health_code_update();
2115
2116 reply_header.status = htobe32(get_packet_status);
2117 if (reply) {
2118 memcpy(reply, &reply_header, sizeof(reply_header));
2119 ret = send_response(conn->sock, reply, reply_size);
2120 } else {
2121 /* No reply to send. */
2122 ret = send_response(conn->sock, &reply_header,
2123 reply_size);
2124 }
2125
2126 health_code_update();
2127 if (ret < 0) {
2128 PERROR("sendmsg of packet data failed");
2129 goto end_free;
2130 }
2131
2132 DBG("Sent %u bytes for stream %" PRIu64, reply_size, stream_id);
2133
2134 end_free:
2135 free(reply);
2136 end:
2137 if (vstream) {
2138 viewer_stream_put(vstream);
2139 }
2140 return ret;
2141 }
2142
2143 /*
2144 * Send the session's metadata
2145 *
2146 * Return 0 on success else a negative value.
2147 */
2148 static
2149 int viewer_get_metadata(struct relay_connection *conn)
2150 {
2151 int ret = 0;
2152 int fd = -1;
2153 ssize_t read_len;
2154 uint64_t len = 0;
2155 char *data = NULL;
2156 struct lttng_viewer_get_metadata request;
2157 struct lttng_viewer_metadata_packet reply;
2158 struct relay_viewer_stream *vstream = NULL;
2159
2160 LTTNG_ASSERT(conn);
2161
2162 health_code_update();
2163
2164 ret = recv_request(conn->sock, &request, sizeof(request));
2165 if (ret < 0) {
2166 goto end;
2167 }
2168 health_code_update();
2169
2170 memset(&reply, 0, sizeof(reply));
2171
2172 vstream = viewer_stream_get_by_id(be64toh(request.stream_id));
2173 if (!vstream) {
2174 /*
2175 * The metadata stream can be closed by a CLOSE command
2176 * just before we attach. It can also be closed by
2177 * per-pid tracing during tracing. Therefore, it is
2178 * possible that we cannot find this viewer stream.
2179 * Reply back to the client with an error if we cannot
2180 * find it.
2181 */
2182 DBG("Client requested metadata of unknown stream id %" PRIu64,
2183 (uint64_t) be64toh(request.stream_id));
2184 reply.status = htobe32(LTTNG_VIEWER_METADATA_ERR);
2185 goto send_reply;
2186 }
2187 pthread_mutex_lock(&vstream->stream->lock);
2188 if (!vstream->stream->is_metadata) {
2189 ERR("Invalid metadata stream");
2190 goto error;
2191 }
2192
2193 if (vstream->metadata_sent >= vstream->stream->metadata_received) {
2194 /*
2195 * The live viewers expect to receive a NO_NEW_METADATA
2196 * status before a stream disappears, otherwise they abort the
2197 * entire live connection when receiving an error status.
2198 *
2199 * Clear feature resets the metadata_sent to 0 until the
2200 * same metadata is received again.
2201 */
2202 reply.status = htobe32(LTTNG_VIEWER_NO_NEW_METADATA);
2203 /*
2204 * The live viewer considers a closed 0 byte metadata stream as
2205 * an error.
2206 */
2207 if (vstream->metadata_sent > 0) {
2208 if (vstream->stream->closed && vstream->stream->no_new_metadata_notified) {
2209 /*
2210 * Release ownership for the viewer metadata
2211 * stream. Note that this reference is the
2212 * viewer's reference. The vstream still exists
2213 * until the end of the function as
2214 * viewer_stream_get_by_id() took a reference.
2215 */
2216 viewer_stream_put(vstream);
2217 }
2218
2219 vstream->stream->no_new_metadata_notified = true;
2220 }
2221 goto send_reply;
2222 }
2223
2224 if (vstream->stream->trace_chunk &&
2225 !lttng_trace_chunk_ids_equal(
2226 conn->viewer_session->current_trace_chunk,
2227 vstream->stream->trace_chunk)) {
2228 /* A rotation has occurred on the relay stream. */
2229 DBG("Metadata relay stream and viewer chunk ids differ");
2230
2231 ret = viewer_session_set_trace_chunk_copy(
2232 conn->viewer_session,
2233 vstream->stream->trace_chunk);
2234 if (ret) {
2235 reply.status = htobe32(LTTNG_VIEWER_METADATA_ERR);
2236 goto send_reply;
2237 }
2238 }
2239
2240 if (conn->viewer_session->current_trace_chunk &&
2241 !lttng_trace_chunk_ids_equal(conn->viewer_session->current_trace_chunk,
2242 vstream->stream_file.trace_chunk)) {
2243 bool acquired_reference;
2244
2245 DBG("Viewer session and viewer stream chunk differ: "
2246 "vsession chunk %p vstream chunk %p",
2247 conn->viewer_session->current_trace_chunk,
2248 vstream->stream_file.trace_chunk);
2249 lttng_trace_chunk_put(vstream->stream_file.trace_chunk);
2250 acquired_reference = lttng_trace_chunk_get(conn->viewer_session->current_trace_chunk);
2251 LTTNG_ASSERT(acquired_reference);
2252 vstream->stream_file.trace_chunk =
2253 conn->viewer_session->current_trace_chunk;
2254 viewer_stream_close_files(vstream);
2255 }
2256
2257 len = vstream->stream->metadata_received - vstream->metadata_sent;
2258
2259 if (!vstream->stream_file.trace_chunk) {
2260 reply.status = htobe32(LTTNG_VIEWER_NO_NEW_METADATA);
2261 len = 0;
2262 goto send_reply;
2263 } else if (vstream->stream_file.trace_chunk &&
2264 !vstream->stream_file.handle && len > 0) {
2265 /*
2266 * Either this is the first time the metadata file is read, or a
2267 * rotation of the corresponding relay stream has occurred.
2268 */
2269 struct fs_handle *fs_handle;
2270 char file_path[LTTNG_PATH_MAX];
2271 enum lttng_trace_chunk_status status;
2272 struct relay_stream *rstream = vstream->stream;
2273
2274 ret = utils_stream_file_path(rstream->path_name,
2275 rstream->channel_name, rstream->tracefile_size,
2276 vstream->current_tracefile_id, NULL, file_path,
2277 sizeof(file_path));
2278 if (ret < 0) {
2279 goto error;
2280 }
2281
2282 /*
2283 * It is possible the the metadata file we are trying to open is
2284 * missing if the stream has been closed (application exits with
2285 * per-pid buffers) and a clear command has been performed.
2286 */
2287 status = lttng_trace_chunk_open_fs_handle(
2288 vstream->stream_file.trace_chunk,
2289 file_path, O_RDONLY, 0, &fs_handle, true);
2290 if (status != LTTNG_TRACE_CHUNK_STATUS_OK) {
2291 if (status == LTTNG_TRACE_CHUNK_STATUS_NO_FILE) {
2292 reply.status = htobe32(LTTNG_VIEWER_NO_NEW_METADATA);
2293 len = 0;
2294 if (vstream->stream->closed) {
2295 viewer_stream_put(vstream);
2296 }
2297 goto send_reply;
2298 }
2299 PERROR("Failed to open metadata file for viewer stream");
2300 goto error;
2301 }
2302 vstream->stream_file.handle = fs_handle;
2303
2304 if (vstream->metadata_sent != 0) {
2305 /*
2306 * The client does not expect to receive any metadata
2307 * it has received and metadata files in successive
2308 * chunks must be a strict superset of one another.
2309 *
2310 * Skip the first `metadata_sent` bytes to ensure
2311 * they are not sent a second time to the client.
2312 *
2313 * Baring a block layer error or an internal error,
2314 * this seek should not fail as
2315 * `vstream->stream->metadata_received` is reset when
2316 * a relay stream is rotated. If this is reached, it is
2317 * safe to assume that
2318 * `metadata_received` > `metadata_sent`.
2319 */
2320 const off_t seek_ret = fs_handle_seek(fs_handle,
2321 vstream->metadata_sent, SEEK_SET);
2322
2323 if (seek_ret < 0) {
2324 PERROR("Failed to seek metadata viewer stream file to `sent` position: pos = %" PRId64,
2325 vstream->metadata_sent);
2326 reply.status = htobe32(LTTNG_VIEWER_METADATA_ERR);
2327 goto send_reply;
2328 }
2329 }
2330 }
2331
2332 reply.len = htobe64(len);
2333 data = zmalloc<char>(len);
2334 if (!data) {
2335 PERROR("viewer metadata zmalloc");
2336 goto error;
2337 }
2338
2339 fd = fs_handle_get_fd(vstream->stream_file.handle);
2340 if (fd < 0) {
2341 ERR("Failed to restore viewer stream file system handle");
2342 goto error;
2343 }
2344 read_len = lttng_read(fd, data, len);
2345 fs_handle_put_fd(vstream->stream_file.handle);
2346 fd = -1;
2347 if (read_len < len) {
2348 if (read_len < 0) {
2349 PERROR("Failed to read metadata file");
2350 goto error;
2351 } else {
2352 /*
2353 * A clear has been performed which prevents the relay
2354 * from sending `len` bytes of metadata.
2355 *
2356 * It is important not to send any metadata if we
2357 * couldn't read all the available metadata in one shot:
2358 * sending partial metadata can cause the client to
2359 * attempt to parse an incomplete (incoherent) metadata
2360 * stream, which would result in an error.
2361 */
2362 const off_t seek_ret = fs_handle_seek(
2363 vstream->stream_file.handle, -read_len,
2364 SEEK_CUR);
2365
2366 DBG("Failed to read metadata: requested = %" PRIu64 ", got = %zd",
2367 len, read_len);
2368 read_len = 0;
2369 len = 0;
2370 if (seek_ret < 0) {
2371 PERROR("Failed to restore metadata file position after partial read");
2372 ret = -1;
2373 goto error;
2374 }
2375 }
2376 }
2377 vstream->metadata_sent += read_len;
2378 reply.status = htobe32(LTTNG_VIEWER_METADATA_OK);
2379
2380 goto send_reply;
2381
2382 error:
2383 reply.status = htobe32(LTTNG_VIEWER_METADATA_ERR);
2384
2385 send_reply:
2386 health_code_update();
2387 if (vstream) {
2388 pthread_mutex_unlock(&vstream->stream->lock);
2389 }
2390 ret = send_response(conn->sock, &reply, sizeof(reply));
2391 if (ret < 0) {
2392 goto end_free;
2393 }
2394 health_code_update();
2395
2396 if (len > 0) {
2397 ret = send_response(conn->sock, data, len);
2398 if (ret < 0) {
2399 goto end_free;
2400 }
2401 }
2402
2403 DBG("Sent %" PRIu64 " bytes of metadata for stream %" PRIu64, len,
2404 (uint64_t) be64toh(request.stream_id));
2405
2406 DBG("Metadata sent");
2407
2408 end_free:
2409 free(data);
2410 end:
2411 if (vstream) {
2412 viewer_stream_put(vstream);
2413 }
2414 return ret;
2415 }
2416
2417 /*
2418 * Create a viewer session.
2419 *
2420 * Return 0 on success or else a negative value.
2421 */
2422 static
2423 int viewer_create_session(struct relay_connection *conn)
2424 {
2425 int ret;
2426 struct lttng_viewer_create_session_response resp;
2427
2428 memset(&resp, 0, sizeof(resp));
2429 resp.status = htobe32(LTTNG_VIEWER_CREATE_SESSION_OK);
2430 conn->viewer_session = viewer_session_create();
2431 if (!conn->viewer_session) {
2432 ERR("Allocation viewer session");
2433 resp.status = htobe32(LTTNG_VIEWER_CREATE_SESSION_ERR);
2434 goto send_reply;
2435 }
2436
2437 send_reply:
2438 health_code_update();
2439 ret = send_response(conn->sock, &resp, sizeof(resp));
2440 if (ret < 0) {
2441 goto end;
2442 }
2443 health_code_update();
2444 ret = 0;
2445
2446 end:
2447 return ret;
2448 }
2449
2450 /*
2451 * Detach a viewer session.
2452 *
2453 * Return 0 on success or else a negative value.
2454 */
2455 static
2456 int viewer_detach_session(struct relay_connection *conn)
2457 {
2458 int ret;
2459 struct lttng_viewer_detach_session_response response;
2460 struct lttng_viewer_detach_session_request request;
2461 struct relay_session *session = NULL;
2462 uint64_t viewer_session_to_close;
2463
2464 LTTNG_ASSERT(conn);
2465
2466 health_code_update();
2467
2468 /* Receive the request from the connected client. */
2469 ret = recv_request(conn->sock, &request, sizeof(request));
2470 if (ret < 0) {
2471 goto end;
2472 }
2473 viewer_session_to_close = be64toh(request.session_id);
2474
2475 if (!conn->viewer_session) {
2476 DBG("Client trying to detach before creating a live viewer session");
2477 response.status = htobe32(LTTNG_VIEWER_DETACH_SESSION_ERR);
2478 goto send_reply;
2479 }
2480
2481 health_code_update();
2482
2483 memset(&response, 0, sizeof(response));
2484 DBG("Detaching from session ID %" PRIu64, viewer_session_to_close);
2485
2486 session = session_get_by_id(be64toh(request.session_id));
2487 if (!session) {
2488 DBG("Relay session %" PRIu64 " not found",
2489 (uint64_t) be64toh(request.session_id));
2490 response.status = htobe32(LTTNG_VIEWER_DETACH_SESSION_UNK);
2491 goto send_reply;
2492 }
2493
2494 ret = viewer_session_is_attached(conn->viewer_session, session);
2495 if (ret != 1) {
2496 DBG("Not attached to this session");
2497 response.status = htobe32(LTTNG_VIEWER_DETACH_SESSION_ERR);
2498 goto send_reply_put;
2499 }
2500
2501 viewer_session_close_one_session(conn->viewer_session, session);
2502 response.status = htobe32(LTTNG_VIEWER_DETACH_SESSION_OK);
2503 DBG("Session %" PRIu64 " detached.", viewer_session_to_close);
2504
2505 send_reply_put:
2506 session_put(session);
2507
2508 send_reply:
2509 health_code_update();
2510 ret = send_response(conn->sock, &response, sizeof(response));
2511 if (ret < 0) {
2512 goto end;
2513 }
2514 health_code_update();
2515 ret = 0;
2516
2517 end:
2518 return ret;
2519 }
2520
2521 /*
2522 * live_relay_unknown_command: send -1 if received unknown command
2523 */
2524 static
2525 void live_relay_unknown_command(struct relay_connection *conn)
2526 {
2527 struct lttcomm_relayd_generic_reply reply;
2528
2529 memset(&reply, 0, sizeof(reply));
2530 reply.ret_code = htobe32(LTTNG_ERR_UNK);
2531 (void) send_response(conn->sock, &reply, sizeof(reply));
2532 }
2533
2534 /*
2535 * Process the commands received on the control socket
2536 */
2537 static
2538 int process_control(struct lttng_viewer_cmd *recv_hdr,
2539 struct relay_connection *conn)
2540 {
2541 int ret = 0;
2542 lttng_viewer_command cmd =
2543 (lttng_viewer_command) be32toh(recv_hdr->cmd);
2544
2545 /*
2546 * Make sure we've done the version check before any command other then
2547 * a new client connection.
2548 */
2549 if (cmd != LTTNG_VIEWER_CONNECT && !conn->version_check_done) {
2550 ERR("Viewer on connection %d requested %s command before version check",
2551 conn->sock->fd, lttng_viewer_command_str(cmd));
2552 ret = -1;
2553 goto end;
2554 }
2555
2556 DBG("Processing %s viewer command from connection %d",
2557 lttng_viewer_command_str(cmd), conn->sock->fd);
2558
2559 switch (cmd) {
2560 case LTTNG_VIEWER_CONNECT:
2561 ret = viewer_connect(conn);
2562 break;
2563 case LTTNG_VIEWER_LIST_SESSIONS:
2564 ret = viewer_list_sessions(conn);
2565 break;
2566 case LTTNG_VIEWER_ATTACH_SESSION:
2567 ret = viewer_attach_session(conn);
2568 break;
2569 case LTTNG_VIEWER_GET_NEXT_INDEX:
2570 ret = viewer_get_next_index(conn);
2571 break;
2572 case LTTNG_VIEWER_GET_PACKET:
2573 ret = viewer_get_packet(conn);
2574 break;
2575 case LTTNG_VIEWER_GET_METADATA:
2576 ret = viewer_get_metadata(conn);
2577 break;
2578 case LTTNG_VIEWER_GET_NEW_STREAMS:
2579 ret = viewer_get_new_streams(conn);
2580 break;
2581 case LTTNG_VIEWER_CREATE_SESSION:
2582 ret = viewer_create_session(conn);
2583 break;
2584 case LTTNG_VIEWER_DETACH_SESSION:
2585 ret = viewer_detach_session(conn);
2586 break;
2587 default:
2588 ERR("Received unknown viewer command (%u)",
2589 be32toh(recv_hdr->cmd));
2590 live_relay_unknown_command(conn);
2591 ret = -1;
2592 goto end;
2593 }
2594
2595 end:
2596 return ret;
2597 }
2598
2599 static
2600 void cleanup_connection_pollfd(struct lttng_poll_event *events, int pollfd)
2601 {
2602 int ret;
2603
2604 (void) lttng_poll_del(events, pollfd);
2605
2606 ret = fd_tracker_close_unsuspendable_fd(the_fd_tracker, &pollfd, 1,
2607 fd_tracker_util_close_fd, NULL);
2608 if (ret < 0) {
2609 ERR("Closing pollfd %d", pollfd);
2610 }
2611 }
2612
2613 /*
2614 * This thread does the actual work
2615 */
2616 static
2617 void *thread_worker(void *data __attribute__((unused)))
2618 {
2619 int ret, err = -1;
2620 uint32_t nb_fd;
2621 struct lttng_poll_event events;
2622 struct lttng_ht *viewer_connections_ht;
2623 struct lttng_ht_iter iter;
2624 struct lttng_viewer_cmd recv_hdr;
2625 struct relay_connection *destroy_conn;
2626
2627 DBG("[thread] Live viewer relay worker started");
2628
2629 rcu_register_thread();
2630
2631 health_register(health_relayd, HEALTH_RELAYD_TYPE_LIVE_WORKER);
2632
2633 if (testpoint(relayd_thread_live_worker)) {
2634 goto error_testpoint;
2635 }
2636
2637 /* table of connections indexed on socket */
2638 viewer_connections_ht = lttng_ht_new(0, LTTNG_HT_TYPE_ULONG);
2639 if (!viewer_connections_ht) {
2640 goto viewer_connections_ht_error;
2641 }
2642
2643 ret = create_named_thread_poll_set(&events, 2,
2644 "Live viewer worker thread epoll");
2645 if (ret < 0) {
2646 goto error_poll_create;
2647 }
2648
2649 ret = lttng_poll_add(&events, live_conn_pipe[0], LPOLLIN | LPOLLRDHUP);
2650 if (ret < 0) {
2651 goto error;
2652 }
2653
2654 restart:
2655 while (1) {
2656 int i;
2657
2658 health_code_update();
2659
2660 /* Infinite blocking call, waiting for transmission */
2661 DBG3("Relayd live viewer worker thread polling...");
2662 health_poll_entry();
2663 ret = lttng_poll_wait(&events, -1);
2664 health_poll_exit();
2665 if (ret < 0) {
2666 /*
2667 * Restart interrupted system call.
2668 */
2669 if (errno == EINTR) {
2670 goto restart;
2671 }
2672 goto error;
2673 }
2674
2675 nb_fd = ret;
2676
2677 /*
2678 * Process control. The control connection is prioritised so we don't
2679 * starve it with high throughput tracing data on the data
2680 * connection.
2681 */
2682 for (i = 0; i < nb_fd; i++) {
2683 /* Fetch once the poll data */
2684 const auto revents = LTTNG_POLL_GETEV(&events, i);
2685 const auto pollfd = LTTNG_POLL_GETFD(&events, i);
2686
2687 health_code_update();
2688
2689 /* Activity on thread quit pipe, exiting. */
2690 if (relayd_is_thread_quit_pipe(pollfd)) {
2691 DBG("Activity on thread quit pipe");
2692 err = 0;
2693 goto exit;
2694 }
2695
2696 /* Inspect the relay conn pipe for new connection. */
2697 if (pollfd == live_conn_pipe[0]) {
2698 if (revents & LPOLLIN) {
2699 struct relay_connection *conn;
2700
2701 ret = lttng_read(live_conn_pipe[0],
2702 &conn, sizeof(conn));
2703 if (ret < 0) {
2704 goto error;
2705 }
2706 ret = lttng_poll_add(&events,
2707 conn->sock->fd,
2708 LPOLLIN | LPOLLRDHUP);
2709 if (ret) {
2710 ERR("Failed to add new live connection file descriptor to poll set");
2711 goto error;
2712 }
2713 connection_ht_add(viewer_connections_ht, conn);
2714 DBG("Connection socket %d added to poll", conn->sock->fd);
2715 } else if (revents & (LPOLLERR | LPOLLHUP | LPOLLRDHUP)) {
2716 ERR("Relay live pipe error");
2717 goto error;
2718 } else {
2719 ERR("Unexpected poll events %u for sock %d", revents, pollfd);
2720 goto error;
2721 }
2722 } else {
2723 /* Connection activity. */
2724 struct relay_connection *conn;
2725
2726 conn = connection_get_by_sock(viewer_connections_ht, pollfd);
2727 if (!conn) {
2728 continue;
2729 }
2730
2731 if (revents & LPOLLIN) {
2732 ret = conn->sock->ops->recvmsg(conn->sock, &recv_hdr,
2733 sizeof(recv_hdr), 0);
2734 if (ret <= 0) {
2735 /* Connection closed. */
2736 cleanup_connection_pollfd(&events, pollfd);
2737 /* Put "create" ownership reference. */
2738 connection_put(conn);
2739 DBG("Viewer control conn closed with %d", pollfd);
2740 } else {
2741 ret = process_control(&recv_hdr, conn);
2742 if (ret < 0) {
2743 /* Clear the session on error. */
2744 cleanup_connection_pollfd(&events, pollfd);
2745 /* Put "create" ownership reference. */
2746 connection_put(conn);
2747 DBG("Viewer connection closed with %d", pollfd);
2748 }
2749 }
2750 } else if (revents & (LPOLLERR | LPOLLHUP | LPOLLRDHUP)) {
2751 cleanup_connection_pollfd(&events, pollfd);
2752 /* Put "create" ownership reference. */
2753 connection_put(conn);
2754 } else {
2755 ERR("Unexpected poll events %u for sock %d", revents, pollfd);
2756 connection_put(conn);
2757 goto error;
2758 }
2759 /* Put local "get_by_sock" reference. */
2760 connection_put(conn);
2761 }
2762 }
2763 }
2764
2765 exit:
2766 error:
2767 (void) fd_tracker_util_poll_clean(the_fd_tracker, &events);
2768
2769 /* Cleanup remaining connection object. */
2770 rcu_read_lock();
2771 cds_lfht_for_each_entry(viewer_connections_ht->ht, &iter.iter,
2772 destroy_conn,
2773 sock_n.node) {
2774 health_code_update();
2775 connection_put(destroy_conn);
2776 }
2777 rcu_read_unlock();
2778 error_poll_create:
2779 lttng_ht_destroy(viewer_connections_ht);
2780 viewer_connections_ht_error:
2781 /* Close relay conn pipes */
2782 (void) fd_tracker_util_pipe_close(the_fd_tracker, live_conn_pipe);
2783 if (err) {
2784 DBG("Viewer worker thread exited with error");
2785 }
2786 DBG("Viewer worker thread cleanup complete");
2787 error_testpoint:
2788 if (err) {
2789 health_error();
2790 ERR("Health error occurred in %s", __func__);
2791 }
2792 health_unregister(health_relayd);
2793 if (lttng_relay_stop_threads()) {
2794 ERR("Error stopping threads");
2795 }
2796 rcu_unregister_thread();
2797 return NULL;
2798 }
2799
2800 /*
2801 * Create the relay command pipe to wake thread_manage_apps.
2802 * Closed in cleanup().
2803 */
2804 static int create_conn_pipe(void)
2805 {
2806 return fd_tracker_util_pipe_open_cloexec(the_fd_tracker,
2807 "Live connection pipe", live_conn_pipe);
2808 }
2809
2810 int relayd_live_join(void)
2811 {
2812 int ret, retval = 0;
2813 void *status;
2814
2815 ret = pthread_join(live_listener_thread, &status);
2816 if (ret) {
2817 errno = ret;
2818 PERROR("pthread_join live listener");
2819 retval = -1;
2820 }
2821
2822 ret = pthread_join(live_worker_thread, &status);
2823 if (ret) {
2824 errno = ret;
2825 PERROR("pthread_join live worker");
2826 retval = -1;
2827 }
2828
2829 ret = pthread_join(live_dispatcher_thread, &status);
2830 if (ret) {
2831 errno = ret;
2832 PERROR("pthread_join live dispatcher");
2833 retval = -1;
2834 }
2835
2836 cleanup_relayd_live();
2837
2838 return retval;
2839 }
2840
2841 /*
2842 * main
2843 */
2844 int relayd_live_create(struct lttng_uri *uri)
2845 {
2846 int ret = 0, retval = 0;
2847 void *status;
2848 int is_root;
2849
2850 if (!uri) {
2851 retval = -1;
2852 goto exit_init_data;
2853 }
2854 live_uri = uri;
2855
2856 /* Check if daemon is UID = 0 */
2857 is_root = !getuid();
2858
2859 if (!is_root) {
2860 if (live_uri->port < 1024) {
2861 ERR("Need to be root to use ports < 1024");
2862 retval = -1;
2863 goto exit_init_data;
2864 }
2865 }
2866
2867 /* Setup the thread apps communication pipe. */
2868 if (create_conn_pipe()) {
2869 retval = -1;
2870 goto exit_init_data;
2871 }
2872
2873 /* Init relay command queue. */
2874 cds_wfcq_init(&viewer_conn_queue.head, &viewer_conn_queue.tail);
2875
2876 /* Set up max poll set size */
2877 if (lttng_poll_set_max_size()) {
2878 retval = -1;
2879 goto exit_init_data;
2880 }
2881
2882 /* Setup the dispatcher thread */
2883 ret = pthread_create(&live_dispatcher_thread, default_pthread_attr(),
2884 thread_dispatcher, (void *) NULL);
2885 if (ret) {
2886 errno = ret;
2887 PERROR("pthread_create viewer dispatcher");
2888 retval = -1;
2889 goto exit_dispatcher_thread;
2890 }
2891
2892 /* Setup the worker thread */
2893 ret = pthread_create(&live_worker_thread, default_pthread_attr(),
2894 thread_worker, NULL);
2895 if (ret) {
2896 errno = ret;
2897 PERROR("pthread_create viewer worker");
2898 retval = -1;
2899 goto exit_worker_thread;
2900 }
2901
2902 /* Setup the listener thread */
2903 ret = pthread_create(&live_listener_thread, default_pthread_attr(),
2904 thread_listener, (void *) NULL);
2905 if (ret) {
2906 errno = ret;
2907 PERROR("pthread_create viewer listener");
2908 retval = -1;
2909 goto exit_listener_thread;
2910 }
2911
2912 /*
2913 * All OK, started all threads.
2914 */
2915 return retval;
2916
2917 /*
2918 * Join on the live_listener_thread should anything be added after
2919 * the live_listener thread's creation.
2920 */
2921
2922 exit_listener_thread:
2923
2924 ret = pthread_join(live_worker_thread, &status);
2925 if (ret) {
2926 errno = ret;
2927 PERROR("pthread_join live worker");
2928 retval = -1;
2929 }
2930 exit_worker_thread:
2931
2932 ret = pthread_join(live_dispatcher_thread, &status);
2933 if (ret) {
2934 errno = ret;
2935 PERROR("pthread_join live dispatcher");
2936 retval = -1;
2937 }
2938 exit_dispatcher_thread:
2939
2940 exit_init_data:
2941 cleanup_relayd_live();
2942
2943 return retval;
2944 }
This page took 0.132132 seconds and 4 git commands to generate.