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