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