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