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