Cleanup relayd live comment
[lttng-tools.git] / src / bin / lttng-relayd / live.c
CommitLineData
d3e2ba59
JD
1/*
2 * Copyright (C) 2013 - Julien Desfossez <jdesfossez@efficios.com>
3 * David Goulet <dgoulet@efficios.com>
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License, version 2 only,
7 * as published by the Free Software Foundation.
8 *
9 * This program is distributed in the hope that it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
12 * more details.
13 *
14 * You should have received a copy of the GNU General Public License along
15 * with this program; if not, write to the Free Software Foundation, Inc.,
16 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
17 */
18
19#define _GNU_SOURCE
20#include <getopt.h>
21#include <grp.h>
22#include <limits.h>
23#include <pthread.h>
24#include <signal.h>
25#include <stdio.h>
26#include <stdlib.h>
27#include <string.h>
28#include <sys/mman.h>
29#include <sys/mount.h>
30#include <sys/resource.h>
31#include <sys/socket.h>
32#include <sys/stat.h>
33#include <sys/types.h>
34#include <sys/wait.h>
35#include <inttypes.h>
36#include <urcu/futex.h>
37#include <urcu/uatomic.h>
38#include <unistd.h>
39#include <fcntl.h>
40#include <config.h>
41
42#include <lttng/lttng.h>
43#include <common/common.h>
44#include <common/compat/poll.h>
45#include <common/compat/socket.h>
46#include <common/defaults.h>
47#include <common/futex.h>
48#include <common/sessiond-comm/sessiond-comm.h>
49#include <common/sessiond-comm/inet.h>
50#include <common/sessiond-comm/relayd.h>
51#include <common/uri.h>
52#include <common/utils.h>
53
54#include "cmd.h"
55#include "live.h"
56#include "lttng-relayd.h"
57#include "lttng-viewer.h"
58#include "utils.h"
eea7556c 59#include "health-relayd.h"
d3e2ba59
JD
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
d3e2ba59
JD
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{
6cd525e8 109 ssize_t ret;
d3e2ba59 110
6cd525e8
MD
111 ret = lttng_write(wpipe, "!", 1);
112 if (ret < 1) {
d3e2ba59
JD
113 PERROR("write poll pipe");
114 }
115
6cd525e8 116 return (int) ret;
d3e2ba59
JD
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
d3e2ba59
JD
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 */
c7759e6a 158 ret = lttng_poll_add(events, live_thread_quit_pipe[0], LPOLLIN | LPOLLERR);
d3e2ba59
JD
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
eea7556c
MD
239 health_register(health_relayd, HEALTH_RELAYD_TYPE_LIVE_LISTENER);
240
241 health_code_update();
242
d3e2ba59
JD
243 live_control_sock = init_socket(live_uri);
244 if (!live_control_sock) {
245 goto error_sock_control;
246 }
247
fb4d42ab 248 /* Pass 2 as size here for the thread quit pipe and control sockets. */
d3e2ba59
JD
249 ret = create_thread_poll_set(&events, 2);
250 if (ret < 0) {
251 goto error_create_poll;
252 }
253
254 /* Add the control socket */
255 ret = lttng_poll_add(&events, live_control_sock->fd, LPOLLIN | LPOLLRDHUP);
256 if (ret < 0) {
257 goto error_poll_add;
258 }
259
3fd27398
MD
260 lttng_relay_notify_ready();
261
d3e2ba59 262 while (1) {
eea7556c
MD
263 health_code_update();
264
d3e2ba59
JD
265 DBG("Listener accepting live viewers connections");
266
267restart:
eea7556c 268 health_poll_entry();
d3e2ba59 269 ret = lttng_poll_wait(&events, -1);
eea7556c 270 health_poll_exit();
d3e2ba59
JD
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++) {
eea7556c
MD
284 health_code_update();
285
d3e2ba59
JD
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) {
eea7556c 360 health_error();
d3e2ba59
JD
361 DBG("Live viewer listener thread exited with error");
362 }
eea7556c 363 health_unregister(health_relayd);
d3e2ba59
JD
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{
6cd525e8
MD
375 int err = -1;
376 ssize_t ret;
d3e2ba59
JD
377 struct cds_wfq_node *node;
378 struct relay_command *relay_cmd = NULL;
379
380 DBG("[thread] Live viewer relay dispatcher started");
381
eea7556c
MD
382 health_register(health_relayd, HEALTH_RELAYD_TYPE_LIVE_DISPATCHER);
383
384 health_code_update();
385
d3e2ba59 386 while (!CMM_LOAD_SHARED(live_dispatch_thread_exit)) {
eea7556c
MD
387 health_code_update();
388
d3e2ba59
JD
389 /* Atomically prepare the queue futex */
390 futex_nto1_prepare(&viewer_cmd_queue.futex);
391
392 do {
eea7556c
MD
393 health_code_update();
394
d3e2ba59
JD
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 */
6cd525e8
MD
413 ret = lttng_write(live_relay_cmd_pipe[1], relay_cmd,
414 sizeof(*relay_cmd));
d3e2ba59 415 free(relay_cmd);
6cd525e8 416 if (ret < sizeof(struct relay_command)) {
d3e2ba59
JD
417 PERROR("write cmd pipe");
418 goto error;
419 }
420 } while (node != NULL);
421
422 /* Futex wait on queue. Blocking call on futex() */
eea7556c 423 health_poll_entry();
d3e2ba59 424 futex_nto1_wait(&viewer_cmd_queue.futex);
eea7556c 425 health_poll_exit();
d3e2ba59
JD
426 }
427
eea7556c
MD
428 /* Normal exit, no error */
429 err = 0;
430
d3e2ba59 431error:
eea7556c
MD
432 if (err) {
433 health_error();
434 ERR("Health error occurred in %s", __func__);
435 }
436 health_unregister(health_relayd);
d3e2ba59
JD
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
eea7556c
MD
457 health_code_update();
458
d3e2ba59
JD
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
eea7556c
MD
472 health_code_update();
473
d3e2ba59
JD
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));
72180669 481 ret = -1;
d3e2ba59
JD
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 }
eea7556c
MD
508
509 health_code_update();
510
d3e2ba59
JD
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
eea7556c
MD
517 health_code_update();
518
d3e2ba59
JD
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
eea7556c
MD
556 health_code_update();
557
d3e2ba59
JD
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
eea7556c
MD
565 health_code_update();
566
d3e2ba59 567 cds_lfht_for_each_entry(sessions_ht->ht, &iter.iter, node, node) {
eea7556c
MD
568 health_code_update();
569
d3e2ba59
JD
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);
87b576ec 583 send_session.streams = htobe32(session->stream_count);
d3e2ba59 584
eea7556c
MD
585 health_code_update();
586
d3e2ba59
JD
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 }
eea7556c
MD
594 health_code_update();
595
d3e2ba59
JD
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
0e6830aa
JD
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];
50adc264 617 struct ctf_packet_index_file_hdr hdr;
0e6830aa 618
6b6b9a5a
JD
619 if (stream->tracefile_count > 0) {
620 ret = snprintf(fullpath, sizeof(fullpath), "%s/" DEFAULT_INDEX_DIR "/%s_%"
621 PRIu64 DEFAULT_INDEX_FILE_SUFFIX, stream->path_name,
622 stream->channel_name, stream->tracefile_count_current);
623 } else {
624 ret = snprintf(fullpath, sizeof(fullpath), "%s/" DEFAULT_INDEX_DIR "/%s"
625 DEFAULT_INDEX_FILE_SUFFIX, stream->path_name,
626 stream->channel_name);
0e6830aa 627 }
0e6830aa
JD
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
6cd525e8
MD
647 ret = lttng_read(stream->index_read_fd, &hdr, sizeof(hdr));
648 if (ret < sizeof(hdr)) {
0e6830aa
JD
649 PERROR("Reading index header");
650 goto error;
651 }
50adc264 652 if (be32toh(hdr.magic) != CTF_INDEX_MAGIC) {
0e6830aa
JD
653 ERR("Invalid header magic");
654 ret = -1;
655 goto error;
656 }
50adc264
JD
657 if (be32toh(hdr.index_major) != CTF_INDEX_MAJOR ||
658 be32toh(hdr.index_minor) != CTF_INDEX_MINOR) {
0e6830aa
JD
659 ERR("Invalid header version");
660 ret = -1;
661 goto error;
662 }
663 ret = 0;
664
665error:
0e6830aa
JD
666 return ret;
667}
668
d3e2ba59
JD
669/*
670 * Allocate and init a new viewer_stream.
671 *
672 * Copies the values from the stream passed in parameter and insert the new
673 * stream in the viewer_streams_ht.
674 *
675 * MUST be called with rcu_read_lock held.
676 *
677 * Returns 0 on success or a negative value on error.
678 */
679static
0e6830aa 680int init_viewer_stream(struct relay_stream *stream, int seek_last)
d3e2ba59
JD
681{
682 int ret;
683 struct relay_viewer_stream *viewer_stream;
684
685 assert(stream);
d3e2ba59
JD
686
687 viewer_stream = zmalloc(sizeof(*viewer_stream));
688 if (!viewer_stream) {
689 PERROR("relay viewer stream zmalloc");
690 ret = -1;
691 goto error;
692 }
d3e2ba59
JD
693 viewer_stream->session_id = stream->session->id;
694 viewer_stream->stream_handle = stream->stream_handle;
695 viewer_stream->path_name = strndup(stream->path_name,
696 LTTNG_VIEWER_PATH_MAX);
697 viewer_stream->channel_name = strndup(stream->channel_name,
698 LTTNG_VIEWER_NAME_MAX);
d3e2ba59
JD
699 viewer_stream->tracefile_count = stream->tracefile_count;
700 viewer_stream->metadata_flag = stream->metadata_flag;
a020f610 701 viewer_stream->tracefile_count_last = -1ULL;
6b6b9a5a
JD
702 if (seek_last) {
703 viewer_stream->tracefile_count_current =
704 stream->tracefile_count_current;
705 } else {
706 viewer_stream->tracefile_count_current =
707 stream->oldest_tracefile_id;
708 }
709
6b6b9a5a 710 viewer_stream->ctf_trace = stream->ctf_trace;
157df586
JD
711 if (viewer_stream->metadata_flag) {
712 viewer_stream->ctf_trace->viewer_metadata_stream =
713 viewer_stream;
714 }
6b6b9a5a 715 uatomic_inc(&viewer_stream->ctf_trace->refcount);
d3e2ba59 716
6b6b9a5a
JD
717 lttng_ht_node_init_u64(&viewer_stream->stream_n, stream->stream_handle);
718 lttng_ht_add_unique_u64(viewer_streams_ht, &viewer_stream->stream_n);
719
720 viewer_stream->index_read_fd = -1;
721 viewer_stream->read_fd = -1;
722
723 /*
724 * This is to avoid a race between the initialization of this object and
725 * the close of the given stream. If the stream is unable to find this
726 * viewer stream when closing, this copy will at least take the latest
727 * value.
728 * We also need that for the seek_last.
729 */
730 viewer_stream->total_index_received = stream->total_index_received;
731
732 /*
733 * If we never received an index for the current stream, delay
734 * the opening of the index, otherwise open it right now.
735 */
736 if (viewer_stream->tracefile_count_current ==
737 stream->tracefile_count_current &&
738 viewer_stream->total_index_received == 0) {
739 viewer_stream->index_read_fd = -1;
740 } else {
0e6830aa
JD
741 ret = open_index(viewer_stream);
742 if (ret < 0) {
743 goto error;
744 }
6b6b9a5a
JD
745 }
746
747 if (seek_last && viewer_stream->index_read_fd > 0) {
0e6830aa
JD
748 ret = lseek(viewer_stream->index_read_fd,
749 viewer_stream->total_index_received *
50adc264 750 sizeof(struct ctf_packet_index),
0e6830aa
JD
751 SEEK_CUR);
752 if (ret < 0) {
753 goto error;
754 }
755 viewer_stream->last_sent_index =
756 viewer_stream->total_index_received;
757 }
758
6b6b9a5a
JD
759 ret = 0;
760
761error:
762 return ret;
763}
764
765/*
766 * Rotate a stream to the next tracefile.
767 *
a020f610 768 * Returns 0 on success, 1 on EOF, a negative value on error.
6b6b9a5a
JD
769 */
770static
771int rotate_viewer_stream(struct relay_viewer_stream *viewer_stream,
772 struct relay_stream *stream)
773{
774 int ret;
775 uint64_t tracefile_id;
776
777 assert(viewer_stream);
778
779 tracefile_id = (viewer_stream->tracefile_count_current + 1) %
780 viewer_stream->tracefile_count;
a020f610
JD
781 /*
782 * Detect the last tracefile to open.
783 */
784 if (viewer_stream->tracefile_count_last != -1ULL &&
785 viewer_stream->tracefile_count_last ==
786 viewer_stream->tracefile_count_current) {
787 ret = 1;
788 goto end;
789 }
d3e2ba59 790
6b6b9a5a
JD
791 if (stream) {
792 pthread_mutex_lock(&stream->viewer_stream_rotation_lock);
793 }
d3e2ba59 794 /*
6b6b9a5a
JD
795 * The writer and the reader are not working in the same
796 * tracefile, we can read up to EOF, we don't care about the
797 * total_index_received.
d3e2ba59 798 */
6b6b9a5a
JD
799 if (!stream || (stream->tracefile_count_current != tracefile_id)) {
800 viewer_stream->close_write_flag = 1;
801 } else {
802 /*
803 * We are opening a file that is still open in write, make
804 * sure we limit our reading to the number of indexes
805 * received.
806 */
807 viewer_stream->close_write_flag = 0;
808 if (stream) {
809 viewer_stream->total_index_received =
810 stream->total_index_received;
811 }
812 }
813 viewer_stream->tracefile_count_current = tracefile_id;
d3e2ba59 814
d1d66061
JD
815 ret = close(viewer_stream->index_read_fd);
816 if (ret < 0) {
817 PERROR("close index file %d",
818 viewer_stream->index_read_fd);
6b6b9a5a 819 }
d1d66061
JD
820 viewer_stream->index_read_fd = -1;
821 ret = close(viewer_stream->read_fd);
822 if (ret < 0) {
823 PERROR("close tracefile %d",
824 viewer_stream->read_fd);
825 }
826 viewer_stream->read_fd = -1;
827
828 pthread_mutex_lock(&viewer_stream->overwrite_lock);
829 viewer_stream->abort_flag = 0;
830 pthread_mutex_unlock(&viewer_stream->overwrite_lock);
6b6b9a5a 831
cef0f7d5 832 viewer_stream->index_read_fd = -1;
6b6b9a5a
JD
833 viewer_stream->read_fd = -1;
834
cef0f7d5
JD
835 if (stream) {
836 pthread_mutex_unlock(&stream->viewer_stream_rotation_lock);
837 }
6b6b9a5a
JD
838 ret = open_index(viewer_stream);
839 if (ret < 0) {
840 goto error;
841 }
d3e2ba59
JD
842
843 ret = 0;
844
a020f610 845end:
d3e2ba59
JD
846error:
847 return ret;
848}
849
850/*
851 * Send the viewer the list of current sessions.
852 */
853static
854int viewer_attach_session(struct relay_command *cmd,
92c6ca54 855 struct lttng_ht *sessions_ht)
d3e2ba59 856{
a4baae1b
JD
857 int ret, send_streams = 0;
858 uint32_t nb_streams = 0, nb_streams_ready = 0;
d3e2ba59
JD
859 struct lttng_viewer_attach_session_request request;
860 struct lttng_viewer_attach_session_response response;
861 struct lttng_viewer_stream send_stream;
862 struct relay_stream *stream;
863 struct relay_viewer_stream *viewer_stream;
864 struct lttng_ht_node_ulong *node;
865 struct lttng_ht_node_u64 *node64;
866 struct lttng_ht_iter iter;
867 struct relay_session *session;
0e6830aa 868 int seek_last = 0;
d3e2ba59
JD
869
870 assert(cmd);
871 assert(sessions_ht);
d3e2ba59
JD
872
873 DBG("Attach session received");
874
875 if (cmd->version_check_done == 0) {
876 ERR("Trying to attach session before version check");
877 ret = -1;
878 goto end_no_session;
879 }
880
eea7556c
MD
881 health_code_update();
882
d3e2ba59
JD
883 ret = cmd->sock->ops->recvmsg(cmd->sock, &request, sizeof(request), 0);
884 if (ret < 0 || ret != sizeof(request)) {
885 if (ret == 0) {
886 /* Orderly shutdown. Not necessary to print an error. */
887 DBG("Socket %d did an orderly shutdown", cmd->sock->fd);
888 } else {
889 ERR("Relay failed to receive the attach parameters.");
890 }
891 ret = -1;
892 goto error;
893 }
894
eea7556c
MD
895 health_code_update();
896
d3e2ba59
JD
897 rcu_read_lock();
898 lttng_ht_lookup(sessions_ht,
899 (void *)((unsigned long) be64toh(request.session_id)), &iter);
900 node = lttng_ht_iter_get_node_ulong(&iter);
901 if (node == NULL) {
902 DBG("Relay session %" PRIu64 " not found",
903 be64toh(request.session_id));
904 response.status = htobe32(VIEWER_ATTACH_UNK);
905 goto send_reply;
906 }
907
908 session = caa_container_of(node, struct relay_session, session_n);
b92fdc2b 909 if (cmd->session_id == session->id) {
d3e2ba59
JD
910 /* Same viewer already attached, just send the stream list. */
911 send_streams = 1;
912 response.status = htobe32(VIEWER_ATTACH_OK);
913 } else if (session->viewer_attached != 0) {
914 DBG("Already a viewer attached");
915 response.status = htobe32(VIEWER_ATTACH_ALREADY);
916 goto send_reply;
917 } else if (session->live_timer == 0) {
918 DBG("Not live session");
919 response.status = htobe32(VIEWER_ATTACH_NOT_LIVE);
920 goto send_reply;
921 } else {
922 session->viewer_attached++;
923 send_streams = 1;
924 response.status = htobe32(VIEWER_ATTACH_OK);
b92fdc2b 925 cmd->session_id = session->id;
d3e2ba59
JD
926 cmd->session = session;
927 }
928
929 switch (be32toh(request.seek)) {
930 case VIEWER_SEEK_BEGINNING:
931 /* Default behaviour. */
932 break;
933 case VIEWER_SEEK_LAST:
0e6830aa 934 seek_last = 1;
d3e2ba59
JD
935 break;
936 default:
937 ERR("Wrong seek parameter");
938 response.status = htobe32(VIEWER_ATTACH_SEEK_ERR);
939 send_streams = 0;
940 goto send_reply;
941 }
942
943 if (send_streams) {
944 /* We should only be there if we have a session to attach to. */
945 assert(session);
946
947 /*
948 * Fill the viewer_streams_ht to count the number of streams
949 * ready to be sent and avoid concurrency issues on the
950 * relay_streams_ht and don't rely on a total session stream count.
951 */
952 cds_lfht_for_each_entry(relay_streams_ht->ht, &iter.iter, node, node) {
953 struct relay_viewer_stream *vstream;
954
eea7556c
MD
955 health_code_update();
956
d3e2ba59
JD
957 node = lttng_ht_iter_get_node_ulong(&iter);
958 if (!node) {
959 continue;
960 }
961 stream = caa_container_of(node, struct relay_stream, stream_n);
962 if (stream->session != cmd->session) {
963 continue;
964 }
a4baae1b 965 nb_streams++;
d3e2ba59
JD
966
967 /*
a4baae1b
JD
968 * Don't send streams with no ctf_trace, they are not
969 * ready to be read.
d3e2ba59 970 */
a4baae1b 971 if (!stream->ctf_trace || !stream->viewer_ready) {
d3e2ba59
JD
972 continue;
973 }
a4baae1b 974 nb_streams_ready++;
d3e2ba59 975
92c6ca54 976 vstream = live_find_viewer_stream_by_id(stream->stream_handle);
d3e2ba59 977 if (!vstream) {
0e6830aa 978 ret = init_viewer_stream(stream, seek_last);
d3e2ba59
JD
979 if (ret < 0) {
980 goto end_unlock;
981 }
982 }
a4baae1b
JD
983 }
984
985 /* We must have the same amount of existing stream and ready stream. */
986 if (nb_streams != nb_streams_ready) {
987 nb_streams = 0;
d3e2ba59
JD
988 }
989 response.streams_count = htobe32(nb_streams);
990 }
991
992send_reply:
eea7556c 993 health_code_update();
d3e2ba59
JD
994 ret = cmd->sock->ops->sendmsg(cmd->sock, &response, sizeof(response), 0);
995 if (ret < 0) {
996 ERR("Relay sending viewer attach response");
997 goto end_unlock;
998 }
eea7556c 999 health_code_update();
d3e2ba59
JD
1000
1001 /*
157df586 1002 * Unknown or empty session, just return gracefully, the viewer knows what
d3e2ba59
JD
1003 * is happening.
1004 */
157df586 1005 if (!send_streams || !nb_streams) {
d3e2ba59
JD
1006 ret = 0;
1007 goto end_unlock;
1008 }
1009
1010 /* We should only be there if we have a session to attach to. */
1011 assert(session);
1012 cds_lfht_for_each_entry(viewer_streams_ht->ht, &iter.iter, node, node) {
eea7556c
MD
1013 health_code_update();
1014
d3e2ba59
JD
1015 node64 = lttng_ht_iter_get_node_u64(&iter);
1016 if (!node64) {
1017 continue;
1018 }
1019 viewer_stream = caa_container_of(node64, struct relay_viewer_stream,
1020 stream_n);
1021 if (viewer_stream->session_id != cmd->session->id) {
1022 continue;
1023 }
1024
1025 send_stream.id = htobe64(viewer_stream->stream_handle);
1026 send_stream.ctf_trace_id = htobe64(viewer_stream->ctf_trace->id);
1027 send_stream.metadata_flag = htobe32(viewer_stream->metadata_flag);
1028 strncpy(send_stream.path_name, viewer_stream->path_name,
1029 sizeof(send_stream.path_name));
1030 strncpy(send_stream.channel_name, viewer_stream->channel_name,
1031 sizeof(send_stream.channel_name));
1032
1033 ret = cmd->sock->ops->sendmsg(cmd->sock, &send_stream,
1034 sizeof(send_stream), 0);
1035 if (ret < 0) {
1036 ERR("Relay sending stream %" PRIu64, viewer_stream->stream_handle);
1037 goto end_unlock;
1038 }
1039 DBG("Sent stream %" PRIu64 " to viewer", viewer_stream->stream_handle);
1040 }
1041 ret = 0;
1042
1043end_unlock:
1044 rcu_read_unlock();
1045end_no_session:
1046error:
1047 return ret;
1048}
1049
d3e2ba59
JD
1050/*
1051 * Get viewer stream from stream id.
1052 *
1053 * RCU read side lock MUST be acquired.
1054 */
92c6ca54 1055struct relay_viewer_stream *live_find_viewer_stream_by_id(uint64_t stream_id)
d3e2ba59
JD
1056{
1057 struct lttng_ht_node_u64 *node;
1058 struct lttng_ht_iter iter;
1059 struct relay_viewer_stream *stream = NULL;
1060
d3e2ba59
JD
1061 lttng_ht_lookup(viewer_streams_ht, &stream_id, &iter);
1062 node = lttng_ht_iter_get_node_u64(&iter);
1063 if (node == NULL) {
1064 DBG("Relay viewer stream %" PRIu64 " not found", stream_id);
1065 goto end;
1066 }
1067 stream = caa_container_of(node, struct relay_viewer_stream, stream_n);
1068
1069end:
1070 return stream;
1071}
1072
157df586
JD
1073static
1074void deferred_free_viewer_stream(struct rcu_head *head)
1075{
1076 struct relay_viewer_stream *stream =
1077 caa_container_of(head, struct relay_viewer_stream, rcu_node);
1078
1079 free(stream->path_name);
1080 free(stream->channel_name);
1081 free(stream);
1082}
1083
1084static
1085void delete_viewer_stream(struct relay_viewer_stream *vstream)
1086{
1087 int delret;
1088 struct lttng_ht_iter iter;
1089
1090 iter.iter.node = &vstream->stream_n.node;
1091 delret = lttng_ht_del(viewer_streams_ht, &iter);
1092 assert(!delret);
1093}
1094
1095static
1096void destroy_viewer_stream(struct relay_viewer_stream *vstream)
1097{
1098 unsigned long ret_ref;
1099 int ret;
1100
1101 assert(vstream);
1102 ret_ref = uatomic_add_return(&vstream->ctf_trace->refcount, -1);
1103 assert(ret_ref >= 0);
1104
1105 if (vstream->read_fd >= 0) {
1106 ret = close(vstream->read_fd);
1107 if (ret < 0) {
1108 PERROR("close read_fd");
1109 }
1110 }
1111 if (vstream->index_read_fd >= 0) {
1112 ret = close(vstream->index_read_fd);
1113 if (ret < 0) {
1114 PERROR("close index_read_fd");
1115 }
1116 }
1117
1118 /*
1119 * If the only stream left in the HT is the metadata stream,
1120 * we need to remove it because we won't detect a EOF for this
1121 * stream.
1122 */
efe12ccc 1123 if (ret_ref == 1 && vstream->ctf_trace->viewer_metadata_stream) {
26f003c3 1124 delete_viewer_stream(vstream->ctf_trace->viewer_metadata_stream);
157df586
JD
1125 destroy_viewer_stream(vstream->ctf_trace->viewer_metadata_stream);
1126 vstream->ctf_trace->metadata_stream = NULL;
1127 DBG("Freeing ctf_trace %" PRIu64, vstream->ctf_trace->id);
1128 /*
1129 * The streaming-side is already closed and we can't receive a new
1130 * stream concurrently at this point (since the session is being
1131 * destroyed), so when we detect the refcount equals 0, we are the
1132 * only owners of the ctf_trace and we can free it ourself.
1133 */
1134 free(vstream->ctf_trace);
1135 }
1136
1137 call_rcu(&vstream->rcu_node, deferred_free_viewer_stream);
1138}
1139
d3e2ba59
JD
1140/*
1141 * Send the next index for a stream.
1142 *
1143 * Return 0 on success or else a negative value.
1144 */
1145static
1146int viewer_get_next_index(struct relay_command *cmd,
92c6ca54 1147 struct lttng_ht *sessions_ht)
d3e2ba59
JD
1148{
1149 int ret;
1150 struct lttng_viewer_get_next_index request_index;
1151 struct lttng_viewer_index viewer_index;
50adc264 1152 struct ctf_packet_index packet_index;
d3e2ba59
JD
1153 struct relay_viewer_stream *vstream;
1154 struct relay_stream *rstream;
1155
1156 assert(cmd);
d3e2ba59
JD
1157 assert(sessions_ht);
1158
1159 DBG("Viewer get next index");
1160
1161 if (cmd->version_check_done == 0) {
1162 ERR("Trying to request index before version check");
1163 ret = -1;
1164 goto end_no_session;
1165 }
1166
eea7556c 1167 health_code_update();
d3e2ba59
JD
1168 ret = cmd->sock->ops->recvmsg(cmd->sock, &request_index,
1169 sizeof(request_index), 0);
1170 if (ret < 0 || ret != sizeof(request_index)) {
1171 ret = -1;
1172 ERR("Relay didn't receive the whole packet");
1173 goto end;
1174 }
eea7556c 1175 health_code_update();
d3e2ba59
JD
1176
1177 rcu_read_lock();
92c6ca54 1178 vstream = live_find_viewer_stream_by_id(be64toh(request_index.stream_id));
d3e2ba59
JD
1179 if (!vstream) {
1180 ret = -1;
1181 goto end_unlock;
1182 }
1183
1184 memset(&viewer_index, 0, sizeof(viewer_index));
1185
1186 /*
1187 * The viewer should not ask for index on metadata stream.
1188 */
1189 if (vstream->metadata_flag) {
1190 viewer_index.status = htobe32(VIEWER_INDEX_HUP);
1191 goto send_reply;
1192 }
1193
1194 /* First time, we open the index file */
1195 if (vstream->index_read_fd < 0) {
1196 ret = open_index(vstream);
0e6830aa 1197 if (ret == -ENOENT) {
d3e2ba59
JD
1198 /*
1199 * The index is created only when the first data packet arrives, it
1200 * might not be ready at the beginning of the session
1201 */
1202 viewer_index.status = htobe32(VIEWER_INDEX_RETRY);
1203 goto send_reply;
1204 } else if (ret < 0) {
1205 viewer_index.status = htobe32(VIEWER_INDEX_ERR);
1206 goto send_reply;
1207 }
1208 }
1209
1210 rstream = relay_stream_find_by_id(vstream->stream_handle);
1211 if (rstream) {
6b6b9a5a
JD
1212 if (vstream->abort_flag) {
1213 /* Rotate on abort (overwrite). */
1214 DBG("Viewer rotate because of overwrite");
1215 ret = rotate_viewer_stream(vstream, rstream);
1216 if (ret < 0) {
1217 goto end_unlock;
a020f610
JD
1218 } else if (ret == 1) {
1219 viewer_index.status = htobe32(VIEWER_INDEX_HUP);
157df586
JD
1220 delete_viewer_stream(vstream);
1221 destroy_viewer_stream(vstream);
a020f610 1222 goto send_reply;
6b6b9a5a
JD
1223 }
1224 }
6b6b9a5a 1225 pthread_mutex_lock(&rstream->viewer_stream_rotation_lock);
cef0f7d5
JD
1226 if (rstream->tracefile_count_current == vstream->tracefile_count_current) {
1227 if (rstream->beacon_ts_end != -1ULL &&
1228 vstream->last_sent_index == rstream->total_index_received) {
1229 viewer_index.status = htobe32(VIEWER_INDEX_INACTIVE);
1230 viewer_index.timestamp_end = htobe64(rstream->beacon_ts_end);
1231 pthread_mutex_unlock(&rstream->viewer_stream_rotation_lock);
1232 goto send_reply;
1233 /*
1234 * Reader and writer are working in the same tracefile, so we care
1235 * about the number of index received and sent. Otherwise, we read
1236 * up to EOF.
1237 */
1238 } else if (rstream->total_index_received <= vstream->last_sent_index
1239 && !vstream->close_write_flag) {
1240 pthread_mutex_unlock(&rstream->viewer_stream_rotation_lock);
1241 /* No new index to send, retry later. */
1242 viewer_index.status = htobe32(VIEWER_INDEX_RETRY);
1243 goto send_reply;
1244 }
d3e2ba59 1245 }
6b6b9a5a
JD
1246 pthread_mutex_unlock(&rstream->viewer_stream_rotation_lock);
1247 } else if (!rstream && vstream->close_write_flag &&
d3e2ba59 1248 vstream->total_index_received == vstream->last_sent_index) {
6b6b9a5a 1249 /* Last index sent and current tracefile closed in write */
d3e2ba59 1250 viewer_index.status = htobe32(VIEWER_INDEX_HUP);
157df586
JD
1251 delete_viewer_stream(vstream);
1252 destroy_viewer_stream(vstream);
d3e2ba59 1253 goto send_reply;
6b6b9a5a
JD
1254 } else {
1255 vstream->close_write_flag = 1;
d3e2ba59
JD
1256 }
1257
1258 if (!vstream->ctf_trace->metadata_received ||
1259 vstream->ctf_trace->metadata_received >
1260 vstream->ctf_trace->metadata_sent) {
1261 viewer_index.flags |= LTTNG_VIEWER_FLAG_NEW_METADATA;
1262 }
1263
cef0f7d5
JD
1264 pthread_mutex_lock(&vstream->overwrite_lock);
1265 if (vstream->abort_flag) {
1266 /*
1267 * The file is being overwritten by the writer, we cannot
1268 * use it.
1269 */
1270 viewer_index.status = htobe32(VIEWER_INDEX_RETRY);
1271 pthread_mutex_unlock(&vstream->overwrite_lock);
1272 ret = rotate_viewer_stream(vstream, rstream);
1273 if (ret < 0) {
1274 goto end_unlock;
a020f610
JD
1275 } else if (ret == 1) {
1276 viewer_index.status = htobe32(VIEWER_INDEX_HUP);
157df586
JD
1277 delete_viewer_stream(vstream);
1278 destroy_viewer_stream(vstream);
a020f610 1279 goto send_reply;
cef0f7d5
JD
1280 }
1281 goto send_reply;
1282 }
6cd525e8
MD
1283 ret = lttng_read(vstream->index_read_fd, &packet_index,
1284 sizeof(packet_index));
cef0f7d5 1285 pthread_mutex_unlock(&vstream->overwrite_lock);
d3e2ba59 1286 if (ret < sizeof(packet_index)) {
6b6b9a5a
JD
1287 /*
1288 * The tracefile is closed in write, so we read up to EOF.
1289 */
1290 if (vstream->close_write_flag == 1) {
1291 viewer_index.status = htobe32(VIEWER_INDEX_RETRY);
1292 /* Rotate on normal EOF */
1293 ret = rotate_viewer_stream(vstream, rstream);
1294 if (ret < 0) {
1295 goto end_unlock;
a020f610
JD
1296 } else if (ret == 1) {
1297 viewer_index.status = htobe32(VIEWER_INDEX_HUP);
157df586
JD
1298 delete_viewer_stream(vstream);
1299 destroy_viewer_stream(vstream);
a020f610 1300 goto send_reply;
6b6b9a5a
JD
1301 }
1302 } else {
cef0f7d5
JD
1303 PERROR("Relay reading index file %d",
1304 vstream->index_read_fd);
1305 viewer_index.status = htobe32(VIEWER_INDEX_ERR);
6b6b9a5a
JD
1306 }
1307 goto send_reply;
d3e2ba59
JD
1308 } else {
1309 viewer_index.status = htobe32(VIEWER_INDEX_OK);
1310 vstream->last_sent_index++;
1311 }
1312
1313 /*
1314 * Indexes are stored in big endian, no need to switch before sending.
1315 */
1316 viewer_index.offset = packet_index.offset;
1317 viewer_index.packet_size = packet_index.packet_size;
1318 viewer_index.content_size = packet_index.content_size;
1319 viewer_index.timestamp_begin = packet_index.timestamp_begin;
1320 viewer_index.timestamp_end = packet_index.timestamp_end;
1321 viewer_index.events_discarded = packet_index.events_discarded;
1322 viewer_index.stream_id = packet_index.stream_id;
1323
1324send_reply:
1325 viewer_index.flags = htobe32(viewer_index.flags);
eea7556c 1326 health_code_update();
d3e2ba59
JD
1327 ret = cmd->sock->ops->sendmsg(cmd->sock, &viewer_index,
1328 sizeof(viewer_index), 0);
1329 if (ret < 0) {
1330 ERR("Relay index to viewer");
1331 goto end_unlock;
1332 }
eea7556c 1333 health_code_update();
d3e2ba59
JD
1334
1335 DBG("Index %" PRIu64 "for stream %" PRIu64 "sent",
1336 vstream->last_sent_index, vstream->stream_handle);
1337
1338end_unlock:
1339 rcu_read_unlock();
1340
1341end_no_session:
1342end:
1343 return ret;
1344}
1345
1346/*
1347 * Send the next index for a stream
1348 *
1349 * Return 0 on success or else a negative value.
1350 */
1351static
92c6ca54 1352int viewer_get_packet(struct relay_command *cmd)
d3e2ba59
JD
1353{
1354 int ret, send_data = 0;
1355 char *data = NULL;
1356 uint32_t len = 0;
1357 ssize_t read_len;
1358 struct lttng_viewer_get_packet get_packet_info;
1359 struct lttng_viewer_trace_packet reply;
1360 struct relay_viewer_stream *stream;
1361
1362 assert(cmd);
d3e2ba59
JD
1363
1364 DBG2("Relay get data packet");
1365
1366 if (cmd->version_check_done == 0) {
1367 ERR("Trying to get packet before version check");
1368 ret = -1;
1369 goto end;
1370 }
1371
eea7556c 1372 health_code_update();
d3e2ba59
JD
1373 ret = cmd->sock->ops->recvmsg(cmd->sock, &get_packet_info,
1374 sizeof(get_packet_info), 0);
1375 if (ret < 0 || ret != sizeof(get_packet_info)) {
1376 ret = -1;
1377 ERR("Relay didn't receive the whole packet");
1378 goto end;
1379 }
eea7556c 1380 health_code_update();
d3e2ba59 1381
0233a6a5
DG
1382 /* From this point on, the error label can be reached. */
1383 memset(&reply, 0, sizeof(reply));
1384
d3e2ba59 1385 rcu_read_lock();
92c6ca54 1386 stream = live_find_viewer_stream_by_id(be64toh(get_packet_info.stream_id));
d3e2ba59
JD
1387 if (!stream) {
1388 goto error;
1389 }
1390 assert(stream->ctf_trace);
1391
1392 /*
1393 * First time we read this stream, we need open the tracefile, we should
1394 * only arrive here if an index has already been sent to the viewer, so the
1395 * tracefile must exist, if it does not it is a fatal error.
1396 */
1397 if (stream->read_fd < 0) {
1398 char fullpath[PATH_MAX];
1399
6b6b9a5a
JD
1400 if (stream->tracefile_count > 0) {
1401 ret = snprintf(fullpath, PATH_MAX, "%s/%s_%" PRIu64, stream->path_name,
1402 stream->channel_name,
1403 stream->tracefile_count_current);
1404 } else {
1405 ret = snprintf(fullpath, PATH_MAX, "%s/%s", stream->path_name,
1406 stream->channel_name);
1407 }
d3e2ba59
JD
1408 if (ret < 0) {
1409 goto error;
1410 }
1411 ret = open(fullpath, O_RDONLY);
1412 if (ret < 0) {
1413 PERROR("Relay opening trace file");
1414 goto error;
1415 }
1416 stream->read_fd = ret;
1417 }
1418
d3e2ba59
JD
1419 if (!stream->ctf_trace->metadata_received ||
1420 stream->ctf_trace->metadata_received >
1421 stream->ctf_trace->metadata_sent) {
1422 reply.status = htobe32(VIEWER_GET_PACKET_ERR);
1423 reply.flags |= LTTNG_VIEWER_FLAG_NEW_METADATA;
d3e2ba59
JD
1424 goto send_reply;
1425 }
1426
1427 len = be32toh(get_packet_info.len);
1428 data = zmalloc(len);
1429 if (!data) {
1430 PERROR("relay data zmalloc");
1431 goto error;
1432 }
1433
1434 ret = lseek(stream->read_fd, be64toh(get_packet_info.offset), SEEK_SET);
1435 if (ret < 0) {
6b6b9a5a
JD
1436 /*
1437 * If the read fd was closed by the streaming side, the
1438 * abort_flag will be set to 1, otherwise it is an error.
1439 */
1440 if (stream->abort_flag == 0) {
1441 PERROR("lseek");
1442 goto error;
1443 }
1444 reply.status = htobe32(VIEWER_GET_PACKET_EOF);
1445 goto send_reply;
d3e2ba59 1446 }
6cd525e8
MD
1447 read_len = lttng_read(stream->read_fd, data, len);
1448 if (read_len < len) {
6b6b9a5a
JD
1449 /*
1450 * If the read fd was closed by the streaming side, the
1451 * abort_flag will be set to 1, otherwise it is an error.
1452 */
1453 if (stream->abort_flag == 0) {
1454 PERROR("Relay reading trace file, fd: %d, offset: %" PRIu64,
1455 stream->read_fd,
1456 be64toh(get_packet_info.offset));
1457 goto error;
1458 } else {
1459 reply.status = htobe32(VIEWER_GET_PACKET_EOF);
1460 goto send_reply;
1461 }
d3e2ba59
JD
1462 }
1463 reply.status = htobe32(VIEWER_GET_PACKET_OK);
1464 reply.len = htobe32(len);
1465 send_data = 1;
1466 goto send_reply;
1467
1468error:
1469 reply.status = htobe32(VIEWER_GET_PACKET_ERR);
1470
1471send_reply:
1472 reply.flags = htobe32(reply.flags);
eea7556c
MD
1473
1474 health_code_update();
d3e2ba59
JD
1475 ret = cmd->sock->ops->sendmsg(cmd->sock, &reply, sizeof(reply), 0);
1476 if (ret < 0) {
1477 ERR("Relay data header to viewer");
1478 goto end_unlock;
1479 }
eea7556c 1480 health_code_update();
d3e2ba59
JD
1481
1482 if (send_data) {
eea7556c 1483 health_code_update();
d3e2ba59
JD
1484 ret = cmd->sock->ops->sendmsg(cmd->sock, data, len, 0);
1485 if (ret < 0) {
1486 ERR("Relay send data to viewer");
1487 goto end_unlock;
1488 }
eea7556c 1489 health_code_update();
d3e2ba59
JD
1490 }
1491
1492 DBG("Sent %u bytes for stream %" PRIu64, len,
1493 be64toh(get_packet_info.stream_id));
1494
1495end_unlock:
1496 free(data);
1497 rcu_read_unlock();
1498
1499end:
1500 return ret;
1501}
1502
1503/*
1504 * Send the session's metadata
1505 *
1506 * Return 0 on success else a negative value.
1507 */
1508static
92c6ca54 1509int viewer_get_metadata(struct relay_command *cmd)
d3e2ba59
JD
1510{
1511 int ret = 0;
1512 ssize_t read_len;
1513 uint64_t len = 0;
1514 char *data = NULL;
1515 struct lttng_viewer_get_metadata request;
1516 struct lttng_viewer_metadata_packet reply;
1517 struct relay_viewer_stream *stream;
1518
1519 assert(cmd);
d3e2ba59
JD
1520
1521 DBG("Relay get metadata");
1522
1523 if (cmd->version_check_done == 0) {
1524 ERR("Trying to get metadata before version check");
1525 ret = -1;
1526 goto end;
1527 }
1528
eea7556c 1529 health_code_update();
d3e2ba59
JD
1530 ret = cmd->sock->ops->recvmsg(cmd->sock, &request,
1531 sizeof(request), 0);
1532 if (ret < 0 || ret != sizeof(request)) {
1533 ret = -1;
1534 ERR("Relay didn't receive the whole packet");
1535 goto end;
1536 }
eea7556c 1537 health_code_update();
d3e2ba59
JD
1538
1539 rcu_read_lock();
92c6ca54 1540 stream = live_find_viewer_stream_by_id(be64toh(request.stream_id));
d3e2ba59
JD
1541 if (!stream || !stream->metadata_flag) {
1542 ERR("Invalid metadata stream");
1543 goto error;
1544 }
1545 assert(stream->ctf_trace);
1546 assert(stream->ctf_trace->metadata_sent <=
1547 stream->ctf_trace->metadata_received);
1548
1549 len = stream->ctf_trace->metadata_received -
1550 stream->ctf_trace->metadata_sent;
1551 if (len == 0) {
1552 reply.status = htobe32(VIEWER_NO_NEW_METADATA);
1553 goto send_reply;
1554 }
1555
1556 /* first time, we open the metadata file */
1557 if (stream->read_fd < 0) {
1558 char fullpath[PATH_MAX];
1559
1560 ret = snprintf(fullpath, PATH_MAX, "%s/%s", stream->path_name,
1561 stream->channel_name);
1562 if (ret < 0) {
1563 goto error;
1564 }
1565 ret = open(fullpath, O_RDONLY);
1566 if (ret < 0) {
1567 PERROR("Relay opening metadata file");
1568 goto error;
1569 }
1570 stream->read_fd = ret;
1571 }
1572
1573 reply.len = htobe64(len);
1574 data = zmalloc(len);
1575 if (!data) {
1576 PERROR("viewer metadata zmalloc");
1577 goto error;
1578 }
1579
6cd525e8
MD
1580 read_len = lttng_read(stream->read_fd, data, len);
1581 if (read_len < len) {
d3e2ba59
JD
1582 PERROR("Relay reading metadata file");
1583 goto error;
1584 }
1585 stream->ctf_trace->metadata_sent += read_len;
1586 reply.status = htobe32(VIEWER_METADATA_OK);
1587 goto send_reply;
1588
1589error:
1590 reply.status = htobe32(VIEWER_METADATA_ERR);
1591
1592send_reply:
eea7556c 1593 health_code_update();
d3e2ba59
JD
1594 ret = cmd->sock->ops->sendmsg(cmd->sock, &reply, sizeof(reply), 0);
1595 if (ret < 0) {
1596 ERR("Relay data header to viewer");
1597 goto end_unlock;
1598 }
eea7556c 1599 health_code_update();
d3e2ba59
JD
1600
1601 if (len > 0) {
1602 ret = cmd->sock->ops->sendmsg(cmd->sock, data, len, 0);
1603 if (ret < 0) {
1604 ERR("Relay send data to viewer");
1605 goto end_unlock;
1606 }
1607 }
1608
1609 DBG("Sent %" PRIu64 " bytes of metadata for stream %" PRIu64, len,
1610 be64toh(request.stream_id));
1611
1612 DBG("Metadata sent");
1613
1614end_unlock:
1615 free(data);
1616 rcu_read_unlock();
1617end:
1618 return ret;
1619}
1620
1621/*
1622 * live_relay_unknown_command: send -1 if received unknown command
1623 */
1624static
1625void live_relay_unknown_command(struct relay_command *cmd)
1626{
1627 struct lttcomm_relayd_generic_reply reply;
1628 int ret;
1629
1630 reply.ret_code = htobe32(LTTNG_ERR_UNK);
1631 ret = cmd->sock->ops->sendmsg(cmd->sock, &reply,
1632 sizeof(struct lttcomm_relayd_generic_reply), 0);
1633 if (ret < 0) {
1634 ERR("Relay sending unknown command");
1635 }
1636}
1637
1638/*
1639 * Process the commands received on the control socket
1640 */
1641static
1642int process_control(struct lttng_viewer_cmd *recv_hdr,
92c6ca54 1643 struct relay_command *cmd, struct lttng_ht *sessions_ht)
d3e2ba59
JD
1644{
1645 int ret = 0;
1646
1647 switch (be32toh(recv_hdr->cmd)) {
1648 case VIEWER_CONNECT:
1649 ret = viewer_connect(cmd);
1650 break;
1651 case VIEWER_LIST_SESSIONS:
1652 ret = viewer_list_sessions(cmd, sessions_ht);
1653 break;
1654 case VIEWER_ATTACH_SESSION:
92c6ca54 1655 ret = viewer_attach_session(cmd, sessions_ht);
d3e2ba59
JD
1656 break;
1657 case VIEWER_GET_NEXT_INDEX:
92c6ca54 1658 ret = viewer_get_next_index(cmd, sessions_ht);
d3e2ba59
JD
1659 break;
1660 case VIEWER_GET_PACKET:
92c6ca54 1661 ret = viewer_get_packet(cmd);
d3e2ba59
JD
1662 break;
1663 case VIEWER_GET_METADATA:
92c6ca54 1664 ret = viewer_get_metadata(cmd);
d3e2ba59
JD
1665 break;
1666 default:
1667 ERR("Received unknown viewer command (%u)", be32toh(recv_hdr->cmd));
1668 live_relay_unknown_command(cmd);
1669 ret = -1;
1670 goto end;
1671 }
1672
1673end:
1674 return ret;
1675}
1676
1677static
1678void cleanup_poll_connection(struct lttng_poll_event *events, int pollfd)
1679{
1680 int ret;
1681
1682 assert(events);
1683
1684 lttng_poll_del(events, pollfd);
1685
1686 ret = close(pollfd);
1687 if (ret < 0) {
1688 ERR("Closing pollfd %d", pollfd);
1689 }
1690}
1691
1692/*
1693 * Create and add connection to the given hash table.
1694 *
1695 * Return poll add value or else -1 on error.
1696 */
1697static
1698int add_connection(int fd, struct lttng_poll_event *events,
1699 struct lttng_ht *relay_connections_ht)
1700{
1701 int ret;
1702 struct relay_command *relay_connection;
1703
1704 assert(events);
1705 assert(relay_connections_ht);
1706
1707 relay_connection = zmalloc(sizeof(struct relay_command));
1708 if (relay_connection == NULL) {
1709 PERROR("Relay command zmalloc");
1710 goto error;
1711 }
1712
6cd525e8
MD
1713 ret = lttng_read(fd, relay_connection, sizeof(*relay_connection));
1714 if (ret < sizeof(*relay_connection)) {
d3e2ba59
JD
1715 PERROR("read relay cmd pipe");
1716 goto error_read;
1717 }
1718
1719 lttng_ht_node_init_ulong(&relay_connection->sock_n,
1720 (unsigned long) relay_connection->sock->fd);
1721 rcu_read_lock();
1722 lttng_ht_add_unique_ulong(relay_connections_ht,
1723 &relay_connection->sock_n);
1724 rcu_read_unlock();
1725
1726 return lttng_poll_add(events, relay_connection->sock->fd,
1727 LPOLLIN | LPOLLRDHUP);
1728
1729error_read:
1730 free(relay_connection);
1731error:
1732 return -1;
1733}
1734
1735static
1736void deferred_free_connection(struct rcu_head *head)
1737{
1738 struct relay_command *relay_connection =
1739 caa_container_of(head, struct relay_command, rcu_node);
1740
1741 if (relay_connection->session &&
1742 relay_connection->session->viewer_attached > 0) {
1743 relay_connection->session->viewer_attached--;
1744 }
1745 lttcomm_destroy_sock(relay_connection->sock);
1746 free(relay_connection);
1747}
1748
157df586
JD
1749/*
1750 * Delete all streams for a specific session ID.
1751 */
d3e2ba59 1752static
b92fdc2b 1753void viewer_del_streams(uint64_t session_id)
d3e2ba59 1754{
d3e2ba59 1755 struct relay_viewer_stream *stream;
d3e2ba59
JD
1756 struct lttng_ht_iter iter;
1757
d3e2ba59 1758 rcu_read_lock();
157df586
JD
1759 cds_lfht_for_each_entry(viewer_streams_ht->ht, &iter.iter, stream,
1760 stream_n.node) {
eea7556c
MD
1761 health_code_update();
1762
b92fdc2b 1763 if (stream->session_id != session_id) {
d3e2ba59
JD
1764 continue;
1765 }
1766
157df586
JD
1767 delete_viewer_stream(stream);
1768 assert(stream->ctf_trace);
1769
1770 if (stream->metadata_flag) {
1771 /*
1772 * The metadata viewer stream is destroyed once the refcount on the
1773 * ctf trace goes to 0 in the destroy stream function thus there is
1774 * no explicit call to that function here.
1775 */
d3e2ba59 1776 stream->ctf_trace->metadata_sent = 0;
157df586
JD
1777 stream->ctf_trace->viewer_metadata_stream = NULL;
1778 } else {
1779 destroy_viewer_stream(stream);
d3e2ba59 1780 }
d3e2ba59
JD
1781 }
1782 rcu_read_unlock();
1783}
1784
1785/*
1786 * Delete and free a connection.
1787 *
1788 * RCU read side lock MUST be acquired.
1789 */
1790static
1791void del_connection(struct lttng_ht *relay_connections_ht,
92c6ca54 1792 struct lttng_ht_iter *iter, struct relay_command *relay_connection)
d3e2ba59
JD
1793{
1794 int ret;
1795
1796 assert(relay_connections_ht);
1797 assert(iter);
1798 assert(relay_connection);
d3e2ba59 1799
157df586
JD
1800 DBG("Cleaning connection of session ID %" PRIu64,
1801 relay_connection->session_id);
1802
d3e2ba59
JD
1803 ret = lttng_ht_del(relay_connections_ht, iter);
1804 assert(!ret);
1805
b92fdc2b 1806 viewer_del_streams(relay_connection->session_id);
d3e2ba59
JD
1807
1808 call_rcu(&relay_connection->rcu_node, deferred_free_connection);
1809}
1810
1811/*
1812 * This thread does the actual work
1813 */
1814static
1815void *thread_worker(void *data)
1816{
1817 int ret, err = -1;
1818 uint32_t nb_fd;
1819 struct relay_command *relay_connection;
1820 struct lttng_poll_event events;
1821 struct lttng_ht *relay_connections_ht;
1822 struct lttng_ht_node_ulong *node;
1823 struct lttng_ht_iter iter;
1824 struct lttng_viewer_cmd recv_hdr;
1825 struct relay_local_data *relay_ctx = (struct relay_local_data *) data;
1826 struct lttng_ht *sessions_ht = relay_ctx->sessions_ht;
d3e2ba59
JD
1827
1828 DBG("[thread] Live viewer relay worker started");
1829
1830 rcu_register_thread();
1831
eea7556c
MD
1832 health_register(health_relayd, HEALTH_RELAYD_TYPE_LIVE_WORKER);
1833
d3e2ba59
JD
1834 /* table of connections indexed on socket */
1835 relay_connections_ht = lttng_ht_new(0, LTTNG_HT_TYPE_ULONG);
1836 if (!relay_connections_ht) {
1837 goto relay_connections_ht_error;
1838 }
1839
1840 ret = create_thread_poll_set(&events, 2);
1841 if (ret < 0) {
1842 goto error_poll_create;
1843 }
1844
1845 ret = lttng_poll_add(&events, live_relay_cmd_pipe[0], LPOLLIN | LPOLLRDHUP);
1846 if (ret < 0) {
1847 goto error;
1848 }
1849
1850restart:
1851 while (1) {
1852 int i;
1853
eea7556c
MD
1854 health_code_update();
1855
d3e2ba59
JD
1856 /* Infinite blocking call, waiting for transmission */
1857 DBG3("Relayd live viewer worker thread polling...");
eea7556c 1858 health_poll_entry();
d3e2ba59 1859 ret = lttng_poll_wait(&events, -1);
eea7556c 1860 health_poll_exit();
d3e2ba59
JD
1861 if (ret < 0) {
1862 /*
1863 * Restart interrupted system call.
1864 */
1865 if (errno == EINTR) {
1866 goto restart;
1867 }
1868 goto error;
1869 }
1870
1871 nb_fd = ret;
1872
1873 /*
1874 * Process control. The control connection is prioritised so we don't
1875 * starve it with high throughput tracing data on the data
1876 * connection.
1877 */
1878 for (i = 0; i < nb_fd; i++) {
1879 /* Fetch once the poll data */
1880 uint32_t revents = LTTNG_POLL_GETEV(&events, i);
1881 int pollfd = LTTNG_POLL_GETFD(&events, i);
1882
eea7556c
MD
1883 health_code_update();
1884
d3e2ba59
JD
1885 /* Thread quit pipe has been closed. Killing thread. */
1886 ret = check_thread_quit_pipe(pollfd, revents);
1887 if (ret) {
1888 err = 0;
1889 goto exit;
1890 }
1891
1892 /* Inspect the relay cmd pipe for new connection */
1893 if (pollfd == live_relay_cmd_pipe[0]) {
1894 if (revents & (LPOLLERR | LPOLLHUP | LPOLLRDHUP)) {
1895 ERR("Relay live pipe error");
1896 goto error;
1897 } else if (revents & LPOLLIN) {
1898 DBG("Relay live viewer command received");
1899 ret = add_connection(live_relay_cmd_pipe[0],
1900 &events, relay_connections_ht);
1901 if (ret < 0) {
1902 goto error;
1903 }
1904 }
1905 } else if (revents) {
1906 rcu_read_lock();
1907 lttng_ht_lookup(relay_connections_ht,
1908 (void *)((unsigned long) pollfd), &iter);
1909 node = lttng_ht_iter_get_node_ulong(&iter);
1910 if (node == NULL) {
1911 DBG2("Relay viewer sock %d not found", pollfd);
1912 rcu_read_unlock();
1913 goto error;
1914 }
1915 relay_connection = caa_container_of(node, struct relay_command,
1916 sock_n);
1917
1918 if (revents & (LPOLLERR)) {
d3e2ba59
JD
1919 cleanup_poll_connection(&events, pollfd);
1920 del_connection(relay_connections_ht, &iter,
92c6ca54 1921 relay_connection);
d3e2ba59
JD
1922 } else if (revents & (LPOLLHUP | LPOLLRDHUP)) {
1923 DBG("Viewer socket %d hung up", pollfd);
1924 cleanup_poll_connection(&events, pollfd);
1925 del_connection(relay_connections_ht, &iter,
92c6ca54 1926 relay_connection);
d3e2ba59
JD
1927 } else if (revents & LPOLLIN) {
1928 ret = relay_connection->sock->ops->recvmsg(
1929 relay_connection->sock, &recv_hdr,
1930 sizeof(struct lttng_viewer_cmd),
1931 0);
1932 /* connection closed */
1933 if (ret <= 0) {
1934 cleanup_poll_connection(&events, pollfd);
aaec7998 1935 del_connection(relay_connections_ht, &iter,
92c6ca54 1936 relay_connection);
d3e2ba59
JD
1937 DBG("Viewer control connection closed with %d",
1938 pollfd);
1939 } else {
1940 if (relay_connection->session) {
1941 DBG2("Relay viewer worker receiving data for "
1942 "session: %" PRIu64,
1943 relay_connection->session->id);
1944 }
1945 ret = process_control(&recv_hdr, relay_connection,
92c6ca54 1946 sessions_ht);
d3e2ba59
JD
1947 if (ret < 0) {
1948 /* Clear the session on error. */
1949 cleanup_poll_connection(&events, pollfd);
1950 del_connection(relay_connections_ht, &iter,
92c6ca54 1951 relay_connection);
d3e2ba59
JD
1952 DBG("Viewer connection closed with %d", pollfd);
1953 }
1954 }
1955 }
1956 rcu_read_unlock();
1957 }
1958 }
1959 }
1960
1961exit:
1962error:
1963 lttng_poll_clean(&events);
1964
1965 /* empty the hash table and free the memory */
1966 rcu_read_lock();
1967 cds_lfht_for_each_entry(relay_connections_ht->ht, &iter.iter, node, node) {
eea7556c
MD
1968 health_code_update();
1969
d3e2ba59
JD
1970 node = lttng_ht_iter_get_node_ulong(&iter);
1971 if (!node) {
1972 continue;
1973 }
1974
1975 relay_connection = caa_container_of(node, struct relay_command,
1976 sock_n);
92c6ca54 1977 del_connection(relay_connections_ht, &iter, relay_connection);
d3e2ba59
JD
1978 }
1979 rcu_read_unlock();
1980error_poll_create:
1981 lttng_ht_destroy(relay_connections_ht);
1982relay_connections_ht_error:
1983 /* Close relay cmd pipes */
1984 utils_close_pipe(live_relay_cmd_pipe);
1985 if (err) {
1986 DBG("Viewer worker thread exited with error");
1987 }
1988 DBG("Viewer worker thread cleanup complete");
eea7556c
MD
1989 if (err) {
1990 health_error();
1991 ERR("Health error occurred in %s", __func__);
1992 }
1993 health_unregister(health_relayd);
d3e2ba59
JD
1994 stop_threads();
1995 rcu_unregister_thread();
1996 return NULL;
1997}
1998
1999/*
2000 * Create the relay command pipe to wake thread_manage_apps.
2001 * Closed in cleanup().
2002 */
2003static int create_relay_cmd_pipe(void)
2004{
2005 int ret;
2006
2007 ret = utils_create_pipe_cloexec(live_relay_cmd_pipe);
2008
2009 return ret;
2010}
2011
aaec7998 2012void live_stop_threads(void)
d3e2ba59
JD
2013{
2014 int ret;
2015 void *status;
2016
2017 stop_threads();
2018
2019 ret = pthread_join(live_listener_thread, &status);
2020 if (ret != 0) {
2021 PERROR("pthread_join live listener");
2022 goto error; /* join error, exit without cleanup */
2023 }
2024
2025 ret = pthread_join(live_worker_thread, &status);
2026 if (ret != 0) {
2027 PERROR("pthread_join live worker");
2028 goto error; /* join error, exit without cleanup */
2029 }
2030
2031 ret = pthread_join(live_dispatcher_thread, &status);
2032 if (ret != 0) {
2033 PERROR("pthread_join live dispatcher");
2034 goto error; /* join error, exit without cleanup */
2035 }
2036
2037 cleanup();
2038
2039error:
2040 return;
2041}
2042
2043/*
2044 * main
2045 */
2046int live_start_threads(struct lttng_uri *uri,
42415026 2047 struct relay_local_data *relay_ctx, int quit_pipe[2])
d3e2ba59
JD
2048{
2049 int ret = 0;
2050 void *status;
2051 int is_root;
2052
2053 assert(uri);
2054 live_uri = uri;
2055
42415026
DG
2056 live_thread_quit_pipe[0] = quit_pipe[0];
2057 live_thread_quit_pipe[1] = quit_pipe[1];
d3e2ba59
JD
2058
2059 /* Check if daemon is UID = 0 */
2060 is_root = !getuid();
2061
2062 if (!is_root) {
2063 if (live_uri->port < 1024) {
2064 ERR("Need to be root to use ports < 1024");
2065 ret = -1;
2066 goto exit;
2067 }
2068 }
2069
2070 /* Setup the thread apps communication pipe. */
2071 if ((ret = create_relay_cmd_pipe()) < 0) {
2072 goto exit;
2073 }
2074
2075 /* Init relay command queue. */
2076 cds_wfq_init(&viewer_cmd_queue.queue);
2077
2078 /* Set up max poll set size */
2079 lttng_poll_set_max_size();
2080
2081 /* Setup the dispatcher thread */
2082 ret = pthread_create(&live_dispatcher_thread, NULL,
2083 thread_dispatcher, (void *) NULL);
2084 if (ret != 0) {
2085 PERROR("pthread_create viewer dispatcher");
2086 goto exit_dispatcher;
2087 }
2088
2089 /* Setup the worker thread */
2090 ret = pthread_create(&live_worker_thread, NULL,
2091 thread_worker, relay_ctx);
2092 if (ret != 0) {
2093 PERROR("pthread_create viewer worker");
2094 goto exit_worker;
2095 }
2096
2097 /* Setup the listener thread */
2098 ret = pthread_create(&live_listener_thread, NULL,
2099 thread_listener, (void *) NULL);
2100 if (ret != 0) {
2101 PERROR("pthread_create viewer listener");
2102 goto exit_listener;
2103 }
2104
2105 ret = 0;
2106 goto end;
2107
2108exit_listener:
2109 ret = pthread_join(live_listener_thread, &status);
2110 if (ret != 0) {
2111 PERROR("pthread_join live listener");
2112 goto error; /* join error, exit without cleanup */
2113 }
2114
2115exit_worker:
2116 ret = pthread_join(live_worker_thread, &status);
2117 if (ret != 0) {
2118 PERROR("pthread_join live worker");
2119 goto error; /* join error, exit without cleanup */
2120 }
2121
2122exit_dispatcher:
2123 ret = pthread_join(live_dispatcher_thread, &status);
2124 if (ret != 0) {
2125 PERROR("pthread_join live dispatcher");
2126 goto error; /* join error, exit without cleanup */
2127 }
2128
2129exit:
2130 cleanup();
2131
2132end:
2133error:
2134 return ret;
2135}
This page took 0.114492 seconds and 4 git commands to generate.