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