Fix: undefined operation on last_relay_viewer_session_id
[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
6550503d
MD
518 if (!revents) {
519 /* No activity for this FD (poll implementation). */
520 continue;
521 }
522
d3e2ba59 523 /* Thread quit pipe has been closed. Killing thread. */
bcf4a440 524 ret = check_thread_quit_pipe(pollfd, revents);
d3e2ba59
JD
525 if (ret) {
526 err = 0;
527 goto exit;
528 }
529
530 if (revents & (LPOLLERR | LPOLLHUP | LPOLLRDHUP)) {
531 ERR("socket poll error");
532 goto error;
533 } else if (revents & LPOLLIN) {
534 /*
535 * Get allocated in this thread, enqueued to a global queue,
536 * dequeued and freed in the worker thread.
537 */
58eb9381
DG
538 int val = 1;
539 struct relay_connection *new_conn;
d3e2ba59
JD
540 struct lttcomm_sock *newsock;
541
58eb9381
DG
542 new_conn = connection_create();
543 if (!new_conn) {
d3e2ba59
JD
544 goto error;
545 }
546
d3e2ba59
JD
547 newsock = live_control_sock->ops->accept(live_control_sock);
548 if (!newsock) {
549 PERROR("accepting control sock");
58eb9381 550 connection_free(new_conn);
d3e2ba59
JD
551 goto error;
552 }
553 DBG("Relay viewer connection accepted socket %d", newsock->fd);
58eb9381 554
d3e2ba59 555 ret = setsockopt(newsock->fd, SOL_SOCKET, SO_REUSEADDR, &val,
58eb9381 556 sizeof(val));
d3e2ba59
JD
557 if (ret < 0) {
558 PERROR("setsockopt inet");
559 lttcomm_destroy_sock(newsock);
58eb9381 560 connection_free(new_conn);
d3e2ba59
JD
561 goto error;
562 }
58eb9381 563 new_conn->sock = newsock;
d3e2ba59 564
58eb9381
DG
565 /* Enqueue request for the dispatcher thread. */
566 cds_wfq_enqueue(&viewer_conn_queue.queue, &new_conn->qnode);
d3e2ba59
JD
567
568 /*
58eb9381
DG
569 * Wake the dispatch queue futex. Implicit memory barrier with
570 * the exchange in cds_wfq_enqueue.
d3e2ba59 571 */
58eb9381 572 futex_nto1_wake(&viewer_conn_queue.futex);
d3e2ba59
JD
573 }
574 }
575 }
576
577exit:
578error:
579error_poll_add:
9b5e0863 580error_testpoint:
d3e2ba59
JD
581 lttng_poll_clean(&events);
582error_create_poll:
583 if (live_control_sock->fd >= 0) {
584 ret = live_control_sock->ops->close(live_control_sock);
585 if (ret) {
586 PERROR("close");
587 }
588 }
589 lttcomm_destroy_sock(live_control_sock);
590error_sock_control:
591 if (err) {
eea7556c 592 health_error();
d3e2ba59
JD
593 DBG("Live viewer listener thread exited with error");
594 }
eea7556c 595 health_unregister(health_relayd);
d3e2ba59
JD
596 DBG("Live viewer listener thread cleanup complete");
597 stop_threads();
598 return NULL;
599}
600
601/*
602 * This thread manages the dispatching of the requests to worker threads
603 */
604static
605void *thread_dispatcher(void *data)
606{
6cd525e8
MD
607 int err = -1;
608 ssize_t ret;
d3e2ba59 609 struct cds_wfq_node *node;
58eb9381 610 struct relay_connection *conn = NULL;
d3e2ba59
JD
611
612 DBG("[thread] Live viewer relay dispatcher started");
613
eea7556c
MD
614 health_register(health_relayd, HEALTH_RELAYD_TYPE_LIVE_DISPATCHER);
615
9b5e0863
MD
616 if (testpoint(relayd_thread_live_dispatcher)) {
617 goto error_testpoint;
618 }
619
eea7556c
MD
620 health_code_update();
621
d3e2ba59 622 while (!CMM_LOAD_SHARED(live_dispatch_thread_exit)) {
eea7556c
MD
623 health_code_update();
624
d3e2ba59 625 /* Atomically prepare the queue futex */
58eb9381 626 futex_nto1_prepare(&viewer_conn_queue.futex);
d3e2ba59
JD
627
628 do {
eea7556c
MD
629 health_code_update();
630
d3e2ba59 631 /* Dequeue commands */
58eb9381 632 node = cds_wfq_dequeue_blocking(&viewer_conn_queue.queue);
d3e2ba59
JD
633 if (node == NULL) {
634 DBG("Woken up but nothing in the live-viewer "
635 "relay command queue");
636 /* Continue thread execution */
637 break;
638 }
58eb9381 639 conn = caa_container_of(node, struct relay_connection, qnode);
d3e2ba59 640 DBG("Dispatching viewer request waiting on sock %d",
58eb9381 641 conn->sock->fd);
d3e2ba59
JD
642
643 /*
644 * Inform worker thread of the new request. This call is blocking
645 * so we can be assured that the data will be read at some point in
646 * time or wait to the end of the world :)
647 */
58eb9381
DG
648 ret = lttng_write(live_conn_pipe[1], &conn, sizeof(conn));
649 if (ret < 0) {
650 PERROR("write conn pipe");
651 connection_destroy(conn);
d3e2ba59
JD
652 goto error;
653 }
654 } while (node != NULL);
655
656 /* Futex wait on queue. Blocking call on futex() */
eea7556c 657 health_poll_entry();
58eb9381 658 futex_nto1_wait(&viewer_conn_queue.futex);
eea7556c 659 health_poll_exit();
d3e2ba59
JD
660 }
661
eea7556c
MD
662 /* Normal exit, no error */
663 err = 0;
664
d3e2ba59 665error:
9b5e0863 666error_testpoint:
eea7556c
MD
667 if (err) {
668 health_error();
669 ERR("Health error occurred in %s", __func__);
670 }
671 health_unregister(health_relayd);
d3e2ba59
JD
672 DBG("Live viewer dispatch thread dying");
673 stop_threads();
674 return NULL;
675}
676
677/*
678 * Establish connection with the viewer and check the versions.
679 *
680 * Return 0 on success or else negative value.
681 */
682static
58eb9381 683int viewer_connect(struct relay_connection *conn)
d3e2ba59
JD
684{
685 int ret;
686 struct lttng_viewer_connect reply, msg;
687
58eb9381 688 assert(conn);
d3e2ba59 689
58eb9381 690 conn->version_check_done = 1;
d3e2ba59 691
eea7556c
MD
692 health_code_update();
693
2f8f53af
DG
694 DBG("Viewer is establishing a connection to the relayd.");
695
58eb9381 696 ret = recv_request(conn->sock, &msg, sizeof(msg));
2f8f53af 697 if (ret < 0) {
d3e2ba59
JD
698 goto end;
699 }
700
eea7556c
MD
701 health_code_update();
702
f46b2ce6 703 memset(&reply, 0, sizeof(reply));
d3e2ba59
JD
704 reply.major = RELAYD_VERSION_COMM_MAJOR;
705 reply.minor = RELAYD_VERSION_COMM_MINOR;
706
707 /* Major versions must be the same */
708 if (reply.major != be32toh(msg.major)) {
2f8f53af
DG
709 DBG("Incompatible major versions ([relayd] %u vs [client] %u)",
710 reply.major, be32toh(msg.major));
72180669 711 ret = -1;
d3e2ba59
JD
712 goto end;
713 }
714
58eb9381 715 conn->major = reply.major;
d3e2ba59
JD
716 /* We adapt to the lowest compatible version */
717 if (reply.minor <= be32toh(msg.minor)) {
58eb9381 718 conn->minor = reply.minor;
d3e2ba59 719 } else {
58eb9381 720 conn->minor = be32toh(msg.minor);
d3e2ba59
JD
721 }
722
c4e361a4 723 if (be32toh(msg.type) == LTTNG_VIEWER_CLIENT_COMMAND) {
58eb9381 724 conn->type = RELAY_VIEWER_COMMAND;
c4e361a4 725 } else if (be32toh(msg.type) == LTTNG_VIEWER_CLIENT_NOTIFICATION) {
58eb9381 726 conn->type = RELAY_VIEWER_NOTIFICATION;
d3e2ba59
JD
727 } else {
728 ERR("Unknown connection type : %u", be32toh(msg.type));
729 ret = -1;
730 goto end;
731 }
732
733 reply.major = htobe32(reply.major);
734 reply.minor = htobe32(reply.minor);
58eb9381 735 if (conn->type == RELAY_VIEWER_COMMAND) {
b25f7101
MD
736 /*
737 * Increment outside of htobe64 macro, because can be used more than once
738 * within the macro, and thus the operation may be undefined.
739 */
740 last_relay_viewer_session_id++;
741 reply.viewer_session_id = htobe64(last_relay_viewer_session_id);
d3e2ba59 742 }
eea7556c
MD
743
744 health_code_update();
745
58eb9381 746 ret = send_response(conn->sock, &reply, sizeof(reply));
d3e2ba59 747 if (ret < 0) {
2f8f53af 748 goto end;
d3e2ba59
JD
749 }
750
eea7556c
MD
751 health_code_update();
752
58eb9381 753 DBG("Version check done using protocol %u.%u", conn->major, conn->minor);
d3e2ba59
JD
754 ret = 0;
755
756end:
757 return ret;
758}
759
760/*
761 * Send the viewer the list of current sessions.
762 *
763 * Return 0 on success or else a negative value.
764 */
765static
58eb9381 766int viewer_list_sessions(struct relay_connection *conn)
d3e2ba59
JD
767{
768 int ret;
769 struct lttng_viewer_list_sessions session_list;
770 unsigned long count;
771 long approx_before, approx_after;
d3e2ba59
JD
772 struct lttng_ht_iter iter;
773 struct lttng_viewer_session send_session;
774 struct relay_session *session;
775
776 DBG("List sessions received");
777
d3e2ba59 778 rcu_read_lock();
58eb9381
DG
779 cds_lfht_count_nodes(conn->sessions_ht->ht, &approx_before, &count,
780 &approx_after);
d3e2ba59
JD
781 session_list.sessions_count = htobe32(count);
782
eea7556c
MD
783 health_code_update();
784
58eb9381 785 ret = send_response(conn->sock, &session_list, sizeof(session_list));
d3e2ba59 786 if (ret < 0) {
d3e2ba59
JD
787 goto end_unlock;
788 }
789
eea7556c
MD
790 health_code_update();
791
58eb9381 792 cds_lfht_for_each_entry(conn->sessions_ht->ht, &iter.iter, session,
2f8f53af 793 session_n.node) {
eea7556c
MD
794 health_code_update();
795
d3e2ba59
JD
796 strncpy(send_session.session_name, session->session_name,
797 sizeof(send_session.session_name));
798 strncpy(send_session.hostname, session->hostname,
799 sizeof(send_session.hostname));
800 send_session.id = htobe64(session->id);
801 send_session.live_timer = htobe32(session->live_timer);
2a174661 802 send_session.clients = htobe32(session->viewer_refcount);
87b576ec 803 send_session.streams = htobe32(session->stream_count);
d3e2ba59 804
eea7556c
MD
805 health_code_update();
806
58eb9381 807 ret = send_response(conn->sock, &send_session, sizeof(send_session));
d3e2ba59 808 if (ret < 0) {
d3e2ba59
JD
809 goto end_unlock;
810 }
811 }
eea7556c
MD
812 health_code_update();
813
d3e2ba59
JD
814 rcu_read_unlock();
815 ret = 0;
816 goto end;
817
818end_unlock:
819 rcu_read_unlock();
820
821end:
d3e2ba59
JD
822 return ret;
823}
824
6763619c
JD
825/*
826 * Check if a connection is attached to a session.
827 * Return 1 if attached, 0 if not attached, a negative value on error.
828 */
829static
830int session_attached(struct relay_connection *conn, uint64_t session_id)
831{
832 struct relay_session *session;
833 int found = 0;
834
835 if (!conn->viewer_session) {
836 goto end;
837 }
838 cds_list_for_each_entry(session,
839 &conn->viewer_session->sessions_head,
840 viewer_session_list) {
841 if (session->id == session_id) {
842 found = 1;
843 goto end;
844 }
845 }
846
847end:
848 return found;
849}
850
d227d5bd
JD
851/*
852 * Delete all streams for a specific session ID.
853 */
854static void destroy_viewer_streams_by_session(struct relay_session *session)
855{
856 struct relay_viewer_stream *stream;
857 struct lttng_ht_iter iter;
858
859 assert(session);
860
861 rcu_read_lock();
862 cds_lfht_for_each_entry(viewer_streams_ht->ht, &iter.iter, stream,
863 stream_n.node) {
864 struct ctf_trace *ctf_trace;
865
866 health_code_update();
867 if (stream->session_id != session->id) {
868 continue;
869 }
870
871 ctf_trace = ctf_trace_find_by_path(session->ctf_traces_ht,
872 stream->path_name);
873 assert(ctf_trace);
874
875 viewer_stream_delete(stream);
876
877 if (stream->metadata_flag) {
878 ctf_trace->metadata_sent = 0;
879 ctf_trace->viewer_metadata_stream = NULL;
880 }
881
882 viewer_stream_destroy(ctf_trace, stream);
883 }
884 rcu_read_unlock();
885}
886
887static void try_destroy_streams(struct relay_session *session)
888{
889 struct ctf_trace *ctf_trace;
890 struct lttng_ht_iter iter;
891
892 assert(session);
893
894 cds_lfht_for_each_entry(session->ctf_traces_ht->ht, &iter.iter, ctf_trace,
895 node.node) {
896 /* Attempt to destroy the ctf trace of that session. */
897 ctf_trace_try_destroy(session, ctf_trace);
898 }
899}
900
901/*
902 * Cleanup a session.
903 */
904static void cleanup_session(struct relay_connection *conn,
905 struct relay_session *session)
906{
907 /*
908 * Very important that this is done before destroying the session so we
909 * can put back every viewer stream reference from the ctf_trace.
910 */
911 destroy_viewer_streams_by_session(session);
912 try_destroy_streams(session);
913 cds_list_del(&session->viewer_session_list);
914 session_viewer_try_destroy(conn->sessions_ht, session);
915}
916
80e8027a
JD
917/*
918 * Send the viewer the list of current sessions.
919 */
920static
58eb9381 921int viewer_get_new_streams(struct relay_connection *conn)
80e8027a
JD
922{
923 int ret, send_streams = 0;
2f8f53af 924 uint32_t nb_created = 0, nb_unsent = 0, nb_streams = 0;
80e8027a
JD
925 struct lttng_viewer_new_streams_request request;
926 struct lttng_viewer_new_streams_response response;
80e8027a 927 struct relay_session *session;
6763619c 928 uint64_t session_id;
80e8027a 929
58eb9381 930 assert(conn);
80e8027a
JD
931
932 DBG("Get new streams received");
933
80e8027a
JD
934 health_code_update();
935
2f8f53af 936 /* Receive the request from the connected client. */
58eb9381 937 ret = recv_request(conn->sock, &request, sizeof(request));
2f8f53af 938 if (ret < 0) {
80e8027a
JD
939 goto error;
940 }
6763619c 941 session_id = be64toh(request.session_id);
80e8027a
JD
942
943 health_code_update();
944
53efb85a
MD
945 memset(&response, 0, sizeof(response));
946
80e8027a 947 rcu_read_lock();
6763619c 948 session = session_find_by_id(conn->sessions_ht, session_id);
2f8f53af 949 if (!session) {
6763619c 950 DBG("Relay session %" PRIu64 " not found", session_id);
c4e361a4 951 response.status = htobe32(LTTNG_VIEWER_NEW_STREAMS_ERR);
80e8027a
JD
952 goto send_reply;
953 }
954
6763619c 955 if (!session_attached(conn, session_id)) {
80e8027a 956 send_streams = 0;
c4e361a4 957 response.status = htobe32(LTTNG_VIEWER_NEW_STREAMS_ERR);
80e8027a
JD
958 goto send_reply;
959 }
960
6763619c
JD
961 send_streams = 1;
962 response.status = htobe32(LTTNG_VIEWER_NEW_STREAMS_OK);
963
c4e361a4 964 ret = make_viewer_streams(session, LTTNG_VIEWER_SEEK_LAST, NULL, &nb_unsent,
2f8f53af
DG
965 &nb_created);
966 if (ret < 0) {
967 goto end_unlock;
968 }
969 /* Only send back the newly created streams with the unsent ones. */
970 nb_streams = nb_created + nb_unsent;
80e8027a
JD
971 response.streams_count = htobe32(nb_streams);
972
4479f682
JD
973 /*
974 * If the session is closed and we have no new streams to send,
975 * it means that the viewer has already received the whole trace
976 * for this session and should now close it.
977 */
978 if (nb_streams == 0 && session->close_flag) {
979 send_streams = 0;
980 response.status = htobe32(LTTNG_VIEWER_NEW_STREAMS_HUP);
6763619c
JD
981 /*
982 * Remove the session from the attached list of the connection
983 * and try to destroy it.
984 */
985 cds_list_del(&session->viewer_session_list);
d227d5bd 986 cleanup_session(conn, session);
4479f682
JD
987 goto send_reply;
988 }
989
80e8027a
JD
990send_reply:
991 health_code_update();
58eb9381 992 ret = send_response(conn->sock, &response, sizeof(response));
80e8027a 993 if (ret < 0) {
80e8027a
JD
994 goto end_unlock;
995 }
996 health_code_update();
997
998 /*
999 * Unknown or empty session, just return gracefully, the viewer knows what
1000 * is happening.
1001 */
1002 if (!send_streams || !nb_streams) {
1003 ret = 0;
1004 goto end_unlock;
1005 }
1006
2f8f53af
DG
1007 /*
1008 * Send stream and *DON'T* ignore the sent flag so every viewer streams
1009 * that were not sent from that point will be sent to the viewer.
1010 */
58eb9381 1011 ret = send_viewer_streams(conn->sock, session, 0);
2f8f53af
DG
1012 if (ret < 0) {
1013 goto end_unlock;
80e8027a
JD
1014 }
1015
80e8027a
JD
1016end_unlock:
1017 rcu_read_unlock();
80e8027a
JD
1018error:
1019 return ret;
1020}
1021
d3e2ba59
JD
1022/*
1023 * Send the viewer the list of current sessions.
1024 */
1025static
58eb9381 1026int viewer_attach_session(struct relay_connection *conn)
d3e2ba59 1027{
2f8f53af
DG
1028 int send_streams = 0;
1029 ssize_t ret;
80e8027a 1030 uint32_t nb_streams = 0;
2f8f53af 1031 enum lttng_viewer_seek seek_type;
d3e2ba59
JD
1032 struct lttng_viewer_attach_session_request request;
1033 struct lttng_viewer_attach_session_response response;
d3e2ba59
JD
1034 struct relay_session *session;
1035
58eb9381 1036 assert(conn);
d3e2ba59 1037
eea7556c
MD
1038 health_code_update();
1039
2f8f53af 1040 /* Receive the request from the connected client. */
58eb9381 1041 ret = recv_request(conn->sock, &request, sizeof(request));
2f8f53af 1042 if (ret < 0) {
d3e2ba59
JD
1043 goto error;
1044 }
1045
eea7556c
MD
1046 health_code_update();
1047
53efb85a
MD
1048 memset(&response, 0, sizeof(response));
1049
c3b7390b
JD
1050 if (!conn->viewer_session) {
1051 DBG("Client trying to attach before creating a live viewer session");
1052 response.status = htobe32(LTTNG_VIEWER_ATTACH_NO_SESSION);
1053 goto send_reply;
1054 }
1055
d3e2ba59 1056 rcu_read_lock();
58eb9381
DG
1057 session = session_find_by_id(conn->sessions_ht,
1058 be64toh(request.session_id));
2f8f53af 1059 if (!session) {
d3e2ba59
JD
1060 DBG("Relay session %" PRIu64 " not found",
1061 be64toh(request.session_id));
c4e361a4 1062 response.status = htobe32(LTTNG_VIEWER_ATTACH_UNK);
d3e2ba59
JD
1063 goto send_reply;
1064 }
2a174661
DG
1065 session_viewer_attach(session);
1066 DBG("Attach session ID %" PRIu64 " received", be64toh(request.session_id));
d3e2ba59 1067
2a174661 1068 if (uatomic_read(&session->viewer_refcount) > 1) {
d3e2ba59 1069 DBG("Already a viewer attached");
c4e361a4 1070 response.status = htobe32(LTTNG_VIEWER_ATTACH_ALREADY);
2a174661 1071 session_viewer_detach(session);
d3e2ba59
JD
1072 goto send_reply;
1073 } else if (session->live_timer == 0) {
1074 DBG("Not live session");
c4e361a4 1075 response.status = htobe32(LTTNG_VIEWER_ATTACH_NOT_LIVE);
d3e2ba59
JD
1076 goto send_reply;
1077 } else {
d3e2ba59 1078 send_streams = 1;
c4e361a4 1079 response.status = htobe32(LTTNG_VIEWER_ATTACH_OK);
6763619c
JD
1080 cds_list_add(&session->viewer_session_list,
1081 &conn->viewer_session->sessions_head);
d3e2ba59
JD
1082 }
1083
1084 switch (be32toh(request.seek)) {
c4e361a4
JD
1085 case LTTNG_VIEWER_SEEK_BEGINNING:
1086 case LTTNG_VIEWER_SEEK_LAST:
2f8f53af 1087 seek_type = be32toh(request.seek);
d3e2ba59
JD
1088 break;
1089 default:
1090 ERR("Wrong seek parameter");
c4e361a4 1091 response.status = htobe32(LTTNG_VIEWER_ATTACH_SEEK_ERR);
d3e2ba59
JD
1092 send_streams = 0;
1093 goto send_reply;
1094 }
1095
2f8f53af
DG
1096 ret = make_viewer_streams(session, seek_type, &nb_streams, NULL, NULL);
1097 if (ret < 0) {
1098 goto end_unlock;
d3e2ba59 1099 }
2f8f53af 1100 response.streams_count = htobe32(nb_streams);
d3e2ba59
JD
1101
1102send_reply:
eea7556c 1103 health_code_update();
58eb9381 1104 ret = send_response(conn->sock, &response, sizeof(response));
d3e2ba59 1105 if (ret < 0) {
d3e2ba59
JD
1106 goto end_unlock;
1107 }
eea7556c 1108 health_code_update();
d3e2ba59
JD
1109
1110 /*
157df586 1111 * Unknown or empty session, just return gracefully, the viewer knows what
d3e2ba59
JD
1112 * is happening.
1113 */
157df586 1114 if (!send_streams || !nb_streams) {
d3e2ba59
JD
1115 ret = 0;
1116 goto end_unlock;
1117 }
1118
2f8f53af 1119 /* Send stream and ignore the sent flag. */
58eb9381 1120 ret = send_viewer_streams(conn->sock, session, 1);
2f8f53af
DG
1121 if (ret < 0) {
1122 goto end_unlock;
d3e2ba59 1123 }
d3e2ba59
JD
1124
1125end_unlock:
1126 rcu_read_unlock();
4a9daf17
JD
1127error:
1128 return ret;
1129}
1130
878c34cf
DG
1131/*
1132 * Open the index file if needed for the given vstream.
1133 *
1134 * If an index file is successfully opened, the index_read_fd of the stream is
1135 * set with it.
1136 *
1137 * Return 0 on success, a negative value on error (-ENOENT if not ready yet).
1138 */
1139static int try_open_index(struct relay_viewer_stream *vstream,
1140 struct relay_stream *rstream)
1141{
1142 int ret = 0;
1143
1144 assert(vstream);
1145 assert(rstream);
1146
1147 if (vstream->index_read_fd >= 0) {
1148 goto end;
1149 }
1150
1151 /*
1152 * First time, we open the index file and at least one index is ready. The
1153 * race between the read and write of the total_index_received is
1154 * acceptable here since the client will be notified to simply come back
1155 * and get the next index.
1156 */
1157 if (rstream->total_index_received <= 0) {
1158 ret = -ENOENT;
1159 goto end;
1160 }
1161 ret = index_open(vstream->path_name, vstream->channel_name,
1162 vstream->tracefile_count, vstream->tracefile_count_current);
1163 if (ret >= 0) {
1164 vstream->index_read_fd = ret;
1165 ret = 0;
1166 goto end;
1167 }
1168
1169end:
1170 return ret;
1171}
1172
1173/*
1174 * Check the status of the index for the given stream. This function updates
1175 * the index structure if needed and can destroy the vstream also for the HUP
1176 * situation.
1177 *
1178 * Return 0 means that we can proceed with the index. A value of 1 means that
1179 * the index has been updated and is ready to be send to the client. A negative
1180 * value indicates an error that can't be handled.
1181 */
1182static int check_index_status(struct relay_viewer_stream *vstream,
1183 struct relay_stream *rstream, struct ctf_trace *trace,
1184 struct lttng_viewer_index *index)
1185{
1186 int ret;
1187
1188 assert(vstream);
1189 assert(rstream);
1190 assert(index);
1191 assert(trace);
1192
1193 if (!rstream->close_flag) {
1194 /* Rotate on abort (overwrite). */
1195 if (vstream->abort_flag) {
1196 DBG("Viewer stream %" PRIu64 " rotate because of overwrite",
1197 vstream->stream_handle);
1198 ret = viewer_stream_rotate(vstream, rstream);
1199 if (ret < 0) {
1200 goto error;
1201 } else if (ret == 1) {
1202 /* EOF */
1203 index->status = htobe32(LTTNG_VIEWER_INDEX_HUP);
1204 goto hup;
1205 }
1206 /* ret == 0 means successful so we continue. */
1207 }
1208
1209 /* Check if we are in the same trace file at this point. */
1210 if (rstream->tracefile_count_current == vstream->tracefile_count_current) {
1211 if (rstream->beacon_ts_end != -1ULL &&
1212 vstream->last_sent_index == rstream->total_index_received) {
1213 /*
1214 * We've received a synchronization beacon and the last index
1215 * available has been sent, the index for now is inactive.
1216 */
1217 index->status = htobe32(LTTNG_VIEWER_INDEX_INACTIVE);
1218 index->timestamp_end = htobe64(rstream->beacon_ts_end);
bb8cf449 1219 index->stream_id = htobe64(rstream->ctf_stream_id);
878c34cf 1220 goto index_ready;
ca3aecdf 1221 } else if (rstream->total_index_received <= vstream->last_sent_index
878c34cf
DG
1222 && !vstream->close_write_flag) {
1223 /*
1224 * Reader and writer are working in the same tracefile, so we care
1225 * about the number of index received and sent. Otherwise, we read
1226 * up to EOF.
1227 */
1228 index->status = htobe32(LTTNG_VIEWER_INDEX_RETRY);
1229 goto index_ready;
1230 }
1231 }
1232 /* Nothing to do with the index, continue with it. */
1233 ret = 0;
1234 } else if (rstream->close_flag && vstream->close_write_flag &&
1235 vstream->total_index_received == vstream->last_sent_index) {
1236 /* Last index sent and current tracefile closed in write */
1237 index->status = htobe32(LTTNG_VIEWER_INDEX_HUP);
1238 goto hup;
1239 } else {
1240 vstream->close_write_flag = 1;
1241 ret = 0;
1242 }
1243
1244error:
1245 return ret;
1246
1247hup:
1248 viewer_stream_delete(vstream);
1249 viewer_stream_destroy(trace, vstream);
1250index_ready:
1251 return 1;
1252}
1253
d3e2ba59
JD
1254/*
1255 * Send the next index for a stream.
1256 *
1257 * Return 0 on success or else a negative value.
1258 */
1259static
58eb9381 1260int viewer_get_next_index(struct relay_connection *conn)
d3e2ba59
JD
1261{
1262 int ret;
878c34cf 1263 ssize_t read_ret;
d3e2ba59
JD
1264 struct lttng_viewer_get_next_index request_index;
1265 struct lttng_viewer_index viewer_index;
50adc264 1266 struct ctf_packet_index packet_index;
d3e2ba59
JD
1267 struct relay_viewer_stream *vstream;
1268 struct relay_stream *rstream;
2a174661
DG
1269 struct ctf_trace *ctf_trace;
1270 struct relay_session *session;
d3e2ba59 1271
58eb9381 1272 assert(conn);
d3e2ba59
JD
1273
1274 DBG("Viewer get next index");
1275
eea7556c 1276 health_code_update();
2f8f53af 1277
58eb9381 1278 ret = recv_request(conn->sock, &request_index, sizeof(request_index));
2f8f53af 1279 if (ret < 0) {
d3e2ba59
JD
1280 goto end;
1281 }
eea7556c 1282 health_code_update();
d3e2ba59
JD
1283
1284 rcu_read_lock();
6763619c
JD
1285 vstream = viewer_stream_find_by_id(be64toh(request_index.stream_id));
1286 if (!vstream) {
2a174661
DG
1287 ret = -1;
1288 goto end_unlock;
1289 }
1290
6763619c
JD
1291 session = session_find_by_id(conn->sessions_ht, vstream->session_id);
1292 if (!session) {
d3e2ba59
JD
1293 ret = -1;
1294 goto end_unlock;
1295 }
1296
2a174661
DG
1297 ctf_trace = ctf_trace_find_by_path(session->ctf_traces_ht, vstream->path_name);
1298 assert(ctf_trace);
1299
d3e2ba59
JD
1300 memset(&viewer_index, 0, sizeof(viewer_index));
1301
1302 /*
1303 * The viewer should not ask for index on metadata stream.
1304 */
1305 if (vstream->metadata_flag) {
c4e361a4 1306 viewer_index.status = htobe32(LTTNG_VIEWER_INDEX_HUP);
d3e2ba59
JD
1307 goto send_reply;
1308 }
1309
878c34cf
DG
1310 rstream = stream_find_by_id(relay_streams_ht, vstream->stream_handle);
1311 assert(rstream);
1312
1313 /* Try to open an index if one is needed for that stream. */
1314 ret = try_open_index(vstream, rstream);
1315 if (ret < 0) {
0e6830aa 1316 if (ret == -ENOENT) {
d3e2ba59
JD
1317 /*
1318 * The index is created only when the first data packet arrives, it
1319 * might not be ready at the beginning of the session
1320 */
c4e361a4 1321 viewer_index.status = htobe32(LTTNG_VIEWER_INDEX_RETRY);
878c34cf
DG
1322 } else {
1323 /* Unhandled error. */
c4e361a4 1324 viewer_index.status = htobe32(LTTNG_VIEWER_INDEX_ERR);
d3e2ba59 1325 }
878c34cf 1326 goto send_reply;
d3e2ba59
JD
1327 }
1328
10b0cd2e 1329 pthread_mutex_lock(&rstream->viewer_stream_rotation_lock);
878c34cf
DG
1330 ret = check_index_status(vstream, rstream, ctf_trace, &viewer_index);
1331 pthread_mutex_unlock(&rstream->viewer_stream_rotation_lock);
1332 if (ret < 0) {
6746f4f2 1333 goto end_unlock;
878c34cf
DG
1334 } else if (ret == 1) {
1335 /*
1336 * This means the viewer index data structure has been populated by the
1337 * check call thus we now send back the reply to the client.
1338 */
d3e2ba59
JD
1339 goto send_reply;
1340 }
878c34cf
DG
1341 /* At this point, ret MUST be 0 thus we continue with the get. */
1342 assert(!ret);
d3e2ba59 1343
2a174661
DG
1344 if (!ctf_trace->metadata_received ||
1345 ctf_trace->metadata_received > ctf_trace->metadata_sent) {
d3e2ba59
JD
1346 viewer_index.flags |= LTTNG_VIEWER_FLAG_NEW_METADATA;
1347 }
1348
f04a971b 1349 ret = check_new_streams(conn);
4a9daf17
JD
1350 if (ret < 0) {
1351 goto end_unlock;
1352 } else if (ret == 1) {
1353 viewer_index.flags |= LTTNG_VIEWER_FLAG_NEW_STREAM;
1354 }
1355
56611b06 1356 pthread_mutex_lock(&rstream->viewer_stream_rotation_lock);
cef0f7d5
JD
1357 pthread_mutex_lock(&vstream->overwrite_lock);
1358 if (vstream->abort_flag) {
878c34cf 1359 /* The file is being overwritten by the writer, we cannot use it. */
cef0f7d5 1360 pthread_mutex_unlock(&vstream->overwrite_lock);
2f8f53af 1361 ret = viewer_stream_rotate(vstream, rstream);
56611b06 1362 pthread_mutex_unlock(&rstream->viewer_stream_rotation_lock);
cef0f7d5
JD
1363 if (ret < 0) {
1364 goto end_unlock;
a020f610 1365 } else if (ret == 1) {
c4e361a4 1366 viewer_index.status = htobe32(LTTNG_VIEWER_INDEX_HUP);
2f8f53af 1367 viewer_stream_delete(vstream);
2a174661 1368 viewer_stream_destroy(ctf_trace, vstream);
878c34cf
DG
1369 } else {
1370 viewer_index.status = htobe32(LTTNG_VIEWER_INDEX_RETRY);
cef0f7d5
JD
1371 }
1372 goto send_reply;
1373 }
2f8f53af 1374
878c34cf 1375 read_ret = lttng_read(vstream->index_read_fd, &packet_index,
6cd525e8 1376 sizeof(packet_index));
cef0f7d5 1377 pthread_mutex_unlock(&vstream->overwrite_lock);
56611b06 1378 pthread_mutex_unlock(&rstream->viewer_stream_rotation_lock);
878c34cf
DG
1379 if (read_ret < 0) {
1380 viewer_index.status = htobe32(LTTNG_VIEWER_INDEX_HUP);
1381 viewer_stream_delete(vstream);
1382 viewer_stream_destroy(ctf_trace, vstream);
1383 goto send_reply;
1384 } else if (read_ret < sizeof(packet_index)) {
10b0cd2e 1385 pthread_mutex_lock(&rstream->viewer_stream_rotation_lock);
878c34cf 1386 if (vstream->close_write_flag) {
2f8f53af 1387 ret = viewer_stream_rotate(vstream, rstream);
6b6b9a5a 1388 if (ret < 0) {
878c34cf 1389 pthread_mutex_unlock(&rstream->viewer_stream_rotation_lock);
6b6b9a5a 1390 goto end_unlock;
a020f610 1391 } else if (ret == 1) {
c4e361a4 1392 viewer_index.status = htobe32(LTTNG_VIEWER_INDEX_HUP);
2f8f53af 1393 viewer_stream_delete(vstream);
2a174661 1394 viewer_stream_destroy(ctf_trace, vstream);
878c34cf
DG
1395 } else {
1396 viewer_index.status = htobe32(LTTNG_VIEWER_INDEX_RETRY);
6b6b9a5a
JD
1397 }
1398 } else {
878c34cf 1399 ERR("Relay reading index file %d", vstream->index_read_fd);
c4e361a4 1400 viewer_index.status = htobe32(LTTNG_VIEWER_INDEX_ERR);
6b6b9a5a 1401 }
878c34cf 1402 pthread_mutex_unlock(&rstream->viewer_stream_rotation_lock);
6b6b9a5a 1403 goto send_reply;
d3e2ba59 1404 } else {
c4e361a4 1405 viewer_index.status = htobe32(LTTNG_VIEWER_INDEX_OK);
d3e2ba59
JD
1406 vstream->last_sent_index++;
1407 }
1408
1409 /*
1410 * Indexes are stored in big endian, no need to switch before sending.
1411 */
1412 viewer_index.offset = packet_index.offset;
1413 viewer_index.packet_size = packet_index.packet_size;
1414 viewer_index.content_size = packet_index.content_size;
1415 viewer_index.timestamp_begin = packet_index.timestamp_begin;
1416 viewer_index.timestamp_end = packet_index.timestamp_end;
1417 viewer_index.events_discarded = packet_index.events_discarded;
1418 viewer_index.stream_id = packet_index.stream_id;
1419
1420send_reply:
1421 viewer_index.flags = htobe32(viewer_index.flags);
eea7556c 1422 health_code_update();
2f8f53af 1423
58eb9381 1424 ret = send_response(conn->sock, &viewer_index, sizeof(viewer_index));
d3e2ba59 1425 if (ret < 0) {
d3e2ba59
JD
1426 goto end_unlock;
1427 }
eea7556c 1428 health_code_update();
d3e2ba59 1429
2f8f53af 1430 DBG("Index %" PRIu64 " for stream %" PRIu64 " sent",
d3e2ba59
JD
1431 vstream->last_sent_index, vstream->stream_handle);
1432
1433end_unlock:
1434 rcu_read_unlock();
1435
d3e2ba59
JD
1436end:
1437 return ret;
1438}
1439
1440/*
1441 * Send the next index for a stream
1442 *
1443 * Return 0 on success or else a negative value.
1444 */
1445static
58eb9381 1446int viewer_get_packet(struct relay_connection *conn)
d3e2ba59
JD
1447{
1448 int ret, send_data = 0;
1449 char *data = NULL;
1450 uint32_t len = 0;
1451 ssize_t read_len;
1452 struct lttng_viewer_get_packet get_packet_info;
1453 struct lttng_viewer_trace_packet reply;
1454 struct relay_viewer_stream *stream;
6763619c 1455 struct relay_session *session;
2a174661 1456 struct ctf_trace *ctf_trace;
d3e2ba59 1457
58eb9381 1458 assert(conn);
d3e2ba59
JD
1459
1460 DBG2("Relay get data packet");
1461
eea7556c 1462 health_code_update();
2f8f53af 1463
58eb9381 1464 ret = recv_request(conn->sock, &get_packet_info, sizeof(get_packet_info));
2f8f53af 1465 if (ret < 0) {
d3e2ba59
JD
1466 goto end;
1467 }
eea7556c 1468 health_code_update();
d3e2ba59 1469
0233a6a5
DG
1470 /* From this point on, the error label can be reached. */
1471 memset(&reply, 0, sizeof(reply));
1472
d3e2ba59 1473 rcu_read_lock();
2f8f53af 1474 stream = viewer_stream_find_by_id(be64toh(get_packet_info.stream_id));
d3e2ba59
JD
1475 if (!stream) {
1476 goto error;
1477 }
2a174661 1478
6763619c
JD
1479 session = session_find_by_id(conn->sessions_ht, stream->session_id);
1480 if (!session) {
1481 ret = -1;
1482 goto error;
1483 }
1484
1485 ctf_trace = ctf_trace_find_by_path(session->ctf_traces_ht,
2a174661
DG
1486 stream->path_name);
1487 assert(ctf_trace);
d3e2ba59
JD
1488
1489 /*
1490 * First time we read this stream, we need open the tracefile, we should
1491 * only arrive here if an index has already been sent to the viewer, so the
1492 * tracefile must exist, if it does not it is a fatal error.
1493 */
1494 if (stream->read_fd < 0) {
1495 char fullpath[PATH_MAX];
1496
6b6b9a5a
JD
1497 if (stream->tracefile_count > 0) {
1498 ret = snprintf(fullpath, PATH_MAX, "%s/%s_%" PRIu64, stream->path_name,
1499 stream->channel_name,
1500 stream->tracefile_count_current);
1501 } else {
1502 ret = snprintf(fullpath, PATH_MAX, "%s/%s", stream->path_name,
1503 stream->channel_name);
1504 }
d3e2ba59
JD
1505 if (ret < 0) {
1506 goto error;
1507 }
1508 ret = open(fullpath, O_RDONLY);
1509 if (ret < 0) {
1510 PERROR("Relay opening trace file");
1511 goto error;
1512 }
1513 stream->read_fd = ret;
1514 }
1515
2a174661
DG
1516 if (!ctf_trace->metadata_received ||
1517 ctf_trace->metadata_received > ctf_trace->metadata_sent) {
c4e361a4 1518 reply.status = htobe32(LTTNG_VIEWER_GET_PACKET_ERR);
d3e2ba59 1519 reply.flags |= LTTNG_VIEWER_FLAG_NEW_METADATA;
d3e2ba59
JD
1520 goto send_reply;
1521 }
1522
f04a971b 1523 ret = check_new_streams(conn);
4a9daf17
JD
1524 if (ret < 0) {
1525 goto end_unlock;
1526 } else if (ret == 1) {
c4e361a4 1527 reply.status = htobe32(LTTNG_VIEWER_GET_PACKET_ERR);
4a9daf17
JD
1528 reply.flags |= LTTNG_VIEWER_FLAG_NEW_STREAM;
1529 goto send_reply;
1530 }
1531
d3e2ba59
JD
1532 len = be32toh(get_packet_info.len);
1533 data = zmalloc(len);
1534 if (!data) {
1535 PERROR("relay data zmalloc");
1536 goto error;
1537 }
1538
1539 ret = lseek(stream->read_fd, be64toh(get_packet_info.offset), SEEK_SET);
1540 if (ret < 0) {
6b6b9a5a
JD
1541 /*
1542 * If the read fd was closed by the streaming side, the
1543 * abort_flag will be set to 1, otherwise it is an error.
1544 */
1545 if (stream->abort_flag == 0) {
1546 PERROR("lseek");
1547 goto error;
1548 }
c4e361a4 1549 reply.status = htobe32(LTTNG_VIEWER_GET_PACKET_EOF);
6b6b9a5a 1550 goto send_reply;
d3e2ba59 1551 }
6cd525e8
MD
1552 read_len = lttng_read(stream->read_fd, data, len);
1553 if (read_len < len) {
6b6b9a5a
JD
1554 /*
1555 * If the read fd was closed by the streaming side, the
1556 * abort_flag will be set to 1, otherwise it is an error.
1557 */
1558 if (stream->abort_flag == 0) {
1559 PERROR("Relay reading trace file, fd: %d, offset: %" PRIu64,
1560 stream->read_fd,
1561 be64toh(get_packet_info.offset));
1562 goto error;
1563 } else {
c4e361a4 1564 reply.status = htobe32(LTTNG_VIEWER_GET_PACKET_EOF);
6b6b9a5a
JD
1565 goto send_reply;
1566 }
d3e2ba59 1567 }
c4e361a4 1568 reply.status = htobe32(LTTNG_VIEWER_GET_PACKET_OK);
d3e2ba59
JD
1569 reply.len = htobe32(len);
1570 send_data = 1;
1571 goto send_reply;
1572
1573error:
c4e361a4 1574 reply.status = htobe32(LTTNG_VIEWER_GET_PACKET_ERR);
d3e2ba59
JD
1575
1576send_reply:
1577 reply.flags = htobe32(reply.flags);
eea7556c
MD
1578
1579 health_code_update();
2f8f53af 1580
58eb9381 1581 ret = send_response(conn->sock, &reply, sizeof(reply));
d3e2ba59 1582 if (ret < 0) {
d3e2ba59
JD
1583 goto end_unlock;
1584 }
eea7556c 1585 health_code_update();
d3e2ba59
JD
1586
1587 if (send_data) {
eea7556c 1588 health_code_update();
58eb9381 1589 ret = send_response(conn->sock, data, len);
d3e2ba59 1590 if (ret < 0) {
d3e2ba59
JD
1591 goto end_unlock;
1592 }
eea7556c 1593 health_code_update();
d3e2ba59
JD
1594 }
1595
1596 DBG("Sent %u bytes for stream %" PRIu64, len,
1597 be64toh(get_packet_info.stream_id));
1598
1599end_unlock:
1600 free(data);
1601 rcu_read_unlock();
1602
1603end:
1604 return ret;
1605}
1606
1607/*
1608 * Send the session's metadata
1609 *
1610 * Return 0 on success else a negative value.
1611 */
1612static
58eb9381 1613int viewer_get_metadata(struct relay_connection *conn)
d3e2ba59
JD
1614{
1615 int ret = 0;
1616 ssize_t read_len;
1617 uint64_t len = 0;
1618 char *data = NULL;
1619 struct lttng_viewer_get_metadata request;
1620 struct lttng_viewer_metadata_packet reply;
1621 struct relay_viewer_stream *stream;
2a174661 1622 struct ctf_trace *ctf_trace;
6763619c 1623 struct relay_session *session;
d3e2ba59 1624
58eb9381 1625 assert(conn);
d3e2ba59
JD
1626
1627 DBG("Relay get metadata");
1628
eea7556c 1629 health_code_update();
2f8f53af 1630
58eb9381 1631 ret = recv_request(conn->sock, &request, sizeof(request));
2f8f53af 1632 if (ret < 0) {
d3e2ba59
JD
1633 goto end;
1634 }
eea7556c 1635 health_code_update();
d3e2ba59 1636
53efb85a
MD
1637 memset(&reply, 0, sizeof(reply));
1638
d3e2ba59 1639 rcu_read_lock();
2f8f53af 1640 stream = viewer_stream_find_by_id(be64toh(request.stream_id));
d3e2ba59
JD
1641 if (!stream || !stream->metadata_flag) {
1642 ERR("Invalid metadata stream");
1643 goto error;
1644 }
d3e2ba59 1645
6763619c
JD
1646 session = session_find_by_id(conn->sessions_ht, stream->session_id);
1647 if (!session) {
1648 ret = -1;
1649 goto error;
1650 }
1651
1652 ctf_trace = ctf_trace_find_by_path(session->ctf_traces_ht,
2a174661
DG
1653 stream->path_name);
1654 assert(ctf_trace);
1655 assert(ctf_trace->metadata_sent <= ctf_trace->metadata_received);
1656
1657 len = ctf_trace->metadata_received - ctf_trace->metadata_sent;
d3e2ba59 1658 if (len == 0) {
c4e361a4 1659 reply.status = htobe32(LTTNG_VIEWER_NO_NEW_METADATA);
d3e2ba59
JD
1660 goto send_reply;
1661 }
1662
1663 /* first time, we open the metadata file */
1664 if (stream->read_fd < 0) {
1665 char fullpath[PATH_MAX];
1666
1667 ret = snprintf(fullpath, PATH_MAX, "%s/%s", stream->path_name,
1668 stream->channel_name);
1669 if (ret < 0) {
1670 goto error;
1671 }
1672 ret = open(fullpath, O_RDONLY);
1673 if (ret < 0) {
1674 PERROR("Relay opening metadata file");
1675 goto error;
1676 }
1677 stream->read_fd = ret;
1678 }
1679
1680 reply.len = htobe64(len);
1681 data = zmalloc(len);
1682 if (!data) {
1683 PERROR("viewer metadata zmalloc");
1684 goto error;
1685 }
1686
6cd525e8
MD
1687 read_len = lttng_read(stream->read_fd, data, len);
1688 if (read_len < len) {
d3e2ba59
JD
1689 PERROR("Relay reading metadata file");
1690 goto error;
1691 }
2a174661 1692 ctf_trace->metadata_sent += read_len;
c4e361a4 1693 reply.status = htobe32(LTTNG_VIEWER_METADATA_OK);
d3e2ba59
JD
1694 goto send_reply;
1695
1696error:
c4e361a4 1697 reply.status = htobe32(LTTNG_VIEWER_METADATA_ERR);
d3e2ba59
JD
1698
1699send_reply:
eea7556c 1700 health_code_update();
58eb9381 1701 ret = send_response(conn->sock, &reply, sizeof(reply));
d3e2ba59 1702 if (ret < 0) {
d3e2ba59
JD
1703 goto end_unlock;
1704 }
eea7556c 1705 health_code_update();
d3e2ba59
JD
1706
1707 if (len > 0) {
58eb9381 1708 ret = send_response(conn->sock, data, len);
d3e2ba59 1709 if (ret < 0) {
d3e2ba59
JD
1710 goto end_unlock;
1711 }
1712 }
1713
1714 DBG("Sent %" PRIu64 " bytes of metadata for stream %" PRIu64, len,
1715 be64toh(request.stream_id));
1716
1717 DBG("Metadata sent");
1718
1719end_unlock:
1720 free(data);
1721 rcu_read_unlock();
1722end:
1723 return ret;
1724}
1725
c3b7390b
JD
1726/*
1727 * Create a viewer session.
1728 *
1729 * Return 0 on success or else a negative value.
1730 */
1731static
1732int viewer_create_session(struct relay_connection *conn)
1733{
1734 int ret;
1735 struct lttng_viewer_create_session_response resp;
1736
1737 DBG("Viewer create session received");
1738
53efb85a 1739 memset(&resp, 0, sizeof(resp));
c3b7390b 1740 resp.status = htobe32(LTTNG_VIEWER_CREATE_SESSION_OK);
32816a93 1741 conn->viewer_session = zmalloc(sizeof(*conn->viewer_session));
c3b7390b
JD
1742 if (!conn->viewer_session) {
1743 ERR("Allocation viewer session");
1744 resp.status = htobe32(LTTNG_VIEWER_CREATE_SESSION_ERR);
1745 goto send_reply;
1746 }
1747 CDS_INIT_LIST_HEAD(&conn->viewer_session->sessions_head);
1748
1749send_reply:
1750 health_code_update();
1751 ret = send_response(conn->sock, &resp, sizeof(resp));
1752 if (ret < 0) {
1753 goto end;
1754 }
1755 health_code_update();
1756 ret = 0;
1757
1758end:
1759 return ret;
1760}
1761
1762
d3e2ba59
JD
1763/*
1764 * live_relay_unknown_command: send -1 if received unknown command
1765 */
1766static
58eb9381 1767void live_relay_unknown_command(struct relay_connection *conn)
d3e2ba59
JD
1768{
1769 struct lttcomm_relayd_generic_reply reply;
d3e2ba59 1770
53efb85a 1771 memset(&reply, 0, sizeof(reply));
d3e2ba59 1772 reply.ret_code = htobe32(LTTNG_ERR_UNK);
58eb9381 1773 (void) send_response(conn->sock, &reply, sizeof(reply));
d3e2ba59
JD
1774}
1775
1776/*
1777 * Process the commands received on the control socket
1778 */
1779static
1780int process_control(struct lttng_viewer_cmd *recv_hdr,
58eb9381 1781 struct relay_connection *conn)
d3e2ba59
JD
1782{
1783 int ret = 0;
2f8f53af
DG
1784 uint32_t msg_value;
1785
1786 assert(recv_hdr);
58eb9381 1787 assert(conn);
2f8f53af
DG
1788
1789 msg_value = be32toh(recv_hdr->cmd);
1790
1791 /*
1792 * Make sure we've done the version check before any command other then a
1793 * new client connection.
1794 */
c4e361a4 1795 if (msg_value != LTTNG_VIEWER_CONNECT && !conn->version_check_done) {
58eb9381 1796 ERR("Viewer conn value %" PRIu32 " before version check", msg_value);
2f8f53af
DG
1797 ret = -1;
1798 goto end;
1799 }
d3e2ba59 1800
2f8f53af 1801 switch (msg_value) {
c4e361a4 1802 case LTTNG_VIEWER_CONNECT:
58eb9381 1803 ret = viewer_connect(conn);
d3e2ba59 1804 break;
c4e361a4 1805 case LTTNG_VIEWER_LIST_SESSIONS:
58eb9381 1806 ret = viewer_list_sessions(conn);
d3e2ba59 1807 break;
c4e361a4 1808 case LTTNG_VIEWER_ATTACH_SESSION:
58eb9381 1809 ret = viewer_attach_session(conn);
d3e2ba59 1810 break;
c4e361a4 1811 case LTTNG_VIEWER_GET_NEXT_INDEX:
58eb9381 1812 ret = viewer_get_next_index(conn);
d3e2ba59 1813 break;
c4e361a4 1814 case LTTNG_VIEWER_GET_PACKET:
58eb9381 1815 ret = viewer_get_packet(conn);
d3e2ba59 1816 break;
c4e361a4 1817 case LTTNG_VIEWER_GET_METADATA:
58eb9381 1818 ret = viewer_get_metadata(conn);
d3e2ba59 1819 break;
c4e361a4 1820 case LTTNG_VIEWER_GET_NEW_STREAMS:
58eb9381 1821 ret = viewer_get_new_streams(conn);
80e8027a 1822 break;
c3b7390b
JD
1823 case LTTNG_VIEWER_CREATE_SESSION:
1824 ret = viewer_create_session(conn);
1825 break;
d3e2ba59
JD
1826 default:
1827 ERR("Received unknown viewer command (%u)", be32toh(recv_hdr->cmd));
58eb9381 1828 live_relay_unknown_command(conn);
d3e2ba59
JD
1829 ret = -1;
1830 goto end;
1831 }
1832
1833end:
1834 return ret;
1835}
1836
1837static
58eb9381 1838void cleanup_connection_pollfd(struct lttng_poll_event *events, int pollfd)
d3e2ba59
JD
1839{
1840 int ret;
1841
1842 assert(events);
1843
58eb9381 1844 (void) lttng_poll_del(events, pollfd);
d3e2ba59
JD
1845
1846 ret = close(pollfd);
1847 if (ret < 0) {
1848 ERR("Closing pollfd %d", pollfd);
1849 }
1850}
1851
d3e2ba59 1852/*
58eb9381 1853 * Delete and destroy a connection.
d3e2ba59
JD
1854 *
1855 * RCU read side lock MUST be acquired.
1856 */
58eb9381
DG
1857static void destroy_connection(struct lttng_ht *relay_connections_ht,
1858 struct relay_connection *conn)
d3e2ba59 1859{
6763619c 1860 struct relay_session *session, *tmp_session;
d3e2ba59
JD
1861
1862 assert(relay_connections_ht);
58eb9381 1863 assert(conn);
d3e2ba59 1864
58eb9381 1865 connection_delete(relay_connections_ht, conn);
d3e2ba59 1866
6763619c
JD
1867 if (!conn->viewer_session) {
1868 goto end;
1869 }
1870
58eb9381 1871 rcu_read_lock();
6763619c
JD
1872 cds_list_for_each_entry_safe(session, tmp_session,
1873 &conn->viewer_session->sessions_head,
1874 viewer_session_list) {
1875 DBG("Cleaning connection of session ID %" PRIu64, session->id);
d227d5bd 1876 cleanup_session(conn, session);
2a174661
DG
1877 }
1878 rcu_read_unlock();
d3e2ba59 1879
6763619c 1880end:
58eb9381 1881 connection_destroy(conn);
d3e2ba59
JD
1882}
1883
1884/*
1885 * This thread does the actual work
1886 */
1887static
1888void *thread_worker(void *data)
1889{
1890 int ret, err = -1;
1891 uint32_t nb_fd;
58eb9381 1892 struct relay_connection *conn;
d3e2ba59
JD
1893 struct lttng_poll_event events;
1894 struct lttng_ht *relay_connections_ht;
d3e2ba59
JD
1895 struct lttng_ht_iter iter;
1896 struct lttng_viewer_cmd recv_hdr;
1897 struct relay_local_data *relay_ctx = (struct relay_local_data *) data;
1898 struct lttng_ht *sessions_ht = relay_ctx->sessions_ht;
d3e2ba59
JD
1899
1900 DBG("[thread] Live viewer relay worker started");
1901
1902 rcu_register_thread();
1903
eea7556c
MD
1904 health_register(health_relayd, HEALTH_RELAYD_TYPE_LIVE_WORKER);
1905
9b5e0863
MD
1906 if (testpoint(relayd_thread_live_worker)) {
1907 goto error_testpoint;
1908 }
1909
d3e2ba59
JD
1910 /* table of connections indexed on socket */
1911 relay_connections_ht = lttng_ht_new(0, LTTNG_HT_TYPE_ULONG);
1912 if (!relay_connections_ht) {
1913 goto relay_connections_ht_error;
1914 }
1915
1916 ret = create_thread_poll_set(&events, 2);
1917 if (ret < 0) {
1918 goto error_poll_create;
1919 }
1920
58eb9381 1921 ret = lttng_poll_add(&events, live_conn_pipe[0], LPOLLIN | LPOLLRDHUP);
d3e2ba59
JD
1922 if (ret < 0) {
1923 goto error;
1924 }
1925
1926restart:
1927 while (1) {
1928 int i;
1929
eea7556c
MD
1930 health_code_update();
1931
d3e2ba59
JD
1932 /* Infinite blocking call, waiting for transmission */
1933 DBG3("Relayd live viewer worker thread polling...");
eea7556c 1934 health_poll_entry();
d3e2ba59 1935 ret = lttng_poll_wait(&events, -1);
eea7556c 1936 health_poll_exit();
d3e2ba59
JD
1937 if (ret < 0) {
1938 /*
1939 * Restart interrupted system call.
1940 */
1941 if (errno == EINTR) {
1942 goto restart;
1943 }
1944 goto error;
1945 }
1946
1947 nb_fd = ret;
1948
1949 /*
1950 * Process control. The control connection is prioritised so we don't
1951 * starve it with high throughput tracing data on the data
1952 * connection.
1953 */
1954 for (i = 0; i < nb_fd; i++) {
1955 /* Fetch once the poll data */
1956 uint32_t revents = LTTNG_POLL_GETEV(&events, i);
1957 int pollfd = LTTNG_POLL_GETFD(&events, i);
1958
eea7556c
MD
1959 health_code_update();
1960
6550503d
MD
1961 if (!revents) {
1962 /* No activity for this FD (poll implementation). */
1963 continue;
1964 }
1965
d3e2ba59 1966 /* Thread quit pipe has been closed. Killing thread. */
bcf4a440 1967 ret = check_thread_quit_pipe(pollfd, revents);
d3e2ba59
JD
1968 if (ret) {
1969 err = 0;
1970 goto exit;
1971 }
1972
58eb9381
DG
1973 /* Inspect the relay conn pipe for new connection */
1974 if (pollfd == live_conn_pipe[0]) {
d3e2ba59
JD
1975 if (revents & (LPOLLERR | LPOLLHUP | LPOLLRDHUP)) {
1976 ERR("Relay live pipe error");
1977 goto error;
1978 } else if (revents & LPOLLIN) {
58eb9381 1979 ret = lttng_read(live_conn_pipe[0], &conn, sizeof(conn));
d3e2ba59
JD
1980 if (ret < 0) {
1981 goto error;
1982 }
58eb9381
DG
1983 conn->sessions_ht = sessions_ht;
1984 connection_init(conn);
1985 lttng_poll_add(&events, conn->sock->fd,
1986 LPOLLIN | LPOLLRDHUP);
1987 rcu_read_lock();
1988 lttng_ht_add_unique_ulong(relay_connections_ht,
1989 &conn->sock_n);
d3e2ba59 1990 rcu_read_unlock();
58eb9381 1991 DBG("Connection socket %d added", conn->sock->fd);
d3e2ba59 1992 }
58eb9381
DG
1993 } else {
1994 rcu_read_lock();
1995 conn = connection_find_by_sock(relay_connections_ht, pollfd);
1996 /* If not found, there is a synchronization issue. */
1997 assert(conn);
1998
1999 if (revents & (LPOLLERR | LPOLLHUP | LPOLLRDHUP)) {
2000 cleanup_connection_pollfd(&events, pollfd);
2001 destroy_connection(relay_connections_ht, conn);
d3e2ba59 2002 } else if (revents & LPOLLIN) {
58eb9381
DG
2003 ret = conn->sock->ops->recvmsg(conn->sock, &recv_hdr,
2004 sizeof(recv_hdr), 0);
d3e2ba59 2005 if (ret <= 0) {
58eb9381
DG
2006 /* Connection closed */
2007 cleanup_connection_pollfd(&events, pollfd);
2008 destroy_connection(relay_connections_ht, conn);
2009 DBG("Viewer control conn closed with %d", pollfd);
d3e2ba59 2010 } else {
58eb9381 2011 ret = process_control(&recv_hdr, conn);
d3e2ba59
JD
2012 if (ret < 0) {
2013 /* Clear the session on error. */
58eb9381
DG
2014 cleanup_connection_pollfd(&events, pollfd);
2015 destroy_connection(relay_connections_ht, conn);
d3e2ba59
JD
2016 DBG("Viewer connection closed with %d", pollfd);
2017 }
2018 }
2019 }
2020 rcu_read_unlock();
2021 }
2022 }
2023 }
2024
2025exit:
2026error:
2027 lttng_poll_clean(&events);
2028
58eb9381 2029 /* Cleanup reamaining connection object. */
d3e2ba59 2030 rcu_read_lock();
58eb9381
DG
2031 cds_lfht_for_each_entry(relay_connections_ht->ht, &iter.iter, conn,
2032 sock_n.node) {
eea7556c 2033 health_code_update();
58eb9381 2034 destroy_connection(relay_connections_ht, conn);
d3e2ba59
JD
2035 }
2036 rcu_read_unlock();
2037error_poll_create:
2038 lttng_ht_destroy(relay_connections_ht);
2039relay_connections_ht_error:
58eb9381
DG
2040 /* Close relay conn pipes */
2041 utils_close_pipe(live_conn_pipe);
d3e2ba59
JD
2042 if (err) {
2043 DBG("Viewer worker thread exited with error");
2044 }
2045 DBG("Viewer worker thread cleanup complete");
9b5e0863 2046error_testpoint:
eea7556c
MD
2047 if (err) {
2048 health_error();
2049 ERR("Health error occurred in %s", __func__);
2050 }
2051 health_unregister(health_relayd);
d3e2ba59
JD
2052 stop_threads();
2053 rcu_unregister_thread();
2054 return NULL;
2055}
2056
2057/*
2058 * Create the relay command pipe to wake thread_manage_apps.
2059 * Closed in cleanup().
2060 */
58eb9381 2061static int create_conn_pipe(void)
d3e2ba59
JD
2062{
2063 int ret;
2064
58eb9381 2065 ret = utils_create_pipe_cloexec(live_conn_pipe);
d3e2ba59
JD
2066
2067 return ret;
2068}
2069
aaec7998 2070void live_stop_threads(void)
d3e2ba59
JD
2071{
2072 int ret;
2073 void *status;
2074
2075 stop_threads();
2076
2077 ret = pthread_join(live_listener_thread, &status);
2078 if (ret != 0) {
2079 PERROR("pthread_join live listener");
2080 goto error; /* join error, exit without cleanup */
2081 }
2082
2083 ret = pthread_join(live_worker_thread, &status);
2084 if (ret != 0) {
2085 PERROR("pthread_join live worker");
2086 goto error; /* join error, exit without cleanup */
2087 }
2088
2089 ret = pthread_join(live_dispatcher_thread, &status);
2090 if (ret != 0) {
2091 PERROR("pthread_join live dispatcher");
2092 goto error; /* join error, exit without cleanup */
2093 }
2094
2095 cleanup();
2096
2097error:
2098 return;
2099}
2100
2101/*
2102 * main
2103 */
2104int live_start_threads(struct lttng_uri *uri,
0b242f62 2105 struct relay_local_data *relay_ctx)
d3e2ba59
JD
2106{
2107 int ret = 0;
2108 void *status;
2109 int is_root;
2110
2111 assert(uri);
2112 live_uri = uri;
2113
d3e2ba59
JD
2114 /* Check if daemon is UID = 0 */
2115 is_root = !getuid();
2116
2117 if (!is_root) {
2118 if (live_uri->port < 1024) {
2119 ERR("Need to be root to use ports < 1024");
2120 ret = -1;
2121 goto exit;
2122 }
2123 }
2124
2125 /* Setup the thread apps communication pipe. */
58eb9381 2126 if ((ret = create_conn_pipe()) < 0) {
d3e2ba59
JD
2127 goto exit;
2128 }
2129
2130 /* Init relay command queue. */
58eb9381 2131 cds_wfq_init(&viewer_conn_queue.queue);
d3e2ba59
JD
2132
2133 /* Set up max poll set size */
2134 lttng_poll_set_max_size();
2135
2136 /* Setup the dispatcher thread */
2137 ret = pthread_create(&live_dispatcher_thread, NULL,
2138 thread_dispatcher, (void *) NULL);
2139 if (ret != 0) {
2140 PERROR("pthread_create viewer dispatcher");
2141 goto exit_dispatcher;
2142 }
2143
2144 /* Setup the worker thread */
2145 ret = pthread_create(&live_worker_thread, NULL,
2146 thread_worker, relay_ctx);
2147 if (ret != 0) {
2148 PERROR("pthread_create viewer worker");
2149 goto exit_worker;
2150 }
2151
2152 /* Setup the listener thread */
2153 ret = pthread_create(&live_listener_thread, NULL,
2154 thread_listener, (void *) NULL);
2155 if (ret != 0) {
2156 PERROR("pthread_create viewer listener");
2157 goto exit_listener;
2158 }
2159
2160 ret = 0;
2161 goto end;
2162
2163exit_listener:
2164 ret = pthread_join(live_listener_thread, &status);
2165 if (ret != 0) {
2166 PERROR("pthread_join live listener");
2167 goto error; /* join error, exit without cleanup */
2168 }
2169
2170exit_worker:
2171 ret = pthread_join(live_worker_thread, &status);
2172 if (ret != 0) {
2173 PERROR("pthread_join live worker");
2174 goto error; /* join error, exit without cleanup */
2175 }
2176
2177exit_dispatcher:
2178 ret = pthread_join(live_dispatcher_thread, &status);
2179 if (ret != 0) {
2180 PERROR("pthread_join live dispatcher");
2181 goto error; /* join error, exit without cleanup */
2182 }
2183
2184exit:
2185 cleanup();
2186
2187end:
2188error:
2189 return ret;
2190}
This page took 0.1317 seconds and 4 git commands to generate.