Fix: deadlock during rotation
[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(thread_quit_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, thread_quit_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_thread_quit_pipe(int fd, uint32_t events)
400 {
401 if (fd == thread_quit_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_thread_quit_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 memset(&reply, 0, sizeof(reply));
698 reply.major = RELAYD_VERSION_COMM_MAJOR;
699 reply.minor = RELAYD_VERSION_COMM_MINOR;
700
701 /* Major versions must be the same */
702 if (reply.major != be32toh(msg.major)) {
703 DBG("Incompatible major versions ([relayd] %u vs [client] %u)",
704 reply.major, be32toh(msg.major));
705 ret = -1;
706 goto end;
707 }
708
709 conn->major = reply.major;
710 /* We adapt to the lowest compatible version */
711 if (reply.minor <= be32toh(msg.minor)) {
712 conn->minor = reply.minor;
713 } else {
714 conn->minor = be32toh(msg.minor);
715 }
716
717 if (be32toh(msg.type) == LTTNG_VIEWER_CLIENT_COMMAND) {
718 conn->type = RELAY_VIEWER_COMMAND;
719 } else if (be32toh(msg.type) == LTTNG_VIEWER_CLIENT_NOTIFICATION) {
720 conn->type = RELAY_VIEWER_NOTIFICATION;
721 } else {
722 ERR("Unknown connection type : %u", be32toh(msg.type));
723 ret = -1;
724 goto end;
725 }
726
727 reply.major = htobe32(reply.major);
728 reply.minor = htobe32(reply.minor);
729 if (conn->type == RELAY_VIEWER_COMMAND) {
730 reply.viewer_session_id = htobe64(++last_relay_viewer_session_id);
731 }
732
733 health_code_update();
734
735 ret = send_response(conn->sock, &reply, sizeof(reply));
736 if (ret < 0) {
737 goto end;
738 }
739
740 health_code_update();
741
742 DBG("Version check done using protocol %u.%u", conn->major, conn->minor);
743 ret = 0;
744
745 end:
746 return ret;
747 }
748
749 /*
750 * Send the viewer the list of current sessions.
751 *
752 * Return 0 on success or else a negative value.
753 */
754 static
755 int viewer_list_sessions(struct relay_connection *conn)
756 {
757 int ret;
758 struct lttng_viewer_list_sessions session_list;
759 unsigned long count;
760 long approx_before, approx_after;
761 struct lttng_ht_iter iter;
762 struct lttng_viewer_session send_session;
763 struct relay_session *session;
764
765 DBG("List sessions received");
766
767 rcu_read_lock();
768 cds_lfht_count_nodes(conn->sessions_ht->ht, &approx_before, &count,
769 &approx_after);
770 session_list.sessions_count = htobe32(count);
771
772 health_code_update();
773
774 ret = send_response(conn->sock, &session_list, sizeof(session_list));
775 if (ret < 0) {
776 goto end_unlock;
777 }
778
779 health_code_update();
780
781 cds_lfht_for_each_entry(conn->sessions_ht->ht, &iter.iter, session,
782 session_n.node) {
783 health_code_update();
784
785 strncpy(send_session.session_name, session->session_name,
786 sizeof(send_session.session_name));
787 strncpy(send_session.hostname, session->hostname,
788 sizeof(send_session.hostname));
789 send_session.id = htobe64(session->id);
790 send_session.live_timer = htobe32(session->live_timer);
791 send_session.clients = htobe32(session->viewer_refcount);
792 send_session.streams = htobe32(session->stream_count);
793
794 health_code_update();
795
796 ret = send_response(conn->sock, &send_session, sizeof(send_session));
797 if (ret < 0) {
798 goto end_unlock;
799 }
800 }
801 health_code_update();
802
803 rcu_read_unlock();
804 ret = 0;
805 goto end;
806
807 end_unlock:
808 rcu_read_unlock();
809
810 end:
811 return ret;
812 }
813
814 /*
815 * Check if a connection is attached to a session.
816 * Return 1 if attached, 0 if not attached, a negative value on error.
817 */
818 static
819 int session_attached(struct relay_connection *conn, uint64_t session_id)
820 {
821 struct relay_session *session;
822 int found = 0;
823
824 if (!conn->viewer_session) {
825 goto end;
826 }
827 cds_list_for_each_entry(session,
828 &conn->viewer_session->sessions_head,
829 viewer_session_list) {
830 if (session->id == session_id) {
831 found = 1;
832 goto end;
833 }
834 }
835
836 end:
837 return found;
838 }
839
840 /*
841 * Delete all streams for a specific session ID.
842 */
843 static void destroy_viewer_streams_by_session(struct relay_session *session)
844 {
845 struct relay_viewer_stream *stream;
846 struct lttng_ht_iter iter;
847
848 assert(session);
849
850 rcu_read_lock();
851 cds_lfht_for_each_entry(viewer_streams_ht->ht, &iter.iter, stream,
852 stream_n.node) {
853 struct ctf_trace *ctf_trace;
854
855 health_code_update();
856 if (stream->session_id != session->id) {
857 continue;
858 }
859
860 ctf_trace = ctf_trace_find_by_path(session->ctf_traces_ht,
861 stream->path_name);
862 assert(ctf_trace);
863
864 viewer_stream_delete(stream);
865
866 if (stream->metadata_flag) {
867 ctf_trace->metadata_sent = 0;
868 ctf_trace->viewer_metadata_stream = NULL;
869 }
870
871 viewer_stream_destroy(ctf_trace, stream);
872 }
873 rcu_read_unlock();
874 }
875
876 static void try_destroy_streams(struct relay_session *session)
877 {
878 struct ctf_trace *ctf_trace;
879 struct lttng_ht_iter iter;
880
881 assert(session);
882
883 cds_lfht_for_each_entry(session->ctf_traces_ht->ht, &iter.iter, ctf_trace,
884 node.node) {
885 /* Attempt to destroy the ctf trace of that session. */
886 ctf_trace_try_destroy(session, ctf_trace);
887 }
888 }
889
890 /*
891 * Cleanup a session.
892 */
893 static void cleanup_session(struct relay_connection *conn,
894 struct relay_session *session)
895 {
896 /*
897 * Very important that this is done before destroying the session so we
898 * can put back every viewer stream reference from the ctf_trace.
899 */
900 destroy_viewer_streams_by_session(session);
901 try_destroy_streams(session);
902 cds_list_del(&session->viewer_session_list);
903 session_viewer_try_destroy(conn->sessions_ht, session);
904 }
905
906 /*
907 * Send the viewer the list of current sessions.
908 */
909 static
910 int viewer_get_new_streams(struct relay_connection *conn)
911 {
912 int ret, send_streams = 0;
913 uint32_t nb_created = 0, nb_unsent = 0, nb_streams = 0;
914 struct lttng_viewer_new_streams_request request;
915 struct lttng_viewer_new_streams_response response;
916 struct relay_session *session;
917 uint64_t session_id;
918
919 assert(conn);
920
921 DBG("Get new streams received");
922
923 health_code_update();
924
925 /* Receive the request from the connected client. */
926 ret = recv_request(conn->sock, &request, sizeof(request));
927 if (ret < 0) {
928 goto error;
929 }
930 session_id = be64toh(request.session_id);
931
932 health_code_update();
933
934 rcu_read_lock();
935 session = session_find_by_id(conn->sessions_ht, session_id);
936 if (!session) {
937 DBG("Relay session %" PRIu64 " not found", session_id);
938 response.status = htobe32(LTTNG_VIEWER_NEW_STREAMS_ERR);
939 goto send_reply;
940 }
941
942 if (!session_attached(conn, session_id)) {
943 send_streams = 0;
944 response.status = htobe32(LTTNG_VIEWER_NEW_STREAMS_ERR);
945 goto send_reply;
946 }
947
948 send_streams = 1;
949 response.status = htobe32(LTTNG_VIEWER_NEW_STREAMS_OK);
950
951 ret = make_viewer_streams(session, LTTNG_VIEWER_SEEK_LAST, NULL, &nb_unsent,
952 &nb_created);
953 if (ret < 0) {
954 goto end_unlock;
955 }
956 /* Only send back the newly created streams with the unsent ones. */
957 nb_streams = nb_created + nb_unsent;
958 response.streams_count = htobe32(nb_streams);
959
960 /*
961 * If the session is closed and we have no new streams to send,
962 * it means that the viewer has already received the whole trace
963 * for this session and should now close it.
964 */
965 if (nb_streams == 0 && session->close_flag) {
966 send_streams = 0;
967 response.status = htobe32(LTTNG_VIEWER_NEW_STREAMS_HUP);
968 /*
969 * Remove the session from the attached list of the connection
970 * and try to destroy it.
971 */
972 cds_list_del(&session->viewer_session_list);
973 cleanup_session(conn, session);
974 goto send_reply;
975 }
976
977 send_reply:
978 health_code_update();
979 ret = send_response(conn->sock, &response, sizeof(response));
980 if (ret < 0) {
981 goto end_unlock;
982 }
983 health_code_update();
984
985 /*
986 * Unknown or empty session, just return gracefully, the viewer knows what
987 * is happening.
988 */
989 if (!send_streams || !nb_streams) {
990 ret = 0;
991 goto end_unlock;
992 }
993
994 /*
995 * Send stream and *DON'T* ignore the sent flag so every viewer streams
996 * that were not sent from that point will be sent to the viewer.
997 */
998 ret = send_viewer_streams(conn->sock, session, 0);
999 if (ret < 0) {
1000 goto end_unlock;
1001 }
1002
1003 end_unlock:
1004 rcu_read_unlock();
1005 error:
1006 return ret;
1007 }
1008
1009 /*
1010 * Send the viewer the list of current sessions.
1011 */
1012 static
1013 int viewer_attach_session(struct relay_connection *conn)
1014 {
1015 int send_streams = 0;
1016 ssize_t ret;
1017 uint32_t nb_streams = 0;
1018 enum lttng_viewer_seek seek_type;
1019 struct lttng_viewer_attach_session_request request;
1020 struct lttng_viewer_attach_session_response response;
1021 struct relay_session *session;
1022
1023 assert(conn);
1024
1025 health_code_update();
1026
1027 /* Receive the request from the connected client. */
1028 ret = recv_request(conn->sock, &request, sizeof(request));
1029 if (ret < 0) {
1030 goto error;
1031 }
1032
1033 health_code_update();
1034
1035 if (!conn->viewer_session) {
1036 DBG("Client trying to attach before creating a live viewer session");
1037 response.status = htobe32(LTTNG_VIEWER_ATTACH_NO_SESSION);
1038 goto send_reply;
1039 }
1040
1041 rcu_read_lock();
1042 session = session_find_by_id(conn->sessions_ht,
1043 be64toh(request.session_id));
1044 if (!session) {
1045 DBG("Relay session %" PRIu64 " not found",
1046 be64toh(request.session_id));
1047 response.status = htobe32(LTTNG_VIEWER_ATTACH_UNK);
1048 goto send_reply;
1049 }
1050 session_viewer_attach(session);
1051 DBG("Attach session ID %" PRIu64 " received", be64toh(request.session_id));
1052
1053 if (uatomic_read(&session->viewer_refcount) > 1) {
1054 DBG("Already a viewer attached");
1055 response.status = htobe32(LTTNG_VIEWER_ATTACH_ALREADY);
1056 session_viewer_detach(session);
1057 goto send_reply;
1058 } else if (session->live_timer == 0) {
1059 DBG("Not live session");
1060 response.status = htobe32(LTTNG_VIEWER_ATTACH_NOT_LIVE);
1061 goto send_reply;
1062 } else {
1063 send_streams = 1;
1064 response.status = htobe32(LTTNG_VIEWER_ATTACH_OK);
1065 cds_list_add(&session->viewer_session_list,
1066 &conn->viewer_session->sessions_head);
1067 }
1068
1069 switch (be32toh(request.seek)) {
1070 case LTTNG_VIEWER_SEEK_BEGINNING:
1071 case LTTNG_VIEWER_SEEK_LAST:
1072 seek_type = be32toh(request.seek);
1073 break;
1074 default:
1075 ERR("Wrong seek parameter");
1076 response.status = htobe32(LTTNG_VIEWER_ATTACH_SEEK_ERR);
1077 send_streams = 0;
1078 goto send_reply;
1079 }
1080
1081 ret = make_viewer_streams(session, seek_type, &nb_streams, NULL, NULL);
1082 if (ret < 0) {
1083 goto end_unlock;
1084 }
1085 response.streams_count = htobe32(nb_streams);
1086
1087 send_reply:
1088 health_code_update();
1089 ret = send_response(conn->sock, &response, sizeof(response));
1090 if (ret < 0) {
1091 goto end_unlock;
1092 }
1093 health_code_update();
1094
1095 /*
1096 * Unknown or empty session, just return gracefully, the viewer knows what
1097 * is happening.
1098 */
1099 if (!send_streams || !nb_streams) {
1100 ret = 0;
1101 goto end_unlock;
1102 }
1103
1104 /* Send stream and ignore the sent flag. */
1105 ret = send_viewer_streams(conn->sock, session, 1);
1106 if (ret < 0) {
1107 goto end_unlock;
1108 }
1109
1110 end_unlock:
1111 rcu_read_unlock();
1112 error:
1113 return ret;
1114 }
1115
1116 /*
1117 * Open the index file if needed for the given vstream.
1118 *
1119 * If an index file is successfully opened, the index_read_fd of the stream is
1120 * set with it.
1121 *
1122 * Return 0 on success, a negative value on error (-ENOENT if not ready yet).
1123 */
1124 static int try_open_index(struct relay_viewer_stream *vstream,
1125 struct relay_stream *rstream)
1126 {
1127 int ret = 0;
1128
1129 assert(vstream);
1130 assert(rstream);
1131
1132 if (vstream->index_read_fd >= 0) {
1133 goto end;
1134 }
1135
1136 /*
1137 * First time, we open the index file and at least one index is ready. The
1138 * race between the read and write of the total_index_received is
1139 * acceptable here since the client will be notified to simply come back
1140 * and get the next index.
1141 */
1142 if (rstream->total_index_received <= 0) {
1143 ret = -ENOENT;
1144 goto end;
1145 }
1146 ret = index_open(vstream->path_name, vstream->channel_name,
1147 vstream->tracefile_count, vstream->tracefile_count_current);
1148 if (ret >= 0) {
1149 vstream->index_read_fd = ret;
1150 ret = 0;
1151 goto end;
1152 }
1153
1154 end:
1155 return ret;
1156 }
1157
1158 /*
1159 * Check the status of the index for the given stream. This function updates
1160 * the index structure if needed and can destroy the vstream also for the HUP
1161 * situation.
1162 *
1163 * Return 0 means that we can proceed with the index. A value of 1 means that
1164 * the index has been updated and is ready to be send to the client. A negative
1165 * value indicates an error that can't be handled.
1166 */
1167 static int check_index_status(struct relay_viewer_stream *vstream,
1168 struct relay_stream *rstream, struct ctf_trace *trace,
1169 struct lttng_viewer_index *index)
1170 {
1171 int ret;
1172
1173 assert(vstream);
1174 assert(rstream);
1175 assert(index);
1176 assert(trace);
1177
1178 if (!rstream->close_flag) {
1179 /* Rotate on abort (overwrite). */
1180 if (vstream->abort_flag) {
1181 DBG("Viewer stream %" PRIu64 " rotate because of overwrite",
1182 vstream->stream_handle);
1183 ret = viewer_stream_rotate(vstream, rstream);
1184 if (ret < 0) {
1185 goto error;
1186 } else if (ret == 1) {
1187 /* EOF */
1188 index->status = htobe32(LTTNG_VIEWER_INDEX_HUP);
1189 goto hup;
1190 }
1191 /* ret == 0 means successful so we continue. */
1192 }
1193
1194 /* Check if we are in the same trace file at this point. */
1195 if (rstream->tracefile_count_current == vstream->tracefile_count_current) {
1196 if (rstream->beacon_ts_end != -1ULL &&
1197 vstream->last_sent_index == rstream->total_index_received) {
1198 /*
1199 * We've received a synchronization beacon and the last index
1200 * available has been sent, the index for now is inactive.
1201 */
1202 index->status = htobe32(LTTNG_VIEWER_INDEX_INACTIVE);
1203 index->timestamp_end = htobe64(rstream->beacon_ts_end);
1204 goto index_ready;
1205 } else if (rstream->total_index_received <= vstream->last_sent_index
1206 && !vstream->close_write_flag) {
1207 /*
1208 * Reader and writer are working in the same tracefile, so we care
1209 * about the number of index received and sent. Otherwise, we read
1210 * up to EOF.
1211 */
1212 index->status = htobe32(LTTNG_VIEWER_INDEX_RETRY);
1213 goto index_ready;
1214 }
1215 }
1216 /* Nothing to do with the index, continue with it. */
1217 ret = 0;
1218 } else if (rstream->close_flag && vstream->close_write_flag &&
1219 vstream->total_index_received == vstream->last_sent_index) {
1220 /* Last index sent and current tracefile closed in write */
1221 index->status = htobe32(LTTNG_VIEWER_INDEX_HUP);
1222 goto hup;
1223 } else {
1224 vstream->close_write_flag = 1;
1225 ret = 0;
1226 }
1227
1228 error:
1229 return ret;
1230
1231 hup:
1232 viewer_stream_delete(vstream);
1233 viewer_stream_destroy(trace, vstream);
1234 index_ready:
1235 return 1;
1236 }
1237
1238 /*
1239 * Send the next index for a stream.
1240 *
1241 * Return 0 on success or else a negative value.
1242 */
1243 static
1244 int viewer_get_next_index(struct relay_connection *conn)
1245 {
1246 int ret;
1247 ssize_t read_ret;
1248 struct lttng_viewer_get_next_index request_index;
1249 struct lttng_viewer_index viewer_index;
1250 struct ctf_packet_index packet_index;
1251 struct relay_viewer_stream *vstream;
1252 struct relay_stream *rstream;
1253 struct ctf_trace *ctf_trace;
1254 struct relay_session *session;
1255
1256 assert(conn);
1257
1258 DBG("Viewer get next index");
1259
1260 health_code_update();
1261
1262 ret = recv_request(conn->sock, &request_index, sizeof(request_index));
1263 if (ret < 0) {
1264 goto end;
1265 }
1266 health_code_update();
1267
1268 rcu_read_lock();
1269 vstream = viewer_stream_find_by_id(be64toh(request_index.stream_id));
1270 if (!vstream) {
1271 ret = -1;
1272 goto end_unlock;
1273 }
1274
1275 session = session_find_by_id(conn->sessions_ht, vstream->session_id);
1276 if (!session) {
1277 ret = -1;
1278 goto end_unlock;
1279 }
1280
1281 ctf_trace = ctf_trace_find_by_path(session->ctf_traces_ht, vstream->path_name);
1282 assert(ctf_trace);
1283
1284 memset(&viewer_index, 0, sizeof(viewer_index));
1285
1286 /*
1287 * The viewer should not ask for index on metadata stream.
1288 */
1289 if (vstream->metadata_flag) {
1290 viewer_index.status = htobe32(LTTNG_VIEWER_INDEX_HUP);
1291 goto send_reply;
1292 }
1293
1294 rstream = stream_find_by_id(relay_streams_ht, vstream->stream_handle);
1295 assert(rstream);
1296
1297 /* Try to open an index if one is needed for that stream. */
1298 ret = try_open_index(vstream, rstream);
1299 if (ret < 0) {
1300 if (ret == -ENOENT) {
1301 /*
1302 * The index is created only when the first data packet arrives, it
1303 * might not be ready at the beginning of the session
1304 */
1305 viewer_index.status = htobe32(LTTNG_VIEWER_INDEX_RETRY);
1306 } else {
1307 /* Unhandled error. */
1308 viewer_index.status = htobe32(LTTNG_VIEWER_INDEX_ERR);
1309 }
1310 goto send_reply;
1311 }
1312
1313 pthread_mutex_lock(&rstream->viewer_stream_rotation_lock);
1314 ret = check_index_status(vstream, rstream, ctf_trace, &viewer_index);
1315 pthread_mutex_unlock(&rstream->viewer_stream_rotation_lock);
1316 if (ret < 0) {
1317 goto end;
1318 } else if (ret == 1) {
1319 /*
1320 * This means the viewer index data structure has been populated by the
1321 * check call thus we now send back the reply to the client.
1322 */
1323 goto send_reply;
1324 }
1325 /* At this point, ret MUST be 0 thus we continue with the get. */
1326 assert(!ret);
1327
1328 if (!ctf_trace->metadata_received ||
1329 ctf_trace->metadata_received > ctf_trace->metadata_sent) {
1330 viewer_index.flags |= LTTNG_VIEWER_FLAG_NEW_METADATA;
1331 }
1332
1333 ret = check_new_streams(conn);
1334 if (ret < 0) {
1335 goto end_unlock;
1336 } else if (ret == 1) {
1337 viewer_index.flags |= LTTNG_VIEWER_FLAG_NEW_STREAM;
1338 }
1339
1340 pthread_mutex_lock(&rstream->viewer_stream_rotation_lock);
1341 pthread_mutex_lock(&vstream->overwrite_lock);
1342 if (vstream->abort_flag) {
1343 /* The file is being overwritten by the writer, we cannot use it. */
1344 pthread_mutex_unlock(&vstream->overwrite_lock);
1345 ret = viewer_stream_rotate(vstream, rstream);
1346 pthread_mutex_unlock(&rstream->viewer_stream_rotation_lock);
1347 if (ret < 0) {
1348 goto end_unlock;
1349 } else if (ret == 1) {
1350 viewer_index.status = htobe32(LTTNG_VIEWER_INDEX_HUP);
1351 viewer_stream_delete(vstream);
1352 viewer_stream_destroy(ctf_trace, vstream);
1353 } else {
1354 viewer_index.status = htobe32(LTTNG_VIEWER_INDEX_RETRY);
1355 }
1356 goto send_reply;
1357 }
1358
1359 read_ret = lttng_read(vstream->index_read_fd, &packet_index,
1360 sizeof(packet_index));
1361 pthread_mutex_unlock(&vstream->overwrite_lock);
1362 pthread_mutex_unlock(&rstream->viewer_stream_rotation_lock);
1363 if (read_ret < 0) {
1364 viewer_index.status = htobe32(LTTNG_VIEWER_INDEX_HUP);
1365 viewer_stream_delete(vstream);
1366 viewer_stream_destroy(ctf_trace, vstream);
1367 goto send_reply;
1368 } else if (read_ret < sizeof(packet_index)) {
1369 pthread_mutex_lock(&rstream->viewer_stream_rotation_lock);
1370 if (vstream->close_write_flag) {
1371 ret = viewer_stream_rotate(vstream, rstream);
1372 if (ret < 0) {
1373 pthread_mutex_unlock(&rstream->viewer_stream_rotation_lock);
1374 goto end_unlock;
1375 } else if (ret == 1) {
1376 viewer_index.status = htobe32(LTTNG_VIEWER_INDEX_HUP);
1377 viewer_stream_delete(vstream);
1378 viewer_stream_destroy(ctf_trace, vstream);
1379 } else {
1380 viewer_index.status = htobe32(LTTNG_VIEWER_INDEX_RETRY);
1381 }
1382 } else {
1383 ERR("Relay reading index file %d", vstream->index_read_fd);
1384 viewer_index.status = htobe32(LTTNG_VIEWER_INDEX_ERR);
1385 }
1386 pthread_mutex_unlock(&rstream->viewer_stream_rotation_lock);
1387 goto send_reply;
1388 } else {
1389 viewer_index.status = htobe32(LTTNG_VIEWER_INDEX_OK);
1390 vstream->last_sent_index++;
1391 }
1392
1393 /*
1394 * Indexes are stored in big endian, no need to switch before sending.
1395 */
1396 viewer_index.offset = packet_index.offset;
1397 viewer_index.packet_size = packet_index.packet_size;
1398 viewer_index.content_size = packet_index.content_size;
1399 viewer_index.timestamp_begin = packet_index.timestamp_begin;
1400 viewer_index.timestamp_end = packet_index.timestamp_end;
1401 viewer_index.events_discarded = packet_index.events_discarded;
1402 viewer_index.stream_id = packet_index.stream_id;
1403
1404 send_reply:
1405 viewer_index.flags = htobe32(viewer_index.flags);
1406 health_code_update();
1407
1408 ret = send_response(conn->sock, &viewer_index, sizeof(viewer_index));
1409 if (ret < 0) {
1410 goto end_unlock;
1411 }
1412 health_code_update();
1413
1414 DBG("Index %" PRIu64 " for stream %" PRIu64 " sent",
1415 vstream->last_sent_index, vstream->stream_handle);
1416
1417 end_unlock:
1418 rcu_read_unlock();
1419
1420 end:
1421 return ret;
1422 }
1423
1424 /*
1425 * Send the next index for a stream
1426 *
1427 * Return 0 on success or else a negative value.
1428 */
1429 static
1430 int viewer_get_packet(struct relay_connection *conn)
1431 {
1432 int ret, send_data = 0;
1433 char *data = NULL;
1434 uint32_t len = 0;
1435 ssize_t read_len;
1436 struct lttng_viewer_get_packet get_packet_info;
1437 struct lttng_viewer_trace_packet reply;
1438 struct relay_viewer_stream *stream;
1439 struct relay_session *session;
1440 struct ctf_trace *ctf_trace;
1441
1442 assert(conn);
1443
1444 DBG2("Relay get data packet");
1445
1446 health_code_update();
1447
1448 ret = recv_request(conn->sock, &get_packet_info, sizeof(get_packet_info));
1449 if (ret < 0) {
1450 goto end;
1451 }
1452 health_code_update();
1453
1454 /* From this point on, the error label can be reached. */
1455 memset(&reply, 0, sizeof(reply));
1456
1457 rcu_read_lock();
1458 stream = viewer_stream_find_by_id(be64toh(get_packet_info.stream_id));
1459 if (!stream) {
1460 goto error;
1461 }
1462
1463 session = session_find_by_id(conn->sessions_ht, stream->session_id);
1464 if (!session) {
1465 ret = -1;
1466 goto error;
1467 }
1468
1469 ctf_trace = ctf_trace_find_by_path(session->ctf_traces_ht,
1470 stream->path_name);
1471 assert(ctf_trace);
1472
1473 /*
1474 * First time we read this stream, we need open the tracefile, we should
1475 * only arrive here if an index has already been sent to the viewer, so the
1476 * tracefile must exist, if it does not it is a fatal error.
1477 */
1478 if (stream->read_fd < 0) {
1479 char fullpath[PATH_MAX];
1480
1481 if (stream->tracefile_count > 0) {
1482 ret = snprintf(fullpath, PATH_MAX, "%s/%s_%" PRIu64, stream->path_name,
1483 stream->channel_name,
1484 stream->tracefile_count_current);
1485 } else {
1486 ret = snprintf(fullpath, PATH_MAX, "%s/%s", stream->path_name,
1487 stream->channel_name);
1488 }
1489 if (ret < 0) {
1490 goto error;
1491 }
1492 ret = open(fullpath, O_RDONLY);
1493 if (ret < 0) {
1494 PERROR("Relay opening trace file");
1495 goto error;
1496 }
1497 stream->read_fd = ret;
1498 }
1499
1500 if (!ctf_trace->metadata_received ||
1501 ctf_trace->metadata_received > ctf_trace->metadata_sent) {
1502 reply.status = htobe32(LTTNG_VIEWER_GET_PACKET_ERR);
1503 reply.flags |= LTTNG_VIEWER_FLAG_NEW_METADATA;
1504 goto send_reply;
1505 }
1506
1507 ret = check_new_streams(conn);
1508 if (ret < 0) {
1509 goto end_unlock;
1510 } else if (ret == 1) {
1511 reply.status = htobe32(LTTNG_VIEWER_GET_PACKET_ERR);
1512 reply.flags |= LTTNG_VIEWER_FLAG_NEW_STREAM;
1513 goto send_reply;
1514 }
1515
1516 len = be32toh(get_packet_info.len);
1517 data = zmalloc(len);
1518 if (!data) {
1519 PERROR("relay data zmalloc");
1520 goto error;
1521 }
1522
1523 ret = lseek(stream->read_fd, be64toh(get_packet_info.offset), SEEK_SET);
1524 if (ret < 0) {
1525 /*
1526 * If the read fd was closed by the streaming side, the
1527 * abort_flag will be set to 1, otherwise it is an error.
1528 */
1529 if (stream->abort_flag == 0) {
1530 PERROR("lseek");
1531 goto error;
1532 }
1533 reply.status = htobe32(LTTNG_VIEWER_GET_PACKET_EOF);
1534 goto send_reply;
1535 }
1536 read_len = lttng_read(stream->read_fd, data, len);
1537 if (read_len < len) {
1538 /*
1539 * If the read fd was closed by the streaming side, the
1540 * abort_flag will be set to 1, otherwise it is an error.
1541 */
1542 if (stream->abort_flag == 0) {
1543 PERROR("Relay reading trace file, fd: %d, offset: %" PRIu64,
1544 stream->read_fd,
1545 be64toh(get_packet_info.offset));
1546 goto error;
1547 } else {
1548 reply.status = htobe32(LTTNG_VIEWER_GET_PACKET_EOF);
1549 goto send_reply;
1550 }
1551 }
1552 reply.status = htobe32(LTTNG_VIEWER_GET_PACKET_OK);
1553 reply.len = htobe32(len);
1554 send_data = 1;
1555 goto send_reply;
1556
1557 error:
1558 reply.status = htobe32(LTTNG_VIEWER_GET_PACKET_ERR);
1559
1560 send_reply:
1561 reply.flags = htobe32(reply.flags);
1562
1563 health_code_update();
1564
1565 ret = send_response(conn->sock, &reply, sizeof(reply));
1566 if (ret < 0) {
1567 goto end_unlock;
1568 }
1569 health_code_update();
1570
1571 if (send_data) {
1572 health_code_update();
1573 ret = send_response(conn->sock, data, len);
1574 if (ret < 0) {
1575 goto end_unlock;
1576 }
1577 health_code_update();
1578 }
1579
1580 DBG("Sent %u bytes for stream %" PRIu64, len,
1581 be64toh(get_packet_info.stream_id));
1582
1583 end_unlock:
1584 free(data);
1585 rcu_read_unlock();
1586
1587 end:
1588 return ret;
1589 }
1590
1591 /*
1592 * Send the session's metadata
1593 *
1594 * Return 0 on success else a negative value.
1595 */
1596 static
1597 int viewer_get_metadata(struct relay_connection *conn)
1598 {
1599 int ret = 0;
1600 ssize_t read_len;
1601 uint64_t len = 0;
1602 char *data = NULL;
1603 struct lttng_viewer_get_metadata request;
1604 struct lttng_viewer_metadata_packet reply;
1605 struct relay_viewer_stream *stream;
1606 struct ctf_trace *ctf_trace;
1607 struct relay_session *session;
1608
1609 assert(conn);
1610
1611 DBG("Relay get metadata");
1612
1613 health_code_update();
1614
1615 ret = recv_request(conn->sock, &request, sizeof(request));
1616 if (ret < 0) {
1617 goto end;
1618 }
1619 health_code_update();
1620
1621 rcu_read_lock();
1622 stream = viewer_stream_find_by_id(be64toh(request.stream_id));
1623 if (!stream || !stream->metadata_flag) {
1624 ERR("Invalid metadata stream");
1625 goto error;
1626 }
1627
1628 session = session_find_by_id(conn->sessions_ht, stream->session_id);
1629 if (!session) {
1630 ret = -1;
1631 goto error;
1632 }
1633
1634 ctf_trace = ctf_trace_find_by_path(session->ctf_traces_ht,
1635 stream->path_name);
1636 assert(ctf_trace);
1637 assert(ctf_trace->metadata_sent <= ctf_trace->metadata_received);
1638
1639 len = ctf_trace->metadata_received - ctf_trace->metadata_sent;
1640 if (len == 0) {
1641 reply.status = htobe32(LTTNG_VIEWER_NO_NEW_METADATA);
1642 goto send_reply;
1643 }
1644
1645 /* first time, we open the metadata file */
1646 if (stream->read_fd < 0) {
1647 char fullpath[PATH_MAX];
1648
1649 ret = snprintf(fullpath, PATH_MAX, "%s/%s", stream->path_name,
1650 stream->channel_name);
1651 if (ret < 0) {
1652 goto error;
1653 }
1654 ret = open(fullpath, O_RDONLY);
1655 if (ret < 0) {
1656 PERROR("Relay opening metadata file");
1657 goto error;
1658 }
1659 stream->read_fd = ret;
1660 }
1661
1662 reply.len = htobe64(len);
1663 data = zmalloc(len);
1664 if (!data) {
1665 PERROR("viewer metadata zmalloc");
1666 goto error;
1667 }
1668
1669 read_len = lttng_read(stream->read_fd, data, len);
1670 if (read_len < len) {
1671 PERROR("Relay reading metadata file");
1672 goto error;
1673 }
1674 ctf_trace->metadata_sent += read_len;
1675 reply.status = htobe32(LTTNG_VIEWER_METADATA_OK);
1676 goto send_reply;
1677
1678 error:
1679 reply.status = htobe32(LTTNG_VIEWER_METADATA_ERR);
1680
1681 send_reply:
1682 health_code_update();
1683 ret = send_response(conn->sock, &reply, sizeof(reply));
1684 if (ret < 0) {
1685 goto end_unlock;
1686 }
1687 health_code_update();
1688
1689 if (len > 0) {
1690 ret = send_response(conn->sock, data, len);
1691 if (ret < 0) {
1692 goto end_unlock;
1693 }
1694 }
1695
1696 DBG("Sent %" PRIu64 " bytes of metadata for stream %" PRIu64, len,
1697 be64toh(request.stream_id));
1698
1699 DBG("Metadata sent");
1700
1701 end_unlock:
1702 free(data);
1703 rcu_read_unlock();
1704 end:
1705 return ret;
1706 }
1707
1708 /*
1709 * Create a viewer session.
1710 *
1711 * Return 0 on success or else a negative value.
1712 */
1713 static
1714 int viewer_create_session(struct relay_connection *conn)
1715 {
1716 int ret;
1717 struct lttng_viewer_create_session_response resp;
1718
1719 DBG("Viewer create session received");
1720
1721 resp.status = htobe32(LTTNG_VIEWER_CREATE_SESSION_OK);
1722 conn->viewer_session = zmalloc(sizeof(*conn->viewer_session));
1723 if (!conn->viewer_session) {
1724 ERR("Allocation viewer session");
1725 resp.status = htobe32(LTTNG_VIEWER_CREATE_SESSION_ERR);
1726 goto send_reply;
1727 }
1728 CDS_INIT_LIST_HEAD(&conn->viewer_session->sessions_head);
1729
1730 send_reply:
1731 health_code_update();
1732 ret = send_response(conn->sock, &resp, sizeof(resp));
1733 if (ret < 0) {
1734 goto end;
1735 }
1736 health_code_update();
1737 ret = 0;
1738
1739 end:
1740 return ret;
1741 }
1742
1743
1744 /*
1745 * live_relay_unknown_command: send -1 if received unknown command
1746 */
1747 static
1748 void live_relay_unknown_command(struct relay_connection *conn)
1749 {
1750 struct lttcomm_relayd_generic_reply reply;
1751
1752 reply.ret_code = htobe32(LTTNG_ERR_UNK);
1753 (void) send_response(conn->sock, &reply, sizeof(reply));
1754 }
1755
1756 /*
1757 * Process the commands received on the control socket
1758 */
1759 static
1760 int process_control(struct lttng_viewer_cmd *recv_hdr,
1761 struct relay_connection *conn)
1762 {
1763 int ret = 0;
1764 uint32_t msg_value;
1765
1766 assert(recv_hdr);
1767 assert(conn);
1768
1769 msg_value = be32toh(recv_hdr->cmd);
1770
1771 /*
1772 * Make sure we've done the version check before any command other then a
1773 * new client connection.
1774 */
1775 if (msg_value != LTTNG_VIEWER_CONNECT && !conn->version_check_done) {
1776 ERR("Viewer conn value %" PRIu32 " before version check", msg_value);
1777 ret = -1;
1778 goto end;
1779 }
1780
1781 switch (msg_value) {
1782 case LTTNG_VIEWER_CONNECT:
1783 ret = viewer_connect(conn);
1784 break;
1785 case LTTNG_VIEWER_LIST_SESSIONS:
1786 ret = viewer_list_sessions(conn);
1787 break;
1788 case LTTNG_VIEWER_ATTACH_SESSION:
1789 ret = viewer_attach_session(conn);
1790 break;
1791 case LTTNG_VIEWER_GET_NEXT_INDEX:
1792 ret = viewer_get_next_index(conn);
1793 break;
1794 case LTTNG_VIEWER_GET_PACKET:
1795 ret = viewer_get_packet(conn);
1796 break;
1797 case LTTNG_VIEWER_GET_METADATA:
1798 ret = viewer_get_metadata(conn);
1799 break;
1800 case LTTNG_VIEWER_GET_NEW_STREAMS:
1801 ret = viewer_get_new_streams(conn);
1802 break;
1803 case LTTNG_VIEWER_CREATE_SESSION:
1804 ret = viewer_create_session(conn);
1805 break;
1806 default:
1807 ERR("Received unknown viewer command (%u)", be32toh(recv_hdr->cmd));
1808 live_relay_unknown_command(conn);
1809 ret = -1;
1810 goto end;
1811 }
1812
1813 end:
1814 return ret;
1815 }
1816
1817 static
1818 void cleanup_connection_pollfd(struct lttng_poll_event *events, int pollfd)
1819 {
1820 int ret;
1821
1822 assert(events);
1823
1824 (void) lttng_poll_del(events, pollfd);
1825
1826 ret = close(pollfd);
1827 if (ret < 0) {
1828 ERR("Closing pollfd %d", pollfd);
1829 }
1830 }
1831
1832 /*
1833 * Delete and destroy a connection.
1834 *
1835 * RCU read side lock MUST be acquired.
1836 */
1837 static void destroy_connection(struct lttng_ht *relay_connections_ht,
1838 struct relay_connection *conn)
1839 {
1840 struct relay_session *session, *tmp_session;
1841
1842 assert(relay_connections_ht);
1843 assert(conn);
1844
1845 connection_delete(relay_connections_ht, conn);
1846
1847 if (!conn->viewer_session) {
1848 goto end;
1849 }
1850
1851 rcu_read_lock();
1852 cds_list_for_each_entry_safe(session, tmp_session,
1853 &conn->viewer_session->sessions_head,
1854 viewer_session_list) {
1855 DBG("Cleaning connection of session ID %" PRIu64, session->id);
1856 cleanup_session(conn, session);
1857 }
1858 rcu_read_unlock();
1859
1860 end:
1861 connection_destroy(conn);
1862 }
1863
1864 /*
1865 * This thread does the actual work
1866 */
1867 static
1868 void *thread_worker(void *data)
1869 {
1870 int ret, err = -1;
1871 uint32_t nb_fd;
1872 struct relay_connection *conn;
1873 struct lttng_poll_event events;
1874 struct lttng_ht *relay_connections_ht;
1875 struct lttng_ht_iter iter;
1876 struct lttng_viewer_cmd recv_hdr;
1877 struct relay_local_data *relay_ctx = (struct relay_local_data *) data;
1878 struct lttng_ht *sessions_ht = relay_ctx->sessions_ht;
1879
1880 DBG("[thread] Live viewer relay worker started");
1881
1882 rcu_register_thread();
1883
1884 health_register(health_relayd, HEALTH_RELAYD_TYPE_LIVE_WORKER);
1885
1886 if (testpoint(relayd_thread_live_worker)) {
1887 goto error_testpoint;
1888 }
1889
1890 /* table of connections indexed on socket */
1891 relay_connections_ht = lttng_ht_new(0, LTTNG_HT_TYPE_ULONG);
1892 if (!relay_connections_ht) {
1893 goto relay_connections_ht_error;
1894 }
1895
1896 ret = create_thread_poll_set(&events, 2);
1897 if (ret < 0) {
1898 goto error_poll_create;
1899 }
1900
1901 ret = lttng_poll_add(&events, live_conn_pipe[0], LPOLLIN | LPOLLRDHUP);
1902 if (ret < 0) {
1903 goto error;
1904 }
1905
1906 restart:
1907 while (1) {
1908 int i;
1909
1910 health_code_update();
1911
1912 /* Infinite blocking call, waiting for transmission */
1913 DBG3("Relayd live viewer worker thread polling...");
1914 health_poll_entry();
1915 ret = lttng_poll_wait(&events, -1);
1916 health_poll_exit();
1917 if (ret < 0) {
1918 /*
1919 * Restart interrupted system call.
1920 */
1921 if (errno == EINTR) {
1922 goto restart;
1923 }
1924 goto error;
1925 }
1926
1927 nb_fd = ret;
1928
1929 /*
1930 * Process control. The control connection is prioritised so we don't
1931 * starve it with high throughput tracing data on the data
1932 * connection.
1933 */
1934 for (i = 0; i < nb_fd; i++) {
1935 /* Fetch once the poll data */
1936 uint32_t revents = LTTNG_POLL_GETEV(&events, i);
1937 int pollfd = LTTNG_POLL_GETFD(&events, i);
1938
1939 health_code_update();
1940
1941 /* Thread quit pipe has been closed. Killing thread. */
1942 ret = check_thread_quit_pipe(pollfd, revents);
1943 if (ret) {
1944 err = 0;
1945 goto exit;
1946 }
1947
1948 /* Inspect the relay conn pipe for new connection */
1949 if (pollfd == live_conn_pipe[0]) {
1950 if (revents & (LPOLLERR | LPOLLHUP | LPOLLRDHUP)) {
1951 ERR("Relay live pipe error");
1952 goto error;
1953 } else if (revents & LPOLLIN) {
1954 ret = lttng_read(live_conn_pipe[0], &conn, sizeof(conn));
1955 if (ret < 0) {
1956 goto error;
1957 }
1958 conn->sessions_ht = sessions_ht;
1959 connection_init(conn);
1960 lttng_poll_add(&events, conn->sock->fd,
1961 LPOLLIN | LPOLLRDHUP);
1962 rcu_read_lock();
1963 lttng_ht_add_unique_ulong(relay_connections_ht,
1964 &conn->sock_n);
1965 rcu_read_unlock();
1966 DBG("Connection socket %d added", conn->sock->fd);
1967 }
1968 } else {
1969 rcu_read_lock();
1970 conn = connection_find_by_sock(relay_connections_ht, pollfd);
1971 /* If not found, there is a synchronization issue. */
1972 assert(conn);
1973
1974 if (revents & (LPOLLERR | LPOLLHUP | LPOLLRDHUP)) {
1975 cleanup_connection_pollfd(&events, pollfd);
1976 destroy_connection(relay_connections_ht, conn);
1977 } else if (revents & LPOLLIN) {
1978 ret = conn->sock->ops->recvmsg(conn->sock, &recv_hdr,
1979 sizeof(recv_hdr), 0);
1980 if (ret <= 0) {
1981 /* Connection closed */
1982 cleanup_connection_pollfd(&events, pollfd);
1983 destroy_connection(relay_connections_ht, conn);
1984 DBG("Viewer control conn closed with %d", pollfd);
1985 } else {
1986 ret = process_control(&recv_hdr, conn);
1987 if (ret < 0) {
1988 /* Clear the session on error. */
1989 cleanup_connection_pollfd(&events, pollfd);
1990 destroy_connection(relay_connections_ht, conn);
1991 DBG("Viewer connection closed with %d", pollfd);
1992 }
1993 }
1994 }
1995 rcu_read_unlock();
1996 }
1997 }
1998 }
1999
2000 exit:
2001 error:
2002 lttng_poll_clean(&events);
2003
2004 /* Cleanup reamaining connection object. */
2005 rcu_read_lock();
2006 cds_lfht_for_each_entry(relay_connections_ht->ht, &iter.iter, conn,
2007 sock_n.node) {
2008 health_code_update();
2009 destroy_connection(relay_connections_ht, conn);
2010 }
2011 rcu_read_unlock();
2012 error_poll_create:
2013 lttng_ht_destroy(relay_connections_ht);
2014 relay_connections_ht_error:
2015 /* Close relay conn pipes */
2016 utils_close_pipe(live_conn_pipe);
2017 if (err) {
2018 DBG("Viewer worker thread exited with error");
2019 }
2020 DBG("Viewer worker thread cleanup complete");
2021 error_testpoint:
2022 if (err) {
2023 health_error();
2024 ERR("Health error occurred in %s", __func__);
2025 }
2026 health_unregister(health_relayd);
2027 stop_threads();
2028 rcu_unregister_thread();
2029 return NULL;
2030 }
2031
2032 /*
2033 * Create the relay command pipe to wake thread_manage_apps.
2034 * Closed in cleanup().
2035 */
2036 static int create_conn_pipe(void)
2037 {
2038 int ret;
2039
2040 ret = utils_create_pipe_cloexec(live_conn_pipe);
2041
2042 return ret;
2043 }
2044
2045 void live_stop_threads(void)
2046 {
2047 int ret;
2048 void *status;
2049
2050 stop_threads();
2051
2052 ret = pthread_join(live_listener_thread, &status);
2053 if (ret != 0) {
2054 PERROR("pthread_join live listener");
2055 goto error; /* join error, exit without cleanup */
2056 }
2057
2058 ret = pthread_join(live_worker_thread, &status);
2059 if (ret != 0) {
2060 PERROR("pthread_join live worker");
2061 goto error; /* join error, exit without cleanup */
2062 }
2063
2064 ret = pthread_join(live_dispatcher_thread, &status);
2065 if (ret != 0) {
2066 PERROR("pthread_join live dispatcher");
2067 goto error; /* join error, exit without cleanup */
2068 }
2069
2070 cleanup();
2071
2072 error:
2073 return;
2074 }
2075
2076 /*
2077 * main
2078 */
2079 int live_start_threads(struct lttng_uri *uri,
2080 struct relay_local_data *relay_ctx)
2081 {
2082 int ret = 0;
2083 void *status;
2084 int is_root;
2085
2086 assert(uri);
2087 live_uri = uri;
2088
2089 /* Check if daemon is UID = 0 */
2090 is_root = !getuid();
2091
2092 if (!is_root) {
2093 if (live_uri->port < 1024) {
2094 ERR("Need to be root to use ports < 1024");
2095 ret = -1;
2096 goto exit;
2097 }
2098 }
2099
2100 /* Setup the thread apps communication pipe. */
2101 if ((ret = create_conn_pipe()) < 0) {
2102 goto exit;
2103 }
2104
2105 /* Init relay command queue. */
2106 cds_wfq_init(&viewer_conn_queue.queue);
2107
2108 /* Set up max poll set size */
2109 lttng_poll_set_max_size();
2110
2111 /* Setup the dispatcher thread */
2112 ret = pthread_create(&live_dispatcher_thread, NULL,
2113 thread_dispatcher, (void *) NULL);
2114 if (ret != 0) {
2115 PERROR("pthread_create viewer dispatcher");
2116 goto exit_dispatcher;
2117 }
2118
2119 /* Setup the worker thread */
2120 ret = pthread_create(&live_worker_thread, NULL,
2121 thread_worker, relay_ctx);
2122 if (ret != 0) {
2123 PERROR("pthread_create viewer worker");
2124 goto exit_worker;
2125 }
2126
2127 /* Setup the listener thread */
2128 ret = pthread_create(&live_listener_thread, NULL,
2129 thread_listener, (void *) NULL);
2130 if (ret != 0) {
2131 PERROR("pthread_create viewer listener");
2132 goto exit_listener;
2133 }
2134
2135 ret = 0;
2136 goto end;
2137
2138 exit_listener:
2139 ret = pthread_join(live_listener_thread, &status);
2140 if (ret != 0) {
2141 PERROR("pthread_join live listener");
2142 goto error; /* join error, exit without cleanup */
2143 }
2144
2145 exit_worker:
2146 ret = pthread_join(live_worker_thread, &status);
2147 if (ret != 0) {
2148 PERROR("pthread_join live worker");
2149 goto error; /* join error, exit without cleanup */
2150 }
2151
2152 exit_dispatcher:
2153 ret = pthread_join(live_dispatcher_thread, &status);
2154 if (ret != 0) {
2155 PERROR("pthread_join live dispatcher");
2156 goto error; /* join error, exit without cleanup */
2157 }
2158
2159 exit:
2160 cleanup();
2161
2162 end:
2163 error:
2164 return ret;
2165 }
This page took 0.110135 seconds and 5 git commands to generate.