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