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