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