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