Fix: remove debug error message
[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
92c6ca54 588int init_viewer_stream(struct relay_stream *stream)
d3e2ba59
JD
589{
590 int ret;
591 struct relay_viewer_stream *viewer_stream;
592
593 assert(stream);
d3e2ba59
JD
594
595 viewer_stream = zmalloc(sizeof(*viewer_stream));
596 if (!viewer_stream) {
597 PERROR("relay viewer stream zmalloc");
598 ret = -1;
599 goto error;
600 }
601
602 viewer_stream->read_fd = -1;
603 viewer_stream->index_read_fd = -1;
604 viewer_stream->session_id = stream->session->id;
605 viewer_stream->stream_handle = stream->stream_handle;
606 viewer_stream->path_name = strndup(stream->path_name,
607 LTTNG_VIEWER_PATH_MAX);
608 viewer_stream->channel_name = strndup(stream->channel_name,
609 LTTNG_VIEWER_NAME_MAX);
610 viewer_stream->total_index_received = stream->total_index_received;
611 viewer_stream->tracefile_size = stream->tracefile_size;
612 viewer_stream->tracefile_count = stream->tracefile_count;
613 viewer_stream->metadata_flag = stream->metadata_flag;
614
615 /*
616 * This is to avoid a race between the initialization of this object and
617 * the close of the given stream. If the stream is unable to find this
618 * viewer stream when closing, this copy will at least take the latest
619 * value.
620 */
621 viewer_stream->total_index_received = stream->total_index_received;
622
623 /*
624 * The deletion of this ctf_trace object is only done in a call RCU of the
625 * relay stream making it valid as long as we have the read side lock.
626 */
627 viewer_stream->ctf_trace = stream->ctf_trace;
628 uatomic_inc(&viewer_stream->ctf_trace->refcount);
629
630 lttng_ht_node_init_u64(&viewer_stream->stream_n, stream->stream_handle);
631 lttng_ht_add_unique_u64(viewer_streams_ht, &viewer_stream->stream_n);
632
633 ret = 0;
634
635error:
636 return ret;
637}
638
639/*
640 * Send the viewer the list of current sessions.
641 */
642static
643int viewer_attach_session(struct relay_command *cmd,
92c6ca54 644 struct lttng_ht *sessions_ht)
d3e2ba59
JD
645{
646 int ret, send_streams = 0, nb_streams = 0;
647 struct lttng_viewer_attach_session_request request;
648 struct lttng_viewer_attach_session_response response;
649 struct lttng_viewer_stream send_stream;
650 struct relay_stream *stream;
651 struct relay_viewer_stream *viewer_stream;
652 struct lttng_ht_node_ulong *node;
653 struct lttng_ht_node_u64 *node64;
654 struct lttng_ht_iter iter;
655 struct relay_session *session;
656
657 assert(cmd);
658 assert(sessions_ht);
d3e2ba59
JD
659
660 DBG("Attach session received");
661
662 if (cmd->version_check_done == 0) {
663 ERR("Trying to attach session before version check");
664 ret = -1;
665 goto end_no_session;
666 }
667
668 ret = cmd->sock->ops->recvmsg(cmd->sock, &request, sizeof(request), 0);
669 if (ret < 0 || ret != sizeof(request)) {
670 if (ret == 0) {
671 /* Orderly shutdown. Not necessary to print an error. */
672 DBG("Socket %d did an orderly shutdown", cmd->sock->fd);
673 } else {
674 ERR("Relay failed to receive the attach parameters.");
675 }
676 ret = -1;
677 goto error;
678 }
679
680 rcu_read_lock();
681 lttng_ht_lookup(sessions_ht,
682 (void *)((unsigned long) be64toh(request.session_id)), &iter);
683 node = lttng_ht_iter_get_node_ulong(&iter);
684 if (node == NULL) {
685 DBG("Relay session %" PRIu64 " not found",
686 be64toh(request.session_id));
687 response.status = htobe32(VIEWER_ATTACH_UNK);
688 goto send_reply;
689 }
690
691 session = caa_container_of(node, struct relay_session, session_n);
692 if (cmd->session == session) {
693 /* Same viewer already attached, just send the stream list. */
694 send_streams = 1;
695 response.status = htobe32(VIEWER_ATTACH_OK);
696 } else if (session->viewer_attached != 0) {
697 DBG("Already a viewer attached");
698 response.status = htobe32(VIEWER_ATTACH_ALREADY);
699 goto send_reply;
700 } else if (session->live_timer == 0) {
701 DBG("Not live session");
702 response.status = htobe32(VIEWER_ATTACH_NOT_LIVE);
703 goto send_reply;
704 } else {
705 session->viewer_attached++;
706 send_streams = 1;
707 response.status = htobe32(VIEWER_ATTACH_OK);
708 cmd->session = session;
709 }
710
711 switch (be32toh(request.seek)) {
712 case VIEWER_SEEK_BEGINNING:
713 /* Default behaviour. */
714 break;
715 case VIEWER_SEEK_LAST:
716 /* TODO */
717 break;
718 default:
719 ERR("Wrong seek parameter");
720 response.status = htobe32(VIEWER_ATTACH_SEEK_ERR);
721 send_streams = 0;
722 goto send_reply;
723 }
724
725 if (send_streams) {
726 /* We should only be there if we have a session to attach to. */
727 assert(session);
728
729 /*
730 * Fill the viewer_streams_ht to count the number of streams
731 * ready to be sent and avoid concurrency issues on the
732 * relay_streams_ht and don't rely on a total session stream count.
733 */
734 cds_lfht_for_each_entry(relay_streams_ht->ht, &iter.iter, node, node) {
735 struct relay_viewer_stream *vstream;
736
737 node = lttng_ht_iter_get_node_ulong(&iter);
738 if (!node) {
739 continue;
740 }
741 stream = caa_container_of(node, struct relay_stream, stream_n);
742 if (stream->session != cmd->session) {
743 continue;
744 }
745
746 /*
747 * Don't send streams with no ctf_trace, they are not ready to be
748 * read.
749 */
750 if (!stream->ctf_trace) {
751 continue;
752 }
753
92c6ca54 754 vstream = live_find_viewer_stream_by_id(stream->stream_handle);
d3e2ba59 755 if (!vstream) {
92c6ca54 756 ret = init_viewer_stream(stream);
d3e2ba59
JD
757 if (ret < 0) {
758 goto end_unlock;
759 }
760 }
761 nb_streams++;
762 }
763 response.streams_count = htobe32(nb_streams);
764 }
765
766send_reply:
767 ret = cmd->sock->ops->sendmsg(cmd->sock, &response, sizeof(response), 0);
768 if (ret < 0) {
769 ERR("Relay sending viewer attach response");
770 goto end_unlock;
771 }
772
773 /*
774 * Unknown or busy session, just return gracefully, the viewer knows what
775 * is happening.
776 */
777 if (!send_streams) {
778 ret = 0;
779 goto end_unlock;
780 }
781
782 /* We should only be there if we have a session to attach to. */
783 assert(session);
784 cds_lfht_for_each_entry(viewer_streams_ht->ht, &iter.iter, node, node) {
785 node64 = lttng_ht_iter_get_node_u64(&iter);
786 if (!node64) {
787 continue;
788 }
789 viewer_stream = caa_container_of(node64, struct relay_viewer_stream,
790 stream_n);
791 if (viewer_stream->session_id != cmd->session->id) {
792 continue;
793 }
794
795 send_stream.id = htobe64(viewer_stream->stream_handle);
796 send_stream.ctf_trace_id = htobe64(viewer_stream->ctf_trace->id);
797 send_stream.metadata_flag = htobe32(viewer_stream->metadata_flag);
798 strncpy(send_stream.path_name, viewer_stream->path_name,
799 sizeof(send_stream.path_name));
800 strncpy(send_stream.channel_name, viewer_stream->channel_name,
801 sizeof(send_stream.channel_name));
802
803 ret = cmd->sock->ops->sendmsg(cmd->sock, &send_stream,
804 sizeof(send_stream), 0);
805 if (ret < 0) {
806 ERR("Relay sending stream %" PRIu64, viewer_stream->stream_handle);
807 goto end_unlock;
808 }
809 DBG("Sent stream %" PRIu64 " to viewer", viewer_stream->stream_handle);
810 }
811 ret = 0;
812
813end_unlock:
814 rcu_read_unlock();
815end_no_session:
816error:
817 return ret;
818}
819
820/*
821 * Open index file using a given viewer stream.
822 *
823 * Return 0 on success or else a negative value.
824 */
825static int open_index(struct relay_viewer_stream *stream)
826{
827 int ret;
828 char fullpath[PATH_MAX];
829 struct lttng_packet_index_file_hdr hdr;
830
831 if (stream->tracefile_size > 0) {
832 /* For now we don't support on-disk ring buffer. */
833 ret = -1;
834 goto end;
835 } else {
836 ret = snprintf(fullpath, sizeof(fullpath), "%s/" DEFAULT_INDEX_DIR
837 "/%s" DEFAULT_INDEX_FILE_SUFFIX,
838 stream->path_name, stream->channel_name);
839 if (ret < 0) {
840 PERROR("snprintf index path");
841 goto error;
842 }
843 }
844
845 DBG("Opening index file %s in read only", fullpath);
846 ret = open(fullpath, O_RDONLY);
847 if (ret < 0) {
848 if (errno == ENOENT) {
849 ret = ENOENT;
850 goto error;
851 } else {
852 PERROR("opening index in read-only");
853 }
854 goto error;
855 }
856 stream->index_read_fd = ret;
857 DBG("Opening index file %s in read only, (fd: %d)", fullpath, ret);
858
859 do {
860 ret = read(stream->index_read_fd, &hdr, sizeof(hdr));
861 } while (ret < 0 && errno == EINTR);
862 if (ret < 0) {
863 PERROR("Reading index header");
864 goto error;
865 }
866 if (strncmp(hdr.magic, INDEX_MAGIC, sizeof(hdr.magic)) != 0) {
867 ERR("Invalid header magic");
868 ret = -1;
869 goto error;
870 }
871 if (be32toh(hdr.index_major) != INDEX_MAJOR ||
872 be32toh(hdr.index_minor) != INDEX_MINOR) {
873 ERR("Invalid header version");
874 ret = -1;
875 goto error;
876 }
877 ret = 0;
878
879error:
880end:
881 return ret;
882}
883
884/*
885 * Get viewer stream from stream id.
886 *
887 * RCU read side lock MUST be acquired.
888 */
92c6ca54 889struct relay_viewer_stream *live_find_viewer_stream_by_id(uint64_t stream_id)
d3e2ba59
JD
890{
891 struct lttng_ht_node_u64 *node;
892 struct lttng_ht_iter iter;
893 struct relay_viewer_stream *stream = NULL;
894
d3e2ba59
JD
895 lttng_ht_lookup(viewer_streams_ht, &stream_id, &iter);
896 node = lttng_ht_iter_get_node_u64(&iter);
897 if (node == NULL) {
898 DBG("Relay viewer stream %" PRIu64 " not found", stream_id);
899 goto end;
900 }
901 stream = caa_container_of(node, struct relay_viewer_stream, stream_n);
902
903end:
904 return stream;
905}
906
907/*
908 * Send the next index for a stream.
909 *
910 * Return 0 on success or else a negative value.
911 */
912static
913int viewer_get_next_index(struct relay_command *cmd,
92c6ca54 914 struct lttng_ht *sessions_ht)
d3e2ba59
JD
915{
916 int ret;
917 struct lttng_viewer_get_next_index request_index;
918 struct lttng_viewer_index viewer_index;
919 struct lttng_packet_index packet_index;
920 struct relay_viewer_stream *vstream;
921 struct relay_stream *rstream;
922
923 assert(cmd);
d3e2ba59
JD
924 assert(sessions_ht);
925
926 DBG("Viewer get next index");
927
928 if (cmd->version_check_done == 0) {
929 ERR("Trying to request index before version check");
930 ret = -1;
931 goto end_no_session;
932 }
933
934 ret = cmd->sock->ops->recvmsg(cmd->sock, &request_index,
935 sizeof(request_index), 0);
936 if (ret < 0 || ret != sizeof(request_index)) {
937 ret = -1;
938 ERR("Relay didn't receive the whole packet");
939 goto end;
940 }
941
942 rcu_read_lock();
92c6ca54 943 vstream = live_find_viewer_stream_by_id(be64toh(request_index.stream_id));
d3e2ba59
JD
944 if (!vstream) {
945 ret = -1;
946 goto end_unlock;
947 }
948
949 memset(&viewer_index, 0, sizeof(viewer_index));
950
951 /*
952 * The viewer should not ask for index on metadata stream.
953 */
954 if (vstream->metadata_flag) {
955 viewer_index.status = htobe32(VIEWER_INDEX_HUP);
956 goto send_reply;
957 }
958
959 /* First time, we open the index file */
960 if (vstream->index_read_fd < 0) {
961 ret = open_index(vstream);
962 if (ret == ENOENT) {
963 /*
964 * The index is created only when the first data packet arrives, it
965 * might not be ready at the beginning of the session
966 */
967 viewer_index.status = htobe32(VIEWER_INDEX_RETRY);
968 goto send_reply;
969 } else if (ret < 0) {
970 viewer_index.status = htobe32(VIEWER_INDEX_ERR);
971 goto send_reply;
972 }
973 }
974
975 rstream = relay_stream_find_by_id(vstream->stream_handle);
976 if (rstream) {
977 if (rstream->beacon_ts_end != -1ULL &&
978 vstream->last_sent_index == rstream->total_index_received) {
979 viewer_index.status = htobe32(VIEWER_INDEX_INACTIVE);
980 viewer_index.timestamp_end = htobe64(rstream->beacon_ts_end);
981 goto send_reply;
982 }
983
984 if (rstream->total_index_received <= vstream->last_sent_index) {
985 /* No new index to send, retry later. */
986 viewer_index.status = htobe32(VIEWER_INDEX_RETRY);
987 goto send_reply;
988 }
989 } else if (!rstream &&
990 vstream->total_index_received == vstream->last_sent_index) {
991 /* Last index sent and stream closed */
992 viewer_index.status = htobe32(VIEWER_INDEX_HUP);
993 goto send_reply;
994 }
995
996 if (!vstream->ctf_trace->metadata_received ||
997 vstream->ctf_trace->metadata_received >
998 vstream->ctf_trace->metadata_sent) {
999 viewer_index.flags |= LTTNG_VIEWER_FLAG_NEW_METADATA;
1000 }
1001
1002 do {
1003 ret = read(vstream->index_read_fd, &packet_index,
1004 sizeof(packet_index));
1005 } while (ret < 0 && errno == EINTR);
1006 if (ret < sizeof(packet_index)) {
1007 PERROR("Relay reading index file");
1008 viewer_index.status = htobe32(VIEWER_INDEX_ERR);
1009 } else {
1010 viewer_index.status = htobe32(VIEWER_INDEX_OK);
1011 vstream->last_sent_index++;
1012 }
1013
1014 /*
1015 * Indexes are stored in big endian, no need to switch before sending.
1016 */
1017 viewer_index.offset = packet_index.offset;
1018 viewer_index.packet_size = packet_index.packet_size;
1019 viewer_index.content_size = packet_index.content_size;
1020 viewer_index.timestamp_begin = packet_index.timestamp_begin;
1021 viewer_index.timestamp_end = packet_index.timestamp_end;
1022 viewer_index.events_discarded = packet_index.events_discarded;
1023 viewer_index.stream_id = packet_index.stream_id;
1024
1025send_reply:
1026 viewer_index.flags = htobe32(viewer_index.flags);
1027 ret = cmd->sock->ops->sendmsg(cmd->sock, &viewer_index,
1028 sizeof(viewer_index), 0);
1029 if (ret < 0) {
1030 ERR("Relay index to viewer");
1031 goto end_unlock;
1032 }
1033
1034 DBG("Index %" PRIu64 "for stream %" PRIu64 "sent",
1035 vstream->last_sent_index, vstream->stream_handle);
1036
1037end_unlock:
1038 rcu_read_unlock();
1039
1040end_no_session:
1041end:
1042 return ret;
1043}
1044
1045/*
1046 * Send the next index for a stream
1047 *
1048 * Return 0 on success or else a negative value.
1049 */
1050static
92c6ca54 1051int viewer_get_packet(struct relay_command *cmd)
d3e2ba59
JD
1052{
1053 int ret, send_data = 0;
1054 char *data = NULL;
1055 uint32_t len = 0;
1056 ssize_t read_len;
1057 struct lttng_viewer_get_packet get_packet_info;
1058 struct lttng_viewer_trace_packet reply;
1059 struct relay_viewer_stream *stream;
1060
1061 assert(cmd);
d3e2ba59
JD
1062
1063 DBG2("Relay get data packet");
1064
1065 if (cmd->version_check_done == 0) {
1066 ERR("Trying to get packet before version check");
1067 ret = -1;
1068 goto end;
1069 }
1070
1071 ret = cmd->sock->ops->recvmsg(cmd->sock, &get_packet_info,
1072 sizeof(get_packet_info), 0);
1073 if (ret < 0 || ret != sizeof(get_packet_info)) {
1074 ret = -1;
1075 ERR("Relay didn't receive the whole packet");
1076 goto end;
1077 }
1078
1079 rcu_read_lock();
92c6ca54 1080 stream = live_find_viewer_stream_by_id(be64toh(get_packet_info.stream_id));
d3e2ba59
JD
1081 if (!stream) {
1082 goto error;
1083 }
1084 assert(stream->ctf_trace);
1085
1086 /*
1087 * First time we read this stream, we need open the tracefile, we should
1088 * only arrive here if an index has already been sent to the viewer, so the
1089 * tracefile must exist, if it does not it is a fatal error.
1090 */
1091 if (stream->read_fd < 0) {
1092 char fullpath[PATH_MAX];
1093
1094 ret = snprintf(fullpath, PATH_MAX, "%s/%s", stream->path_name,
1095 stream->channel_name);
1096 if (ret < 0) {
1097 goto error;
1098 }
1099 ret = open(fullpath, O_RDONLY);
1100 if (ret < 0) {
1101 PERROR("Relay opening trace file");
1102 goto error;
1103 }
1104 stream->read_fd = ret;
1105 }
1106
1107 memset(&reply, 0, sizeof(reply));
1108
1109 if (!stream->ctf_trace->metadata_received ||
1110 stream->ctf_trace->metadata_received >
1111 stream->ctf_trace->metadata_sent) {
1112 reply.status = htobe32(VIEWER_GET_PACKET_ERR);
1113 reply.flags |= LTTNG_VIEWER_FLAG_NEW_METADATA;
1114
1115 goto send_reply;
1116 }
1117
1118 len = be32toh(get_packet_info.len);
1119 data = zmalloc(len);
1120 if (!data) {
1121 PERROR("relay data zmalloc");
1122 goto error;
1123 }
1124
1125 ret = lseek(stream->read_fd, be64toh(get_packet_info.offset), SEEK_SET);
1126 if (ret < 0) {
1127 PERROR("lseek");
1128 goto error;
1129 }
1130 read_len = read(stream->read_fd, data, len);
1131 if (read_len < (ssize_t) len) {
1132 PERROR("Relay reading trace file, fd: %d, offset: %" PRIu64,
1133 stream->read_fd, be64toh(get_packet_info.offset));
1134 goto error;
1135 }
1136 reply.status = htobe32(VIEWER_GET_PACKET_OK);
1137 reply.len = htobe32(len);
1138 send_data = 1;
1139 goto send_reply;
1140
1141error:
1142 reply.status = htobe32(VIEWER_GET_PACKET_ERR);
1143
1144send_reply:
1145 reply.flags = htobe32(reply.flags);
1146 ret = cmd->sock->ops->sendmsg(cmd->sock, &reply, sizeof(reply), 0);
1147 if (ret < 0) {
1148 ERR("Relay data header to viewer");
1149 goto end_unlock;
1150 }
1151
1152 if (send_data) {
1153 ret = cmd->sock->ops->sendmsg(cmd->sock, data, len, 0);
1154 if (ret < 0) {
1155 ERR("Relay send data to viewer");
1156 goto end_unlock;
1157 }
1158 }
1159
1160 DBG("Sent %u bytes for stream %" PRIu64, len,
1161 be64toh(get_packet_info.stream_id));
1162
1163end_unlock:
1164 free(data);
1165 rcu_read_unlock();
1166
1167end:
1168 return ret;
1169}
1170
1171/*
1172 * Send the session's metadata
1173 *
1174 * Return 0 on success else a negative value.
1175 */
1176static
92c6ca54 1177int viewer_get_metadata(struct relay_command *cmd)
d3e2ba59
JD
1178{
1179 int ret = 0;
1180 ssize_t read_len;
1181 uint64_t len = 0;
1182 char *data = NULL;
1183 struct lttng_viewer_get_metadata request;
1184 struct lttng_viewer_metadata_packet reply;
1185 struct relay_viewer_stream *stream;
1186
1187 assert(cmd);
d3e2ba59
JD
1188
1189 DBG("Relay get metadata");
1190
1191 if (cmd->version_check_done == 0) {
1192 ERR("Trying to get metadata before version check");
1193 ret = -1;
1194 goto end;
1195 }
1196
1197 ret = cmd->sock->ops->recvmsg(cmd->sock, &request,
1198 sizeof(request), 0);
1199 if (ret < 0 || ret != sizeof(request)) {
1200 ret = -1;
1201 ERR("Relay didn't receive the whole packet");
1202 goto end;
1203 }
1204
1205 rcu_read_lock();
92c6ca54 1206 stream = live_find_viewer_stream_by_id(be64toh(request.stream_id));
d3e2ba59
JD
1207 if (!stream || !stream->metadata_flag) {
1208 ERR("Invalid metadata stream");
1209 goto error;
1210 }
1211 assert(stream->ctf_trace);
1212 assert(stream->ctf_trace->metadata_sent <=
1213 stream->ctf_trace->metadata_received);
1214
1215 len = stream->ctf_trace->metadata_received -
1216 stream->ctf_trace->metadata_sent;
1217 if (len == 0) {
1218 reply.status = htobe32(VIEWER_NO_NEW_METADATA);
1219 goto send_reply;
1220 }
1221
1222 /* first time, we open the metadata file */
1223 if (stream->read_fd < 0) {
1224 char fullpath[PATH_MAX];
1225
1226 ret = snprintf(fullpath, PATH_MAX, "%s/%s", stream->path_name,
1227 stream->channel_name);
1228 if (ret < 0) {
1229 goto error;
1230 }
1231 ret = open(fullpath, O_RDONLY);
1232 if (ret < 0) {
1233 PERROR("Relay opening metadata file");
1234 goto error;
1235 }
1236 stream->read_fd = ret;
1237 }
1238
1239 reply.len = htobe64(len);
1240 data = zmalloc(len);
1241 if (!data) {
1242 PERROR("viewer metadata zmalloc");
1243 goto error;
1244 }
1245
1246 read_len = read(stream->read_fd, data, len);
1247 if (read_len < (ssize_t) len) {
1248 PERROR("Relay reading metadata file");
1249 goto error;
1250 }
1251 stream->ctf_trace->metadata_sent += read_len;
1252 reply.status = htobe32(VIEWER_METADATA_OK);
1253 goto send_reply;
1254
1255error:
1256 reply.status = htobe32(VIEWER_METADATA_ERR);
1257
1258send_reply:
1259 ret = cmd->sock->ops->sendmsg(cmd->sock, &reply, sizeof(reply), 0);
1260 if (ret < 0) {
1261 ERR("Relay data header to viewer");
1262 goto end_unlock;
1263 }
1264
1265 if (len > 0) {
1266 ret = cmd->sock->ops->sendmsg(cmd->sock, data, len, 0);
1267 if (ret < 0) {
1268 ERR("Relay send data to viewer");
1269 goto end_unlock;
1270 }
1271 }
1272
1273 DBG("Sent %" PRIu64 " bytes of metadata for stream %" PRIu64, len,
1274 be64toh(request.stream_id));
1275
1276 DBG("Metadata sent");
1277
1278end_unlock:
1279 free(data);
1280 rcu_read_unlock();
1281end:
1282 return ret;
1283}
1284
1285/*
1286 * live_relay_unknown_command: send -1 if received unknown command
1287 */
1288static
1289void live_relay_unknown_command(struct relay_command *cmd)
1290{
1291 struct lttcomm_relayd_generic_reply reply;
1292 int ret;
1293
1294 reply.ret_code = htobe32(LTTNG_ERR_UNK);
1295 ret = cmd->sock->ops->sendmsg(cmd->sock, &reply,
1296 sizeof(struct lttcomm_relayd_generic_reply), 0);
1297 if (ret < 0) {
1298 ERR("Relay sending unknown command");
1299 }
1300}
1301
1302/*
1303 * Process the commands received on the control socket
1304 */
1305static
1306int process_control(struct lttng_viewer_cmd *recv_hdr,
92c6ca54 1307 struct relay_command *cmd, struct lttng_ht *sessions_ht)
d3e2ba59
JD
1308{
1309 int ret = 0;
1310
1311 switch (be32toh(recv_hdr->cmd)) {
1312 case VIEWER_CONNECT:
1313 ret = viewer_connect(cmd);
1314 break;
1315 case VIEWER_LIST_SESSIONS:
1316 ret = viewer_list_sessions(cmd, sessions_ht);
1317 break;
1318 case VIEWER_ATTACH_SESSION:
92c6ca54 1319 ret = viewer_attach_session(cmd, sessions_ht);
d3e2ba59
JD
1320 break;
1321 case VIEWER_GET_NEXT_INDEX:
92c6ca54 1322 ret = viewer_get_next_index(cmd, sessions_ht);
d3e2ba59
JD
1323 break;
1324 case VIEWER_GET_PACKET:
92c6ca54 1325 ret = viewer_get_packet(cmd);
d3e2ba59
JD
1326 break;
1327 case VIEWER_GET_METADATA:
92c6ca54 1328 ret = viewer_get_metadata(cmd);
d3e2ba59
JD
1329 break;
1330 default:
1331 ERR("Received unknown viewer command (%u)", be32toh(recv_hdr->cmd));
1332 live_relay_unknown_command(cmd);
1333 ret = -1;
1334 goto end;
1335 }
1336
1337end:
1338 return ret;
1339}
1340
1341static
1342void cleanup_poll_connection(struct lttng_poll_event *events, int pollfd)
1343{
1344 int ret;
1345
1346 assert(events);
1347
1348 lttng_poll_del(events, pollfd);
1349
1350 ret = close(pollfd);
1351 if (ret < 0) {
1352 ERR("Closing pollfd %d", pollfd);
1353 }
1354}
1355
1356/*
1357 * Create and add connection to the given hash table.
1358 *
1359 * Return poll add value or else -1 on error.
1360 */
1361static
1362int add_connection(int fd, struct lttng_poll_event *events,
1363 struct lttng_ht *relay_connections_ht)
1364{
1365 int ret;
1366 struct relay_command *relay_connection;
1367
1368 assert(events);
1369 assert(relay_connections_ht);
1370
1371 relay_connection = zmalloc(sizeof(struct relay_command));
1372 if (relay_connection == NULL) {
1373 PERROR("Relay command zmalloc");
1374 goto error;
1375 }
1376
1377 do {
1378 ret = read(fd, relay_connection, sizeof(*relay_connection));
1379 } while (ret < 0 && errno == EINTR);
1380 if (ret < 0 || ret < sizeof(*relay_connection)) {
1381 PERROR("read relay cmd pipe");
1382 goto error_read;
1383 }
1384
1385 lttng_ht_node_init_ulong(&relay_connection->sock_n,
1386 (unsigned long) relay_connection->sock->fd);
1387 rcu_read_lock();
1388 lttng_ht_add_unique_ulong(relay_connections_ht,
1389 &relay_connection->sock_n);
1390 rcu_read_unlock();
1391
1392 return lttng_poll_add(events, relay_connection->sock->fd,
1393 LPOLLIN | LPOLLRDHUP);
1394
1395error_read:
1396 free(relay_connection);
1397error:
1398 return -1;
1399}
1400
1401static
1402void deferred_free_connection(struct rcu_head *head)
1403{
1404 struct relay_command *relay_connection =
1405 caa_container_of(head, struct relay_command, rcu_node);
1406
1407 if (relay_connection->session &&
1408 relay_connection->session->viewer_attached > 0) {
1409 relay_connection->session->viewer_attached--;
1410 }
1411 lttcomm_destroy_sock(relay_connection->sock);
1412 free(relay_connection);
1413}
1414
1415static
1416void deferred_free_viewer_stream(struct rcu_head *head)
1417{
1418 struct relay_viewer_stream *stream =
1419 caa_container_of(head, struct relay_viewer_stream, rcu_node);
1420
1421 if (stream->ctf_trace) {
1422 uatomic_dec(&stream->ctf_trace->refcount);
1423 assert(uatomic_read(&stream->ctf_trace->refcount) >= 0);
1424 if (uatomic_read(&stream->ctf_trace->refcount) == 0) {
1425 DBG("Freeing ctf_trace %" PRIu64, stream->ctf_trace->id);
1426 free(stream->ctf_trace);
1427 }
1428 }
1429
1430 free(stream->path_name);
1431 free(stream->channel_name);
1432 free(stream);
1433}
1434
1435static
92c6ca54 1436void viewer_del_streams(struct relay_session *session)
d3e2ba59
JD
1437{
1438 int ret;
1439 struct relay_viewer_stream *stream;
1440 struct lttng_ht_node_u64 *node;
1441 struct lttng_ht_iter iter;
1442
d3e2ba59
JD
1443 assert(session);
1444
1445 rcu_read_lock();
1446 cds_lfht_for_each_entry(viewer_streams_ht->ht, &iter.iter, node, node) {
1447 node = lttng_ht_iter_get_node_u64(&iter);
1448 if (!node) {
1449 continue;
1450 }
1451
1452 stream = caa_container_of(node, struct relay_viewer_stream, stream_n);
1453 if (stream->session_id != session->id) {
1454 continue;
1455 }
1456
1457 if (stream->read_fd > 0) {
1458 ret = close(stream->read_fd);
1459 if (ret < 0) {
1460 PERROR("close read_fd");
1461 }
1462 }
1463 if (stream->index_read_fd > 0) {
1464 ret = close(stream->index_read_fd);
1465 if (ret < 0) {
1466 PERROR("close index_read_fd");
1467 }
1468 }
1469 if (stream->metadata_flag && stream->ctf_trace) {
1470 stream->ctf_trace->metadata_sent = 0;
1471 }
1472 ret = lttng_ht_del(viewer_streams_ht, &iter);
1473 assert(!ret);
1474 call_rcu(&stream->rcu_node, deferred_free_viewer_stream);
1475 }
1476 rcu_read_unlock();
1477}
1478
1479/*
1480 * Delete and free a connection.
1481 *
1482 * RCU read side lock MUST be acquired.
1483 */
1484static
1485void del_connection(struct lttng_ht *relay_connections_ht,
92c6ca54 1486 struct lttng_ht_iter *iter, struct relay_command *relay_connection)
d3e2ba59
JD
1487{
1488 int ret;
1489
1490 assert(relay_connections_ht);
1491 assert(iter);
1492 assert(relay_connection);
d3e2ba59
JD
1493
1494 ret = lttng_ht_del(relay_connections_ht, iter);
1495 assert(!ret);
1496
1497 if (relay_connection->session) {
92c6ca54 1498 viewer_del_streams(relay_connection->session);
d3e2ba59
JD
1499 }
1500
1501 call_rcu(&relay_connection->rcu_node, deferred_free_connection);
1502}
1503
1504/*
1505 * This thread does the actual work
1506 */
1507static
1508void *thread_worker(void *data)
1509{
1510 int ret, err = -1;
1511 uint32_t nb_fd;
1512 struct relay_command *relay_connection;
1513 struct lttng_poll_event events;
1514 struct lttng_ht *relay_connections_ht;
1515 struct lttng_ht_node_ulong *node;
1516 struct lttng_ht_iter iter;
1517 struct lttng_viewer_cmd recv_hdr;
1518 struct relay_local_data *relay_ctx = (struct relay_local_data *) data;
1519 struct lttng_ht *sessions_ht = relay_ctx->sessions_ht;
d3e2ba59
JD
1520
1521 DBG("[thread] Live viewer relay worker started");
1522
1523 rcu_register_thread();
1524
1525 /* table of connections indexed on socket */
1526 relay_connections_ht = lttng_ht_new(0, LTTNG_HT_TYPE_ULONG);
1527 if (!relay_connections_ht) {
1528 goto relay_connections_ht_error;
1529 }
1530
1531 ret = create_thread_poll_set(&events, 2);
1532 if (ret < 0) {
1533 goto error_poll_create;
1534 }
1535
1536 ret = lttng_poll_add(&events, live_relay_cmd_pipe[0], LPOLLIN | LPOLLRDHUP);
1537 if (ret < 0) {
1538 goto error;
1539 }
1540
1541restart:
1542 while (1) {
1543 int i;
1544
1545 /* Infinite blocking call, waiting for transmission */
1546 DBG3("Relayd live viewer worker thread polling...");
1547 ret = lttng_poll_wait(&events, -1);
1548 if (ret < 0) {
1549 /*
1550 * Restart interrupted system call.
1551 */
1552 if (errno == EINTR) {
1553 goto restart;
1554 }
1555 goto error;
1556 }
1557
1558 nb_fd = ret;
1559
1560 /*
1561 * Process control. The control connection is prioritised so we don't
1562 * starve it with high throughput tracing data on the data
1563 * connection.
1564 */
1565 for (i = 0; i < nb_fd; i++) {
1566 /* Fetch once the poll data */
1567 uint32_t revents = LTTNG_POLL_GETEV(&events, i);
1568 int pollfd = LTTNG_POLL_GETFD(&events, i);
1569
1570 /* Thread quit pipe has been closed. Killing thread. */
1571 ret = check_thread_quit_pipe(pollfd, revents);
1572 if (ret) {
1573 err = 0;
1574 goto exit;
1575 }
1576
1577 /* Inspect the relay cmd pipe for new connection */
1578 if (pollfd == live_relay_cmd_pipe[0]) {
1579 if (revents & (LPOLLERR | LPOLLHUP | LPOLLRDHUP)) {
1580 ERR("Relay live pipe error");
1581 goto error;
1582 } else if (revents & LPOLLIN) {
1583 DBG("Relay live viewer command received");
1584 ret = add_connection(live_relay_cmd_pipe[0],
1585 &events, relay_connections_ht);
1586 if (ret < 0) {
1587 goto error;
1588 }
1589 }
1590 } else if (revents) {
1591 rcu_read_lock();
1592 lttng_ht_lookup(relay_connections_ht,
1593 (void *)((unsigned long) pollfd), &iter);
1594 node = lttng_ht_iter_get_node_ulong(&iter);
1595 if (node == NULL) {
1596 DBG2("Relay viewer sock %d not found", pollfd);
1597 rcu_read_unlock();
1598 goto error;
1599 }
1600 relay_connection = caa_container_of(node, struct relay_command,
1601 sock_n);
1602
1603 if (revents & (LPOLLERR)) {
d3e2ba59
JD
1604 cleanup_poll_connection(&events, pollfd);
1605 del_connection(relay_connections_ht, &iter,
92c6ca54 1606 relay_connection);
d3e2ba59
JD
1607 } else if (revents & (LPOLLHUP | LPOLLRDHUP)) {
1608 DBG("Viewer socket %d hung up", pollfd);
1609 cleanup_poll_connection(&events, pollfd);
1610 del_connection(relay_connections_ht, &iter,
92c6ca54 1611 relay_connection);
d3e2ba59
JD
1612 } else if (revents & LPOLLIN) {
1613 ret = relay_connection->sock->ops->recvmsg(
1614 relay_connection->sock, &recv_hdr,
1615 sizeof(struct lttng_viewer_cmd),
1616 0);
1617 /* connection closed */
1618 if (ret <= 0) {
1619 cleanup_poll_connection(&events, pollfd);
1620 del_connection( relay_connections_ht, &iter,
92c6ca54 1621 relay_connection);
d3e2ba59
JD
1622 DBG("Viewer control connection closed with %d",
1623 pollfd);
1624 } else {
1625 if (relay_connection->session) {
1626 DBG2("Relay viewer worker receiving data for "
1627 "session: %" PRIu64,
1628 relay_connection->session->id);
1629 }
1630 ret = process_control(&recv_hdr, relay_connection,
92c6ca54 1631 sessions_ht);
d3e2ba59
JD
1632 if (ret < 0) {
1633 /* Clear the session on error. */
1634 cleanup_poll_connection(&events, pollfd);
1635 del_connection(relay_connections_ht, &iter,
92c6ca54 1636 relay_connection);
d3e2ba59
JD
1637 DBG("Viewer connection closed with %d", pollfd);
1638 }
1639 }
1640 }
1641 rcu_read_unlock();
1642 }
1643 }
1644 }
1645
1646exit:
1647error:
1648 lttng_poll_clean(&events);
1649
1650 /* empty the hash table and free the memory */
1651 rcu_read_lock();
1652 cds_lfht_for_each_entry(relay_connections_ht->ht, &iter.iter, node, node) {
1653 node = lttng_ht_iter_get_node_ulong(&iter);
1654 if (!node) {
1655 continue;
1656 }
1657
1658 relay_connection = caa_container_of(node, struct relay_command,
1659 sock_n);
92c6ca54 1660 del_connection(relay_connections_ht, &iter, relay_connection);
d3e2ba59
JD
1661 }
1662 rcu_read_unlock();
1663error_poll_create:
1664 lttng_ht_destroy(relay_connections_ht);
1665relay_connections_ht_error:
1666 /* Close relay cmd pipes */
1667 utils_close_pipe(live_relay_cmd_pipe);
1668 if (err) {
1669 DBG("Viewer worker thread exited with error");
1670 }
1671 DBG("Viewer worker thread cleanup complete");
1672 stop_threads();
1673 rcu_unregister_thread();
1674 return NULL;
1675}
1676
1677/*
1678 * Create the relay command pipe to wake thread_manage_apps.
1679 * Closed in cleanup().
1680 */
1681static int create_relay_cmd_pipe(void)
1682{
1683 int ret;
1684
1685 ret = utils_create_pipe_cloexec(live_relay_cmd_pipe);
1686
1687 return ret;
1688}
1689
1690void live_stop_threads()
1691{
1692 int ret;
1693 void *status;
1694
1695 stop_threads();
1696
1697 ret = pthread_join(live_listener_thread, &status);
1698 if (ret != 0) {
1699 PERROR("pthread_join live listener");
1700 goto error; /* join error, exit without cleanup */
1701 }
1702
1703 ret = pthread_join(live_worker_thread, &status);
1704 if (ret != 0) {
1705 PERROR("pthread_join live worker");
1706 goto error; /* join error, exit without cleanup */
1707 }
1708
1709 ret = pthread_join(live_dispatcher_thread, &status);
1710 if (ret != 0) {
1711 PERROR("pthread_join live dispatcher");
1712 goto error; /* join error, exit without cleanup */
1713 }
1714
1715 cleanup();
1716
1717error:
1718 return;
1719}
1720
1721/*
1722 * main
1723 */
1724int live_start_threads(struct lttng_uri *uri,
1725 struct relay_local_data *relay_ctx)
1726{
1727 int ret = 0;
1728 void *status;
1729 int is_root;
1730
1731 assert(uri);
1732 live_uri = uri;
1733
1734 /* Create thread quit pipe */
1735 if ((ret = init_thread_quit_pipe()) < 0) {
1736 goto error;
1737 }
1738
1739 /* Check if daemon is UID = 0 */
1740 is_root = !getuid();
1741
1742 if (!is_root) {
1743 if (live_uri->port < 1024) {
1744 ERR("Need to be root to use ports < 1024");
1745 ret = -1;
1746 goto exit;
1747 }
1748 }
1749
1750 /* Setup the thread apps communication pipe. */
1751 if ((ret = create_relay_cmd_pipe()) < 0) {
1752 goto exit;
1753 }
1754
1755 /* Init relay command queue. */
1756 cds_wfq_init(&viewer_cmd_queue.queue);
1757
1758 /* Set up max poll set size */
1759 lttng_poll_set_max_size();
1760
1761 /* Setup the dispatcher thread */
1762 ret = pthread_create(&live_dispatcher_thread, NULL,
1763 thread_dispatcher, (void *) NULL);
1764 if (ret != 0) {
1765 PERROR("pthread_create viewer dispatcher");
1766 goto exit_dispatcher;
1767 }
1768
1769 /* Setup the worker thread */
1770 ret = pthread_create(&live_worker_thread, NULL,
1771 thread_worker, relay_ctx);
1772 if (ret != 0) {
1773 PERROR("pthread_create viewer worker");
1774 goto exit_worker;
1775 }
1776
1777 /* Setup the listener thread */
1778 ret = pthread_create(&live_listener_thread, NULL,
1779 thread_listener, (void *) NULL);
1780 if (ret != 0) {
1781 PERROR("pthread_create viewer listener");
1782 goto exit_listener;
1783 }
1784
1785 ret = 0;
1786 goto end;
1787
1788exit_listener:
1789 ret = pthread_join(live_listener_thread, &status);
1790 if (ret != 0) {
1791 PERROR("pthread_join live listener");
1792 goto error; /* join error, exit without cleanup */
1793 }
1794
1795exit_worker:
1796 ret = pthread_join(live_worker_thread, &status);
1797 if (ret != 0) {
1798 PERROR("pthread_join live worker");
1799 goto error; /* join error, exit without cleanup */
1800 }
1801
1802exit_dispatcher:
1803 ret = pthread_join(live_dispatcher_thread, &status);
1804 if (ret != 0) {
1805 PERROR("pthread_join live dispatcher");
1806 goto error; /* join error, exit without cleanup */
1807 }
1808
1809exit:
1810 cleanup();
1811
1812end:
1813error:
1814 return ret;
1815}
This page took 0.087542 seconds and 4 git commands to generate.