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