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