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