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