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