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