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