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