Implement the relayd live features
[lttng-tools.git] / src / bin / lttng-relayd / live.c
CommitLineData
d3e2ba59
JD
1/*
2 * Copyright (C) 2013 - Julien Desfossez <jdesfossez@efficios.com>
3 * David Goulet <dgoulet@efficios.com>
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License, version 2 only,
7 * as published by the Free Software Foundation.
8 *
9 * This program is distributed in the hope that it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
12 * more details.
13 *
14 * You should have received a copy of the GNU General Public License along
15 * with this program; if not, write to the Free Software Foundation, Inc.,
16 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
17 */
18
19#define _GNU_SOURCE
20#include <getopt.h>
21#include <grp.h>
22#include <limits.h>
23#include <pthread.h>
24#include <signal.h>
25#include <stdio.h>
26#include <stdlib.h>
27#include <string.h>
28#include <sys/mman.h>
29#include <sys/mount.h>
30#include <sys/resource.h>
31#include <sys/socket.h>
32#include <sys/stat.h>
33#include <sys/types.h>
34#include <sys/wait.h>
35#include <inttypes.h>
36#include <urcu/futex.h>
37#include <urcu/uatomic.h>
38#include <unistd.h>
39#include <fcntl.h>
40#include <config.h>
41
42#include <lttng/lttng.h>
43#include <common/common.h>
44#include <common/compat/poll.h>
45#include <common/compat/socket.h>
46#include <common/defaults.h>
47#include <common/futex.h>
48#include <common/sessiond-comm/sessiond-comm.h>
49#include <common/sessiond-comm/inet.h>
50#include <common/sessiond-comm/relayd.h>
51#include <common/uri.h>
52#include <common/utils.h>
53
54#include "cmd.h"
55#include "live.h"
56#include "lttng-relayd.h"
57#include "lttng-viewer.h"
58#include "utils.h"
59
60static struct lttng_uri *live_uri;
61
62/*
63 * Quit pipe for all threads. This permits a single cancellation point
64 * for all threads when receiving an event on the pipe.
65 */
66static int live_thread_quit_pipe[2] = { -1, -1 };
67
68/*
69 * This pipe is used to inform the worker thread that a command is queued and
70 * ready to be processed.
71 */
72static int live_relay_cmd_pipe[2] = { -1, -1 };
73
74/* Shared between threads */
75static int live_dispatch_thread_exit;
76
77static pthread_t live_listener_thread;
78static pthread_t live_dispatcher_thread;
79static pthread_t live_worker_thread;
80
81/*
82 * Relay command queue.
83 *
84 * The live_thread_listener and live_thread_dispatcher communicate with this
85 * queue.
86 */
87static struct relay_cmd_queue viewer_cmd_queue;
88
89static uint64_t last_relay_viewer_session_id;
90
91/*
92 * Cleanup the daemon
93 */
94static
95void cleanup(void)
96{
97 DBG("Cleaning up");
98
99 /* Close thread quit pipes */
100 utils_close_pipe(live_thread_quit_pipe);
101 free(live_uri);
102}
103
104/*
105 * Write to writable pipe used to notify a thread.
106 */
107static
108int notify_thread_pipe(int wpipe)
109{
110 int ret;
111
112 do {
113 ret = write(wpipe, "!", 1);
114 } while (ret < 0 && errno == EINTR);
115 if (ret < 0 || ret != 1) {
116 PERROR("write poll pipe");
117 }
118
119 return ret;
120}
121
122/*
123 * Stop all threads by closing the thread quit pipe.
124 */
125static
126void stop_threads(void)
127{
128 int ret;
129
130 /* Stopping all threads */
131 DBG("Terminating all live threads");
132 ret = notify_thread_pipe(live_thread_quit_pipe[1]);
133 if (ret < 0) {
134 ERR("write error on thread quit pipe");
135 }
136
137 /* Dispatch thread */
138 CMM_STORE_SHARED(live_dispatch_thread_exit, 1);
139 futex_nto1_wake(&viewer_cmd_queue.futex);
140}
141
142/*
143 * Init thread quit pipe.
144 *
145 * Return -1 on error or 0 if all pipes are created.
146 */
147static
148int init_thread_quit_pipe(void)
149{
150 int ret;
151
152 ret = utils_create_pipe_cloexec(live_thread_quit_pipe);
153
154 return ret;
155}
156
157/*
158 * Create a poll set with O_CLOEXEC and add the thread quit pipe to the set.
159 */
160static
161int create_thread_poll_set(struct lttng_poll_event *events, int size)
162{
163 int ret;
164
165 if (events == NULL || size == 0) {
166 ret = -1;
167 goto error;
168 }
169
170 ret = lttng_poll_create(events, size, LTTNG_CLOEXEC);
171 if (ret < 0) {
172 goto error;
173 }
174
175 /* Add quit pipe */
176 ret = lttng_poll_add(events, live_thread_quit_pipe[0], LPOLLIN);
177 if (ret < 0) {
178 goto error;
179 }
180
181 return 0;
182
183error:
184 return ret;
185}
186
187/*
188 * Check if the thread quit pipe was triggered.
189 *
190 * Return 1 if it was triggered else 0;
191 */
192static
193int check_thread_quit_pipe(int fd, uint32_t events)
194{
195 if (fd == live_thread_quit_pipe[0] && (events & LPOLLIN)) {
196 return 1;
197 }
198
199 return 0;
200}
201
202/*
203 * Create and init socket from uri.
204 */
205static
206struct lttcomm_sock *init_socket(struct lttng_uri *uri)
207{
208 int ret;
209 struct lttcomm_sock *sock = NULL;
210
211 sock = lttcomm_alloc_sock_from_uri(uri);
212 if (sock == NULL) {
213 ERR("Allocating socket");
214 goto error;
215 }
216
217 ret = lttcomm_create_sock(sock);
218 if (ret < 0) {
219 goto error;
220 }
221 DBG("Listening on sock %d for live", sock->fd);
222
223 ret = sock->ops->bind(sock);
224 if (ret < 0) {
225 goto error;
226 }
227
228 ret = sock->ops->listen(sock, -1);
229 if (ret < 0) {
230 goto error;
231
232 }
233
234 return sock;
235
236error:
237 if (sock) {
238 lttcomm_destroy_sock(sock);
239 }
240 return NULL;
241}
242
243/*
244 * This thread manages the listening for new connections on the network
245 */
246static
247void *thread_listener(void *data)
248{
249 int i, ret, pollfd, err = -1;
250 int val = 1;
251 uint32_t revents, nb_fd;
252 struct lttng_poll_event events;
253 struct lttcomm_sock *live_control_sock;
254
255 DBG("[thread] Relay live listener started");
256
257 live_control_sock = init_socket(live_uri);
258 if (!live_control_sock) {
259 goto error_sock_control;
260 }
261
262 /*
263 * Pass 3 as size here for the thread quit pipe, control and data socket.
264 */
265 ret = create_thread_poll_set(&events, 2);
266 if (ret < 0) {
267 goto error_create_poll;
268 }
269
270 /* Add the control socket */
271 ret = lttng_poll_add(&events, live_control_sock->fd, LPOLLIN | LPOLLRDHUP);
272 if (ret < 0) {
273 goto error_poll_add;
274 }
275
276 while (1) {
277 DBG("Listener accepting live viewers connections");
278
279restart:
280 ret = lttng_poll_wait(&events, -1);
281 if (ret < 0) {
282 /*
283 * Restart interrupted system call.
284 */
285 if (errno == EINTR) {
286 goto restart;
287 }
288 goto error;
289 }
290 nb_fd = ret;
291
292 DBG("Relay new viewer connection received");
293 for (i = 0; i < nb_fd; i++) {
294 /* Fetch once the poll data */
295 revents = LTTNG_POLL_GETEV(&events, i);
296 pollfd = LTTNG_POLL_GETFD(&events, i);
297
298 /* Thread quit pipe has been closed. Killing thread. */
299 ret = check_thread_quit_pipe(pollfd, revents);
300 if (ret) {
301 err = 0;
302 goto exit;
303 }
304
305 if (revents & (LPOLLERR | LPOLLHUP | LPOLLRDHUP)) {
306 ERR("socket poll error");
307 goto error;
308 } else if (revents & LPOLLIN) {
309 /*
310 * Get allocated in this thread, enqueued to a global queue,
311 * dequeued and freed in the worker thread.
312 */
313 struct relay_command *relay_cmd;
314 struct lttcomm_sock *newsock;
315
316 relay_cmd = zmalloc(sizeof(*relay_cmd));
317 if (!relay_cmd) {
318 PERROR("relay command zmalloc");
319 goto error;
320 }
321
322 assert(pollfd == live_control_sock->fd);
323 newsock = live_control_sock->ops->accept(live_control_sock);
324 if (!newsock) {
325 PERROR("accepting control sock");
326 free(relay_cmd);
327 goto error;
328 }
329 DBG("Relay viewer connection accepted socket %d", newsock->fd);
330 ret = setsockopt(newsock->fd, SOL_SOCKET, SO_REUSEADDR, &val,
331 sizeof(int));
332 if (ret < 0) {
333 PERROR("setsockopt inet");
334 lttcomm_destroy_sock(newsock);
335 free(relay_cmd);
336 goto error;
337 }
338 relay_cmd->sock = newsock;
339
340 /*
341 * Lock free enqueue the request.
342 */
343 cds_wfq_enqueue(&viewer_cmd_queue.queue, &relay_cmd->node);
344
345 /*
346 * Wake the dispatch queue futex. Implicit memory
347 * barrier with the exchange in cds_wfq_enqueue.
348 */
349 futex_nto1_wake(&viewer_cmd_queue.futex);
350 }
351 }
352 }
353
354exit:
355error:
356error_poll_add:
357 lttng_poll_clean(&events);
358error_create_poll:
359 if (live_control_sock->fd >= 0) {
360 ret = live_control_sock->ops->close(live_control_sock);
361 if (ret) {
362 PERROR("close");
363 }
364 }
365 lttcomm_destroy_sock(live_control_sock);
366error_sock_control:
367 if (err) {
368 DBG("Live viewer listener thread exited with error");
369 }
370 DBG("Live viewer listener thread cleanup complete");
371 stop_threads();
372 return NULL;
373}
374
375/*
376 * This thread manages the dispatching of the requests to worker threads
377 */
378static
379void *thread_dispatcher(void *data)
380{
381 int ret;
382 struct cds_wfq_node *node;
383 struct relay_command *relay_cmd = NULL;
384
385 DBG("[thread] Live viewer relay dispatcher started");
386
387 while (!CMM_LOAD_SHARED(live_dispatch_thread_exit)) {
388 /* Atomically prepare the queue futex */
389 futex_nto1_prepare(&viewer_cmd_queue.futex);
390
391 do {
392 /* Dequeue commands */
393 node = cds_wfq_dequeue_blocking(&viewer_cmd_queue.queue);
394 if (node == NULL) {
395 DBG("Woken up but nothing in the live-viewer "
396 "relay command queue");
397 /* Continue thread execution */
398 break;
399 }
400
401 relay_cmd = caa_container_of(node, struct relay_command, node);
402 DBG("Dispatching viewer request waiting on sock %d",
403 relay_cmd->sock->fd);
404
405 /*
406 * Inform worker thread of the new request. This call is blocking
407 * so we can be assured that the data will be read at some point in
408 * time or wait to the end of the world :)
409 */
410 do {
411 ret = write(live_relay_cmd_pipe[1], relay_cmd,
412 sizeof(*relay_cmd));
413 } while (ret < 0 && errno == EINTR);
414 free(relay_cmd);
415 if (ret < 0 || ret != sizeof(struct relay_command)) {
416 PERROR("write cmd pipe");
417 goto error;
418 }
419 } while (node != NULL);
420
421 /* Futex wait on queue. Blocking call on futex() */
422 futex_nto1_wait(&viewer_cmd_queue.futex);
423 }
424
425error:
426 DBG("Live viewer dispatch thread dying");
427 stop_threads();
428 return NULL;
429}
430
431/*
432 * Establish connection with the viewer and check the versions.
433 *
434 * Return 0 on success or else negative value.
435 */
436static
437int viewer_connect(struct relay_command *cmd)
438{
439 int ret;
440 struct lttng_viewer_connect reply, msg;
441
442 assert(cmd);
443
444 cmd->version_check_done = 1;
445
446 /* Get version from the other side. */
447 ret = cmd->sock->ops->recvmsg(cmd->sock, &msg, sizeof(msg), 0);
448 if (ret < 0 || ret != sizeof(msg)) {
449 if (ret == 0) {
450 /* Orderly shutdown. Not necessary to print an error. */
451 DBG("Socket %d did an orderly shutdown", cmd->sock->fd);
452 } else {
453 ERR("Relay failed to receive the version values.");
454 }
455 ret = -1;
456 goto end;
457 }
458
459 reply.major = RELAYD_VERSION_COMM_MAJOR;
460 reply.minor = RELAYD_VERSION_COMM_MINOR;
461
462 /* Major versions must be the same */
463 if (reply.major != be32toh(msg.major)) {
464 DBG("Incompatible major versions (%u vs %u)", reply.major,
465 be32toh(msg.major));
466 ret = 0;
467 goto end;
468 }
469
470 cmd->major = reply.major;
471 /* We adapt to the lowest compatible version */
472 if (reply.minor <= be32toh(msg.minor)) {
473 cmd->minor = reply.minor;
474 } else {
475 cmd->minor = be32toh(msg.minor);
476 }
477
478 if (be32toh(msg.type) == VIEWER_CLIENT_COMMAND) {
479 cmd->type = RELAY_VIEWER_COMMAND;
480 } else if (be32toh(msg.type) == VIEWER_CLIENT_NOTIFICATION) {
481 cmd->type = RELAY_VIEWER_NOTIFICATION;
482 } else {
483 ERR("Unknown connection type : %u", be32toh(msg.type));
484 ret = -1;
485 goto end;
486 }
487
488 reply.major = htobe32(reply.major);
489 reply.minor = htobe32(reply.minor);
490 if (cmd->type == RELAY_VIEWER_COMMAND) {
491 reply.viewer_session_id = htobe64(++last_relay_viewer_session_id);
492 }
493 ret = cmd->sock->ops->sendmsg(cmd->sock, &reply,
494 sizeof(struct lttng_viewer_connect), 0);
495 if (ret < 0) {
496 ERR("Relay sending version");
497 }
498
499 DBG("Version check done using protocol %u.%u", cmd->major, cmd->minor);
500 ret = 0;
501
502end:
503 return ret;
504}
505
506/*
507 * Send the viewer the list of current sessions.
508 *
509 * Return 0 on success or else a negative value.
510 */
511static
512int viewer_list_sessions(struct relay_command *cmd,
513 struct lttng_ht *sessions_ht)
514{
515 int ret;
516 struct lttng_viewer_list_sessions session_list;
517 unsigned long count;
518 long approx_before, approx_after;
519 struct lttng_ht_node_ulong *node;
520 struct lttng_ht_iter iter;
521 struct lttng_viewer_session send_session;
522 struct relay_session *session;
523
524 DBG("List sessions received");
525
526 if (cmd->version_check_done == 0) {
527 ERR("Trying to list sessions before version check");
528 ret = -1;
529 goto end_no_session;
530 }
531
532 rcu_read_lock();
533 cds_lfht_count_nodes(sessions_ht->ht, &approx_before, &count, &approx_after);
534 session_list.sessions_count = htobe32(count);
535
536 ret = cmd->sock->ops->sendmsg(cmd->sock, &session_list,
537 sizeof(session_list), 0);
538 if (ret < 0) {
539 ERR("Relay sending sessions list");
540 goto end_unlock;
541 }
542
543 cds_lfht_for_each_entry(sessions_ht->ht, &iter.iter, node, node) {
544 node = lttng_ht_iter_get_node_ulong(&iter);
545 if (!node) {
546 goto end_unlock;
547 }
548 session = caa_container_of(node, struct relay_session, session_n);
549
550 strncpy(send_session.session_name, session->session_name,
551 sizeof(send_session.session_name));
552 strncpy(send_session.hostname, session->hostname,
553 sizeof(send_session.hostname));
554 send_session.id = htobe64(session->id);
555 send_session.live_timer = htobe32(session->live_timer);
556 send_session.clients = htobe32(session->viewer_attached);
557
558 ret = cmd->sock->ops->sendmsg(cmd->sock, &send_session,
559 sizeof(send_session), 0);
560 if (ret < 0) {
561 ERR("Relay sending session info");
562 goto end_unlock;
563 }
564 }
565 rcu_read_unlock();
566 ret = 0;
567 goto end;
568
569end_unlock:
570 rcu_read_unlock();
571
572end:
573end_no_session:
574 return ret;
575}
576
577/*
578 * Allocate and init a new viewer_stream.
579 *
580 * Copies the values from the stream passed in parameter and insert the new
581 * stream in the viewer_streams_ht.
582 *
583 * MUST be called with rcu_read_lock held.
584 *
585 * Returns 0 on success or a negative value on error.
586 */
587static
588int init_viewer_stream(struct relay_stream *stream,
589 struct lttng_ht *viewer_streams_ht)
590{
591 int ret;
592 struct relay_viewer_stream *viewer_stream;
593
594 assert(stream);
595 assert(viewer_streams_ht);
596
597 viewer_stream = zmalloc(sizeof(*viewer_stream));
598 if (!viewer_stream) {
599 PERROR("relay viewer stream zmalloc");
600 ret = -1;
601 goto error;
602 }
603
604 viewer_stream->read_fd = -1;
605 viewer_stream->index_read_fd = -1;
606 viewer_stream->session_id = stream->session->id;
607 viewer_stream->stream_handle = stream->stream_handle;
608 viewer_stream->path_name = strndup(stream->path_name,
609 LTTNG_VIEWER_PATH_MAX);
610 viewer_stream->channel_name = strndup(stream->channel_name,
611 LTTNG_VIEWER_NAME_MAX);
612 viewer_stream->total_index_received = stream->total_index_received;
613 viewer_stream->tracefile_size = stream->tracefile_size;
614 viewer_stream->tracefile_count = stream->tracefile_count;
615 viewer_stream->metadata_flag = stream->metadata_flag;
616
617 /*
618 * This is to avoid a race between the initialization of this object and
619 * the close of the given stream. If the stream is unable to find this
620 * viewer stream when closing, this copy will at least take the latest
621 * value.
622 */
623 viewer_stream->total_index_received = stream->total_index_received;
624
625 /*
626 * The deletion of this ctf_trace object is only done in a call RCU of the
627 * relay stream making it valid as long as we have the read side lock.
628 */
629 viewer_stream->ctf_trace = stream->ctf_trace;
630 uatomic_inc(&viewer_stream->ctf_trace->refcount);
631
632 lttng_ht_node_init_u64(&viewer_stream->stream_n, stream->stream_handle);
633 lttng_ht_add_unique_u64(viewer_streams_ht, &viewer_stream->stream_n);
634
635 ret = 0;
636
637error:
638 return ret;
639}
640
641/*
642 * Send the viewer the list of current sessions.
643 */
644static
645int viewer_attach_session(struct relay_command *cmd,
646 struct lttng_ht *sessions_ht,
647 struct lttng_ht *viewer_streams_ht)
648{
649 int ret, send_streams = 0, nb_streams = 0;
650 struct lttng_viewer_attach_session_request request;
651 struct lttng_viewer_attach_session_response response;
652 struct lttng_viewer_stream send_stream;
653 struct relay_stream *stream;
654 struct relay_viewer_stream *viewer_stream;
655 struct lttng_ht_node_ulong *node;
656 struct lttng_ht_node_u64 *node64;
657 struct lttng_ht_iter iter;
658 struct relay_session *session;
659
660 assert(cmd);
661 assert(sessions_ht);
662 assert(viewer_streams_ht);
663
664 DBG("Attach session received");
665
666 if (cmd->version_check_done == 0) {
667 ERR("Trying to attach session before version check");
668 ret = -1;
669 goto end_no_session;
670 }
671
672 ret = cmd->sock->ops->recvmsg(cmd->sock, &request, sizeof(request), 0);
673 if (ret < 0 || ret != sizeof(request)) {
674 if (ret == 0) {
675 /* Orderly shutdown. Not necessary to print an error. */
676 DBG("Socket %d did an orderly shutdown", cmd->sock->fd);
677 } else {
678 ERR("Relay failed to receive the attach parameters.");
679 }
680 ret = -1;
681 goto error;
682 }
683
684 rcu_read_lock();
685 lttng_ht_lookup(sessions_ht,
686 (void *)((unsigned long) be64toh(request.session_id)), &iter);
687 node = lttng_ht_iter_get_node_ulong(&iter);
688 if (node == NULL) {
689 DBG("Relay session %" PRIu64 " not found",
690 be64toh(request.session_id));
691 response.status = htobe32(VIEWER_ATTACH_UNK);
692 goto send_reply;
693 }
694
695 session = caa_container_of(node, struct relay_session, session_n);
696 if (cmd->session == session) {
697 /* Same viewer already attached, just send the stream list. */
698 send_streams = 1;
699 response.status = htobe32(VIEWER_ATTACH_OK);
700 } else if (session->viewer_attached != 0) {
701 DBG("Already a viewer attached");
702 response.status = htobe32(VIEWER_ATTACH_ALREADY);
703 goto send_reply;
704 } else if (session->live_timer == 0) {
705 DBG("Not live session");
706 response.status = htobe32(VIEWER_ATTACH_NOT_LIVE);
707 goto send_reply;
708 } else {
709 session->viewer_attached++;
710 send_streams = 1;
711 response.status = htobe32(VIEWER_ATTACH_OK);
712 cmd->session = session;
713 }
714
715 switch (be32toh(request.seek)) {
716 case VIEWER_SEEK_BEGINNING:
717 /* Default behaviour. */
718 break;
719 case VIEWER_SEEK_LAST:
720 /* TODO */
721 break;
722 default:
723 ERR("Wrong seek parameter");
724 response.status = htobe32(VIEWER_ATTACH_SEEK_ERR);
725 send_streams = 0;
726 goto send_reply;
727 }
728
729 if (send_streams) {
730 /* We should only be there if we have a session to attach to. */
731 assert(session);
732
733 /*
734 * Fill the viewer_streams_ht to count the number of streams
735 * ready to be sent and avoid concurrency issues on the
736 * relay_streams_ht and don't rely on a total session stream count.
737 */
738 cds_lfht_for_each_entry(relay_streams_ht->ht, &iter.iter, node, node) {
739 struct relay_viewer_stream *vstream;
740
741 node = lttng_ht_iter_get_node_ulong(&iter);
742 if (!node) {
743 continue;
744 }
745 stream = caa_container_of(node, struct relay_stream, stream_n);
746 if (stream->session != cmd->session) {
747 continue;
748 }
749
750 /*
751 * Don't send streams with no ctf_trace, they are not ready to be
752 * read.
753 */
754 if (!stream->ctf_trace) {
755 continue;
756 }
757
758 vstream = live_find_viewer_stream_by_id(stream->stream_handle,
759 viewer_streams_ht);
760 if (!vstream) {
761 ret = init_viewer_stream(stream, viewer_streams_ht);
762 if (ret < 0) {
763 goto end_unlock;
764 }
765 }
766 nb_streams++;
767 }
768 response.streams_count = htobe32(nb_streams);
769 }
770
771send_reply:
772 ret = cmd->sock->ops->sendmsg(cmd->sock, &response, sizeof(response), 0);
773 if (ret < 0) {
774 ERR("Relay sending viewer attach response");
775 goto end_unlock;
776 }
777
778 /*
779 * Unknown or busy session, just return gracefully, the viewer knows what
780 * is happening.
781 */
782 if (!send_streams) {
783 ret = 0;
784 goto end_unlock;
785 }
786
787 /* We should only be there if we have a session to attach to. */
788 assert(session);
789 cds_lfht_for_each_entry(viewer_streams_ht->ht, &iter.iter, node, node) {
790 node64 = lttng_ht_iter_get_node_u64(&iter);
791 if (!node64) {
792 continue;
793 }
794 viewer_stream = caa_container_of(node64, struct relay_viewer_stream,
795 stream_n);
796 if (viewer_stream->session_id != cmd->session->id) {
797 continue;
798 }
799
800 send_stream.id = htobe64(viewer_stream->stream_handle);
801 send_stream.ctf_trace_id = htobe64(viewer_stream->ctf_trace->id);
802 send_stream.metadata_flag = htobe32(viewer_stream->metadata_flag);
803 strncpy(send_stream.path_name, viewer_stream->path_name,
804 sizeof(send_stream.path_name));
805 strncpy(send_stream.channel_name, viewer_stream->channel_name,
806 sizeof(send_stream.channel_name));
807
808 ret = cmd->sock->ops->sendmsg(cmd->sock, &send_stream,
809 sizeof(send_stream), 0);
810 if (ret < 0) {
811 ERR("Relay sending stream %" PRIu64, viewer_stream->stream_handle);
812 goto end_unlock;
813 }
814 DBG("Sent stream %" PRIu64 " to viewer", viewer_stream->stream_handle);
815 }
816 ret = 0;
817
818end_unlock:
819 rcu_read_unlock();
820end_no_session:
821error:
822 return ret;
823}
824
825/*
826 * Open index file using a given viewer stream.
827 *
828 * Return 0 on success or else a negative value.
829 */
830static int open_index(struct relay_viewer_stream *stream)
831{
832 int ret;
833 char fullpath[PATH_MAX];
834 struct lttng_packet_index_file_hdr hdr;
835
836 if (stream->tracefile_size > 0) {
837 /* For now we don't support on-disk ring buffer. */
838 ret = -1;
839 goto end;
840 } else {
841 ret = snprintf(fullpath, sizeof(fullpath), "%s/" DEFAULT_INDEX_DIR
842 "/%s" DEFAULT_INDEX_FILE_SUFFIX,
843 stream->path_name, stream->channel_name);
844 if (ret < 0) {
845 PERROR("snprintf index path");
846 goto error;
847 }
848 }
849
850 DBG("Opening index file %s in read only", fullpath);
851 ret = open(fullpath, O_RDONLY);
852 if (ret < 0) {
853 if (errno == ENOENT) {
854 ret = ENOENT;
855 goto error;
856 } else {
857 PERROR("opening index in read-only");
858 }
859 goto error;
860 }
861 stream->index_read_fd = ret;
862 DBG("Opening index file %s in read only, (fd: %d)", fullpath, ret);
863
864 do {
865 ret = read(stream->index_read_fd, &hdr, sizeof(hdr));
866 } while (ret < 0 && errno == EINTR);
867 if (ret < 0) {
868 PERROR("Reading index header");
869 goto error;
870 }
871 if (strncmp(hdr.magic, INDEX_MAGIC, sizeof(hdr.magic)) != 0) {
872 ERR("Invalid header magic");
873 ret = -1;
874 goto error;
875 }
876 if (be32toh(hdr.index_major) != INDEX_MAJOR ||
877 be32toh(hdr.index_minor) != INDEX_MINOR) {
878 ERR("Invalid header version");
879 ret = -1;
880 goto error;
881 }
882 ret = 0;
883
884error:
885end:
886 return ret;
887}
888
889/*
890 * Get viewer stream from stream id.
891 *
892 * RCU read side lock MUST be acquired.
893 */
894struct relay_viewer_stream *live_find_viewer_stream_by_id(uint64_t stream_id,
895 struct lttng_ht *viewer_streams_ht)
896{
897 struct lttng_ht_node_u64 *node;
898 struct lttng_ht_iter iter;
899 struct relay_viewer_stream *stream = NULL;
900
901 assert(viewer_streams_ht);
902
903 lttng_ht_lookup(viewer_streams_ht, &stream_id, &iter);
904 node = lttng_ht_iter_get_node_u64(&iter);
905 if (node == NULL) {
906 DBG("Relay viewer stream %" PRIu64 " not found", stream_id);
907 goto end;
908 }
909 stream = caa_container_of(node, struct relay_viewer_stream, stream_n);
910
911end:
912 return stream;
913}
914
915/*
916 * Send the next index for a stream.
917 *
918 * Return 0 on success or else a negative value.
919 */
920static
921int viewer_get_next_index(struct relay_command *cmd,
922 struct lttng_ht *viewer_streams_ht, struct lttng_ht *sessions_ht)
923{
924 int ret;
925 struct lttng_viewer_get_next_index request_index;
926 struct lttng_viewer_index viewer_index;
927 struct lttng_packet_index packet_index;
928 struct relay_viewer_stream *vstream;
929 struct relay_stream *rstream;
930
931 assert(cmd);
932 assert(viewer_streams_ht);
933 assert(sessions_ht);
934
935 DBG("Viewer get next index");
936
937 if (cmd->version_check_done == 0) {
938 ERR("Trying to request index before version check");
939 ret = -1;
940 goto end_no_session;
941 }
942
943 ret = cmd->sock->ops->recvmsg(cmd->sock, &request_index,
944 sizeof(request_index), 0);
945 if (ret < 0 || ret != sizeof(request_index)) {
946 ret = -1;
947 ERR("Relay didn't receive the whole packet");
948 goto end;
949 }
950
951 rcu_read_lock();
952 vstream = live_find_viewer_stream_by_id(be64toh(request_index.stream_id),
953 viewer_streams_ht);
954 if (!vstream) {
955 ret = -1;
956 goto end_unlock;
957 }
958
959 memset(&viewer_index, 0, sizeof(viewer_index));
960
961 /*
962 * The viewer should not ask for index on metadata stream.
963 */
964 if (vstream->metadata_flag) {
965 viewer_index.status = htobe32(VIEWER_INDEX_HUP);
966 goto send_reply;
967 }
968
969 /* First time, we open the index file */
970 if (vstream->index_read_fd < 0) {
971 ret = open_index(vstream);
972 if (ret == ENOENT) {
973 /*
974 * The index is created only when the first data packet arrives, it
975 * might not be ready at the beginning of the session
976 */
977 viewer_index.status = htobe32(VIEWER_INDEX_RETRY);
978 goto send_reply;
979 } else if (ret < 0) {
980 viewer_index.status = htobe32(VIEWER_INDEX_ERR);
981 goto send_reply;
982 }
983 }
984
985 rstream = relay_stream_find_by_id(vstream->stream_handle);
986 if (rstream) {
987 if (rstream->beacon_ts_end != -1ULL &&
988 vstream->last_sent_index == rstream->total_index_received) {
989 viewer_index.status = htobe32(VIEWER_INDEX_INACTIVE);
990 viewer_index.timestamp_end = htobe64(rstream->beacon_ts_end);
991 goto send_reply;
992 }
993
994 if (rstream->total_index_received <= vstream->last_sent_index) {
995 /* No new index to send, retry later. */
996 viewer_index.status = htobe32(VIEWER_INDEX_RETRY);
997 goto send_reply;
998 }
999 } else if (!rstream &&
1000 vstream->total_index_received == vstream->last_sent_index) {
1001 /* Last index sent and stream closed */
1002 viewer_index.status = htobe32(VIEWER_INDEX_HUP);
1003 goto send_reply;
1004 }
1005
1006 if (!vstream->ctf_trace->metadata_received ||
1007 vstream->ctf_trace->metadata_received >
1008 vstream->ctf_trace->metadata_sent) {
1009 viewer_index.flags |= LTTNG_VIEWER_FLAG_NEW_METADATA;
1010 }
1011
1012 do {
1013 ret = read(vstream->index_read_fd, &packet_index,
1014 sizeof(packet_index));
1015 } while (ret < 0 && errno == EINTR);
1016 if (ret < sizeof(packet_index)) {
1017 PERROR("Relay reading index file");
1018 viewer_index.status = htobe32(VIEWER_INDEX_ERR);
1019 } else {
1020 viewer_index.status = htobe32(VIEWER_INDEX_OK);
1021 vstream->last_sent_index++;
1022 }
1023
1024 /*
1025 * Indexes are stored in big endian, no need to switch before sending.
1026 */
1027 viewer_index.offset = packet_index.offset;
1028 viewer_index.packet_size = packet_index.packet_size;
1029 viewer_index.content_size = packet_index.content_size;
1030 viewer_index.timestamp_begin = packet_index.timestamp_begin;
1031 viewer_index.timestamp_end = packet_index.timestamp_end;
1032 viewer_index.events_discarded = packet_index.events_discarded;
1033 viewer_index.stream_id = packet_index.stream_id;
1034
1035send_reply:
1036 viewer_index.flags = htobe32(viewer_index.flags);
1037 ret = cmd->sock->ops->sendmsg(cmd->sock, &viewer_index,
1038 sizeof(viewer_index), 0);
1039 if (ret < 0) {
1040 ERR("Relay index to viewer");
1041 goto end_unlock;
1042 }
1043
1044 DBG("Index %" PRIu64 "for stream %" PRIu64 "sent",
1045 vstream->last_sent_index, vstream->stream_handle);
1046
1047end_unlock:
1048 rcu_read_unlock();
1049
1050end_no_session:
1051end:
1052 return ret;
1053}
1054
1055/*
1056 * Send the next index for a stream
1057 *
1058 * Return 0 on success or else a negative value.
1059 */
1060static
1061int viewer_get_packet(struct relay_command *cmd,
1062 struct lttng_ht *viewer_streams_ht)
1063{
1064 int ret, send_data = 0;
1065 char *data = NULL;
1066 uint32_t len = 0;
1067 ssize_t read_len;
1068 struct lttng_viewer_get_packet get_packet_info;
1069 struct lttng_viewer_trace_packet reply;
1070 struct relay_viewer_stream *stream;
1071
1072 assert(cmd);
1073 assert(viewer_streams_ht);
1074
1075 DBG2("Relay get data packet");
1076
1077 if (cmd->version_check_done == 0) {
1078 ERR("Trying to get packet before version check");
1079 ret = -1;
1080 goto end;
1081 }
1082
1083 ret = cmd->sock->ops->recvmsg(cmd->sock, &get_packet_info,
1084 sizeof(get_packet_info), 0);
1085 if (ret < 0 || ret != sizeof(get_packet_info)) {
1086 ret = -1;
1087 ERR("Relay didn't receive the whole packet");
1088 goto end;
1089 }
1090
1091 rcu_read_lock();
1092 stream = live_find_viewer_stream_by_id(be64toh(get_packet_info.stream_id),
1093 viewer_streams_ht);
1094 if (!stream) {
1095 goto error;
1096 }
1097 assert(stream->ctf_trace);
1098
1099 /*
1100 * First time we read this stream, we need open the tracefile, we should
1101 * only arrive here if an index has already been sent to the viewer, so the
1102 * tracefile must exist, if it does not it is a fatal error.
1103 */
1104 if (stream->read_fd < 0) {
1105 char fullpath[PATH_MAX];
1106
1107 ret = snprintf(fullpath, PATH_MAX, "%s/%s", stream->path_name,
1108 stream->channel_name);
1109 if (ret < 0) {
1110 goto error;
1111 }
1112 ret = open(fullpath, O_RDONLY);
1113 if (ret < 0) {
1114 PERROR("Relay opening trace file");
1115 goto error;
1116 }
1117 stream->read_fd = ret;
1118 }
1119
1120 memset(&reply, 0, sizeof(reply));
1121
1122 if (!stream->ctf_trace->metadata_received ||
1123 stream->ctf_trace->metadata_received >
1124 stream->ctf_trace->metadata_sent) {
1125 reply.status = htobe32(VIEWER_GET_PACKET_ERR);
1126 reply.flags |= LTTNG_VIEWER_FLAG_NEW_METADATA;
1127
1128 goto send_reply;
1129 }
1130
1131 len = be32toh(get_packet_info.len);
1132 data = zmalloc(len);
1133 if (!data) {
1134 PERROR("relay data zmalloc");
1135 goto error;
1136 }
1137
1138 ret = lseek(stream->read_fd, be64toh(get_packet_info.offset), SEEK_SET);
1139 if (ret < 0) {
1140 PERROR("lseek");
1141 goto error;
1142 }
1143 read_len = read(stream->read_fd, data, len);
1144 if (read_len < (ssize_t) len) {
1145 PERROR("Relay reading trace file, fd: %d, offset: %" PRIu64,
1146 stream->read_fd, be64toh(get_packet_info.offset));
1147 goto error;
1148 }
1149 reply.status = htobe32(VIEWER_GET_PACKET_OK);
1150 reply.len = htobe32(len);
1151 send_data = 1;
1152 goto send_reply;
1153
1154error:
1155 reply.status = htobe32(VIEWER_GET_PACKET_ERR);
1156
1157send_reply:
1158 reply.flags = htobe32(reply.flags);
1159 ret = cmd->sock->ops->sendmsg(cmd->sock, &reply, sizeof(reply), 0);
1160 if (ret < 0) {
1161 ERR("Relay data header to viewer");
1162 goto end_unlock;
1163 }
1164
1165 if (send_data) {
1166 ret = cmd->sock->ops->sendmsg(cmd->sock, data, len, 0);
1167 if (ret < 0) {
1168 ERR("Relay send data to viewer");
1169 goto end_unlock;
1170 }
1171 }
1172
1173 DBG("Sent %u bytes for stream %" PRIu64, len,
1174 be64toh(get_packet_info.stream_id));
1175
1176end_unlock:
1177 free(data);
1178 rcu_read_unlock();
1179
1180end:
1181 return ret;
1182}
1183
1184/*
1185 * Send the session's metadata
1186 *
1187 * Return 0 on success else a negative value.
1188 */
1189static
1190int viewer_get_metadata(struct relay_command *cmd,
1191 struct lttng_ht *viewer_streams_ht)
1192{
1193 int ret = 0;
1194 ssize_t read_len;
1195 uint64_t len = 0;
1196 char *data = NULL;
1197 struct lttng_viewer_get_metadata request;
1198 struct lttng_viewer_metadata_packet reply;
1199 struct relay_viewer_stream *stream;
1200
1201 assert(cmd);
1202 assert(viewer_streams_ht);
1203
1204 DBG("Relay get metadata");
1205
1206 if (cmd->version_check_done == 0) {
1207 ERR("Trying to get metadata before version check");
1208 ret = -1;
1209 goto end;
1210 }
1211
1212 ret = cmd->sock->ops->recvmsg(cmd->sock, &request,
1213 sizeof(request), 0);
1214 if (ret < 0 || ret != sizeof(request)) {
1215 ret = -1;
1216 ERR("Relay didn't receive the whole packet");
1217 goto end;
1218 }
1219
1220 rcu_read_lock();
1221 stream = live_find_viewer_stream_by_id(be64toh(request.stream_id),
1222 viewer_streams_ht);
1223 if (!stream || !stream->metadata_flag) {
1224 ERR("Invalid metadata stream");
1225 goto error;
1226 }
1227 assert(stream->ctf_trace);
1228 assert(stream->ctf_trace->metadata_sent <=
1229 stream->ctf_trace->metadata_received);
1230
1231 len = stream->ctf_trace->metadata_received -
1232 stream->ctf_trace->metadata_sent;
1233 if (len == 0) {
1234 reply.status = htobe32(VIEWER_NO_NEW_METADATA);
1235 goto send_reply;
1236 }
1237
1238 /* first time, we open the metadata file */
1239 if (stream->read_fd < 0) {
1240 char fullpath[PATH_MAX];
1241
1242 ret = snprintf(fullpath, PATH_MAX, "%s/%s", stream->path_name,
1243 stream->channel_name);
1244 if (ret < 0) {
1245 goto error;
1246 }
1247 ret = open(fullpath, O_RDONLY);
1248 if (ret < 0) {
1249 PERROR("Relay opening metadata file");
1250 goto error;
1251 }
1252 stream->read_fd = ret;
1253 }
1254
1255 reply.len = htobe64(len);
1256 data = zmalloc(len);
1257 if (!data) {
1258 PERROR("viewer metadata zmalloc");
1259 goto error;
1260 }
1261
1262 read_len = read(stream->read_fd, data, len);
1263 if (read_len < (ssize_t) len) {
1264 PERROR("Relay reading metadata file");
1265 goto error;
1266 }
1267 stream->ctf_trace->metadata_sent += read_len;
1268 reply.status = htobe32(VIEWER_METADATA_OK);
1269 goto send_reply;
1270
1271error:
1272 reply.status = htobe32(VIEWER_METADATA_ERR);
1273
1274send_reply:
1275 ret = cmd->sock->ops->sendmsg(cmd->sock, &reply, sizeof(reply), 0);
1276 if (ret < 0) {
1277 ERR("Relay data header to viewer");
1278 goto end_unlock;
1279 }
1280
1281 if (len > 0) {
1282 ret = cmd->sock->ops->sendmsg(cmd->sock, data, len, 0);
1283 if (ret < 0) {
1284 ERR("Relay send data to viewer");
1285 goto end_unlock;
1286 }
1287 }
1288
1289 DBG("Sent %" PRIu64 " bytes of metadata for stream %" PRIu64, len,
1290 be64toh(request.stream_id));
1291
1292 DBG("Metadata sent");
1293
1294end_unlock:
1295 free(data);
1296 rcu_read_unlock();
1297end:
1298 return ret;
1299}
1300
1301/*
1302 * live_relay_unknown_command: send -1 if received unknown command
1303 */
1304static
1305void live_relay_unknown_command(struct relay_command *cmd)
1306{
1307 struct lttcomm_relayd_generic_reply reply;
1308 int ret;
1309
1310 reply.ret_code = htobe32(LTTNG_ERR_UNK);
1311 ret = cmd->sock->ops->sendmsg(cmd->sock, &reply,
1312 sizeof(struct lttcomm_relayd_generic_reply), 0);
1313 if (ret < 0) {
1314 ERR("Relay sending unknown command");
1315 }
1316}
1317
1318/*
1319 * Process the commands received on the control socket
1320 */
1321static
1322int process_control(struct lttng_viewer_cmd *recv_hdr,
1323 struct relay_command *cmd, struct lttng_ht *sessions_ht,
1324 struct lttng_ht *viewer_streams_ht)
1325{
1326 int ret = 0;
1327
1328 switch (be32toh(recv_hdr->cmd)) {
1329 case VIEWER_CONNECT:
1330 ret = viewer_connect(cmd);
1331 break;
1332 case VIEWER_LIST_SESSIONS:
1333 ret = viewer_list_sessions(cmd, sessions_ht);
1334 break;
1335 case VIEWER_ATTACH_SESSION:
1336 ret = viewer_attach_session(cmd, sessions_ht,
1337 viewer_streams_ht);
1338 break;
1339 case VIEWER_GET_NEXT_INDEX:
1340 ret = viewer_get_next_index(cmd, viewer_streams_ht, sessions_ht);
1341 break;
1342 case VIEWER_GET_PACKET:
1343 ret = viewer_get_packet(cmd, viewer_streams_ht);
1344 break;
1345 case VIEWER_GET_METADATA:
1346 ret = viewer_get_metadata(cmd, viewer_streams_ht);
1347 break;
1348 default:
1349 ERR("Received unknown viewer command (%u)", be32toh(recv_hdr->cmd));
1350 live_relay_unknown_command(cmd);
1351 ret = -1;
1352 goto end;
1353 }
1354
1355end:
1356 return ret;
1357}
1358
1359static
1360void cleanup_poll_connection(struct lttng_poll_event *events, int pollfd)
1361{
1362 int ret;
1363
1364 assert(events);
1365
1366 lttng_poll_del(events, pollfd);
1367
1368 ret = close(pollfd);
1369 if (ret < 0) {
1370 ERR("Closing pollfd %d", pollfd);
1371 }
1372}
1373
1374/*
1375 * Create and add connection to the given hash table.
1376 *
1377 * Return poll add value or else -1 on error.
1378 */
1379static
1380int add_connection(int fd, struct lttng_poll_event *events,
1381 struct lttng_ht *relay_connections_ht)
1382{
1383 int ret;
1384 struct relay_command *relay_connection;
1385
1386 assert(events);
1387 assert(relay_connections_ht);
1388
1389 relay_connection = zmalloc(sizeof(struct relay_command));
1390 if (relay_connection == NULL) {
1391 PERROR("Relay command zmalloc");
1392 goto error;
1393 }
1394
1395 do {
1396 ret = read(fd, relay_connection, sizeof(*relay_connection));
1397 } while (ret < 0 && errno == EINTR);
1398 if (ret < 0 || ret < sizeof(*relay_connection)) {
1399 PERROR("read relay cmd pipe");
1400 goto error_read;
1401 }
1402
1403 lttng_ht_node_init_ulong(&relay_connection->sock_n,
1404 (unsigned long) relay_connection->sock->fd);
1405 rcu_read_lock();
1406 lttng_ht_add_unique_ulong(relay_connections_ht,
1407 &relay_connection->sock_n);
1408 rcu_read_unlock();
1409
1410 return lttng_poll_add(events, relay_connection->sock->fd,
1411 LPOLLIN | LPOLLRDHUP);
1412
1413error_read:
1414 free(relay_connection);
1415error:
1416 return -1;
1417}
1418
1419static
1420void deferred_free_connection(struct rcu_head *head)
1421{
1422 struct relay_command *relay_connection =
1423 caa_container_of(head, struct relay_command, rcu_node);
1424
1425 if (relay_connection->session &&
1426 relay_connection->session->viewer_attached > 0) {
1427 relay_connection->session->viewer_attached--;
1428 }
1429 lttcomm_destroy_sock(relay_connection->sock);
1430 free(relay_connection);
1431}
1432
1433static
1434void deferred_free_viewer_stream(struct rcu_head *head)
1435{
1436 struct relay_viewer_stream *stream =
1437 caa_container_of(head, struct relay_viewer_stream, rcu_node);
1438
1439 if (stream->ctf_trace) {
1440 uatomic_dec(&stream->ctf_trace->refcount);
1441 assert(uatomic_read(&stream->ctf_trace->refcount) >= 0);
1442 if (uatomic_read(&stream->ctf_trace->refcount) == 0) {
1443 DBG("Freeing ctf_trace %" PRIu64, stream->ctf_trace->id);
1444 free(stream->ctf_trace);
1445 }
1446 }
1447
1448 free(stream->path_name);
1449 free(stream->channel_name);
1450 free(stream);
1451}
1452
1453static
1454void viewer_del_streams(struct lttng_ht *viewer_streams_ht,
1455 struct relay_session *session)
1456{
1457 int ret;
1458 struct relay_viewer_stream *stream;
1459 struct lttng_ht_node_u64 *node;
1460 struct lttng_ht_iter iter;
1461
1462 assert(viewer_streams_ht);
1463 assert(session);
1464
1465 rcu_read_lock();
1466 cds_lfht_for_each_entry(viewer_streams_ht->ht, &iter.iter, node, node) {
1467 node = lttng_ht_iter_get_node_u64(&iter);
1468 if (!node) {
1469 continue;
1470 }
1471
1472 stream = caa_container_of(node, struct relay_viewer_stream, stream_n);
1473 if (stream->session_id != session->id) {
1474 continue;
1475 }
1476
1477 if (stream->read_fd > 0) {
1478 ret = close(stream->read_fd);
1479 if (ret < 0) {
1480 PERROR("close read_fd");
1481 }
1482 }
1483 if (stream->index_read_fd > 0) {
1484 ret = close(stream->index_read_fd);
1485 if (ret < 0) {
1486 PERROR("close index_read_fd");
1487 }
1488 }
1489 if (stream->metadata_flag && stream->ctf_trace) {
1490 stream->ctf_trace->metadata_sent = 0;
1491 }
1492 ret = lttng_ht_del(viewer_streams_ht, &iter);
1493 assert(!ret);
1494 call_rcu(&stream->rcu_node, deferred_free_viewer_stream);
1495 }
1496 rcu_read_unlock();
1497}
1498
1499/*
1500 * Delete and free a connection.
1501 *
1502 * RCU read side lock MUST be acquired.
1503 */
1504static
1505void del_connection(struct lttng_ht *relay_connections_ht,
1506 struct lttng_ht_iter *iter, struct relay_command *relay_connection,
1507 struct lttng_ht *viewer_streams_ht)
1508{
1509 int ret;
1510
1511 assert(relay_connections_ht);
1512 assert(iter);
1513 assert(relay_connection);
1514 assert(viewer_streams_ht);
1515
1516 ret = lttng_ht_del(relay_connections_ht, iter);
1517 assert(!ret);
1518
1519 if (relay_connection->session) {
1520 viewer_del_streams(viewer_streams_ht, relay_connection->session);
1521 }
1522
1523 call_rcu(&relay_connection->rcu_node, deferred_free_connection);
1524}
1525
1526/*
1527 * This thread does the actual work
1528 */
1529static
1530void *thread_worker(void *data)
1531{
1532 int ret, err = -1;
1533 uint32_t nb_fd;
1534 struct relay_command *relay_connection;
1535 struct lttng_poll_event events;
1536 struct lttng_ht *relay_connections_ht;
1537 struct lttng_ht_node_ulong *node;
1538 struct lttng_ht_iter iter;
1539 struct lttng_viewer_cmd recv_hdr;
1540 struct relay_local_data *relay_ctx = (struct relay_local_data *) data;
1541 struct lttng_ht *sessions_ht = relay_ctx->sessions_ht;
1542 struct lttng_ht *viewer_streams_ht = relay_ctx->viewer_streams_ht;
1543
1544 DBG("[thread] Live viewer relay worker started");
1545
1546 rcu_register_thread();
1547
1548 /* table of connections indexed on socket */
1549 relay_connections_ht = lttng_ht_new(0, LTTNG_HT_TYPE_ULONG);
1550 if (!relay_connections_ht) {
1551 goto relay_connections_ht_error;
1552 }
1553
1554 ret = create_thread_poll_set(&events, 2);
1555 if (ret < 0) {
1556 goto error_poll_create;
1557 }
1558
1559 ret = lttng_poll_add(&events, live_relay_cmd_pipe[0], LPOLLIN | LPOLLRDHUP);
1560 if (ret < 0) {
1561 goto error;
1562 }
1563
1564restart:
1565 while (1) {
1566 int i;
1567
1568 /* Infinite blocking call, waiting for transmission */
1569 DBG3("Relayd live viewer worker thread polling...");
1570 ret = lttng_poll_wait(&events, -1);
1571 if (ret < 0) {
1572 /*
1573 * Restart interrupted system call.
1574 */
1575 if (errno == EINTR) {
1576 goto restart;
1577 }
1578 goto error;
1579 }
1580
1581 nb_fd = ret;
1582
1583 /*
1584 * Process control. The control connection is prioritised so we don't
1585 * starve it with high throughput tracing data on the data
1586 * connection.
1587 */
1588 for (i = 0; i < nb_fd; i++) {
1589 /* Fetch once the poll data */
1590 uint32_t revents = LTTNG_POLL_GETEV(&events, i);
1591 int pollfd = LTTNG_POLL_GETFD(&events, i);
1592
1593 /* Thread quit pipe has been closed. Killing thread. */
1594 ret = check_thread_quit_pipe(pollfd, revents);
1595 if (ret) {
1596 err = 0;
1597 goto exit;
1598 }
1599
1600 /* Inspect the relay cmd pipe for new connection */
1601 if (pollfd == live_relay_cmd_pipe[0]) {
1602 if (revents & (LPOLLERR | LPOLLHUP | LPOLLRDHUP)) {
1603 ERR("Relay live pipe error");
1604 goto error;
1605 } else if (revents & LPOLLIN) {
1606 DBG("Relay live viewer command received");
1607 ret = add_connection(live_relay_cmd_pipe[0],
1608 &events, relay_connections_ht);
1609 if (ret < 0) {
1610 goto error;
1611 }
1612 }
1613 } else if (revents) {
1614 rcu_read_lock();
1615 lttng_ht_lookup(relay_connections_ht,
1616 (void *)((unsigned long) pollfd), &iter);
1617 node = lttng_ht_iter_get_node_ulong(&iter);
1618 if (node == NULL) {
1619 DBG2("Relay viewer sock %d not found", pollfd);
1620 rcu_read_unlock();
1621 goto error;
1622 }
1623 relay_connection = caa_container_of(node, struct relay_command,
1624 sock_n);
1625
1626 if (revents & (LPOLLERR)) {
1627 ERR("VIEWER POLL ERROR");
1628 cleanup_poll_connection(&events, pollfd);
1629 del_connection(relay_connections_ht, &iter,
1630 relay_connection, viewer_streams_ht);
1631 } else if (revents & (LPOLLHUP | LPOLLRDHUP)) {
1632 DBG("Viewer socket %d hung up", pollfd);
1633 cleanup_poll_connection(&events, pollfd);
1634 del_connection(relay_connections_ht, &iter,
1635 relay_connection, viewer_streams_ht);
1636 } else if (revents & LPOLLIN) {
1637 ret = relay_connection->sock->ops->recvmsg(
1638 relay_connection->sock, &recv_hdr,
1639 sizeof(struct lttng_viewer_cmd),
1640 0);
1641 /* connection closed */
1642 if (ret <= 0) {
1643 cleanup_poll_connection(&events, pollfd);
1644 del_connection( relay_connections_ht, &iter,
1645 relay_connection, viewer_streams_ht);
1646 DBG("Viewer control connection closed with %d",
1647 pollfd);
1648 } else {
1649 if (relay_connection->session) {
1650 DBG2("Relay viewer worker receiving data for "
1651 "session: %" PRIu64,
1652 relay_connection->session->id);
1653 }
1654 ret = process_control(&recv_hdr, relay_connection,
1655 sessions_ht, viewer_streams_ht);
1656 if (ret < 0) {
1657 /* Clear the session on error. */
1658 cleanup_poll_connection(&events, pollfd);
1659 del_connection(relay_connections_ht, &iter,
1660 relay_connection, viewer_streams_ht);
1661 DBG("Viewer connection closed with %d", pollfd);
1662 }
1663 }
1664 }
1665 rcu_read_unlock();
1666 }
1667 }
1668 }
1669
1670exit:
1671error:
1672 lttng_poll_clean(&events);
1673
1674 /* empty the hash table and free the memory */
1675 rcu_read_lock();
1676 cds_lfht_for_each_entry(relay_connections_ht->ht, &iter.iter, node, node) {
1677 node = lttng_ht_iter_get_node_ulong(&iter);
1678 if (!node) {
1679 continue;
1680 }
1681
1682 relay_connection = caa_container_of(node, struct relay_command,
1683 sock_n);
1684 del_connection(relay_connections_ht, &iter, relay_connection,
1685 viewer_streams_ht);
1686 }
1687 rcu_read_unlock();
1688error_poll_create:
1689 lttng_ht_destroy(relay_connections_ht);
1690relay_connections_ht_error:
1691 /* Close relay cmd pipes */
1692 utils_close_pipe(live_relay_cmd_pipe);
1693 if (err) {
1694 DBG("Viewer worker thread exited with error");
1695 }
1696 DBG("Viewer worker thread cleanup complete");
1697 stop_threads();
1698 rcu_unregister_thread();
1699 return NULL;
1700}
1701
1702/*
1703 * Create the relay command pipe to wake thread_manage_apps.
1704 * Closed in cleanup().
1705 */
1706static int create_relay_cmd_pipe(void)
1707{
1708 int ret;
1709
1710 ret = utils_create_pipe_cloexec(live_relay_cmd_pipe);
1711
1712 return ret;
1713}
1714
1715void live_stop_threads()
1716{
1717 int ret;
1718 void *status;
1719
1720 stop_threads();
1721
1722 ret = pthread_join(live_listener_thread, &status);
1723 if (ret != 0) {
1724 PERROR("pthread_join live listener");
1725 goto error; /* join error, exit without cleanup */
1726 }
1727
1728 ret = pthread_join(live_worker_thread, &status);
1729 if (ret != 0) {
1730 PERROR("pthread_join live worker");
1731 goto error; /* join error, exit without cleanup */
1732 }
1733
1734 ret = pthread_join(live_dispatcher_thread, &status);
1735 if (ret != 0) {
1736 PERROR("pthread_join live dispatcher");
1737 goto error; /* join error, exit without cleanup */
1738 }
1739
1740 cleanup();
1741
1742error:
1743 return;
1744}
1745
1746/*
1747 * main
1748 */
1749int live_start_threads(struct lttng_uri *uri,
1750 struct relay_local_data *relay_ctx)
1751{
1752 int ret = 0;
1753 void *status;
1754 int is_root;
1755
1756 assert(uri);
1757 live_uri = uri;
1758
1759 /* Create thread quit pipe */
1760 if ((ret = init_thread_quit_pipe()) < 0) {
1761 goto error;
1762 }
1763
1764 /* Check if daemon is UID = 0 */
1765 is_root = !getuid();
1766
1767 if (!is_root) {
1768 if (live_uri->port < 1024) {
1769 ERR("Need to be root to use ports < 1024");
1770 ret = -1;
1771 goto exit;
1772 }
1773 }
1774
1775 /* Setup the thread apps communication pipe. */
1776 if ((ret = create_relay_cmd_pipe()) < 0) {
1777 goto exit;
1778 }
1779
1780 /* Init relay command queue. */
1781 cds_wfq_init(&viewer_cmd_queue.queue);
1782
1783 /* Set up max poll set size */
1784 lttng_poll_set_max_size();
1785
1786 /* Setup the dispatcher thread */
1787 ret = pthread_create(&live_dispatcher_thread, NULL,
1788 thread_dispatcher, (void *) NULL);
1789 if (ret != 0) {
1790 PERROR("pthread_create viewer dispatcher");
1791 goto exit_dispatcher;
1792 }
1793
1794 /* Setup the worker thread */
1795 ret = pthread_create(&live_worker_thread, NULL,
1796 thread_worker, relay_ctx);
1797 if (ret != 0) {
1798 PERROR("pthread_create viewer worker");
1799 goto exit_worker;
1800 }
1801
1802 /* Setup the listener thread */
1803 ret = pthread_create(&live_listener_thread, NULL,
1804 thread_listener, (void *) NULL);
1805 if (ret != 0) {
1806 PERROR("pthread_create viewer listener");
1807 goto exit_listener;
1808 }
1809
1810 ret = 0;
1811 goto end;
1812
1813exit_listener:
1814 ret = pthread_join(live_listener_thread, &status);
1815 if (ret != 0) {
1816 PERROR("pthread_join live listener");
1817 goto error; /* join error, exit without cleanup */
1818 }
1819
1820exit_worker:
1821 ret = pthread_join(live_worker_thread, &status);
1822 if (ret != 0) {
1823 PERROR("pthread_join live worker");
1824 goto error; /* join error, exit without cleanup */
1825 }
1826
1827exit_dispatcher:
1828 ret = pthread_join(live_dispatcher_thread, &status);
1829 if (ret != 0) {
1830 PERROR("pthread_join live dispatcher");
1831 goto error; /* join error, exit without cleanup */
1832 }
1833
1834exit:
1835 cleanup();
1836
1837end:
1838error:
1839 return ret;
1840}
This page took 0.151925 seconds and 4 git commands to generate.