Backport: relayd: track the live_conn_pipe with the fd-tracker
[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>
07bc1945 56#include <common/fd-tracker/utils.h>
d3e2ba59
JD
57
58#include "cmd.h"
59#include "live.h"
60#include "lttng-relayd.h"
d3e2ba59 61#include "utils.h"
eea7556c 62#include "health-relayd.h"
9b5e0863 63#include "testpoint.h"
2f8f53af 64#include "viewer-stream.h"
2a174661
DG
65#include "stream.h"
66#include "session.h"
67#include "ctf-trace.h"
58eb9381 68#include "connection.h"
7591bab1
MD
69#include "viewer-session.h"
70
71#define SESSION_BUF_DEFAULT_COUNT 16
d3e2ba59
JD
72
73static struct lttng_uri *live_uri;
74
d3e2ba59
JD
75/*
76 * This pipe is used to inform the worker thread that a command is queued and
77 * ready to be processed.
78 */
58eb9381 79static int live_conn_pipe[2] = { -1, -1 };
d3e2ba59
JD
80
81/* Shared between threads */
82static int live_dispatch_thread_exit;
83
84static pthread_t live_listener_thread;
85static pthread_t live_dispatcher_thread;
86static pthread_t live_worker_thread;
87
88/*
89 * Relay command queue.
90 *
91 * The live_thread_listener and live_thread_dispatcher communicate with this
92 * queue.
93 */
58eb9381 94static struct relay_conn_queue viewer_conn_queue;
d3e2ba59
JD
95
96static uint64_t last_relay_viewer_session_id;
7591bab1
MD
97static pthread_mutex_t last_relay_viewer_session_id_lock =
98 PTHREAD_MUTEX_INITIALIZER;
d3e2ba59
JD
99
100/*
101 * Cleanup the daemon
102 */
103static
178a0557 104void cleanup_relayd_live(void)
d3e2ba59
JD
105{
106 DBG("Cleaning up");
107
d3e2ba59
JD
108 free(live_uri);
109}
110
2f8f53af
DG
111/*
112 * Receive a request buffer using a given socket, destination allocated buffer
113 * of length size.
114 *
115 * Return the size of the received message or else a negative value on error
116 * with errno being set by recvmsg() syscall.
117 */
118static
119ssize_t recv_request(struct lttcomm_sock *sock, void *buf, size_t size)
120{
121 ssize_t ret;
122
2f8f53af
DG
123 ret = sock->ops->recvmsg(sock, buf, size, 0);
124 if (ret < 0 || ret != size) {
125 if (ret == 0) {
126 /* Orderly shutdown. Not necessary to print an error. */
127 DBG("Socket %d did an orderly shutdown", sock->fd);
128 } else {
129 ERR("Relay failed to receive request.");
130 }
131 ret = -1;
132 }
133
134 return ret;
135}
136
137/*
138 * Send a response buffer using a given socket, source allocated buffer of
139 * length size.
140 *
141 * Return the size of the sent message or else a negative value on error with
142 * errno being set by sendmsg() syscall.
143 */
144static
145ssize_t send_response(struct lttcomm_sock *sock, void *buf, size_t size)
146{
147 ssize_t ret;
148
2f8f53af
DG
149 ret = sock->ops->sendmsg(sock, buf, size, 0);
150 if (ret < 0) {
151 ERR("Relayd failed to send response.");
152 }
153
154 return ret;
155}
156
157/*
f04a971b
JD
158 * Atomically check if new streams got added in one of the sessions attached
159 * and reset the flag to 0.
2f8f53af
DG
160 *
161 * Returns 1 if new streams got added, 0 if nothing changed, a negative value
162 * on error.
163 */
164static
f04a971b 165int check_new_streams(struct relay_connection *conn)
2f8f53af 166{
2f8f53af 167 struct relay_session *session;
f04a971b
JD
168 unsigned long current_val;
169 int ret = 0;
2f8f53af 170
f04a971b
JD
171 if (!conn->viewer_session) {
172 goto end;
173 }
7591bab1
MD
174 rcu_read_lock();
175 cds_list_for_each_entry_rcu(session,
176 &conn->viewer_session->session_list,
177 viewer_session_node) {
178 if (!session_get(session)) {
179 continue;
180 }
f04a971b
JD
181 current_val = uatomic_cmpxchg(&session->new_streams, 1, 0);
182 ret = current_val;
7591bab1 183 session_put(session);
f04a971b
JD
184 if (ret == 1) {
185 goto end;
186 }
2f8f53af 187 }
f04a971b 188end:
7591bab1 189 rcu_read_unlock();
2f8f53af
DG
190 return ret;
191}
192
193/*
194 * Send viewer streams to the given socket. The ignore_sent_flag indicates if
195 * this function should ignore the sent flag or not.
196 *
197 * Return 0 on success or else a negative value.
198 */
199static
200ssize_t send_viewer_streams(struct lttcomm_sock *sock,
201 struct relay_session *session, unsigned int ignore_sent_flag)
202{
203 ssize_t ret;
204 struct lttng_viewer_stream send_stream;
205 struct lttng_ht_iter iter;
206 struct relay_viewer_stream *vstream;
207
2f8f53af
DG
208 rcu_read_lock();
209
210 cds_lfht_for_each_entry(viewer_streams_ht->ht, &iter.iter, vstream,
211 stream_n.node) {
2a174661
DG
212 struct ctf_trace *ctf_trace;
213
2f8f53af
DG
214 health_code_update();
215
7591bab1
MD
216 if (!viewer_stream_get(vstream)) {
217 continue;
218 }
219
220 pthread_mutex_lock(&vstream->stream->lock);
2f8f53af 221 /* Ignore if not the same session. */
7591bab1 222 if (vstream->stream->trace->session->id != session->id ||
2f8f53af 223 (!ignore_sent_flag && vstream->sent_flag)) {
7591bab1
MD
224 pthread_mutex_unlock(&vstream->stream->lock);
225 viewer_stream_put(vstream);
2f8f53af
DG
226 continue;
227 }
228
7591bab1
MD
229 ctf_trace = vstream->stream->trace;
230 send_stream.id = htobe64(vstream->stream->stream_handle);
2a174661 231 send_stream.ctf_trace_id = htobe64(ctf_trace->id);
7591bab1
MD
232 send_stream.metadata_flag = htobe32(
233 vstream->stream->is_metadata);
f8f011fb
MD
234 if (lttng_strncpy(send_stream.path_name, vstream->path_name,
235 sizeof(send_stream.path_name))) {
236 pthread_mutex_unlock(&vstream->stream->lock);
237 viewer_stream_put(vstream);
238 ret = -1; /* Error. */
239 goto end_unlock;
240 }
241 if (lttng_strncpy(send_stream.channel_name,
242 vstream->channel_name,
243 sizeof(send_stream.channel_name))) {
244 pthread_mutex_unlock(&vstream->stream->lock);
245 viewer_stream_put(vstream);
246 ret = -1; /* Error. */
247 goto end_unlock;
248 }
2f8f53af 249
7591bab1
MD
250 DBG("Sending stream %" PRIu64 " to viewer",
251 vstream->stream->stream_handle);
252 vstream->sent_flag = 1;
253 pthread_mutex_unlock(&vstream->stream->lock);
254
2f8f53af 255 ret = send_response(sock, &send_stream, sizeof(send_stream));
7591bab1 256 viewer_stream_put(vstream);
2f8f53af
DG
257 if (ret < 0) {
258 goto end_unlock;
259 }
2f8f53af
DG
260 }
261
262 ret = 0;
263
264end_unlock:
265 rcu_read_unlock();
266 return ret;
267}
268
269/*
270 * Create every viewer stream possible for the given session with the seek
271 * type. Three counters *can* be return which are in order the total amount of
272 * viewer stream of the session, the number of unsent stream and the number of
273 * stream created. Those counters can be NULL and thus will be ignored.
274 *
275 * Return 0 on success or else a negative value.
276 */
277static
278int make_viewer_streams(struct relay_session *session,
279 enum lttng_viewer_seek seek_t, uint32_t *nb_total, uint32_t *nb_unsent,
bddf80e4 280 uint32_t *nb_created, bool *closed)
2f8f53af
DG
281{
282 int ret;
2f8f53af 283 struct lttng_ht_iter iter;
2a174661 284 struct ctf_trace *ctf_trace;
2f8f53af
DG
285
286 assert(session);
287
288 /*
7591bab1
MD
289 * Hold the session lock to ensure that we see either none or
290 * all initial streams for a session, but no intermediate state.
2f8f53af 291 */
7591bab1 292 pthread_mutex_lock(&session->lock);
2f8f53af 293
bddf80e4
MD
294 if (session->connection_closed) {
295 *closed = true;
296 }
297
2f8f53af 298 /*
7591bab1
MD
299 * Create viewer streams for relay streams that are ready to be
300 * used for a the given session id only.
2f8f53af 301 */
2a174661
DG
302 rcu_read_lock();
303 cds_lfht_for_each_entry(session->ctf_traces_ht->ht, &iter.iter, ctf_trace,
304 node.node) {
305 struct relay_stream *stream;
2f8f53af
DG
306
307 health_code_update();
308
7591bab1 309 if (!ctf_trace_get(ctf_trace)) {
2f8f53af
DG
310 continue;
311 }
312
7591bab1 313 cds_list_for_each_entry_rcu(stream, &ctf_trace->stream_list, stream_node) {
2a174661
DG
314 struct relay_viewer_stream *vstream;
315
7591bab1 316 if (!stream_get(stream)) {
2a174661
DG
317 continue;
318 }
7591bab1 319 /*
d812ecb9 320 * stream published is protected by the session lock.
7591bab1
MD
321 */
322 if (!stream->published) {
323 goto next;
324 }
325 vstream = viewer_stream_get_by_id(stream->stream_handle);
2f8f53af 326 if (!vstream) {
7591bab1 327 vstream = viewer_stream_create(stream, seek_t);
2a174661
DG
328 if (!vstream) {
329 ret = -1;
7591bab1
MD
330 ctf_trace_put(ctf_trace);
331 stream_put(stream);
2a174661
DG
332 goto error_unlock;
333 }
2a174661
DG
334
335 if (nb_created) {
336 /* Update number of created stream counter. */
337 (*nb_created)++;
338 }
2229a09c
MD
339 /*
340 * Ensure a self-reference is preserved even
341 * after we have put our local reference.
342 */
862d3a3b
MD
343 if (!viewer_stream_get(vstream)) {
344 ERR("Unable to get self-reference on viewer stream, logic error.");
345 abort();
346 }
7591bab1
MD
347 } else {
348 if (!vstream->sent_flag && nb_unsent) {
349 /* Update number of unsent stream counter. */
350 (*nb_unsent)++;
351 }
2f8f53af 352 }
2a174661
DG
353 /* Update number of total stream counter. */
354 if (nb_total) {
2229a09c
MD
355 if (stream->is_metadata) {
356 if (!stream->closed ||
357 stream->metadata_received > vstream->metadata_sent) {
358 (*nb_total)++;
359 }
360 } else {
361 if (!stream->closed ||
362 !(((int64_t) (stream->prev_seq - stream->last_net_seq_num)) >= 0)) {
363
364 (*nb_total)++;
365 }
366 }
2f8f53af 367 }
2229a09c
MD
368 /* Put local reference. */
369 viewer_stream_put(vstream);
7591bab1
MD
370 next:
371 stream_put(stream);
2f8f53af 372 }
7591bab1 373 ctf_trace_put(ctf_trace);
2f8f53af
DG
374 }
375
376 ret = 0;
377
378error_unlock:
2a174661 379 rcu_read_unlock();
7591bab1 380 pthread_mutex_unlock(&session->lock);
2f8f53af
DG
381 return ret;
382}
383
b4aacfdc 384int relayd_live_stop(void)
d3e2ba59 385{
b4aacfdc 386 /* Stop dispatch thread */
d3e2ba59 387 CMM_STORE_SHARED(live_dispatch_thread_exit, 1);
58eb9381 388 futex_nto1_wake(&viewer_conn_queue.futex);
b4aacfdc 389 return 0;
d3e2ba59
JD
390}
391
d3e2ba59
JD
392/*
393 * Create a poll set with O_CLOEXEC and add the thread quit pipe to the set.
394 */
395static
396int create_thread_poll_set(struct lttng_poll_event *events, int size)
397{
398 int ret;
399
400 if (events == NULL || size == 0) {
401 ret = -1;
402 goto error;
403 }
404
405 ret = lttng_poll_create(events, size, LTTNG_CLOEXEC);
406 if (ret < 0) {
407 goto error;
408 }
409
410 /* Add quit pipe */
bcf4a440 411 ret = lttng_poll_add(events, thread_quit_pipe[0], LPOLLIN | LPOLLERR);
d3e2ba59
JD
412 if (ret < 0) {
413 goto error;
414 }
415
416 return 0;
417
418error:
419 return ret;
420}
421
422/*
423 * Check if the thread quit pipe was triggered.
424 *
425 * Return 1 if it was triggered else 0;
426 */
427static
bcf4a440 428int check_thread_quit_pipe(int fd, uint32_t events)
d3e2ba59 429{
bcf4a440 430 if (fd == thread_quit_pipe[0] && (events & LPOLLIN)) {
d3e2ba59
JD
431 return 1;
432 }
433
434 return 0;
435}
436
437/*
438 * Create and init socket from uri.
439 */
440static
441struct lttcomm_sock *init_socket(struct lttng_uri *uri)
442{
443 int ret;
444 struct lttcomm_sock *sock = NULL;
445
446 sock = lttcomm_alloc_sock_from_uri(uri);
447 if (sock == NULL) {
448 ERR("Allocating socket");
449 goto error;
450 }
451
452 ret = lttcomm_create_sock(sock);
453 if (ret < 0) {
454 goto error;
455 }
456 DBG("Listening on sock %d for live", sock->fd);
457
458 ret = sock->ops->bind(sock);
459 if (ret < 0) {
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
68924dc1 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
68924dc1
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
7591bab1
MD
838 if (count >= buf_count) {
839 struct lttng_viewer_session *newbuf;
840 uint32_t new_buf_count = buf_count << 1;
eea7556c 841
7591bab1
MD
842 newbuf = realloc(send_session_buf,
843 new_buf_count * sizeof(*send_session_buf));
844 if (!newbuf) {
845 ret = -1;
5f10c6b1 846 break;
7591bab1
MD
847 }
848 send_session_buf = newbuf;
849 buf_count = new_buf_count;
6763619c 850 }
7591bab1 851 send_session = &send_session_buf[count];
bfc3fb17
MD
852 if (lttng_strncpy(send_session->session_name,
853 session->session_name,
854 sizeof(send_session->session_name))) {
855 ret = -1;
5f10c6b1 856 break;
bfc3fb17
MD
857 }
858 if (lttng_strncpy(send_session->hostname, session->hostname,
859 sizeof(send_session->hostname))) {
860 ret = -1;
5f10c6b1 861 break;
bfc3fb17 862 }
7591bab1
MD
863 send_session->id = htobe64(session->id);
864 send_session->live_timer = htobe32(session->live_timer);
865 if (session->viewer_attached) {
866 send_session->clients = htobe32(1);
867 } else {
868 send_session->clients = htobe32(0);
d227d5bd 869 }
7591bab1
MD
870 send_session->streams = htobe32(session->stream_count);
871 count++;
872 }
873 rcu_read_unlock();
5f10c6b1
JG
874 if (ret < 0) {
875 goto end_free;
876 }
d227d5bd 877
7591bab1 878 session_list.sessions_count = htobe32(count);
d227d5bd 879
7591bab1 880 health_code_update();
d227d5bd 881
7591bab1
MD
882 ret = send_response(conn->sock, &session_list, sizeof(session_list));
883 if (ret < 0) {
884 goto end_free;
d227d5bd 885 }
d227d5bd 886
7591bab1 887 health_code_update();
d227d5bd 888
7591bab1
MD
889 ret = send_response(conn->sock, send_session_buf,
890 count * sizeof(*send_session_buf));
891 if (ret < 0) {
892 goto end_free;
d227d5bd 893 }
7591bab1 894 health_code_update();
d227d5bd 895
7591bab1
MD
896 ret = 0;
897end_free:
898 free(send_session_buf);
899 return ret;
d227d5bd
JD
900}
901
80e8027a 902/*
7591bab1 903 * Send the viewer the list of current streams.
80e8027a
JD
904 */
905static
58eb9381 906int viewer_get_new_streams(struct relay_connection *conn)
80e8027a
JD
907{
908 int ret, send_streams = 0;
7591bab1 909 uint32_t nb_created = 0, nb_unsent = 0, nb_streams = 0, nb_total = 0;
80e8027a
JD
910 struct lttng_viewer_new_streams_request request;
911 struct lttng_viewer_new_streams_response response;
80e8027a 912 struct relay_session *session;
6763619c 913 uint64_t session_id;
bddf80e4 914 bool closed = false;
80e8027a 915
58eb9381 916 assert(conn);
80e8027a
JD
917
918 DBG("Get new streams received");
919
80e8027a
JD
920 health_code_update();
921
2f8f53af 922 /* Receive the request from the connected client. */
58eb9381 923 ret = recv_request(conn->sock, &request, sizeof(request));
2f8f53af 924 if (ret < 0) {
80e8027a
JD
925 goto error;
926 }
6763619c 927 session_id = be64toh(request.session_id);
80e8027a
JD
928
929 health_code_update();
930
53efb85a
MD
931 memset(&response, 0, sizeof(response));
932
7591bab1 933 session = session_get_by_id(session_id);
2f8f53af 934 if (!session) {
6763619c 935 DBG("Relay session %" PRIu64 " not found", session_id);
c4e361a4 936 response.status = htobe32(LTTNG_VIEWER_NEW_STREAMS_ERR);
80e8027a
JD
937 goto send_reply;
938 }
939
7591bab1 940 if (!viewer_session_is_attached(conn->viewer_session, session)) {
80e8027a 941 send_streams = 0;
c4e361a4 942 response.status = htobe32(LTTNG_VIEWER_NEW_STREAMS_ERR);
80e8027a
JD
943 goto send_reply;
944 }
945
6763619c
JD
946 send_streams = 1;
947 response.status = htobe32(LTTNG_VIEWER_NEW_STREAMS_OK);
948
7591bab1 949 ret = make_viewer_streams(session, LTTNG_VIEWER_SEEK_LAST, &nb_total, &nb_unsent,
bddf80e4 950 &nb_created, &closed);
2f8f53af 951 if (ret < 0) {
7591bab1 952 goto end_put_session;
2f8f53af
DG
953 }
954 /* Only send back the newly created streams with the unsent ones. */
955 nb_streams = nb_created + nb_unsent;
80e8027a
JD
956 response.streams_count = htobe32(nb_streams);
957
4479f682 958 /*
2229a09c
MD
959 * If the session is closed, HUP when there are no more streams
960 * with data.
4479f682 961 */
bddf80e4 962 if (closed && nb_total == 0) {
4479f682 963 send_streams = 0;
bddf80e4 964 response.streams_count = 0;
4479f682
JD
965 response.status = htobe32(LTTNG_VIEWER_NEW_STREAMS_HUP);
966 goto send_reply;
967 }
968
80e8027a
JD
969send_reply:
970 health_code_update();
58eb9381 971 ret = send_response(conn->sock, &response, sizeof(response));
80e8027a 972 if (ret < 0) {
7591bab1 973 goto end_put_session;
80e8027a
JD
974 }
975 health_code_update();
976
977 /*
7591bab1
MD
978 * Unknown or empty session, just return gracefully, the viewer
979 * knows what is happening.
80e8027a
JD
980 */
981 if (!send_streams || !nb_streams) {
982 ret = 0;
7591bab1 983 goto end_put_session;
80e8027a
JD
984 }
985
2f8f53af 986 /*
7591bab1
MD
987 * Send stream and *DON'T* ignore the sent flag so every viewer
988 * streams that were not sent from that point will be sent to
989 * the viewer.
2f8f53af 990 */
58eb9381 991 ret = send_viewer_streams(conn->sock, session, 0);
2f8f53af 992 if (ret < 0) {
7591bab1 993 goto end_put_session;
80e8027a
JD
994 }
995
7591bab1
MD
996end_put_session:
997 if (session) {
998 session_put(session);
999 }
80e8027a
JD
1000error:
1001 return ret;
1002}
1003
d3e2ba59
JD
1004/*
1005 * Send the viewer the list of current sessions.
1006 */
1007static
58eb9381 1008int viewer_attach_session(struct relay_connection *conn)
d3e2ba59 1009{
2f8f53af
DG
1010 int send_streams = 0;
1011 ssize_t ret;
80e8027a 1012 uint32_t nb_streams = 0;
2f8f53af 1013 enum lttng_viewer_seek seek_type;
d3e2ba59
JD
1014 struct lttng_viewer_attach_session_request request;
1015 struct lttng_viewer_attach_session_response response;
7591bab1 1016 struct relay_session *session = NULL;
bddf80e4 1017 bool closed = false;
d3e2ba59 1018
58eb9381 1019 assert(conn);
d3e2ba59 1020
eea7556c
MD
1021 health_code_update();
1022
2f8f53af 1023 /* Receive the request from the connected client. */
58eb9381 1024 ret = recv_request(conn->sock, &request, sizeof(request));
2f8f53af 1025 if (ret < 0) {
d3e2ba59
JD
1026 goto error;
1027 }
1028
eea7556c
MD
1029 health_code_update();
1030
53efb85a
MD
1031 memset(&response, 0, sizeof(response));
1032
c3b7390b
JD
1033 if (!conn->viewer_session) {
1034 DBG("Client trying to attach before creating a live viewer session");
1035 response.status = htobe32(LTTNG_VIEWER_ATTACH_NO_SESSION);
1036 goto send_reply;
1037 }
1038
7591bab1 1039 session = session_get_by_id(be64toh(request.session_id));
2f8f53af 1040 if (!session) {
d3e2ba59 1041 DBG("Relay session %" PRIu64 " not found",
f8fc08d2 1042 (uint64_t) be64toh(request.session_id));
c4e361a4 1043 response.status = htobe32(LTTNG_VIEWER_ATTACH_UNK);
d3e2ba59
JD
1044 goto send_reply;
1045 }
7591bab1 1046 DBG("Attach session ID %" PRIu64 " received",
f8fc08d2 1047 (uint64_t) be64toh(request.session_id));
d3e2ba59 1048
7591bab1 1049 if (session->live_timer == 0) {
d3e2ba59 1050 DBG("Not live session");
c4e361a4 1051 response.status = htobe32(LTTNG_VIEWER_ATTACH_NOT_LIVE);
d3e2ba59 1052 goto send_reply;
7591bab1
MD
1053 }
1054
1055 send_streams = 1;
1056 ret = viewer_session_attach(conn->viewer_session, session);
1057 if (ret) {
1058 DBG("Already a viewer attached");
1059 response.status = htobe32(LTTNG_VIEWER_ATTACH_ALREADY);
1060 goto send_reply;
d3e2ba59
JD
1061 }
1062
1063 switch (be32toh(request.seek)) {
c4e361a4
JD
1064 case LTTNG_VIEWER_SEEK_BEGINNING:
1065 case LTTNG_VIEWER_SEEK_LAST:
7591bab1 1066 response.status = htobe32(LTTNG_VIEWER_ATTACH_OK);
2f8f53af 1067 seek_type = be32toh(request.seek);
d3e2ba59
JD
1068 break;
1069 default:
1070 ERR("Wrong seek parameter");
c4e361a4 1071 response.status = htobe32(LTTNG_VIEWER_ATTACH_SEEK_ERR);
d3e2ba59
JD
1072 send_streams = 0;
1073 goto send_reply;
1074 }
1075
bddf80e4
MD
1076 ret = make_viewer_streams(session, seek_type, &nb_streams, NULL,
1077 NULL, &closed);
2f8f53af 1078 if (ret < 0) {
7591bab1 1079 goto end_put_session;
d3e2ba59 1080 }
2f8f53af 1081 response.streams_count = htobe32(nb_streams);
d3e2ba59 1082
bddf80e4
MD
1083 /*
1084 * If the session is closed when the viewer is attaching, it
1085 * means some of the streams may have been concurrently removed,
1086 * so we don't allow the viewer to attach, even if there are
1087 * streams available.
1088 */
1089 if (closed) {
1090 send_streams = 0;
1091 response.streams_count = 0;
1092 response.status = htobe32(LTTNG_VIEWER_NEW_STREAMS_HUP);
1093 goto send_reply;
1094 }
1095
d3e2ba59 1096send_reply:
eea7556c 1097 health_code_update();
58eb9381 1098 ret = send_response(conn->sock, &response, sizeof(response));
d3e2ba59 1099 if (ret < 0) {
7591bab1 1100 goto end_put_session;
d3e2ba59 1101 }
eea7556c 1102 health_code_update();
d3e2ba59
JD
1103
1104 /*
7591bab1
MD
1105 * Unknown or empty session, just return gracefully, the viewer
1106 * knows what is happening.
d3e2ba59 1107 */
157df586 1108 if (!send_streams || !nb_streams) {
d3e2ba59 1109 ret = 0;
7591bab1 1110 goto end_put_session;
d3e2ba59
JD
1111 }
1112
2f8f53af 1113 /* Send stream and ignore the sent flag. */
58eb9381 1114 ret = send_viewer_streams(conn->sock, session, 1);
2f8f53af 1115 if (ret < 0) {
7591bab1 1116 goto end_put_session;
d3e2ba59 1117 }
d3e2ba59 1118
7591bab1
MD
1119end_put_session:
1120 if (session) {
1121 session_put(session);
1122 }
4a9daf17
JD
1123error:
1124 return ret;
1125}
1126
878c34cf
DG
1127/*
1128 * Open the index file if needed for the given vstream.
1129 *
e0547b83
MD
1130 * If an index file is successfully opened, the vstream will set it as its
1131 * current index file.
878c34cf
DG
1132 *
1133 * Return 0 on success, a negative value on error (-ENOENT if not ready yet).
7591bab1
MD
1134 *
1135 * Called with rstream lock held.
878c34cf
DG
1136 */
1137static int try_open_index(struct relay_viewer_stream *vstream,
1138 struct relay_stream *rstream)
1139{
1140 int ret = 0;
1141
e0547b83 1142 if (vstream->index_file) {
878c34cf
DG
1143 goto end;
1144 }
1145
1146 /*
7591bab1 1147 * First time, we open the index file and at least one index is ready.
878c34cf 1148 */
a44ca2ca 1149 if (rstream->index_received_seqcount == 0) {
878c34cf
DG
1150 ret = -ENOENT;
1151 goto end;
1152 }
e0547b83
MD
1153 vstream->index_file = lttng_index_file_open(vstream->path_name,
1154 vstream->channel_name,
7591bab1
MD
1155 vstream->stream->tracefile_count,
1156 vstream->current_tracefile_id);
e0547b83
MD
1157 if (!vstream->index_file) {
1158 ret = -1;
878c34cf
DG
1159 }
1160
1161end:
1162 return ret;
1163}
1164
1165/*
7591bab1
MD
1166 * Check the status of the index for the given stream. This function
1167 * updates the index structure if needed and can put (close) the vstream
1168 * in the HUP situation.
1169 *
1170 * Return 0 means that we can proceed with the index. A value of 1 means
1171 * that the index has been updated and is ready to be sent to the
1172 * client. A negative value indicates an error that can't be handled.
878c34cf 1173 *
7591bab1 1174 * Called with rstream lock held.
878c34cf
DG
1175 */
1176static int check_index_status(struct relay_viewer_stream *vstream,
1177 struct relay_stream *rstream, struct ctf_trace *trace,
1178 struct lttng_viewer_index *index)
1179{
1180 int ret;
1181
7591bab1 1182 if (trace->session->connection_closed
a44ca2ca
MD
1183 && rstream->index_received_seqcount
1184 == vstream->index_sent_seqcount) {
7591bab1
MD
1185 /* Last index sent and session connection is closed. */
1186 index->status = htobe32(LTTNG_VIEWER_INDEX_HUP);
1187 goto hup;
1188 } else if (rstream->beacon_ts_end != -1ULL &&
a44ca2ca
MD
1189 rstream->index_received_seqcount
1190 == vstream->index_sent_seqcount) {
7591bab1
MD
1191 /*
1192 * We've received a synchronization beacon and the last index
1193 * available has been sent, the index for now is inactive.
1194 *
1195 * In this case, we have received a beacon which allows us to
1196 * inform the client of a time interval during which we can
1197 * guarantee that there are no events to read (and never will
1198 * be).
1199 */
1200 index->status = htobe32(LTTNG_VIEWER_INDEX_INACTIVE);
1201 index->timestamp_end = htobe64(rstream->beacon_ts_end);
1202 index->stream_id = htobe64(rstream->ctf_stream_id);
1203 goto index_ready;
a44ca2ca
MD
1204 } else if (rstream->index_received_seqcount
1205 == vstream->index_sent_seqcount) {
7591bab1 1206 /*
a44ca2ca
MD
1207 * This checks whether received == sent seqcount. In
1208 * this case, we have not received a beacon. Therefore,
1209 * we can only ask the client to retry later.
7591bab1
MD
1210 */
1211 index->status = htobe32(LTTNG_VIEWER_INDEX_RETRY);
1212 goto index_ready;
a44ca2ca
MD
1213 } else if (!tracefile_array_seq_in_file(rstream->tfa,
1214 vstream->current_tracefile_id,
1215 vstream->index_sent_seqcount)) {
7591bab1 1216 /*
a44ca2ca
MD
1217 * The next index we want to send cannot be read either
1218 * because we need to perform a rotation, or due to
1219 * the producer having overwritten its trace file.
7591bab1 1220 */
a44ca2ca 1221 DBG("Viewer stream %" PRIu64 " rotation",
7591bab1
MD
1222 vstream->stream->stream_handle);
1223 ret = viewer_stream_rotate(vstream);
1224 if (ret < 0) {
1225 goto end;
1226 } else if (ret == 1) {
1227 /* EOF across entire stream. */
1228 index->status = htobe32(LTTNG_VIEWER_INDEX_HUP);
1229 goto hup;
1230 }
7591bab1 1231 /*
a44ca2ca
MD
1232 * If we have been pushed due to overwrite, it
1233 * necessarily means there is data that can be read in
1234 * the stream. If we rotated because we reached the end
1235 * of a tracefile, it means the following tracefile
1236 * needs to contain at least one index, else we would
1237 * have already returned LTTNG_VIEWER_INDEX_RETRY to the
1238 * viewer. The updated index_sent_seqcount needs to
1239 * point to a readable index entry now.
1240 *
1241 * In the case where we "rotate" on a single file, we
1242 * can end up in a case where the requested index is
1243 * still unavailable.
7591bab1 1244 */
a44ca2ca
MD
1245 if (rstream->tracefile_count == 1 &&
1246 !tracefile_array_seq_in_file(
1247 rstream->tfa,
1248 vstream->current_tracefile_id,
1249 vstream->index_sent_seqcount)) {
1250 index->status = htobe32(LTTNG_VIEWER_INDEX_RETRY);
1251 goto index_ready;
878c34cf 1252 }
a44ca2ca
MD
1253 assert(tracefile_array_seq_in_file(rstream->tfa,
1254 vstream->current_tracefile_id,
1255 vstream->index_sent_seqcount));
878c34cf 1256 }
a44ca2ca
MD
1257 /* ret == 0 means successful so we continue. */
1258 ret = 0;
7591bab1 1259end:
878c34cf
DG
1260 return ret;
1261
1262hup:
7591bab1 1263 viewer_stream_put(vstream);
878c34cf
DG
1264index_ready:
1265 return 1;
1266}
1267
d3e2ba59
JD
1268/*
1269 * Send the next index for a stream.
1270 *
1271 * Return 0 on success or else a negative value.
1272 */
1273static
58eb9381 1274int viewer_get_next_index(struct relay_connection *conn)
d3e2ba59
JD
1275{
1276 int ret;
1277 struct lttng_viewer_get_next_index request_index;
1278 struct lttng_viewer_index viewer_index;
50adc264 1279 struct ctf_packet_index packet_index;
7591bab1
MD
1280 struct relay_viewer_stream *vstream = NULL;
1281 struct relay_stream *rstream = NULL;
1282 struct ctf_trace *ctf_trace = NULL;
1283 struct relay_viewer_stream *metadata_viewer_stream = NULL;
d3e2ba59 1284
58eb9381 1285 assert(conn);
d3e2ba59
JD
1286
1287 DBG("Viewer get next index");
1288
7591bab1 1289 memset(&viewer_index, 0, sizeof(viewer_index));
eea7556c 1290 health_code_update();
2f8f53af 1291
58eb9381 1292 ret = recv_request(conn->sock, &request_index, sizeof(request_index));
2f8f53af 1293 if (ret < 0) {
d3e2ba59
JD
1294 goto end;
1295 }
eea7556c 1296 health_code_update();
d3e2ba59 1297
7591bab1 1298 vstream = viewer_stream_get_by_id(be64toh(request_index.stream_id));
6763619c 1299 if (!vstream) {
a44ca2ca 1300 DBG("Client requested index of unknown stream id %" PRIu64,
f8fc08d2 1301 (uint64_t) be64toh(request_index.stream_id));
f1883937
MD
1302 viewer_index.status = htobe32(LTTNG_VIEWER_INDEX_ERR);
1303 goto send_reply;
2a174661
DG
1304 }
1305
7591bab1
MD
1306 /* Use back. ref. Protected by refcounts. */
1307 rstream = vstream->stream;
1308 ctf_trace = rstream->trace;
d3e2ba59 1309
7591bab1
MD
1310 /* metadata_viewer_stream may be NULL. */
1311 metadata_viewer_stream =
1312 ctf_trace_get_viewer_metadata_stream(ctf_trace);
2a174661 1313
7591bab1 1314 pthread_mutex_lock(&rstream->lock);
d3e2ba59
JD
1315
1316 /*
1317 * The viewer should not ask for index on metadata stream.
1318 */
7591bab1 1319 if (rstream->is_metadata) {
c4e361a4 1320 viewer_index.status = htobe32(LTTNG_VIEWER_INDEX_HUP);
d3e2ba59
JD
1321 goto send_reply;
1322 }
1323
878c34cf
DG
1324 /* Try to open an index if one is needed for that stream. */
1325 ret = try_open_index(vstream, rstream);
1326 if (ret < 0) {
0e6830aa 1327 if (ret == -ENOENT) {
d3e2ba59 1328 /*
7591bab1
MD
1329 * The index is created only when the first data
1330 * packet arrives, it might not be ready at the
1331 * beginning of the session
d3e2ba59 1332 */
c4e361a4 1333 viewer_index.status = htobe32(LTTNG_VIEWER_INDEX_RETRY);
878c34cf
DG
1334 } else {
1335 /* Unhandled error. */
c4e361a4 1336 viewer_index.status = htobe32(LTTNG_VIEWER_INDEX_ERR);
d3e2ba59 1337 }
878c34cf 1338 goto send_reply;
d3e2ba59
JD
1339 }
1340
878c34cf 1341 ret = check_index_status(vstream, rstream, ctf_trace, &viewer_index);
878c34cf 1342 if (ret < 0) {
7591bab1 1343 goto error_put;
878c34cf
DG
1344 } else if (ret == 1) {
1345 /*
7591bab1
MD
1346 * We have no index to send and check_index_status has populated
1347 * viewer_index's status.
878c34cf 1348 */
d3e2ba59
JD
1349 goto send_reply;
1350 }
7591bab1 1351 /* At this point, ret is 0 thus we will be able to read the index. */
878c34cf 1352 assert(!ret);
d3e2ba59 1353
7591bab1
MD
1354 /*
1355 * vstream->stream_fd may be NULL if it has been closed by
1356 * tracefile rotation, or if we are at the beginning of the
1357 * stream. We open the data stream file here to protect against
1358 * overwrite caused by tracefile rotation (in association with
1359 * unlink performed before overwrite).
1360 */
1361 if (!vstream->stream_fd) {
1362 char fullpath[PATH_MAX];
1363
1364 if (vstream->stream->tracefile_count > 0) {
1365 ret = snprintf(fullpath, PATH_MAX, "%s/%s_%" PRIu64,
1366 vstream->path_name,
1367 vstream->channel_name,
1368 vstream->current_tracefile_id);
1369 } else {
1370 ret = snprintf(fullpath, PATH_MAX, "%s/%s",
1371 vstream->path_name,
1372 vstream->channel_name);
1373 }
1374 if (ret < 0) {
1375 goto error_put;
1376 }
1377 ret = open(fullpath, O_RDONLY);
1378 if (ret < 0) {
1379 PERROR("Relay opening trace file");
1380 goto error_put;
1381 }
1382 vstream->stream_fd = stream_fd_create(ret);
1383 if (!vstream->stream_fd) {
1384 if (close(ret)) {
1385 PERROR("close");
1386 }
1387 goto error_put;
1388 }
d3e2ba59
JD
1389 }
1390
f04a971b 1391 ret = check_new_streams(conn);
4a9daf17 1392 if (ret < 0) {
7591bab1
MD
1393 viewer_index.status = htobe32(LTTNG_VIEWER_INDEX_ERR);
1394 goto send_reply;
4a9daf17
JD
1395 } else if (ret == 1) {
1396 viewer_index.flags |= LTTNG_VIEWER_FLAG_NEW_STREAM;
1397 }
1398
e0547b83
MD
1399 ret = lttng_index_file_read(vstream->index_file, &packet_index);
1400 if (ret) {
1401 ERR("Relay error reading index file %d",
1402 vstream->index_file->fd);
7591bab1 1403 viewer_index.status = htobe32(LTTNG_VIEWER_INDEX_ERR);
6b6b9a5a 1404 goto send_reply;
d3e2ba59 1405 } else {
c4e361a4 1406 viewer_index.status = htobe32(LTTNG_VIEWER_INDEX_OK);
a44ca2ca 1407 vstream->index_sent_seqcount++;
d3e2ba59
JD
1408 }
1409
1410 /*
1411 * Indexes are stored in big endian, no need to switch before sending.
1412 */
7591bab1
MD
1413 DBG("Sending viewer index for stream %" PRIu64 " offset %" PRIu64,
1414 rstream->stream_handle,
f8fc08d2 1415 (uint64_t) be64toh(packet_index.offset));
d3e2ba59
JD
1416 viewer_index.offset = packet_index.offset;
1417 viewer_index.packet_size = packet_index.packet_size;
1418 viewer_index.content_size = packet_index.content_size;
1419 viewer_index.timestamp_begin = packet_index.timestamp_begin;
1420 viewer_index.timestamp_end = packet_index.timestamp_end;
1421 viewer_index.events_discarded = packet_index.events_discarded;
1422 viewer_index.stream_id = packet_index.stream_id;
1423
1424send_reply:
f1883937
MD
1425 if (rstream) {
1426 pthread_mutex_unlock(&rstream->lock);
1427 }
7591bab1
MD
1428
1429 if (metadata_viewer_stream) {
1430 pthread_mutex_lock(&metadata_viewer_stream->stream->lock);
1431 DBG("get next index metadata check: recv %" PRIu64
1432 " sent %" PRIu64,
1433 metadata_viewer_stream->stream->metadata_received,
1434 metadata_viewer_stream->metadata_sent);
1435 if (!metadata_viewer_stream->stream->metadata_received ||
1436 metadata_viewer_stream->stream->metadata_received >
1437 metadata_viewer_stream->metadata_sent) {
1438 viewer_index.flags |= LTTNG_VIEWER_FLAG_NEW_METADATA;
1439 }
1440 pthread_mutex_unlock(&metadata_viewer_stream->stream->lock);
1441 }
1442
d3e2ba59 1443 viewer_index.flags = htobe32(viewer_index.flags);
eea7556c 1444 health_code_update();
2f8f53af 1445
58eb9381 1446 ret = send_response(conn->sock, &viewer_index, sizeof(viewer_index));
d3e2ba59 1447 if (ret < 0) {
7591bab1 1448 goto end;
d3e2ba59 1449 }
eea7556c 1450 health_code_update();
d3e2ba59 1451
9237e6a1
MD
1452 if (vstream) {
1453 DBG("Index %" PRIu64 " for stream %" PRIu64 " sent",
a44ca2ca 1454 vstream->index_sent_seqcount,
9237e6a1
MD
1455 vstream->stream->stream_handle);
1456 }
d3e2ba59 1457end:
7591bab1
MD
1458 if (metadata_viewer_stream) {
1459 viewer_stream_put(metadata_viewer_stream);
1460 }
1461 if (vstream) {
1462 viewer_stream_put(vstream);
1463 }
1464 return ret;
1465
1466error_put:
1467 pthread_mutex_unlock(&rstream->lock);
1468 if (metadata_viewer_stream) {
1469 viewer_stream_put(metadata_viewer_stream);
1470 }
1471 viewer_stream_put(vstream);
d3e2ba59
JD
1472 return ret;
1473}
1474
1475/*
1476 * Send the next index for a stream
1477 *
1478 * Return 0 on success or else a negative value.
1479 */
1480static
58eb9381 1481int viewer_get_packet(struct relay_connection *conn)
d3e2ba59 1482{
4e8db96d 1483 int ret;
b61e1dca 1484 off_t lseek_ret;
6d1bce6b 1485 char *reply = NULL;
d3e2ba59 1486 struct lttng_viewer_get_packet get_packet_info;
6d1bce6b 1487 struct lttng_viewer_trace_packet reply_header;
7591bab1 1488 struct relay_viewer_stream *vstream = NULL;
6d1bce6b 1489 uint32_t reply_size = sizeof(reply_header);
4e8db96d
JR
1490 uint32_t packet_data_len = 0;
1491 ssize_t read_len;
d3e2ba59
JD
1492
1493 DBG2("Relay get data packet");
1494
eea7556c 1495 health_code_update();
2f8f53af 1496
7591bab1
MD
1497 ret = recv_request(conn->sock, &get_packet_info,
1498 sizeof(get_packet_info));
2f8f53af 1499 if (ret < 0) {
d3e2ba59
JD
1500 goto end;
1501 }
eea7556c 1502 health_code_update();
d3e2ba59 1503
0233a6a5 1504 /* From this point on, the error label can be reached. */
6d1bce6b 1505 memset(&reply_header, 0, sizeof(reply_header));
0233a6a5 1506
7591bab1
MD
1507 vstream = viewer_stream_get_by_id(be64toh(get_packet_info.stream_id));
1508 if (!vstream) {
a44ca2ca 1509 DBG("Client requested packet of unknown stream id %" PRIu64,
f8fc08d2 1510 (uint64_t) be64toh(get_packet_info.stream_id));
6d1bce6b
JG
1511 reply_header.status = htobe32(LTTNG_VIEWER_GET_PACKET_ERR);
1512 goto send_reply_nolock;
4e8db96d
JR
1513 } else {
1514 packet_data_len = be32toh(get_packet_info.len);
6d1bce6b 1515 reply_size += packet_data_len;
d3e2ba59 1516 }
2a174661 1517
6d1bce6b
JG
1518 reply = zmalloc(reply_size);
1519 if (!reply) {
1520 PERROR("packet reply zmalloc");
1521 reply_size = sizeof(reply_header);
d3e2ba59
JD
1522 goto error;
1523 }
1524
4e8db96d 1525 pthread_mutex_lock(&vstream->stream->lock);
b61e1dca 1526 lseek_ret = lseek(vstream->stream_fd->fd, be64toh(get_packet_info.offset),
7591bab1 1527 SEEK_SET);
b61e1dca 1528 if (lseek_ret < 0) {
7591bab1 1529 PERROR("lseek fd %d to offset %" PRIu64, vstream->stream_fd->fd,
f8fc08d2 1530 (uint64_t) be64toh(get_packet_info.offset));
7591bab1 1531 goto error;
d3e2ba59 1532 }
4e8db96d 1533 read_len = lttng_read(vstream->stream_fd->fd,
6d1bce6b 1534 reply + sizeof(reply_header),
4e8db96d
JR
1535 packet_data_len);
1536 if (read_len < packet_data_len) {
7591bab1
MD
1537 PERROR("Relay reading trace file, fd: %d, offset: %" PRIu64,
1538 vstream->stream_fd->fd,
f8fc08d2 1539 (uint64_t) be64toh(get_packet_info.offset));
7591bab1 1540 goto error;
d3e2ba59 1541 }
6d1bce6b
JG
1542 reply_header.status = htobe32(LTTNG_VIEWER_GET_PACKET_OK);
1543 reply_header.len = htobe32(packet_data_len);
d3e2ba59
JD
1544 goto send_reply;
1545
1546error:
6d1bce6b 1547 reply_header.status = htobe32(LTTNG_VIEWER_GET_PACKET_ERR);
d3e2ba59
JD
1548
1549send_reply:
7591bab1
MD
1550 if (vstream) {
1551 pthread_mutex_unlock(&vstream->stream->lock);
1552 }
1553send_reply_nolock:
eea7556c
MD
1554
1555 health_code_update();
2f8f53af 1556
6d1bce6b
JG
1557 if (reply) {
1558 memcpy(reply, &reply_header, sizeof(reply_header));
1559 ret = send_response(conn->sock, reply, reply_size);
1560 } else {
1561 /* No reply to send. */
1562 ret = send_response(conn->sock, &reply_header,
1563 reply_size);
1564 }
1565
4e8db96d 1566 health_code_update();
d3e2ba59 1567 if (ret < 0) {
4e8db96d 1568 PERROR("sendmsg of packet data failed");
7591bab1 1569 goto end_free;
d3e2ba59 1570 }
d3e2ba59 1571
6d1bce6b 1572 DBG("Sent %u bytes for stream %" PRIu64, reply_size,
f8fc08d2 1573 (uint64_t) be64toh(get_packet_info.stream_id));
d3e2ba59 1574
7591bab1 1575end_free:
6d1bce6b 1576 free(reply);
d3e2ba59 1577end:
7591bab1
MD
1578 if (vstream) {
1579 viewer_stream_put(vstream);
1580 }
d3e2ba59
JD
1581 return ret;
1582}
1583
1584/*
1585 * Send the session's metadata
1586 *
1587 * Return 0 on success else a negative value.
1588 */
1589static
58eb9381 1590int viewer_get_metadata(struct relay_connection *conn)
d3e2ba59
JD
1591{
1592 int ret = 0;
1593 ssize_t read_len;
1594 uint64_t len = 0;
1595 char *data = NULL;
1596 struct lttng_viewer_get_metadata request;
1597 struct lttng_viewer_metadata_packet reply;
7591bab1 1598 struct relay_viewer_stream *vstream = NULL;
d3e2ba59 1599
58eb9381 1600 assert(conn);
d3e2ba59
JD
1601
1602 DBG("Relay get metadata");
1603
eea7556c 1604 health_code_update();
2f8f53af 1605
58eb9381 1606 ret = recv_request(conn->sock, &request, sizeof(request));
2f8f53af 1607 if (ret < 0) {
d3e2ba59
JD
1608 goto end;
1609 }
eea7556c 1610 health_code_update();
d3e2ba59 1611
53efb85a
MD
1612 memset(&reply, 0, sizeof(reply));
1613
7591bab1
MD
1614 vstream = viewer_stream_get_by_id(be64toh(request.stream_id));
1615 if (!vstream) {
3b463131
MD
1616 /*
1617 * The metadata stream can be closed by a CLOSE command
1618 * just before we attach. It can also be closed by
1619 * per-pid tracing during tracing. Therefore, it is
1620 * possible that we cannot find this viewer stream.
1621 * Reply back to the client with an error if we cannot
1622 * find it.
1623 */
a44ca2ca 1624 DBG("Client requested metadata of unknown stream id %" PRIu64,
f8fc08d2 1625 (uint64_t) be64toh(request.stream_id));
3b463131 1626 reply.status = htobe32(LTTNG_VIEWER_METADATA_ERR);
7591bab1 1627 goto send_reply;
d3e2ba59 1628 }
7591bab1
MD
1629 pthread_mutex_lock(&vstream->stream->lock);
1630 if (!vstream->stream->is_metadata) {
1631 ERR("Invalid metadata stream");
6763619c
JD
1632 goto error;
1633 }
1634
7591bab1 1635 assert(vstream->metadata_sent <= vstream->stream->metadata_received);
2a174661 1636
7591bab1 1637 len = vstream->stream->metadata_received - vstream->metadata_sent;
d3e2ba59 1638 if (len == 0) {
c4e361a4 1639 reply.status = htobe32(LTTNG_VIEWER_NO_NEW_METADATA);
d3e2ba59
JD
1640 goto send_reply;
1641 }
1642
1643 /* first time, we open the metadata file */
7591bab1 1644 if (!vstream->stream_fd) {
d3e2ba59
JD
1645 char fullpath[PATH_MAX];
1646
7591bab1
MD
1647 ret = snprintf(fullpath, PATH_MAX, "%s/%s", vstream->path_name,
1648 vstream->channel_name);
d3e2ba59
JD
1649 if (ret < 0) {
1650 goto error;
1651 }
1652 ret = open(fullpath, O_RDONLY);
1653 if (ret < 0) {
1654 PERROR("Relay opening metadata file");
1655 goto error;
1656 }
7591bab1
MD
1657 vstream->stream_fd = stream_fd_create(ret);
1658 if (!vstream->stream_fd) {
1659 if (close(ret)) {
1660 PERROR("close");
1661 }
1662 goto error;
1663 }
d3e2ba59
JD
1664 }
1665
1666 reply.len = htobe64(len);
1667 data = zmalloc(len);
1668 if (!data) {
1669 PERROR("viewer metadata zmalloc");
1670 goto error;
1671 }
1672
7591bab1 1673 read_len = lttng_read(vstream->stream_fd->fd, data, len);
6cd525e8 1674 if (read_len < len) {
d3e2ba59
JD
1675 PERROR("Relay reading metadata file");
1676 goto error;
1677 }
7591bab1
MD
1678 vstream->metadata_sent += read_len;
1679 if (vstream->metadata_sent == vstream->stream->metadata_received
1680 && vstream->stream->closed) {
1681 /* Release ownership for the viewer metadata stream. */
1682 viewer_stream_put(vstream);
1683 }
1684
c4e361a4 1685 reply.status = htobe32(LTTNG_VIEWER_METADATA_OK);
7591bab1 1686
d3e2ba59
JD
1687 goto send_reply;
1688
1689error:
c4e361a4 1690 reply.status = htobe32(LTTNG_VIEWER_METADATA_ERR);
d3e2ba59
JD
1691
1692send_reply:
eea7556c 1693 health_code_update();
7591bab1
MD
1694 if (vstream) {
1695 pthread_mutex_unlock(&vstream->stream->lock);
1696 }
58eb9381 1697 ret = send_response(conn->sock, &reply, sizeof(reply));
d3e2ba59 1698 if (ret < 0) {
7591bab1 1699 goto end_free;
d3e2ba59 1700 }
eea7556c 1701 health_code_update();
d3e2ba59
JD
1702
1703 if (len > 0) {
58eb9381 1704 ret = send_response(conn->sock, data, len);
d3e2ba59 1705 if (ret < 0) {
7591bab1 1706 goto end_free;
d3e2ba59
JD
1707 }
1708 }
1709
1710 DBG("Sent %" PRIu64 " bytes of metadata for stream %" PRIu64, len,
f8fc08d2 1711 (uint64_t) be64toh(request.stream_id));
d3e2ba59
JD
1712
1713 DBG("Metadata sent");
1714
7591bab1 1715end_free:
d3e2ba59 1716 free(data);
d3e2ba59 1717end:
7591bab1
MD
1718 if (vstream) {
1719 viewer_stream_put(vstream);
1720 }
d3e2ba59
JD
1721 return ret;
1722}
1723
c3b7390b
JD
1724/*
1725 * Create a viewer session.
1726 *
1727 * Return 0 on success or else a negative value.
1728 */
1729static
1730int viewer_create_session(struct relay_connection *conn)
1731{
1732 int ret;
1733 struct lttng_viewer_create_session_response resp;
1734
1735 DBG("Viewer create session received");
1736
53efb85a 1737 memset(&resp, 0, sizeof(resp));
c3b7390b 1738 resp.status = htobe32(LTTNG_VIEWER_CREATE_SESSION_OK);
7591bab1 1739 conn->viewer_session = viewer_session_create();
c3b7390b
JD
1740 if (!conn->viewer_session) {
1741 ERR("Allocation viewer session");
1742 resp.status = htobe32(LTTNG_VIEWER_CREATE_SESSION_ERR);
1743 goto send_reply;
1744 }
c3b7390b
JD
1745
1746send_reply:
1747 health_code_update();
1748 ret = send_response(conn->sock, &resp, sizeof(resp));
1749 if (ret < 0) {
1750 goto end;
1751 }
1752 health_code_update();
1753 ret = 0;
1754
1755end:
1756 return ret;
1757}
1758
d62023be
JD
1759/*
1760 * Detach a viewer session.
1761 *
1762 * Return 0 on success or else a negative value.
1763 */
1764static
1765int viewer_detach_session(struct relay_connection *conn)
1766{
1767 int ret;
1768 struct lttng_viewer_detach_session_response response;
1769 struct lttng_viewer_detach_session_request request;
1770 struct relay_session *session = NULL;
1771 uint64_t viewer_session_to_close;
1772
1773 DBG("Viewer detach session received");
1774
1775 assert(conn);
1776
1777 health_code_update();
1778
1779 /* Receive the request from the connected client. */
1780 ret = recv_request(conn->sock, &request, sizeof(request));
1781 if (ret < 0) {
1782 goto end;
1783 }
1784 viewer_session_to_close = be64toh(request.session_id);
1785
1786 if (!conn->viewer_session) {
1787 DBG("Client trying to detach before creating a live viewer session");
1788 response.status = htobe32(LTTNG_VIEWER_DETACH_SESSION_ERR);
1789 goto send_reply;
1790 }
1791
1792 health_code_update();
1793
1794 memset(&response, 0, sizeof(response));
1795 DBG("Detaching from session ID %" PRIu64, viewer_session_to_close);
1796
1797 session = session_get_by_id(be64toh(request.session_id));
1798 if (!session) {
1799 DBG("Relay session %" PRIu64 " not found",
f8fc08d2 1800 (uint64_t) be64toh(request.session_id));
d62023be
JD
1801 response.status = htobe32(LTTNG_VIEWER_DETACH_SESSION_UNK);
1802 goto send_reply;
1803 }
1804
1805 ret = viewer_session_is_attached(conn->viewer_session, session);
1806 if (ret != 1) {
1807 DBG("Not attached to this session");
1808 response.status = htobe32(LTTNG_VIEWER_DETACH_SESSION_ERR);
1809 goto send_reply_put;
1810 }
1811
1812 viewer_session_close_one_session(conn->viewer_session, session);
1813 response.status = htobe32(LTTNG_VIEWER_DETACH_SESSION_OK);
1814 DBG("Session %" PRIu64 " detached.", viewer_session_to_close);
1815
1816send_reply_put:
1817 session_put(session);
1818
1819send_reply:
1820 health_code_update();
1821 ret = send_response(conn->sock, &response, sizeof(response));
1822 if (ret < 0) {
1823 goto end;
1824 }
1825 health_code_update();
1826 ret = 0;
1827
1828end:
1829 return ret;
1830}
c3b7390b 1831
d3e2ba59
JD
1832/*
1833 * live_relay_unknown_command: send -1 if received unknown command
1834 */
1835static
58eb9381 1836void live_relay_unknown_command(struct relay_connection *conn)
d3e2ba59
JD
1837{
1838 struct lttcomm_relayd_generic_reply reply;
d3e2ba59 1839
53efb85a 1840 memset(&reply, 0, sizeof(reply));
d3e2ba59 1841 reply.ret_code = htobe32(LTTNG_ERR_UNK);
58eb9381 1842 (void) send_response(conn->sock, &reply, sizeof(reply));
d3e2ba59
JD
1843}
1844
1845/*
1846 * Process the commands received on the control socket
1847 */
1848static
1849int process_control(struct lttng_viewer_cmd *recv_hdr,
58eb9381 1850 struct relay_connection *conn)
d3e2ba59
JD
1851{
1852 int ret = 0;
2f8f53af
DG
1853 uint32_t msg_value;
1854
2f8f53af
DG
1855 msg_value = be32toh(recv_hdr->cmd);
1856
1857 /*
1858 * Make sure we've done the version check before any command other then a
1859 * new client connection.
1860 */
c4e361a4 1861 if (msg_value != LTTNG_VIEWER_CONNECT && !conn->version_check_done) {
58eb9381 1862 ERR("Viewer conn value %" PRIu32 " before version check", msg_value);
2f8f53af
DG
1863 ret = -1;
1864 goto end;
1865 }
d3e2ba59 1866
2f8f53af 1867 switch (msg_value) {
c4e361a4 1868 case LTTNG_VIEWER_CONNECT:
58eb9381 1869 ret = viewer_connect(conn);
d3e2ba59 1870 break;
c4e361a4 1871 case LTTNG_VIEWER_LIST_SESSIONS:
58eb9381 1872 ret = viewer_list_sessions(conn);
d3e2ba59 1873 break;
c4e361a4 1874 case LTTNG_VIEWER_ATTACH_SESSION:
58eb9381 1875 ret = viewer_attach_session(conn);
d3e2ba59 1876 break;
c4e361a4 1877 case LTTNG_VIEWER_GET_NEXT_INDEX:
58eb9381 1878 ret = viewer_get_next_index(conn);
d3e2ba59 1879 break;
c4e361a4 1880 case LTTNG_VIEWER_GET_PACKET:
58eb9381 1881 ret = viewer_get_packet(conn);
d3e2ba59 1882 break;
c4e361a4 1883 case LTTNG_VIEWER_GET_METADATA:
58eb9381 1884 ret = viewer_get_metadata(conn);
d3e2ba59 1885 break;
c4e361a4 1886 case LTTNG_VIEWER_GET_NEW_STREAMS:
58eb9381 1887 ret = viewer_get_new_streams(conn);
80e8027a 1888 break;
c3b7390b
JD
1889 case LTTNG_VIEWER_CREATE_SESSION:
1890 ret = viewer_create_session(conn);
1891 break;
d62023be
JD
1892 case LTTNG_VIEWER_DETACH_SESSION:
1893 ret = viewer_detach_session(conn);
1894 break;
d3e2ba59 1895 default:
7591bab1
MD
1896 ERR("Received unknown viewer command (%u)",
1897 be32toh(recv_hdr->cmd));
58eb9381 1898 live_relay_unknown_command(conn);
d3e2ba59
JD
1899 ret = -1;
1900 goto end;
1901 }
1902
1903end:
1904 return ret;
1905}
1906
1907static
58eb9381 1908void cleanup_connection_pollfd(struct lttng_poll_event *events, int pollfd)
d3e2ba59
JD
1909{
1910 int ret;
1911
58eb9381 1912 (void) lttng_poll_del(events, pollfd);
d3e2ba59
JD
1913
1914 ret = close(pollfd);
1915 if (ret < 0) {
1916 ERR("Closing pollfd %d", pollfd);
1917 }
1918}
1919
d3e2ba59
JD
1920/*
1921 * This thread does the actual work
1922 */
1923static
1924void *thread_worker(void *data)
1925{
1926 int ret, err = -1;
1927 uint32_t nb_fd;
d3e2ba59 1928 struct lttng_poll_event events;
7591bab1 1929 struct lttng_ht *viewer_connections_ht;
d3e2ba59
JD
1930 struct lttng_ht_iter iter;
1931 struct lttng_viewer_cmd recv_hdr;
302d8906 1932 struct relay_connection *destroy_conn;
d3e2ba59
JD
1933
1934 DBG("[thread] Live viewer relay worker started");
1935
1936 rcu_register_thread();
1937
eea7556c
MD
1938 health_register(health_relayd, HEALTH_RELAYD_TYPE_LIVE_WORKER);
1939
9b5e0863
MD
1940 if (testpoint(relayd_thread_live_worker)) {
1941 goto error_testpoint;
1942 }
1943
d3e2ba59 1944 /* table of connections indexed on socket */
7591bab1
MD
1945 viewer_connections_ht = lttng_ht_new(0, LTTNG_HT_TYPE_ULONG);
1946 if (!viewer_connections_ht) {
1947 goto viewer_connections_ht_error;
d3e2ba59
JD
1948 }
1949
1950 ret = create_thread_poll_set(&events, 2);
1951 if (ret < 0) {
1952 goto error_poll_create;
1953 }
1954
58eb9381 1955 ret = lttng_poll_add(&events, live_conn_pipe[0], LPOLLIN | LPOLLRDHUP);
d3e2ba59
JD
1956 if (ret < 0) {
1957 goto error;
1958 }
1959
1960restart:
1961 while (1) {
1962 int i;
1963
eea7556c
MD
1964 health_code_update();
1965
d3e2ba59
JD
1966 /* Infinite blocking call, waiting for transmission */
1967 DBG3("Relayd live viewer worker thread polling...");
eea7556c 1968 health_poll_entry();
d3e2ba59 1969 ret = lttng_poll_wait(&events, -1);
eea7556c 1970 health_poll_exit();
d3e2ba59
JD
1971 if (ret < 0) {
1972 /*
1973 * Restart interrupted system call.
1974 */
1975 if (errno == EINTR) {
1976 goto restart;
1977 }
1978 goto error;
1979 }
1980
1981 nb_fd = ret;
1982
1983 /*
1984 * Process control. The control connection is prioritised so we don't
1985 * starve it with high throughput tracing data on the data
1986 * connection.
1987 */
1988 for (i = 0; i < nb_fd; i++) {
1989 /* Fetch once the poll data */
1990 uint32_t revents = LTTNG_POLL_GETEV(&events, i);
1991 int pollfd = LTTNG_POLL_GETFD(&events, i);
1992
eea7556c
MD
1993 health_code_update();
1994
fd20dac9
MD
1995 if (!revents) {
1996 /* No activity for this FD (poll implementation). */
1997 continue;
1998 }
1999
d3e2ba59 2000 /* Thread quit pipe has been closed. Killing thread. */
bcf4a440 2001 ret = check_thread_quit_pipe(pollfd, revents);
d3e2ba59
JD
2002 if (ret) {
2003 err = 0;
2004 goto exit;
2005 }
2006
7591bab1 2007 /* Inspect the relay conn pipe for new connection. */
58eb9381 2008 if (pollfd == live_conn_pipe[0]) {
03e43155 2009 if (revents & LPOLLIN) {
302d8906
JG
2010 struct relay_connection *conn;
2011
7591bab1
MD
2012 ret = lttng_read(live_conn_pipe[0],
2013 &conn, sizeof(conn));
d3e2ba59
JD
2014 if (ret < 0) {
2015 goto error;
2016 }
58eb9381
DG
2017 lttng_poll_add(&events, conn->sock->fd,
2018 LPOLLIN | LPOLLRDHUP);
7591bab1
MD
2019 connection_ht_add(viewer_connections_ht, conn);
2020 DBG("Connection socket %d added to poll", conn->sock->fd);
03e43155
MD
2021 } else if (revents & (LPOLLERR | LPOLLHUP | LPOLLRDHUP)) {
2022 ERR("Relay live pipe error");
2023 goto error;
2024 } else {
2025 ERR("Unexpected poll events %u for sock %d", revents, pollfd);
2026 goto error;
d3e2ba59 2027 }
58eb9381 2028 } else {
7591bab1 2029 /* Connection activity. */
302d8906
JG
2030 struct relay_connection *conn;
2031
7591bab1
MD
2032 conn = connection_get_by_sock(viewer_connections_ht, pollfd);
2033 if (!conn) {
2034 continue;
2035 }
58eb9381 2036
03e43155 2037 if (revents & LPOLLIN) {
58eb9381
DG
2038 ret = conn->sock->ops->recvmsg(conn->sock, &recv_hdr,
2039 sizeof(recv_hdr), 0);
d3e2ba59 2040 if (ret <= 0) {
7591bab1 2041 /* Connection closed. */
58eb9381 2042 cleanup_connection_pollfd(&events, pollfd);
7591bab1
MD
2043 /* Put "create" ownership reference. */
2044 connection_put(conn);
58eb9381 2045 DBG("Viewer control conn closed with %d", pollfd);
d3e2ba59 2046 } else {
58eb9381 2047 ret = process_control(&recv_hdr, conn);
d3e2ba59
JD
2048 if (ret < 0) {
2049 /* Clear the session on error. */
58eb9381 2050 cleanup_connection_pollfd(&events, pollfd);
7591bab1
MD
2051 /* Put "create" ownership reference. */
2052 connection_put(conn);
d3e2ba59
JD
2053 DBG("Viewer connection closed with %d", pollfd);
2054 }
2055 }
03e43155
MD
2056 } else if (revents & (LPOLLERR | LPOLLHUP | LPOLLRDHUP)) {
2057 cleanup_connection_pollfd(&events, pollfd);
2058 /* Put "create" ownership reference. */
2059 connection_put(conn);
2060 } else {
2061 ERR("Unexpected poll events %u for sock %d", revents, pollfd);
2062 connection_put(conn);
2063 goto error;
d3e2ba59 2064 }
7591bab1
MD
2065 /* Put local "get_by_sock" reference. */
2066 connection_put(conn);
d3e2ba59
JD
2067 }
2068 }
2069 }
2070
2071exit:
2072error:
2073 lttng_poll_clean(&events);
2074
58eb9381 2075 /* Cleanup reamaining connection object. */
d3e2ba59 2076 rcu_read_lock();
7591bab1 2077 cds_lfht_for_each_entry(viewer_connections_ht->ht, &iter.iter,
302d8906 2078 destroy_conn,
58eb9381 2079 sock_n.node) {
eea7556c 2080 health_code_update();
7591bab1 2081 connection_put(destroy_conn);
d3e2ba59
JD
2082 }
2083 rcu_read_unlock();
2084error_poll_create:
7591bab1
MD
2085 lttng_ht_destroy(viewer_connections_ht);
2086viewer_connections_ht_error:
58eb9381 2087 /* Close relay conn pipes */
07bc1945 2088 (void) fd_tracker_util_pipe_close(the_fd_tracker, live_conn_pipe);
d3e2ba59
JD
2089 if (err) {
2090 DBG("Viewer worker thread exited with error");
2091 }
2092 DBG("Viewer worker thread cleanup complete");
9b5e0863 2093error_testpoint:
eea7556c
MD
2094 if (err) {
2095 health_error();
2096 ERR("Health error occurred in %s", __func__);
2097 }
2098 health_unregister(health_relayd);
b4aacfdc
MD
2099 if (lttng_relay_stop_threads()) {
2100 ERR("Error stopping threads");
178a0557 2101 }
d3e2ba59
JD
2102 rcu_unregister_thread();
2103 return NULL;
2104}
2105
2106/*
2107 * Create the relay command pipe to wake thread_manage_apps.
2108 * Closed in cleanup().
2109 */
58eb9381 2110static int create_conn_pipe(void)
d3e2ba59 2111{
07bc1945
JG
2112 return fd_tracker_util_pipe_open_cloexec(the_fd_tracker,
2113 "Live connection pipe", live_conn_pipe);
178a0557 2114}
d3e2ba59 2115
178a0557 2116int relayd_live_join(void)
d3e2ba59 2117{
178a0557 2118 int ret, retval = 0;
d3e2ba59
JD
2119 void *status;
2120
d3e2ba59 2121 ret = pthread_join(live_listener_thread, &status);
178a0557
MD
2122 if (ret) {
2123 errno = ret;
d3e2ba59 2124 PERROR("pthread_join live listener");
178a0557 2125 retval = -1;
d3e2ba59
JD
2126 }
2127
2128 ret = pthread_join(live_worker_thread, &status);
178a0557
MD
2129 if (ret) {
2130 errno = ret;
d3e2ba59 2131 PERROR("pthread_join live worker");
178a0557 2132 retval = -1;
d3e2ba59
JD
2133 }
2134
2135 ret = pthread_join(live_dispatcher_thread, &status);
178a0557
MD
2136 if (ret) {
2137 errno = ret;
d3e2ba59 2138 PERROR("pthread_join live dispatcher");
178a0557 2139 retval = -1;
d3e2ba59
JD
2140 }
2141
178a0557 2142 cleanup_relayd_live();
d3e2ba59 2143
178a0557 2144 return retval;
d3e2ba59
JD
2145}
2146
2147/*
2148 * main
2149 */
7591bab1 2150int relayd_live_create(struct lttng_uri *uri)
d3e2ba59 2151{
178a0557 2152 int ret = 0, retval = 0;
d3e2ba59
JD
2153 void *status;
2154 int is_root;
2155
178a0557
MD
2156 if (!uri) {
2157 retval = -1;
2158 goto exit_init_data;
2159 }
d3e2ba59
JD
2160 live_uri = uri;
2161
d3e2ba59
JD
2162 /* Check if daemon is UID = 0 */
2163 is_root = !getuid();
2164
2165 if (!is_root) {
2166 if (live_uri->port < 1024) {
2167 ERR("Need to be root to use ports < 1024");
178a0557
MD
2168 retval = -1;
2169 goto exit_init_data;
d3e2ba59
JD
2170 }
2171 }
2172
2173 /* Setup the thread apps communication pipe. */
178a0557
MD
2174 if (create_conn_pipe()) {
2175 retval = -1;
2176 goto exit_init_data;
d3e2ba59
JD
2177 }
2178
2179 /* Init relay command queue. */
8bdee6e2 2180 cds_wfcq_init(&viewer_conn_queue.head, &viewer_conn_queue.tail);
d3e2ba59
JD
2181
2182 /* Set up max poll set size */
25b397f9
MD
2183 if (lttng_poll_set_max_size()) {
2184 retval = -1;
2185 goto exit_init_data;
2186 }
d3e2ba59
JD
2187
2188 /* Setup the dispatcher thread */
1a1a34b4 2189 ret = pthread_create(&live_dispatcher_thread, default_pthread_attr(),
d3e2ba59 2190 thread_dispatcher, (void *) NULL);
178a0557
MD
2191 if (ret) {
2192 errno = ret;
d3e2ba59 2193 PERROR("pthread_create viewer dispatcher");
178a0557
MD
2194 retval = -1;
2195 goto exit_dispatcher_thread;
d3e2ba59
JD
2196 }
2197
2198 /* Setup the worker thread */
1a1a34b4 2199 ret = pthread_create(&live_worker_thread, default_pthread_attr(),
7591bab1 2200 thread_worker, NULL);
178a0557
MD
2201 if (ret) {
2202 errno = ret;
d3e2ba59 2203 PERROR("pthread_create viewer worker");
178a0557
MD
2204 retval = -1;
2205 goto exit_worker_thread;
d3e2ba59
JD
2206 }
2207
2208 /* Setup the listener thread */
1a1a34b4 2209 ret = pthread_create(&live_listener_thread, default_pthread_attr(),
d3e2ba59 2210 thread_listener, (void *) NULL);
178a0557
MD
2211 if (ret) {
2212 errno = ret;
d3e2ba59 2213 PERROR("pthread_create viewer listener");
178a0557
MD
2214 retval = -1;
2215 goto exit_listener_thread;
d3e2ba59
JD
2216 }
2217
178a0557
MD
2218 /*
2219 * All OK, started all threads.
2220 */
2221 return retval;
2222
9911d21b
JG
2223 /*
2224 * Join on the live_listener_thread should anything be added after
2225 * the live_listener thread's creation.
2226 */
d3e2ba59 2227
178a0557 2228exit_listener_thread:
d3e2ba59 2229
d3e2ba59 2230 ret = pthread_join(live_worker_thread, &status);
178a0557
MD
2231 if (ret) {
2232 errno = ret;
d3e2ba59 2233 PERROR("pthread_join live worker");
178a0557 2234 retval = -1;
d3e2ba59 2235 }
178a0557 2236exit_worker_thread:
d3e2ba59 2237
d3e2ba59 2238 ret = pthread_join(live_dispatcher_thread, &status);
178a0557
MD
2239 if (ret) {
2240 errno = ret;
d3e2ba59 2241 PERROR("pthread_join live dispatcher");
178a0557 2242 retval = -1;
d3e2ba59 2243 }
178a0557 2244exit_dispatcher_thread:
d3e2ba59 2245
178a0557
MD
2246exit_init_data:
2247 cleanup_relayd_live();
d3e2ba59 2248
178a0557 2249 return retval;
d3e2ba59 2250}
This page took 0.1673 seconds and 5 git commands to generate.