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