Fix: allow attach command to multiple sessions
[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>
2f8f53af 48#include <common/index/index.h>
d3e2ba59
JD
49#include <common/sessiond-comm/sessiond-comm.h>
50#include <common/sessiond-comm/inet.h>
51#include <common/sessiond-comm/relayd.h>
52#include <common/uri.h>
53#include <common/utils.h>
54
55#include "cmd.h"
56#include "live.h"
57#include "lttng-relayd.h"
d3e2ba59 58#include "utils.h"
eea7556c 59#include "health-relayd.h"
9b5e0863 60#include "testpoint.h"
2f8f53af 61#include "viewer-stream.h"
2a174661
DG
62#include "stream.h"
63#include "session.h"
64#include "ctf-trace.h"
58eb9381 65#include "connection.h"
d3e2ba59
JD
66
67static struct lttng_uri *live_uri;
68
d3e2ba59
JD
69/*
70 * This pipe is used to inform the worker thread that a command is queued and
71 * ready to be processed.
72 */
58eb9381 73static int live_conn_pipe[2] = { -1, -1 };
d3e2ba59
JD
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 */
58eb9381 88static struct relay_conn_queue viewer_conn_queue;
d3e2ba59
JD
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
2f8f53af
DG
103/*
104 * Receive a request buffer using a given socket, destination allocated buffer
105 * of length size.
106 *
107 * Return the size of the received message or else a negative value on error
108 * with errno being set by recvmsg() syscall.
109 */
110static
111ssize_t recv_request(struct lttcomm_sock *sock, void *buf, size_t size)
112{
113 ssize_t ret;
114
115 assert(sock);
116 assert(buf);
117
118 ret = sock->ops->recvmsg(sock, buf, size, 0);
119 if (ret < 0 || ret != size) {
120 if (ret == 0) {
121 /* Orderly shutdown. Not necessary to print an error. */
122 DBG("Socket %d did an orderly shutdown", sock->fd);
123 } else {
124 ERR("Relay failed to receive request.");
125 }
126 ret = -1;
127 }
128
129 return ret;
130}
131
132/*
133 * Send a response buffer using a given socket, source allocated buffer of
134 * length size.
135 *
136 * Return the size of the sent message or else a negative value on error with
137 * errno being set by sendmsg() syscall.
138 */
139static
140ssize_t send_response(struct lttcomm_sock *sock, void *buf, size_t size)
141{
142 ssize_t ret;
143
144 assert(sock);
145 assert(buf);
146
147 ret = sock->ops->sendmsg(sock, buf, size, 0);
148 if (ret < 0) {
149 ERR("Relayd failed to send response.");
150 }
151
152 return ret;
153}
154
155/*
156 * Atomically check if new streams got added in the session since the last
157 * check and reset the flag to 0.
158 *
159 * Returns 1 if new streams got added, 0 if nothing changed, a negative value
160 * on error.
161 */
162static
163int check_new_streams(uint64_t session_id, struct lttng_ht *sessions_ht)
164{
165 int ret;
166 unsigned long current_val;
167 struct relay_session *session;
168
169 assert(sessions_ht);
170
171 session = session_find_by_id(sessions_ht, session_id);
172 if (!session) {
173 DBG("Relay session %" PRIu64 " not found", session_id);
174 ret = -1;
175 goto error;
176 }
177
178 current_val = uatomic_cmpxchg(&session->new_streams, 1, 0);
179 ret = current_val;
180
181error:
182 return ret;
183}
184
185/*
186 * Send viewer streams to the given socket. The ignore_sent_flag indicates if
187 * this function should ignore the sent flag or not.
188 *
189 * Return 0 on success or else a negative value.
190 */
191static
192ssize_t send_viewer_streams(struct lttcomm_sock *sock,
193 struct relay_session *session, unsigned int ignore_sent_flag)
194{
195 ssize_t ret;
196 struct lttng_viewer_stream send_stream;
197 struct lttng_ht_iter iter;
198 struct relay_viewer_stream *vstream;
199
200 assert(session);
201
202 rcu_read_lock();
203
204 cds_lfht_for_each_entry(viewer_streams_ht->ht, &iter.iter, vstream,
205 stream_n.node) {
2a174661
DG
206 struct ctf_trace *ctf_trace;
207
2f8f53af
DG
208 health_code_update();
209
210 /* Ignore if not the same session. */
211 if (vstream->session_id != session->id ||
212 (!ignore_sent_flag && vstream->sent_flag)) {
213 continue;
214 }
215
2a174661
DG
216 ctf_trace = ctf_trace_find_by_path(session->ctf_traces_ht,
217 vstream->path_name);
218 assert(ctf_trace);
219
2f8f53af 220 send_stream.id = htobe64(vstream->stream_handle);
2a174661 221 send_stream.ctf_trace_id = htobe64(ctf_trace->id);
2f8f53af
DG
222 send_stream.metadata_flag = htobe32(vstream->metadata_flag);
223 strncpy(send_stream.path_name, vstream->path_name,
224 sizeof(send_stream.path_name));
225 strncpy(send_stream.channel_name, vstream->channel_name,
226 sizeof(send_stream.channel_name));
227
228 DBG("Sending stream %" PRIu64 " to viewer", vstream->stream_handle);
229 ret = send_response(sock, &send_stream, sizeof(send_stream));
230 if (ret < 0) {
231 goto end_unlock;
232 }
233 vstream->sent_flag = 1;
234 }
235
236 ret = 0;
237
238end_unlock:
239 rcu_read_unlock();
240 return ret;
241}
242
243/*
244 * Create every viewer stream possible for the given session with the seek
245 * type. Three counters *can* be return which are in order the total amount of
246 * viewer stream of the session, the number of unsent stream and the number of
247 * stream created. Those counters can be NULL and thus will be ignored.
248 *
249 * Return 0 on success or else a negative value.
250 */
251static
252int make_viewer_streams(struct relay_session *session,
253 enum lttng_viewer_seek seek_t, uint32_t *nb_total, uint32_t *nb_unsent,
254 uint32_t *nb_created)
255{
256 int ret;
2f8f53af 257 struct lttng_ht_iter iter;
2a174661 258 struct ctf_trace *ctf_trace;
2f8f53af
DG
259
260 assert(session);
261
262 /*
263 * This is to make sure we create viewer streams for a full received
264 * channel. For instance, if we have 8 streams for a channel that are
265 * concurrently being flagged ready, we can end up creating just a subset
266 * of the 8 streams (the ones that are flagged). This lock avoids this
267 * limbo state.
268 */
269 pthread_mutex_lock(&session->viewer_ready_lock);
270
271 /*
272 * Create viewer streams for relay streams that are ready to be used for a
273 * the given session id only.
274 */
2a174661
DG
275 rcu_read_lock();
276 cds_lfht_for_each_entry(session->ctf_traces_ht->ht, &iter.iter, ctf_trace,
277 node.node) {
278 struct relay_stream *stream;
2f8f53af
DG
279
280 health_code_update();
281
2a174661 282 if (ctf_trace->invalid_flag) {
2f8f53af
DG
283 continue;
284 }
285
2a174661
DG
286 cds_list_for_each_entry(stream, &ctf_trace->stream_list, trace_list) {
287 struct relay_viewer_stream *vstream;
288
289 if (!stream->viewer_ready) {
290 continue;
291 }
292
293 vstream = viewer_stream_find_by_id(stream->stream_handle);
2f8f53af 294 if (!vstream) {
2a174661
DG
295 vstream = viewer_stream_create(stream, seek_t, ctf_trace);
296 if (!vstream) {
297 ret = -1;
298 goto error_unlock;
299 }
300 /* Acquire reference to ctf_trace. */
301 ctf_trace_get_ref(ctf_trace);
302
303 if (nb_created) {
304 /* Update number of created stream counter. */
305 (*nb_created)++;
306 }
307 } else if (!vstream->sent_flag && nb_unsent) {
308 /* Update number of unsent stream counter. */
309 (*nb_unsent)++;
2f8f53af 310 }
2a174661
DG
311 /* Update number of total stream counter. */
312 if (nb_total) {
313 (*nb_total)++;
2f8f53af 314 }
2f8f53af
DG
315 }
316 }
317
318 ret = 0;
319
320error_unlock:
2a174661 321 rcu_read_unlock();
2f8f53af
DG
322 pthread_mutex_unlock(&session->viewer_ready_lock);
323 return ret;
324}
325
d3e2ba59
JD
326/*
327 * Write to writable pipe used to notify a thread.
328 */
329static
330int notify_thread_pipe(int wpipe)
331{
6cd525e8 332 ssize_t ret;
d3e2ba59 333
6cd525e8
MD
334 ret = lttng_write(wpipe, "!", 1);
335 if (ret < 1) {
d3e2ba59
JD
336 PERROR("write poll pipe");
337 }
338
6cd525e8 339 return (int) ret;
d3e2ba59
JD
340}
341
342/*
343 * Stop all threads by closing the thread quit pipe.
344 */
345static
346void stop_threads(void)
347{
348 int ret;
349
350 /* Stopping all threads */
351 DBG("Terminating all live threads");
2a174661 352 ret = notify_thread_pipe(live_conn_pipe[1]);
d3e2ba59
JD
353 if (ret < 0) {
354 ERR("write error on thread quit pipe");
355 }
356
357 /* Dispatch thread */
358 CMM_STORE_SHARED(live_dispatch_thread_exit, 1);
58eb9381 359 futex_nto1_wake(&viewer_conn_queue.futex);
d3e2ba59
JD
360}
361
d3e2ba59
JD
362/*
363 * Create a poll set with O_CLOEXEC and add the thread quit pipe to the set.
364 */
365static
366int create_thread_poll_set(struct lttng_poll_event *events, int size)
367{
368 int ret;
369
370 if (events == NULL || size == 0) {
371 ret = -1;
372 goto error;
373 }
374
375 ret = lttng_poll_create(events, size, LTTNG_CLOEXEC);
376 if (ret < 0) {
377 goto error;
378 }
379
380 /* Add quit pipe */
2a174661 381 ret = lttng_poll_add(events, live_conn_pipe[0], LPOLLIN | LPOLLERR);
d3e2ba59
JD
382 if (ret < 0) {
383 goto error;
384 }
385
386 return 0;
387
388error:
389 return ret;
390}
391
392/*
393 * Check if the thread quit pipe was triggered.
394 *
395 * Return 1 if it was triggered else 0;
396 */
397static
2a174661 398int check_live_conn_pipe(int fd, uint32_t events)
d3e2ba59 399{
2a174661 400 if (fd == live_conn_pipe[0] && (events & LPOLLIN)) {
d3e2ba59
JD
401 return 1;
402 }
403
404 return 0;
405}
406
407/*
408 * Create and init socket from uri.
409 */
410static
411struct lttcomm_sock *init_socket(struct lttng_uri *uri)
412{
413 int ret;
414 struct lttcomm_sock *sock = NULL;
415
416 sock = lttcomm_alloc_sock_from_uri(uri);
417 if (sock == NULL) {
418 ERR("Allocating socket");
419 goto error;
420 }
421
422 ret = lttcomm_create_sock(sock);
423 if (ret < 0) {
424 goto error;
425 }
426 DBG("Listening on sock %d for live", sock->fd);
427
428 ret = sock->ops->bind(sock);
429 if (ret < 0) {
430 goto error;
431 }
432
433 ret = sock->ops->listen(sock, -1);
434 if (ret < 0) {
435 goto error;
436
437 }
438
439 return sock;
440
441error:
442 if (sock) {
443 lttcomm_destroy_sock(sock);
444 }
445 return NULL;
446}
447
448/*
449 * This thread manages the listening for new connections on the network
450 */
451static
452void *thread_listener(void *data)
453{
454 int i, ret, pollfd, err = -1;
d3e2ba59
JD
455 uint32_t revents, nb_fd;
456 struct lttng_poll_event events;
457 struct lttcomm_sock *live_control_sock;
458
459 DBG("[thread] Relay live listener started");
460
eea7556c
MD
461 health_register(health_relayd, HEALTH_RELAYD_TYPE_LIVE_LISTENER);
462
463 health_code_update();
464
d3e2ba59
JD
465 live_control_sock = init_socket(live_uri);
466 if (!live_control_sock) {
467 goto error_sock_control;
468 }
469
fb4d42ab 470 /* Pass 2 as size here for the thread quit pipe and control sockets. */
d3e2ba59
JD
471 ret = create_thread_poll_set(&events, 2);
472 if (ret < 0) {
473 goto error_create_poll;
474 }
475
476 /* Add the control socket */
477 ret = lttng_poll_add(&events, live_control_sock->fd, LPOLLIN | LPOLLRDHUP);
478 if (ret < 0) {
479 goto error_poll_add;
480 }
481
3fd27398
MD
482 lttng_relay_notify_ready();
483
9b5e0863
MD
484 if (testpoint(relayd_thread_live_listener)) {
485 goto error_testpoint;
486 }
487
d3e2ba59 488 while (1) {
eea7556c
MD
489 health_code_update();
490
d3e2ba59
JD
491 DBG("Listener accepting live viewers connections");
492
493restart:
eea7556c 494 health_poll_entry();
d3e2ba59 495 ret = lttng_poll_wait(&events, -1);
eea7556c 496 health_poll_exit();
d3e2ba59
JD
497 if (ret < 0) {
498 /*
499 * Restart interrupted system call.
500 */
501 if (errno == EINTR) {
502 goto restart;
503 }
504 goto error;
505 }
506 nb_fd = ret;
507
508 DBG("Relay new viewer connection received");
509 for (i = 0; i < nb_fd; i++) {
eea7556c
MD
510 health_code_update();
511
d3e2ba59
JD
512 /* Fetch once the poll data */
513 revents = LTTNG_POLL_GETEV(&events, i);
514 pollfd = LTTNG_POLL_GETFD(&events, i);
515
516 /* Thread quit pipe has been closed. Killing thread. */
2a174661 517 ret = check_live_conn_pipe(pollfd, revents);
d3e2ba59
JD
518 if (ret) {
519 err = 0;
520 goto exit;
521 }
522
523 if (revents & (LPOLLERR | LPOLLHUP | LPOLLRDHUP)) {
524 ERR("socket poll error");
525 goto error;
526 } else if (revents & LPOLLIN) {
527 /*
528 * Get allocated in this thread, enqueued to a global queue,
529 * dequeued and freed in the worker thread.
530 */
58eb9381
DG
531 int val = 1;
532 struct relay_connection *new_conn;
d3e2ba59
JD
533 struct lttcomm_sock *newsock;
534
58eb9381
DG
535 new_conn = connection_create();
536 if (!new_conn) {
d3e2ba59
JD
537 goto error;
538 }
539
d3e2ba59
JD
540 newsock = live_control_sock->ops->accept(live_control_sock);
541 if (!newsock) {
542 PERROR("accepting control sock");
58eb9381 543 connection_free(new_conn);
d3e2ba59
JD
544 goto error;
545 }
546 DBG("Relay viewer connection accepted socket %d", newsock->fd);
58eb9381 547
d3e2ba59 548 ret = setsockopt(newsock->fd, SOL_SOCKET, SO_REUSEADDR, &val,
58eb9381 549 sizeof(val));
d3e2ba59
JD
550 if (ret < 0) {
551 PERROR("setsockopt inet");
552 lttcomm_destroy_sock(newsock);
58eb9381 553 connection_free(new_conn);
d3e2ba59
JD
554 goto error;
555 }
58eb9381 556 new_conn->sock = newsock;
d3e2ba59 557
58eb9381
DG
558 /* Enqueue request for the dispatcher thread. */
559 cds_wfq_enqueue(&viewer_conn_queue.queue, &new_conn->qnode);
d3e2ba59
JD
560
561 /*
58eb9381
DG
562 * Wake the dispatch queue futex. Implicit memory barrier with
563 * the exchange in cds_wfq_enqueue.
d3e2ba59 564 */
58eb9381 565 futex_nto1_wake(&viewer_conn_queue.futex);
d3e2ba59
JD
566 }
567 }
568 }
569
570exit:
571error:
572error_poll_add:
9b5e0863 573error_testpoint:
d3e2ba59
JD
574 lttng_poll_clean(&events);
575error_create_poll:
576 if (live_control_sock->fd >= 0) {
577 ret = live_control_sock->ops->close(live_control_sock);
578 if (ret) {
579 PERROR("close");
580 }
581 }
582 lttcomm_destroy_sock(live_control_sock);
583error_sock_control:
584 if (err) {
eea7556c 585 health_error();
d3e2ba59
JD
586 DBG("Live viewer listener thread exited with error");
587 }
eea7556c 588 health_unregister(health_relayd);
d3e2ba59
JD
589 DBG("Live viewer listener thread cleanup complete");
590 stop_threads();
591 return NULL;
592}
593
594/*
595 * This thread manages the dispatching of the requests to worker threads
596 */
597static
598void *thread_dispatcher(void *data)
599{
6cd525e8
MD
600 int err = -1;
601 ssize_t ret;
d3e2ba59 602 struct cds_wfq_node *node;
58eb9381 603 struct relay_connection *conn = NULL;
d3e2ba59
JD
604
605 DBG("[thread] Live viewer relay dispatcher started");
606
eea7556c
MD
607 health_register(health_relayd, HEALTH_RELAYD_TYPE_LIVE_DISPATCHER);
608
9b5e0863
MD
609 if (testpoint(relayd_thread_live_dispatcher)) {
610 goto error_testpoint;
611 }
612
eea7556c
MD
613 health_code_update();
614
d3e2ba59 615 while (!CMM_LOAD_SHARED(live_dispatch_thread_exit)) {
eea7556c
MD
616 health_code_update();
617
d3e2ba59 618 /* Atomically prepare the queue futex */
58eb9381 619 futex_nto1_prepare(&viewer_conn_queue.futex);
d3e2ba59
JD
620
621 do {
eea7556c
MD
622 health_code_update();
623
d3e2ba59 624 /* Dequeue commands */
58eb9381 625 node = cds_wfq_dequeue_blocking(&viewer_conn_queue.queue);
d3e2ba59
JD
626 if (node == NULL) {
627 DBG("Woken up but nothing in the live-viewer "
628 "relay command queue");
629 /* Continue thread execution */
630 break;
631 }
58eb9381 632 conn = caa_container_of(node, struct relay_connection, qnode);
d3e2ba59 633 DBG("Dispatching viewer request waiting on sock %d",
58eb9381 634 conn->sock->fd);
d3e2ba59
JD
635
636 /*
637 * Inform worker thread of the new request. This call is blocking
638 * so we can be assured that the data will be read at some point in
639 * time or wait to the end of the world :)
640 */
58eb9381
DG
641 ret = lttng_write(live_conn_pipe[1], &conn, sizeof(conn));
642 if (ret < 0) {
643 PERROR("write conn pipe");
644 connection_destroy(conn);
d3e2ba59
JD
645 goto error;
646 }
647 } while (node != NULL);
648
649 /* Futex wait on queue. Blocking call on futex() */
eea7556c 650 health_poll_entry();
58eb9381 651 futex_nto1_wait(&viewer_conn_queue.futex);
eea7556c 652 health_poll_exit();
d3e2ba59
JD
653 }
654
eea7556c
MD
655 /* Normal exit, no error */
656 err = 0;
657
d3e2ba59 658error:
9b5e0863 659error_testpoint:
eea7556c
MD
660 if (err) {
661 health_error();
662 ERR("Health error occurred in %s", __func__);
663 }
664 health_unregister(health_relayd);
d3e2ba59
JD
665 DBG("Live viewer dispatch thread dying");
666 stop_threads();
667 return NULL;
668}
669
670/*
671 * Establish connection with the viewer and check the versions.
672 *
673 * Return 0 on success or else negative value.
674 */
675static
58eb9381 676int viewer_connect(struct relay_connection *conn)
d3e2ba59
JD
677{
678 int ret;
679 struct lttng_viewer_connect reply, msg;
680
58eb9381 681 assert(conn);
d3e2ba59 682
58eb9381 683 conn->version_check_done = 1;
d3e2ba59 684
eea7556c
MD
685 health_code_update();
686
2f8f53af
DG
687 DBG("Viewer is establishing a connection to the relayd.");
688
58eb9381 689 ret = recv_request(conn->sock, &msg, sizeof(msg));
2f8f53af 690 if (ret < 0) {
d3e2ba59
JD
691 goto end;
692 }
693
eea7556c
MD
694 health_code_update();
695
d3e2ba59
JD
696 reply.major = RELAYD_VERSION_COMM_MAJOR;
697 reply.minor = RELAYD_VERSION_COMM_MINOR;
698
699 /* Major versions must be the same */
700 if (reply.major != be32toh(msg.major)) {
2f8f53af
DG
701 DBG("Incompatible major versions ([relayd] %u vs [client] %u)",
702 reply.major, be32toh(msg.major));
72180669 703 ret = -1;
d3e2ba59
JD
704 goto end;
705 }
706
58eb9381 707 conn->major = reply.major;
d3e2ba59
JD
708 /* We adapt to the lowest compatible version */
709 if (reply.minor <= be32toh(msg.minor)) {
58eb9381 710 conn->minor = reply.minor;
d3e2ba59 711 } else {
58eb9381 712 conn->minor = be32toh(msg.minor);
d3e2ba59
JD
713 }
714
c4e361a4 715 if (be32toh(msg.type) == LTTNG_VIEWER_CLIENT_COMMAND) {
58eb9381 716 conn->type = RELAY_VIEWER_COMMAND;
c4e361a4 717 } else if (be32toh(msg.type) == LTTNG_VIEWER_CLIENT_NOTIFICATION) {
58eb9381 718 conn->type = RELAY_VIEWER_NOTIFICATION;
d3e2ba59
JD
719 } else {
720 ERR("Unknown connection type : %u", be32toh(msg.type));
721 ret = -1;
722 goto end;
723 }
724
725 reply.major = htobe32(reply.major);
726 reply.minor = htobe32(reply.minor);
58eb9381 727 if (conn->type == RELAY_VIEWER_COMMAND) {
d3e2ba59
JD
728 reply.viewer_session_id = htobe64(++last_relay_viewer_session_id);
729 }
eea7556c
MD
730
731 health_code_update();
732
58eb9381 733 ret = send_response(conn->sock, &reply, sizeof(reply));
d3e2ba59 734 if (ret < 0) {
2f8f53af 735 goto end;
d3e2ba59
JD
736 }
737
eea7556c
MD
738 health_code_update();
739
58eb9381 740 DBG("Version check done using protocol %u.%u", conn->major, conn->minor);
d3e2ba59
JD
741 ret = 0;
742
743end:
744 return ret;
745}
746
747/*
748 * Send the viewer the list of current sessions.
749 *
750 * Return 0 on success or else a negative value.
751 */
752static
58eb9381 753int viewer_list_sessions(struct relay_connection *conn)
d3e2ba59
JD
754{
755 int ret;
756 struct lttng_viewer_list_sessions session_list;
757 unsigned long count;
758 long approx_before, approx_after;
d3e2ba59
JD
759 struct lttng_ht_iter iter;
760 struct lttng_viewer_session send_session;
761 struct relay_session *session;
762
763 DBG("List sessions received");
764
d3e2ba59 765 rcu_read_lock();
58eb9381
DG
766 cds_lfht_count_nodes(conn->sessions_ht->ht, &approx_before, &count,
767 &approx_after);
d3e2ba59
JD
768 session_list.sessions_count = htobe32(count);
769
eea7556c
MD
770 health_code_update();
771
58eb9381 772 ret = send_response(conn->sock, &session_list, sizeof(session_list));
d3e2ba59 773 if (ret < 0) {
d3e2ba59
JD
774 goto end_unlock;
775 }
776
eea7556c
MD
777 health_code_update();
778
58eb9381 779 cds_lfht_for_each_entry(conn->sessions_ht->ht, &iter.iter, session,
2f8f53af 780 session_n.node) {
eea7556c
MD
781 health_code_update();
782
d3e2ba59
JD
783 strncpy(send_session.session_name, session->session_name,
784 sizeof(send_session.session_name));
785 strncpy(send_session.hostname, session->hostname,
786 sizeof(send_session.hostname));
787 send_session.id = htobe64(session->id);
788 send_session.live_timer = htobe32(session->live_timer);
2a174661 789 send_session.clients = htobe32(session->viewer_refcount);
87b576ec 790 send_session.streams = htobe32(session->stream_count);
d3e2ba59 791
eea7556c
MD
792 health_code_update();
793
58eb9381 794 ret = send_response(conn->sock, &send_session, sizeof(send_session));
d3e2ba59 795 if (ret < 0) {
d3e2ba59
JD
796 goto end_unlock;
797 }
798 }
eea7556c
MD
799 health_code_update();
800
d3e2ba59
JD
801 rcu_read_unlock();
802 ret = 0;
803 goto end;
804
805end_unlock:
806 rcu_read_unlock();
807
808end:
d3e2ba59
JD
809 return ret;
810}
811
6763619c
JD
812/*
813 * Check if a connection is attached to a session.
814 * Return 1 if attached, 0 if not attached, a negative value on error.
815 */
816static
817int session_attached(struct relay_connection *conn, uint64_t session_id)
818{
819 struct relay_session *session;
820 int found = 0;
821
822 if (!conn->viewer_session) {
823 goto end;
824 }
825 cds_list_for_each_entry(session,
826 &conn->viewer_session->sessions_head,
827 viewer_session_list) {
828 if (session->id == session_id) {
829 found = 1;
830 goto end;
831 }
832 }
833
834end:
835 return found;
836}
837
80e8027a
JD
838/*
839 * Send the viewer the list of current sessions.
840 */
841static
58eb9381 842int viewer_get_new_streams(struct relay_connection *conn)
80e8027a
JD
843{
844 int ret, send_streams = 0;
2f8f53af 845 uint32_t nb_created = 0, nb_unsent = 0, nb_streams = 0;
80e8027a
JD
846 struct lttng_viewer_new_streams_request request;
847 struct lttng_viewer_new_streams_response response;
80e8027a 848 struct relay_session *session;
6763619c 849 uint64_t session_id;
80e8027a 850
58eb9381 851 assert(conn);
80e8027a
JD
852
853 DBG("Get new streams received");
854
80e8027a
JD
855 health_code_update();
856
2f8f53af 857 /* Receive the request from the connected client. */
58eb9381 858 ret = recv_request(conn->sock, &request, sizeof(request));
2f8f53af 859 if (ret < 0) {
80e8027a
JD
860 goto error;
861 }
6763619c 862 session_id = be64toh(request.session_id);
80e8027a
JD
863
864 health_code_update();
865
866 rcu_read_lock();
6763619c 867 session = session_find_by_id(conn->sessions_ht, session_id);
2f8f53af 868 if (!session) {
6763619c 869 DBG("Relay session %" PRIu64 " not found", session_id);
c4e361a4 870 response.status = htobe32(LTTNG_VIEWER_NEW_STREAMS_ERR);
80e8027a
JD
871 goto send_reply;
872 }
873
6763619c 874 if (!session_attached(conn, session_id)) {
80e8027a 875 send_streams = 0;
c4e361a4 876 response.status = htobe32(LTTNG_VIEWER_NEW_STREAMS_ERR);
80e8027a
JD
877 goto send_reply;
878 }
879
6763619c
JD
880 send_streams = 1;
881 response.status = htobe32(LTTNG_VIEWER_NEW_STREAMS_OK);
882
2f8f53af
DG
883 if (!send_streams) {
884 goto send_reply;
80e8027a 885 }
80e8027a 886
c4e361a4 887 ret = make_viewer_streams(session, LTTNG_VIEWER_SEEK_LAST, NULL, &nb_unsent,
2f8f53af
DG
888 &nb_created);
889 if (ret < 0) {
890 goto end_unlock;
891 }
892 /* Only send back the newly created streams with the unsent ones. */
893 nb_streams = nb_created + nb_unsent;
80e8027a
JD
894 response.streams_count = htobe32(nb_streams);
895
4479f682
JD
896 /*
897 * If the session is closed and we have no new streams to send,
898 * it means that the viewer has already received the whole trace
899 * for this session and should now close it.
900 */
901 if (nb_streams == 0 && session->close_flag) {
902 send_streams = 0;
903 response.status = htobe32(LTTNG_VIEWER_NEW_STREAMS_HUP);
6763619c
JD
904 /*
905 * Remove the session from the attached list of the connection
906 * and try to destroy it.
907 */
908 cds_list_del(&session->viewer_session_list);
909 session_viewer_try_destroy(conn->sessions_ht, session);
4479f682
JD
910 goto send_reply;
911 }
912
80e8027a
JD
913send_reply:
914 health_code_update();
58eb9381 915 ret = send_response(conn->sock, &response, sizeof(response));
80e8027a 916 if (ret < 0) {
80e8027a
JD
917 goto end_unlock;
918 }
919 health_code_update();
920
921 /*
922 * Unknown or empty session, just return gracefully, the viewer knows what
923 * is happening.
924 */
925 if (!send_streams || !nb_streams) {
926 ret = 0;
927 goto end_unlock;
928 }
929
2f8f53af
DG
930 /*
931 * Send stream and *DON'T* ignore the sent flag so every viewer streams
932 * that were not sent from that point will be sent to the viewer.
933 */
58eb9381 934 ret = send_viewer_streams(conn->sock, session, 0);
2f8f53af
DG
935 if (ret < 0) {
936 goto end_unlock;
80e8027a
JD
937 }
938
80e8027a
JD
939end_unlock:
940 rcu_read_unlock();
80e8027a
JD
941error:
942 return ret;
943}
944
d3e2ba59
JD
945/*
946 * Send the viewer the list of current sessions.
947 */
948static
58eb9381 949int viewer_attach_session(struct relay_connection *conn)
d3e2ba59 950{
2f8f53af
DG
951 int send_streams = 0;
952 ssize_t ret;
80e8027a 953 uint32_t nb_streams = 0;
2f8f53af 954 enum lttng_viewer_seek seek_type;
d3e2ba59
JD
955 struct lttng_viewer_attach_session_request request;
956 struct lttng_viewer_attach_session_response response;
d3e2ba59
JD
957 struct relay_session *session;
958
58eb9381 959 assert(conn);
d3e2ba59 960
eea7556c
MD
961 health_code_update();
962
2f8f53af 963 /* Receive the request from the connected client. */
58eb9381 964 ret = recv_request(conn->sock, &request, sizeof(request));
2f8f53af 965 if (ret < 0) {
d3e2ba59
JD
966 goto error;
967 }
968
eea7556c
MD
969 health_code_update();
970
c3b7390b
JD
971 if (!conn->viewer_session) {
972 DBG("Client trying to attach before creating a live viewer session");
973 response.status = htobe32(LTTNG_VIEWER_ATTACH_NO_SESSION);
974 goto send_reply;
975 }
976
d3e2ba59 977 rcu_read_lock();
58eb9381
DG
978 session = session_find_by_id(conn->sessions_ht,
979 be64toh(request.session_id));
2f8f53af 980 if (!session) {
d3e2ba59
JD
981 DBG("Relay session %" PRIu64 " not found",
982 be64toh(request.session_id));
c4e361a4 983 response.status = htobe32(LTTNG_VIEWER_ATTACH_UNK);
d3e2ba59
JD
984 goto send_reply;
985 }
2a174661
DG
986 session_viewer_attach(session);
987 DBG("Attach session ID %" PRIu64 " received", be64toh(request.session_id));
d3e2ba59 988
2a174661 989 if (uatomic_read(&session->viewer_refcount) > 1) {
d3e2ba59 990 DBG("Already a viewer attached");
c4e361a4 991 response.status = htobe32(LTTNG_VIEWER_ATTACH_ALREADY);
2a174661 992 session_viewer_detach(session);
d3e2ba59
JD
993 goto send_reply;
994 } else if (session->live_timer == 0) {
995 DBG("Not live session");
c4e361a4 996 response.status = htobe32(LTTNG_VIEWER_ATTACH_NOT_LIVE);
d3e2ba59
JD
997 goto send_reply;
998 } else {
d3e2ba59 999 send_streams = 1;
c4e361a4 1000 response.status = htobe32(LTTNG_VIEWER_ATTACH_OK);
6763619c
JD
1001 cds_list_add(&session->viewer_session_list,
1002 &conn->viewer_session->sessions_head);
d3e2ba59
JD
1003 }
1004
1005 switch (be32toh(request.seek)) {
c4e361a4
JD
1006 case LTTNG_VIEWER_SEEK_BEGINNING:
1007 case LTTNG_VIEWER_SEEK_LAST:
2f8f53af 1008 seek_type = be32toh(request.seek);
d3e2ba59
JD
1009 break;
1010 default:
1011 ERR("Wrong seek parameter");
c4e361a4 1012 response.status = htobe32(LTTNG_VIEWER_ATTACH_SEEK_ERR);
d3e2ba59
JD
1013 send_streams = 0;
1014 goto send_reply;
1015 }
1016
2f8f53af
DG
1017 if (!send_streams) {
1018 goto send_reply;
1019 }
a4baae1b 1020
2f8f53af
DG
1021 ret = make_viewer_streams(session, seek_type, &nb_streams, NULL, NULL);
1022 if (ret < 0) {
1023 goto end_unlock;
d3e2ba59 1024 }
2f8f53af 1025 response.streams_count = htobe32(nb_streams);
d3e2ba59
JD
1026
1027send_reply:
eea7556c 1028 health_code_update();
58eb9381 1029 ret = send_response(conn->sock, &response, sizeof(response));
d3e2ba59 1030 if (ret < 0) {
d3e2ba59
JD
1031 goto end_unlock;
1032 }
eea7556c 1033 health_code_update();
d3e2ba59
JD
1034
1035 /*
157df586 1036 * Unknown or empty session, just return gracefully, the viewer knows what
d3e2ba59
JD
1037 * is happening.
1038 */
157df586 1039 if (!send_streams || !nb_streams) {
d3e2ba59
JD
1040 ret = 0;
1041 goto end_unlock;
1042 }
1043
2f8f53af 1044 /* Send stream and ignore the sent flag. */
58eb9381 1045 ret = send_viewer_streams(conn->sock, session, 1);
2f8f53af
DG
1046 if (ret < 0) {
1047 goto end_unlock;
d3e2ba59 1048 }
d3e2ba59
JD
1049
1050end_unlock:
1051 rcu_read_unlock();
4a9daf17
JD
1052error:
1053 return ret;
1054}
1055
d3e2ba59
JD
1056/*
1057 * Send the next index for a stream.
1058 *
1059 * Return 0 on success or else a negative value.
1060 */
1061static
58eb9381 1062int viewer_get_next_index(struct relay_connection *conn)
d3e2ba59
JD
1063{
1064 int ret;
1065 struct lttng_viewer_get_next_index request_index;
1066 struct lttng_viewer_index viewer_index;
50adc264 1067 struct ctf_packet_index packet_index;
d3e2ba59
JD
1068 struct relay_viewer_stream *vstream;
1069 struct relay_stream *rstream;
2a174661
DG
1070 struct ctf_trace *ctf_trace;
1071 struct relay_session *session;
d3e2ba59 1072
58eb9381 1073 assert(conn);
d3e2ba59
JD
1074
1075 DBG("Viewer get next index");
1076
eea7556c 1077 health_code_update();
2f8f53af 1078
58eb9381 1079 ret = recv_request(conn->sock, &request_index, sizeof(request_index));
2f8f53af 1080 if (ret < 0) {
d3e2ba59
JD
1081 goto end;
1082 }
eea7556c 1083 health_code_update();
d3e2ba59
JD
1084
1085 rcu_read_lock();
6763619c
JD
1086 vstream = viewer_stream_find_by_id(be64toh(request_index.stream_id));
1087 if (!vstream) {
2a174661
DG
1088 ret = -1;
1089 goto end_unlock;
1090 }
1091
6763619c
JD
1092 session = session_find_by_id(conn->sessions_ht, vstream->session_id);
1093 if (!session) {
d3e2ba59
JD
1094 ret = -1;
1095 goto end_unlock;
1096 }
1097
2a174661
DG
1098 ctf_trace = ctf_trace_find_by_path(session->ctf_traces_ht, vstream->path_name);
1099 assert(ctf_trace);
1100
d3e2ba59
JD
1101 memset(&viewer_index, 0, sizeof(viewer_index));
1102
1103 /*
1104 * The viewer should not ask for index on metadata stream.
1105 */
1106 if (vstream->metadata_flag) {
c4e361a4 1107 viewer_index.status = htobe32(LTTNG_VIEWER_INDEX_HUP);
d3e2ba59
JD
1108 goto send_reply;
1109 }
1110
1111 /* First time, we open the index file */
1112 if (vstream->index_read_fd < 0) {
2f8f53af
DG
1113 ret = index_open(vstream->path_name, vstream->channel_name,
1114 vstream->tracefile_count, vstream->tracefile_count_current);
0e6830aa 1115 if (ret == -ENOENT) {
d3e2ba59
JD
1116 /*
1117 * The index is created only when the first data packet arrives, it
1118 * might not be ready at the beginning of the session
1119 */
c4e361a4 1120 viewer_index.status = htobe32(LTTNG_VIEWER_INDEX_RETRY);
d3e2ba59
JD
1121 goto send_reply;
1122 } else if (ret < 0) {
c4e361a4 1123 viewer_index.status = htobe32(LTTNG_VIEWER_INDEX_ERR);
d3e2ba59
JD
1124 goto send_reply;
1125 }
2f8f53af 1126 vstream->index_read_fd = ret;
d3e2ba59
JD
1127 }
1128
2a174661
DG
1129 rstream = stream_find_by_id(relay_streams_ht, vstream->stream_handle);
1130 assert(rstream);
1131
1132 if (!rstream->close_flag) {
6b6b9a5a
JD
1133 if (vstream->abort_flag) {
1134 /* Rotate on abort (overwrite). */
1135 DBG("Viewer rotate because of overwrite");
2f8f53af 1136 ret = viewer_stream_rotate(vstream, rstream);
6b6b9a5a
JD
1137 if (ret < 0) {
1138 goto end_unlock;
a020f610 1139 } else if (ret == 1) {
c4e361a4 1140 viewer_index.status = htobe32(LTTNG_VIEWER_INDEX_HUP);
2f8f53af 1141 viewer_stream_delete(vstream);
2a174661 1142 viewer_stream_destroy(ctf_trace, vstream);
a020f610 1143 goto send_reply;
6b6b9a5a 1144 }
2a174661 1145 /* ret == 0 means successful so we continue. */
6b6b9a5a 1146 }
2a174661 1147
6b6b9a5a 1148 pthread_mutex_lock(&rstream->viewer_stream_rotation_lock);
cef0f7d5
JD
1149 if (rstream->tracefile_count_current == vstream->tracefile_count_current) {
1150 if (rstream->beacon_ts_end != -1ULL &&
1151 vstream->last_sent_index == rstream->total_index_received) {
c4e361a4 1152 viewer_index.status = htobe32(LTTNG_VIEWER_INDEX_INACTIVE);
cef0f7d5
JD
1153 viewer_index.timestamp_end = htobe64(rstream->beacon_ts_end);
1154 pthread_mutex_unlock(&rstream->viewer_stream_rotation_lock);
1155 goto send_reply;
cef0f7d5
JD
1156 } else if (rstream->total_index_received <= vstream->last_sent_index
1157 && !vstream->close_write_flag) {
2a174661
DG
1158 /*
1159 * Reader and writer are working in the same tracefile, so we care
1160 * about the number of index received and sent. Otherwise, we read
1161 * up to EOF.
1162 */
cef0f7d5
JD
1163 pthread_mutex_unlock(&rstream->viewer_stream_rotation_lock);
1164 /* No new index to send, retry later. */
c4e361a4 1165 viewer_index.status = htobe32(LTTNG_VIEWER_INDEX_RETRY);
cef0f7d5
JD
1166 goto send_reply;
1167 }
d3e2ba59 1168 }
6b6b9a5a 1169 pthread_mutex_unlock(&rstream->viewer_stream_rotation_lock);
2a174661 1170 } else if (rstream->close_flag && vstream->close_write_flag &&
d3e2ba59 1171 vstream->total_index_received == vstream->last_sent_index) {
6b6b9a5a 1172 /* Last index sent and current tracefile closed in write */
c4e361a4 1173 viewer_index.status = htobe32(LTTNG_VIEWER_INDEX_HUP);
2f8f53af 1174 viewer_stream_delete(vstream);
2a174661 1175 viewer_stream_destroy(ctf_trace, vstream);
d3e2ba59 1176 goto send_reply;
6b6b9a5a
JD
1177 } else {
1178 vstream->close_write_flag = 1;
d3e2ba59
JD
1179 }
1180
2a174661
DG
1181 if (!ctf_trace->metadata_received ||
1182 ctf_trace->metadata_received > ctf_trace->metadata_sent) {
d3e2ba59
JD
1183 viewer_index.flags |= LTTNG_VIEWER_FLAG_NEW_METADATA;
1184 }
1185
58eb9381 1186 ret = check_new_streams(vstream->session_id, conn->sessions_ht);
4a9daf17
JD
1187 if (ret < 0) {
1188 goto end_unlock;
1189 } else if (ret == 1) {
1190 viewer_index.flags |= LTTNG_VIEWER_FLAG_NEW_STREAM;
1191 }
1192
cef0f7d5
JD
1193 pthread_mutex_lock(&vstream->overwrite_lock);
1194 if (vstream->abort_flag) {
1195 /*
2a174661 1196 * The file is being overwritten by the writer, we cannot * use it.
cef0f7d5 1197 */
c4e361a4 1198 viewer_index.status = htobe32(LTTNG_VIEWER_INDEX_RETRY);
cef0f7d5 1199 pthread_mutex_unlock(&vstream->overwrite_lock);
2f8f53af 1200 ret = viewer_stream_rotate(vstream, rstream);
cef0f7d5
JD
1201 if (ret < 0) {
1202 goto end_unlock;
a020f610 1203 } else if (ret == 1) {
c4e361a4 1204 viewer_index.status = htobe32(LTTNG_VIEWER_INDEX_HUP);
2f8f53af 1205 viewer_stream_delete(vstream);
2a174661 1206 viewer_stream_destroy(ctf_trace, vstream);
a020f610 1207 goto send_reply;
cef0f7d5
JD
1208 }
1209 goto send_reply;
1210 }
2f8f53af 1211
6cd525e8
MD
1212 ret = lttng_read(vstream->index_read_fd, &packet_index,
1213 sizeof(packet_index));
cef0f7d5 1214 pthread_mutex_unlock(&vstream->overwrite_lock);
d3e2ba59 1215 if (ret < sizeof(packet_index)) {
6b6b9a5a
JD
1216 /*
1217 * The tracefile is closed in write, so we read up to EOF.
1218 */
1219 if (vstream->close_write_flag == 1) {
c4e361a4 1220 viewer_index.status = htobe32(LTTNG_VIEWER_INDEX_RETRY);
6b6b9a5a 1221 /* Rotate on normal EOF */
2f8f53af 1222 ret = viewer_stream_rotate(vstream, rstream);
6b6b9a5a
JD
1223 if (ret < 0) {
1224 goto end_unlock;
a020f610 1225 } else if (ret == 1) {
c4e361a4 1226 viewer_index.status = htobe32(LTTNG_VIEWER_INDEX_HUP);
2f8f53af 1227 viewer_stream_delete(vstream);
2a174661 1228 viewer_stream_destroy(ctf_trace, vstream);
a020f610 1229 goto send_reply;
6b6b9a5a
JD
1230 }
1231 } else {
2f8f53af 1232 PERROR("Relay reading index file %d", vstream->index_read_fd);
c4e361a4 1233 viewer_index.status = htobe32(LTTNG_VIEWER_INDEX_ERR);
6b6b9a5a
JD
1234 }
1235 goto send_reply;
d3e2ba59 1236 } else {
c4e361a4 1237 viewer_index.status = htobe32(LTTNG_VIEWER_INDEX_OK);
d3e2ba59
JD
1238 vstream->last_sent_index++;
1239 }
1240
1241 /*
1242 * Indexes are stored in big endian, no need to switch before sending.
1243 */
1244 viewer_index.offset = packet_index.offset;
1245 viewer_index.packet_size = packet_index.packet_size;
1246 viewer_index.content_size = packet_index.content_size;
1247 viewer_index.timestamp_begin = packet_index.timestamp_begin;
1248 viewer_index.timestamp_end = packet_index.timestamp_end;
1249 viewer_index.events_discarded = packet_index.events_discarded;
1250 viewer_index.stream_id = packet_index.stream_id;
1251
1252send_reply:
1253 viewer_index.flags = htobe32(viewer_index.flags);
eea7556c 1254 health_code_update();
2f8f53af 1255
58eb9381 1256 ret = send_response(conn->sock, &viewer_index, sizeof(viewer_index));
d3e2ba59 1257 if (ret < 0) {
d3e2ba59
JD
1258 goto end_unlock;
1259 }
eea7556c 1260 health_code_update();
d3e2ba59 1261
2f8f53af 1262 DBG("Index %" PRIu64 " for stream %" PRIu64 " sent",
d3e2ba59
JD
1263 vstream->last_sent_index, vstream->stream_handle);
1264
1265end_unlock:
1266 rcu_read_unlock();
1267
d3e2ba59
JD
1268end:
1269 return ret;
1270}
1271
1272/*
1273 * Send the next index for a stream
1274 *
1275 * Return 0 on success or else a negative value.
1276 */
1277static
58eb9381 1278int viewer_get_packet(struct relay_connection *conn)
d3e2ba59
JD
1279{
1280 int ret, send_data = 0;
1281 char *data = NULL;
1282 uint32_t len = 0;
1283 ssize_t read_len;
1284 struct lttng_viewer_get_packet get_packet_info;
1285 struct lttng_viewer_trace_packet reply;
1286 struct relay_viewer_stream *stream;
6763619c 1287 struct relay_session *session;
2a174661 1288 struct ctf_trace *ctf_trace;
d3e2ba59 1289
58eb9381 1290 assert(conn);
d3e2ba59
JD
1291
1292 DBG2("Relay get data packet");
1293
eea7556c 1294 health_code_update();
2f8f53af 1295
58eb9381 1296 ret = recv_request(conn->sock, &get_packet_info, sizeof(get_packet_info));
2f8f53af 1297 if (ret < 0) {
d3e2ba59
JD
1298 goto end;
1299 }
eea7556c 1300 health_code_update();
d3e2ba59 1301
0233a6a5
DG
1302 /* From this point on, the error label can be reached. */
1303 memset(&reply, 0, sizeof(reply));
1304
d3e2ba59 1305 rcu_read_lock();
2f8f53af 1306 stream = viewer_stream_find_by_id(be64toh(get_packet_info.stream_id));
d3e2ba59
JD
1307 if (!stream) {
1308 goto error;
1309 }
2a174661 1310
6763619c
JD
1311 session = session_find_by_id(conn->sessions_ht, stream->session_id);
1312 if (!session) {
1313 ret = -1;
1314 goto error;
1315 }
1316
1317 ctf_trace = ctf_trace_find_by_path(session->ctf_traces_ht,
2a174661
DG
1318 stream->path_name);
1319 assert(ctf_trace);
d3e2ba59
JD
1320
1321 /*
1322 * First time we read this stream, we need open the tracefile, we should
1323 * only arrive here if an index has already been sent to the viewer, so the
1324 * tracefile must exist, if it does not it is a fatal error.
1325 */
1326 if (stream->read_fd < 0) {
1327 char fullpath[PATH_MAX];
1328
6b6b9a5a
JD
1329 if (stream->tracefile_count > 0) {
1330 ret = snprintf(fullpath, PATH_MAX, "%s/%s_%" PRIu64, stream->path_name,
1331 stream->channel_name,
1332 stream->tracefile_count_current);
1333 } else {
1334 ret = snprintf(fullpath, PATH_MAX, "%s/%s", stream->path_name,
1335 stream->channel_name);
1336 }
d3e2ba59
JD
1337 if (ret < 0) {
1338 goto error;
1339 }
1340 ret = open(fullpath, O_RDONLY);
1341 if (ret < 0) {
1342 PERROR("Relay opening trace file");
1343 goto error;
1344 }
1345 stream->read_fd = ret;
1346 }
1347
2a174661
DG
1348 if (!ctf_trace->metadata_received ||
1349 ctf_trace->metadata_received > ctf_trace->metadata_sent) {
c4e361a4 1350 reply.status = htobe32(LTTNG_VIEWER_GET_PACKET_ERR);
d3e2ba59 1351 reply.flags |= LTTNG_VIEWER_FLAG_NEW_METADATA;
d3e2ba59
JD
1352 goto send_reply;
1353 }
1354
58eb9381 1355 ret = check_new_streams(stream->session_id, conn->sessions_ht);
4a9daf17
JD
1356 if (ret < 0) {
1357 goto end_unlock;
1358 } else if (ret == 1) {
c4e361a4 1359 reply.status = htobe32(LTTNG_VIEWER_GET_PACKET_ERR);
4a9daf17
JD
1360 reply.flags |= LTTNG_VIEWER_FLAG_NEW_STREAM;
1361 goto send_reply;
1362 }
1363
d3e2ba59
JD
1364 len = be32toh(get_packet_info.len);
1365 data = zmalloc(len);
1366 if (!data) {
1367 PERROR("relay data zmalloc");
1368 goto error;
1369 }
1370
1371 ret = lseek(stream->read_fd, be64toh(get_packet_info.offset), SEEK_SET);
1372 if (ret < 0) {
6b6b9a5a
JD
1373 /*
1374 * If the read fd was closed by the streaming side, the
1375 * abort_flag will be set to 1, otherwise it is an error.
1376 */
1377 if (stream->abort_flag == 0) {
1378 PERROR("lseek");
1379 goto error;
1380 }
c4e361a4 1381 reply.status = htobe32(LTTNG_VIEWER_GET_PACKET_EOF);
6b6b9a5a 1382 goto send_reply;
d3e2ba59 1383 }
6cd525e8
MD
1384 read_len = lttng_read(stream->read_fd, data, len);
1385 if (read_len < len) {
6b6b9a5a
JD
1386 /*
1387 * If the read fd was closed by the streaming side, the
1388 * abort_flag will be set to 1, otherwise it is an error.
1389 */
1390 if (stream->abort_flag == 0) {
1391 PERROR("Relay reading trace file, fd: %d, offset: %" PRIu64,
1392 stream->read_fd,
1393 be64toh(get_packet_info.offset));
1394 goto error;
1395 } else {
c4e361a4 1396 reply.status = htobe32(LTTNG_VIEWER_GET_PACKET_EOF);
6b6b9a5a
JD
1397 goto send_reply;
1398 }
d3e2ba59 1399 }
c4e361a4 1400 reply.status = htobe32(LTTNG_VIEWER_GET_PACKET_OK);
d3e2ba59
JD
1401 reply.len = htobe32(len);
1402 send_data = 1;
1403 goto send_reply;
1404
1405error:
c4e361a4 1406 reply.status = htobe32(LTTNG_VIEWER_GET_PACKET_ERR);
d3e2ba59
JD
1407
1408send_reply:
1409 reply.flags = htobe32(reply.flags);
eea7556c
MD
1410
1411 health_code_update();
2f8f53af 1412
58eb9381 1413 ret = send_response(conn->sock, &reply, sizeof(reply));
d3e2ba59 1414 if (ret < 0) {
d3e2ba59
JD
1415 goto end_unlock;
1416 }
eea7556c 1417 health_code_update();
d3e2ba59
JD
1418
1419 if (send_data) {
eea7556c 1420 health_code_update();
58eb9381 1421 ret = send_response(conn->sock, data, len);
d3e2ba59 1422 if (ret < 0) {
d3e2ba59
JD
1423 goto end_unlock;
1424 }
eea7556c 1425 health_code_update();
d3e2ba59
JD
1426 }
1427
1428 DBG("Sent %u bytes for stream %" PRIu64, len,
1429 be64toh(get_packet_info.stream_id));
1430
1431end_unlock:
1432 free(data);
1433 rcu_read_unlock();
1434
1435end:
1436 return ret;
1437}
1438
1439/*
1440 * Send the session's metadata
1441 *
1442 * Return 0 on success else a negative value.
1443 */
1444static
58eb9381 1445int viewer_get_metadata(struct relay_connection *conn)
d3e2ba59
JD
1446{
1447 int ret = 0;
1448 ssize_t read_len;
1449 uint64_t len = 0;
1450 char *data = NULL;
1451 struct lttng_viewer_get_metadata request;
1452 struct lttng_viewer_metadata_packet reply;
1453 struct relay_viewer_stream *stream;
2a174661 1454 struct ctf_trace *ctf_trace;
6763619c 1455 struct relay_session *session;
d3e2ba59 1456
58eb9381 1457 assert(conn);
d3e2ba59
JD
1458
1459 DBG("Relay get metadata");
1460
eea7556c 1461 health_code_update();
2f8f53af 1462
58eb9381 1463 ret = recv_request(conn->sock, &request, sizeof(request));
2f8f53af 1464 if (ret < 0) {
d3e2ba59
JD
1465 goto end;
1466 }
eea7556c 1467 health_code_update();
d3e2ba59
JD
1468
1469 rcu_read_lock();
2f8f53af 1470 stream = viewer_stream_find_by_id(be64toh(request.stream_id));
d3e2ba59
JD
1471 if (!stream || !stream->metadata_flag) {
1472 ERR("Invalid metadata stream");
1473 goto error;
1474 }
d3e2ba59 1475
6763619c
JD
1476 session = session_find_by_id(conn->sessions_ht, stream->session_id);
1477 if (!session) {
1478 ret = -1;
1479 goto error;
1480 }
1481
1482 ctf_trace = ctf_trace_find_by_path(session->ctf_traces_ht,
2a174661
DG
1483 stream->path_name);
1484 assert(ctf_trace);
1485 assert(ctf_trace->metadata_sent <= ctf_trace->metadata_received);
1486
1487 len = ctf_trace->metadata_received - ctf_trace->metadata_sent;
d3e2ba59 1488 if (len == 0) {
c4e361a4 1489 reply.status = htobe32(LTTNG_VIEWER_NO_NEW_METADATA);
d3e2ba59
JD
1490 goto send_reply;
1491 }
1492
1493 /* first time, we open the metadata file */
1494 if (stream->read_fd < 0) {
1495 char fullpath[PATH_MAX];
1496
1497 ret = snprintf(fullpath, PATH_MAX, "%s/%s", stream->path_name,
1498 stream->channel_name);
1499 if (ret < 0) {
1500 goto error;
1501 }
1502 ret = open(fullpath, O_RDONLY);
1503 if (ret < 0) {
1504 PERROR("Relay opening metadata file");
1505 goto error;
1506 }
1507 stream->read_fd = ret;
1508 }
1509
1510 reply.len = htobe64(len);
1511 data = zmalloc(len);
1512 if (!data) {
1513 PERROR("viewer metadata zmalloc");
1514 goto error;
1515 }
1516
6cd525e8
MD
1517 read_len = lttng_read(stream->read_fd, data, len);
1518 if (read_len < len) {
d3e2ba59
JD
1519 PERROR("Relay reading metadata file");
1520 goto error;
1521 }
2a174661 1522 ctf_trace->metadata_sent += read_len;
c4e361a4 1523 reply.status = htobe32(LTTNG_VIEWER_METADATA_OK);
d3e2ba59
JD
1524 goto send_reply;
1525
1526error:
c4e361a4 1527 reply.status = htobe32(LTTNG_VIEWER_METADATA_ERR);
d3e2ba59
JD
1528
1529send_reply:
eea7556c 1530 health_code_update();
58eb9381 1531 ret = send_response(conn->sock, &reply, sizeof(reply));
d3e2ba59 1532 if (ret < 0) {
d3e2ba59
JD
1533 goto end_unlock;
1534 }
eea7556c 1535 health_code_update();
d3e2ba59
JD
1536
1537 if (len > 0) {
58eb9381 1538 ret = send_response(conn->sock, data, len);
d3e2ba59 1539 if (ret < 0) {
d3e2ba59
JD
1540 goto end_unlock;
1541 }
1542 }
1543
1544 DBG("Sent %" PRIu64 " bytes of metadata for stream %" PRIu64, len,
1545 be64toh(request.stream_id));
1546
1547 DBG("Metadata sent");
1548
1549end_unlock:
1550 free(data);
1551 rcu_read_unlock();
1552end:
1553 return ret;
1554}
1555
c3b7390b
JD
1556/*
1557 * Create a viewer session.
1558 *
1559 * Return 0 on success or else a negative value.
1560 */
1561static
1562int viewer_create_session(struct relay_connection *conn)
1563{
1564 int ret;
1565 struct lttng_viewer_create_session_response resp;
1566
1567 DBG("Viewer create session received");
1568
1569 resp.status = htobe32(LTTNG_VIEWER_CREATE_SESSION_OK);
1570 conn->viewer_session = zmalloc(sizeof(conn->viewer_session));
1571 if (!conn->viewer_session) {
1572 ERR("Allocation viewer session");
1573 resp.status = htobe32(LTTNG_VIEWER_CREATE_SESSION_ERR);
1574 goto send_reply;
1575 }
1576 CDS_INIT_LIST_HEAD(&conn->viewer_session->sessions_head);
1577
1578send_reply:
1579 health_code_update();
1580 ret = send_response(conn->sock, &resp, sizeof(resp));
1581 if (ret < 0) {
1582 goto end;
1583 }
1584 health_code_update();
1585 ret = 0;
1586
1587end:
1588 return ret;
1589}
1590
1591
d3e2ba59
JD
1592/*
1593 * live_relay_unknown_command: send -1 if received unknown command
1594 */
1595static
58eb9381 1596void live_relay_unknown_command(struct relay_connection *conn)
d3e2ba59
JD
1597{
1598 struct lttcomm_relayd_generic_reply reply;
d3e2ba59
JD
1599
1600 reply.ret_code = htobe32(LTTNG_ERR_UNK);
58eb9381 1601 (void) send_response(conn->sock, &reply, sizeof(reply));
d3e2ba59
JD
1602}
1603
1604/*
1605 * Process the commands received on the control socket
1606 */
1607static
1608int process_control(struct lttng_viewer_cmd *recv_hdr,
58eb9381 1609 struct relay_connection *conn)
d3e2ba59
JD
1610{
1611 int ret = 0;
2f8f53af
DG
1612 uint32_t msg_value;
1613
1614 assert(recv_hdr);
58eb9381 1615 assert(conn);
2f8f53af
DG
1616
1617 msg_value = be32toh(recv_hdr->cmd);
1618
1619 /*
1620 * Make sure we've done the version check before any command other then a
1621 * new client connection.
1622 */
c4e361a4 1623 if (msg_value != LTTNG_VIEWER_CONNECT && !conn->version_check_done) {
58eb9381 1624 ERR("Viewer conn value %" PRIu32 " before version check", msg_value);
2f8f53af
DG
1625 ret = -1;
1626 goto end;
1627 }
d3e2ba59 1628
2f8f53af 1629 switch (msg_value) {
c4e361a4 1630 case LTTNG_VIEWER_CONNECT:
58eb9381 1631 ret = viewer_connect(conn);
d3e2ba59 1632 break;
c4e361a4 1633 case LTTNG_VIEWER_LIST_SESSIONS:
58eb9381 1634 ret = viewer_list_sessions(conn);
d3e2ba59 1635 break;
c4e361a4 1636 case LTTNG_VIEWER_ATTACH_SESSION:
58eb9381 1637 ret = viewer_attach_session(conn);
d3e2ba59 1638 break;
c4e361a4 1639 case LTTNG_VIEWER_GET_NEXT_INDEX:
58eb9381 1640 ret = viewer_get_next_index(conn);
d3e2ba59 1641 break;
c4e361a4 1642 case LTTNG_VIEWER_GET_PACKET:
58eb9381 1643 ret = viewer_get_packet(conn);
d3e2ba59 1644 break;
c4e361a4 1645 case LTTNG_VIEWER_GET_METADATA:
58eb9381 1646 ret = viewer_get_metadata(conn);
d3e2ba59 1647 break;
c4e361a4 1648 case LTTNG_VIEWER_GET_NEW_STREAMS:
58eb9381 1649 ret = viewer_get_new_streams(conn);
80e8027a 1650 break;
c3b7390b
JD
1651 case LTTNG_VIEWER_CREATE_SESSION:
1652 ret = viewer_create_session(conn);
1653 break;
d3e2ba59
JD
1654 default:
1655 ERR("Received unknown viewer command (%u)", be32toh(recv_hdr->cmd));
58eb9381 1656 live_relay_unknown_command(conn);
d3e2ba59
JD
1657 ret = -1;
1658 goto end;
1659 }
1660
1661end:
1662 return ret;
1663}
1664
1665static
58eb9381 1666void cleanup_connection_pollfd(struct lttng_poll_event *events, int pollfd)
d3e2ba59
JD
1667{
1668 int ret;
1669
1670 assert(events);
1671
58eb9381 1672 (void) lttng_poll_del(events, pollfd);
d3e2ba59
JD
1673
1674 ret = close(pollfd);
1675 if (ret < 0) {
1676 ERR("Closing pollfd %d", pollfd);
1677 }
1678}
1679
157df586
JD
1680/*
1681 * Delete all streams for a specific session ID.
1682 */
2a174661 1683static void destroy_viewer_streams_by_session(struct relay_session *session)
d3e2ba59 1684{
d3e2ba59 1685 struct relay_viewer_stream *stream;
d3e2ba59
JD
1686 struct lttng_ht_iter iter;
1687
2a174661
DG
1688 assert(session);
1689
d3e2ba59 1690 rcu_read_lock();
157df586
JD
1691 cds_lfht_for_each_entry(viewer_streams_ht->ht, &iter.iter, stream,
1692 stream_n.node) {
2a174661 1693 struct ctf_trace *ctf_trace;
eea7556c 1694
2a174661
DG
1695 health_code_update();
1696 if (stream->session_id != session->id) {
d3e2ba59
JD
1697 continue;
1698 }
1699
2a174661
DG
1700 ctf_trace = ctf_trace_find_by_path(session->ctf_traces_ht,
1701 stream->path_name);
1702 assert(ctf_trace);
1703
2f8f53af 1704 viewer_stream_delete(stream);
157df586
JD
1705
1706 if (stream->metadata_flag) {
2a174661
DG
1707 ctf_trace->metadata_sent = 0;
1708 ctf_trace->viewer_metadata_stream = NULL;
d3e2ba59 1709 }
2a174661
DG
1710
1711 viewer_stream_destroy(ctf_trace, stream);
d3e2ba59
JD
1712 }
1713 rcu_read_unlock();
1714}
1715
2a174661
DG
1716static void try_destroy_streams(struct relay_session *session)
1717{
1718 struct ctf_trace *ctf_trace;
1719 struct lttng_ht_iter iter;
1720
1721 assert(session);
1722
1723 cds_lfht_for_each_entry(session->ctf_traces_ht->ht, &iter.iter, ctf_trace,
1724 node.node) {
1725 /* Attempt to destroy the ctf trace of that session. */
1726 ctf_trace_try_destroy(session, ctf_trace);
1727 }
1728}
1729
d3e2ba59 1730/*
58eb9381 1731 * Delete and destroy a connection.
d3e2ba59
JD
1732 *
1733 * RCU read side lock MUST be acquired.
1734 */
58eb9381
DG
1735static void destroy_connection(struct lttng_ht *relay_connections_ht,
1736 struct relay_connection *conn)
d3e2ba59 1737{
6763619c 1738 struct relay_session *session, *tmp_session;
d3e2ba59
JD
1739
1740 assert(relay_connections_ht);
58eb9381 1741 assert(conn);
d3e2ba59 1742
58eb9381 1743 connection_delete(relay_connections_ht, conn);
d3e2ba59 1744
6763619c
JD
1745 if (!conn->viewer_session) {
1746 goto end;
1747 }
1748
58eb9381 1749 rcu_read_lock();
6763619c
JD
1750 cds_list_for_each_entry_safe(session, tmp_session,
1751 &conn->viewer_session->sessions_head,
1752 viewer_session_list) {
1753 DBG("Cleaning connection of session ID %" PRIu64, session->id);
2a174661
DG
1754 /*
1755 * Very important that this is done before destroying the session so we
1756 * can put back every viewer stream reference from the ctf_trace.
1757 */
1758 destroy_viewer_streams_by_session(session);
1759 try_destroy_streams(session);
6763619c 1760 cds_list_del(&session->viewer_session_list);
58eb9381 1761 session_viewer_try_destroy(conn->sessions_ht, session);
2a174661
DG
1762 }
1763 rcu_read_unlock();
d3e2ba59 1764
6763619c 1765end:
58eb9381 1766 connection_destroy(conn);
d3e2ba59
JD
1767}
1768
1769/*
1770 * This thread does the actual work
1771 */
1772static
1773void *thread_worker(void *data)
1774{
1775 int ret, err = -1;
1776 uint32_t nb_fd;
58eb9381 1777 struct relay_connection *conn;
d3e2ba59
JD
1778 struct lttng_poll_event events;
1779 struct lttng_ht *relay_connections_ht;
d3e2ba59
JD
1780 struct lttng_ht_iter iter;
1781 struct lttng_viewer_cmd recv_hdr;
1782 struct relay_local_data *relay_ctx = (struct relay_local_data *) data;
1783 struct lttng_ht *sessions_ht = relay_ctx->sessions_ht;
d3e2ba59
JD
1784
1785 DBG("[thread] Live viewer relay worker started");
1786
1787 rcu_register_thread();
1788
eea7556c
MD
1789 health_register(health_relayd, HEALTH_RELAYD_TYPE_LIVE_WORKER);
1790
9b5e0863
MD
1791 if (testpoint(relayd_thread_live_worker)) {
1792 goto error_testpoint;
1793 }
1794
d3e2ba59
JD
1795 /* table of connections indexed on socket */
1796 relay_connections_ht = lttng_ht_new(0, LTTNG_HT_TYPE_ULONG);
1797 if (!relay_connections_ht) {
1798 goto relay_connections_ht_error;
1799 }
1800
1801 ret = create_thread_poll_set(&events, 2);
1802 if (ret < 0) {
1803 goto error_poll_create;
1804 }
1805
58eb9381 1806 ret = lttng_poll_add(&events, live_conn_pipe[0], LPOLLIN | LPOLLRDHUP);
d3e2ba59
JD
1807 if (ret < 0) {
1808 goto error;
1809 }
1810
1811restart:
1812 while (1) {
1813 int i;
1814
eea7556c
MD
1815 health_code_update();
1816
d3e2ba59
JD
1817 /* Infinite blocking call, waiting for transmission */
1818 DBG3("Relayd live viewer worker thread polling...");
eea7556c 1819 health_poll_entry();
d3e2ba59 1820 ret = lttng_poll_wait(&events, -1);
eea7556c 1821 health_poll_exit();
d3e2ba59
JD
1822 if (ret < 0) {
1823 /*
1824 * Restart interrupted system call.
1825 */
1826 if (errno == EINTR) {
1827 goto restart;
1828 }
1829 goto error;
1830 }
1831
1832 nb_fd = ret;
1833
1834 /*
1835 * Process control. The control connection is prioritised so we don't
1836 * starve it with high throughput tracing data on the data
1837 * connection.
1838 */
1839 for (i = 0; i < nb_fd; i++) {
1840 /* Fetch once the poll data */
1841 uint32_t revents = LTTNG_POLL_GETEV(&events, i);
1842 int pollfd = LTTNG_POLL_GETFD(&events, i);
1843
eea7556c
MD
1844 health_code_update();
1845
d3e2ba59 1846 /* Thread quit pipe has been closed. Killing thread. */
2a174661 1847 ret = check_live_conn_pipe(pollfd, revents);
d3e2ba59
JD
1848 if (ret) {
1849 err = 0;
1850 goto exit;
1851 }
1852
58eb9381
DG
1853 /* Inspect the relay conn pipe for new connection */
1854 if (pollfd == live_conn_pipe[0]) {
d3e2ba59
JD
1855 if (revents & (LPOLLERR | LPOLLHUP | LPOLLRDHUP)) {
1856 ERR("Relay live pipe error");
1857 goto error;
1858 } else if (revents & LPOLLIN) {
58eb9381 1859 ret = lttng_read(live_conn_pipe[0], &conn, sizeof(conn));
d3e2ba59
JD
1860 if (ret < 0) {
1861 goto error;
1862 }
58eb9381
DG
1863 conn->sessions_ht = sessions_ht;
1864 connection_init(conn);
1865 lttng_poll_add(&events, conn->sock->fd,
1866 LPOLLIN | LPOLLRDHUP);
1867 rcu_read_lock();
1868 lttng_ht_add_unique_ulong(relay_connections_ht,
1869 &conn->sock_n);
d3e2ba59 1870 rcu_read_unlock();
58eb9381 1871 DBG("Connection socket %d added", conn->sock->fd);
d3e2ba59 1872 }
58eb9381
DG
1873 } else {
1874 rcu_read_lock();
1875 conn = connection_find_by_sock(relay_connections_ht, pollfd);
1876 /* If not found, there is a synchronization issue. */
1877 assert(conn);
1878
1879 if (revents & (LPOLLERR | LPOLLHUP | LPOLLRDHUP)) {
1880 cleanup_connection_pollfd(&events, pollfd);
1881 destroy_connection(relay_connections_ht, conn);
d3e2ba59 1882 } else if (revents & LPOLLIN) {
58eb9381
DG
1883 ret = conn->sock->ops->recvmsg(conn->sock, &recv_hdr,
1884 sizeof(recv_hdr), 0);
d3e2ba59 1885 if (ret <= 0) {
58eb9381
DG
1886 /* Connection closed */
1887 cleanup_connection_pollfd(&events, pollfd);
1888 destroy_connection(relay_connections_ht, conn);
1889 DBG("Viewer control conn closed with %d", pollfd);
d3e2ba59 1890 } else {
58eb9381 1891 ret = process_control(&recv_hdr, conn);
d3e2ba59
JD
1892 if (ret < 0) {
1893 /* Clear the session on error. */
58eb9381
DG
1894 cleanup_connection_pollfd(&events, pollfd);
1895 destroy_connection(relay_connections_ht, conn);
d3e2ba59
JD
1896 DBG("Viewer connection closed with %d", pollfd);
1897 }
1898 }
1899 }
1900 rcu_read_unlock();
1901 }
1902 }
1903 }
1904
1905exit:
1906error:
1907 lttng_poll_clean(&events);
1908
58eb9381 1909 /* Cleanup reamaining connection object. */
d3e2ba59 1910 rcu_read_lock();
58eb9381
DG
1911 cds_lfht_for_each_entry(relay_connections_ht->ht, &iter.iter, conn,
1912 sock_n.node) {
eea7556c 1913 health_code_update();
58eb9381 1914 destroy_connection(relay_connections_ht, conn);
d3e2ba59
JD
1915 }
1916 rcu_read_unlock();
1917error_poll_create:
1918 lttng_ht_destroy(relay_connections_ht);
1919relay_connections_ht_error:
58eb9381
DG
1920 /* Close relay conn pipes */
1921 utils_close_pipe(live_conn_pipe);
d3e2ba59
JD
1922 if (err) {
1923 DBG("Viewer worker thread exited with error");
1924 }
1925 DBG("Viewer worker thread cleanup complete");
9b5e0863 1926error_testpoint:
eea7556c
MD
1927 if (err) {
1928 health_error();
1929 ERR("Health error occurred in %s", __func__);
1930 }
1931 health_unregister(health_relayd);
d3e2ba59
JD
1932 stop_threads();
1933 rcu_unregister_thread();
1934 return NULL;
1935}
1936
1937/*
1938 * Create the relay command pipe to wake thread_manage_apps.
1939 * Closed in cleanup().
1940 */
58eb9381 1941static int create_conn_pipe(void)
d3e2ba59
JD
1942{
1943 int ret;
1944
58eb9381 1945 ret = utils_create_pipe_cloexec(live_conn_pipe);
d3e2ba59
JD
1946
1947 return ret;
1948}
1949
aaec7998 1950void live_stop_threads(void)
d3e2ba59
JD
1951{
1952 int ret;
1953 void *status;
1954
1955 stop_threads();
1956
1957 ret = pthread_join(live_listener_thread, &status);
1958 if (ret != 0) {
1959 PERROR("pthread_join live listener");
1960 goto error; /* join error, exit without cleanup */
1961 }
1962
1963 ret = pthread_join(live_worker_thread, &status);
1964 if (ret != 0) {
1965 PERROR("pthread_join live worker");
1966 goto error; /* join error, exit without cleanup */
1967 }
1968
1969 ret = pthread_join(live_dispatcher_thread, &status);
1970 if (ret != 0) {
1971 PERROR("pthread_join live dispatcher");
1972 goto error; /* join error, exit without cleanup */
1973 }
1974
1975 cleanup();
1976
1977error:
1978 return;
1979}
1980
1981/*
1982 * main
1983 */
1984int live_start_threads(struct lttng_uri *uri,
0b242f62 1985 struct relay_local_data *relay_ctx)
d3e2ba59
JD
1986{
1987 int ret = 0;
1988 void *status;
1989 int is_root;
1990
1991 assert(uri);
1992 live_uri = uri;
1993
d3e2ba59
JD
1994 /* Check if daemon is UID = 0 */
1995 is_root = !getuid();
1996
1997 if (!is_root) {
1998 if (live_uri->port < 1024) {
1999 ERR("Need to be root to use ports < 1024");
2000 ret = -1;
2001 goto exit;
2002 }
2003 }
2004
2005 /* Setup the thread apps communication pipe. */
58eb9381 2006 if ((ret = create_conn_pipe()) < 0) {
d3e2ba59
JD
2007 goto exit;
2008 }
2009
2010 /* Init relay command queue. */
58eb9381 2011 cds_wfq_init(&viewer_conn_queue.queue);
d3e2ba59
JD
2012
2013 /* Set up max poll set size */
2014 lttng_poll_set_max_size();
2015
2016 /* Setup the dispatcher thread */
2017 ret = pthread_create(&live_dispatcher_thread, NULL,
2018 thread_dispatcher, (void *) NULL);
2019 if (ret != 0) {
2020 PERROR("pthread_create viewer dispatcher");
2021 goto exit_dispatcher;
2022 }
2023
2024 /* Setup the worker thread */
2025 ret = pthread_create(&live_worker_thread, NULL,
2026 thread_worker, relay_ctx);
2027 if (ret != 0) {
2028 PERROR("pthread_create viewer worker");
2029 goto exit_worker;
2030 }
2031
2032 /* Setup the listener thread */
2033 ret = pthread_create(&live_listener_thread, NULL,
2034 thread_listener, (void *) NULL);
2035 if (ret != 0) {
2036 PERROR("pthread_create viewer listener");
2037 goto exit_listener;
2038 }
2039
2040 ret = 0;
2041 goto end;
2042
2043exit_listener:
2044 ret = pthread_join(live_listener_thread, &status);
2045 if (ret != 0) {
2046 PERROR("pthread_join live listener");
2047 goto error; /* join error, exit without cleanup */
2048 }
2049
2050exit_worker:
2051 ret = pthread_join(live_worker_thread, &status);
2052 if (ret != 0) {
2053 PERROR("pthread_join live worker");
2054 goto error; /* join error, exit without cleanup */
2055 }
2056
2057exit_dispatcher:
2058 ret = pthread_join(live_dispatcher_thread, &status);
2059 if (ret != 0) {
2060 PERROR("pthread_join live dispatcher");
2061 goto error; /* join error, exit without cleanup */
2062 }
2063
2064exit:
2065 cleanup();
2066
2067end:
2068error:
2069 return ret;
2070}
This page took 0.12017 seconds and 4 git commands to generate.