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