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