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