Test: clear: local, streaming, live, tracefile rotation
[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>
7591bab1 4 * 2015 - Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
d3e2ba59
JD
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License, version 2 only,
8 * as published by the Free Software Foundation.
9 *
10 * This program is distributed in the hope that it will be useful, but WITHOUT
11 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
12 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
13 * more details.
14 *
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18 */
19
6c1c0768 20#define _LGPL_SOURCE
d3e2ba59
JD
21#include <getopt.h>
22#include <grp.h>
23#include <limits.h>
24#include <pthread.h>
25#include <signal.h>
26#include <stdio.h>
27#include <stdlib.h>
28#include <string.h>
29#include <sys/mman.h>
30#include <sys/mount.h>
31#include <sys/resource.h>
32#include <sys/socket.h>
33#include <sys/stat.h>
34#include <sys/types.h>
35#include <sys/wait.h>
36#include <inttypes.h>
37#include <urcu/futex.h>
38#include <urcu/uatomic.h>
7591bab1 39#include <urcu/rculist.h>
d3e2ba59
JD
40#include <unistd.h>
41#include <fcntl.h>
d3e2ba59
JD
42
43#include <lttng/lttng.h>
44#include <common/common.h>
45#include <common/compat/poll.h>
46#include <common/compat/socket.h>
f263b7fd 47#include <common/compat/endian.h>
d3e2ba59
JD
48#include <common/defaults.h>
49#include <common/futex.h>
2f8f53af 50#include <common/index/index.h>
d3e2ba59
JD
51#include <common/sessiond-comm/sessiond-comm.h>
52#include <common/sessiond-comm/inet.h>
53#include <common/sessiond-comm/relayd.h>
54#include <common/uri.h>
55#include <common/utils.h>
56
57#include "cmd.h"
58#include "live.h"
59#include "lttng-relayd.h"
d3e2ba59 60#include "utils.h"
eea7556c 61#include "health-relayd.h"
9b5e0863 62#include "testpoint.h"
2f8f53af 63#include "viewer-stream.h"
2a174661
DG
64#include "stream.h"
65#include "session.h"
66#include "ctf-trace.h"
58eb9381 67#include "connection.h"
7591bab1
MD
68#include "viewer-session.h"
69
70#define SESSION_BUF_DEFAULT_COUNT 16
d3e2ba59
JD
71
72static struct lttng_uri *live_uri;
73
d3e2ba59
JD
74/*
75 * This pipe is used to inform the worker thread that a command is queued and
76 * ready to be processed.
77 */
58eb9381 78static int live_conn_pipe[2] = { -1, -1 };
d3e2ba59
JD
79
80/* Shared between threads */
81static int live_dispatch_thread_exit;
82
83static pthread_t live_listener_thread;
84static pthread_t live_dispatcher_thread;
85static pthread_t live_worker_thread;
86
87/*
88 * Relay command queue.
89 *
90 * The live_thread_listener and live_thread_dispatcher communicate with this
91 * queue.
92 */
58eb9381 93static struct relay_conn_queue viewer_conn_queue;
d3e2ba59
JD
94
95static uint64_t last_relay_viewer_session_id;
7591bab1
MD
96static pthread_mutex_t last_relay_viewer_session_id_lock =
97 PTHREAD_MUTEX_INITIALIZER;
d3e2ba59
JD
98
99/*
100 * Cleanup the daemon
101 */
102static
178a0557 103void cleanup_relayd_live(void)
d3e2ba59
JD
104{
105 DBG("Cleaning up");
106
d3e2ba59
JD
107 free(live_uri);
108}
109
2f8f53af
DG
110/*
111 * Receive a request buffer using a given socket, destination allocated buffer
112 * of length size.
113 *
114 * Return the size of the received message or else a negative value on error
115 * with errno being set by recvmsg() syscall.
116 */
117static
118ssize_t recv_request(struct lttcomm_sock *sock, void *buf, size_t size)
119{
120 ssize_t ret;
121
2f8f53af
DG
122 ret = sock->ops->recvmsg(sock, buf, size, 0);
123 if (ret < 0 || ret != size) {
124 if (ret == 0) {
125 /* Orderly shutdown. Not necessary to print an error. */
126 DBG("Socket %d did an orderly shutdown", sock->fd);
127 } else {
128 ERR("Relay failed to receive request.");
129 }
130 ret = -1;
131 }
132
133 return ret;
134}
135
136/*
137 * Send a response buffer using a given socket, source allocated buffer of
138 * length size.
139 *
140 * Return the size of the sent message or else a negative value on error with
141 * errno being set by sendmsg() syscall.
142 */
143static
144ssize_t send_response(struct lttcomm_sock *sock, void *buf, size_t size)
145{
146 ssize_t ret;
147
2f8f53af
DG
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 }
7591bab1
MD
173 rcu_read_lock();
174 cds_list_for_each_entry_rcu(session,
175 &conn->viewer_session->session_list,
176 viewer_session_node) {
177 if (!session_get(session)) {
178 continue;
179 }
f04a971b
JD
180 current_val = uatomic_cmpxchg(&session->new_streams, 1, 0);
181 ret = current_val;
7591bab1 182 session_put(session);
f04a971b
JD
183 if (ret == 1) {
184 goto end;
185 }
2f8f53af 186 }
f04a971b 187end:
7591bab1 188 rcu_read_unlock();
2f8f53af
DG
189 return ret;
190}
191
192/*
193 * Send viewer streams to the given socket. The ignore_sent_flag indicates if
194 * this function should ignore the sent flag or not.
195 *
196 * Return 0 on success or else a negative value.
197 */
198static
199ssize_t send_viewer_streams(struct lttcomm_sock *sock,
200 struct relay_session *session, unsigned int ignore_sent_flag)
201{
202 ssize_t ret;
203 struct lttng_viewer_stream send_stream;
204 struct lttng_ht_iter iter;
205 struct relay_viewer_stream *vstream;
206
2f8f53af
DG
207 rcu_read_lock();
208
209 cds_lfht_for_each_entry(viewer_streams_ht->ht, &iter.iter, vstream,
210 stream_n.node) {
2a174661
DG
211 struct ctf_trace *ctf_trace;
212
2f8f53af
DG
213 health_code_update();
214
7591bab1
MD
215 if (!viewer_stream_get(vstream)) {
216 continue;
217 }
218
219 pthread_mutex_lock(&vstream->stream->lock);
2f8f53af 220 /* Ignore if not the same session. */
7591bab1 221 if (vstream->stream->trace->session->id != session->id ||
2f8f53af 222 (!ignore_sent_flag && vstream->sent_flag)) {
7591bab1
MD
223 pthread_mutex_unlock(&vstream->stream->lock);
224 viewer_stream_put(vstream);
2f8f53af
DG
225 continue;
226 }
227
7591bab1
MD
228 ctf_trace = vstream->stream->trace;
229 send_stream.id = htobe64(vstream->stream->stream_handle);
2a174661 230 send_stream.ctf_trace_id = htobe64(ctf_trace->id);
7591bab1
MD
231 send_stream.metadata_flag = htobe32(
232 vstream->stream->is_metadata);
f8f011fb
MD
233 if (lttng_strncpy(send_stream.path_name, vstream->path_name,
234 sizeof(send_stream.path_name))) {
235 pthread_mutex_unlock(&vstream->stream->lock);
236 viewer_stream_put(vstream);
237 ret = -1; /* Error. */
238 goto end_unlock;
239 }
240 if (lttng_strncpy(send_stream.channel_name,
241 vstream->channel_name,
242 sizeof(send_stream.channel_name))) {
243 pthread_mutex_unlock(&vstream->stream->lock);
244 viewer_stream_put(vstream);
245 ret = -1; /* Error. */
246 goto end_unlock;
247 }
2f8f53af 248
7591bab1
MD
249 DBG("Sending stream %" PRIu64 " to viewer",
250 vstream->stream->stream_handle);
251 vstream->sent_flag = 1;
252 pthread_mutex_unlock(&vstream->stream->lock);
253
2f8f53af 254 ret = send_response(sock, &send_stream, sizeof(send_stream));
7591bab1 255 viewer_stream_put(vstream);
2f8f53af
DG
256 if (ret < 0) {
257 goto end_unlock;
258 }
2f8f53af
DG
259 }
260
261 ret = 0;
262
263end_unlock:
264 rcu_read_unlock();
265 return ret;
266}
267
268/*
269 * Create every viewer stream possible for the given session with the seek
270 * type. Three counters *can* be return which are in order the total amount of
271 * viewer stream of the session, the number of unsent stream and the number of
272 * stream created. Those counters can be NULL and thus will be ignored.
273 *
274 * Return 0 on success or else a negative value.
275 */
276static
277int make_viewer_streams(struct relay_session *session,
278 enum lttng_viewer_seek seek_t, uint32_t *nb_total, uint32_t *nb_unsent,
bddf80e4 279 uint32_t *nb_created, bool *closed)
2f8f53af
DG
280{
281 int ret;
2f8f53af 282 struct lttng_ht_iter iter;
2a174661 283 struct ctf_trace *ctf_trace;
2f8f53af
DG
284
285 assert(session);
286
287 /*
7591bab1
MD
288 * Hold the session lock to ensure that we see either none or
289 * all initial streams for a session, but no intermediate state.
2f8f53af 290 */
7591bab1 291 pthread_mutex_lock(&session->lock);
2f8f53af 292
bddf80e4
MD
293 if (session->connection_closed) {
294 *closed = true;
295 }
296
2f8f53af 297 /*
7591bab1
MD
298 * Create viewer streams for relay streams that are ready to be
299 * used for a the given session id only.
2f8f53af 300 */
2a174661
DG
301 rcu_read_lock();
302 cds_lfht_for_each_entry(session->ctf_traces_ht->ht, &iter.iter, ctf_trace,
303 node.node) {
304 struct relay_stream *stream;
2f8f53af
DG
305
306 health_code_update();
307
7591bab1 308 if (!ctf_trace_get(ctf_trace)) {
2f8f53af
DG
309 continue;
310 }
311
7591bab1 312 cds_list_for_each_entry_rcu(stream, &ctf_trace->stream_list, stream_node) {
2a174661
DG
313 struct relay_viewer_stream *vstream;
314
7591bab1 315 if (!stream_get(stream)) {
2a174661
DG
316 continue;
317 }
7591bab1 318 /*
d812ecb9 319 * stream published is protected by the session lock.
7591bab1
MD
320 */
321 if (!stream->published) {
322 goto next;
323 }
324 vstream = viewer_stream_get_by_id(stream->stream_handle);
2f8f53af 325 if (!vstream) {
7591bab1 326 vstream = viewer_stream_create(stream, seek_t);
2a174661
DG
327 if (!vstream) {
328 ret = -1;
7591bab1
MD
329 ctf_trace_put(ctf_trace);
330 stream_put(stream);
2a174661
DG
331 goto error_unlock;
332 }
2a174661
DG
333
334 if (nb_created) {
335 /* Update number of created stream counter. */
336 (*nb_created)++;
337 }
2229a09c
MD
338 /*
339 * Ensure a self-reference is preserved even
340 * after we have put our local reference.
341 */
862d3a3b
MD
342 if (!viewer_stream_get(vstream)) {
343 ERR("Unable to get self-reference on viewer stream, logic error.");
344 abort();
345 }
7591bab1
MD
346 } else {
347 if (!vstream->sent_flag && nb_unsent) {
348 /* Update number of unsent stream counter. */
349 (*nb_unsent)++;
350 }
2f8f53af 351 }
2a174661
DG
352 /* Update number of total stream counter. */
353 if (nb_total) {
2229a09c
MD
354 if (stream->is_metadata) {
355 if (!stream->closed ||
356 stream->metadata_received > vstream->metadata_sent) {
357 (*nb_total)++;
358 }
359 } else {
360 if (!stream->closed ||
361 !(((int64_t) (stream->prev_seq - stream->last_net_seq_num)) >= 0)) {
362
363 (*nb_total)++;
364 }
365 }
2f8f53af 366 }
2229a09c
MD
367 /* Put local reference. */
368 viewer_stream_put(vstream);
7591bab1
MD
369 next:
370 stream_put(stream);
2f8f53af 371 }
7591bab1 372 ctf_trace_put(ctf_trace);
2f8f53af
DG
373 }
374
375 ret = 0;
376
377error_unlock:
2a174661 378 rcu_read_unlock();
7591bab1 379 pthread_mutex_unlock(&session->lock);
2f8f53af
DG
380 return ret;
381}
382
b4aacfdc 383int relayd_live_stop(void)
d3e2ba59 384{
b4aacfdc 385 /* Stop dispatch thread */
d3e2ba59 386 CMM_STORE_SHARED(live_dispatch_thread_exit, 1);
58eb9381 387 futex_nto1_wake(&viewer_conn_queue.futex);
b4aacfdc 388 return 0;
d3e2ba59
JD
389}
390
d3e2ba59
JD
391/*
392 * Create a poll set with O_CLOEXEC and add the thread quit pipe to the set.
393 */
394static
395int create_thread_poll_set(struct lttng_poll_event *events, int size)
396{
397 int ret;
398
399 if (events == NULL || size == 0) {
400 ret = -1;
401 goto error;
402 }
403
404 ret = lttng_poll_create(events, size, LTTNG_CLOEXEC);
405 if (ret < 0) {
406 goto error;
407 }
408
409 /* Add quit pipe */
bcf4a440 410 ret = lttng_poll_add(events, thread_quit_pipe[0], LPOLLIN | LPOLLERR);
d3e2ba59
JD
411 if (ret < 0) {
412 goto error;
413 }
414
415 return 0;
416
417error:
418 return ret;
419}
420
421/*
422 * Check if the thread quit pipe was triggered.
423 *
424 * Return 1 if it was triggered else 0;
425 */
426static
bcf4a440 427int check_thread_quit_pipe(int fd, uint32_t events)
d3e2ba59 428{
bcf4a440 429 if (fd == thread_quit_pipe[0] && (events & LPOLLIN)) {
d3e2ba59
JD
430 return 1;
431 }
432
433 return 0;
434}
435
436/*
437 * Create and init socket from uri.
438 */
439static
440struct lttcomm_sock *init_socket(struct lttng_uri *uri)
441{
442 int ret;
443 struct lttcomm_sock *sock = NULL;
444
445 sock = lttcomm_alloc_sock_from_uri(uri);
446 if (sock == NULL) {
447 ERR("Allocating socket");
448 goto error;
449 }
450
451 ret = lttcomm_create_sock(sock);
452 if (ret < 0) {
453 goto error;
454 }
e6c533d6 455 DBG("Listening on sock %d for lttng-live", sock->fd);
d3e2ba59
JD
456
457 ret = sock->ops->bind(sock);
458 if (ret < 0) {
e6c533d6 459 PERROR("Failed to bind lttng-live socket");
d3e2ba59
JD
460 goto error;
461 }
462
463 ret = sock->ops->listen(sock, -1);
464 if (ret < 0) {
465 goto error;
466
467 }
468
469 return sock;
470
471error:
472 if (sock) {
473 lttcomm_destroy_sock(sock);
474 }
475 return NULL;
476}
477
478/*
479 * This thread manages the listening for new connections on the network
480 */
481static
482void *thread_listener(void *data)
483{
484 int i, ret, pollfd, err = -1;
d3e2ba59
JD
485 uint32_t revents, nb_fd;
486 struct lttng_poll_event events;
487 struct lttcomm_sock *live_control_sock;
488
489 DBG("[thread] Relay live listener started");
490
eea7556c
MD
491 health_register(health_relayd, HEALTH_RELAYD_TYPE_LIVE_LISTENER);
492
493 health_code_update();
494
d3e2ba59
JD
495 live_control_sock = init_socket(live_uri);
496 if (!live_control_sock) {
497 goto error_sock_control;
498 }
499
fb4d42ab 500 /* Pass 2 as size here for the thread quit pipe and control sockets. */
d3e2ba59
JD
501 ret = create_thread_poll_set(&events, 2);
502 if (ret < 0) {
503 goto error_create_poll;
504 }
505
506 /* Add the control socket */
507 ret = lttng_poll_add(&events, live_control_sock->fd, LPOLLIN | LPOLLRDHUP);
508 if (ret < 0) {
509 goto error_poll_add;
510 }
511
3fd27398
MD
512 lttng_relay_notify_ready();
513
9b5e0863
MD
514 if (testpoint(relayd_thread_live_listener)) {
515 goto error_testpoint;
516 }
517
d3e2ba59 518 while (1) {
eea7556c
MD
519 health_code_update();
520
d3e2ba59
JD
521 DBG("Listener accepting live viewers connections");
522
523restart:
eea7556c 524 health_poll_entry();
d3e2ba59 525 ret = lttng_poll_wait(&events, -1);
eea7556c 526 health_poll_exit();
d3e2ba59
JD
527 if (ret < 0) {
528 /*
529 * Restart interrupted system call.
530 */
531 if (errno == EINTR) {
532 goto restart;
533 }
534 goto error;
535 }
536 nb_fd = ret;
537
538 DBG("Relay new viewer connection received");
539 for (i = 0; i < nb_fd; i++) {
eea7556c
MD
540 health_code_update();
541
d3e2ba59
JD
542 /* Fetch once the poll data */
543 revents = LTTNG_POLL_GETEV(&events, i);
544 pollfd = LTTNG_POLL_GETFD(&events, i);
545
fd20dac9
MD
546 if (!revents) {
547 /* No activity for this FD (poll implementation). */
548 continue;
549 }
550
d3e2ba59 551 /* Thread quit pipe has been closed. Killing thread. */
bcf4a440 552 ret = check_thread_quit_pipe(pollfd, revents);
d3e2ba59
JD
553 if (ret) {
554 err = 0;
555 goto exit;
556 }
557
03e43155 558 if (revents & LPOLLIN) {
d3e2ba59 559 /*
7591bab1
MD
560 * A new connection is requested, therefore a
561 * viewer connection is allocated in this
562 * thread, enqueued to a global queue and
563 * dequeued (and freed) in the worker thread.
d3e2ba59 564 */
58eb9381
DG
565 int val = 1;
566 struct relay_connection *new_conn;
d3e2ba59
JD
567 struct lttcomm_sock *newsock;
568
d3e2ba59
JD
569 newsock = live_control_sock->ops->accept(live_control_sock);
570 if (!newsock) {
571 PERROR("accepting control sock");
d3e2ba59
JD
572 goto error;
573 }
574 DBG("Relay viewer connection accepted socket %d", newsock->fd);
58eb9381 575
d3e2ba59 576 ret = setsockopt(newsock->fd, SOL_SOCKET, SO_REUSEADDR, &val,
58eb9381 577 sizeof(val));
d3e2ba59
JD
578 if (ret < 0) {
579 PERROR("setsockopt inet");
580 lttcomm_destroy_sock(newsock);
d3e2ba59
JD
581 goto error;
582 }
7591bab1
MD
583 new_conn = connection_create(newsock, RELAY_CONNECTION_UNKNOWN);
584 if (!new_conn) {
585 lttcomm_destroy_sock(newsock);
586 goto error;
587 }
588 /* Ownership assumed by the connection. */
589 newsock = NULL;
d3e2ba59 590
58eb9381 591 /* Enqueue request for the dispatcher thread. */
8bdee6e2
SM
592 cds_wfcq_enqueue(&viewer_conn_queue.head, &viewer_conn_queue.tail,
593 &new_conn->qnode);
d3e2ba59
JD
594
595 /*
7591bab1
MD
596 * Wake the dispatch queue futex.
597 * Implicit memory barrier with the
598 * exchange in cds_wfcq_enqueue.
d3e2ba59 599 */
58eb9381 600 futex_nto1_wake(&viewer_conn_queue.futex);
03e43155
MD
601 } else if (revents & (LPOLLERR | LPOLLHUP | LPOLLRDHUP)) {
602 ERR("socket poll error");
603 goto error;
604 } else {
605 ERR("Unexpected poll events %u for sock %d", revents, pollfd);
606 goto error;
d3e2ba59
JD
607 }
608 }
609 }
610
611exit:
612error:
613error_poll_add:
9b5e0863 614error_testpoint:
d3e2ba59
JD
615 lttng_poll_clean(&events);
616error_create_poll:
617 if (live_control_sock->fd >= 0) {
618 ret = live_control_sock->ops->close(live_control_sock);
619 if (ret) {
620 PERROR("close");
621 }
622 }
623 lttcomm_destroy_sock(live_control_sock);
624error_sock_control:
625 if (err) {
eea7556c 626 health_error();
d3e2ba59
JD
627 DBG("Live viewer listener thread exited with error");
628 }
eea7556c 629 health_unregister(health_relayd);
d3e2ba59 630 DBG("Live viewer listener thread cleanup complete");
b4aacfdc
MD
631 if (lttng_relay_stop_threads()) {
632 ERR("Error stopping threads");
178a0557 633 }
d3e2ba59
JD
634 return NULL;
635}
636
637/*
638 * This thread manages the dispatching of the requests to worker threads
639 */
640static
641void *thread_dispatcher(void *data)
642{
6cd525e8
MD
643 int err = -1;
644 ssize_t ret;
8bdee6e2 645 struct cds_wfcq_node *node;
58eb9381 646 struct relay_connection *conn = NULL;
d3e2ba59
JD
647
648 DBG("[thread] Live viewer relay dispatcher started");
649
eea7556c
MD
650 health_register(health_relayd, HEALTH_RELAYD_TYPE_LIVE_DISPATCHER);
651
9b5e0863
MD
652 if (testpoint(relayd_thread_live_dispatcher)) {
653 goto error_testpoint;
654 }
655
eea7556c
MD
656 health_code_update();
657
5f7d3a72 658 for (;;) {
eea7556c
MD
659 health_code_update();
660
d3e2ba59 661 /* Atomically prepare the queue futex */
58eb9381 662 futex_nto1_prepare(&viewer_conn_queue.futex);
d3e2ba59 663
5f7d3a72
MD
664 if (CMM_LOAD_SHARED(live_dispatch_thread_exit)) {
665 break;
666 }
667
d3e2ba59 668 do {
eea7556c
MD
669 health_code_update();
670
d3e2ba59 671 /* Dequeue commands */
8bdee6e2
SM
672 node = cds_wfcq_dequeue_blocking(&viewer_conn_queue.head,
673 &viewer_conn_queue.tail);
d3e2ba59
JD
674 if (node == NULL) {
675 DBG("Woken up but nothing in the live-viewer "
676 "relay command queue");
677 /* Continue thread execution */
678 break;
679 }
58eb9381 680 conn = caa_container_of(node, struct relay_connection, qnode);
d3e2ba59 681 DBG("Dispatching viewer request waiting on sock %d",
58eb9381 682 conn->sock->fd);
d3e2ba59
JD
683
684 /*
7591bab1
MD
685 * Inform worker thread of the new request. This
686 * call is blocking so we can be assured that
687 * the data will be read at some point in time
688 * or wait to the end of the world :)
d3e2ba59 689 */
58eb9381
DG
690 ret = lttng_write(live_conn_pipe[1], &conn, sizeof(conn));
691 if (ret < 0) {
692 PERROR("write conn pipe");
7591bab1 693 connection_put(conn);
d3e2ba59
JD
694 goto error;
695 }
696 } while (node != NULL);
697
698 /* Futex wait on queue. Blocking call on futex() */
eea7556c 699 health_poll_entry();
58eb9381 700 futex_nto1_wait(&viewer_conn_queue.futex);
eea7556c 701 health_poll_exit();
d3e2ba59
JD
702 }
703
eea7556c
MD
704 /* Normal exit, no error */
705 err = 0;
706
d3e2ba59 707error:
9b5e0863 708error_testpoint:
eea7556c
MD
709 if (err) {
710 health_error();
711 ERR("Health error occurred in %s", __func__);
712 }
713 health_unregister(health_relayd);
d3e2ba59 714 DBG("Live viewer dispatch thread dying");
b4aacfdc
MD
715 if (lttng_relay_stop_threads()) {
716 ERR("Error stopping threads");
178a0557 717 }
d3e2ba59
JD
718 return NULL;
719}
720
721/*
722 * Establish connection with the viewer and check the versions.
723 *
724 * Return 0 on success or else negative value.
725 */
726static
58eb9381 727int viewer_connect(struct relay_connection *conn)
d3e2ba59
JD
728{
729 int ret;
730 struct lttng_viewer_connect reply, msg;
731
58eb9381 732 conn->version_check_done = 1;
d3e2ba59 733
eea7556c
MD
734 health_code_update();
735
2f8f53af
DG
736 DBG("Viewer is establishing a connection to the relayd.");
737
58eb9381 738 ret = recv_request(conn->sock, &msg, sizeof(msg));
2f8f53af 739 if (ret < 0) {
d3e2ba59
JD
740 goto end;
741 }
742
eea7556c
MD
743 health_code_update();
744
f46b2ce6 745 memset(&reply, 0, sizeof(reply));
d3e2ba59
JD
746 reply.major = RELAYD_VERSION_COMM_MAJOR;
747 reply.minor = RELAYD_VERSION_COMM_MINOR;
748
749 /* Major versions must be the same */
750 if (reply.major != be32toh(msg.major)) {
2f8f53af
DG
751 DBG("Incompatible major versions ([relayd] %u vs [client] %u)",
752 reply.major, be32toh(msg.major));
72180669 753 ret = -1;
d3e2ba59
JD
754 goto end;
755 }
756
58eb9381 757 conn->major = reply.major;
d3e2ba59
JD
758 /* We adapt to the lowest compatible version */
759 if (reply.minor <= be32toh(msg.minor)) {
58eb9381 760 conn->minor = reply.minor;
d3e2ba59 761 } else {
58eb9381 762 conn->minor = be32toh(msg.minor);
d3e2ba59
JD
763 }
764
c4e361a4 765 if (be32toh(msg.type) == LTTNG_VIEWER_CLIENT_COMMAND) {
58eb9381 766 conn->type = RELAY_VIEWER_COMMAND;
c4e361a4 767 } else if (be32toh(msg.type) == LTTNG_VIEWER_CLIENT_NOTIFICATION) {
58eb9381 768 conn->type = RELAY_VIEWER_NOTIFICATION;
d3e2ba59
JD
769 } else {
770 ERR("Unknown connection type : %u", be32toh(msg.type));
771 ret = -1;
772 goto end;
773 }
774
775 reply.major = htobe32(reply.major);
776 reply.minor = htobe32(reply.minor);
58eb9381 777 if (conn->type == RELAY_VIEWER_COMMAND) {
93b4787b 778 /*
7591bab1
MD
779 * Increment outside of htobe64 macro, because the argument can
780 * be used more than once within the macro, and thus the
781 * operation may be undefined.
93b4787b 782 */
7591bab1 783 pthread_mutex_lock(&last_relay_viewer_session_id_lock);
93b4787b 784 last_relay_viewer_session_id++;
7591bab1 785 pthread_mutex_unlock(&last_relay_viewer_session_id_lock);
93b4787b 786 reply.viewer_session_id = htobe64(last_relay_viewer_session_id);
d3e2ba59 787 }
eea7556c
MD
788
789 health_code_update();
790
58eb9381 791 ret = send_response(conn->sock, &reply, sizeof(reply));
d3e2ba59 792 if (ret < 0) {
2f8f53af 793 goto end;
d3e2ba59
JD
794 }
795
eea7556c
MD
796 health_code_update();
797
58eb9381 798 DBG("Version check done using protocol %u.%u", conn->major, conn->minor);
d3e2ba59
JD
799 ret = 0;
800
801end:
802 return ret;
803}
804
805/*
806 * Send the viewer the list of current sessions.
7591bab1
MD
807 * We need to create a copy of the hash table content because otherwise
808 * we cannot assume the number of entries stays the same between getting
809 * the number of HT elements and iteration over the HT.
d3e2ba59
JD
810 *
811 * Return 0 on success or else a negative value.
812 */
813static
58eb9381 814int viewer_list_sessions(struct relay_connection *conn)
d3e2ba59 815{
5f10c6b1 816 int ret = 0;
d3e2ba59 817 struct lttng_viewer_list_sessions session_list;
d3e2ba59 818 struct lttng_ht_iter iter;
d3e2ba59 819 struct relay_session *session;
7591bab1
MD
820 struct lttng_viewer_session *send_session_buf = NULL;
821 uint32_t buf_count = SESSION_BUF_DEFAULT_COUNT;
822 uint32_t count = 0;
d3e2ba59
JD
823
824 DBG("List sessions received");
825
7591bab1
MD
826 send_session_buf = zmalloc(SESSION_BUF_DEFAULT_COUNT * sizeof(*send_session_buf));
827 if (!send_session_buf) {
828 return -1;
d3e2ba59
JD
829 }
830
7591bab1
MD
831 rcu_read_lock();
832 cds_lfht_for_each_entry(sessions_ht->ht, &iter.iter, session,
2f8f53af 833 session_n.node) {
7591bab1 834 struct lttng_viewer_session *send_session;
d3e2ba59 835
eea7556c
MD
836 health_code_update();
837
fee5a44e
JR
838 if (session->connection_closed) {
839 /* Skip closed session */
840 continue;
841 }
842
7591bab1
MD
843 if (count >= buf_count) {
844 struct lttng_viewer_session *newbuf;
845 uint32_t new_buf_count = buf_count << 1;
eea7556c 846
7591bab1
MD
847 newbuf = realloc(send_session_buf,
848 new_buf_count * sizeof(*send_session_buf));
849 if (!newbuf) {
850 ret = -1;
5f10c6b1 851 break;
7591bab1
MD
852 }
853 send_session_buf = newbuf;
854 buf_count = new_buf_count;
6763619c 855 }
7591bab1 856 send_session = &send_session_buf[count];
bfc3fb17
MD
857 if (lttng_strncpy(send_session->session_name,
858 session->session_name,
859 sizeof(send_session->session_name))) {
860 ret = -1;
5f10c6b1 861 break;
bfc3fb17
MD
862 }
863 if (lttng_strncpy(send_session->hostname, session->hostname,
864 sizeof(send_session->hostname))) {
865 ret = -1;
5f10c6b1 866 break;
bfc3fb17 867 }
7591bab1
MD
868 send_session->id = htobe64(session->id);
869 send_session->live_timer = htobe32(session->live_timer);
870 if (session->viewer_attached) {
871 send_session->clients = htobe32(1);
872 } else {
873 send_session->clients = htobe32(0);
d227d5bd 874 }
7591bab1
MD
875 send_session->streams = htobe32(session->stream_count);
876 count++;
877 }
878 rcu_read_unlock();
5f10c6b1
JG
879 if (ret < 0) {
880 goto end_free;
881 }
d227d5bd 882
7591bab1 883 session_list.sessions_count = htobe32(count);
d227d5bd 884
7591bab1 885 health_code_update();
d227d5bd 886
7591bab1
MD
887 ret = send_response(conn->sock, &session_list, sizeof(session_list));
888 if (ret < 0) {
889 goto end_free;
d227d5bd 890 }
d227d5bd 891
7591bab1 892 health_code_update();
d227d5bd 893
7591bab1
MD
894 ret = send_response(conn->sock, send_session_buf,
895 count * sizeof(*send_session_buf));
896 if (ret < 0) {
897 goto end_free;
d227d5bd 898 }
7591bab1 899 health_code_update();
d227d5bd 900
7591bab1
MD
901 ret = 0;
902end_free:
903 free(send_session_buf);
904 return ret;
d227d5bd
JD
905}
906
80e8027a 907/*
7591bab1 908 * Send the viewer the list of current streams.
80e8027a
JD
909 */
910static
58eb9381 911int viewer_get_new_streams(struct relay_connection *conn)
80e8027a
JD
912{
913 int ret, send_streams = 0;
7591bab1 914 uint32_t nb_created = 0, nb_unsent = 0, nb_streams = 0, nb_total = 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;
bddf80e4 919 bool closed = false;
80e8027a 920
58eb9381 921 assert(conn);
80e8027a
JD
922
923 DBG("Get new streams received");
924
80e8027a
JD
925 health_code_update();
926
2f8f53af 927 /* Receive the request from the connected client. */
58eb9381 928 ret = recv_request(conn->sock, &request, sizeof(request));
2f8f53af 929 if (ret < 0) {
80e8027a
JD
930 goto error;
931 }
6763619c 932 session_id = be64toh(request.session_id);
80e8027a
JD
933
934 health_code_update();
935
53efb85a
MD
936 memset(&response, 0, sizeof(response));
937
7591bab1 938 session = session_get_by_id(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
7591bab1 945 if (!viewer_session_is_attached(conn->viewer_session, session)) {
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
7591bab1 954 ret = make_viewer_streams(session, LTTNG_VIEWER_SEEK_LAST, &nb_total, &nb_unsent,
bddf80e4 955 &nb_created, &closed);
2f8f53af 956 if (ret < 0) {
7591bab1 957 goto end_put_session;
2f8f53af
DG
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 963 /*
2229a09c
MD
964 * If the session is closed, HUP when there are no more streams
965 * with data.
4479f682 966 */
bddf80e4 967 if (closed && nb_total == 0) {
4479f682 968 send_streams = 0;
bddf80e4 969 response.streams_count = 0;
4479f682
JD
970 response.status = htobe32(LTTNG_VIEWER_NEW_STREAMS_HUP);
971 goto send_reply;
972 }
973
80e8027a
JD
974send_reply:
975 health_code_update();
58eb9381 976 ret = send_response(conn->sock, &response, sizeof(response));
80e8027a 977 if (ret < 0) {
7591bab1 978 goto end_put_session;
80e8027a
JD
979 }
980 health_code_update();
981
982 /*
7591bab1
MD
983 * Unknown or empty session, just return gracefully, the viewer
984 * knows what is happening.
80e8027a
JD
985 */
986 if (!send_streams || !nb_streams) {
987 ret = 0;
7591bab1 988 goto end_put_session;
80e8027a
JD
989 }
990
2f8f53af 991 /*
7591bab1
MD
992 * Send stream and *DON'T* ignore the sent flag so every viewer
993 * streams that were not sent from that point will be sent to
994 * the viewer.
2f8f53af 995 */
58eb9381 996 ret = send_viewer_streams(conn->sock, session, 0);
2f8f53af 997 if (ret < 0) {
7591bab1 998 goto end_put_session;
80e8027a
JD
999 }
1000
7591bab1
MD
1001end_put_session:
1002 if (session) {
1003 session_put(session);
1004 }
80e8027a
JD
1005error:
1006 return ret;
1007}
1008
d3e2ba59
JD
1009/*
1010 * Send the viewer the list of current sessions.
1011 */
1012static
58eb9381 1013int viewer_attach_session(struct relay_connection *conn)
d3e2ba59 1014{
2f8f53af
DG
1015 int send_streams = 0;
1016 ssize_t ret;
80e8027a 1017 uint32_t nb_streams = 0;
2f8f53af 1018 enum lttng_viewer_seek seek_type;
d3e2ba59
JD
1019 struct lttng_viewer_attach_session_request request;
1020 struct lttng_viewer_attach_session_response response;
7591bab1 1021 struct relay_session *session = NULL;
bddf80e4 1022 bool closed = false;
d3e2ba59 1023
58eb9381 1024 assert(conn);
d3e2ba59 1025
eea7556c
MD
1026 health_code_update();
1027
2f8f53af 1028 /* Receive the request from the connected client. */
58eb9381 1029 ret = recv_request(conn->sock, &request, sizeof(request));
2f8f53af 1030 if (ret < 0) {
d3e2ba59
JD
1031 goto error;
1032 }
1033
eea7556c
MD
1034 health_code_update();
1035
53efb85a
MD
1036 memset(&response, 0, sizeof(response));
1037
c3b7390b
JD
1038 if (!conn->viewer_session) {
1039 DBG("Client trying to attach before creating a live viewer session");
1040 response.status = htobe32(LTTNG_VIEWER_ATTACH_NO_SESSION);
1041 goto send_reply;
1042 }
1043
7591bab1 1044 session = session_get_by_id(be64toh(request.session_id));
2f8f53af 1045 if (!session) {
d3e2ba59 1046 DBG("Relay session %" PRIu64 " not found",
c9d85d29 1047 (uint64_t) be64toh(request.session_id));
c4e361a4 1048 response.status = htobe32(LTTNG_VIEWER_ATTACH_UNK);
d3e2ba59
JD
1049 goto send_reply;
1050 }
7591bab1 1051 DBG("Attach session ID %" PRIu64 " received",
c9d85d29 1052 (uint64_t) be64toh(request.session_id));
d3e2ba59 1053
7591bab1 1054 if (session->live_timer == 0) {
d3e2ba59 1055 DBG("Not live session");
c4e361a4 1056 response.status = htobe32(LTTNG_VIEWER_ATTACH_NOT_LIVE);
d3e2ba59 1057 goto send_reply;
7591bab1
MD
1058 }
1059
1060 send_streams = 1;
1061 ret = viewer_session_attach(conn->viewer_session, session);
1062 if (ret) {
1063 DBG("Already a viewer attached");
1064 response.status = htobe32(LTTNG_VIEWER_ATTACH_ALREADY);
1065 goto send_reply;
d3e2ba59
JD
1066 }
1067
1068 switch (be32toh(request.seek)) {
c4e361a4
JD
1069 case LTTNG_VIEWER_SEEK_BEGINNING:
1070 case LTTNG_VIEWER_SEEK_LAST:
7591bab1 1071 response.status = htobe32(LTTNG_VIEWER_ATTACH_OK);
2f8f53af 1072 seek_type = be32toh(request.seek);
d3e2ba59
JD
1073 break;
1074 default:
1075 ERR("Wrong seek parameter");
c4e361a4 1076 response.status = htobe32(LTTNG_VIEWER_ATTACH_SEEK_ERR);
d3e2ba59
JD
1077 send_streams = 0;
1078 goto send_reply;
1079 }
1080
bddf80e4
MD
1081 ret = make_viewer_streams(session, seek_type, &nb_streams, NULL,
1082 NULL, &closed);
2f8f53af 1083 if (ret < 0) {
7591bab1 1084 goto end_put_session;
d3e2ba59 1085 }
2f8f53af 1086 response.streams_count = htobe32(nb_streams);
d3e2ba59 1087
bddf80e4
MD
1088 /*
1089 * If the session is closed when the viewer is attaching, it
1090 * means some of the streams may have been concurrently removed,
1091 * so we don't allow the viewer to attach, even if there are
1092 * streams available.
1093 */
1094 if (closed) {
1095 send_streams = 0;
1096 response.streams_count = 0;
a077f1ca 1097 response.status = htobe32(LTTNG_VIEWER_ATTACH_UNK);
bddf80e4
MD
1098 goto send_reply;
1099 }
1100
d8b7ee90
JR
1101 if (testpoint(relayd_viewer_session_attach)) {
1102 ERR("Viewer session attach testpoint failed");
1103 send_streams = 0;
1104 response.streams_count = 0;
1105 response.status = htobe32(LTTNG_VIEWER_ATTACH_UNK);
1106 goto send_reply;
1107 }
1108
1109
d3e2ba59 1110send_reply:
eea7556c 1111 health_code_update();
58eb9381 1112 ret = send_response(conn->sock, &response, sizeof(response));
d3e2ba59 1113 if (ret < 0) {
7591bab1 1114 goto end_put_session;
d3e2ba59 1115 }
eea7556c 1116 health_code_update();
d3e2ba59
JD
1117
1118 /*
7591bab1
MD
1119 * Unknown or empty session, just return gracefully, the viewer
1120 * knows what is happening.
d3e2ba59 1121 */
157df586 1122 if (!send_streams || !nb_streams) {
d3e2ba59 1123 ret = 0;
7591bab1 1124 goto end_put_session;
d3e2ba59
JD
1125 }
1126
2f8f53af 1127 /* Send stream and ignore the sent flag. */
58eb9381 1128 ret = send_viewer_streams(conn->sock, session, 1);
2f8f53af 1129 if (ret < 0) {
7591bab1 1130 goto end_put_session;
d3e2ba59 1131 }
d3e2ba59 1132
7591bab1
MD
1133end_put_session:
1134 if (session) {
1135 session_put(session);
1136 }
4a9daf17
JD
1137error:
1138 return ret;
1139}
1140
878c34cf
DG
1141/*
1142 * Open the index file if needed for the given vstream.
1143 *
f8f3885c
MD
1144 * If an index file is successfully opened, the vstream will set it as its
1145 * current index file.
878c34cf
DG
1146 *
1147 * Return 0 on success, a negative value on error (-ENOENT if not ready yet).
7591bab1
MD
1148 *
1149 * Called with rstream lock held.
878c34cf
DG
1150 */
1151static int try_open_index(struct relay_viewer_stream *vstream,
1152 struct relay_stream *rstream)
1153{
1154 int ret = 0;
1155
f8f3885c 1156 if (vstream->index_file) {
878c34cf
DG
1157 goto end;
1158 }
1159
1160 /*
82b4cfcc 1161 * Deal with streams that have not received any index so far (or after clear).
878c34cf 1162 */
82b4cfcc 1163 if (rstream->index_received_seqcount <= rstream->clear_position_index_seqcount) {
878c34cf
DG
1164 ret = -ENOENT;
1165 goto end;
1166 }
f8f3885c
MD
1167 vstream->index_file = lttng_index_file_open(vstream->path_name,
1168 vstream->channel_name,
7591bab1
MD
1169 vstream->stream->tracefile_count,
1170 vstream->current_tracefile_id);
f8f3885c
MD
1171 if (!vstream->index_file) {
1172 ret = -1;
878c34cf
DG
1173 }
1174
1175end:
1176 return ret;
1177}
1178
1179/*
7591bab1
MD
1180 * Check the status of the index for the given stream. This function
1181 * updates the index structure if needed and can put (close) the vstream
1182 * in the HUP situation.
1183 *
1184 * Return 0 means that we can proceed with the index. A value of 1 means
1185 * that the index has been updated and is ready to be sent to the
1186 * client. A negative value indicates an error that can't be handled.
878c34cf 1187 *
7591bab1 1188 * Called with rstream lock held.
878c34cf
DG
1189 */
1190static int check_index_status(struct relay_viewer_stream *vstream,
1191 struct relay_stream *rstream, struct ctf_trace *trace,
1192 struct lttng_viewer_index *index)
1193{
1194 int ret;
1195
82b4cfcc
MD
1196 DBG("check status: recv %" PRIu64 " sent %" PRIu64 " clear index %" PRIu64 " clear data %" PRIu64 " for stream %" PRIu64,
1197 rstream->index_received_seqcount,
1198 vstream->index_sent_seqcount,
1199 rstream->clear_position_index_seqcount,
1200 rstream->clear_position_data_seqcount, vstream->stream->stream_handle);
797bc362 1201 if ((trace->session->connection_closed || rstream->closed)
a44ca2ca
MD
1202 && rstream->index_received_seqcount
1203 == vstream->index_sent_seqcount) {
797bc362
MD
1204 /*
1205 * Last index sent and session connection or relay
1206 * stream are closed.
1207 */
7591bab1
MD
1208 index->status = htobe32(LTTNG_VIEWER_INDEX_HUP);
1209 goto hup;
1210 } else if (rstream->beacon_ts_end != -1ULL &&
a44ca2ca 1211 rstream->index_received_seqcount
82b4cfcc 1212 <= vstream->index_sent_seqcount) {
7591bab1
MD
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 * In this case, we have received a beacon which allows us to
1218 * inform the client of a time interval during which we can
1219 * guarantee that there are no events to read (and never will
1220 * be).
82b4cfcc
MD
1221 *
1222 * The sent seqcount can grow higher than receive seqcount on clear.
7591bab1
MD
1223 */
1224 index->status = htobe32(LTTNG_VIEWER_INDEX_INACTIVE);
1225 index->timestamp_end = htobe64(rstream->beacon_ts_end);
1226 index->stream_id = htobe64(rstream->ctf_stream_id);
82b4cfcc 1227 DBG("INACTIVE: with beacon, r v recv eq for stream %" PRIu64, vstream->stream->stream_handle);
7591bab1 1228 goto index_ready;
a44ca2ca 1229 } else if (rstream->index_received_seqcount
82b4cfcc 1230 <= vstream->index_sent_seqcount) {
7591bab1 1231 /*
82b4cfcc 1232 * This checks whether received <= sent seqcount. In
a44ca2ca
MD
1233 * this case, we have not received a beacon. Therefore,
1234 * we can only ask the client to retry later.
82b4cfcc
MD
1235 *
1236 * The sent seqcount can grow higher than receive seqcount on clear.
7591bab1
MD
1237 */
1238 index->status = htobe32(LTTNG_VIEWER_INDEX_RETRY);
82b4cfcc
MD
1239 DBG("RETRY: r v recv leq for stream %" PRIu64, vstream->stream->stream_handle);
1240 goto index_ready;
1241 } else if (rstream->index_received_seqcount <= rstream->clear_position_index_seqcount ||
1242 rstream->index_received_seqcount <= rstream->clear_position_data_seqcount) {
1243 /*
1244 * Send "retry" reply if a clear operation is in progress.
1245 */
1246 index->status = htobe32(LTTNG_VIEWER_INDEX_RETRY);
1247 DBG("RETRY: r <= clear pos for stream %" PRIu64, vstream->stream->stream_handle);
7591bab1 1248 goto index_ready;
a44ca2ca
MD
1249 } else if (!tracefile_array_seq_in_file(rstream->tfa,
1250 vstream->current_tracefile_id,
1251 vstream->index_sent_seqcount)) {
7591bab1 1252 /*
a44ca2ca
MD
1253 * The next index we want to send cannot be read either
1254 * because we need to perform a rotation, or due to
1255 * the producer having overwritten its trace file.
7591bab1 1256 */
a44ca2ca 1257 DBG("Viewer stream %" PRIu64 " rotation",
7591bab1
MD
1258 vstream->stream->stream_handle);
1259 ret = viewer_stream_rotate(vstream);
1260 if (ret < 0) {
1261 goto end;
1262 } else if (ret == 1) {
1263 /* EOF across entire stream. */
1264 index->status = htobe32(LTTNG_VIEWER_INDEX_HUP);
1265 goto hup;
1266 }
7591bab1 1267 /*
a44ca2ca
MD
1268 * If we have been pushed due to overwrite, it
1269 * necessarily means there is data that can be read in
1270 * the stream. If we rotated because we reached the end
1271 * of a tracefile, it means the following tracefile
1272 * needs to contain at least one index, else we would
1273 * have already returned LTTNG_VIEWER_INDEX_RETRY to the
1274 * viewer. The updated index_sent_seqcount needs to
1275 * point to a readable index entry now.
1276 *
1277 * In the case where we "rotate" on a single file, we
1278 * can end up in a case where the requested index is
1279 * still unavailable.
7591bab1 1280 */
a44ca2ca
MD
1281 if (rstream->tracefile_count == 1 &&
1282 !tracefile_array_seq_in_file(
1283 rstream->tfa,
1284 vstream->current_tracefile_id,
1285 vstream->index_sent_seqcount)) {
1286 index->status = htobe32(LTTNG_VIEWER_INDEX_RETRY);
82b4cfcc 1287 DBG("RETRY: !tfs seq in file for stream %" PRIu64, vstream->stream->stream_handle);
a44ca2ca 1288 goto index_ready;
878c34cf 1289 }
a44ca2ca
MD
1290 assert(tracefile_array_seq_in_file(rstream->tfa,
1291 vstream->current_tracefile_id,
1292 vstream->index_sent_seqcount));
878c34cf 1293 }
a44ca2ca
MD
1294 /* ret == 0 means successful so we continue. */
1295 ret = 0;
7591bab1 1296end:
878c34cf
DG
1297 return ret;
1298
1299hup:
7591bab1 1300 viewer_stream_put(vstream);
878c34cf
DG
1301index_ready:
1302 return 1;
1303}
1304
d3e2ba59
JD
1305/*
1306 * Send the next index for a stream.
1307 *
1308 * Return 0 on success or else a negative value.
1309 */
1310static
58eb9381 1311int viewer_get_next_index(struct relay_connection *conn)
d3e2ba59
JD
1312{
1313 int ret;
1314 struct lttng_viewer_get_next_index request_index;
1315 struct lttng_viewer_index viewer_index;
50adc264 1316 struct ctf_packet_index packet_index;
7591bab1
MD
1317 struct relay_viewer_stream *vstream = NULL;
1318 struct relay_stream *rstream = NULL;
1319 struct ctf_trace *ctf_trace = NULL;
1320 struct relay_viewer_stream *metadata_viewer_stream = NULL;
d3e2ba59 1321
58eb9381 1322 assert(conn);
d3e2ba59
JD
1323
1324 DBG("Viewer get next index");
1325
7591bab1 1326 memset(&viewer_index, 0, sizeof(viewer_index));
eea7556c 1327 health_code_update();
2f8f53af 1328
58eb9381 1329 ret = recv_request(conn->sock, &request_index, sizeof(request_index));
2f8f53af 1330 if (ret < 0) {
d3e2ba59
JD
1331 goto end;
1332 }
eea7556c 1333 health_code_update();
d3e2ba59 1334
7591bab1 1335 vstream = viewer_stream_get_by_id(be64toh(request_index.stream_id));
6763619c 1336 if (!vstream) {
a44ca2ca 1337 DBG("Client requested index of unknown stream id %" PRIu64,
c9d85d29 1338 (uint64_t) be64toh(request_index.stream_id));
f1883937
MD
1339 viewer_index.status = htobe32(LTTNG_VIEWER_INDEX_ERR);
1340 goto send_reply;
2a174661
DG
1341 }
1342
7591bab1
MD
1343 /* Use back. ref. Protected by refcounts. */
1344 rstream = vstream->stream;
1345 ctf_trace = rstream->trace;
d3e2ba59 1346
7591bab1
MD
1347 /* metadata_viewer_stream may be NULL. */
1348 metadata_viewer_stream =
1349 ctf_trace_get_viewer_metadata_stream(ctf_trace);
2a174661 1350
7591bab1 1351 pthread_mutex_lock(&rstream->lock);
d3e2ba59
JD
1352
1353 /*
1354 * The viewer should not ask for index on metadata stream.
1355 */
7591bab1 1356 if (rstream->is_metadata) {
c4e361a4 1357 viewer_index.status = htobe32(LTTNG_VIEWER_INDEX_HUP);
d3e2ba59
JD
1358 goto send_reply;
1359 }
1360
82b4cfcc
MD
1361 /*
1362 * In case the stream has been cleared, we need to push the viewer
1363 * stream index sent seqcount forward. Note that this can temporarily
1364 * bring the index_sent position beyond the index received position.
1365 */
1366 if (rstream->clear_position_index_seqcount >= vstream->index_sent_seqcount) {
1367 vstream->index_sent_seqcount = rstream->clear_position_index_seqcount;
1368 /*
1369 * Close the currently open index and data files to ensure we
1370 * sync up with the receive side.
1371 */
1372 if (vstream->index_file) {
1373 lttng_index_file_put(vstream->index_file);
1374 vstream->index_file = NULL;
1375 }
1376 if (vstream->stream_fd) {
1377 stream_fd_put(vstream->stream_fd);
1378 vstream->stream_fd = NULL;
1379 }
1380 /*
1381 * In tracefile rotation, we reset the current tracefile to 0.
1382 */
1383 vstream->current_tracefile_id = 0;
1384 }
1385
878c34cf
DG
1386 /* Try to open an index if one is needed for that stream. */
1387 ret = try_open_index(vstream, rstream);
1388 if (ret < 0) {
0e6830aa 1389 if (ret == -ENOENT) {
d3e2ba59 1390 /*
7591bab1
MD
1391 * The index is created only when the first data
1392 * packet arrives, it might not be ready at the
82b4cfcc
MD
1393 * beginning of the session. Let check_index_status
1394 * deal with inactivity beacons.
d3e2ba59 1395 */
82b4cfcc 1396 goto check_status;
878c34cf
DG
1397 } else {
1398 /* Unhandled error. */
c4e361a4 1399 viewer_index.status = htobe32(LTTNG_VIEWER_INDEX_ERR);
d3e2ba59 1400 }
878c34cf 1401 goto send_reply;
d3e2ba59
JD
1402 }
1403
82b4cfcc 1404check_status:
878c34cf 1405 ret = check_index_status(vstream, rstream, ctf_trace, &viewer_index);
878c34cf 1406 if (ret < 0) {
7591bab1 1407 goto error_put;
878c34cf
DG
1408 } else if (ret == 1) {
1409 /*
7591bab1
MD
1410 * We have no index to send and check_index_status has populated
1411 * viewer_index's status.
878c34cf 1412 */
d3e2ba59
JD
1413 goto send_reply;
1414 }
7591bab1 1415 /* At this point, ret is 0 thus we will be able to read the index. */
878c34cf 1416 assert(!ret);
d3e2ba59 1417
7591bab1
MD
1418 /*
1419 * vstream->stream_fd may be NULL if it has been closed by
1420 * tracefile rotation, or if we are at the beginning of the
1421 * stream. We open the data stream file here to protect against
1422 * overwrite caused by tracefile rotation (in association with
1423 * unlink performed before overwrite).
1424 */
1425 if (!vstream->stream_fd) {
1426 char fullpath[PATH_MAX];
1427
1428 if (vstream->stream->tracefile_count > 0) {
1429 ret = snprintf(fullpath, PATH_MAX, "%s/%s_%" PRIu64,
1430 vstream->path_name,
1431 vstream->channel_name,
1432 vstream->current_tracefile_id);
1433 } else {
1434 ret = snprintf(fullpath, PATH_MAX, "%s/%s",
1435 vstream->path_name,
1436 vstream->channel_name);
1437 }
1438 if (ret < 0) {
1439 goto error_put;
1440 }
1441 ret = open(fullpath, O_RDONLY);
1442 if (ret < 0) {
1443 PERROR("Relay opening trace file");
1444 goto error_put;
1445 }
1446 vstream->stream_fd = stream_fd_create(ret);
1447 if (!vstream->stream_fd) {
1448 if (close(ret)) {
1449 PERROR("close");
1450 }
1451 goto error_put;
1452 }
d3e2ba59
JD
1453 }
1454
f04a971b 1455 ret = check_new_streams(conn);
4a9daf17 1456 if (ret < 0) {
7591bab1
MD
1457 viewer_index.status = htobe32(LTTNG_VIEWER_INDEX_ERR);
1458 goto send_reply;
4a9daf17
JD
1459 } else if (ret == 1) {
1460 viewer_index.flags |= LTTNG_VIEWER_FLAG_NEW_STREAM;
1461 }
1462
f8f3885c
MD
1463 ret = lttng_index_file_read(vstream->index_file, &packet_index);
1464 if (ret) {
1465 ERR("Relay error reading index file %d",
1466 vstream->index_file->fd);
7591bab1 1467 viewer_index.status = htobe32(LTTNG_VIEWER_INDEX_ERR);
6b6b9a5a 1468 goto send_reply;
d3e2ba59 1469 } else {
c4e361a4 1470 viewer_index.status = htobe32(LTTNG_VIEWER_INDEX_OK);
a44ca2ca 1471 vstream->index_sent_seqcount++;
d3e2ba59
JD
1472 }
1473
1474 /*
1475 * Indexes are stored in big endian, no need to switch before sending.
1476 */
7591bab1
MD
1477 DBG("Sending viewer index for stream %" PRIu64 " offset %" PRIu64,
1478 rstream->stream_handle,
c9d85d29 1479 (uint64_t) be64toh(packet_index.offset));
d3e2ba59
JD
1480 viewer_index.offset = packet_index.offset;
1481 viewer_index.packet_size = packet_index.packet_size;
1482 viewer_index.content_size = packet_index.content_size;
1483 viewer_index.timestamp_begin = packet_index.timestamp_begin;
1484 viewer_index.timestamp_end = packet_index.timestamp_end;
1485 viewer_index.events_discarded = packet_index.events_discarded;
1486 viewer_index.stream_id = packet_index.stream_id;
1487
1488send_reply:
f1883937
MD
1489 if (rstream) {
1490 pthread_mutex_unlock(&rstream->lock);
1491 }
7591bab1
MD
1492
1493 if (metadata_viewer_stream) {
1494 pthread_mutex_lock(&metadata_viewer_stream->stream->lock);
1495 DBG("get next index metadata check: recv %" PRIu64
1496 " sent %" PRIu64,
1497 metadata_viewer_stream->stream->metadata_received,
1498 metadata_viewer_stream->metadata_sent);
1499 if (!metadata_viewer_stream->stream->metadata_received ||
1500 metadata_viewer_stream->stream->metadata_received >
1501 metadata_viewer_stream->metadata_sent) {
1502 viewer_index.flags |= LTTNG_VIEWER_FLAG_NEW_METADATA;
1503 }
1504 pthread_mutex_unlock(&metadata_viewer_stream->stream->lock);
1505 }
1506
d3e2ba59 1507 viewer_index.flags = htobe32(viewer_index.flags);
eea7556c 1508 health_code_update();
2f8f53af 1509
58eb9381 1510 ret = send_response(conn->sock, &viewer_index, sizeof(viewer_index));
d3e2ba59 1511 if (ret < 0) {
7591bab1 1512 goto end;
d3e2ba59 1513 }
eea7556c 1514 health_code_update();
d3e2ba59 1515
9237e6a1
MD
1516 if (vstream) {
1517 DBG("Index %" PRIu64 " for stream %" PRIu64 " sent",
a44ca2ca 1518 vstream->index_sent_seqcount,
9237e6a1
MD
1519 vstream->stream->stream_handle);
1520 }
d3e2ba59 1521end:
7591bab1
MD
1522 if (metadata_viewer_stream) {
1523 viewer_stream_put(metadata_viewer_stream);
1524 }
1525 if (vstream) {
1526 viewer_stream_put(vstream);
1527 }
1528 return ret;
1529
1530error_put:
1531 pthread_mutex_unlock(&rstream->lock);
1532 if (metadata_viewer_stream) {
1533 viewer_stream_put(metadata_viewer_stream);
1534 }
1535 viewer_stream_put(vstream);
d3e2ba59
JD
1536 return ret;
1537}
1538
1539/*
1540 * Send the next index for a stream
1541 *
1542 * Return 0 on success or else a negative value.
1543 */
1544static
58eb9381 1545int viewer_get_packet(struct relay_connection *conn)
d3e2ba59 1546{
7791c08d 1547 int ret;
987af241 1548 off_t lseek_ret;
a9f0dd93 1549 char *reply = NULL;
d3e2ba59 1550 struct lttng_viewer_get_packet get_packet_info;
a9f0dd93 1551 struct lttng_viewer_trace_packet reply_header;
7591bab1 1552 struct relay_viewer_stream *vstream = NULL;
a9f0dd93 1553 uint32_t reply_size = sizeof(reply_header);
7791c08d
JR
1554 uint32_t packet_data_len = 0;
1555 ssize_t read_len;
d3e2ba59
JD
1556
1557 DBG2("Relay get data packet");
1558
eea7556c 1559 health_code_update();
2f8f53af 1560
7591bab1
MD
1561 ret = recv_request(conn->sock, &get_packet_info,
1562 sizeof(get_packet_info));
2f8f53af 1563 if (ret < 0) {
d3e2ba59
JD
1564 goto end;
1565 }
eea7556c 1566 health_code_update();
d3e2ba59 1567
0233a6a5 1568 /* From this point on, the error label can be reached. */
a9f0dd93 1569 memset(&reply_header, 0, sizeof(reply_header));
0233a6a5 1570
7591bab1
MD
1571 vstream = viewer_stream_get_by_id(be64toh(get_packet_info.stream_id));
1572 if (!vstream) {
a44ca2ca 1573 DBG("Client requested packet of unknown stream id %" PRIu64,
c9d85d29 1574 (uint64_t) be64toh(get_packet_info.stream_id));
a9f0dd93
JG
1575 reply_header.status = htobe32(LTTNG_VIEWER_GET_PACKET_ERR);
1576 goto send_reply_nolock;
7791c08d
JR
1577 } else {
1578 packet_data_len = be32toh(get_packet_info.len);
a9f0dd93 1579 reply_size += packet_data_len;
d3e2ba59 1580 }
2a174661 1581
a9f0dd93
JG
1582 reply = zmalloc(reply_size);
1583 if (!reply) {
1584 PERROR("packet reply zmalloc");
1585 reply_size = sizeof(reply_header);
d3e2ba59
JD
1586 goto error;
1587 }
1588
7791c08d 1589 pthread_mutex_lock(&vstream->stream->lock);
987af241 1590 lseek_ret = lseek(vstream->stream_fd->fd, be64toh(get_packet_info.offset),
7591bab1 1591 SEEK_SET);
987af241 1592 if (lseek_ret < 0) {
7591bab1 1593 PERROR("lseek fd %d to offset %" PRIu64, vstream->stream_fd->fd,
c9d85d29 1594 (uint64_t) be64toh(get_packet_info.offset));
7591bab1 1595 goto error;
d3e2ba59 1596 }
7791c08d 1597 read_len = lttng_read(vstream->stream_fd->fd,
a9f0dd93 1598 reply + sizeof(reply_header),
7791c08d
JR
1599 packet_data_len);
1600 if (read_len < packet_data_len) {
7591bab1
MD
1601 PERROR("Relay reading trace file, fd: %d, offset: %" PRIu64,
1602 vstream->stream_fd->fd,
c9d85d29 1603 (uint64_t) be64toh(get_packet_info.offset));
7591bab1 1604 goto error;
d3e2ba59 1605 }
a9f0dd93
JG
1606 reply_header.status = htobe32(LTTNG_VIEWER_GET_PACKET_OK);
1607 reply_header.len = htobe32(packet_data_len);
d3e2ba59
JD
1608 goto send_reply;
1609
1610error:
a9f0dd93 1611 reply_header.status = htobe32(LTTNG_VIEWER_GET_PACKET_ERR);
d3e2ba59
JD
1612
1613send_reply:
7591bab1
MD
1614 if (vstream) {
1615 pthread_mutex_unlock(&vstream->stream->lock);
1616 }
1617send_reply_nolock:
eea7556c
MD
1618
1619 health_code_update();
2f8f53af 1620
a9f0dd93
JG
1621 if (reply) {
1622 memcpy(reply, &reply_header, sizeof(reply_header));
1623 ret = send_response(conn->sock, reply, reply_size);
1624 } else {
1625 /* No reply to send. */
1626 ret = send_response(conn->sock, &reply_header,
1627 reply_size);
1628 }
1629
7791c08d 1630 health_code_update();
d3e2ba59 1631 if (ret < 0) {
7791c08d 1632 PERROR("sendmsg of packet data failed");
7591bab1 1633 goto end_free;
d3e2ba59 1634 }
d3e2ba59 1635
a9f0dd93 1636 DBG("Sent %u bytes for stream %" PRIu64, reply_size,
c9d85d29 1637 (uint64_t) be64toh(get_packet_info.stream_id));
d3e2ba59 1638
7591bab1 1639end_free:
a9f0dd93 1640 free(reply);
d3e2ba59 1641end:
7591bab1
MD
1642 if (vstream) {
1643 viewer_stream_put(vstream);
1644 }
d3e2ba59
JD
1645 return ret;
1646}
1647
1648/*
1649 * Send the session's metadata
1650 *
1651 * Return 0 on success else a negative value.
1652 */
1653static
58eb9381 1654int viewer_get_metadata(struct relay_connection *conn)
d3e2ba59
JD
1655{
1656 int ret = 0;
1657 ssize_t read_len;
1658 uint64_t len = 0;
1659 char *data = NULL;
1660 struct lttng_viewer_get_metadata request;
1661 struct lttng_viewer_metadata_packet reply;
7591bab1 1662 struct relay_viewer_stream *vstream = NULL;
d3e2ba59 1663
58eb9381 1664 assert(conn);
d3e2ba59
JD
1665
1666 DBG("Relay get metadata");
1667
eea7556c 1668 health_code_update();
2f8f53af 1669
58eb9381 1670 ret = recv_request(conn->sock, &request, sizeof(request));
2f8f53af 1671 if (ret < 0) {
d3e2ba59
JD
1672 goto end;
1673 }
eea7556c 1674 health_code_update();
d3e2ba59 1675
53efb85a
MD
1676 memset(&reply, 0, sizeof(reply));
1677
7591bab1
MD
1678 vstream = viewer_stream_get_by_id(be64toh(request.stream_id));
1679 if (!vstream) {
3b463131
MD
1680 /*
1681 * The metadata stream can be closed by a CLOSE command
1682 * just before we attach. It can also be closed by
1683 * per-pid tracing during tracing. Therefore, it is
1684 * possible that we cannot find this viewer stream.
1685 * Reply back to the client with an error if we cannot
1686 * find it.
1687 */
a44ca2ca 1688 DBG("Client requested metadata of unknown stream id %" PRIu64,
c9d85d29 1689 (uint64_t) be64toh(request.stream_id));
3b463131 1690 reply.status = htobe32(LTTNG_VIEWER_METADATA_ERR);
7591bab1 1691 goto send_reply;
d3e2ba59 1692 }
7591bab1
MD
1693 pthread_mutex_lock(&vstream->stream->lock);
1694 if (!vstream->stream->is_metadata) {
1695 ERR("Invalid metadata stream");
6763619c
JD
1696 goto error;
1697 }
1698
7591bab1 1699 assert(vstream->metadata_sent <= vstream->stream->metadata_received);
2a174661 1700
7591bab1 1701 len = vstream->stream->metadata_received - vstream->metadata_sent;
d3e2ba59 1702 if (len == 0) {
c4e361a4 1703 reply.status = htobe32(LTTNG_VIEWER_NO_NEW_METADATA);
d3e2ba59
JD
1704 goto send_reply;
1705 }
1706
1707 /* first time, we open the metadata file */
7591bab1 1708 if (!vstream->stream_fd) {
d3e2ba59
JD
1709 char fullpath[PATH_MAX];
1710
7591bab1
MD
1711 ret = snprintf(fullpath, PATH_MAX, "%s/%s", vstream->path_name,
1712 vstream->channel_name);
d3e2ba59
JD
1713 if (ret < 0) {
1714 goto error;
1715 }
1716 ret = open(fullpath, O_RDONLY);
1717 if (ret < 0) {
1718 PERROR("Relay opening metadata file");
1719 goto error;
1720 }
7591bab1
MD
1721 vstream->stream_fd = stream_fd_create(ret);
1722 if (!vstream->stream_fd) {
1723 if (close(ret)) {
1724 PERROR("close");
1725 }
1726 goto error;
1727 }
d3e2ba59
JD
1728 }
1729
1730 reply.len = htobe64(len);
1731 data = zmalloc(len);
1732 if (!data) {
1733 PERROR("viewer metadata zmalloc");
1734 goto error;
1735 }
1736
7591bab1 1737 read_len = lttng_read(vstream->stream_fd->fd, data, len);
6cd525e8 1738 if (read_len < len) {
d3e2ba59
JD
1739 PERROR("Relay reading metadata file");
1740 goto error;
1741 }
7591bab1
MD
1742 vstream->metadata_sent += read_len;
1743 if (vstream->metadata_sent == vstream->stream->metadata_received
1744 && vstream->stream->closed) {
1745 /* Release ownership for the viewer metadata stream. */
1746 viewer_stream_put(vstream);
1747 }
1748
c4e361a4 1749 reply.status = htobe32(LTTNG_VIEWER_METADATA_OK);
7591bab1 1750
d3e2ba59
JD
1751 goto send_reply;
1752
1753error:
c4e361a4 1754 reply.status = htobe32(LTTNG_VIEWER_METADATA_ERR);
d3e2ba59
JD
1755
1756send_reply:
eea7556c 1757 health_code_update();
7591bab1
MD
1758 if (vstream) {
1759 pthread_mutex_unlock(&vstream->stream->lock);
1760 }
58eb9381 1761 ret = send_response(conn->sock, &reply, sizeof(reply));
d3e2ba59 1762 if (ret < 0) {
7591bab1 1763 goto end_free;
d3e2ba59 1764 }
eea7556c 1765 health_code_update();
d3e2ba59
JD
1766
1767 if (len > 0) {
58eb9381 1768 ret = send_response(conn->sock, data, len);
d3e2ba59 1769 if (ret < 0) {
7591bab1 1770 goto end_free;
d3e2ba59
JD
1771 }
1772 }
1773
1774 DBG("Sent %" PRIu64 " bytes of metadata for stream %" PRIu64, len,
c9d85d29 1775 (uint64_t) be64toh(request.stream_id));
d3e2ba59
JD
1776
1777 DBG("Metadata sent");
1778
7591bab1 1779end_free:
d3e2ba59 1780 free(data);
d3e2ba59 1781end:
7591bab1
MD
1782 if (vstream) {
1783 viewer_stream_put(vstream);
1784 }
d3e2ba59
JD
1785 return ret;
1786}
1787
c3b7390b
JD
1788/*
1789 * Create a viewer session.
1790 *
1791 * Return 0 on success or else a negative value.
1792 */
1793static
1794int viewer_create_session(struct relay_connection *conn)
1795{
1796 int ret;
1797 struct lttng_viewer_create_session_response resp;
1798
1799 DBG("Viewer create session received");
1800
53efb85a 1801 memset(&resp, 0, sizeof(resp));
c3b7390b 1802 resp.status = htobe32(LTTNG_VIEWER_CREATE_SESSION_OK);
7591bab1 1803 conn->viewer_session = viewer_session_create();
c3b7390b
JD
1804 if (!conn->viewer_session) {
1805 ERR("Allocation viewer session");
1806 resp.status = htobe32(LTTNG_VIEWER_CREATE_SESSION_ERR);
1807 goto send_reply;
1808 }
c3b7390b
JD
1809
1810send_reply:
1811 health_code_update();
1812 ret = send_response(conn->sock, &resp, sizeof(resp));
1813 if (ret < 0) {
1814 goto end;
1815 }
1816 health_code_update();
1817 ret = 0;
1818
1819end:
1820 return ret;
1821}
1822
d62023be
JD
1823/*
1824 * Detach a viewer session.
1825 *
1826 * Return 0 on success or else a negative value.
1827 */
1828static
1829int viewer_detach_session(struct relay_connection *conn)
1830{
1831 int ret;
1832 struct lttng_viewer_detach_session_response response;
1833 struct lttng_viewer_detach_session_request request;
1834 struct relay_session *session = NULL;
1835 uint64_t viewer_session_to_close;
1836
1837 DBG("Viewer detach session received");
1838
1839 assert(conn);
1840
1841 health_code_update();
1842
1843 /* Receive the request from the connected client. */
1844 ret = recv_request(conn->sock, &request, sizeof(request));
1845 if (ret < 0) {
1846 goto end;
1847 }
1848 viewer_session_to_close = be64toh(request.session_id);
1849
1850 if (!conn->viewer_session) {
1851 DBG("Client trying to detach before creating a live viewer session");
1852 response.status = htobe32(LTTNG_VIEWER_DETACH_SESSION_ERR);
1853 goto send_reply;
1854 }
1855
1856 health_code_update();
1857
1858 memset(&response, 0, sizeof(response));
1859 DBG("Detaching from session ID %" PRIu64, viewer_session_to_close);
1860
1861 session = session_get_by_id(be64toh(request.session_id));
1862 if (!session) {
1863 DBG("Relay session %" PRIu64 " not found",
c9d85d29 1864 (uint64_t) be64toh(request.session_id));
d62023be
JD
1865 response.status = htobe32(LTTNG_VIEWER_DETACH_SESSION_UNK);
1866 goto send_reply;
1867 }
1868
1869 ret = viewer_session_is_attached(conn->viewer_session, session);
1870 if (ret != 1) {
1871 DBG("Not attached to this session");
1872 response.status = htobe32(LTTNG_VIEWER_DETACH_SESSION_ERR);
1873 goto send_reply_put;
1874 }
1875
1876 viewer_session_close_one_session(conn->viewer_session, session);
1877 response.status = htobe32(LTTNG_VIEWER_DETACH_SESSION_OK);
1878 DBG("Session %" PRIu64 " detached.", viewer_session_to_close);
1879
1880send_reply_put:
1881 session_put(session);
1882
1883send_reply:
1884 health_code_update();
1885 ret = send_response(conn->sock, &response, sizeof(response));
1886 if (ret < 0) {
1887 goto end;
1888 }
1889 health_code_update();
1890 ret = 0;
1891
1892end:
1893 return ret;
1894}
c3b7390b 1895
d3e2ba59
JD
1896/*
1897 * live_relay_unknown_command: send -1 if received unknown command
1898 */
1899static
58eb9381 1900void live_relay_unknown_command(struct relay_connection *conn)
d3e2ba59
JD
1901{
1902 struct lttcomm_relayd_generic_reply reply;
d3e2ba59 1903
53efb85a 1904 memset(&reply, 0, sizeof(reply));
d3e2ba59 1905 reply.ret_code = htobe32(LTTNG_ERR_UNK);
58eb9381 1906 (void) send_response(conn->sock, &reply, sizeof(reply));
d3e2ba59
JD
1907}
1908
1909/*
1910 * Process the commands received on the control socket
1911 */
1912static
1913int process_control(struct lttng_viewer_cmd *recv_hdr,
58eb9381 1914 struct relay_connection *conn)
d3e2ba59
JD
1915{
1916 int ret = 0;
2f8f53af
DG
1917 uint32_t msg_value;
1918
2f8f53af
DG
1919 msg_value = be32toh(recv_hdr->cmd);
1920
1921 /*
1922 * Make sure we've done the version check before any command other then a
1923 * new client connection.
1924 */
c4e361a4 1925 if (msg_value != LTTNG_VIEWER_CONNECT && !conn->version_check_done) {
58eb9381 1926 ERR("Viewer conn value %" PRIu32 " before version check", msg_value);
2f8f53af
DG
1927 ret = -1;
1928 goto end;
1929 }
d3e2ba59 1930
2f8f53af 1931 switch (msg_value) {
c4e361a4 1932 case LTTNG_VIEWER_CONNECT:
58eb9381 1933 ret = viewer_connect(conn);
d3e2ba59 1934 break;
c4e361a4 1935 case LTTNG_VIEWER_LIST_SESSIONS:
58eb9381 1936 ret = viewer_list_sessions(conn);
d3e2ba59 1937 break;
c4e361a4 1938 case LTTNG_VIEWER_ATTACH_SESSION:
58eb9381 1939 ret = viewer_attach_session(conn);
d3e2ba59 1940 break;
c4e361a4 1941 case LTTNG_VIEWER_GET_NEXT_INDEX:
58eb9381 1942 ret = viewer_get_next_index(conn);
d3e2ba59 1943 break;
c4e361a4 1944 case LTTNG_VIEWER_GET_PACKET:
58eb9381 1945 ret = viewer_get_packet(conn);
d3e2ba59 1946 break;
c4e361a4 1947 case LTTNG_VIEWER_GET_METADATA:
58eb9381 1948 ret = viewer_get_metadata(conn);
d3e2ba59 1949 break;
c4e361a4 1950 case LTTNG_VIEWER_GET_NEW_STREAMS:
58eb9381 1951 ret = viewer_get_new_streams(conn);
80e8027a 1952 break;
c3b7390b
JD
1953 case LTTNG_VIEWER_CREATE_SESSION:
1954 ret = viewer_create_session(conn);
1955 break;
d62023be
JD
1956 case LTTNG_VIEWER_DETACH_SESSION:
1957 ret = viewer_detach_session(conn);
1958 break;
d3e2ba59 1959 default:
7591bab1
MD
1960 ERR("Received unknown viewer command (%u)",
1961 be32toh(recv_hdr->cmd));
58eb9381 1962 live_relay_unknown_command(conn);
d3e2ba59
JD
1963 ret = -1;
1964 goto end;
1965 }
1966
1967end:
1968 return ret;
1969}
1970
1971static
58eb9381 1972void cleanup_connection_pollfd(struct lttng_poll_event *events, int pollfd)
d3e2ba59
JD
1973{
1974 int ret;
1975
58eb9381 1976 (void) lttng_poll_del(events, pollfd);
d3e2ba59
JD
1977
1978 ret = close(pollfd);
1979 if (ret < 0) {
1980 ERR("Closing pollfd %d", pollfd);
1981 }
1982}
1983
d3e2ba59
JD
1984/*
1985 * This thread does the actual work
1986 */
1987static
1988void *thread_worker(void *data)
1989{
1990 int ret, err = -1;
1991 uint32_t nb_fd;
d3e2ba59 1992 struct lttng_poll_event events;
7591bab1 1993 struct lttng_ht *viewer_connections_ht;
d3e2ba59
JD
1994 struct lttng_ht_iter iter;
1995 struct lttng_viewer_cmd recv_hdr;
302d8906 1996 struct relay_connection *destroy_conn;
d3e2ba59
JD
1997
1998 DBG("[thread] Live viewer relay worker started");
1999
2000 rcu_register_thread();
2001
eea7556c
MD
2002 health_register(health_relayd, HEALTH_RELAYD_TYPE_LIVE_WORKER);
2003
9b5e0863
MD
2004 if (testpoint(relayd_thread_live_worker)) {
2005 goto error_testpoint;
2006 }
2007
d3e2ba59 2008 /* table of connections indexed on socket */
7591bab1
MD
2009 viewer_connections_ht = lttng_ht_new(0, LTTNG_HT_TYPE_ULONG);
2010 if (!viewer_connections_ht) {
2011 goto viewer_connections_ht_error;
d3e2ba59
JD
2012 }
2013
2014 ret = create_thread_poll_set(&events, 2);
2015 if (ret < 0) {
2016 goto error_poll_create;
2017 }
2018
58eb9381 2019 ret = lttng_poll_add(&events, live_conn_pipe[0], LPOLLIN | LPOLLRDHUP);
d3e2ba59
JD
2020 if (ret < 0) {
2021 goto error;
2022 }
2023
2024restart:
2025 while (1) {
2026 int i;
2027
eea7556c
MD
2028 health_code_update();
2029
d3e2ba59
JD
2030 /* Infinite blocking call, waiting for transmission */
2031 DBG3("Relayd live viewer worker thread polling...");
eea7556c 2032 health_poll_entry();
d3e2ba59 2033 ret = lttng_poll_wait(&events, -1);
eea7556c 2034 health_poll_exit();
d3e2ba59
JD
2035 if (ret < 0) {
2036 /*
2037 * Restart interrupted system call.
2038 */
2039 if (errno == EINTR) {
2040 goto restart;
2041 }
2042 goto error;
2043 }
2044
2045 nb_fd = ret;
2046
2047 /*
2048 * Process control. The control connection is prioritised so we don't
2049 * starve it with high throughput tracing data on the data
2050 * connection.
2051 */
2052 for (i = 0; i < nb_fd; i++) {
2053 /* Fetch once the poll data */
2054 uint32_t revents = LTTNG_POLL_GETEV(&events, i);
2055 int pollfd = LTTNG_POLL_GETFD(&events, i);
2056
eea7556c
MD
2057 health_code_update();
2058
fd20dac9
MD
2059 if (!revents) {
2060 /* No activity for this FD (poll implementation). */
2061 continue;
2062 }
2063
d3e2ba59 2064 /* Thread quit pipe has been closed. Killing thread. */
bcf4a440 2065 ret = check_thread_quit_pipe(pollfd, revents);
d3e2ba59
JD
2066 if (ret) {
2067 err = 0;
2068 goto exit;
2069 }
2070
7591bab1 2071 /* Inspect the relay conn pipe for new connection. */
58eb9381 2072 if (pollfd == live_conn_pipe[0]) {
03e43155 2073 if (revents & LPOLLIN) {
302d8906
JG
2074 struct relay_connection *conn;
2075
7591bab1
MD
2076 ret = lttng_read(live_conn_pipe[0],
2077 &conn, sizeof(conn));
d3e2ba59
JD
2078 if (ret < 0) {
2079 goto error;
2080 }
58eb9381
DG
2081 lttng_poll_add(&events, conn->sock->fd,
2082 LPOLLIN | LPOLLRDHUP);
7591bab1
MD
2083 connection_ht_add(viewer_connections_ht, conn);
2084 DBG("Connection socket %d added to poll", conn->sock->fd);
03e43155
MD
2085 } else if (revents & (LPOLLERR | LPOLLHUP | LPOLLRDHUP)) {
2086 ERR("Relay live pipe error");
2087 goto error;
2088 } else {
2089 ERR("Unexpected poll events %u for sock %d", revents, pollfd);
2090 goto error;
d3e2ba59 2091 }
58eb9381 2092 } else {
7591bab1 2093 /* Connection activity. */
302d8906
JG
2094 struct relay_connection *conn;
2095
7591bab1
MD
2096 conn = connection_get_by_sock(viewer_connections_ht, pollfd);
2097 if (!conn) {
2098 continue;
2099 }
58eb9381 2100
03e43155 2101 if (revents & LPOLLIN) {
58eb9381
DG
2102 ret = conn->sock->ops->recvmsg(conn->sock, &recv_hdr,
2103 sizeof(recv_hdr), 0);
d3e2ba59 2104 if (ret <= 0) {
7591bab1 2105 /* Connection closed. */
58eb9381 2106 cleanup_connection_pollfd(&events, pollfd);
7591bab1
MD
2107 /* Put "create" ownership reference. */
2108 connection_put(conn);
58eb9381 2109 DBG("Viewer control conn closed with %d", pollfd);
d3e2ba59 2110 } else {
58eb9381 2111 ret = process_control(&recv_hdr, conn);
d3e2ba59
JD
2112 if (ret < 0) {
2113 /* Clear the session on error. */
58eb9381 2114 cleanup_connection_pollfd(&events, pollfd);
7591bab1
MD
2115 /* Put "create" ownership reference. */
2116 connection_put(conn);
d3e2ba59
JD
2117 DBG("Viewer connection closed with %d", pollfd);
2118 }
2119 }
03e43155
MD
2120 } else if (revents & (LPOLLERR | LPOLLHUP | LPOLLRDHUP)) {
2121 cleanup_connection_pollfd(&events, pollfd);
2122 /* Put "create" ownership reference. */
2123 connection_put(conn);
2124 } else {
2125 ERR("Unexpected poll events %u for sock %d", revents, pollfd);
2126 connection_put(conn);
2127 goto error;
d3e2ba59 2128 }
7591bab1
MD
2129 /* Put local "get_by_sock" reference. */
2130 connection_put(conn);
d3e2ba59
JD
2131 }
2132 }
2133 }
2134
2135exit:
2136error:
2137 lttng_poll_clean(&events);
2138
58eb9381 2139 /* Cleanup reamaining connection object. */
d3e2ba59 2140 rcu_read_lock();
7591bab1 2141 cds_lfht_for_each_entry(viewer_connections_ht->ht, &iter.iter,
302d8906 2142 destroy_conn,
58eb9381 2143 sock_n.node) {
eea7556c 2144 health_code_update();
7591bab1 2145 connection_put(destroy_conn);
d3e2ba59
JD
2146 }
2147 rcu_read_unlock();
2148error_poll_create:
7591bab1
MD
2149 lttng_ht_destroy(viewer_connections_ht);
2150viewer_connections_ht_error:
58eb9381
DG
2151 /* Close relay conn pipes */
2152 utils_close_pipe(live_conn_pipe);
d3e2ba59
JD
2153 if (err) {
2154 DBG("Viewer worker thread exited with error");
2155 }
2156 DBG("Viewer worker thread cleanup complete");
9b5e0863 2157error_testpoint:
eea7556c
MD
2158 if (err) {
2159 health_error();
2160 ERR("Health error occurred in %s", __func__);
2161 }
2162 health_unregister(health_relayd);
b4aacfdc
MD
2163 if (lttng_relay_stop_threads()) {
2164 ERR("Error stopping threads");
178a0557 2165 }
d3e2ba59
JD
2166 rcu_unregister_thread();
2167 return NULL;
2168}
2169
2170/*
2171 * Create the relay command pipe to wake thread_manage_apps.
2172 * Closed in cleanup().
2173 */
58eb9381 2174static int create_conn_pipe(void)
d3e2ba59 2175{
178a0557
MD
2176 return utils_create_pipe_cloexec(live_conn_pipe);
2177}
d3e2ba59 2178
178a0557 2179int relayd_live_join(void)
d3e2ba59 2180{
178a0557 2181 int ret, retval = 0;
d3e2ba59
JD
2182 void *status;
2183
d3e2ba59 2184 ret = pthread_join(live_listener_thread, &status);
178a0557
MD
2185 if (ret) {
2186 errno = ret;
d3e2ba59 2187 PERROR("pthread_join live listener");
178a0557 2188 retval = -1;
d3e2ba59
JD
2189 }
2190
2191 ret = pthread_join(live_worker_thread, &status);
178a0557
MD
2192 if (ret) {
2193 errno = ret;
d3e2ba59 2194 PERROR("pthread_join live worker");
178a0557 2195 retval = -1;
d3e2ba59
JD
2196 }
2197
2198 ret = pthread_join(live_dispatcher_thread, &status);
178a0557
MD
2199 if (ret) {
2200 errno = ret;
d3e2ba59 2201 PERROR("pthread_join live dispatcher");
178a0557 2202 retval = -1;
d3e2ba59
JD
2203 }
2204
178a0557 2205 cleanup_relayd_live();
d3e2ba59 2206
178a0557 2207 return retval;
d3e2ba59
JD
2208}
2209
2210/*
2211 * main
2212 */
7591bab1 2213int relayd_live_create(struct lttng_uri *uri)
d3e2ba59 2214{
178a0557 2215 int ret = 0, retval = 0;
d3e2ba59
JD
2216 void *status;
2217 int is_root;
2218
178a0557
MD
2219 if (!uri) {
2220 retval = -1;
2221 goto exit_init_data;
2222 }
d3e2ba59
JD
2223 live_uri = uri;
2224
d3e2ba59
JD
2225 /* Check if daemon is UID = 0 */
2226 is_root = !getuid();
2227
2228 if (!is_root) {
2229 if (live_uri->port < 1024) {
2230 ERR("Need to be root to use ports < 1024");
178a0557
MD
2231 retval = -1;
2232 goto exit_init_data;
d3e2ba59
JD
2233 }
2234 }
2235
2236 /* Setup the thread apps communication pipe. */
178a0557
MD
2237 if (create_conn_pipe()) {
2238 retval = -1;
2239 goto exit_init_data;
d3e2ba59
JD
2240 }
2241
2242 /* Init relay command queue. */
8bdee6e2 2243 cds_wfcq_init(&viewer_conn_queue.head, &viewer_conn_queue.tail);
d3e2ba59
JD
2244
2245 /* Set up max poll set size */
25b397f9
MD
2246 if (lttng_poll_set_max_size()) {
2247 retval = -1;
2248 goto exit_init_data;
2249 }
d3e2ba59
JD
2250
2251 /* Setup the dispatcher thread */
1a1a34b4 2252 ret = pthread_create(&live_dispatcher_thread, default_pthread_attr(),
d3e2ba59 2253 thread_dispatcher, (void *) NULL);
178a0557
MD
2254 if (ret) {
2255 errno = ret;
d3e2ba59 2256 PERROR("pthread_create viewer dispatcher");
178a0557
MD
2257 retval = -1;
2258 goto exit_dispatcher_thread;
d3e2ba59
JD
2259 }
2260
2261 /* Setup the worker thread */
1a1a34b4 2262 ret = pthread_create(&live_worker_thread, default_pthread_attr(),
7591bab1 2263 thread_worker, NULL);
178a0557
MD
2264 if (ret) {
2265 errno = ret;
d3e2ba59 2266 PERROR("pthread_create viewer worker");
178a0557
MD
2267 retval = -1;
2268 goto exit_worker_thread;
d3e2ba59
JD
2269 }
2270
2271 /* Setup the listener thread */
1a1a34b4 2272 ret = pthread_create(&live_listener_thread, default_pthread_attr(),
d3e2ba59 2273 thread_listener, (void *) NULL);
178a0557
MD
2274 if (ret) {
2275 errno = ret;
d3e2ba59 2276 PERROR("pthread_create viewer listener");
178a0557
MD
2277 retval = -1;
2278 goto exit_listener_thread;
d3e2ba59
JD
2279 }
2280
178a0557
MD
2281 /*
2282 * All OK, started all threads.
2283 */
2284 return retval;
2285
9911d21b
JG
2286 /*
2287 * Join on the live_listener_thread should anything be added after
2288 * the live_listener thread's creation.
2289 */
d3e2ba59 2290
178a0557 2291exit_listener_thread:
d3e2ba59 2292
d3e2ba59 2293 ret = pthread_join(live_worker_thread, &status);
178a0557
MD
2294 if (ret) {
2295 errno = ret;
d3e2ba59 2296 PERROR("pthread_join live worker");
178a0557 2297 retval = -1;
d3e2ba59 2298 }
178a0557 2299exit_worker_thread:
d3e2ba59 2300
d3e2ba59 2301 ret = pthread_join(live_dispatcher_thread, &status);
178a0557
MD
2302 if (ret) {
2303 errno = ret;
d3e2ba59 2304 PERROR("pthread_join live dispatcher");
178a0557 2305 retval = -1;
d3e2ba59 2306 }
178a0557 2307exit_dispatcher_thread:
d3e2ba59 2308
178a0557
MD
2309exit_init_data:
2310 cleanup_relayd_live();
d3e2ba59 2311
178a0557 2312 return retval;
d3e2ba59 2313}
This page took 0.18174 seconds and 5 git commands to generate.