Fix: check for new streams in all attached sessions
[lttng-tools.git] / src / bin / lttng-relayd / live.c
1 /*
2 * Copyright (C) 2013 - Julien Desfossez <jdesfossez@efficios.com>
3 * David Goulet <dgoulet@efficios.com>
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License, version 2 only,
7 * as published by the Free Software Foundation.
8 *
9 * This program is distributed in the hope that it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
12 * more details.
13 *
14 * You should have received a copy of the GNU General Public License along
15 * with this program; if not, write to the Free Software Foundation, Inc.,
16 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
17 */
18
19 #define _GNU_SOURCE
20 #include <getopt.h>
21 #include <grp.h>
22 #include <limits.h>
23 #include <pthread.h>
24 #include <signal.h>
25 #include <stdio.h>
26 #include <stdlib.h>
27 #include <string.h>
28 #include <sys/mman.h>
29 #include <sys/mount.h>
30 #include <sys/resource.h>
31 #include <sys/socket.h>
32 #include <sys/stat.h>
33 #include <sys/types.h>
34 #include <sys/wait.h>
35 #include <inttypes.h>
36 #include <urcu/futex.h>
37 #include <urcu/uatomic.h>
38 #include <unistd.h>
39 #include <fcntl.h>
40 #include <config.h>
41
42 #include <lttng/lttng.h>
43 #include <common/common.h>
44 #include <common/compat/poll.h>
45 #include <common/compat/socket.h>
46 #include <common/defaults.h>
47 #include <common/futex.h>
48 #include <common/index/index.h>
49 #include <common/sessiond-comm/sessiond-comm.h>
50 #include <common/sessiond-comm/inet.h>
51 #include <common/sessiond-comm/relayd.h>
52 #include <common/uri.h>
53 #include <common/utils.h>
54
55 #include "cmd.h"
56 #include "live.h"
57 #include "lttng-relayd.h"
58 #include "utils.h"
59 #include "health-relayd.h"
60 #include "testpoint.h"
61 #include "viewer-stream.h"
62 #include "stream.h"
63 #include "session.h"
64 #include "ctf-trace.h"
65 #include "connection.h"
66
67 static struct lttng_uri *live_uri;
68
69 /*
70 * This pipe is used to inform the worker thread that a command is queued and
71 * ready to be processed.
72 */
73 static int live_conn_pipe[2] = { -1, -1 };
74
75 /* Shared between threads */
76 static int live_dispatch_thread_exit;
77
78 static pthread_t live_listener_thread;
79 static pthread_t live_dispatcher_thread;
80 static pthread_t live_worker_thread;
81
82 /*
83 * Relay command queue.
84 *
85 * The live_thread_listener and live_thread_dispatcher communicate with this
86 * queue.
87 */
88 static struct relay_conn_queue viewer_conn_queue;
89
90 static uint64_t last_relay_viewer_session_id;
91
92 /*
93 * Cleanup the daemon
94 */
95 static
96 void cleanup(void)
97 {
98 DBG("Cleaning up");
99
100 free(live_uri);
101 }
102
103 /*
104 * Receive a request buffer using a given socket, destination allocated buffer
105 * of length size.
106 *
107 * Return the size of the received message or else a negative value on error
108 * with errno being set by recvmsg() syscall.
109 */
110 static
111 ssize_t recv_request(struct lttcomm_sock *sock, void *buf, size_t size)
112 {
113 ssize_t ret;
114
115 assert(sock);
116 assert(buf);
117
118 ret = sock->ops->recvmsg(sock, buf, size, 0);
119 if (ret < 0 || ret != size) {
120 if (ret == 0) {
121 /* Orderly shutdown. Not necessary to print an error. */
122 DBG("Socket %d did an orderly shutdown", sock->fd);
123 } else {
124 ERR("Relay failed to receive request.");
125 }
126 ret = -1;
127 }
128
129 return ret;
130 }
131
132 /*
133 * Send a response buffer using a given socket, source allocated buffer of
134 * length size.
135 *
136 * Return the size of the sent message or else a negative value on error with
137 * errno being set by sendmsg() syscall.
138 */
139 static
140 ssize_t send_response(struct lttcomm_sock *sock, void *buf, size_t size)
141 {
142 ssize_t ret;
143
144 assert(sock);
145 assert(buf);
146
147 ret = sock->ops->sendmsg(sock, buf, size, 0);
148 if (ret < 0) {
149 ERR("Relayd failed to send response.");
150 }
151
152 return ret;
153 }
154
155 /*
156 * Atomically check if new streams got added in one of the sessions attached
157 * and reset the flag to 0.
158 *
159 * Returns 1 if new streams got added, 0 if nothing changed, a negative value
160 * on error.
161 */
162 static
163 int check_new_streams(struct relay_connection *conn)
164 {
165 struct relay_session *session;
166 unsigned long current_val;
167 int ret = 0;
168
169 if (!conn->viewer_session) {
170 goto end;
171 }
172 cds_list_for_each_entry(session,
173 &conn->viewer_session->sessions_head,
174 viewer_session_list) {
175 current_val = uatomic_cmpxchg(&session->new_streams, 1, 0);
176 ret = current_val;
177 if (ret == 1) {
178 goto end;
179 }
180 }
181
182 end:
183 return ret;
184 }
185
186 /*
187 * Send viewer streams to the given socket. The ignore_sent_flag indicates if
188 * this function should ignore the sent flag or not.
189 *
190 * Return 0 on success or else a negative value.
191 */
192 static
193 ssize_t send_viewer_streams(struct lttcomm_sock *sock,
194 struct relay_session *session, unsigned int ignore_sent_flag)
195 {
196 ssize_t ret;
197 struct lttng_viewer_stream send_stream;
198 struct lttng_ht_iter iter;
199 struct relay_viewer_stream *vstream;
200
201 assert(session);
202
203 rcu_read_lock();
204
205 cds_lfht_for_each_entry(viewer_streams_ht->ht, &iter.iter, vstream,
206 stream_n.node) {
207 struct ctf_trace *ctf_trace;
208
209 health_code_update();
210
211 /* Ignore if not the same session. */
212 if (vstream->session_id != session->id ||
213 (!ignore_sent_flag && vstream->sent_flag)) {
214 continue;
215 }
216
217 ctf_trace = ctf_trace_find_by_path(session->ctf_traces_ht,
218 vstream->path_name);
219 assert(ctf_trace);
220
221 send_stream.id = htobe64(vstream->stream_handle);
222 send_stream.ctf_trace_id = htobe64(ctf_trace->id);
223 send_stream.metadata_flag = htobe32(vstream->metadata_flag);
224 strncpy(send_stream.path_name, vstream->path_name,
225 sizeof(send_stream.path_name));
226 strncpy(send_stream.channel_name, vstream->channel_name,
227 sizeof(send_stream.channel_name));
228
229 DBG("Sending stream %" PRIu64 " to viewer", vstream->stream_handle);
230 ret = send_response(sock, &send_stream, sizeof(send_stream));
231 if (ret < 0) {
232 goto end_unlock;
233 }
234 vstream->sent_flag = 1;
235 }
236
237 ret = 0;
238
239 end_unlock:
240 rcu_read_unlock();
241 return ret;
242 }
243
244 /*
245 * Create every viewer stream possible for the given session with the seek
246 * type. Three counters *can* be return which are in order the total amount of
247 * viewer stream of the session, the number of unsent stream and the number of
248 * stream created. Those counters can be NULL and thus will be ignored.
249 *
250 * Return 0 on success or else a negative value.
251 */
252 static
253 int make_viewer_streams(struct relay_session *session,
254 enum lttng_viewer_seek seek_t, uint32_t *nb_total, uint32_t *nb_unsent,
255 uint32_t *nb_created)
256 {
257 int ret;
258 struct lttng_ht_iter iter;
259 struct ctf_trace *ctf_trace;
260
261 assert(session);
262
263 /*
264 * This is to make sure we create viewer streams for a full received
265 * channel. For instance, if we have 8 streams for a channel that are
266 * concurrently being flagged ready, we can end up creating just a subset
267 * of the 8 streams (the ones that are flagged). This lock avoids this
268 * limbo state.
269 */
270 pthread_mutex_lock(&session->viewer_ready_lock);
271
272 /*
273 * Create viewer streams for relay streams that are ready to be used for a
274 * the given session id only.
275 */
276 rcu_read_lock();
277 cds_lfht_for_each_entry(session->ctf_traces_ht->ht, &iter.iter, ctf_trace,
278 node.node) {
279 struct relay_stream *stream;
280
281 health_code_update();
282
283 if (ctf_trace->invalid_flag) {
284 continue;
285 }
286
287 cds_list_for_each_entry(stream, &ctf_trace->stream_list, trace_list) {
288 struct relay_viewer_stream *vstream;
289
290 if (!stream->viewer_ready) {
291 continue;
292 }
293
294 vstream = viewer_stream_find_by_id(stream->stream_handle);
295 if (!vstream) {
296 vstream = viewer_stream_create(stream, seek_t, ctf_trace);
297 if (!vstream) {
298 ret = -1;
299 goto error_unlock;
300 }
301 /* Acquire reference to ctf_trace. */
302 ctf_trace_get_ref(ctf_trace);
303
304 if (nb_created) {
305 /* Update number of created stream counter. */
306 (*nb_created)++;
307 }
308 } else if (!vstream->sent_flag && nb_unsent) {
309 /* Update number of unsent stream counter. */
310 (*nb_unsent)++;
311 }
312 /* Update number of total stream counter. */
313 if (nb_total) {
314 (*nb_total)++;
315 }
316 }
317 }
318
319 ret = 0;
320
321 error_unlock:
322 rcu_read_unlock();
323 pthread_mutex_unlock(&session->viewer_ready_lock);
324 return ret;
325 }
326
327 /*
328 * Write to writable pipe used to notify a thread.
329 */
330 static
331 int notify_thread_pipe(int wpipe)
332 {
333 ssize_t ret;
334
335 ret = lttng_write(wpipe, "!", 1);
336 if (ret < 1) {
337 PERROR("write poll pipe");
338 }
339
340 return (int) ret;
341 }
342
343 /*
344 * Stop all threads by closing the thread quit pipe.
345 */
346 static
347 void stop_threads(void)
348 {
349 int ret;
350
351 /* Stopping all threads */
352 DBG("Terminating all live threads");
353 ret = notify_thread_pipe(live_conn_pipe[1]);
354 if (ret < 0) {
355 ERR("write error on thread quit pipe");
356 }
357
358 /* Dispatch thread */
359 CMM_STORE_SHARED(live_dispatch_thread_exit, 1);
360 futex_nto1_wake(&viewer_conn_queue.futex);
361 }
362
363 /*
364 * Create a poll set with O_CLOEXEC and add the thread quit pipe to the set.
365 */
366 static
367 int create_thread_poll_set(struct lttng_poll_event *events, int size)
368 {
369 int ret;
370
371 if (events == NULL || size == 0) {
372 ret = -1;
373 goto error;
374 }
375
376 ret = lttng_poll_create(events, size, LTTNG_CLOEXEC);
377 if (ret < 0) {
378 goto error;
379 }
380
381 /* Add quit pipe */
382 ret = lttng_poll_add(events, live_conn_pipe[0], LPOLLIN | LPOLLERR);
383 if (ret < 0) {
384 goto error;
385 }
386
387 return 0;
388
389 error:
390 return ret;
391 }
392
393 /*
394 * Check if the thread quit pipe was triggered.
395 *
396 * Return 1 if it was triggered else 0;
397 */
398 static
399 int check_live_conn_pipe(int fd, uint32_t events)
400 {
401 if (fd == live_conn_pipe[0] && (events & LPOLLIN)) {
402 return 1;
403 }
404
405 return 0;
406 }
407
408 /*
409 * Create and init socket from uri.
410 */
411 static
412 struct lttcomm_sock *init_socket(struct lttng_uri *uri)
413 {
414 int ret;
415 struct lttcomm_sock *sock = NULL;
416
417 sock = lttcomm_alloc_sock_from_uri(uri);
418 if (sock == NULL) {
419 ERR("Allocating socket");
420 goto error;
421 }
422
423 ret = lttcomm_create_sock(sock);
424 if (ret < 0) {
425 goto error;
426 }
427 DBG("Listening on sock %d for live", sock->fd);
428
429 ret = sock->ops->bind(sock);
430 if (ret < 0) {
431 goto error;
432 }
433
434 ret = sock->ops->listen(sock, -1);
435 if (ret < 0) {
436 goto error;
437
438 }
439
440 return sock;
441
442 error:
443 if (sock) {
444 lttcomm_destroy_sock(sock);
445 }
446 return NULL;
447 }
448
449 /*
450 * This thread manages the listening for new connections on the network
451 */
452 static
453 void *thread_listener(void *data)
454 {
455 int i, ret, pollfd, err = -1;
456 uint32_t revents, nb_fd;
457 struct lttng_poll_event events;
458 struct lttcomm_sock *live_control_sock;
459
460 DBG("[thread] Relay live listener started");
461
462 health_register(health_relayd, HEALTH_RELAYD_TYPE_LIVE_LISTENER);
463
464 health_code_update();
465
466 live_control_sock = init_socket(live_uri);
467 if (!live_control_sock) {
468 goto error_sock_control;
469 }
470
471 /* Pass 2 as size here for the thread quit pipe and control sockets. */
472 ret = create_thread_poll_set(&events, 2);
473 if (ret < 0) {
474 goto error_create_poll;
475 }
476
477 /* Add the control socket */
478 ret = lttng_poll_add(&events, live_control_sock->fd, LPOLLIN | LPOLLRDHUP);
479 if (ret < 0) {
480 goto error_poll_add;
481 }
482
483 lttng_relay_notify_ready();
484
485 if (testpoint(relayd_thread_live_listener)) {
486 goto error_testpoint;
487 }
488
489 while (1) {
490 health_code_update();
491
492 DBG("Listener accepting live viewers connections");
493
494 restart:
495 health_poll_entry();
496 ret = lttng_poll_wait(&events, -1);
497 health_poll_exit();
498 if (ret < 0) {
499 /*
500 * Restart interrupted system call.
501 */
502 if (errno == EINTR) {
503 goto restart;
504 }
505 goto error;
506 }
507 nb_fd = ret;
508
509 DBG("Relay new viewer connection received");
510 for (i = 0; i < nb_fd; i++) {
511 health_code_update();
512
513 /* Fetch once the poll data */
514 revents = LTTNG_POLL_GETEV(&events, i);
515 pollfd = LTTNG_POLL_GETFD(&events, i);
516
517 /* Thread quit pipe has been closed. Killing thread. */
518 ret = check_live_conn_pipe(pollfd, revents);
519 if (ret) {
520 err = 0;
521 goto exit;
522 }
523
524 if (revents & (LPOLLERR | LPOLLHUP | LPOLLRDHUP)) {
525 ERR("socket poll error");
526 goto error;
527 } else if (revents & LPOLLIN) {
528 /*
529 * Get allocated in this thread, enqueued to a global queue,
530 * dequeued and freed in the worker thread.
531 */
532 int val = 1;
533 struct relay_connection *new_conn;
534 struct lttcomm_sock *newsock;
535
536 new_conn = connection_create();
537 if (!new_conn) {
538 goto error;
539 }
540
541 newsock = live_control_sock->ops->accept(live_control_sock);
542 if (!newsock) {
543 PERROR("accepting control sock");
544 connection_free(new_conn);
545 goto error;
546 }
547 DBG("Relay viewer connection accepted socket %d", newsock->fd);
548
549 ret = setsockopt(newsock->fd, SOL_SOCKET, SO_REUSEADDR, &val,
550 sizeof(val));
551 if (ret < 0) {
552 PERROR("setsockopt inet");
553 lttcomm_destroy_sock(newsock);
554 connection_free(new_conn);
555 goto error;
556 }
557 new_conn->sock = newsock;
558
559 /* Enqueue request for the dispatcher thread. */
560 cds_wfq_enqueue(&viewer_conn_queue.queue, &new_conn->qnode);
561
562 /*
563 * Wake the dispatch queue futex. Implicit memory barrier with
564 * the exchange in cds_wfq_enqueue.
565 */
566 futex_nto1_wake(&viewer_conn_queue.futex);
567 }
568 }
569 }
570
571 exit:
572 error:
573 error_poll_add:
574 error_testpoint:
575 lttng_poll_clean(&events);
576 error_create_poll:
577 if (live_control_sock->fd >= 0) {
578 ret = live_control_sock->ops->close(live_control_sock);
579 if (ret) {
580 PERROR("close");
581 }
582 }
583 lttcomm_destroy_sock(live_control_sock);
584 error_sock_control:
585 if (err) {
586 health_error();
587 DBG("Live viewer listener thread exited with error");
588 }
589 health_unregister(health_relayd);
590 DBG("Live viewer listener thread cleanup complete");
591 stop_threads();
592 return NULL;
593 }
594
595 /*
596 * This thread manages the dispatching of the requests to worker threads
597 */
598 static
599 void *thread_dispatcher(void *data)
600 {
601 int err = -1;
602 ssize_t ret;
603 struct cds_wfq_node *node;
604 struct relay_connection *conn = NULL;
605
606 DBG("[thread] Live viewer relay dispatcher started");
607
608 health_register(health_relayd, HEALTH_RELAYD_TYPE_LIVE_DISPATCHER);
609
610 if (testpoint(relayd_thread_live_dispatcher)) {
611 goto error_testpoint;
612 }
613
614 health_code_update();
615
616 while (!CMM_LOAD_SHARED(live_dispatch_thread_exit)) {
617 health_code_update();
618
619 /* Atomically prepare the queue futex */
620 futex_nto1_prepare(&viewer_conn_queue.futex);
621
622 do {
623 health_code_update();
624
625 /* Dequeue commands */
626 node = cds_wfq_dequeue_blocking(&viewer_conn_queue.queue);
627 if (node == NULL) {
628 DBG("Woken up but nothing in the live-viewer "
629 "relay command queue");
630 /* Continue thread execution */
631 break;
632 }
633 conn = caa_container_of(node, struct relay_connection, qnode);
634 DBG("Dispatching viewer request waiting on sock %d",
635 conn->sock->fd);
636
637 /*
638 * Inform worker thread of the new request. This call is blocking
639 * so we can be assured that the data will be read at some point in
640 * time or wait to the end of the world :)
641 */
642 ret = lttng_write(live_conn_pipe[1], &conn, sizeof(conn));
643 if (ret < 0) {
644 PERROR("write conn pipe");
645 connection_destroy(conn);
646 goto error;
647 }
648 } while (node != NULL);
649
650 /* Futex wait on queue. Blocking call on futex() */
651 health_poll_entry();
652 futex_nto1_wait(&viewer_conn_queue.futex);
653 health_poll_exit();
654 }
655
656 /* Normal exit, no error */
657 err = 0;
658
659 error:
660 error_testpoint:
661 if (err) {
662 health_error();
663 ERR("Health error occurred in %s", __func__);
664 }
665 health_unregister(health_relayd);
666 DBG("Live viewer dispatch thread dying");
667 stop_threads();
668 return NULL;
669 }
670
671 /*
672 * Establish connection with the viewer and check the versions.
673 *
674 * Return 0 on success or else negative value.
675 */
676 static
677 int viewer_connect(struct relay_connection *conn)
678 {
679 int ret;
680 struct lttng_viewer_connect reply, msg;
681
682 assert(conn);
683
684 conn->version_check_done = 1;
685
686 health_code_update();
687
688 DBG("Viewer is establishing a connection to the relayd.");
689
690 ret = recv_request(conn->sock, &msg, sizeof(msg));
691 if (ret < 0) {
692 goto end;
693 }
694
695 health_code_update();
696
697 reply.major = RELAYD_VERSION_COMM_MAJOR;
698 reply.minor = RELAYD_VERSION_COMM_MINOR;
699
700 /* Major versions must be the same */
701 if (reply.major != be32toh(msg.major)) {
702 DBG("Incompatible major versions ([relayd] %u vs [client] %u)",
703 reply.major, be32toh(msg.major));
704 ret = -1;
705 goto end;
706 }
707
708 conn->major = reply.major;
709 /* We adapt to the lowest compatible version */
710 if (reply.minor <= be32toh(msg.minor)) {
711 conn->minor = reply.minor;
712 } else {
713 conn->minor = be32toh(msg.minor);
714 }
715
716 if (be32toh(msg.type) == LTTNG_VIEWER_CLIENT_COMMAND) {
717 conn->type = RELAY_VIEWER_COMMAND;
718 } else if (be32toh(msg.type) == LTTNG_VIEWER_CLIENT_NOTIFICATION) {
719 conn->type = RELAY_VIEWER_NOTIFICATION;
720 } else {
721 ERR("Unknown connection type : %u", be32toh(msg.type));
722 ret = -1;
723 goto end;
724 }
725
726 reply.major = htobe32(reply.major);
727 reply.minor = htobe32(reply.minor);
728 if (conn->type == RELAY_VIEWER_COMMAND) {
729 reply.viewer_session_id = htobe64(++last_relay_viewer_session_id);
730 }
731
732 health_code_update();
733
734 ret = send_response(conn->sock, &reply, sizeof(reply));
735 if (ret < 0) {
736 goto end;
737 }
738
739 health_code_update();
740
741 DBG("Version check done using protocol %u.%u", conn->major, conn->minor);
742 ret = 0;
743
744 end:
745 return ret;
746 }
747
748 /*
749 * Send the viewer the list of current sessions.
750 *
751 * Return 0 on success or else a negative value.
752 */
753 static
754 int viewer_list_sessions(struct relay_connection *conn)
755 {
756 int ret;
757 struct lttng_viewer_list_sessions session_list;
758 unsigned long count;
759 long approx_before, approx_after;
760 struct lttng_ht_iter iter;
761 struct lttng_viewer_session send_session;
762 struct relay_session *session;
763
764 DBG("List sessions received");
765
766 rcu_read_lock();
767 cds_lfht_count_nodes(conn->sessions_ht->ht, &approx_before, &count,
768 &approx_after);
769 session_list.sessions_count = htobe32(count);
770
771 health_code_update();
772
773 ret = send_response(conn->sock, &session_list, sizeof(session_list));
774 if (ret < 0) {
775 goto end_unlock;
776 }
777
778 health_code_update();
779
780 cds_lfht_for_each_entry(conn->sessions_ht->ht, &iter.iter, session,
781 session_n.node) {
782 health_code_update();
783
784 strncpy(send_session.session_name, session->session_name,
785 sizeof(send_session.session_name));
786 strncpy(send_session.hostname, session->hostname,
787 sizeof(send_session.hostname));
788 send_session.id = htobe64(session->id);
789 send_session.live_timer = htobe32(session->live_timer);
790 send_session.clients = htobe32(session->viewer_refcount);
791 send_session.streams = htobe32(session->stream_count);
792
793 health_code_update();
794
795 ret = send_response(conn->sock, &send_session, sizeof(send_session));
796 if (ret < 0) {
797 goto end_unlock;
798 }
799 }
800 health_code_update();
801
802 rcu_read_unlock();
803 ret = 0;
804 goto end;
805
806 end_unlock:
807 rcu_read_unlock();
808
809 end:
810 return ret;
811 }
812
813 /*
814 * Check if a connection is attached to a session.
815 * Return 1 if attached, 0 if not attached, a negative value on error.
816 */
817 static
818 int session_attached(struct relay_connection *conn, uint64_t session_id)
819 {
820 struct relay_session *session;
821 int found = 0;
822
823 if (!conn->viewer_session) {
824 goto end;
825 }
826 cds_list_for_each_entry(session,
827 &conn->viewer_session->sessions_head,
828 viewer_session_list) {
829 if (session->id == session_id) {
830 found = 1;
831 goto end;
832 }
833 }
834
835 end:
836 return found;
837 }
838
839 /*
840 * Delete all streams for a specific session ID.
841 */
842 static void destroy_viewer_streams_by_session(struct relay_session *session)
843 {
844 struct relay_viewer_stream *stream;
845 struct lttng_ht_iter iter;
846
847 assert(session);
848
849 rcu_read_lock();
850 cds_lfht_for_each_entry(viewer_streams_ht->ht, &iter.iter, stream,
851 stream_n.node) {
852 struct ctf_trace *ctf_trace;
853
854 health_code_update();
855 if (stream->session_id != session->id) {
856 continue;
857 }
858
859 ctf_trace = ctf_trace_find_by_path(session->ctf_traces_ht,
860 stream->path_name);
861 assert(ctf_trace);
862
863 viewer_stream_delete(stream);
864
865 if (stream->metadata_flag) {
866 ctf_trace->metadata_sent = 0;
867 ctf_trace->viewer_metadata_stream = NULL;
868 }
869
870 viewer_stream_destroy(ctf_trace, stream);
871 }
872 rcu_read_unlock();
873 }
874
875 static void try_destroy_streams(struct relay_session *session)
876 {
877 struct ctf_trace *ctf_trace;
878 struct lttng_ht_iter iter;
879
880 assert(session);
881
882 cds_lfht_for_each_entry(session->ctf_traces_ht->ht, &iter.iter, ctf_trace,
883 node.node) {
884 /* Attempt to destroy the ctf trace of that session. */
885 ctf_trace_try_destroy(session, ctf_trace);
886 }
887 }
888
889 /*
890 * Cleanup a session.
891 */
892 static void cleanup_session(struct relay_connection *conn,
893 struct relay_session *session)
894 {
895 /*
896 * Very important that this is done before destroying the session so we
897 * can put back every viewer stream reference from the ctf_trace.
898 */
899 destroy_viewer_streams_by_session(session);
900 try_destroy_streams(session);
901 cds_list_del(&session->viewer_session_list);
902 session_viewer_try_destroy(conn->sessions_ht, session);
903 }
904
905 /*
906 * Send the viewer the list of current sessions.
907 */
908 static
909 int viewer_get_new_streams(struct relay_connection *conn)
910 {
911 int ret, send_streams = 0;
912 uint32_t nb_created = 0, nb_unsent = 0, nb_streams = 0;
913 struct lttng_viewer_new_streams_request request;
914 struct lttng_viewer_new_streams_response response;
915 struct relay_session *session;
916 uint64_t session_id;
917
918 assert(conn);
919
920 DBG("Get new streams received");
921
922 health_code_update();
923
924 /* Receive the request from the connected client. */
925 ret = recv_request(conn->sock, &request, sizeof(request));
926 if (ret < 0) {
927 goto error;
928 }
929 session_id = be64toh(request.session_id);
930
931 health_code_update();
932
933 rcu_read_lock();
934 session = session_find_by_id(conn->sessions_ht, session_id);
935 if (!session) {
936 DBG("Relay session %" PRIu64 " not found", session_id);
937 response.status = htobe32(LTTNG_VIEWER_NEW_STREAMS_ERR);
938 goto send_reply;
939 }
940
941 if (!session_attached(conn, session_id)) {
942 send_streams = 0;
943 response.status = htobe32(LTTNG_VIEWER_NEW_STREAMS_ERR);
944 goto send_reply;
945 }
946
947 send_streams = 1;
948 response.status = htobe32(LTTNG_VIEWER_NEW_STREAMS_OK);
949
950 if (!send_streams) {
951 goto send_reply;
952 }
953
954 ret = make_viewer_streams(session, LTTNG_VIEWER_SEEK_LAST, NULL, &nb_unsent,
955 &nb_created);
956 if (ret < 0) {
957 goto end_unlock;
958 }
959 /* Only send back the newly created streams with the unsent ones. */
960 nb_streams = nb_created + nb_unsent;
961 response.streams_count = htobe32(nb_streams);
962
963 /*
964 * If the session is closed and we have no new streams to send,
965 * it means that the viewer has already received the whole trace
966 * for this session and should now close it.
967 */
968 if (nb_streams == 0 && session->close_flag) {
969 send_streams = 0;
970 response.status = htobe32(LTTNG_VIEWER_NEW_STREAMS_HUP);
971 /*
972 * Remove the session from the attached list of the connection
973 * and try to destroy it.
974 */
975 cds_list_del(&session->viewer_session_list);
976 cleanup_session(conn, session);
977 goto send_reply;
978 }
979
980 send_reply:
981 health_code_update();
982 ret = send_response(conn->sock, &response, sizeof(response));
983 if (ret < 0) {
984 goto end_unlock;
985 }
986 health_code_update();
987
988 /*
989 * Unknown or empty session, just return gracefully, the viewer knows what
990 * is happening.
991 */
992 if (!send_streams || !nb_streams) {
993 ret = 0;
994 goto end_unlock;
995 }
996
997 /*
998 * Send stream and *DON'T* ignore the sent flag so every viewer streams
999 * that were not sent from that point will be sent to the viewer.
1000 */
1001 ret = send_viewer_streams(conn->sock, session, 0);
1002 if (ret < 0) {
1003 goto end_unlock;
1004 }
1005
1006 end_unlock:
1007 rcu_read_unlock();
1008 error:
1009 return ret;
1010 }
1011
1012 /*
1013 * Send the viewer the list of current sessions.
1014 */
1015 static
1016 int viewer_attach_session(struct relay_connection *conn)
1017 {
1018 int send_streams = 0;
1019 ssize_t ret;
1020 uint32_t nb_streams = 0;
1021 enum lttng_viewer_seek seek_type;
1022 struct lttng_viewer_attach_session_request request;
1023 struct lttng_viewer_attach_session_response response;
1024 struct relay_session *session;
1025
1026 assert(conn);
1027
1028 health_code_update();
1029
1030 /* Receive the request from the connected client. */
1031 ret = recv_request(conn->sock, &request, sizeof(request));
1032 if (ret < 0) {
1033 goto error;
1034 }
1035
1036 health_code_update();
1037
1038 if (!conn->viewer_session) {
1039 DBG("Client trying to attach before creating a live viewer session");
1040 response.status = htobe32(LTTNG_VIEWER_ATTACH_NO_SESSION);
1041 goto send_reply;
1042 }
1043
1044 rcu_read_lock();
1045 session = session_find_by_id(conn->sessions_ht,
1046 be64toh(request.session_id));
1047 if (!session) {
1048 DBG("Relay session %" PRIu64 " not found",
1049 be64toh(request.session_id));
1050 response.status = htobe32(LTTNG_VIEWER_ATTACH_UNK);
1051 goto send_reply;
1052 }
1053 session_viewer_attach(session);
1054 DBG("Attach session ID %" PRIu64 " received", be64toh(request.session_id));
1055
1056 if (uatomic_read(&session->viewer_refcount) > 1) {
1057 DBG("Already a viewer attached");
1058 response.status = htobe32(LTTNG_VIEWER_ATTACH_ALREADY);
1059 session_viewer_detach(session);
1060 goto send_reply;
1061 } else if (session->live_timer == 0) {
1062 DBG("Not live session");
1063 response.status = htobe32(LTTNG_VIEWER_ATTACH_NOT_LIVE);
1064 goto send_reply;
1065 } else {
1066 send_streams = 1;
1067 response.status = htobe32(LTTNG_VIEWER_ATTACH_OK);
1068 cds_list_add(&session->viewer_session_list,
1069 &conn->viewer_session->sessions_head);
1070 }
1071
1072 switch (be32toh(request.seek)) {
1073 case LTTNG_VIEWER_SEEK_BEGINNING:
1074 case LTTNG_VIEWER_SEEK_LAST:
1075 seek_type = be32toh(request.seek);
1076 break;
1077 default:
1078 ERR("Wrong seek parameter");
1079 response.status = htobe32(LTTNG_VIEWER_ATTACH_SEEK_ERR);
1080 send_streams = 0;
1081 goto send_reply;
1082 }
1083
1084 if (!send_streams) {
1085 goto send_reply;
1086 }
1087
1088 ret = make_viewer_streams(session, seek_type, &nb_streams, NULL, NULL);
1089 if (ret < 0) {
1090 goto end_unlock;
1091 }
1092 response.streams_count = htobe32(nb_streams);
1093
1094 send_reply:
1095 health_code_update();
1096 ret = send_response(conn->sock, &response, sizeof(response));
1097 if (ret < 0) {
1098 goto end_unlock;
1099 }
1100 health_code_update();
1101
1102 /*
1103 * Unknown or empty session, just return gracefully, the viewer knows what
1104 * is happening.
1105 */
1106 if (!send_streams || !nb_streams) {
1107 ret = 0;
1108 goto end_unlock;
1109 }
1110
1111 /* Send stream and ignore the sent flag. */
1112 ret = send_viewer_streams(conn->sock, session, 1);
1113 if (ret < 0) {
1114 goto end_unlock;
1115 }
1116
1117 end_unlock:
1118 rcu_read_unlock();
1119 error:
1120 return ret;
1121 }
1122
1123 /*
1124 * Send the next index for a stream.
1125 *
1126 * Return 0 on success or else a negative value.
1127 */
1128 static
1129 int viewer_get_next_index(struct relay_connection *conn)
1130 {
1131 int ret;
1132 struct lttng_viewer_get_next_index request_index;
1133 struct lttng_viewer_index viewer_index;
1134 struct ctf_packet_index packet_index;
1135 struct relay_viewer_stream *vstream;
1136 struct relay_stream *rstream;
1137 struct ctf_trace *ctf_trace;
1138 struct relay_session *session;
1139
1140 assert(conn);
1141
1142 DBG("Viewer get next index");
1143
1144 health_code_update();
1145
1146 ret = recv_request(conn->sock, &request_index, sizeof(request_index));
1147 if (ret < 0) {
1148 goto end;
1149 }
1150 health_code_update();
1151
1152 rcu_read_lock();
1153 vstream = viewer_stream_find_by_id(be64toh(request_index.stream_id));
1154 if (!vstream) {
1155 ret = -1;
1156 goto end_unlock;
1157 }
1158
1159 session = session_find_by_id(conn->sessions_ht, vstream->session_id);
1160 if (!session) {
1161 ret = -1;
1162 goto end_unlock;
1163 }
1164
1165 ctf_trace = ctf_trace_find_by_path(session->ctf_traces_ht, vstream->path_name);
1166 assert(ctf_trace);
1167
1168 memset(&viewer_index, 0, sizeof(viewer_index));
1169
1170 /*
1171 * The viewer should not ask for index on metadata stream.
1172 */
1173 if (vstream->metadata_flag) {
1174 viewer_index.status = htobe32(LTTNG_VIEWER_INDEX_HUP);
1175 goto send_reply;
1176 }
1177
1178 /* First time, we open the index file */
1179 if (vstream->index_read_fd < 0) {
1180 ret = index_open(vstream->path_name, vstream->channel_name,
1181 vstream->tracefile_count, vstream->tracefile_count_current);
1182 if (ret == -ENOENT) {
1183 /*
1184 * The index is created only when the first data packet arrives, it
1185 * might not be ready at the beginning of the session
1186 */
1187 viewer_index.status = htobe32(LTTNG_VIEWER_INDEX_RETRY);
1188 goto send_reply;
1189 } else if (ret < 0) {
1190 viewer_index.status = htobe32(LTTNG_VIEWER_INDEX_ERR);
1191 goto send_reply;
1192 }
1193 vstream->index_read_fd = ret;
1194 }
1195
1196 rstream = stream_find_by_id(relay_streams_ht, vstream->stream_handle);
1197 assert(rstream);
1198
1199 if (!rstream->close_flag) {
1200 if (vstream->abort_flag) {
1201 /* Rotate on abort (overwrite). */
1202 DBG("Viewer rotate because of overwrite");
1203 ret = viewer_stream_rotate(vstream, rstream);
1204 if (ret < 0) {
1205 goto end_unlock;
1206 } else if (ret == 1) {
1207 viewer_index.status = htobe32(LTTNG_VIEWER_INDEX_HUP);
1208 viewer_stream_delete(vstream);
1209 viewer_stream_destroy(ctf_trace, vstream);
1210 goto send_reply;
1211 }
1212 /* ret == 0 means successful so we continue. */
1213 }
1214
1215 pthread_mutex_lock(&rstream->viewer_stream_rotation_lock);
1216 if (rstream->tracefile_count_current == vstream->tracefile_count_current) {
1217 if (rstream->beacon_ts_end != -1ULL &&
1218 vstream->last_sent_index == rstream->total_index_received) {
1219 viewer_index.status = htobe32(LTTNG_VIEWER_INDEX_INACTIVE);
1220 viewer_index.timestamp_end = htobe64(rstream->beacon_ts_end);
1221 pthread_mutex_unlock(&rstream->viewer_stream_rotation_lock);
1222 goto send_reply;
1223 } else if (rstream->total_index_received <= vstream->last_sent_index
1224 && !vstream->close_write_flag) {
1225 /*
1226 * Reader and writer are working in the same tracefile, so we care
1227 * about the number of index received and sent. Otherwise, we read
1228 * up to EOF.
1229 */
1230 pthread_mutex_unlock(&rstream->viewer_stream_rotation_lock);
1231 /* No new index to send, retry later. */
1232 viewer_index.status = htobe32(LTTNG_VIEWER_INDEX_RETRY);
1233 goto send_reply;
1234 }
1235 }
1236 pthread_mutex_unlock(&rstream->viewer_stream_rotation_lock);
1237 } else if (rstream->close_flag && vstream->close_write_flag &&
1238 vstream->total_index_received == vstream->last_sent_index) {
1239 /* Last index sent and current tracefile closed in write */
1240 viewer_index.status = htobe32(LTTNG_VIEWER_INDEX_HUP);
1241 viewer_stream_delete(vstream);
1242 viewer_stream_destroy(ctf_trace, vstream);
1243 goto send_reply;
1244 } else {
1245 vstream->close_write_flag = 1;
1246 }
1247
1248 if (!ctf_trace->metadata_received ||
1249 ctf_trace->metadata_received > ctf_trace->metadata_sent) {
1250 viewer_index.flags |= LTTNG_VIEWER_FLAG_NEW_METADATA;
1251 }
1252
1253 ret = check_new_streams(conn);
1254 if (ret < 0) {
1255 goto end_unlock;
1256 } else if (ret == 1) {
1257 viewer_index.flags |= LTTNG_VIEWER_FLAG_NEW_STREAM;
1258 }
1259
1260 pthread_mutex_lock(&vstream->overwrite_lock);
1261 if (vstream->abort_flag) {
1262 /*
1263 * The file is being overwritten by the writer, we cannot * use it.
1264 */
1265 viewer_index.status = htobe32(LTTNG_VIEWER_INDEX_RETRY);
1266 pthread_mutex_unlock(&vstream->overwrite_lock);
1267 ret = viewer_stream_rotate(vstream, rstream);
1268 if (ret < 0) {
1269 goto end_unlock;
1270 } else if (ret == 1) {
1271 viewer_index.status = htobe32(LTTNG_VIEWER_INDEX_HUP);
1272 viewer_stream_delete(vstream);
1273 viewer_stream_destroy(ctf_trace, vstream);
1274 goto send_reply;
1275 }
1276 goto send_reply;
1277 }
1278
1279 ret = lttng_read(vstream->index_read_fd, &packet_index,
1280 sizeof(packet_index));
1281 pthread_mutex_unlock(&vstream->overwrite_lock);
1282 if (ret < sizeof(packet_index)) {
1283 /*
1284 * The tracefile is closed in write, so we read up to EOF.
1285 */
1286 if (vstream->close_write_flag == 1) {
1287 viewer_index.status = htobe32(LTTNG_VIEWER_INDEX_RETRY);
1288 /* Rotate on normal EOF */
1289 ret = viewer_stream_rotate(vstream, rstream);
1290 if (ret < 0) {
1291 goto end_unlock;
1292 } else if (ret == 1) {
1293 viewer_index.status = htobe32(LTTNG_VIEWER_INDEX_HUP);
1294 viewer_stream_delete(vstream);
1295 viewer_stream_destroy(ctf_trace, vstream);
1296 goto send_reply;
1297 }
1298 } else {
1299 PERROR("Relay reading index file %d", vstream->index_read_fd);
1300 viewer_index.status = htobe32(LTTNG_VIEWER_INDEX_ERR);
1301 }
1302 goto send_reply;
1303 } else {
1304 viewer_index.status = htobe32(LTTNG_VIEWER_INDEX_OK);
1305 vstream->last_sent_index++;
1306 }
1307
1308 /*
1309 * Indexes are stored in big endian, no need to switch before sending.
1310 */
1311 viewer_index.offset = packet_index.offset;
1312 viewer_index.packet_size = packet_index.packet_size;
1313 viewer_index.content_size = packet_index.content_size;
1314 viewer_index.timestamp_begin = packet_index.timestamp_begin;
1315 viewer_index.timestamp_end = packet_index.timestamp_end;
1316 viewer_index.events_discarded = packet_index.events_discarded;
1317 viewer_index.stream_id = packet_index.stream_id;
1318
1319 send_reply:
1320 viewer_index.flags = htobe32(viewer_index.flags);
1321 health_code_update();
1322
1323 ret = send_response(conn->sock, &viewer_index, sizeof(viewer_index));
1324 if (ret < 0) {
1325 goto end_unlock;
1326 }
1327 health_code_update();
1328
1329 DBG("Index %" PRIu64 " for stream %" PRIu64 " sent",
1330 vstream->last_sent_index, vstream->stream_handle);
1331
1332 end_unlock:
1333 rcu_read_unlock();
1334
1335 end:
1336 return ret;
1337 }
1338
1339 /*
1340 * Send the next index for a stream
1341 *
1342 * Return 0 on success or else a negative value.
1343 */
1344 static
1345 int viewer_get_packet(struct relay_connection *conn)
1346 {
1347 int ret, send_data = 0;
1348 char *data = NULL;
1349 uint32_t len = 0;
1350 ssize_t read_len;
1351 struct lttng_viewer_get_packet get_packet_info;
1352 struct lttng_viewer_trace_packet reply;
1353 struct relay_viewer_stream *stream;
1354 struct relay_session *session;
1355 struct ctf_trace *ctf_trace;
1356
1357 assert(conn);
1358
1359 DBG2("Relay get data packet");
1360
1361 health_code_update();
1362
1363 ret = recv_request(conn->sock, &get_packet_info, sizeof(get_packet_info));
1364 if (ret < 0) {
1365 goto end;
1366 }
1367 health_code_update();
1368
1369 /* From this point on, the error label can be reached. */
1370 memset(&reply, 0, sizeof(reply));
1371
1372 rcu_read_lock();
1373 stream = viewer_stream_find_by_id(be64toh(get_packet_info.stream_id));
1374 if (!stream) {
1375 goto error;
1376 }
1377
1378 session = session_find_by_id(conn->sessions_ht, stream->session_id);
1379 if (!session) {
1380 ret = -1;
1381 goto error;
1382 }
1383
1384 ctf_trace = ctf_trace_find_by_path(session->ctf_traces_ht,
1385 stream->path_name);
1386 assert(ctf_trace);
1387
1388 /*
1389 * First time we read this stream, we need open the tracefile, we should
1390 * only arrive here if an index has already been sent to the viewer, so the
1391 * tracefile must exist, if it does not it is a fatal error.
1392 */
1393 if (stream->read_fd < 0) {
1394 char fullpath[PATH_MAX];
1395
1396 if (stream->tracefile_count > 0) {
1397 ret = snprintf(fullpath, PATH_MAX, "%s/%s_%" PRIu64, stream->path_name,
1398 stream->channel_name,
1399 stream->tracefile_count_current);
1400 } else {
1401 ret = snprintf(fullpath, PATH_MAX, "%s/%s", stream->path_name,
1402 stream->channel_name);
1403 }
1404 if (ret < 0) {
1405 goto error;
1406 }
1407 ret = open(fullpath, O_RDONLY);
1408 if (ret < 0) {
1409 PERROR("Relay opening trace file");
1410 goto error;
1411 }
1412 stream->read_fd = ret;
1413 }
1414
1415 if (!ctf_trace->metadata_received ||
1416 ctf_trace->metadata_received > ctf_trace->metadata_sent) {
1417 reply.status = htobe32(LTTNG_VIEWER_GET_PACKET_ERR);
1418 reply.flags |= LTTNG_VIEWER_FLAG_NEW_METADATA;
1419 goto send_reply;
1420 }
1421
1422 ret = check_new_streams(conn);
1423 if (ret < 0) {
1424 goto end_unlock;
1425 } else if (ret == 1) {
1426 reply.status = htobe32(LTTNG_VIEWER_GET_PACKET_ERR);
1427 reply.flags |= LTTNG_VIEWER_FLAG_NEW_STREAM;
1428 goto send_reply;
1429 }
1430
1431 len = be32toh(get_packet_info.len);
1432 data = zmalloc(len);
1433 if (!data) {
1434 PERROR("relay data zmalloc");
1435 goto error;
1436 }
1437
1438 ret = lseek(stream->read_fd, be64toh(get_packet_info.offset), SEEK_SET);
1439 if (ret < 0) {
1440 /*
1441 * If the read fd was closed by the streaming side, the
1442 * abort_flag will be set to 1, otherwise it is an error.
1443 */
1444 if (stream->abort_flag == 0) {
1445 PERROR("lseek");
1446 goto error;
1447 }
1448 reply.status = htobe32(LTTNG_VIEWER_GET_PACKET_EOF);
1449 goto send_reply;
1450 }
1451 read_len = lttng_read(stream->read_fd, data, len);
1452 if (read_len < len) {
1453 /*
1454 * If the read fd was closed by the streaming side, the
1455 * abort_flag will be set to 1, otherwise it is an error.
1456 */
1457 if (stream->abort_flag == 0) {
1458 PERROR("Relay reading trace file, fd: %d, offset: %" PRIu64,
1459 stream->read_fd,
1460 be64toh(get_packet_info.offset));
1461 goto error;
1462 } else {
1463 reply.status = htobe32(LTTNG_VIEWER_GET_PACKET_EOF);
1464 goto send_reply;
1465 }
1466 }
1467 reply.status = htobe32(LTTNG_VIEWER_GET_PACKET_OK);
1468 reply.len = htobe32(len);
1469 send_data = 1;
1470 goto send_reply;
1471
1472 error:
1473 reply.status = htobe32(LTTNG_VIEWER_GET_PACKET_ERR);
1474
1475 send_reply:
1476 reply.flags = htobe32(reply.flags);
1477
1478 health_code_update();
1479
1480 ret = send_response(conn->sock, &reply, sizeof(reply));
1481 if (ret < 0) {
1482 goto end_unlock;
1483 }
1484 health_code_update();
1485
1486 if (send_data) {
1487 health_code_update();
1488 ret = send_response(conn->sock, data, len);
1489 if (ret < 0) {
1490 goto end_unlock;
1491 }
1492 health_code_update();
1493 }
1494
1495 DBG("Sent %u bytes for stream %" PRIu64, len,
1496 be64toh(get_packet_info.stream_id));
1497
1498 end_unlock:
1499 free(data);
1500 rcu_read_unlock();
1501
1502 end:
1503 return ret;
1504 }
1505
1506 /*
1507 * Send the session's metadata
1508 *
1509 * Return 0 on success else a negative value.
1510 */
1511 static
1512 int viewer_get_metadata(struct relay_connection *conn)
1513 {
1514 int ret = 0;
1515 ssize_t read_len;
1516 uint64_t len = 0;
1517 char *data = NULL;
1518 struct lttng_viewer_get_metadata request;
1519 struct lttng_viewer_metadata_packet reply;
1520 struct relay_viewer_stream *stream;
1521 struct ctf_trace *ctf_trace;
1522 struct relay_session *session;
1523
1524 assert(conn);
1525
1526 DBG("Relay get metadata");
1527
1528 health_code_update();
1529
1530 ret = recv_request(conn->sock, &request, sizeof(request));
1531 if (ret < 0) {
1532 goto end;
1533 }
1534 health_code_update();
1535
1536 rcu_read_lock();
1537 stream = viewer_stream_find_by_id(be64toh(request.stream_id));
1538 if (!stream || !stream->metadata_flag) {
1539 ERR("Invalid metadata stream");
1540 goto error;
1541 }
1542
1543 session = session_find_by_id(conn->sessions_ht, stream->session_id);
1544 if (!session) {
1545 ret = -1;
1546 goto error;
1547 }
1548
1549 ctf_trace = ctf_trace_find_by_path(session->ctf_traces_ht,
1550 stream->path_name);
1551 assert(ctf_trace);
1552 assert(ctf_trace->metadata_sent <= ctf_trace->metadata_received);
1553
1554 len = ctf_trace->metadata_received - ctf_trace->metadata_sent;
1555 if (len == 0) {
1556 reply.status = htobe32(LTTNG_VIEWER_NO_NEW_METADATA);
1557 goto send_reply;
1558 }
1559
1560 /* first time, we open the metadata file */
1561 if (stream->read_fd < 0) {
1562 char fullpath[PATH_MAX];
1563
1564 ret = snprintf(fullpath, PATH_MAX, "%s/%s", stream->path_name,
1565 stream->channel_name);
1566 if (ret < 0) {
1567 goto error;
1568 }
1569 ret = open(fullpath, O_RDONLY);
1570 if (ret < 0) {
1571 PERROR("Relay opening metadata file");
1572 goto error;
1573 }
1574 stream->read_fd = ret;
1575 }
1576
1577 reply.len = htobe64(len);
1578 data = zmalloc(len);
1579 if (!data) {
1580 PERROR("viewer metadata zmalloc");
1581 goto error;
1582 }
1583
1584 read_len = lttng_read(stream->read_fd, data, len);
1585 if (read_len < len) {
1586 PERROR("Relay reading metadata file");
1587 goto error;
1588 }
1589 ctf_trace->metadata_sent += read_len;
1590 reply.status = htobe32(LTTNG_VIEWER_METADATA_OK);
1591 goto send_reply;
1592
1593 error:
1594 reply.status = htobe32(LTTNG_VIEWER_METADATA_ERR);
1595
1596 send_reply:
1597 health_code_update();
1598 ret = send_response(conn->sock, &reply, sizeof(reply));
1599 if (ret < 0) {
1600 goto end_unlock;
1601 }
1602 health_code_update();
1603
1604 if (len > 0) {
1605 ret = send_response(conn->sock, data, len);
1606 if (ret < 0) {
1607 goto end_unlock;
1608 }
1609 }
1610
1611 DBG("Sent %" PRIu64 " bytes of metadata for stream %" PRIu64, len,
1612 be64toh(request.stream_id));
1613
1614 DBG("Metadata sent");
1615
1616 end_unlock:
1617 free(data);
1618 rcu_read_unlock();
1619 end:
1620 return ret;
1621 }
1622
1623 /*
1624 * Create a viewer session.
1625 *
1626 * Return 0 on success or else a negative value.
1627 */
1628 static
1629 int viewer_create_session(struct relay_connection *conn)
1630 {
1631 int ret;
1632 struct lttng_viewer_create_session_response resp;
1633
1634 DBG("Viewer create session received");
1635
1636 resp.status = htobe32(LTTNG_VIEWER_CREATE_SESSION_OK);
1637 conn->viewer_session = zmalloc(sizeof(conn->viewer_session));
1638 if (!conn->viewer_session) {
1639 ERR("Allocation viewer session");
1640 resp.status = htobe32(LTTNG_VIEWER_CREATE_SESSION_ERR);
1641 goto send_reply;
1642 }
1643 CDS_INIT_LIST_HEAD(&conn->viewer_session->sessions_head);
1644
1645 send_reply:
1646 health_code_update();
1647 ret = send_response(conn->sock, &resp, sizeof(resp));
1648 if (ret < 0) {
1649 goto end;
1650 }
1651 health_code_update();
1652 ret = 0;
1653
1654 end:
1655 return ret;
1656 }
1657
1658
1659 /*
1660 * live_relay_unknown_command: send -1 if received unknown command
1661 */
1662 static
1663 void live_relay_unknown_command(struct relay_connection *conn)
1664 {
1665 struct lttcomm_relayd_generic_reply reply;
1666
1667 reply.ret_code = htobe32(LTTNG_ERR_UNK);
1668 (void) send_response(conn->sock, &reply, sizeof(reply));
1669 }
1670
1671 /*
1672 * Process the commands received on the control socket
1673 */
1674 static
1675 int process_control(struct lttng_viewer_cmd *recv_hdr,
1676 struct relay_connection *conn)
1677 {
1678 int ret = 0;
1679 uint32_t msg_value;
1680
1681 assert(recv_hdr);
1682 assert(conn);
1683
1684 msg_value = be32toh(recv_hdr->cmd);
1685
1686 /*
1687 * Make sure we've done the version check before any command other then a
1688 * new client connection.
1689 */
1690 if (msg_value != LTTNG_VIEWER_CONNECT && !conn->version_check_done) {
1691 ERR("Viewer conn value %" PRIu32 " before version check", msg_value);
1692 ret = -1;
1693 goto end;
1694 }
1695
1696 switch (msg_value) {
1697 case LTTNG_VIEWER_CONNECT:
1698 ret = viewer_connect(conn);
1699 break;
1700 case LTTNG_VIEWER_LIST_SESSIONS:
1701 ret = viewer_list_sessions(conn);
1702 break;
1703 case LTTNG_VIEWER_ATTACH_SESSION:
1704 ret = viewer_attach_session(conn);
1705 break;
1706 case LTTNG_VIEWER_GET_NEXT_INDEX:
1707 ret = viewer_get_next_index(conn);
1708 break;
1709 case LTTNG_VIEWER_GET_PACKET:
1710 ret = viewer_get_packet(conn);
1711 break;
1712 case LTTNG_VIEWER_GET_METADATA:
1713 ret = viewer_get_metadata(conn);
1714 break;
1715 case LTTNG_VIEWER_GET_NEW_STREAMS:
1716 ret = viewer_get_new_streams(conn);
1717 break;
1718 case LTTNG_VIEWER_CREATE_SESSION:
1719 ret = viewer_create_session(conn);
1720 break;
1721 default:
1722 ERR("Received unknown viewer command (%u)", be32toh(recv_hdr->cmd));
1723 live_relay_unknown_command(conn);
1724 ret = -1;
1725 goto end;
1726 }
1727
1728 end:
1729 return ret;
1730 }
1731
1732 static
1733 void cleanup_connection_pollfd(struct lttng_poll_event *events, int pollfd)
1734 {
1735 int ret;
1736
1737 assert(events);
1738
1739 (void) lttng_poll_del(events, pollfd);
1740
1741 ret = close(pollfd);
1742 if (ret < 0) {
1743 ERR("Closing pollfd %d", pollfd);
1744 }
1745 }
1746
1747 /*
1748 * Delete and destroy a connection.
1749 *
1750 * RCU read side lock MUST be acquired.
1751 */
1752 static void destroy_connection(struct lttng_ht *relay_connections_ht,
1753 struct relay_connection *conn)
1754 {
1755 struct relay_session *session, *tmp_session;
1756
1757 assert(relay_connections_ht);
1758 assert(conn);
1759
1760 connection_delete(relay_connections_ht, conn);
1761
1762 if (!conn->viewer_session) {
1763 goto end;
1764 }
1765
1766 rcu_read_lock();
1767 cds_list_for_each_entry_safe(session, tmp_session,
1768 &conn->viewer_session->sessions_head,
1769 viewer_session_list) {
1770 DBG("Cleaning connection of session ID %" PRIu64, session->id);
1771 cleanup_session(conn, session);
1772 }
1773 rcu_read_unlock();
1774
1775 end:
1776 connection_destroy(conn);
1777 }
1778
1779 /*
1780 * This thread does the actual work
1781 */
1782 static
1783 void *thread_worker(void *data)
1784 {
1785 int ret, err = -1;
1786 uint32_t nb_fd;
1787 struct relay_connection *conn;
1788 struct lttng_poll_event events;
1789 struct lttng_ht *relay_connections_ht;
1790 struct lttng_ht_iter iter;
1791 struct lttng_viewer_cmd recv_hdr;
1792 struct relay_local_data *relay_ctx = (struct relay_local_data *) data;
1793 struct lttng_ht *sessions_ht = relay_ctx->sessions_ht;
1794
1795 DBG("[thread] Live viewer relay worker started");
1796
1797 rcu_register_thread();
1798
1799 health_register(health_relayd, HEALTH_RELAYD_TYPE_LIVE_WORKER);
1800
1801 if (testpoint(relayd_thread_live_worker)) {
1802 goto error_testpoint;
1803 }
1804
1805 /* table of connections indexed on socket */
1806 relay_connections_ht = lttng_ht_new(0, LTTNG_HT_TYPE_ULONG);
1807 if (!relay_connections_ht) {
1808 goto relay_connections_ht_error;
1809 }
1810
1811 ret = create_thread_poll_set(&events, 2);
1812 if (ret < 0) {
1813 goto error_poll_create;
1814 }
1815
1816 ret = lttng_poll_add(&events, live_conn_pipe[0], LPOLLIN | LPOLLRDHUP);
1817 if (ret < 0) {
1818 goto error;
1819 }
1820
1821 restart:
1822 while (1) {
1823 int i;
1824
1825 health_code_update();
1826
1827 /* Infinite blocking call, waiting for transmission */
1828 DBG3("Relayd live viewer worker thread polling...");
1829 health_poll_entry();
1830 ret = lttng_poll_wait(&events, -1);
1831 health_poll_exit();
1832 if (ret < 0) {
1833 /*
1834 * Restart interrupted system call.
1835 */
1836 if (errno == EINTR) {
1837 goto restart;
1838 }
1839 goto error;
1840 }
1841
1842 nb_fd = ret;
1843
1844 /*
1845 * Process control. The control connection is prioritised so we don't
1846 * starve it with high throughput tracing data on the data
1847 * connection.
1848 */
1849 for (i = 0; i < nb_fd; i++) {
1850 /* Fetch once the poll data */
1851 uint32_t revents = LTTNG_POLL_GETEV(&events, i);
1852 int pollfd = LTTNG_POLL_GETFD(&events, i);
1853
1854 health_code_update();
1855
1856 /* Thread quit pipe has been closed. Killing thread. */
1857 ret = check_live_conn_pipe(pollfd, revents);
1858 if (ret) {
1859 err = 0;
1860 goto exit;
1861 }
1862
1863 /* Inspect the relay conn pipe for new connection */
1864 if (pollfd == live_conn_pipe[0]) {
1865 if (revents & (LPOLLERR | LPOLLHUP | LPOLLRDHUP)) {
1866 ERR("Relay live pipe error");
1867 goto error;
1868 } else if (revents & LPOLLIN) {
1869 ret = lttng_read(live_conn_pipe[0], &conn, sizeof(conn));
1870 if (ret < 0) {
1871 goto error;
1872 }
1873 conn->sessions_ht = sessions_ht;
1874 connection_init(conn);
1875 lttng_poll_add(&events, conn->sock->fd,
1876 LPOLLIN | LPOLLRDHUP);
1877 rcu_read_lock();
1878 lttng_ht_add_unique_ulong(relay_connections_ht,
1879 &conn->sock_n);
1880 rcu_read_unlock();
1881 DBG("Connection socket %d added", conn->sock->fd);
1882 }
1883 } else {
1884 rcu_read_lock();
1885 conn = connection_find_by_sock(relay_connections_ht, pollfd);
1886 /* If not found, there is a synchronization issue. */
1887 assert(conn);
1888
1889 if (revents & (LPOLLERR | LPOLLHUP | LPOLLRDHUP)) {
1890 cleanup_connection_pollfd(&events, pollfd);
1891 destroy_connection(relay_connections_ht, conn);
1892 } else if (revents & LPOLLIN) {
1893 ret = conn->sock->ops->recvmsg(conn->sock, &recv_hdr,
1894 sizeof(recv_hdr), 0);
1895 if (ret <= 0) {
1896 /* Connection closed */
1897 cleanup_connection_pollfd(&events, pollfd);
1898 destroy_connection(relay_connections_ht, conn);
1899 DBG("Viewer control conn closed with %d", pollfd);
1900 } else {
1901 ret = process_control(&recv_hdr, conn);
1902 if (ret < 0) {
1903 /* Clear the session on error. */
1904 cleanup_connection_pollfd(&events, pollfd);
1905 destroy_connection(relay_connections_ht, conn);
1906 DBG("Viewer connection closed with %d", pollfd);
1907 }
1908 }
1909 }
1910 rcu_read_unlock();
1911 }
1912 }
1913 }
1914
1915 exit:
1916 error:
1917 lttng_poll_clean(&events);
1918
1919 /* Cleanup reamaining connection object. */
1920 rcu_read_lock();
1921 cds_lfht_for_each_entry(relay_connections_ht->ht, &iter.iter, conn,
1922 sock_n.node) {
1923 health_code_update();
1924 destroy_connection(relay_connections_ht, conn);
1925 }
1926 rcu_read_unlock();
1927 error_poll_create:
1928 lttng_ht_destroy(relay_connections_ht);
1929 relay_connections_ht_error:
1930 /* Close relay conn pipes */
1931 utils_close_pipe(live_conn_pipe);
1932 if (err) {
1933 DBG("Viewer worker thread exited with error");
1934 }
1935 DBG("Viewer worker thread cleanup complete");
1936 error_testpoint:
1937 if (err) {
1938 health_error();
1939 ERR("Health error occurred in %s", __func__);
1940 }
1941 health_unregister(health_relayd);
1942 stop_threads();
1943 rcu_unregister_thread();
1944 return NULL;
1945 }
1946
1947 /*
1948 * Create the relay command pipe to wake thread_manage_apps.
1949 * Closed in cleanup().
1950 */
1951 static int create_conn_pipe(void)
1952 {
1953 int ret;
1954
1955 ret = utils_create_pipe_cloexec(live_conn_pipe);
1956
1957 return ret;
1958 }
1959
1960 void live_stop_threads(void)
1961 {
1962 int ret;
1963 void *status;
1964
1965 stop_threads();
1966
1967 ret = pthread_join(live_listener_thread, &status);
1968 if (ret != 0) {
1969 PERROR("pthread_join live listener");
1970 goto error; /* join error, exit without cleanup */
1971 }
1972
1973 ret = pthread_join(live_worker_thread, &status);
1974 if (ret != 0) {
1975 PERROR("pthread_join live worker");
1976 goto error; /* join error, exit without cleanup */
1977 }
1978
1979 ret = pthread_join(live_dispatcher_thread, &status);
1980 if (ret != 0) {
1981 PERROR("pthread_join live dispatcher");
1982 goto error; /* join error, exit without cleanup */
1983 }
1984
1985 cleanup();
1986
1987 error:
1988 return;
1989 }
1990
1991 /*
1992 * main
1993 */
1994 int live_start_threads(struct lttng_uri *uri,
1995 struct relay_local_data *relay_ctx)
1996 {
1997 int ret = 0;
1998 void *status;
1999 int is_root;
2000
2001 assert(uri);
2002 live_uri = uri;
2003
2004 /* Check if daemon is UID = 0 */
2005 is_root = !getuid();
2006
2007 if (!is_root) {
2008 if (live_uri->port < 1024) {
2009 ERR("Need to be root to use ports < 1024");
2010 ret = -1;
2011 goto exit;
2012 }
2013 }
2014
2015 /* Setup the thread apps communication pipe. */
2016 if ((ret = create_conn_pipe()) < 0) {
2017 goto exit;
2018 }
2019
2020 /* Init relay command queue. */
2021 cds_wfq_init(&viewer_conn_queue.queue);
2022
2023 /* Set up max poll set size */
2024 lttng_poll_set_max_size();
2025
2026 /* Setup the dispatcher thread */
2027 ret = pthread_create(&live_dispatcher_thread, NULL,
2028 thread_dispatcher, (void *) NULL);
2029 if (ret != 0) {
2030 PERROR("pthread_create viewer dispatcher");
2031 goto exit_dispatcher;
2032 }
2033
2034 /* Setup the worker thread */
2035 ret = pthread_create(&live_worker_thread, NULL,
2036 thread_worker, relay_ctx);
2037 if (ret != 0) {
2038 PERROR("pthread_create viewer worker");
2039 goto exit_worker;
2040 }
2041
2042 /* Setup the listener thread */
2043 ret = pthread_create(&live_listener_thread, NULL,
2044 thread_listener, (void *) NULL);
2045 if (ret != 0) {
2046 PERROR("pthread_create viewer listener");
2047 goto exit_listener;
2048 }
2049
2050 ret = 0;
2051 goto end;
2052
2053 exit_listener:
2054 ret = pthread_join(live_listener_thread, &status);
2055 if (ret != 0) {
2056 PERROR("pthread_join live listener");
2057 goto error; /* join error, exit without cleanup */
2058 }
2059
2060 exit_worker:
2061 ret = pthread_join(live_worker_thread, &status);
2062 if (ret != 0) {
2063 PERROR("pthread_join live worker");
2064 goto error; /* join error, exit without cleanup */
2065 }
2066
2067 exit_dispatcher:
2068 ret = pthread_join(live_dispatcher_thread, &status);
2069 if (ret != 0) {
2070 PERROR("pthread_join live dispatcher");
2071 goto error; /* join error, exit without cleanup */
2072 }
2073
2074 exit:
2075 cleanup();
2076
2077 end:
2078 error:
2079 return ret;
2080 }
This page took 0.125403 seconds and 4 git commands to generate.