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