Implement lttng_read()/lttng_write()
[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>
48#include <common/sessiond-comm/sessiond-comm.h>
49#include <common/sessiond-comm/inet.h>
50#include <common/sessiond-comm/relayd.h>
51#include <common/uri.h>
52#include <common/utils.h>
53
54#include "cmd.h"
55#include "live.h"
56#include "lttng-relayd.h"
57#include "lttng-viewer.h"
58#include "utils.h"
eea7556c 59#include "health-relayd.h"
d3e2ba59
JD
60
61static struct lttng_uri *live_uri;
62
63/*
64 * Quit pipe for all threads. This permits a single cancellation point
65 * for all threads when receiving an event on the pipe.
66 */
67static int live_thread_quit_pipe[2] = { -1, -1 };
68
69/*
70 * This pipe is used to inform the worker thread that a command is queued and
71 * ready to be processed.
72 */
73static int live_relay_cmd_pipe[2] = { -1, -1 };
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 */
88static struct relay_cmd_queue viewer_cmd_queue;
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
103/*
104 * Write to writable pipe used to notify a thread.
105 */
106static
107int notify_thread_pipe(int wpipe)
108{
109 int ret;
110
111 do {
112 ret = write(wpipe, "!", 1);
113 } while (ret < 0 && errno == EINTR);
114 if (ret < 0 || ret != 1) {
115 PERROR("write poll pipe");
116 }
117
118 return ret;
119}
120
121/*
122 * Stop all threads by closing the thread quit pipe.
123 */
124static
125void stop_threads(void)
126{
127 int ret;
128
129 /* Stopping all threads */
130 DBG("Terminating all live threads");
131 ret = notify_thread_pipe(live_thread_quit_pipe[1]);
132 if (ret < 0) {
133 ERR("write error on thread quit pipe");
134 }
135
136 /* Dispatch thread */
137 CMM_STORE_SHARED(live_dispatch_thread_exit, 1);
138 futex_nto1_wake(&viewer_cmd_queue.futex);
139}
140
d3e2ba59
JD
141/*
142 * Create a poll set with O_CLOEXEC and add the thread quit pipe to the set.
143 */
144static
145int create_thread_poll_set(struct lttng_poll_event *events, int size)
146{
147 int ret;
148
149 if (events == NULL || size == 0) {
150 ret = -1;
151 goto error;
152 }
153
154 ret = lttng_poll_create(events, size, LTTNG_CLOEXEC);
155 if (ret < 0) {
156 goto error;
157 }
158
159 /* Add quit pipe */
160 ret = lttng_poll_add(events, live_thread_quit_pipe[0], LPOLLIN);
161 if (ret < 0) {
162 goto error;
163 }
164
165 return 0;
166
167error:
168 return ret;
169}
170
171/*
172 * Check if the thread quit pipe was triggered.
173 *
174 * Return 1 if it was triggered else 0;
175 */
176static
177int check_thread_quit_pipe(int fd, uint32_t events)
178{
179 if (fd == live_thread_quit_pipe[0] && (events & LPOLLIN)) {
180 return 1;
181 }
182
183 return 0;
184}
185
186/*
187 * Create and init socket from uri.
188 */
189static
190struct lttcomm_sock *init_socket(struct lttng_uri *uri)
191{
192 int ret;
193 struct lttcomm_sock *sock = NULL;
194
195 sock = lttcomm_alloc_sock_from_uri(uri);
196 if (sock == NULL) {
197 ERR("Allocating socket");
198 goto error;
199 }
200
201 ret = lttcomm_create_sock(sock);
202 if (ret < 0) {
203 goto error;
204 }
205 DBG("Listening on sock %d for live", sock->fd);
206
207 ret = sock->ops->bind(sock);
208 if (ret < 0) {
209 goto error;
210 }
211
212 ret = sock->ops->listen(sock, -1);
213 if (ret < 0) {
214 goto error;
215
216 }
217
218 return sock;
219
220error:
221 if (sock) {
222 lttcomm_destroy_sock(sock);
223 }
224 return NULL;
225}
226
227/*
228 * This thread manages the listening for new connections on the network
229 */
230static
231void *thread_listener(void *data)
232{
233 int i, ret, pollfd, err = -1;
234 int val = 1;
235 uint32_t revents, nb_fd;
236 struct lttng_poll_event events;
237 struct lttcomm_sock *live_control_sock;
238
239 DBG("[thread] Relay live listener started");
240
eea7556c
MD
241 health_register(health_relayd, HEALTH_RELAYD_TYPE_LIVE_LISTENER);
242
243 health_code_update();
244
d3e2ba59
JD
245 live_control_sock = init_socket(live_uri);
246 if (!live_control_sock) {
247 goto error_sock_control;
248 }
249
250 /*
251 * Pass 3 as size here for the thread quit pipe, control and data socket.
252 */
253 ret = create_thread_poll_set(&events, 2);
254 if (ret < 0) {
255 goto error_create_poll;
256 }
257
258 /* Add the control socket */
259 ret = lttng_poll_add(&events, live_control_sock->fd, LPOLLIN | LPOLLRDHUP);
260 if (ret < 0) {
261 goto error_poll_add;
262 }
263
264 while (1) {
eea7556c
MD
265 health_code_update();
266
d3e2ba59
JD
267 DBG("Listener accepting live viewers connections");
268
269restart:
eea7556c 270 health_poll_entry();
d3e2ba59 271 ret = lttng_poll_wait(&events, -1);
eea7556c 272 health_poll_exit();
d3e2ba59
JD
273 if (ret < 0) {
274 /*
275 * Restart interrupted system call.
276 */
277 if (errno == EINTR) {
278 goto restart;
279 }
280 goto error;
281 }
282 nb_fd = ret;
283
284 DBG("Relay new viewer connection received");
285 for (i = 0; i < nb_fd; i++) {
eea7556c
MD
286 health_code_update();
287
d3e2ba59
JD
288 /* Fetch once the poll data */
289 revents = LTTNG_POLL_GETEV(&events, i);
290 pollfd = LTTNG_POLL_GETFD(&events, i);
291
292 /* Thread quit pipe has been closed. Killing thread. */
293 ret = check_thread_quit_pipe(pollfd, revents);
294 if (ret) {
295 err = 0;
296 goto exit;
297 }
298
299 if (revents & (LPOLLERR | LPOLLHUP | LPOLLRDHUP)) {
300 ERR("socket poll error");
301 goto error;
302 } else if (revents & LPOLLIN) {
303 /*
304 * Get allocated in this thread, enqueued to a global queue,
305 * dequeued and freed in the worker thread.
306 */
307 struct relay_command *relay_cmd;
308 struct lttcomm_sock *newsock;
309
310 relay_cmd = zmalloc(sizeof(*relay_cmd));
311 if (!relay_cmd) {
312 PERROR("relay command zmalloc");
313 goto error;
314 }
315
316 assert(pollfd == live_control_sock->fd);
317 newsock = live_control_sock->ops->accept(live_control_sock);
318 if (!newsock) {
319 PERROR("accepting control sock");
320 free(relay_cmd);
321 goto error;
322 }
323 DBG("Relay viewer connection accepted socket %d", newsock->fd);
324 ret = setsockopt(newsock->fd, SOL_SOCKET, SO_REUSEADDR, &val,
325 sizeof(int));
326 if (ret < 0) {
327 PERROR("setsockopt inet");
328 lttcomm_destroy_sock(newsock);
329 free(relay_cmd);
330 goto error;
331 }
332 relay_cmd->sock = newsock;
333
334 /*
335 * Lock free enqueue the request.
336 */
337 cds_wfq_enqueue(&viewer_cmd_queue.queue, &relay_cmd->node);
338
339 /*
340 * Wake the dispatch queue futex. Implicit memory
341 * barrier with the exchange in cds_wfq_enqueue.
342 */
343 futex_nto1_wake(&viewer_cmd_queue.futex);
344 }
345 }
346 }
347
348exit:
349error:
350error_poll_add:
351 lttng_poll_clean(&events);
352error_create_poll:
353 if (live_control_sock->fd >= 0) {
354 ret = live_control_sock->ops->close(live_control_sock);
355 if (ret) {
356 PERROR("close");
357 }
358 }
359 lttcomm_destroy_sock(live_control_sock);
360error_sock_control:
361 if (err) {
eea7556c 362 health_error();
d3e2ba59
JD
363 DBG("Live viewer listener thread exited with error");
364 }
eea7556c 365 health_unregister(health_relayd);
d3e2ba59
JD
366 DBG("Live viewer listener thread cleanup complete");
367 stop_threads();
368 return NULL;
369}
370
371/*
372 * This thread manages the dispatching of the requests to worker threads
373 */
374static
375void *thread_dispatcher(void *data)
376{
eea7556c 377 int ret, err = -1;
d3e2ba59
JD
378 struct cds_wfq_node *node;
379 struct relay_command *relay_cmd = NULL;
380
381 DBG("[thread] Live viewer relay dispatcher started");
382
eea7556c
MD
383 health_register(health_relayd, HEALTH_RELAYD_TYPE_LIVE_DISPATCHER);
384
385 health_code_update();
386
d3e2ba59 387 while (!CMM_LOAD_SHARED(live_dispatch_thread_exit)) {
eea7556c
MD
388 health_code_update();
389
d3e2ba59
JD
390 /* Atomically prepare the queue futex */
391 futex_nto1_prepare(&viewer_cmd_queue.futex);
392
393 do {
eea7556c
MD
394 health_code_update();
395
d3e2ba59
JD
396 /* Dequeue commands */
397 node = cds_wfq_dequeue_blocking(&viewer_cmd_queue.queue);
398 if (node == NULL) {
399 DBG("Woken up but nothing in the live-viewer "
400 "relay command queue");
401 /* Continue thread execution */
402 break;
403 }
404
405 relay_cmd = caa_container_of(node, struct relay_command, node);
406 DBG("Dispatching viewer request waiting on sock %d",
407 relay_cmd->sock->fd);
408
409 /*
410 * Inform worker thread of the new request. This call is blocking
411 * so we can be assured that the data will be read at some point in
412 * time or wait to the end of the world :)
413 */
414 do {
415 ret = write(live_relay_cmd_pipe[1], relay_cmd,
416 sizeof(*relay_cmd));
417 } while (ret < 0 && errno == EINTR);
418 free(relay_cmd);
419 if (ret < 0 || ret != sizeof(struct relay_command)) {
420 PERROR("write cmd pipe");
421 goto error;
422 }
423 } while (node != NULL);
424
425 /* Futex wait on queue. Blocking call on futex() */
eea7556c 426 health_poll_entry();
d3e2ba59 427 futex_nto1_wait(&viewer_cmd_queue.futex);
eea7556c 428 health_poll_exit();
d3e2ba59
JD
429 }
430
eea7556c
MD
431 /* Normal exit, no error */
432 err = 0;
433
d3e2ba59 434error:
eea7556c
MD
435 if (err) {
436 health_error();
437 ERR("Health error occurred in %s", __func__);
438 }
439 health_unregister(health_relayd);
d3e2ba59
JD
440 DBG("Live viewer dispatch thread dying");
441 stop_threads();
442 return NULL;
443}
444
445/*
446 * Establish connection with the viewer and check the versions.
447 *
448 * Return 0 on success or else negative value.
449 */
450static
451int viewer_connect(struct relay_command *cmd)
452{
453 int ret;
454 struct lttng_viewer_connect reply, msg;
455
456 assert(cmd);
457
458 cmd->version_check_done = 1;
459
eea7556c
MD
460 health_code_update();
461
d3e2ba59
JD
462 /* Get version from the other side. */
463 ret = cmd->sock->ops->recvmsg(cmd->sock, &msg, sizeof(msg), 0);
464 if (ret < 0 || ret != sizeof(msg)) {
465 if (ret == 0) {
466 /* Orderly shutdown. Not necessary to print an error. */
467 DBG("Socket %d did an orderly shutdown", cmd->sock->fd);
468 } else {
469 ERR("Relay failed to receive the version values.");
470 }
471 ret = -1;
472 goto end;
473 }
474
eea7556c
MD
475 health_code_update();
476
d3e2ba59
JD
477 reply.major = RELAYD_VERSION_COMM_MAJOR;
478 reply.minor = RELAYD_VERSION_COMM_MINOR;
479
480 /* Major versions must be the same */
481 if (reply.major != be32toh(msg.major)) {
482 DBG("Incompatible major versions (%u vs %u)", reply.major,
483 be32toh(msg.major));
484 ret = 0;
485 goto end;
486 }
487
488 cmd->major = reply.major;
489 /* We adapt to the lowest compatible version */
490 if (reply.minor <= be32toh(msg.minor)) {
491 cmd->minor = reply.minor;
492 } else {
493 cmd->minor = be32toh(msg.minor);
494 }
495
496 if (be32toh(msg.type) == VIEWER_CLIENT_COMMAND) {
497 cmd->type = RELAY_VIEWER_COMMAND;
498 } else if (be32toh(msg.type) == VIEWER_CLIENT_NOTIFICATION) {
499 cmd->type = RELAY_VIEWER_NOTIFICATION;
500 } else {
501 ERR("Unknown connection type : %u", be32toh(msg.type));
502 ret = -1;
503 goto end;
504 }
505
506 reply.major = htobe32(reply.major);
507 reply.minor = htobe32(reply.minor);
508 if (cmd->type == RELAY_VIEWER_COMMAND) {
509 reply.viewer_session_id = htobe64(++last_relay_viewer_session_id);
510 }
eea7556c
MD
511
512 health_code_update();
513
d3e2ba59
JD
514 ret = cmd->sock->ops->sendmsg(cmd->sock, &reply,
515 sizeof(struct lttng_viewer_connect), 0);
516 if (ret < 0) {
517 ERR("Relay sending version");
518 }
519
eea7556c
MD
520 health_code_update();
521
d3e2ba59
JD
522 DBG("Version check done using protocol %u.%u", cmd->major, cmd->minor);
523 ret = 0;
524
525end:
526 return ret;
527}
528
529/*
530 * Send the viewer the list of current sessions.
531 *
532 * Return 0 on success or else a negative value.
533 */
534static
535int viewer_list_sessions(struct relay_command *cmd,
536 struct lttng_ht *sessions_ht)
537{
538 int ret;
539 struct lttng_viewer_list_sessions session_list;
540 unsigned long count;
541 long approx_before, approx_after;
542 struct lttng_ht_node_ulong *node;
543 struct lttng_ht_iter iter;
544 struct lttng_viewer_session send_session;
545 struct relay_session *session;
546
547 DBG("List sessions received");
548
549 if (cmd->version_check_done == 0) {
550 ERR("Trying to list sessions before version check");
551 ret = -1;
552 goto end_no_session;
553 }
554
555 rcu_read_lock();
556 cds_lfht_count_nodes(sessions_ht->ht, &approx_before, &count, &approx_after);
557 session_list.sessions_count = htobe32(count);
558
eea7556c
MD
559 health_code_update();
560
d3e2ba59
JD
561 ret = cmd->sock->ops->sendmsg(cmd->sock, &session_list,
562 sizeof(session_list), 0);
563 if (ret < 0) {
564 ERR("Relay sending sessions list");
565 goto end_unlock;
566 }
567
eea7556c
MD
568 health_code_update();
569
d3e2ba59 570 cds_lfht_for_each_entry(sessions_ht->ht, &iter.iter, node, node) {
eea7556c
MD
571 health_code_update();
572
d3e2ba59
JD
573 node = lttng_ht_iter_get_node_ulong(&iter);
574 if (!node) {
575 goto end_unlock;
576 }
577 session = caa_container_of(node, struct relay_session, session_n);
578
579 strncpy(send_session.session_name, session->session_name,
580 sizeof(send_session.session_name));
581 strncpy(send_session.hostname, session->hostname,
582 sizeof(send_session.hostname));
583 send_session.id = htobe64(session->id);
584 send_session.live_timer = htobe32(session->live_timer);
585 send_session.clients = htobe32(session->viewer_attached);
87b576ec 586 send_session.streams = htobe32(session->stream_count);
d3e2ba59 587
eea7556c
MD
588 health_code_update();
589
d3e2ba59
JD
590 ret = cmd->sock->ops->sendmsg(cmd->sock, &send_session,
591 sizeof(send_session), 0);
592 if (ret < 0) {
593 ERR("Relay sending session info");
594 goto end_unlock;
595 }
596 }
eea7556c
MD
597 health_code_update();
598
d3e2ba59
JD
599 rcu_read_unlock();
600 ret = 0;
601 goto end;
602
603end_unlock:
604 rcu_read_unlock();
605
606end:
607end_no_session:
608 return ret;
609}
610
0e6830aa
JD
611/*
612 * Open index file using a given viewer stream.
613 *
614 * Return 0 on success or else a negative value.
615 */
616static int open_index(struct relay_viewer_stream *stream)
617{
618 int ret;
619 char fullpath[PATH_MAX];
620 struct lttng_packet_index_file_hdr hdr;
621
622 if (stream->tracefile_size > 0) {
623 /* For now we don't support on-disk ring buffer. */
624 ret = -1;
625 goto end;
626 }
627
628 ret = snprintf(fullpath, sizeof(fullpath), "%s/" DEFAULT_INDEX_DIR "/%s"
629 DEFAULT_INDEX_FILE_SUFFIX, stream->path_name,
630 stream->channel_name);
631 if (ret < 0) {
632 PERROR("snprintf index path");
633 goto error;
634 }
635
636 DBG("Opening index file %s in read only", fullpath);
637 ret = open(fullpath, O_RDONLY);
638 if (ret < 0) {
639 if (errno == ENOENT) {
640 ret = -ENOENT;
641 goto error;
642 } else {
643 PERROR("opening index in read-only");
644 }
645 goto error;
646 }
647 stream->index_read_fd = ret;
648 DBG("Opening index file %s in read only, (fd: %d)", fullpath, ret);
649
650 do {
651 health_code_update();
652 ret = read(stream->index_read_fd, &hdr, sizeof(hdr));
653 } while (ret < 0 && errno == EINTR);
654 if (ret < 0) {
655 PERROR("Reading index header");
656 goto error;
657 }
658 if (strncmp(hdr.magic, INDEX_MAGIC, sizeof(hdr.magic)) != 0) {
659 ERR("Invalid header magic");
660 ret = -1;
661 goto error;
662 }
663 if (be32toh(hdr.index_major) != INDEX_MAJOR ||
664 be32toh(hdr.index_minor) != INDEX_MINOR) {
665 ERR("Invalid header version");
666 ret = -1;
667 goto error;
668 }
669 ret = 0;
670
671error:
672end:
673 return ret;
674}
675
d3e2ba59
JD
676/*
677 * Allocate and init a new viewer_stream.
678 *
679 * Copies the values from the stream passed in parameter and insert the new
680 * stream in the viewer_streams_ht.
681 *
682 * MUST be called with rcu_read_lock held.
683 *
684 * Returns 0 on success or a negative value on error.
685 */
686static
0e6830aa 687int init_viewer_stream(struct relay_stream *stream, int seek_last)
d3e2ba59
JD
688{
689 int ret;
690 struct relay_viewer_stream *viewer_stream;
691
692 assert(stream);
d3e2ba59
JD
693
694 viewer_stream = zmalloc(sizeof(*viewer_stream));
695 if (!viewer_stream) {
696 PERROR("relay viewer stream zmalloc");
697 ret = -1;
698 goto error;
699 }
700
701 viewer_stream->read_fd = -1;
702 viewer_stream->index_read_fd = -1;
703 viewer_stream->session_id = stream->session->id;
704 viewer_stream->stream_handle = stream->stream_handle;
705 viewer_stream->path_name = strndup(stream->path_name,
706 LTTNG_VIEWER_PATH_MAX);
707 viewer_stream->channel_name = strndup(stream->channel_name,
708 LTTNG_VIEWER_NAME_MAX);
709 viewer_stream->total_index_received = stream->total_index_received;
710 viewer_stream->tracefile_size = stream->tracefile_size;
711 viewer_stream->tracefile_count = stream->tracefile_count;
712 viewer_stream->metadata_flag = stream->metadata_flag;
713
0e6830aa
JD
714 if (seek_last && viewer_stream->total_index_received > 0) {
715 ret = open_index(viewer_stream);
716 if (ret < 0) {
717 goto error;
718 }
719 ret = lseek(viewer_stream->index_read_fd,
720 viewer_stream->total_index_received *
721 sizeof(struct lttng_packet_index),
722 SEEK_CUR);
723 if (ret < 0) {
724 goto error;
725 }
726 viewer_stream->last_sent_index =
727 viewer_stream->total_index_received;
728 }
729
d3e2ba59
JD
730 /*
731 * This is to avoid a race between the initialization of this object and
732 * the close of the given stream. If the stream is unable to find this
733 * viewer stream when closing, this copy will at least take the latest
734 * value.
735 */
736 viewer_stream->total_index_received = stream->total_index_received;
737
738 /*
739 * The deletion of this ctf_trace object is only done in a call RCU of the
740 * relay stream making it valid as long as we have the read side lock.
741 */
742 viewer_stream->ctf_trace = stream->ctf_trace;
743 uatomic_inc(&viewer_stream->ctf_trace->refcount);
744
745 lttng_ht_node_init_u64(&viewer_stream->stream_n, stream->stream_handle);
746 lttng_ht_add_unique_u64(viewer_streams_ht, &viewer_stream->stream_n);
747
748 ret = 0;
749
750error:
751 return ret;
752}
753
754/*
755 * Send the viewer the list of current sessions.
756 */
757static
758int viewer_attach_session(struct relay_command *cmd,
92c6ca54 759 struct lttng_ht *sessions_ht)
d3e2ba59
JD
760{
761 int ret, send_streams = 0, nb_streams = 0;
762 struct lttng_viewer_attach_session_request request;
763 struct lttng_viewer_attach_session_response response;
764 struct lttng_viewer_stream send_stream;
765 struct relay_stream *stream;
766 struct relay_viewer_stream *viewer_stream;
767 struct lttng_ht_node_ulong *node;
768 struct lttng_ht_node_u64 *node64;
769 struct lttng_ht_iter iter;
770 struct relay_session *session;
0e6830aa 771 int seek_last = 0;
d3e2ba59
JD
772
773 assert(cmd);
774 assert(sessions_ht);
d3e2ba59
JD
775
776 DBG("Attach session received");
777
778 if (cmd->version_check_done == 0) {
779 ERR("Trying to attach session before version check");
780 ret = -1;
781 goto end_no_session;
782 }
783
eea7556c
MD
784 health_code_update();
785
d3e2ba59
JD
786 ret = cmd->sock->ops->recvmsg(cmd->sock, &request, sizeof(request), 0);
787 if (ret < 0 || ret != sizeof(request)) {
788 if (ret == 0) {
789 /* Orderly shutdown. Not necessary to print an error. */
790 DBG("Socket %d did an orderly shutdown", cmd->sock->fd);
791 } else {
792 ERR("Relay failed to receive the attach parameters.");
793 }
794 ret = -1;
795 goto error;
796 }
797
eea7556c
MD
798 health_code_update();
799
d3e2ba59
JD
800 rcu_read_lock();
801 lttng_ht_lookup(sessions_ht,
802 (void *)((unsigned long) be64toh(request.session_id)), &iter);
803 node = lttng_ht_iter_get_node_ulong(&iter);
804 if (node == NULL) {
805 DBG("Relay session %" PRIu64 " not found",
806 be64toh(request.session_id));
807 response.status = htobe32(VIEWER_ATTACH_UNK);
808 goto send_reply;
809 }
810
811 session = caa_container_of(node, struct relay_session, session_n);
b92fdc2b 812 if (cmd->session_id == session->id) {
d3e2ba59
JD
813 /* Same viewer already attached, just send the stream list. */
814 send_streams = 1;
815 response.status = htobe32(VIEWER_ATTACH_OK);
816 } else if (session->viewer_attached != 0) {
817 DBG("Already a viewer attached");
818 response.status = htobe32(VIEWER_ATTACH_ALREADY);
819 goto send_reply;
820 } else if (session->live_timer == 0) {
821 DBG("Not live session");
822 response.status = htobe32(VIEWER_ATTACH_NOT_LIVE);
823 goto send_reply;
824 } else {
825 session->viewer_attached++;
826 send_streams = 1;
827 response.status = htobe32(VIEWER_ATTACH_OK);
b92fdc2b 828 cmd->session_id = session->id;
d3e2ba59
JD
829 cmd->session = session;
830 }
831
832 switch (be32toh(request.seek)) {
833 case VIEWER_SEEK_BEGINNING:
834 /* Default behaviour. */
835 break;
836 case VIEWER_SEEK_LAST:
0e6830aa 837 seek_last = 1;
d3e2ba59
JD
838 break;
839 default:
840 ERR("Wrong seek parameter");
841 response.status = htobe32(VIEWER_ATTACH_SEEK_ERR);
842 send_streams = 0;
843 goto send_reply;
844 }
845
846 if (send_streams) {
847 /* We should only be there if we have a session to attach to. */
848 assert(session);
849
850 /*
851 * Fill the viewer_streams_ht to count the number of streams
852 * ready to be sent and avoid concurrency issues on the
853 * relay_streams_ht and don't rely on a total session stream count.
854 */
855 cds_lfht_for_each_entry(relay_streams_ht->ht, &iter.iter, node, node) {
856 struct relay_viewer_stream *vstream;
857
eea7556c
MD
858 health_code_update();
859
d3e2ba59
JD
860 node = lttng_ht_iter_get_node_ulong(&iter);
861 if (!node) {
862 continue;
863 }
864 stream = caa_container_of(node, struct relay_stream, stream_n);
865 if (stream->session != cmd->session) {
866 continue;
867 }
868
869 /*
870 * Don't send streams with no ctf_trace, they are not ready to be
871 * read.
872 */
873 if (!stream->ctf_trace) {
874 continue;
875 }
876
92c6ca54 877 vstream = live_find_viewer_stream_by_id(stream->stream_handle);
d3e2ba59 878 if (!vstream) {
0e6830aa 879 ret = init_viewer_stream(stream, seek_last);
d3e2ba59
JD
880 if (ret < 0) {
881 goto end_unlock;
882 }
883 }
884 nb_streams++;
885 }
886 response.streams_count = htobe32(nb_streams);
887 }
888
889send_reply:
eea7556c 890 health_code_update();
d3e2ba59
JD
891 ret = cmd->sock->ops->sendmsg(cmd->sock, &response, sizeof(response), 0);
892 if (ret < 0) {
893 ERR("Relay sending viewer attach response");
894 goto end_unlock;
895 }
eea7556c 896 health_code_update();
d3e2ba59
JD
897
898 /*
899 * Unknown or busy session, just return gracefully, the viewer knows what
900 * is happening.
901 */
902 if (!send_streams) {
903 ret = 0;
904 goto end_unlock;
905 }
906
907 /* We should only be there if we have a session to attach to. */
908 assert(session);
909 cds_lfht_for_each_entry(viewer_streams_ht->ht, &iter.iter, node, node) {
eea7556c
MD
910 health_code_update();
911
d3e2ba59
JD
912 node64 = lttng_ht_iter_get_node_u64(&iter);
913 if (!node64) {
914 continue;
915 }
916 viewer_stream = caa_container_of(node64, struct relay_viewer_stream,
917 stream_n);
918 if (viewer_stream->session_id != cmd->session->id) {
919 continue;
920 }
921
922 send_stream.id = htobe64(viewer_stream->stream_handle);
923 send_stream.ctf_trace_id = htobe64(viewer_stream->ctf_trace->id);
924 send_stream.metadata_flag = htobe32(viewer_stream->metadata_flag);
925 strncpy(send_stream.path_name, viewer_stream->path_name,
926 sizeof(send_stream.path_name));
927 strncpy(send_stream.channel_name, viewer_stream->channel_name,
928 sizeof(send_stream.channel_name));
929
930 ret = cmd->sock->ops->sendmsg(cmd->sock, &send_stream,
931 sizeof(send_stream), 0);
932 if (ret < 0) {
933 ERR("Relay sending stream %" PRIu64, viewer_stream->stream_handle);
934 goto end_unlock;
935 }
936 DBG("Sent stream %" PRIu64 " to viewer", viewer_stream->stream_handle);
937 }
938 ret = 0;
939
940end_unlock:
941 rcu_read_unlock();
942end_no_session:
943error:
944 return ret;
945}
946
d3e2ba59
JD
947/*
948 * Get viewer stream from stream id.
949 *
950 * RCU read side lock MUST be acquired.
951 */
92c6ca54 952struct relay_viewer_stream *live_find_viewer_stream_by_id(uint64_t stream_id)
d3e2ba59
JD
953{
954 struct lttng_ht_node_u64 *node;
955 struct lttng_ht_iter iter;
956 struct relay_viewer_stream *stream = NULL;
957
d3e2ba59
JD
958 lttng_ht_lookup(viewer_streams_ht, &stream_id, &iter);
959 node = lttng_ht_iter_get_node_u64(&iter);
960 if (node == NULL) {
961 DBG("Relay viewer stream %" PRIu64 " not found", stream_id);
962 goto end;
963 }
964 stream = caa_container_of(node, struct relay_viewer_stream, stream_n);
965
966end:
967 return stream;
968}
969
970/*
971 * Send the next index for a stream.
972 *
973 * Return 0 on success or else a negative value.
974 */
975static
976int viewer_get_next_index(struct relay_command *cmd,
92c6ca54 977 struct lttng_ht *sessions_ht)
d3e2ba59
JD
978{
979 int ret;
980 struct lttng_viewer_get_next_index request_index;
981 struct lttng_viewer_index viewer_index;
982 struct lttng_packet_index packet_index;
983 struct relay_viewer_stream *vstream;
984 struct relay_stream *rstream;
985
986 assert(cmd);
d3e2ba59
JD
987 assert(sessions_ht);
988
989 DBG("Viewer get next index");
990
991 if (cmd->version_check_done == 0) {
992 ERR("Trying to request index before version check");
993 ret = -1;
994 goto end_no_session;
995 }
996
eea7556c 997 health_code_update();
d3e2ba59
JD
998 ret = cmd->sock->ops->recvmsg(cmd->sock, &request_index,
999 sizeof(request_index), 0);
1000 if (ret < 0 || ret != sizeof(request_index)) {
1001 ret = -1;
1002 ERR("Relay didn't receive the whole packet");
1003 goto end;
1004 }
eea7556c 1005 health_code_update();
d3e2ba59
JD
1006
1007 rcu_read_lock();
92c6ca54 1008 vstream = live_find_viewer_stream_by_id(be64toh(request_index.stream_id));
d3e2ba59
JD
1009 if (!vstream) {
1010 ret = -1;
1011 goto end_unlock;
1012 }
1013
1014 memset(&viewer_index, 0, sizeof(viewer_index));
1015
1016 /*
1017 * The viewer should not ask for index on metadata stream.
1018 */
1019 if (vstream->metadata_flag) {
1020 viewer_index.status = htobe32(VIEWER_INDEX_HUP);
1021 goto send_reply;
1022 }
1023
1024 /* First time, we open the index file */
1025 if (vstream->index_read_fd < 0) {
1026 ret = open_index(vstream);
0e6830aa 1027 if (ret == -ENOENT) {
d3e2ba59
JD
1028 /*
1029 * The index is created only when the first data packet arrives, it
1030 * might not be ready at the beginning of the session
1031 */
1032 viewer_index.status = htobe32(VIEWER_INDEX_RETRY);
1033 goto send_reply;
1034 } else if (ret < 0) {
1035 viewer_index.status = htobe32(VIEWER_INDEX_ERR);
1036 goto send_reply;
1037 }
1038 }
1039
1040 rstream = relay_stream_find_by_id(vstream->stream_handle);
1041 if (rstream) {
1042 if (rstream->beacon_ts_end != -1ULL &&
1043 vstream->last_sent_index == rstream->total_index_received) {
1044 viewer_index.status = htobe32(VIEWER_INDEX_INACTIVE);
1045 viewer_index.timestamp_end = htobe64(rstream->beacon_ts_end);
1046 goto send_reply;
1047 }
1048
1049 if (rstream->total_index_received <= vstream->last_sent_index) {
1050 /* No new index to send, retry later. */
1051 viewer_index.status = htobe32(VIEWER_INDEX_RETRY);
1052 goto send_reply;
1053 }
1054 } else if (!rstream &&
1055 vstream->total_index_received == vstream->last_sent_index) {
1056 /* Last index sent and stream closed */
1057 viewer_index.status = htobe32(VIEWER_INDEX_HUP);
1058 goto send_reply;
1059 }
1060
1061 if (!vstream->ctf_trace->metadata_received ||
1062 vstream->ctf_trace->metadata_received >
1063 vstream->ctf_trace->metadata_sent) {
1064 viewer_index.flags |= LTTNG_VIEWER_FLAG_NEW_METADATA;
1065 }
1066
1067 do {
eea7556c 1068 health_code_update();
d3e2ba59
JD
1069 ret = read(vstream->index_read_fd, &packet_index,
1070 sizeof(packet_index));
1071 } while (ret < 0 && errno == EINTR);
1072 if (ret < sizeof(packet_index)) {
1073 PERROR("Relay reading index file");
1074 viewer_index.status = htobe32(VIEWER_INDEX_ERR);
1075 } else {
1076 viewer_index.status = htobe32(VIEWER_INDEX_OK);
1077 vstream->last_sent_index++;
1078 }
1079
1080 /*
1081 * Indexes are stored in big endian, no need to switch before sending.
1082 */
1083 viewer_index.offset = packet_index.offset;
1084 viewer_index.packet_size = packet_index.packet_size;
1085 viewer_index.content_size = packet_index.content_size;
1086 viewer_index.timestamp_begin = packet_index.timestamp_begin;
1087 viewer_index.timestamp_end = packet_index.timestamp_end;
1088 viewer_index.events_discarded = packet_index.events_discarded;
1089 viewer_index.stream_id = packet_index.stream_id;
1090
1091send_reply:
1092 viewer_index.flags = htobe32(viewer_index.flags);
eea7556c 1093 health_code_update();
d3e2ba59
JD
1094 ret = cmd->sock->ops->sendmsg(cmd->sock, &viewer_index,
1095 sizeof(viewer_index), 0);
1096 if (ret < 0) {
1097 ERR("Relay index to viewer");
1098 goto end_unlock;
1099 }
eea7556c 1100 health_code_update();
d3e2ba59
JD
1101
1102 DBG("Index %" PRIu64 "for stream %" PRIu64 "sent",
1103 vstream->last_sent_index, vstream->stream_handle);
1104
1105end_unlock:
1106 rcu_read_unlock();
1107
1108end_no_session:
1109end:
1110 return ret;
1111}
1112
1113/*
1114 * Send the next index for a stream
1115 *
1116 * Return 0 on success or else a negative value.
1117 */
1118static
92c6ca54 1119int viewer_get_packet(struct relay_command *cmd)
d3e2ba59
JD
1120{
1121 int ret, send_data = 0;
1122 char *data = NULL;
1123 uint32_t len = 0;
1124 ssize_t read_len;
1125 struct lttng_viewer_get_packet get_packet_info;
1126 struct lttng_viewer_trace_packet reply;
1127 struct relay_viewer_stream *stream;
1128
1129 assert(cmd);
d3e2ba59
JD
1130
1131 DBG2("Relay get data packet");
1132
1133 if (cmd->version_check_done == 0) {
1134 ERR("Trying to get packet before version check");
1135 ret = -1;
1136 goto end;
1137 }
1138
eea7556c 1139 health_code_update();
d3e2ba59
JD
1140 ret = cmd->sock->ops->recvmsg(cmd->sock, &get_packet_info,
1141 sizeof(get_packet_info), 0);
1142 if (ret < 0 || ret != sizeof(get_packet_info)) {
1143 ret = -1;
1144 ERR("Relay didn't receive the whole packet");
1145 goto end;
1146 }
eea7556c 1147 health_code_update();
d3e2ba59 1148
0233a6a5
DG
1149 /* From this point on, the error label can be reached. */
1150 memset(&reply, 0, sizeof(reply));
1151
d3e2ba59 1152 rcu_read_lock();
92c6ca54 1153 stream = live_find_viewer_stream_by_id(be64toh(get_packet_info.stream_id));
d3e2ba59
JD
1154 if (!stream) {
1155 goto error;
1156 }
1157 assert(stream->ctf_trace);
1158
1159 /*
1160 * First time we read this stream, we need open the tracefile, we should
1161 * only arrive here if an index has already been sent to the viewer, so the
1162 * tracefile must exist, if it does not it is a fatal error.
1163 */
1164 if (stream->read_fd < 0) {
1165 char fullpath[PATH_MAX];
1166
1167 ret = snprintf(fullpath, PATH_MAX, "%s/%s", stream->path_name,
1168 stream->channel_name);
1169 if (ret < 0) {
1170 goto error;
1171 }
1172 ret = open(fullpath, O_RDONLY);
1173 if (ret < 0) {
1174 PERROR("Relay opening trace file");
1175 goto error;
1176 }
1177 stream->read_fd = ret;
1178 }
1179
d3e2ba59
JD
1180 if (!stream->ctf_trace->metadata_received ||
1181 stream->ctf_trace->metadata_received >
1182 stream->ctf_trace->metadata_sent) {
1183 reply.status = htobe32(VIEWER_GET_PACKET_ERR);
1184 reply.flags |= LTTNG_VIEWER_FLAG_NEW_METADATA;
d3e2ba59
JD
1185 goto send_reply;
1186 }
1187
1188 len = be32toh(get_packet_info.len);
1189 data = zmalloc(len);
1190 if (!data) {
1191 PERROR("relay data zmalloc");
1192 goto error;
1193 }
1194
1195 ret = lseek(stream->read_fd, be64toh(get_packet_info.offset), SEEK_SET);
1196 if (ret < 0) {
1197 PERROR("lseek");
1198 goto error;
1199 }
1200 read_len = read(stream->read_fd, data, len);
1201 if (read_len < (ssize_t) len) {
1202 PERROR("Relay reading trace file, fd: %d, offset: %" PRIu64,
1203 stream->read_fd, be64toh(get_packet_info.offset));
1204 goto error;
1205 }
1206 reply.status = htobe32(VIEWER_GET_PACKET_OK);
1207 reply.len = htobe32(len);
1208 send_data = 1;
1209 goto send_reply;
1210
1211error:
1212 reply.status = htobe32(VIEWER_GET_PACKET_ERR);
1213
1214send_reply:
1215 reply.flags = htobe32(reply.flags);
eea7556c
MD
1216
1217 health_code_update();
d3e2ba59
JD
1218 ret = cmd->sock->ops->sendmsg(cmd->sock, &reply, sizeof(reply), 0);
1219 if (ret < 0) {
1220 ERR("Relay data header to viewer");
1221 goto end_unlock;
1222 }
eea7556c 1223 health_code_update();
d3e2ba59
JD
1224
1225 if (send_data) {
eea7556c 1226 health_code_update();
d3e2ba59
JD
1227 ret = cmd->sock->ops->sendmsg(cmd->sock, data, len, 0);
1228 if (ret < 0) {
1229 ERR("Relay send data to viewer");
1230 goto end_unlock;
1231 }
eea7556c 1232 health_code_update();
d3e2ba59
JD
1233 }
1234
1235 DBG("Sent %u bytes for stream %" PRIu64, len,
1236 be64toh(get_packet_info.stream_id));
1237
1238end_unlock:
1239 free(data);
1240 rcu_read_unlock();
1241
1242end:
1243 return ret;
1244}
1245
1246/*
1247 * Send the session's metadata
1248 *
1249 * Return 0 on success else a negative value.
1250 */
1251static
92c6ca54 1252int viewer_get_metadata(struct relay_command *cmd)
d3e2ba59
JD
1253{
1254 int ret = 0;
1255 ssize_t read_len;
1256 uint64_t len = 0;
1257 char *data = NULL;
1258 struct lttng_viewer_get_metadata request;
1259 struct lttng_viewer_metadata_packet reply;
1260 struct relay_viewer_stream *stream;
1261
1262 assert(cmd);
d3e2ba59
JD
1263
1264 DBG("Relay get metadata");
1265
1266 if (cmd->version_check_done == 0) {
1267 ERR("Trying to get metadata before version check");
1268 ret = -1;
1269 goto end;
1270 }
1271
eea7556c 1272 health_code_update();
d3e2ba59
JD
1273 ret = cmd->sock->ops->recvmsg(cmd->sock, &request,
1274 sizeof(request), 0);
1275 if (ret < 0 || ret != sizeof(request)) {
1276 ret = -1;
1277 ERR("Relay didn't receive the whole packet");
1278 goto end;
1279 }
eea7556c 1280 health_code_update();
d3e2ba59
JD
1281
1282 rcu_read_lock();
92c6ca54 1283 stream = live_find_viewer_stream_by_id(be64toh(request.stream_id));
d3e2ba59
JD
1284 if (!stream || !stream->metadata_flag) {
1285 ERR("Invalid metadata stream");
1286 goto error;
1287 }
1288 assert(stream->ctf_trace);
1289 assert(stream->ctf_trace->metadata_sent <=
1290 stream->ctf_trace->metadata_received);
1291
1292 len = stream->ctf_trace->metadata_received -
1293 stream->ctf_trace->metadata_sent;
1294 if (len == 0) {
1295 reply.status = htobe32(VIEWER_NO_NEW_METADATA);
1296 goto send_reply;
1297 }
1298
1299 /* first time, we open the metadata file */
1300 if (stream->read_fd < 0) {
1301 char fullpath[PATH_MAX];
1302
1303 ret = snprintf(fullpath, PATH_MAX, "%s/%s", stream->path_name,
1304 stream->channel_name);
1305 if (ret < 0) {
1306 goto error;
1307 }
1308 ret = open(fullpath, O_RDONLY);
1309 if (ret < 0) {
1310 PERROR("Relay opening metadata file");
1311 goto error;
1312 }
1313 stream->read_fd = ret;
1314 }
1315
1316 reply.len = htobe64(len);
1317 data = zmalloc(len);
1318 if (!data) {
1319 PERROR("viewer metadata zmalloc");
1320 goto error;
1321 }
1322
1323 read_len = read(stream->read_fd, data, len);
1324 if (read_len < (ssize_t) len) {
1325 PERROR("Relay reading metadata file");
1326 goto error;
1327 }
1328 stream->ctf_trace->metadata_sent += read_len;
1329 reply.status = htobe32(VIEWER_METADATA_OK);
1330 goto send_reply;
1331
1332error:
1333 reply.status = htobe32(VIEWER_METADATA_ERR);
1334
1335send_reply:
eea7556c 1336 health_code_update();
d3e2ba59
JD
1337 ret = cmd->sock->ops->sendmsg(cmd->sock, &reply, sizeof(reply), 0);
1338 if (ret < 0) {
1339 ERR("Relay data header to viewer");
1340 goto end_unlock;
1341 }
eea7556c 1342 health_code_update();
d3e2ba59
JD
1343
1344 if (len > 0) {
1345 ret = cmd->sock->ops->sendmsg(cmd->sock, data, len, 0);
1346 if (ret < 0) {
1347 ERR("Relay send data to viewer");
1348 goto end_unlock;
1349 }
1350 }
1351
1352 DBG("Sent %" PRIu64 " bytes of metadata for stream %" PRIu64, len,
1353 be64toh(request.stream_id));
1354
1355 DBG("Metadata sent");
1356
1357end_unlock:
1358 free(data);
1359 rcu_read_unlock();
1360end:
1361 return ret;
1362}
1363
1364/*
1365 * live_relay_unknown_command: send -1 if received unknown command
1366 */
1367static
1368void live_relay_unknown_command(struct relay_command *cmd)
1369{
1370 struct lttcomm_relayd_generic_reply reply;
1371 int ret;
1372
1373 reply.ret_code = htobe32(LTTNG_ERR_UNK);
1374 ret = cmd->sock->ops->sendmsg(cmd->sock, &reply,
1375 sizeof(struct lttcomm_relayd_generic_reply), 0);
1376 if (ret < 0) {
1377 ERR("Relay sending unknown command");
1378 }
1379}
1380
1381/*
1382 * Process the commands received on the control socket
1383 */
1384static
1385int process_control(struct lttng_viewer_cmd *recv_hdr,
92c6ca54 1386 struct relay_command *cmd, struct lttng_ht *sessions_ht)
d3e2ba59
JD
1387{
1388 int ret = 0;
1389
1390 switch (be32toh(recv_hdr->cmd)) {
1391 case VIEWER_CONNECT:
1392 ret = viewer_connect(cmd);
1393 break;
1394 case VIEWER_LIST_SESSIONS:
1395 ret = viewer_list_sessions(cmd, sessions_ht);
1396 break;
1397 case VIEWER_ATTACH_SESSION:
92c6ca54 1398 ret = viewer_attach_session(cmd, sessions_ht);
d3e2ba59
JD
1399 break;
1400 case VIEWER_GET_NEXT_INDEX:
92c6ca54 1401 ret = viewer_get_next_index(cmd, sessions_ht);
d3e2ba59
JD
1402 break;
1403 case VIEWER_GET_PACKET:
92c6ca54 1404 ret = viewer_get_packet(cmd);
d3e2ba59
JD
1405 break;
1406 case VIEWER_GET_METADATA:
92c6ca54 1407 ret = viewer_get_metadata(cmd);
d3e2ba59
JD
1408 break;
1409 default:
1410 ERR("Received unknown viewer command (%u)", be32toh(recv_hdr->cmd));
1411 live_relay_unknown_command(cmd);
1412 ret = -1;
1413 goto end;
1414 }
1415
1416end:
1417 return ret;
1418}
1419
1420static
1421void cleanup_poll_connection(struct lttng_poll_event *events, int pollfd)
1422{
1423 int ret;
1424
1425 assert(events);
1426
1427 lttng_poll_del(events, pollfd);
1428
1429 ret = close(pollfd);
1430 if (ret < 0) {
1431 ERR("Closing pollfd %d", pollfd);
1432 }
1433}
1434
1435/*
1436 * Create and add connection to the given hash table.
1437 *
1438 * Return poll add value or else -1 on error.
1439 */
1440static
1441int add_connection(int fd, struct lttng_poll_event *events,
1442 struct lttng_ht *relay_connections_ht)
1443{
1444 int ret;
1445 struct relay_command *relay_connection;
1446
1447 assert(events);
1448 assert(relay_connections_ht);
1449
1450 relay_connection = zmalloc(sizeof(struct relay_command));
1451 if (relay_connection == NULL) {
1452 PERROR("Relay command zmalloc");
1453 goto error;
1454 }
1455
1456 do {
eea7556c 1457 health_code_update();
d3e2ba59
JD
1458 ret = read(fd, relay_connection, sizeof(*relay_connection));
1459 } while (ret < 0 && errno == EINTR);
1460 if (ret < 0 || ret < sizeof(*relay_connection)) {
1461 PERROR("read relay cmd pipe");
1462 goto error_read;
1463 }
1464
1465 lttng_ht_node_init_ulong(&relay_connection->sock_n,
1466 (unsigned long) relay_connection->sock->fd);
1467 rcu_read_lock();
1468 lttng_ht_add_unique_ulong(relay_connections_ht,
1469 &relay_connection->sock_n);
1470 rcu_read_unlock();
1471
1472 return lttng_poll_add(events, relay_connection->sock->fd,
1473 LPOLLIN | LPOLLRDHUP);
1474
1475error_read:
1476 free(relay_connection);
1477error:
1478 return -1;
1479}
1480
1481static
1482void deferred_free_connection(struct rcu_head *head)
1483{
1484 struct relay_command *relay_connection =
1485 caa_container_of(head, struct relay_command, rcu_node);
1486
1487 if (relay_connection->session &&
1488 relay_connection->session->viewer_attached > 0) {
1489 relay_connection->session->viewer_attached--;
1490 }
1491 lttcomm_destroy_sock(relay_connection->sock);
1492 free(relay_connection);
1493}
1494
1495static
1496void deferred_free_viewer_stream(struct rcu_head *head)
1497{
1498 struct relay_viewer_stream *stream =
1499 caa_container_of(head, struct relay_viewer_stream, rcu_node);
1500
1501 if (stream->ctf_trace) {
1502 uatomic_dec(&stream->ctf_trace->refcount);
1503 assert(uatomic_read(&stream->ctf_trace->refcount) >= 0);
1504 if (uatomic_read(&stream->ctf_trace->refcount) == 0) {
1505 DBG("Freeing ctf_trace %" PRIu64, stream->ctf_trace->id);
1506 free(stream->ctf_trace);
1507 }
1508 }
1509
1510 free(stream->path_name);
1511 free(stream->channel_name);
1512 free(stream);
1513}
1514
1515static
b92fdc2b 1516void viewer_del_streams(uint64_t session_id)
d3e2ba59
JD
1517{
1518 int ret;
1519 struct relay_viewer_stream *stream;
1520 struct lttng_ht_node_u64 *node;
1521 struct lttng_ht_iter iter;
1522
d3e2ba59
JD
1523 rcu_read_lock();
1524 cds_lfht_for_each_entry(viewer_streams_ht->ht, &iter.iter, node, node) {
eea7556c
MD
1525 health_code_update();
1526
d3e2ba59
JD
1527 node = lttng_ht_iter_get_node_u64(&iter);
1528 if (!node) {
1529 continue;
1530 }
1531
1532 stream = caa_container_of(node, struct relay_viewer_stream, stream_n);
b92fdc2b 1533 if (stream->session_id != session_id) {
d3e2ba59
JD
1534 continue;
1535 }
1536
1537 if (stream->read_fd > 0) {
1538 ret = close(stream->read_fd);
1539 if (ret < 0) {
1540 PERROR("close read_fd");
1541 }
1542 }
1543 if (stream->index_read_fd > 0) {
1544 ret = close(stream->index_read_fd);
1545 if (ret < 0) {
1546 PERROR("close index_read_fd");
1547 }
1548 }
1549 if (stream->metadata_flag && stream->ctf_trace) {
1550 stream->ctf_trace->metadata_sent = 0;
1551 }
1552 ret = lttng_ht_del(viewer_streams_ht, &iter);
1553 assert(!ret);
1554 call_rcu(&stream->rcu_node, deferred_free_viewer_stream);
1555 }
1556 rcu_read_unlock();
1557}
1558
1559/*
1560 * Delete and free a connection.
1561 *
1562 * RCU read side lock MUST be acquired.
1563 */
1564static
1565void del_connection(struct lttng_ht *relay_connections_ht,
92c6ca54 1566 struct lttng_ht_iter *iter, struct relay_command *relay_connection)
d3e2ba59
JD
1567{
1568 int ret;
1569
1570 assert(relay_connections_ht);
1571 assert(iter);
1572 assert(relay_connection);
d3e2ba59
JD
1573
1574 ret = lttng_ht_del(relay_connections_ht, iter);
1575 assert(!ret);
1576
b92fdc2b 1577 viewer_del_streams(relay_connection->session_id);
d3e2ba59
JD
1578
1579 call_rcu(&relay_connection->rcu_node, deferred_free_connection);
1580}
1581
1582/*
1583 * This thread does the actual work
1584 */
1585static
1586void *thread_worker(void *data)
1587{
1588 int ret, err = -1;
1589 uint32_t nb_fd;
1590 struct relay_command *relay_connection;
1591 struct lttng_poll_event events;
1592 struct lttng_ht *relay_connections_ht;
1593 struct lttng_ht_node_ulong *node;
1594 struct lttng_ht_iter iter;
1595 struct lttng_viewer_cmd recv_hdr;
1596 struct relay_local_data *relay_ctx = (struct relay_local_data *) data;
1597 struct lttng_ht *sessions_ht = relay_ctx->sessions_ht;
d3e2ba59
JD
1598
1599 DBG("[thread] Live viewer relay worker started");
1600
1601 rcu_register_thread();
1602
eea7556c
MD
1603 health_register(health_relayd, HEALTH_RELAYD_TYPE_LIVE_WORKER);
1604
d3e2ba59
JD
1605 /* table of connections indexed on socket */
1606 relay_connections_ht = lttng_ht_new(0, LTTNG_HT_TYPE_ULONG);
1607 if (!relay_connections_ht) {
1608 goto relay_connections_ht_error;
1609 }
1610
1611 ret = create_thread_poll_set(&events, 2);
1612 if (ret < 0) {
1613 goto error_poll_create;
1614 }
1615
1616 ret = lttng_poll_add(&events, live_relay_cmd_pipe[0], LPOLLIN | LPOLLRDHUP);
1617 if (ret < 0) {
1618 goto error;
1619 }
1620
1621restart:
1622 while (1) {
1623 int i;
1624
eea7556c
MD
1625 health_code_update();
1626
d3e2ba59
JD
1627 /* Infinite blocking call, waiting for transmission */
1628 DBG3("Relayd live viewer worker thread polling...");
eea7556c 1629 health_poll_entry();
d3e2ba59 1630 ret = lttng_poll_wait(&events, -1);
eea7556c 1631 health_poll_exit();
d3e2ba59
JD
1632 if (ret < 0) {
1633 /*
1634 * Restart interrupted system call.
1635 */
1636 if (errno == EINTR) {
1637 goto restart;
1638 }
1639 goto error;
1640 }
1641
1642 nb_fd = ret;
1643
1644 /*
1645 * Process control. The control connection is prioritised so we don't
1646 * starve it with high throughput tracing data on the data
1647 * connection.
1648 */
1649 for (i = 0; i < nb_fd; i++) {
1650 /* Fetch once the poll data */
1651 uint32_t revents = LTTNG_POLL_GETEV(&events, i);
1652 int pollfd = LTTNG_POLL_GETFD(&events, i);
1653
eea7556c
MD
1654 health_code_update();
1655
d3e2ba59
JD
1656 /* Thread quit pipe has been closed. Killing thread. */
1657 ret = check_thread_quit_pipe(pollfd, revents);
1658 if (ret) {
1659 err = 0;
1660 goto exit;
1661 }
1662
1663 /* Inspect the relay cmd pipe for new connection */
1664 if (pollfd == live_relay_cmd_pipe[0]) {
1665 if (revents & (LPOLLERR | LPOLLHUP | LPOLLRDHUP)) {
1666 ERR("Relay live pipe error");
1667 goto error;
1668 } else if (revents & LPOLLIN) {
1669 DBG("Relay live viewer command received");
1670 ret = add_connection(live_relay_cmd_pipe[0],
1671 &events, relay_connections_ht);
1672 if (ret < 0) {
1673 goto error;
1674 }
1675 }
1676 } else if (revents) {
1677 rcu_read_lock();
1678 lttng_ht_lookup(relay_connections_ht,
1679 (void *)((unsigned long) pollfd), &iter);
1680 node = lttng_ht_iter_get_node_ulong(&iter);
1681 if (node == NULL) {
1682 DBG2("Relay viewer sock %d not found", pollfd);
1683 rcu_read_unlock();
1684 goto error;
1685 }
1686 relay_connection = caa_container_of(node, struct relay_command,
1687 sock_n);
1688
1689 if (revents & (LPOLLERR)) {
d3e2ba59
JD
1690 cleanup_poll_connection(&events, pollfd);
1691 del_connection(relay_connections_ht, &iter,
92c6ca54 1692 relay_connection);
d3e2ba59
JD
1693 } else if (revents & (LPOLLHUP | LPOLLRDHUP)) {
1694 DBG("Viewer socket %d hung up", pollfd);
1695 cleanup_poll_connection(&events, pollfd);
1696 del_connection(relay_connections_ht, &iter,
92c6ca54 1697 relay_connection);
d3e2ba59
JD
1698 } else if (revents & LPOLLIN) {
1699 ret = relay_connection->sock->ops->recvmsg(
1700 relay_connection->sock, &recv_hdr,
1701 sizeof(struct lttng_viewer_cmd),
1702 0);
1703 /* connection closed */
1704 if (ret <= 0) {
1705 cleanup_poll_connection(&events, pollfd);
aaec7998 1706 del_connection(relay_connections_ht, &iter,
92c6ca54 1707 relay_connection);
d3e2ba59
JD
1708 DBG("Viewer control connection closed with %d",
1709 pollfd);
1710 } else {
1711 if (relay_connection->session) {
1712 DBG2("Relay viewer worker receiving data for "
1713 "session: %" PRIu64,
1714 relay_connection->session->id);
1715 }
1716 ret = process_control(&recv_hdr, relay_connection,
92c6ca54 1717 sessions_ht);
d3e2ba59
JD
1718 if (ret < 0) {
1719 /* Clear the session on error. */
1720 cleanup_poll_connection(&events, pollfd);
1721 del_connection(relay_connections_ht, &iter,
92c6ca54 1722 relay_connection);
d3e2ba59
JD
1723 DBG("Viewer connection closed with %d", pollfd);
1724 }
1725 }
1726 }
1727 rcu_read_unlock();
1728 }
1729 }
1730 }
1731
1732exit:
1733error:
1734 lttng_poll_clean(&events);
1735
1736 /* empty the hash table and free the memory */
1737 rcu_read_lock();
1738 cds_lfht_for_each_entry(relay_connections_ht->ht, &iter.iter, node, node) {
eea7556c
MD
1739 health_code_update();
1740
d3e2ba59
JD
1741 node = lttng_ht_iter_get_node_ulong(&iter);
1742 if (!node) {
1743 continue;
1744 }
1745
1746 relay_connection = caa_container_of(node, struct relay_command,
1747 sock_n);
92c6ca54 1748 del_connection(relay_connections_ht, &iter, relay_connection);
d3e2ba59
JD
1749 }
1750 rcu_read_unlock();
1751error_poll_create:
1752 lttng_ht_destroy(relay_connections_ht);
1753relay_connections_ht_error:
1754 /* Close relay cmd pipes */
1755 utils_close_pipe(live_relay_cmd_pipe);
1756 if (err) {
1757 DBG("Viewer worker thread exited with error");
1758 }
1759 DBG("Viewer worker thread cleanup complete");
eea7556c
MD
1760 if (err) {
1761 health_error();
1762 ERR("Health error occurred in %s", __func__);
1763 }
1764 health_unregister(health_relayd);
d3e2ba59
JD
1765 stop_threads();
1766 rcu_unregister_thread();
1767 return NULL;
1768}
1769
1770/*
1771 * Create the relay command pipe to wake thread_manage_apps.
1772 * Closed in cleanup().
1773 */
1774static int create_relay_cmd_pipe(void)
1775{
1776 int ret;
1777
1778 ret = utils_create_pipe_cloexec(live_relay_cmd_pipe);
1779
1780 return ret;
1781}
1782
aaec7998 1783void live_stop_threads(void)
d3e2ba59
JD
1784{
1785 int ret;
1786 void *status;
1787
1788 stop_threads();
1789
1790 ret = pthread_join(live_listener_thread, &status);
1791 if (ret != 0) {
1792 PERROR("pthread_join live listener");
1793 goto error; /* join error, exit without cleanup */
1794 }
1795
1796 ret = pthread_join(live_worker_thread, &status);
1797 if (ret != 0) {
1798 PERROR("pthread_join live worker");
1799 goto error; /* join error, exit without cleanup */
1800 }
1801
1802 ret = pthread_join(live_dispatcher_thread, &status);
1803 if (ret != 0) {
1804 PERROR("pthread_join live dispatcher");
1805 goto error; /* join error, exit without cleanup */
1806 }
1807
1808 cleanup();
1809
1810error:
1811 return;
1812}
1813
1814/*
1815 * main
1816 */
1817int live_start_threads(struct lttng_uri *uri,
42415026 1818 struct relay_local_data *relay_ctx, int quit_pipe[2])
d3e2ba59
JD
1819{
1820 int ret = 0;
1821 void *status;
1822 int is_root;
1823
1824 assert(uri);
1825 live_uri = uri;
1826
42415026
DG
1827 live_thread_quit_pipe[0] = quit_pipe[0];
1828 live_thread_quit_pipe[1] = quit_pipe[1];
d3e2ba59
JD
1829
1830 /* Check if daemon is UID = 0 */
1831 is_root = !getuid();
1832
1833 if (!is_root) {
1834 if (live_uri->port < 1024) {
1835 ERR("Need to be root to use ports < 1024");
1836 ret = -1;
1837 goto exit;
1838 }
1839 }
1840
1841 /* Setup the thread apps communication pipe. */
1842 if ((ret = create_relay_cmd_pipe()) < 0) {
1843 goto exit;
1844 }
1845
1846 /* Init relay command queue. */
1847 cds_wfq_init(&viewer_cmd_queue.queue);
1848
1849 /* Set up max poll set size */
1850 lttng_poll_set_max_size();
1851
1852 /* Setup the dispatcher thread */
1853 ret = pthread_create(&live_dispatcher_thread, NULL,
1854 thread_dispatcher, (void *) NULL);
1855 if (ret != 0) {
1856 PERROR("pthread_create viewer dispatcher");
1857 goto exit_dispatcher;
1858 }
1859
1860 /* Setup the worker thread */
1861 ret = pthread_create(&live_worker_thread, NULL,
1862 thread_worker, relay_ctx);
1863 if (ret != 0) {
1864 PERROR("pthread_create viewer worker");
1865 goto exit_worker;
1866 }
1867
1868 /* Setup the listener thread */
1869 ret = pthread_create(&live_listener_thread, NULL,
1870 thread_listener, (void *) NULL);
1871 if (ret != 0) {
1872 PERROR("pthread_create viewer listener");
1873 goto exit_listener;
1874 }
1875
1876 ret = 0;
1877 goto end;
1878
1879exit_listener:
1880 ret = pthread_join(live_listener_thread, &status);
1881 if (ret != 0) {
1882 PERROR("pthread_join live listener");
1883 goto error; /* join error, exit without cleanup */
1884 }
1885
1886exit_worker:
1887 ret = pthread_join(live_worker_thread, &status);
1888 if (ret != 0) {
1889 PERROR("pthread_join live worker");
1890 goto error; /* join error, exit without cleanup */
1891 }
1892
1893exit_dispatcher:
1894 ret = pthread_join(live_dispatcher_thread, &status);
1895 if (ret != 0) {
1896 PERROR("pthread_join live dispatcher");
1897 goto error; /* join error, exit without cleanup */
1898 }
1899
1900exit:
1901 cleanup();
1902
1903end:
1904error:
1905 return ret;
1906}
This page took 0.099709 seconds and 4 git commands to generate.