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