Commit | Line | Data |
---|---|---|
d3e2ba59 JD |
1 | /* |
2 | * Copyright (C) 2013 - Julien Desfossez <jdesfossez@efficios.com> | |
3 | * David Goulet <dgoulet@efficios.com> | |
7591bab1 | 4 | * 2015 - Mathieu Desnoyers <mathieu.desnoyers@efficios.com> |
d3e2ba59 JD |
5 | * |
6 | * This program is free software; you can redistribute it and/or modify | |
7 | * it under the terms of the GNU General Public License, version 2 only, | |
8 | * as published by the Free Software Foundation. | |
9 | * | |
10 | * This program is distributed in the hope that it will be useful, but WITHOUT | |
11 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or | |
12 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for | |
13 | * more details. | |
14 | * | |
15 | * You should have received a copy of the GNU General Public License along | |
16 | * with this program; if not, write to the Free Software Foundation, Inc., | |
17 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. | |
18 | */ | |
19 | ||
6c1c0768 | 20 | #define _LGPL_SOURCE |
d3e2ba59 JD |
21 | #include <getopt.h> |
22 | #include <grp.h> | |
23 | #include <limits.h> | |
24 | #include <pthread.h> | |
25 | #include <signal.h> | |
26 | #include <stdio.h> | |
27 | #include <stdlib.h> | |
28 | #include <string.h> | |
29 | #include <sys/mman.h> | |
30 | #include <sys/mount.h> | |
31 | #include <sys/resource.h> | |
32 | #include <sys/socket.h> | |
33 | #include <sys/stat.h> | |
34 | #include <sys/types.h> | |
35 | #include <sys/wait.h> | |
36 | #include <inttypes.h> | |
37 | #include <urcu/futex.h> | |
38 | #include <urcu/uatomic.h> | |
7591bab1 | 39 | #include <urcu/rculist.h> |
d3e2ba59 JD |
40 | #include <unistd.h> |
41 | #include <fcntl.h> | |
d3e2ba59 JD |
42 | |
43 | #include <lttng/lttng.h> | |
44 | #include <common/common.h> | |
45 | #include <common/compat/poll.h> | |
46 | #include <common/compat/socket.h> | |
f263b7fd | 47 | #include <common/compat/endian.h> |
d3e2ba59 JD |
48 | #include <common/defaults.h> |
49 | #include <common/futex.h> | |
2f8f53af | 50 | #include <common/index/index.h> |
d3e2ba59 JD |
51 | #include <common/sessiond-comm/sessiond-comm.h> |
52 | #include <common/sessiond-comm/inet.h> | |
53 | #include <common/sessiond-comm/relayd.h> | |
54 | #include <common/uri.h> | |
55 | #include <common/utils.h> | |
87bcbe91 | 56 | #include <common/fd-tracker/utils.h> |
d3e2ba59 JD |
57 | |
58 | #include "cmd.h" | |
59 | #include "live.h" | |
60 | #include "lttng-relayd.h" | |
d3e2ba59 | 61 | #include "utils.h" |
eea7556c | 62 | #include "health-relayd.h" |
9b5e0863 | 63 | #include "testpoint.h" |
2f8f53af | 64 | #include "viewer-stream.h" |
2a174661 DG |
65 | #include "stream.h" |
66 | #include "session.h" | |
67 | #include "ctf-trace.h" | |
58eb9381 | 68 | #include "connection.h" |
7591bab1 MD |
69 | #include "viewer-session.h" |
70 | ||
71 | #define SESSION_BUF_DEFAULT_COUNT 16 | |
d3e2ba59 JD |
72 | |
73 | static struct lttng_uri *live_uri; | |
74 | ||
d3e2ba59 JD |
75 | /* |
76 | * This pipe is used to inform the worker thread that a command is queued and | |
77 | * ready to be processed. | |
78 | */ | |
58eb9381 | 79 | static int live_conn_pipe[2] = { -1, -1 }; |
d3e2ba59 JD |
80 | |
81 | /* Shared between threads */ | |
82 | static int live_dispatch_thread_exit; | |
83 | ||
84 | static pthread_t live_listener_thread; | |
85 | static pthread_t live_dispatcher_thread; | |
86 | static pthread_t live_worker_thread; | |
87 | ||
88 | /* | |
89 | * Relay command queue. | |
90 | * | |
91 | * The live_thread_listener and live_thread_dispatcher communicate with this | |
92 | * queue. | |
93 | */ | |
58eb9381 | 94 | static struct relay_conn_queue viewer_conn_queue; |
d3e2ba59 JD |
95 | |
96 | static uint64_t last_relay_viewer_session_id; | |
7591bab1 MD |
97 | static pthread_mutex_t last_relay_viewer_session_id_lock = |
98 | PTHREAD_MUTEX_INITIALIZER; | |
d3e2ba59 JD |
99 | |
100 | /* | |
101 | * Cleanup the daemon | |
102 | */ | |
103 | static | |
178a0557 | 104 | void cleanup_relayd_live(void) |
d3e2ba59 JD |
105 | { |
106 | DBG("Cleaning up"); | |
107 | ||
d3e2ba59 JD |
108 | free(live_uri); |
109 | } | |
110 | ||
2f8f53af DG |
111 | /* |
112 | * Receive a request buffer using a given socket, destination allocated buffer | |
113 | * of length size. | |
114 | * | |
115 | * Return the size of the received message or else a negative value on error | |
116 | * with errno being set by recvmsg() syscall. | |
117 | */ | |
118 | static | |
119 | ssize_t recv_request(struct lttcomm_sock *sock, void *buf, size_t size) | |
120 | { | |
121 | ssize_t ret; | |
122 | ||
2f8f53af DG |
123 | ret = sock->ops->recvmsg(sock, buf, size, 0); |
124 | if (ret < 0 || ret != size) { | |
125 | if (ret == 0) { | |
126 | /* Orderly shutdown. Not necessary to print an error. */ | |
127 | DBG("Socket %d did an orderly shutdown", sock->fd); | |
128 | } else { | |
129 | ERR("Relay failed to receive request."); | |
130 | } | |
131 | ret = -1; | |
132 | } | |
133 | ||
134 | return ret; | |
135 | } | |
136 | ||
137 | /* | |
138 | * Send a response buffer using a given socket, source allocated buffer of | |
139 | * length size. | |
140 | * | |
141 | * Return the size of the sent message or else a negative value on error with | |
142 | * errno being set by sendmsg() syscall. | |
143 | */ | |
144 | static | |
145 | ssize_t send_response(struct lttcomm_sock *sock, void *buf, size_t size) | |
146 | { | |
147 | ssize_t ret; | |
148 | ||
2f8f53af DG |
149 | ret = sock->ops->sendmsg(sock, buf, size, 0); |
150 | if (ret < 0) { | |
151 | ERR("Relayd failed to send response."); | |
152 | } | |
153 | ||
154 | return ret; | |
155 | } | |
156 | ||
157 | /* | |
f04a971b JD |
158 | * Atomically check if new streams got added in one of the sessions attached |
159 | * and reset the flag to 0. | |
2f8f53af DG |
160 | * |
161 | * Returns 1 if new streams got added, 0 if nothing changed, a negative value | |
162 | * on error. | |
163 | */ | |
164 | static | |
f04a971b | 165 | int check_new_streams(struct relay_connection *conn) |
2f8f53af | 166 | { |
2f8f53af | 167 | struct relay_session *session; |
f04a971b JD |
168 | unsigned long current_val; |
169 | int ret = 0; | |
2f8f53af | 170 | |
f04a971b JD |
171 | if (!conn->viewer_session) { |
172 | goto end; | |
173 | } | |
7591bab1 MD |
174 | rcu_read_lock(); |
175 | cds_list_for_each_entry_rcu(session, | |
176 | &conn->viewer_session->session_list, | |
177 | viewer_session_node) { | |
178 | if (!session_get(session)) { | |
179 | continue; | |
180 | } | |
f04a971b JD |
181 | current_val = uatomic_cmpxchg(&session->new_streams, 1, 0); |
182 | ret = current_val; | |
7591bab1 | 183 | session_put(session); |
f04a971b JD |
184 | if (ret == 1) { |
185 | goto end; | |
186 | } | |
2f8f53af | 187 | } |
f04a971b | 188 | end: |
7591bab1 | 189 | rcu_read_unlock(); |
2f8f53af DG |
190 | return ret; |
191 | } | |
192 | ||
193 | /* | |
194 | * Send viewer streams to the given socket. The ignore_sent_flag indicates if | |
195 | * this function should ignore the sent flag or not. | |
196 | * | |
197 | * Return 0 on success or else a negative value. | |
198 | */ | |
199 | static | |
200 | ssize_t send_viewer_streams(struct lttcomm_sock *sock, | |
c06fdd95 | 201 | uint64_t session_id, unsigned int ignore_sent_flag) |
2f8f53af DG |
202 | { |
203 | ssize_t ret; | |
204 | struct lttng_viewer_stream send_stream; | |
205 | struct lttng_ht_iter iter; | |
206 | struct relay_viewer_stream *vstream; | |
207 | ||
2f8f53af DG |
208 | rcu_read_lock(); |
209 | ||
210 | cds_lfht_for_each_entry(viewer_streams_ht->ht, &iter.iter, vstream, | |
211 | stream_n.node) { | |
2a174661 DG |
212 | struct ctf_trace *ctf_trace; |
213 | ||
2f8f53af DG |
214 | health_code_update(); |
215 | ||
7591bab1 MD |
216 | if (!viewer_stream_get(vstream)) { |
217 | continue; | |
218 | } | |
219 | ||
220 | pthread_mutex_lock(&vstream->stream->lock); | |
2f8f53af | 221 | /* Ignore if not the same session. */ |
c06fdd95 | 222 | if (vstream->stream->trace->session->id != session_id || |
2f8f53af | 223 | (!ignore_sent_flag && vstream->sent_flag)) { |
7591bab1 MD |
224 | pthread_mutex_unlock(&vstream->stream->lock); |
225 | viewer_stream_put(vstream); | |
2f8f53af DG |
226 | continue; |
227 | } | |
228 | ||
7591bab1 MD |
229 | ctf_trace = vstream->stream->trace; |
230 | send_stream.id = htobe64(vstream->stream->stream_handle); | |
2a174661 | 231 | send_stream.ctf_trace_id = htobe64(ctf_trace->id); |
7591bab1 MD |
232 | send_stream.metadata_flag = htobe32( |
233 | vstream->stream->is_metadata); | |
f8f011fb MD |
234 | if (lttng_strncpy(send_stream.path_name, vstream->path_name, |
235 | sizeof(send_stream.path_name))) { | |
236 | pthread_mutex_unlock(&vstream->stream->lock); | |
237 | viewer_stream_put(vstream); | |
238 | ret = -1; /* Error. */ | |
239 | goto end_unlock; | |
240 | } | |
241 | if (lttng_strncpy(send_stream.channel_name, | |
242 | vstream->channel_name, | |
243 | sizeof(send_stream.channel_name))) { | |
244 | pthread_mutex_unlock(&vstream->stream->lock); | |
245 | viewer_stream_put(vstream); | |
246 | ret = -1; /* Error. */ | |
247 | goto end_unlock; | |
248 | } | |
2f8f53af | 249 | |
7591bab1 MD |
250 | DBG("Sending stream %" PRIu64 " to viewer", |
251 | vstream->stream->stream_handle); | |
252 | vstream->sent_flag = 1; | |
253 | pthread_mutex_unlock(&vstream->stream->lock); | |
254 | ||
2f8f53af | 255 | ret = send_response(sock, &send_stream, sizeof(send_stream)); |
7591bab1 | 256 | viewer_stream_put(vstream); |
2f8f53af DG |
257 | if (ret < 0) { |
258 | goto end_unlock; | |
259 | } | |
2f8f53af DG |
260 | } |
261 | ||
262 | ret = 0; | |
263 | ||
264 | end_unlock: | |
265 | rcu_read_unlock(); | |
266 | return ret; | |
267 | } | |
268 | ||
269 | /* | |
270 | * Create every viewer stream possible for the given session with the seek | |
271 | * type. Three counters *can* be return which are in order the total amount of | |
272 | * viewer stream of the session, the number of unsent stream and the number of | |
273 | * stream created. Those counters can be NULL and thus will be ignored. | |
274 | * | |
c06fdd95 JG |
275 | * session must be locked to ensure that we see either none or all initial |
276 | * streams for a session, but no intermediate state.. | |
277 | * | |
2f8f53af DG |
278 | * Return 0 on success or else a negative value. |
279 | */ | |
b66a15d1 JG |
280 | static int make_viewer_streams(struct relay_session *session, |
281 | struct lttng_trace_chunk *viewer_trace_chunk, | |
282 | enum lttng_viewer_seek seek_t, | |
283 | uint32_t *nb_total, | |
284 | uint32_t *nb_unsent, | |
285 | uint32_t *nb_created, | |
286 | bool *closed) | |
2f8f53af DG |
287 | { |
288 | int ret; | |
2f8f53af | 289 | struct lttng_ht_iter iter; |
2a174661 | 290 | struct ctf_trace *ctf_trace; |
2f8f53af DG |
291 | |
292 | assert(session); | |
c06fdd95 | 293 | ASSERT_LOCKED(session->lock); |
2f8f53af | 294 | |
dbd6665b JG |
295 | if (!viewer_trace_chunk) { |
296 | ERR("Internal error: viewer session associated with session \"%s\" has a NULL trace chunk", | |
297 | session->session_name); | |
298 | ret = -1; | |
299 | goto error; | |
300 | } | |
301 | ||
bddf80e4 MD |
302 | if (session->connection_closed) { |
303 | *closed = true; | |
304 | } | |
305 | ||
2f8f53af | 306 | /* |
7591bab1 MD |
307 | * Create viewer streams for relay streams that are ready to be |
308 | * used for a the given session id only. | |
2f8f53af | 309 | */ |
2a174661 DG |
310 | rcu_read_lock(); |
311 | cds_lfht_for_each_entry(session->ctf_traces_ht->ht, &iter.iter, ctf_trace, | |
312 | node.node) { | |
123ed7c2 | 313 | bool trace_has_metadata_stream = false; |
2a174661 | 314 | struct relay_stream *stream; |
2f8f53af DG |
315 | |
316 | health_code_update(); | |
317 | ||
7591bab1 | 318 | if (!ctf_trace_get(ctf_trace)) { |
2f8f53af DG |
319 | continue; |
320 | } | |
321 | ||
123ed7c2 FD |
322 | /* |
323 | * Iterate over all the streams of the trace to see if we have a | |
324 | * metadata stream. | |
325 | */ | |
326 | cds_list_for_each_entry_rcu( | |
327 | stream, &ctf_trace->stream_list, stream_node) | |
328 | { | |
329 | if (stream->is_metadata) { | |
330 | trace_has_metadata_stream = true; | |
331 | break; | |
332 | } | |
333 | } | |
334 | ||
335 | /* | |
336 | * If there is no metadata stream in this trace at the moment | |
337 | * and we never sent one to the viewer, skip the trace. We | |
338 | * accept that the viewer will not see this trace at all. | |
339 | */ | |
340 | if (!trace_has_metadata_stream && | |
341 | !ctf_trace->metadata_stream_sent_to_viewer) { | |
3d7708ff JG |
342 | ctf_trace_put(ctf_trace); |
343 | continue; | |
123ed7c2 FD |
344 | } |
345 | ||
7591bab1 | 346 | cds_list_for_each_entry_rcu(stream, &ctf_trace->stream_list, stream_node) { |
2a174661 DG |
347 | struct relay_viewer_stream *vstream; |
348 | ||
7591bab1 | 349 | if (!stream_get(stream)) { |
2a174661 DG |
350 | continue; |
351 | } | |
7591bab1 | 352 | /* |
d812ecb9 | 353 | * stream published is protected by the session lock. |
7591bab1 MD |
354 | */ |
355 | if (!stream->published) { | |
356 | goto next; | |
357 | } | |
358 | vstream = viewer_stream_get_by_id(stream->stream_handle); | |
2f8f53af | 359 | if (!vstream) { |
123ed7c2 FD |
360 | /* |
361 | * Save that we sent the metadata stream to the | |
362 | * viewer. So that we know what trace the viewer | |
363 | * is aware of. | |
364 | */ | |
365 | if (stream->is_metadata) { | |
366 | ctf_trace->metadata_stream_sent_to_viewer = | |
367 | true; | |
368 | } | |
b66a15d1 JG |
369 | vstream = viewer_stream_create(stream, |
370 | viewer_trace_chunk, seek_t); | |
2a174661 DG |
371 | if (!vstream) { |
372 | ret = -1; | |
7591bab1 MD |
373 | ctf_trace_put(ctf_trace); |
374 | stream_put(stream); | |
2a174661 DG |
375 | goto error_unlock; |
376 | } | |
2a174661 DG |
377 | |
378 | if (nb_created) { | |
379 | /* Update number of created stream counter. */ | |
380 | (*nb_created)++; | |
381 | } | |
2229a09c MD |
382 | /* |
383 | * Ensure a self-reference is preserved even | |
384 | * after we have put our local reference. | |
385 | */ | |
862d3a3b MD |
386 | if (!viewer_stream_get(vstream)) { |
387 | ERR("Unable to get self-reference on viewer stream, logic error."); | |
388 | abort(); | |
389 | } | |
7591bab1 MD |
390 | } else { |
391 | if (!vstream->sent_flag && nb_unsent) { | |
392 | /* Update number of unsent stream counter. */ | |
393 | (*nb_unsent)++; | |
394 | } | |
2f8f53af | 395 | } |
2a174661 DG |
396 | /* Update number of total stream counter. */ |
397 | if (nb_total) { | |
2229a09c MD |
398 | if (stream->is_metadata) { |
399 | if (!stream->closed || | |
400 | stream->metadata_received > vstream->metadata_sent) { | |
401 | (*nb_total)++; | |
402 | } | |
403 | } else { | |
404 | if (!stream->closed || | |
a8f9f353 | 405 | !(((int64_t) (stream->prev_data_seq - stream->last_net_seq_num)) >= 0)) { |
2229a09c MD |
406 | |
407 | (*nb_total)++; | |
408 | } | |
409 | } | |
2f8f53af | 410 | } |
2229a09c MD |
411 | /* Put local reference. */ |
412 | viewer_stream_put(vstream); | |
7591bab1 MD |
413 | next: |
414 | stream_put(stream); | |
2f8f53af | 415 | } |
7591bab1 | 416 | ctf_trace_put(ctf_trace); |
2f8f53af DG |
417 | } |
418 | ||
419 | ret = 0; | |
420 | ||
421 | error_unlock: | |
2a174661 | 422 | rcu_read_unlock(); |
dbd6665b | 423 | error: |
2f8f53af DG |
424 | return ret; |
425 | } | |
426 | ||
b4aacfdc | 427 | int relayd_live_stop(void) |
d3e2ba59 | 428 | { |
b4aacfdc | 429 | /* Stop dispatch thread */ |
d3e2ba59 | 430 | CMM_STORE_SHARED(live_dispatch_thread_exit, 1); |
58eb9381 | 431 | futex_nto1_wake(&viewer_conn_queue.futex); |
b4aacfdc | 432 | return 0; |
d3e2ba59 JD |
433 | } |
434 | ||
d3e2ba59 JD |
435 | /* |
436 | * Create a poll set with O_CLOEXEC and add the thread quit pipe to the set. | |
437 | */ | |
438 | static | |
23f940ff JG |
439 | int create_named_thread_poll_set(struct lttng_poll_event *events, |
440 | int size, const char *name) | |
d3e2ba59 JD |
441 | { |
442 | int ret; | |
443 | ||
444 | if (events == NULL || size == 0) { | |
445 | ret = -1; | |
446 | goto error; | |
447 | } | |
448 | ||
23f940ff JG |
449 | ret = fd_tracker_util_poll_create(the_fd_tracker, |
450 | name, events, 1, LTTNG_CLOEXEC); | |
d3e2ba59 JD |
451 | |
452 | /* Add quit pipe */ | |
bcf4a440 | 453 | ret = lttng_poll_add(events, thread_quit_pipe[0], LPOLLIN | LPOLLERR); |
d3e2ba59 JD |
454 | if (ret < 0) { |
455 | goto error; | |
456 | } | |
457 | ||
458 | return 0; | |
459 | ||
460 | error: | |
461 | return ret; | |
462 | } | |
463 | ||
464 | /* | |
465 | * Check if the thread quit pipe was triggered. | |
466 | * | |
467 | * Return 1 if it was triggered else 0; | |
468 | */ | |
469 | static | |
bcf4a440 | 470 | int check_thread_quit_pipe(int fd, uint32_t events) |
d3e2ba59 | 471 | { |
bcf4a440 | 472 | if (fd == thread_quit_pipe[0] && (events & LPOLLIN)) { |
d3e2ba59 JD |
473 | return 1; |
474 | } | |
475 | ||
476 | return 0; | |
477 | } | |
478 | ||
8855795d JG |
479 | static |
480 | int create_sock(void *data, int *out_fd) | |
481 | { | |
482 | int ret; | |
483 | struct lttcomm_sock *sock = data; | |
484 | ||
485 | ret = lttcomm_create_sock(sock); | |
486 | if (ret < 0) { | |
487 | goto end; | |
488 | } | |
489 | ||
490 | *out_fd = sock->fd; | |
491 | end: | |
492 | return ret; | |
493 | } | |
494 | ||
495 | static | |
496 | int close_sock(void *data, int *in_fd) | |
497 | { | |
498 | struct lttcomm_sock *sock = data; | |
499 | ||
500 | return sock->ops->close(sock); | |
501 | } | |
502 | ||
29555a78 JG |
503 | static int accept_sock(void *data, int *out_fd) |
504 | { | |
505 | int ret = 0; | |
506 | /* Socks is an array of in_sock, out_sock. */ | |
507 | struct lttcomm_sock **socks = data; | |
508 | struct lttcomm_sock *in_sock = socks[0]; | |
509 | ||
510 | socks[1] = in_sock->ops->accept(in_sock); | |
511 | if (!socks[1]) { | |
512 | ret = -1; | |
513 | goto end; | |
514 | } | |
515 | *out_fd = socks[1]->fd; | |
516 | end: | |
517 | return ret; | |
518 | } | |
519 | ||
520 | static | |
521 | struct lttcomm_sock *accept_live_sock(struct lttcomm_sock *listening_sock, | |
522 | const char *name) | |
523 | { | |
524 | int out_fd, ret; | |
525 | struct lttcomm_sock *socks[2] = { listening_sock, NULL }; | |
526 | struct lttcomm_sock *new_sock = NULL; | |
527 | ||
528 | ret = fd_tracker_open_unsuspendable_fd(the_fd_tracker, &out_fd, | |
529 | (const char **) &name, 1, accept_sock, &socks); | |
530 | if (ret) { | |
531 | goto end; | |
532 | } | |
533 | new_sock = socks[1]; | |
534 | DBG("%s accepted, socket %d", name, new_sock->fd); | |
535 | end: | |
536 | return new_sock; | |
537 | } | |
538 | ||
d3e2ba59 JD |
539 | /* |
540 | * Create and init socket from uri. | |
541 | */ | |
542 | static | |
8855795d | 543 | struct lttcomm_sock *init_socket(struct lttng_uri *uri, const char *name) |
d3e2ba59 | 544 | { |
8855795d | 545 | int ret, sock_fd; |
d3e2ba59 | 546 | struct lttcomm_sock *sock = NULL; |
8855795d JG |
547 | char uri_str[LTTNG_PATH_MAX]; |
548 | char *formated_name = NULL; | |
d3e2ba59 JD |
549 | |
550 | sock = lttcomm_alloc_sock_from_uri(uri); | |
551 | if (sock == NULL) { | |
552 | ERR("Allocating socket"); | |
553 | goto error; | |
554 | } | |
555 | ||
8855795d JG |
556 | /* |
557 | * Don't fail to create the socket if the name can't be built as it is | |
558 | * only used for debugging purposes. | |
559 | */ | |
560 | ret = uri_to_str_url(uri, uri_str, sizeof(uri_str)); | |
561 | uri_str[sizeof(uri_str) - 1] = '\0'; | |
562 | if (ret >= 0) { | |
563 | ret = asprintf(&formated_name, "%s socket @ %s", name, | |
564 | uri_str); | |
565 | if (ret < 0) { | |
566 | formated_name = NULL; | |
567 | } | |
d3e2ba59 | 568 | } |
8855795d JG |
569 | |
570 | ret = fd_tracker_open_unsuspendable_fd(the_fd_tracker, &sock_fd, | |
571 | (const char **) (formated_name ? &formated_name : NULL), | |
572 | 1, create_sock, sock); | |
573 | free(formated_name); | |
574 | DBG("Listening on %s socket %d", name, sock->fd); | |
d3e2ba59 JD |
575 | |
576 | ret = sock->ops->bind(sock); | |
577 | if (ret < 0) { | |
2288467f | 578 | PERROR("Failed to bind lttng-live socket"); |
d3e2ba59 JD |
579 | goto error; |
580 | } | |
581 | ||
582 | ret = sock->ops->listen(sock, -1); | |
583 | if (ret < 0) { | |
584 | goto error; | |
585 | ||
586 | } | |
587 | ||
588 | return sock; | |
589 | ||
590 | error: | |
591 | if (sock) { | |
592 | lttcomm_destroy_sock(sock); | |
593 | } | |
594 | return NULL; | |
595 | } | |
596 | ||
597 | /* | |
598 | * This thread manages the listening for new connections on the network | |
599 | */ | |
600 | static | |
601 | void *thread_listener(void *data) | |
602 | { | |
603 | int i, ret, pollfd, err = -1; | |
d3e2ba59 JD |
604 | uint32_t revents, nb_fd; |
605 | struct lttng_poll_event events; | |
606 | struct lttcomm_sock *live_control_sock; | |
607 | ||
608 | DBG("[thread] Relay live listener started"); | |
609 | ||
eea7556c MD |
610 | health_register(health_relayd, HEALTH_RELAYD_TYPE_LIVE_LISTENER); |
611 | ||
612 | health_code_update(); | |
613 | ||
8855795d | 614 | live_control_sock = init_socket(live_uri, "Live listener"); |
d3e2ba59 JD |
615 | if (!live_control_sock) { |
616 | goto error_sock_control; | |
617 | } | |
618 | ||
fb4d42ab | 619 | /* Pass 2 as size here for the thread quit pipe and control sockets. */ |
23f940ff JG |
620 | ret = create_named_thread_poll_set(&events, 2, |
621 | "Live listener thread epoll"); | |
d3e2ba59 JD |
622 | if (ret < 0) { |
623 | goto error_create_poll; | |
624 | } | |
625 | ||
626 | /* Add the control socket */ | |
627 | ret = lttng_poll_add(&events, live_control_sock->fd, LPOLLIN | LPOLLRDHUP); | |
628 | if (ret < 0) { | |
629 | goto error_poll_add; | |
630 | } | |
631 | ||
3fd27398 MD |
632 | lttng_relay_notify_ready(); |
633 | ||
9b5e0863 MD |
634 | if (testpoint(relayd_thread_live_listener)) { |
635 | goto error_testpoint; | |
636 | } | |
637 | ||
d3e2ba59 | 638 | while (1) { |
eea7556c MD |
639 | health_code_update(); |
640 | ||
d3e2ba59 JD |
641 | DBG("Listener accepting live viewers connections"); |
642 | ||
643 | restart: | |
eea7556c | 644 | health_poll_entry(); |
d3e2ba59 | 645 | ret = lttng_poll_wait(&events, -1); |
eea7556c | 646 | health_poll_exit(); |
d3e2ba59 JD |
647 | if (ret < 0) { |
648 | /* | |
649 | * Restart interrupted system call. | |
650 | */ | |
651 | if (errno == EINTR) { | |
652 | goto restart; | |
653 | } | |
654 | goto error; | |
655 | } | |
656 | nb_fd = ret; | |
657 | ||
658 | DBG("Relay new viewer connection received"); | |
659 | for (i = 0; i < nb_fd; i++) { | |
eea7556c MD |
660 | health_code_update(); |
661 | ||
d3e2ba59 JD |
662 | /* Fetch once the poll data */ |
663 | revents = LTTNG_POLL_GETEV(&events, i); | |
664 | pollfd = LTTNG_POLL_GETFD(&events, i); | |
665 | ||
666 | /* Thread quit pipe has been closed. Killing thread. */ | |
bcf4a440 | 667 | ret = check_thread_quit_pipe(pollfd, revents); |
d3e2ba59 JD |
668 | if (ret) { |
669 | err = 0; | |
670 | goto exit; | |
671 | } | |
672 | ||
03e43155 | 673 | if (revents & LPOLLIN) { |
d3e2ba59 | 674 | /* |
7591bab1 MD |
675 | * A new connection is requested, therefore a |
676 | * viewer connection is allocated in this | |
677 | * thread, enqueued to a global queue and | |
678 | * dequeued (and freed) in the worker thread. | |
d3e2ba59 | 679 | */ |
58eb9381 DG |
680 | int val = 1; |
681 | struct relay_connection *new_conn; | |
d3e2ba59 JD |
682 | struct lttcomm_sock *newsock; |
683 | ||
29555a78 JG |
684 | newsock = accept_live_sock(live_control_sock, |
685 | "Live socket to client"); | |
d3e2ba59 JD |
686 | if (!newsock) { |
687 | PERROR("accepting control sock"); | |
d3e2ba59 JD |
688 | goto error; |
689 | } | |
690 | DBG("Relay viewer connection accepted socket %d", newsock->fd); | |
58eb9381 | 691 | |
d3e2ba59 | 692 | ret = setsockopt(newsock->fd, SOL_SOCKET, SO_REUSEADDR, &val, |
58eb9381 | 693 | sizeof(val)); |
d3e2ba59 JD |
694 | if (ret < 0) { |
695 | PERROR("setsockopt inet"); | |
696 | lttcomm_destroy_sock(newsock); | |
d3e2ba59 JD |
697 | goto error; |
698 | } | |
7591bab1 MD |
699 | new_conn = connection_create(newsock, RELAY_CONNECTION_UNKNOWN); |
700 | if (!new_conn) { | |
701 | lttcomm_destroy_sock(newsock); | |
702 | goto error; | |
703 | } | |
704 | /* Ownership assumed by the connection. */ | |
705 | newsock = NULL; | |
d3e2ba59 | 706 | |
58eb9381 | 707 | /* Enqueue request for the dispatcher thread. */ |
8bdee6e2 SM |
708 | cds_wfcq_enqueue(&viewer_conn_queue.head, &viewer_conn_queue.tail, |
709 | &new_conn->qnode); | |
d3e2ba59 JD |
710 | |
711 | /* | |
7591bab1 MD |
712 | * Wake the dispatch queue futex. |
713 | * Implicit memory barrier with the | |
714 | * exchange in cds_wfcq_enqueue. | |
d3e2ba59 | 715 | */ |
58eb9381 | 716 | futex_nto1_wake(&viewer_conn_queue.futex); |
03e43155 MD |
717 | } else if (revents & (LPOLLERR | LPOLLHUP | LPOLLRDHUP)) { |
718 | ERR("socket poll error"); | |
719 | goto error; | |
720 | } else { | |
721 | ERR("Unexpected poll events %u for sock %d", revents, pollfd); | |
722 | goto error; | |
d3e2ba59 JD |
723 | } |
724 | } | |
725 | } | |
726 | ||
727 | exit: | |
728 | error: | |
729 | error_poll_add: | |
9b5e0863 | 730 | error_testpoint: |
23f940ff | 731 | (void) fd_tracker_util_poll_clean(the_fd_tracker, &events); |
d3e2ba59 JD |
732 | error_create_poll: |
733 | if (live_control_sock->fd >= 0) { | |
8855795d JG |
734 | int sock_fd = live_control_sock->fd; |
735 | ||
736 | ret = fd_tracker_close_unsuspendable_fd(the_fd_tracker, | |
737 | &sock_fd, 1, close_sock, | |
738 | live_control_sock); | |
d3e2ba59 JD |
739 | if (ret) { |
740 | PERROR("close"); | |
741 | } | |
8855795d | 742 | live_control_sock->fd = -1; |
d3e2ba59 JD |
743 | } |
744 | lttcomm_destroy_sock(live_control_sock); | |
745 | error_sock_control: | |
746 | if (err) { | |
eea7556c | 747 | health_error(); |
d3e2ba59 JD |
748 | DBG("Live viewer listener thread exited with error"); |
749 | } | |
eea7556c | 750 | health_unregister(health_relayd); |
d3e2ba59 | 751 | DBG("Live viewer listener thread cleanup complete"); |
b4aacfdc MD |
752 | if (lttng_relay_stop_threads()) { |
753 | ERR("Error stopping threads"); | |
178a0557 | 754 | } |
d3e2ba59 JD |
755 | return NULL; |
756 | } | |
757 | ||
758 | /* | |
759 | * This thread manages the dispatching of the requests to worker threads | |
760 | */ | |
761 | static | |
762 | void *thread_dispatcher(void *data) | |
763 | { | |
6cd525e8 MD |
764 | int err = -1; |
765 | ssize_t ret; | |
8bdee6e2 | 766 | struct cds_wfcq_node *node; |
58eb9381 | 767 | struct relay_connection *conn = NULL; |
d3e2ba59 JD |
768 | |
769 | DBG("[thread] Live viewer relay dispatcher started"); | |
770 | ||
eea7556c MD |
771 | health_register(health_relayd, HEALTH_RELAYD_TYPE_LIVE_DISPATCHER); |
772 | ||
9b5e0863 MD |
773 | if (testpoint(relayd_thread_live_dispatcher)) { |
774 | goto error_testpoint; | |
775 | } | |
776 | ||
eea7556c MD |
777 | health_code_update(); |
778 | ||
0ed3b1a8 | 779 | for (;;) { |
eea7556c MD |
780 | health_code_update(); |
781 | ||
d3e2ba59 | 782 | /* Atomically prepare the queue futex */ |
58eb9381 | 783 | futex_nto1_prepare(&viewer_conn_queue.futex); |
d3e2ba59 | 784 | |
0ed3b1a8 MD |
785 | if (CMM_LOAD_SHARED(live_dispatch_thread_exit)) { |
786 | break; | |
787 | } | |
788 | ||
d3e2ba59 | 789 | do { |
eea7556c MD |
790 | health_code_update(); |
791 | ||
d3e2ba59 | 792 | /* Dequeue commands */ |
8bdee6e2 SM |
793 | node = cds_wfcq_dequeue_blocking(&viewer_conn_queue.head, |
794 | &viewer_conn_queue.tail); | |
d3e2ba59 JD |
795 | if (node == NULL) { |
796 | DBG("Woken up but nothing in the live-viewer " | |
797 | "relay command queue"); | |
798 | /* Continue thread execution */ | |
799 | break; | |
800 | } | |
58eb9381 | 801 | conn = caa_container_of(node, struct relay_connection, qnode); |
d3e2ba59 | 802 | DBG("Dispatching viewer request waiting on sock %d", |
58eb9381 | 803 | conn->sock->fd); |
d3e2ba59 JD |
804 | |
805 | /* | |
7591bab1 MD |
806 | * Inform worker thread of the new request. This |
807 | * call is blocking so we can be assured that | |
808 | * the data will be read at some point in time | |
809 | * or wait to the end of the world :) | |
d3e2ba59 | 810 | */ |
58eb9381 DG |
811 | ret = lttng_write(live_conn_pipe[1], &conn, sizeof(conn)); |
812 | if (ret < 0) { | |
813 | PERROR("write conn pipe"); | |
7591bab1 | 814 | connection_put(conn); |
d3e2ba59 JD |
815 | goto error; |
816 | } | |
817 | } while (node != NULL); | |
818 | ||
819 | /* Futex wait on queue. Blocking call on futex() */ | |
eea7556c | 820 | health_poll_entry(); |
58eb9381 | 821 | futex_nto1_wait(&viewer_conn_queue.futex); |
eea7556c | 822 | health_poll_exit(); |
d3e2ba59 JD |
823 | } |
824 | ||
eea7556c MD |
825 | /* Normal exit, no error */ |
826 | err = 0; | |
827 | ||
d3e2ba59 | 828 | error: |
9b5e0863 | 829 | error_testpoint: |
eea7556c MD |
830 | if (err) { |
831 | health_error(); | |
832 | ERR("Health error occurred in %s", __func__); | |
833 | } | |
834 | health_unregister(health_relayd); | |
d3e2ba59 | 835 | DBG("Live viewer dispatch thread dying"); |
b4aacfdc MD |
836 | if (lttng_relay_stop_threads()) { |
837 | ERR("Error stopping threads"); | |
178a0557 | 838 | } |
d3e2ba59 JD |
839 | return NULL; |
840 | } | |
841 | ||
842 | /* | |
843 | * Establish connection with the viewer and check the versions. | |
844 | * | |
845 | * Return 0 on success or else negative value. | |
846 | */ | |
847 | static | |
58eb9381 | 848 | int viewer_connect(struct relay_connection *conn) |
d3e2ba59 JD |
849 | { |
850 | int ret; | |
851 | struct lttng_viewer_connect reply, msg; | |
852 | ||
58eb9381 | 853 | conn->version_check_done = 1; |
d3e2ba59 | 854 | |
eea7556c MD |
855 | health_code_update(); |
856 | ||
2f8f53af DG |
857 | DBG("Viewer is establishing a connection to the relayd."); |
858 | ||
58eb9381 | 859 | ret = recv_request(conn->sock, &msg, sizeof(msg)); |
2f8f53af | 860 | if (ret < 0) { |
d3e2ba59 JD |
861 | goto end; |
862 | } | |
863 | ||
eea7556c MD |
864 | health_code_update(); |
865 | ||
f46b2ce6 | 866 | memset(&reply, 0, sizeof(reply)); |
d3e2ba59 JD |
867 | reply.major = RELAYD_VERSION_COMM_MAJOR; |
868 | reply.minor = RELAYD_VERSION_COMM_MINOR; | |
869 | ||
870 | /* Major versions must be the same */ | |
871 | if (reply.major != be32toh(msg.major)) { | |
2f8f53af DG |
872 | DBG("Incompatible major versions ([relayd] %u vs [client] %u)", |
873 | reply.major, be32toh(msg.major)); | |
72180669 | 874 | ret = -1; |
d3e2ba59 JD |
875 | goto end; |
876 | } | |
877 | ||
58eb9381 | 878 | conn->major = reply.major; |
d3e2ba59 JD |
879 | /* We adapt to the lowest compatible version */ |
880 | if (reply.minor <= be32toh(msg.minor)) { | |
58eb9381 | 881 | conn->minor = reply.minor; |
d3e2ba59 | 882 | } else { |
58eb9381 | 883 | conn->minor = be32toh(msg.minor); |
d3e2ba59 JD |
884 | } |
885 | ||
c4e361a4 | 886 | if (be32toh(msg.type) == LTTNG_VIEWER_CLIENT_COMMAND) { |
58eb9381 | 887 | conn->type = RELAY_VIEWER_COMMAND; |
c4e361a4 | 888 | } else if (be32toh(msg.type) == LTTNG_VIEWER_CLIENT_NOTIFICATION) { |
58eb9381 | 889 | conn->type = RELAY_VIEWER_NOTIFICATION; |
d3e2ba59 JD |
890 | } else { |
891 | ERR("Unknown connection type : %u", be32toh(msg.type)); | |
892 | ret = -1; | |
893 | goto end; | |
894 | } | |
895 | ||
896 | reply.major = htobe32(reply.major); | |
897 | reply.minor = htobe32(reply.minor); | |
58eb9381 | 898 | if (conn->type == RELAY_VIEWER_COMMAND) { |
93b4787b | 899 | /* |
7591bab1 MD |
900 | * Increment outside of htobe64 macro, because the argument can |
901 | * be used more than once within the macro, and thus the | |
902 | * operation may be undefined. | |
93b4787b | 903 | */ |
7591bab1 | 904 | pthread_mutex_lock(&last_relay_viewer_session_id_lock); |
93b4787b | 905 | last_relay_viewer_session_id++; |
7591bab1 | 906 | pthread_mutex_unlock(&last_relay_viewer_session_id_lock); |
93b4787b | 907 | reply.viewer_session_id = htobe64(last_relay_viewer_session_id); |
d3e2ba59 | 908 | } |
eea7556c MD |
909 | |
910 | health_code_update(); | |
911 | ||
58eb9381 | 912 | ret = send_response(conn->sock, &reply, sizeof(reply)); |
d3e2ba59 | 913 | if (ret < 0) { |
2f8f53af | 914 | goto end; |
d3e2ba59 JD |
915 | } |
916 | ||
eea7556c MD |
917 | health_code_update(); |
918 | ||
58eb9381 | 919 | DBG("Version check done using protocol %u.%u", conn->major, conn->minor); |
d3e2ba59 JD |
920 | ret = 0; |
921 | ||
922 | end: | |
923 | return ret; | |
924 | } | |
925 | ||
926 | /* | |
927 | * Send the viewer the list of current sessions. | |
7591bab1 MD |
928 | * We need to create a copy of the hash table content because otherwise |
929 | * we cannot assume the number of entries stays the same between getting | |
930 | * the number of HT elements and iteration over the HT. | |
d3e2ba59 JD |
931 | * |
932 | * Return 0 on success or else a negative value. | |
933 | */ | |
934 | static | |
58eb9381 | 935 | int viewer_list_sessions(struct relay_connection *conn) |
d3e2ba59 | 936 | { |
5f10c6b1 | 937 | int ret = 0; |
d3e2ba59 | 938 | struct lttng_viewer_list_sessions session_list; |
d3e2ba59 | 939 | struct lttng_ht_iter iter; |
d3e2ba59 | 940 | struct relay_session *session; |
7591bab1 MD |
941 | struct lttng_viewer_session *send_session_buf = NULL; |
942 | uint32_t buf_count = SESSION_BUF_DEFAULT_COUNT; | |
943 | uint32_t count = 0; | |
d3e2ba59 JD |
944 | |
945 | DBG("List sessions received"); | |
946 | ||
7591bab1 MD |
947 | send_session_buf = zmalloc(SESSION_BUF_DEFAULT_COUNT * sizeof(*send_session_buf)); |
948 | if (!send_session_buf) { | |
949 | return -1; | |
d3e2ba59 JD |
950 | } |
951 | ||
7591bab1 MD |
952 | rcu_read_lock(); |
953 | cds_lfht_for_each_entry(sessions_ht->ht, &iter.iter, session, | |
2f8f53af | 954 | session_n.node) { |
7591bab1 | 955 | struct lttng_viewer_session *send_session; |
d3e2ba59 | 956 | |
eea7556c MD |
957 | health_code_update(); |
958 | ||
d995f382 | 959 | pthread_mutex_lock(&session->lock); |
7e0a4379 JR |
960 | if (session->connection_closed) { |
961 | /* Skip closed session */ | |
d995f382 JG |
962 | goto next_session; |
963 | } | |
964 | if (!session->current_trace_chunk) { | |
965 | /* | |
966 | * Skip un-attachable session. It is either | |
967 | * being destroyed or has not had a trace | |
968 | * chunk created against it yet. | |
969 | */ | |
970 | goto next_session; | |
7e0a4379 JR |
971 | } |
972 | ||
7591bab1 MD |
973 | if (count >= buf_count) { |
974 | struct lttng_viewer_session *newbuf; | |
975 | uint32_t new_buf_count = buf_count << 1; | |
eea7556c | 976 | |
7591bab1 MD |
977 | newbuf = realloc(send_session_buf, |
978 | new_buf_count * sizeof(*send_session_buf)); | |
979 | if (!newbuf) { | |
980 | ret = -1; | |
d995f382 | 981 | goto break_loop; |
7591bab1 MD |
982 | } |
983 | send_session_buf = newbuf; | |
984 | buf_count = new_buf_count; | |
6763619c | 985 | } |
7591bab1 | 986 | send_session = &send_session_buf[count]; |
bfc3fb17 MD |
987 | if (lttng_strncpy(send_session->session_name, |
988 | session->session_name, | |
989 | sizeof(send_session->session_name))) { | |
990 | ret = -1; | |
d995f382 | 991 | goto break_loop; |
bfc3fb17 MD |
992 | } |
993 | if (lttng_strncpy(send_session->hostname, session->hostname, | |
994 | sizeof(send_session->hostname))) { | |
995 | ret = -1; | |
d995f382 | 996 | goto break_loop; |
bfc3fb17 | 997 | } |
7591bab1 MD |
998 | send_session->id = htobe64(session->id); |
999 | send_session->live_timer = htobe32(session->live_timer); | |
1000 | if (session->viewer_attached) { | |
1001 | send_session->clients = htobe32(1); | |
1002 | } else { | |
1003 | send_session->clients = htobe32(0); | |
d227d5bd | 1004 | } |
7591bab1 MD |
1005 | send_session->streams = htobe32(session->stream_count); |
1006 | count++; | |
d995f382 JG |
1007 | next_session: |
1008 | pthread_mutex_unlock(&session->lock); | |
1009 | continue; | |
1010 | break_loop: | |
1011 | pthread_mutex_unlock(&session->lock); | |
1012 | break; | |
7591bab1 MD |
1013 | } |
1014 | rcu_read_unlock(); | |
5f10c6b1 JG |
1015 | if (ret < 0) { |
1016 | goto end_free; | |
1017 | } | |
d227d5bd | 1018 | |
7591bab1 | 1019 | session_list.sessions_count = htobe32(count); |
d227d5bd | 1020 | |
7591bab1 | 1021 | health_code_update(); |
d227d5bd | 1022 | |
7591bab1 MD |
1023 | ret = send_response(conn->sock, &session_list, sizeof(session_list)); |
1024 | if (ret < 0) { | |
1025 | goto end_free; | |
d227d5bd | 1026 | } |
d227d5bd | 1027 | |
7591bab1 | 1028 | health_code_update(); |
d227d5bd | 1029 | |
7591bab1 MD |
1030 | ret = send_response(conn->sock, send_session_buf, |
1031 | count * sizeof(*send_session_buf)); | |
1032 | if (ret < 0) { | |
1033 | goto end_free; | |
d227d5bd | 1034 | } |
7591bab1 | 1035 | health_code_update(); |
d227d5bd | 1036 | |
7591bab1 MD |
1037 | ret = 0; |
1038 | end_free: | |
1039 | free(send_session_buf); | |
1040 | return ret; | |
d227d5bd JD |
1041 | } |
1042 | ||
80e8027a | 1043 | /* |
7591bab1 | 1044 | * Send the viewer the list of current streams. |
80e8027a JD |
1045 | */ |
1046 | static | |
58eb9381 | 1047 | int viewer_get_new_streams(struct relay_connection *conn) |
80e8027a JD |
1048 | { |
1049 | int ret, send_streams = 0; | |
7591bab1 | 1050 | uint32_t nb_created = 0, nb_unsent = 0, nb_streams = 0, nb_total = 0; |
80e8027a JD |
1051 | struct lttng_viewer_new_streams_request request; |
1052 | struct lttng_viewer_new_streams_response response; | |
b66a15d1 | 1053 | struct relay_session *session = NULL; |
6763619c | 1054 | uint64_t session_id; |
bddf80e4 | 1055 | bool closed = false; |
80e8027a | 1056 | |
58eb9381 | 1057 | assert(conn); |
80e8027a JD |
1058 | |
1059 | DBG("Get new streams received"); | |
1060 | ||
80e8027a JD |
1061 | health_code_update(); |
1062 | ||
2f8f53af | 1063 | /* Receive the request from the connected client. */ |
58eb9381 | 1064 | ret = recv_request(conn->sock, &request, sizeof(request)); |
2f8f53af | 1065 | if (ret < 0) { |
80e8027a JD |
1066 | goto error; |
1067 | } | |
6763619c | 1068 | session_id = be64toh(request.session_id); |
80e8027a JD |
1069 | |
1070 | health_code_update(); | |
1071 | ||
53efb85a MD |
1072 | memset(&response, 0, sizeof(response)); |
1073 | ||
7591bab1 | 1074 | session = session_get_by_id(session_id); |
2f8f53af | 1075 | if (!session) { |
6763619c | 1076 | DBG("Relay session %" PRIu64 " not found", session_id); |
c4e361a4 | 1077 | response.status = htobe32(LTTNG_VIEWER_NEW_STREAMS_ERR); |
80e8027a JD |
1078 | goto send_reply; |
1079 | } | |
1080 | ||
7591bab1 | 1081 | if (!viewer_session_is_attached(conn->viewer_session, session)) { |
c4e361a4 | 1082 | response.status = htobe32(LTTNG_VIEWER_NEW_STREAMS_ERR); |
80e8027a JD |
1083 | goto send_reply; |
1084 | } | |
1085 | ||
c06fdd95 | 1086 | pthread_mutex_lock(&session->lock); |
b66a15d1 JG |
1087 | ret = make_viewer_streams(session, |
1088 | conn->viewer_session->current_trace_chunk, | |
1089 | LTTNG_VIEWER_SEEK_LAST, &nb_total, &nb_unsent, | |
bddf80e4 | 1090 | &nb_created, &closed); |
2f8f53af | 1091 | if (ret < 0) { |
b66a15d1 | 1092 | goto error_unlock_session; |
2f8f53af | 1093 | } |
3d0bbd40 JG |
1094 | send_streams = 1; |
1095 | response.status = htobe32(LTTNG_VIEWER_NEW_STREAMS_OK); | |
b66a15d1 | 1096 | |
2f8f53af DG |
1097 | /* Only send back the newly created streams with the unsent ones. */ |
1098 | nb_streams = nb_created + nb_unsent; | |
80e8027a JD |
1099 | response.streams_count = htobe32(nb_streams); |
1100 | ||
4479f682 | 1101 | /* |
2229a09c MD |
1102 | * If the session is closed, HUP when there are no more streams |
1103 | * with data. | |
4479f682 | 1104 | */ |
bddf80e4 | 1105 | if (closed && nb_total == 0) { |
4479f682 | 1106 | send_streams = 0; |
bddf80e4 | 1107 | response.streams_count = 0; |
4479f682 | 1108 | response.status = htobe32(LTTNG_VIEWER_NEW_STREAMS_HUP); |
3d0bbd40 | 1109 | goto send_reply_unlock; |
4479f682 | 1110 | } |
3d0bbd40 JG |
1111 | send_reply_unlock: |
1112 | pthread_mutex_unlock(&session->lock); | |
4479f682 | 1113 | |
80e8027a JD |
1114 | send_reply: |
1115 | health_code_update(); | |
58eb9381 | 1116 | ret = send_response(conn->sock, &response, sizeof(response)); |
80e8027a | 1117 | if (ret < 0) { |
7591bab1 | 1118 | goto end_put_session; |
80e8027a JD |
1119 | } |
1120 | health_code_update(); | |
1121 | ||
1122 | /* | |
7591bab1 MD |
1123 | * Unknown or empty session, just return gracefully, the viewer |
1124 | * knows what is happening. | |
80e8027a JD |
1125 | */ |
1126 | if (!send_streams || !nb_streams) { | |
1127 | ret = 0; | |
7591bab1 | 1128 | goto end_put_session; |
80e8027a JD |
1129 | } |
1130 | ||
2f8f53af | 1131 | /* |
7591bab1 MD |
1132 | * Send stream and *DON'T* ignore the sent flag so every viewer |
1133 | * streams that were not sent from that point will be sent to | |
1134 | * the viewer. | |
2f8f53af | 1135 | */ |
c06fdd95 | 1136 | ret = send_viewer_streams(conn->sock, session_id, 0); |
2f8f53af | 1137 | if (ret < 0) { |
7591bab1 | 1138 | goto end_put_session; |
80e8027a JD |
1139 | } |
1140 | ||
7591bab1 MD |
1141 | end_put_session: |
1142 | if (session) { | |
1143 | session_put(session); | |
1144 | } | |
80e8027a JD |
1145 | error: |
1146 | return ret; | |
b66a15d1 JG |
1147 | error_unlock_session: |
1148 | pthread_mutex_unlock(&session->lock); | |
1149 | session_put(session); | |
1150 | return ret; | |
80e8027a JD |
1151 | } |
1152 | ||
d3e2ba59 JD |
1153 | /* |
1154 | * Send the viewer the list of current sessions. | |
1155 | */ | |
1156 | static | |
58eb9381 | 1157 | int viewer_attach_session(struct relay_connection *conn) |
d3e2ba59 | 1158 | { |
2f8f53af DG |
1159 | int send_streams = 0; |
1160 | ssize_t ret; | |
80e8027a | 1161 | uint32_t nb_streams = 0; |
2f8f53af | 1162 | enum lttng_viewer_seek seek_type; |
d3e2ba59 JD |
1163 | struct lttng_viewer_attach_session_request request; |
1164 | struct lttng_viewer_attach_session_response response; | |
7591bab1 | 1165 | struct relay_session *session = NULL; |
dbd6665b | 1166 | enum lttng_viewer_attach_return_code viewer_attach_status; |
bddf80e4 | 1167 | bool closed = false; |
c06fdd95 | 1168 | uint64_t session_id; |
d3e2ba59 | 1169 | |
58eb9381 | 1170 | assert(conn); |
d3e2ba59 | 1171 | |
eea7556c MD |
1172 | health_code_update(); |
1173 | ||
2f8f53af | 1174 | /* Receive the request from the connected client. */ |
58eb9381 | 1175 | ret = recv_request(conn->sock, &request, sizeof(request)); |
2f8f53af | 1176 | if (ret < 0) { |
d3e2ba59 JD |
1177 | goto error; |
1178 | } | |
1179 | ||
c06fdd95 | 1180 | session_id = be64toh(request.session_id); |
eea7556c MD |
1181 | health_code_update(); |
1182 | ||
53efb85a MD |
1183 | memset(&response, 0, sizeof(response)); |
1184 | ||
c3b7390b JD |
1185 | if (!conn->viewer_session) { |
1186 | DBG("Client trying to attach before creating a live viewer session"); | |
1187 | response.status = htobe32(LTTNG_VIEWER_ATTACH_NO_SESSION); | |
1188 | goto send_reply; | |
1189 | } | |
1190 | ||
c06fdd95 | 1191 | session = session_get_by_id(session_id); |
2f8f53af | 1192 | if (!session) { |
c06fdd95 | 1193 | DBG("Relay session %" PRIu64 " not found", session_id); |
c4e361a4 | 1194 | response.status = htobe32(LTTNG_VIEWER_ATTACH_UNK); |
d3e2ba59 JD |
1195 | goto send_reply; |
1196 | } | |
c06fdd95 | 1197 | DBG("Attach session ID %" PRIu64 " received", session_id); |
d3e2ba59 | 1198 | |
c06fdd95 | 1199 | pthread_mutex_lock(&session->lock); |
79a42980 JG |
1200 | if (!session->current_trace_chunk) { |
1201 | /* | |
1202 | * Session is either being destroyed or it never had a trace | |
1203 | * chunk created against it. | |
1204 | */ | |
1205 | DBG("Session requested by live client has no current trace chunk, returning unknown session"); | |
1206 | response.status = htobe32(LTTNG_VIEWER_ATTACH_UNK); | |
1207 | goto send_reply; | |
1208 | } | |
7591bab1 | 1209 | if (session->live_timer == 0) { |
d3e2ba59 | 1210 | DBG("Not live session"); |
c4e361a4 | 1211 | response.status = htobe32(LTTNG_VIEWER_ATTACH_NOT_LIVE); |
d3e2ba59 | 1212 | goto send_reply; |
7591bab1 MD |
1213 | } |
1214 | ||
1215 | send_streams = 1; | |
dbd6665b JG |
1216 | viewer_attach_status = viewer_session_attach(conn->viewer_session, |
1217 | session); | |
1218 | if (viewer_attach_status != LTTNG_VIEWER_ATTACH_OK) { | |
1219 | response.status = htobe32(viewer_attach_status); | |
7591bab1 | 1220 | goto send_reply; |
d3e2ba59 JD |
1221 | } |
1222 | ||
1223 | switch (be32toh(request.seek)) { | |
c4e361a4 JD |
1224 | case LTTNG_VIEWER_SEEK_BEGINNING: |
1225 | case LTTNG_VIEWER_SEEK_LAST: | |
7591bab1 | 1226 | response.status = htobe32(LTTNG_VIEWER_ATTACH_OK); |
2f8f53af | 1227 | seek_type = be32toh(request.seek); |
d3e2ba59 JD |
1228 | break; |
1229 | default: | |
1230 | ERR("Wrong seek parameter"); | |
c4e361a4 | 1231 | response.status = htobe32(LTTNG_VIEWER_ATTACH_SEEK_ERR); |
d3e2ba59 JD |
1232 | send_streams = 0; |
1233 | goto send_reply; | |
1234 | } | |
1235 | ||
b66a15d1 JG |
1236 | ret = make_viewer_streams(session, |
1237 | conn->viewer_session->current_trace_chunk, seek_type, | |
1238 | &nb_streams, NULL, NULL, &closed); | |
2f8f53af | 1239 | if (ret < 0) { |
7591bab1 | 1240 | goto end_put_session; |
d3e2ba59 | 1241 | } |
c06fdd95 JG |
1242 | pthread_mutex_unlock(&session->lock); |
1243 | session_put(session); | |
1244 | session = NULL; | |
1245 | ||
1246 | response.streams_count = htobe32(nb_streams); | |
bddf80e4 MD |
1247 | /* |
1248 | * If the session is closed when the viewer is attaching, it | |
1249 | * means some of the streams may have been concurrently removed, | |
1250 | * so we don't allow the viewer to attach, even if there are | |
1251 | * streams available. | |
1252 | */ | |
1253 | if (closed) { | |
1254 | send_streams = 0; | |
1255 | response.streams_count = 0; | |
06079f15 | 1256 | response.status = htobe32(LTTNG_VIEWER_ATTACH_UNK); |
bddf80e4 MD |
1257 | goto send_reply; |
1258 | } | |
1259 | ||
d3e2ba59 | 1260 | send_reply: |
eea7556c | 1261 | health_code_update(); |
58eb9381 | 1262 | ret = send_response(conn->sock, &response, sizeof(response)); |
d3e2ba59 | 1263 | if (ret < 0) { |
7591bab1 | 1264 | goto end_put_session; |
d3e2ba59 | 1265 | } |
eea7556c | 1266 | health_code_update(); |
d3e2ba59 JD |
1267 | |
1268 | /* | |
7591bab1 MD |
1269 | * Unknown or empty session, just return gracefully, the viewer |
1270 | * knows what is happening. | |
d3e2ba59 | 1271 | */ |
157df586 | 1272 | if (!send_streams || !nb_streams) { |
d3e2ba59 | 1273 | ret = 0; |
7591bab1 | 1274 | goto end_put_session; |
d3e2ba59 JD |
1275 | } |
1276 | ||
2f8f53af | 1277 | /* Send stream and ignore the sent flag. */ |
c06fdd95 | 1278 | ret = send_viewer_streams(conn->sock, session_id, 1); |
2f8f53af | 1279 | if (ret < 0) { |
7591bab1 | 1280 | goto end_put_session; |
d3e2ba59 | 1281 | } |
d3e2ba59 | 1282 | |
7591bab1 MD |
1283 | end_put_session: |
1284 | if (session) { | |
c06fdd95 | 1285 | pthread_mutex_unlock(&session->lock); |
7591bab1 MD |
1286 | session_put(session); |
1287 | } | |
4a9daf17 JD |
1288 | error: |
1289 | return ret; | |
1290 | } | |
1291 | ||
878c34cf DG |
1292 | /* |
1293 | * Open the index file if needed for the given vstream. | |
1294 | * | |
f8f3885c MD |
1295 | * If an index file is successfully opened, the vstream will set it as its |
1296 | * current index file. | |
878c34cf DG |
1297 | * |
1298 | * Return 0 on success, a negative value on error (-ENOENT if not ready yet). | |
7591bab1 MD |
1299 | * |
1300 | * Called with rstream lock held. | |
878c34cf DG |
1301 | */ |
1302 | static int try_open_index(struct relay_viewer_stream *vstream, | |
1303 | struct relay_stream *rstream) | |
1304 | { | |
1305 | int ret = 0; | |
ebb29c10 JG |
1306 | const uint32_t connection_major = rstream->trace->session->major; |
1307 | const uint32_t connection_minor = rstream->trace->session->minor; | |
3ff5c5db | 1308 | enum lttng_trace_chunk_status chunk_status; |
878c34cf | 1309 | |
f8f3885c | 1310 | if (vstream->index_file) { |
878c34cf DG |
1311 | goto end; |
1312 | } | |
1313 | ||
1314 | /* | |
7591bab1 | 1315 | * First time, we open the index file and at least one index is ready. |
878c34cf | 1316 | */ |
a44ca2ca | 1317 | if (rstream->index_received_seqcount == 0) { |
878c34cf DG |
1318 | ret = -ENOENT; |
1319 | goto end; | |
1320 | } | |
3ff5c5db | 1321 | chunk_status = lttng_index_file_create_from_trace_chunk_read_only( |
b66a15d1 | 1322 | vstream->stream_file.trace_chunk, rstream->path_name, |
ebb29c10 JG |
1323 | rstream->channel_name, rstream->tracefile_size, |
1324 | vstream->current_tracefile_id, | |
1325 | lttng_to_index_major(connection_major, connection_minor), | |
3ff5c5db MD |
1326 | lttng_to_index_minor(connection_major, connection_minor), |
1327 | true, &vstream->index_file); | |
1328 | if (chunk_status != LTTNG_TRACE_CHUNK_STATUS_OK) { | |
1329 | if (chunk_status == LTTNG_TRACE_CHUNK_STATUS_NO_FILE) { | |
1330 | ret = -ENOENT; | |
1331 | } else { | |
1332 | ret = -1; | |
1333 | } | |
878c34cf DG |
1334 | } |
1335 | ||
1336 | end: | |
1337 | return ret; | |
1338 | } | |
1339 | ||
1340 | /* | |
7591bab1 MD |
1341 | * Check the status of the index for the given stream. This function |
1342 | * updates the index structure if needed and can put (close) the vstream | |
1343 | * in the HUP situation. | |
1344 | * | |
1345 | * Return 0 means that we can proceed with the index. A value of 1 means | |
1346 | * that the index has been updated and is ready to be sent to the | |
1347 | * client. A negative value indicates an error that can't be handled. | |
878c34cf | 1348 | * |
7591bab1 | 1349 | * Called with rstream lock held. |
878c34cf DG |
1350 | */ |
1351 | static int check_index_status(struct relay_viewer_stream *vstream, | |
1352 | struct relay_stream *rstream, struct ctf_trace *trace, | |
1353 | struct lttng_viewer_index *index) | |
1354 | { | |
1355 | int ret; | |
1356 | ||
68c40154 MD |
1357 | DBG("Check index status: index_received_seqcount %" PRIu64 " " |
1358 | "index_sent_seqcount %" PRIu64 " " | |
1359 | "for stream %" PRIu64, | |
1360 | rstream->index_received_seqcount, | |
1361 | vstream->index_sent_seqcount, | |
1362 | vstream->stream->stream_handle); | |
797bc362 | 1363 | if ((trace->session->connection_closed || rstream->closed) |
a44ca2ca MD |
1364 | && rstream->index_received_seqcount |
1365 | == vstream->index_sent_seqcount) { | |
797bc362 MD |
1366 | /* |
1367 | * Last index sent and session connection or relay | |
1368 | * stream are closed. | |
1369 | */ | |
7591bab1 MD |
1370 | index->status = htobe32(LTTNG_VIEWER_INDEX_HUP); |
1371 | goto hup; | |
1372 | } else if (rstream->beacon_ts_end != -1ULL && | |
b0d240a2 MD |
1373 | (rstream->index_received_seqcount == 0 || |
1374 | (vstream->index_sent_seqcount != 0 && | |
a44ca2ca | 1375 | rstream->index_received_seqcount |
b0d240a2 | 1376 | <= vstream->index_sent_seqcount))) { |
7591bab1 MD |
1377 | /* |
1378 | * We've received a synchronization beacon and the last index | |
1379 | * available has been sent, the index for now is inactive. | |
1380 | * | |
1381 | * In this case, we have received a beacon which allows us to | |
1382 | * inform the client of a time interval during which we can | |
1383 | * guarantee that there are no events to read (and never will | |
1384 | * be). | |
b0d240a2 MD |
1385 | * |
1386 | * The sent seqcount can grow higher than receive seqcount on | |
1387 | * clear because the rotation performed by clear will push | |
1388 | * the index_sent_seqcount ahead (see | |
1389 | * viewer_stream_sync_tracefile_array_tail) and skip over | |
1390 | * packet sequence numbers. | |
7591bab1 MD |
1391 | */ |
1392 | index->status = htobe32(LTTNG_VIEWER_INDEX_INACTIVE); | |
1393 | index->timestamp_end = htobe64(rstream->beacon_ts_end); | |
1394 | index->stream_id = htobe64(rstream->ctf_stream_id); | |
68c40154 MD |
1395 | DBG("Check index status: inactive with beacon, for stream %" PRIu64, |
1396 | vstream->stream->stream_handle); | |
7591bab1 | 1397 | goto index_ready; |
b0d240a2 MD |
1398 | } else if (rstream->index_received_seqcount == 0 || |
1399 | (vstream->index_sent_seqcount != 0 && | |
1400 | rstream->index_received_seqcount | |
1401 | <= vstream->index_sent_seqcount)) { | |
7591bab1 | 1402 | /* |
b0d240a2 | 1403 | * This checks whether received <= sent seqcount. In |
a44ca2ca MD |
1404 | * this case, we have not received a beacon. Therefore, |
1405 | * we can only ask the client to retry later. | |
b0d240a2 MD |
1406 | * |
1407 | * The sent seqcount can grow higher than receive seqcount on | |
1408 | * clear because the rotation performed by clear will push | |
1409 | * the index_sent_seqcount ahead (see | |
1410 | * viewer_stream_sync_tracefile_array_tail) and skip over | |
1411 | * packet sequence numbers. | |
7591bab1 MD |
1412 | */ |
1413 | index->status = htobe32(LTTNG_VIEWER_INDEX_RETRY); | |
68c40154 MD |
1414 | DBG("Check index status: retry for stream %" PRIu64, |
1415 | vstream->stream->stream_handle); | |
7591bab1 | 1416 | goto index_ready; |
a44ca2ca MD |
1417 | } else if (!tracefile_array_seq_in_file(rstream->tfa, |
1418 | vstream->current_tracefile_id, | |
1419 | vstream->index_sent_seqcount)) { | |
7591bab1 | 1420 | /* |
a44ca2ca MD |
1421 | * The next index we want to send cannot be read either |
1422 | * because we need to perform a rotation, or due to | |
1423 | * the producer having overwritten its trace file. | |
7591bab1 | 1424 | */ |
a44ca2ca | 1425 | DBG("Viewer stream %" PRIu64 " rotation", |
7591bab1 MD |
1426 | vstream->stream->stream_handle); |
1427 | ret = viewer_stream_rotate(vstream); | |
b0d240a2 | 1428 | if (ret == 1) { |
7591bab1 MD |
1429 | /* EOF across entire stream. */ |
1430 | index->status = htobe32(LTTNG_VIEWER_INDEX_HUP); | |
1431 | goto hup; | |
1432 | } | |
7591bab1 | 1433 | /* |
a44ca2ca MD |
1434 | * If we have been pushed due to overwrite, it |
1435 | * necessarily means there is data that can be read in | |
1436 | * the stream. If we rotated because we reached the end | |
1437 | * of a tracefile, it means the following tracefile | |
1438 | * needs to contain at least one index, else we would | |
1439 | * have already returned LTTNG_VIEWER_INDEX_RETRY to the | |
1440 | * viewer. The updated index_sent_seqcount needs to | |
1441 | * point to a readable index entry now. | |
1442 | * | |
1443 | * In the case where we "rotate" on a single file, we | |
1444 | * can end up in a case where the requested index is | |
1445 | * still unavailable. | |
7591bab1 | 1446 | */ |
a44ca2ca MD |
1447 | if (rstream->tracefile_count == 1 && |
1448 | !tracefile_array_seq_in_file( | |
1449 | rstream->tfa, | |
1450 | vstream->current_tracefile_id, | |
1451 | vstream->index_sent_seqcount)) { | |
1452 | index->status = htobe32(LTTNG_VIEWER_INDEX_RETRY); | |
68c40154 MD |
1453 | DBG("Check index status: retry: " |
1454 | "tracefile array sequence number %" PRIu64 | |
1455 | " not in file for stream %" PRIu64, | |
1456 | vstream->index_sent_seqcount, | |
1457 | vstream->stream->stream_handle); | |
a44ca2ca | 1458 | goto index_ready; |
878c34cf | 1459 | } |
a44ca2ca MD |
1460 | assert(tracefile_array_seq_in_file(rstream->tfa, |
1461 | vstream->current_tracefile_id, | |
1462 | vstream->index_sent_seqcount)); | |
878c34cf | 1463 | } |
a44ca2ca MD |
1464 | /* ret == 0 means successful so we continue. */ |
1465 | ret = 0; | |
878c34cf DG |
1466 | return ret; |
1467 | ||
1468 | hup: | |
7591bab1 | 1469 | viewer_stream_put(vstream); |
878c34cf DG |
1470 | index_ready: |
1471 | return 1; | |
1472 | } | |
1473 | ||
d3e2ba59 JD |
1474 | /* |
1475 | * Send the next index for a stream. | |
1476 | * | |
1477 | * Return 0 on success or else a negative value. | |
1478 | */ | |
1479 | static | |
58eb9381 | 1480 | int viewer_get_next_index(struct relay_connection *conn) |
d3e2ba59 JD |
1481 | { |
1482 | int ret; | |
1483 | struct lttng_viewer_get_next_index request_index; | |
1484 | struct lttng_viewer_index viewer_index; | |
50adc264 | 1485 | struct ctf_packet_index packet_index; |
7591bab1 MD |
1486 | struct relay_viewer_stream *vstream = NULL; |
1487 | struct relay_stream *rstream = NULL; | |
1488 | struct ctf_trace *ctf_trace = NULL; | |
1489 | struct relay_viewer_stream *metadata_viewer_stream = NULL; | |
d3e2ba59 | 1490 | |
58eb9381 | 1491 | assert(conn); |
d3e2ba59 JD |
1492 | |
1493 | DBG("Viewer get next index"); | |
1494 | ||
7591bab1 | 1495 | memset(&viewer_index, 0, sizeof(viewer_index)); |
eea7556c | 1496 | health_code_update(); |
2f8f53af | 1497 | |
58eb9381 | 1498 | ret = recv_request(conn->sock, &request_index, sizeof(request_index)); |
2f8f53af | 1499 | if (ret < 0) { |
d3e2ba59 JD |
1500 | goto end; |
1501 | } | |
eea7556c | 1502 | health_code_update(); |
d3e2ba59 | 1503 | |
7591bab1 | 1504 | vstream = viewer_stream_get_by_id(be64toh(request_index.stream_id)); |
6763619c | 1505 | if (!vstream) { |
a44ca2ca | 1506 | DBG("Client requested index of unknown stream id %" PRIu64, |
9b9f9f94 | 1507 | (uint64_t) be64toh(request_index.stream_id)); |
f1883937 MD |
1508 | viewer_index.status = htobe32(LTTNG_VIEWER_INDEX_ERR); |
1509 | goto send_reply; | |
2a174661 DG |
1510 | } |
1511 | ||
7591bab1 MD |
1512 | /* Use back. ref. Protected by refcounts. */ |
1513 | rstream = vstream->stream; | |
1514 | ctf_trace = rstream->trace; | |
d3e2ba59 | 1515 | |
7591bab1 MD |
1516 | /* metadata_viewer_stream may be NULL. */ |
1517 | metadata_viewer_stream = | |
1518 | ctf_trace_get_viewer_metadata_stream(ctf_trace); | |
2a174661 | 1519 | |
7591bab1 | 1520 | pthread_mutex_lock(&rstream->lock); |
d3e2ba59 JD |
1521 | |
1522 | /* | |
1523 | * The viewer should not ask for index on metadata stream. | |
1524 | */ | |
7591bab1 | 1525 | if (rstream->is_metadata) { |
c4e361a4 | 1526 | viewer_index.status = htobe32(LTTNG_VIEWER_INDEX_HUP); |
d3e2ba59 JD |
1527 | goto send_reply; |
1528 | } | |
1529 | ||
b0d240a2 MD |
1530 | if (rstream->ongoing_rotation.is_set) { |
1531 | /* Rotation is ongoing, try again later. */ | |
1532 | viewer_index.status = htobe32(LTTNG_VIEWER_INDEX_RETRY); | |
1533 | goto send_reply; | |
1534 | } | |
1535 | ||
1536 | if (rstream->trace->session->ongoing_rotation) { | |
1537 | /* Rotation is ongoing, try again later. */ | |
1538 | viewer_index.status = htobe32(LTTNG_VIEWER_INDEX_RETRY); | |
1539 | goto send_reply; | |
1540 | } | |
1541 | ||
1542 | if (rstream->trace_chunk) { | |
1543 | uint64_t rchunk_id, vchunk_id; | |
1544 | ||
1545 | /* | |
1546 | * If the relay stream is not yet closed, ensure the viewer | |
1547 | * chunk matches the relay chunk after clear. | |
1548 | */ | |
1549 | if (lttng_trace_chunk_get_id(rstream->trace_chunk, | |
1550 | &rchunk_id) != LTTNG_TRACE_CHUNK_STATUS_OK) { | |
c4e361a4 | 1551 | viewer_index.status = htobe32(LTTNG_VIEWER_INDEX_ERR); |
b0d240a2 MD |
1552 | goto send_reply; |
1553 | } | |
1554 | if (lttng_trace_chunk_get_id( | |
1555 | conn->viewer_session->current_trace_chunk, | |
1556 | &vchunk_id) != LTTNG_TRACE_CHUNK_STATUS_OK) { | |
1557 | viewer_index.status = htobe32(LTTNG_VIEWER_INDEX_ERR); | |
1558 | goto send_reply; | |
1559 | } | |
1560 | ||
1561 | if (rchunk_id != vchunk_id) { | |
1562 | DBG("Relay and viewer chunk ids differ: " | |
1563 | "rchunk_id %" PRIu64 " vchunk_id %" PRIu64, | |
1564 | rchunk_id, vchunk_id); | |
1565 | ||
1566 | lttng_trace_chunk_put( | |
1567 | conn->viewer_session->current_trace_chunk); | |
1568 | conn->viewer_session->current_trace_chunk = NULL; | |
1569 | ret = viewer_session_set_trace_chunk_copy( | |
1570 | conn->viewer_session, | |
1571 | rstream->trace_chunk); | |
1572 | if (ret) { | |
1573 | viewer_index.status = | |
1574 | htobe32(LTTNG_VIEWER_INDEX_ERR); | |
1575 | goto send_reply; | |
1576 | } | |
d3e2ba59 | 1577 | } |
b0d240a2 MD |
1578 | } |
1579 | if (conn->viewer_session->current_trace_chunk != | |
1580 | vstream->stream_file.trace_chunk) { | |
1581 | bool acquired_reference; | |
1582 | ||
1583 | DBG("Viewer session and viewer stream chunk differ: " | |
1584 | "vsession chunk %p vstream chunk %p", | |
1585 | conn->viewer_session->current_trace_chunk, | |
1586 | vstream->stream_file.trace_chunk); | |
1587 | lttng_trace_chunk_put(vstream->stream_file.trace_chunk); | |
1588 | acquired_reference = lttng_trace_chunk_get(conn->viewer_session->current_trace_chunk); | |
1589 | assert(acquired_reference); | |
1590 | vstream->stream_file.trace_chunk = | |
1591 | conn->viewer_session->current_trace_chunk; | |
1592 | viewer_stream_sync_tracefile_array_tail(vstream); | |
1593 | viewer_stream_close_files(vstream); | |
d3e2ba59 JD |
1594 | } |
1595 | ||
878c34cf | 1596 | ret = check_index_status(vstream, rstream, ctf_trace, &viewer_index); |
878c34cf | 1597 | if (ret < 0) { |
7591bab1 | 1598 | goto error_put; |
878c34cf DG |
1599 | } else if (ret == 1) { |
1600 | /* | |
7591bab1 MD |
1601 | * We have no index to send and check_index_status has populated |
1602 | * viewer_index's status. | |
878c34cf | 1603 | */ |
d3e2ba59 JD |
1604 | goto send_reply; |
1605 | } | |
7591bab1 | 1606 | /* At this point, ret is 0 thus we will be able to read the index. */ |
878c34cf | 1607 | assert(!ret); |
d3e2ba59 | 1608 | |
b0d240a2 MD |
1609 | /* Try to open an index if one is needed for that stream. */ |
1610 | ret = try_open_index(vstream, rstream); | |
1611 | if (ret == -ENOENT) { | |
1612 | if (rstream->closed) { | |
1613 | viewer_index.status = htobe32(LTTNG_VIEWER_INDEX_HUP); | |
1614 | goto send_reply; | |
1615 | } else { | |
1616 | viewer_index.status = htobe32(LTTNG_VIEWER_INDEX_RETRY); | |
1617 | goto send_reply; | |
1618 | } | |
1619 | } | |
1620 | if (ret < 0) { | |
1621 | viewer_index.status = htobe32(LTTNG_VIEWER_INDEX_ERR); | |
1622 | goto send_reply; | |
1623 | } | |
1624 | ||
7591bab1 MD |
1625 | /* |
1626 | * vstream->stream_fd may be NULL if it has been closed by | |
1627 | * tracefile rotation, or if we are at the beginning of the | |
1628 | * stream. We open the data stream file here to protect against | |
1629 | * overwrite caused by tracefile rotation (in association with | |
1630 | * unlink performed before overwrite). | |
1631 | */ | |
ebb29c10 JG |
1632 | if (!vstream->stream_file.fd) { |
1633 | int fd; | |
1634 | char file_path[LTTNG_PATH_MAX]; | |
1635 | enum lttng_trace_chunk_status status; | |
1636 | ||
1637 | ret = utils_stream_file_path(rstream->path_name, | |
1638 | rstream->channel_name, rstream->tracefile_size, | |
1639 | vstream->current_tracefile_id, NULL, file_path, | |
1640 | sizeof(file_path)); | |
7591bab1 MD |
1641 | if (ret < 0) { |
1642 | goto error_put; | |
1643 | } | |
ebb29c10 | 1644 | |
3ff5c5db MD |
1645 | /* |
1646 | * It is possible the the file we are trying to open is | |
1647 | * missing if the stream has been closed (application exits with | |
1648 | * per-pid buffers) and a clear command has been performed. | |
1649 | */ | |
ebb29c10 JG |
1650 | status = lttng_trace_chunk_open_file( |
1651 | vstream->stream_file.trace_chunk, | |
3ff5c5db | 1652 | file_path, O_RDONLY, 0, &fd, true); |
ebb29c10 | 1653 | if (status != LTTNG_TRACE_CHUNK_STATUS_OK) { |
b0d240a2 MD |
1654 | if (status == LTTNG_TRACE_CHUNK_STATUS_NO_FILE && |
1655 | rstream->closed) { | |
1656 | viewer_index.status = htobe32(LTTNG_VIEWER_INDEX_HUP); | |
1657 | goto send_reply; | |
1658 | } | |
ebb29c10 | 1659 | PERROR("Failed to open trace file for viewer stream"); |
7591bab1 MD |
1660 | goto error_put; |
1661 | } | |
ebb29c10 JG |
1662 | vstream->stream_file.fd = stream_fd_create(fd); |
1663 | if (!vstream->stream_file.fd) { | |
1664 | if (close(fd)) { | |
1665 | PERROR("Failed to close viewer stream file"); | |
7591bab1 MD |
1666 | } |
1667 | goto error_put; | |
1668 | } | |
d3e2ba59 JD |
1669 | } |
1670 | ||
f04a971b | 1671 | ret = check_new_streams(conn); |
4a9daf17 | 1672 | if (ret < 0) { |
7591bab1 MD |
1673 | viewer_index.status = htobe32(LTTNG_VIEWER_INDEX_ERR); |
1674 | goto send_reply; | |
4a9daf17 JD |
1675 | } else if (ret == 1) { |
1676 | viewer_index.flags |= LTTNG_VIEWER_FLAG_NEW_STREAM; | |
1677 | } | |
1678 | ||
f8f3885c MD |
1679 | ret = lttng_index_file_read(vstream->index_file, &packet_index); |
1680 | if (ret) { | |
1681 | ERR("Relay error reading index file %d", | |
1682 | vstream->index_file->fd); | |
7591bab1 | 1683 | viewer_index.status = htobe32(LTTNG_VIEWER_INDEX_ERR); |
6b6b9a5a | 1684 | goto send_reply; |
d3e2ba59 | 1685 | } else { |
c4e361a4 | 1686 | viewer_index.status = htobe32(LTTNG_VIEWER_INDEX_OK); |
a44ca2ca | 1687 | vstream->index_sent_seqcount++; |
d3e2ba59 JD |
1688 | } |
1689 | ||
1690 | /* | |
1691 | * Indexes are stored in big endian, no need to switch before sending. | |
1692 | */ | |
7591bab1 MD |
1693 | DBG("Sending viewer index for stream %" PRIu64 " offset %" PRIu64, |
1694 | rstream->stream_handle, | |
9b9f9f94 | 1695 | (uint64_t) be64toh(packet_index.offset)); |
d3e2ba59 JD |
1696 | viewer_index.offset = packet_index.offset; |
1697 | viewer_index.packet_size = packet_index.packet_size; | |
1698 | viewer_index.content_size = packet_index.content_size; | |
1699 | viewer_index.timestamp_begin = packet_index.timestamp_begin; | |
1700 | viewer_index.timestamp_end = packet_index.timestamp_end; | |
1701 | viewer_index.events_discarded = packet_index.events_discarded; | |
1702 | viewer_index.stream_id = packet_index.stream_id; | |
1703 | ||
1704 | send_reply: | |
f1883937 MD |
1705 | if (rstream) { |
1706 | pthread_mutex_unlock(&rstream->lock); | |
1707 | } | |
7591bab1 MD |
1708 | |
1709 | if (metadata_viewer_stream) { | |
1710 | pthread_mutex_lock(&metadata_viewer_stream->stream->lock); | |
1711 | DBG("get next index metadata check: recv %" PRIu64 | |
1712 | " sent %" PRIu64, | |
1713 | metadata_viewer_stream->stream->metadata_received, | |
1714 | metadata_viewer_stream->metadata_sent); | |
1715 | if (!metadata_viewer_stream->stream->metadata_received || | |
1716 | metadata_viewer_stream->stream->metadata_received > | |
1717 | metadata_viewer_stream->metadata_sent) { | |
1718 | viewer_index.flags |= LTTNG_VIEWER_FLAG_NEW_METADATA; | |
1719 | } | |
1720 | pthread_mutex_unlock(&metadata_viewer_stream->stream->lock); | |
1721 | } | |
1722 | ||
d3e2ba59 | 1723 | viewer_index.flags = htobe32(viewer_index.flags); |
eea7556c | 1724 | health_code_update(); |
2f8f53af | 1725 | |
58eb9381 | 1726 | ret = send_response(conn->sock, &viewer_index, sizeof(viewer_index)); |
d3e2ba59 | 1727 | if (ret < 0) { |
7591bab1 | 1728 | goto end; |
d3e2ba59 | 1729 | } |
eea7556c | 1730 | health_code_update(); |
d3e2ba59 | 1731 | |
9237e6a1 MD |
1732 | if (vstream) { |
1733 | DBG("Index %" PRIu64 " for stream %" PRIu64 " sent", | |
a44ca2ca | 1734 | vstream->index_sent_seqcount, |
9237e6a1 MD |
1735 | vstream->stream->stream_handle); |
1736 | } | |
d3e2ba59 | 1737 | end: |
7591bab1 MD |
1738 | if (metadata_viewer_stream) { |
1739 | viewer_stream_put(metadata_viewer_stream); | |
1740 | } | |
1741 | if (vstream) { | |
1742 | viewer_stream_put(vstream); | |
1743 | } | |
1744 | return ret; | |
1745 | ||
1746 | error_put: | |
1747 | pthread_mutex_unlock(&rstream->lock); | |
1748 | if (metadata_viewer_stream) { | |
1749 | viewer_stream_put(metadata_viewer_stream); | |
1750 | } | |
1751 | viewer_stream_put(vstream); | |
d3e2ba59 JD |
1752 | return ret; |
1753 | } | |
1754 | ||
1755 | /* | |
1756 | * Send the next index for a stream | |
1757 | * | |
1758 | * Return 0 on success or else a negative value. | |
1759 | */ | |
1760 | static | |
58eb9381 | 1761 | int viewer_get_packet(struct relay_connection *conn) |
d3e2ba59 | 1762 | { |
b6025e94 | 1763 | int ret; |
a5df8828 | 1764 | off_t lseek_ret; |
553b2adb | 1765 | char *reply = NULL; |
d3e2ba59 | 1766 | struct lttng_viewer_get_packet get_packet_info; |
553b2adb | 1767 | struct lttng_viewer_trace_packet reply_header; |
7591bab1 | 1768 | struct relay_viewer_stream *vstream = NULL; |
553b2adb | 1769 | uint32_t reply_size = sizeof(reply_header); |
b6025e94 JR |
1770 | uint32_t packet_data_len = 0; |
1771 | ssize_t read_len; | |
d3e2ba59 JD |
1772 | |
1773 | DBG2("Relay get data packet"); | |
1774 | ||
eea7556c | 1775 | health_code_update(); |
2f8f53af | 1776 | |
7591bab1 MD |
1777 | ret = recv_request(conn->sock, &get_packet_info, |
1778 | sizeof(get_packet_info)); | |
2f8f53af | 1779 | if (ret < 0) { |
d3e2ba59 JD |
1780 | goto end; |
1781 | } | |
eea7556c | 1782 | health_code_update(); |
d3e2ba59 | 1783 | |
0233a6a5 | 1784 | /* From this point on, the error label can be reached. */ |
553b2adb | 1785 | memset(&reply_header, 0, sizeof(reply_header)); |
0233a6a5 | 1786 | |
7591bab1 MD |
1787 | vstream = viewer_stream_get_by_id(be64toh(get_packet_info.stream_id)); |
1788 | if (!vstream) { | |
a44ca2ca | 1789 | DBG("Client requested packet of unknown stream id %" PRIu64, |
9b9f9f94 | 1790 | (uint64_t) be64toh(get_packet_info.stream_id)); |
553b2adb JG |
1791 | reply_header.status = htobe32(LTTNG_VIEWER_GET_PACKET_ERR); |
1792 | goto send_reply_nolock; | |
b6025e94 JR |
1793 | } else { |
1794 | packet_data_len = be32toh(get_packet_info.len); | |
553b2adb | 1795 | reply_size += packet_data_len; |
d3e2ba59 | 1796 | } |
2a174661 | 1797 | |
553b2adb JG |
1798 | reply = zmalloc(reply_size); |
1799 | if (!reply) { | |
1800 | PERROR("packet reply zmalloc"); | |
1801 | reply_size = sizeof(reply_header); | |
d3e2ba59 JD |
1802 | goto error; |
1803 | } | |
1804 | ||
b6025e94 | 1805 | pthread_mutex_lock(&vstream->stream->lock); |
ebb29c10 JG |
1806 | lseek_ret = lseek(vstream->stream_file.fd->fd, |
1807 | be64toh(get_packet_info.offset), SEEK_SET); | |
a5df8828 | 1808 | if (lseek_ret < 0) { |
ebb29c10 JG |
1809 | PERROR("lseek fd %d to offset %" PRIu64, |
1810 | vstream->stream_file.fd->fd, | |
1811 | (uint64_t) be64toh(get_packet_info.offset)); | |
7591bab1 | 1812 | goto error; |
d3e2ba59 | 1813 | } |
ebb29c10 JG |
1814 | read_len = lttng_read(vstream->stream_file.fd->fd, |
1815 | reply + sizeof(reply_header), packet_data_len); | |
b6025e94 | 1816 | if (read_len < packet_data_len) { |
7591bab1 | 1817 | PERROR("Relay reading trace file, fd: %d, offset: %" PRIu64, |
ebb29c10 | 1818 | vstream->stream_file.fd->fd, |
9b9f9f94 | 1819 | (uint64_t) be64toh(get_packet_info.offset)); |
7591bab1 | 1820 | goto error; |
d3e2ba59 | 1821 | } |
553b2adb JG |
1822 | reply_header.status = htobe32(LTTNG_VIEWER_GET_PACKET_OK); |
1823 | reply_header.len = htobe32(packet_data_len); | |
d3e2ba59 JD |
1824 | goto send_reply; |
1825 | ||
1826 | error: | |
553b2adb | 1827 | reply_header.status = htobe32(LTTNG_VIEWER_GET_PACKET_ERR); |
d3e2ba59 JD |
1828 | |
1829 | send_reply: | |
7591bab1 MD |
1830 | if (vstream) { |
1831 | pthread_mutex_unlock(&vstream->stream->lock); | |
1832 | } | |
1833 | send_reply_nolock: | |
eea7556c MD |
1834 | |
1835 | health_code_update(); | |
2f8f53af | 1836 | |
553b2adb JG |
1837 | if (reply) { |
1838 | memcpy(reply, &reply_header, sizeof(reply_header)); | |
1839 | ret = send_response(conn->sock, reply, reply_size); | |
1840 | } else { | |
1841 | /* No reply to send. */ | |
1842 | ret = send_response(conn->sock, &reply_header, | |
1843 | reply_size); | |
1844 | } | |
1845 | ||
b6025e94 | 1846 | health_code_update(); |
d3e2ba59 | 1847 | if (ret < 0) { |
b6025e94 | 1848 | PERROR("sendmsg of packet data failed"); |
7591bab1 | 1849 | goto end_free; |
d3e2ba59 | 1850 | } |
d3e2ba59 | 1851 | |
553b2adb | 1852 | DBG("Sent %u bytes for stream %" PRIu64, reply_size, |
9b9f9f94 | 1853 | (uint64_t) be64toh(get_packet_info.stream_id)); |
d3e2ba59 | 1854 | |
7591bab1 | 1855 | end_free: |
553b2adb | 1856 | free(reply); |
d3e2ba59 | 1857 | end: |
7591bab1 MD |
1858 | if (vstream) { |
1859 | viewer_stream_put(vstream); | |
1860 | } | |
d3e2ba59 JD |
1861 | return ret; |
1862 | } | |
1863 | ||
1864 | /* | |
1865 | * Send the session's metadata | |
1866 | * | |
1867 | * Return 0 on success else a negative value. | |
1868 | */ | |
1869 | static | |
58eb9381 | 1870 | int viewer_get_metadata(struct relay_connection *conn) |
d3e2ba59 JD |
1871 | { |
1872 | int ret = 0; | |
1873 | ssize_t read_len; | |
1874 | uint64_t len = 0; | |
1875 | char *data = NULL; | |
1876 | struct lttng_viewer_get_metadata request; | |
1877 | struct lttng_viewer_metadata_packet reply; | |
7591bab1 | 1878 | struct relay_viewer_stream *vstream = NULL; |
d3e2ba59 | 1879 | |
58eb9381 | 1880 | assert(conn); |
d3e2ba59 JD |
1881 | |
1882 | DBG("Relay get metadata"); | |
1883 | ||
eea7556c | 1884 | health_code_update(); |
2f8f53af | 1885 | |
58eb9381 | 1886 | ret = recv_request(conn->sock, &request, sizeof(request)); |
2f8f53af | 1887 | if (ret < 0) { |
d3e2ba59 JD |
1888 | goto end; |
1889 | } | |
eea7556c | 1890 | health_code_update(); |
d3e2ba59 | 1891 | |
53efb85a MD |
1892 | memset(&reply, 0, sizeof(reply)); |
1893 | ||
7591bab1 MD |
1894 | vstream = viewer_stream_get_by_id(be64toh(request.stream_id)); |
1895 | if (!vstream) { | |
3b463131 MD |
1896 | /* |
1897 | * The metadata stream can be closed by a CLOSE command | |
1898 | * just before we attach. It can also be closed by | |
1899 | * per-pid tracing during tracing. Therefore, it is | |
1900 | * possible that we cannot find this viewer stream. | |
1901 | * Reply back to the client with an error if we cannot | |
1902 | * find it. | |
1903 | */ | |
a44ca2ca | 1904 | DBG("Client requested metadata of unknown stream id %" PRIu64, |
9b9f9f94 | 1905 | (uint64_t) be64toh(request.stream_id)); |
3b463131 | 1906 | reply.status = htobe32(LTTNG_VIEWER_METADATA_ERR); |
7591bab1 | 1907 | goto send_reply; |
d3e2ba59 | 1908 | } |
7591bab1 MD |
1909 | pthread_mutex_lock(&vstream->stream->lock); |
1910 | if (!vstream->stream->is_metadata) { | |
1911 | ERR("Invalid metadata stream"); | |
6763619c JD |
1912 | goto error; |
1913 | } | |
1914 | ||
b0d240a2 | 1915 | if (vstream->metadata_sent >= vstream->stream->metadata_received) { |
94f73d08 MD |
1916 | /* |
1917 | * The live viewers expect to receive a NO_NEW_METADATA | |
1918 | * status before a stream disappears, otherwise they abort the | |
1919 | * entire live connection when receiving an error status. | |
b0d240a2 MD |
1920 | * |
1921 | * Clear feature resets the metadata_sent to 0 until the | |
1922 | * same metadata is received again. | |
94f73d08 | 1923 | */ |
c4e361a4 | 1924 | reply.status = htobe32(LTTNG_VIEWER_NO_NEW_METADATA); |
94f73d08 MD |
1925 | /* |
1926 | * The live viewer considers a closed 0 byte metadata stream as | |
1927 | * an error. | |
1928 | */ | |
1929 | if (vstream->metadata_sent > 0) { | |
1930 | vstream->stream->no_new_metadata_notified = true; | |
1931 | if (vstream->stream->closed) { | |
1932 | /* Release ownership for the viewer metadata stream. */ | |
1933 | viewer_stream_put(vstream); | |
1934 | } | |
1935 | } | |
d3e2ba59 JD |
1936 | goto send_reply; |
1937 | } | |
1938 | ||
b0d240a2 MD |
1939 | len = vstream->stream->metadata_received - vstream->metadata_sent; |
1940 | ||
d3e2ba59 | 1941 | /* first time, we open the metadata file */ |
ebb29c10 JG |
1942 | if (!vstream->stream_file.fd) { |
1943 | int fd; | |
1944 | char file_path[LTTNG_PATH_MAX]; | |
1945 | enum lttng_trace_chunk_status status; | |
1946 | struct relay_stream *rstream = vstream->stream; | |
1947 | ||
1948 | ret = utils_stream_file_path(rstream->path_name, | |
1949 | rstream->channel_name, rstream->tracefile_size, | |
1950 | vstream->current_tracefile_id, NULL, file_path, | |
1951 | sizeof(file_path)); | |
d3e2ba59 JD |
1952 | if (ret < 0) { |
1953 | goto error; | |
1954 | } | |
ebb29c10 | 1955 | |
3ff5c5db MD |
1956 | /* |
1957 | * It is possible the the metadata file we are trying to open is | |
1958 | * missing if the stream has been closed (application exits with | |
1959 | * per-pid buffers) and a clear command has been performed. | |
1960 | */ | |
ebb29c10 JG |
1961 | status = lttng_trace_chunk_open_file( |
1962 | vstream->stream_file.trace_chunk, | |
3ff5c5db | 1963 | file_path, O_RDONLY, 0, &fd, true); |
ebb29c10 | 1964 | if (status != LTTNG_TRACE_CHUNK_STATUS_OK) { |
b0d240a2 MD |
1965 | if (status == LTTNG_TRACE_CHUNK_STATUS_NO_FILE) { |
1966 | reply.status = htobe32(LTTNG_VIEWER_NO_NEW_METADATA); | |
1967 | len = 0; | |
1968 | if (vstream->stream->closed) { | |
1969 | viewer_stream_put(vstream); | |
1970 | } | |
1971 | goto send_reply; | |
1972 | } | |
ebb29c10 | 1973 | PERROR("Failed to open metadata file for viewer stream"); |
d3e2ba59 JD |
1974 | goto error; |
1975 | } | |
ebb29c10 JG |
1976 | vstream->stream_file.fd = stream_fd_create(fd); |
1977 | if (!vstream->stream_file.fd) { | |
1978 | if (close(fd)) { | |
1979 | PERROR("Failed to close viewer metadata file"); | |
7591bab1 MD |
1980 | } |
1981 | goto error; | |
1982 | } | |
d3e2ba59 JD |
1983 | } |
1984 | ||
1985 | reply.len = htobe64(len); | |
1986 | data = zmalloc(len); | |
1987 | if (!data) { | |
1988 | PERROR("viewer metadata zmalloc"); | |
1989 | goto error; | |
1990 | } | |
1991 | ||
ebb29c10 | 1992 | read_len = lttng_read(vstream->stream_file.fd->fd, data, len); |
6cd525e8 | 1993 | if (read_len < len) { |
d3e2ba59 JD |
1994 | PERROR("Relay reading metadata file"); |
1995 | goto error; | |
1996 | } | |
7591bab1 | 1997 | vstream->metadata_sent += read_len; |
c4e361a4 | 1998 | reply.status = htobe32(LTTNG_VIEWER_METADATA_OK); |
7591bab1 | 1999 | |
d3e2ba59 JD |
2000 | goto send_reply; |
2001 | ||
2002 | error: | |
c4e361a4 | 2003 | reply.status = htobe32(LTTNG_VIEWER_METADATA_ERR); |
d3e2ba59 JD |
2004 | |
2005 | send_reply: | |
eea7556c | 2006 | health_code_update(); |
7591bab1 MD |
2007 | if (vstream) { |
2008 | pthread_mutex_unlock(&vstream->stream->lock); | |
2009 | } | |
58eb9381 | 2010 | ret = send_response(conn->sock, &reply, sizeof(reply)); |
d3e2ba59 | 2011 | if (ret < 0) { |
7591bab1 | 2012 | goto end_free; |
d3e2ba59 | 2013 | } |
eea7556c | 2014 | health_code_update(); |
d3e2ba59 JD |
2015 | |
2016 | if (len > 0) { | |
58eb9381 | 2017 | ret = send_response(conn->sock, data, len); |
d3e2ba59 | 2018 | if (ret < 0) { |
7591bab1 | 2019 | goto end_free; |
d3e2ba59 JD |
2020 | } |
2021 | } | |
2022 | ||
2023 | DBG("Sent %" PRIu64 " bytes of metadata for stream %" PRIu64, len, | |
9b9f9f94 | 2024 | (uint64_t) be64toh(request.stream_id)); |
d3e2ba59 JD |
2025 | |
2026 | DBG("Metadata sent"); | |
2027 | ||
7591bab1 | 2028 | end_free: |
d3e2ba59 | 2029 | free(data); |
d3e2ba59 | 2030 | end: |
7591bab1 MD |
2031 | if (vstream) { |
2032 | viewer_stream_put(vstream); | |
2033 | } | |
d3e2ba59 JD |
2034 | return ret; |
2035 | } | |
2036 | ||
c3b7390b JD |
2037 | /* |
2038 | * Create a viewer session. | |
2039 | * | |
2040 | * Return 0 on success or else a negative value. | |
2041 | */ | |
2042 | static | |
2043 | int viewer_create_session(struct relay_connection *conn) | |
2044 | { | |
2045 | int ret; | |
2046 | struct lttng_viewer_create_session_response resp; | |
2047 | ||
2048 | DBG("Viewer create session received"); | |
2049 | ||
53efb85a | 2050 | memset(&resp, 0, sizeof(resp)); |
c3b7390b | 2051 | resp.status = htobe32(LTTNG_VIEWER_CREATE_SESSION_OK); |
7591bab1 | 2052 | conn->viewer_session = viewer_session_create(); |
c3b7390b JD |
2053 | if (!conn->viewer_session) { |
2054 | ERR("Allocation viewer session"); | |
2055 | resp.status = htobe32(LTTNG_VIEWER_CREATE_SESSION_ERR); | |
2056 | goto send_reply; | |
2057 | } | |
c3b7390b JD |
2058 | |
2059 | send_reply: | |
2060 | health_code_update(); | |
2061 | ret = send_response(conn->sock, &resp, sizeof(resp)); | |
2062 | if (ret < 0) { | |
2063 | goto end; | |
2064 | } | |
2065 | health_code_update(); | |
2066 | ret = 0; | |
2067 | ||
2068 | end: | |
2069 | return ret; | |
2070 | } | |
2071 | ||
d62023be JD |
2072 | /* |
2073 | * Detach a viewer session. | |
2074 | * | |
2075 | * Return 0 on success or else a negative value. | |
2076 | */ | |
2077 | static | |
2078 | int viewer_detach_session(struct relay_connection *conn) | |
2079 | { | |
2080 | int ret; | |
2081 | struct lttng_viewer_detach_session_response response; | |
2082 | struct lttng_viewer_detach_session_request request; | |
2083 | struct relay_session *session = NULL; | |
2084 | uint64_t viewer_session_to_close; | |
2085 | ||
2086 | DBG("Viewer detach session received"); | |
2087 | ||
2088 | assert(conn); | |
2089 | ||
2090 | health_code_update(); | |
2091 | ||
2092 | /* Receive the request from the connected client. */ | |
2093 | ret = recv_request(conn->sock, &request, sizeof(request)); | |
2094 | if (ret < 0) { | |
2095 | goto end; | |
2096 | } | |
2097 | viewer_session_to_close = be64toh(request.session_id); | |
2098 | ||
2099 | if (!conn->viewer_session) { | |
2100 | DBG("Client trying to detach before creating a live viewer session"); | |
2101 | response.status = htobe32(LTTNG_VIEWER_DETACH_SESSION_ERR); | |
2102 | goto send_reply; | |
2103 | } | |
2104 | ||
2105 | health_code_update(); | |
2106 | ||
2107 | memset(&response, 0, sizeof(response)); | |
2108 | DBG("Detaching from session ID %" PRIu64, viewer_session_to_close); | |
2109 | ||
2110 | session = session_get_by_id(be64toh(request.session_id)); | |
2111 | if (!session) { | |
2112 | DBG("Relay session %" PRIu64 " not found", | |
9b9f9f94 | 2113 | (uint64_t) be64toh(request.session_id)); |
d62023be JD |
2114 | response.status = htobe32(LTTNG_VIEWER_DETACH_SESSION_UNK); |
2115 | goto send_reply; | |
2116 | } | |
2117 | ||
2118 | ret = viewer_session_is_attached(conn->viewer_session, session); | |
2119 | if (ret != 1) { | |
2120 | DBG("Not attached to this session"); | |
2121 | response.status = htobe32(LTTNG_VIEWER_DETACH_SESSION_ERR); | |
2122 | goto send_reply_put; | |
2123 | } | |
2124 | ||
2125 | viewer_session_close_one_session(conn->viewer_session, session); | |
2126 | response.status = htobe32(LTTNG_VIEWER_DETACH_SESSION_OK); | |
2127 | DBG("Session %" PRIu64 " detached.", viewer_session_to_close); | |
2128 | ||
2129 | send_reply_put: | |
2130 | session_put(session); | |
2131 | ||
2132 | send_reply: | |
2133 | health_code_update(); | |
2134 | ret = send_response(conn->sock, &response, sizeof(response)); | |
2135 | if (ret < 0) { | |
2136 | goto end; | |
2137 | } | |
2138 | health_code_update(); | |
2139 | ret = 0; | |
2140 | ||
2141 | end: | |
2142 | return ret; | |
2143 | } | |
c3b7390b | 2144 | |
d3e2ba59 JD |
2145 | /* |
2146 | * live_relay_unknown_command: send -1 if received unknown command | |
2147 | */ | |
2148 | static | |
58eb9381 | 2149 | void live_relay_unknown_command(struct relay_connection *conn) |
d3e2ba59 JD |
2150 | { |
2151 | struct lttcomm_relayd_generic_reply reply; | |
d3e2ba59 | 2152 | |
53efb85a | 2153 | memset(&reply, 0, sizeof(reply)); |
d3e2ba59 | 2154 | reply.ret_code = htobe32(LTTNG_ERR_UNK); |
58eb9381 | 2155 | (void) send_response(conn->sock, &reply, sizeof(reply)); |
d3e2ba59 JD |
2156 | } |
2157 | ||
2158 | /* | |
2159 | * Process the commands received on the control socket | |
2160 | */ | |
2161 | static | |
2162 | int process_control(struct lttng_viewer_cmd *recv_hdr, | |
58eb9381 | 2163 | struct relay_connection *conn) |
d3e2ba59 JD |
2164 | { |
2165 | int ret = 0; | |
2f8f53af DG |
2166 | uint32_t msg_value; |
2167 | ||
2f8f53af DG |
2168 | msg_value = be32toh(recv_hdr->cmd); |
2169 | ||
2170 | /* | |
2171 | * Make sure we've done the version check before any command other then a | |
2172 | * new client connection. | |
2173 | */ | |
c4e361a4 | 2174 | if (msg_value != LTTNG_VIEWER_CONNECT && !conn->version_check_done) { |
58eb9381 | 2175 | ERR("Viewer conn value %" PRIu32 " before version check", msg_value); |
2f8f53af DG |
2176 | ret = -1; |
2177 | goto end; | |
2178 | } | |
d3e2ba59 | 2179 | |
2f8f53af | 2180 | switch (msg_value) { |
c4e361a4 | 2181 | case LTTNG_VIEWER_CONNECT: |
58eb9381 | 2182 | ret = viewer_connect(conn); |
d3e2ba59 | 2183 | break; |
c4e361a4 | 2184 | case LTTNG_VIEWER_LIST_SESSIONS: |
58eb9381 | 2185 | ret = viewer_list_sessions(conn); |
d3e2ba59 | 2186 | break; |
c4e361a4 | 2187 | case LTTNG_VIEWER_ATTACH_SESSION: |
58eb9381 | 2188 | ret = viewer_attach_session(conn); |
d3e2ba59 | 2189 | break; |
c4e361a4 | 2190 | case LTTNG_VIEWER_GET_NEXT_INDEX: |
58eb9381 | 2191 | ret = viewer_get_next_index(conn); |
d3e2ba59 | 2192 | break; |
c4e361a4 | 2193 | case LTTNG_VIEWER_GET_PACKET: |
58eb9381 | 2194 | ret = viewer_get_packet(conn); |
d3e2ba59 | 2195 | break; |
c4e361a4 | 2196 | case LTTNG_VIEWER_GET_METADATA: |
58eb9381 | 2197 | ret = viewer_get_metadata(conn); |
d3e2ba59 | 2198 | break; |
c4e361a4 | 2199 | case LTTNG_VIEWER_GET_NEW_STREAMS: |
58eb9381 | 2200 | ret = viewer_get_new_streams(conn); |
80e8027a | 2201 | break; |
c3b7390b JD |
2202 | case LTTNG_VIEWER_CREATE_SESSION: |
2203 | ret = viewer_create_session(conn); | |
2204 | break; | |
d62023be JD |
2205 | case LTTNG_VIEWER_DETACH_SESSION: |
2206 | ret = viewer_detach_session(conn); | |
2207 | break; | |
d3e2ba59 | 2208 | default: |
7591bab1 MD |
2209 | ERR("Received unknown viewer command (%u)", |
2210 | be32toh(recv_hdr->cmd)); | |
58eb9381 | 2211 | live_relay_unknown_command(conn); |
d3e2ba59 JD |
2212 | ret = -1; |
2213 | goto end; | |
2214 | } | |
2215 | ||
2216 | end: | |
2217 | return ret; | |
2218 | } | |
2219 | ||
2220 | static | |
58eb9381 | 2221 | void cleanup_connection_pollfd(struct lttng_poll_event *events, int pollfd) |
d3e2ba59 JD |
2222 | { |
2223 | int ret; | |
2224 | ||
58eb9381 | 2225 | (void) lttng_poll_del(events, pollfd); |
d3e2ba59 | 2226 | |
29555a78 JG |
2227 | ret = fd_tracker_close_unsuspendable_fd(the_fd_tracker, &pollfd, 1, |
2228 | fd_tracker_util_close_fd, NULL); | |
d3e2ba59 JD |
2229 | if (ret < 0) { |
2230 | ERR("Closing pollfd %d", pollfd); | |
2231 | } | |
2232 | } | |
2233 | ||
d3e2ba59 JD |
2234 | /* |
2235 | * This thread does the actual work | |
2236 | */ | |
2237 | static | |
2238 | void *thread_worker(void *data) | |
2239 | { | |
2240 | int ret, err = -1; | |
2241 | uint32_t nb_fd; | |
d3e2ba59 | 2242 | struct lttng_poll_event events; |
7591bab1 | 2243 | struct lttng_ht *viewer_connections_ht; |
d3e2ba59 JD |
2244 | struct lttng_ht_iter iter; |
2245 | struct lttng_viewer_cmd recv_hdr; | |
302d8906 | 2246 | struct relay_connection *destroy_conn; |
d3e2ba59 JD |
2247 | |
2248 | DBG("[thread] Live viewer relay worker started"); | |
2249 | ||
2250 | rcu_register_thread(); | |
2251 | ||
eea7556c MD |
2252 | health_register(health_relayd, HEALTH_RELAYD_TYPE_LIVE_WORKER); |
2253 | ||
9b5e0863 MD |
2254 | if (testpoint(relayd_thread_live_worker)) { |
2255 | goto error_testpoint; | |
2256 | } | |
2257 | ||
d3e2ba59 | 2258 | /* table of connections indexed on socket */ |
7591bab1 MD |
2259 | viewer_connections_ht = lttng_ht_new(0, LTTNG_HT_TYPE_ULONG); |
2260 | if (!viewer_connections_ht) { | |
2261 | goto viewer_connections_ht_error; | |
d3e2ba59 JD |
2262 | } |
2263 | ||
35bc1f58 JG |
2264 | ret = create_named_thread_poll_set(&events, 2, |
2265 | "Live viewer worker thread epoll"); | |
d3e2ba59 JD |
2266 | if (ret < 0) { |
2267 | goto error_poll_create; | |
2268 | } | |
2269 | ||
58eb9381 | 2270 | ret = lttng_poll_add(&events, live_conn_pipe[0], LPOLLIN | LPOLLRDHUP); |
d3e2ba59 JD |
2271 | if (ret < 0) { |
2272 | goto error; | |
2273 | } | |
2274 | ||
2275 | restart: | |
2276 | while (1) { | |
2277 | int i; | |
2278 | ||
eea7556c MD |
2279 | health_code_update(); |
2280 | ||
d3e2ba59 JD |
2281 | /* Infinite blocking call, waiting for transmission */ |
2282 | DBG3("Relayd live viewer worker thread polling..."); | |
eea7556c | 2283 | health_poll_entry(); |
d3e2ba59 | 2284 | ret = lttng_poll_wait(&events, -1); |
eea7556c | 2285 | health_poll_exit(); |
d3e2ba59 JD |
2286 | if (ret < 0) { |
2287 | /* | |
2288 | * Restart interrupted system call. | |
2289 | */ | |
2290 | if (errno == EINTR) { | |
2291 | goto restart; | |
2292 | } | |
2293 | goto error; | |
2294 | } | |
2295 | ||
2296 | nb_fd = ret; | |
2297 | ||
2298 | /* | |
2299 | * Process control. The control connection is prioritised so we don't | |
2300 | * starve it with high throughput tracing data on the data | |
2301 | * connection. | |
2302 | */ | |
2303 | for (i = 0; i < nb_fd; i++) { | |
2304 | /* Fetch once the poll data */ | |
2305 | uint32_t revents = LTTNG_POLL_GETEV(&events, i); | |
2306 | int pollfd = LTTNG_POLL_GETFD(&events, i); | |
2307 | ||
eea7556c MD |
2308 | health_code_update(); |
2309 | ||
d3e2ba59 | 2310 | /* Thread quit pipe has been closed. Killing thread. */ |
bcf4a440 | 2311 | ret = check_thread_quit_pipe(pollfd, revents); |
d3e2ba59 JD |
2312 | if (ret) { |
2313 | err = 0; | |
2314 | goto exit; | |
2315 | } | |
2316 | ||
7591bab1 | 2317 | /* Inspect the relay conn pipe for new connection. */ |
58eb9381 | 2318 | if (pollfd == live_conn_pipe[0]) { |
03e43155 | 2319 | if (revents & LPOLLIN) { |
302d8906 JG |
2320 | struct relay_connection *conn; |
2321 | ||
7591bab1 MD |
2322 | ret = lttng_read(live_conn_pipe[0], |
2323 | &conn, sizeof(conn)); | |
d3e2ba59 JD |
2324 | if (ret < 0) { |
2325 | goto error; | |
2326 | } | |
73039936 FD |
2327 | ret = lttng_poll_add(&events, |
2328 | conn->sock->fd, | |
58eb9381 | 2329 | LPOLLIN | LPOLLRDHUP); |
73039936 FD |
2330 | if (ret) { |
2331 | ERR("Failed to add new live connection file descriptor to poll set"); | |
2332 | goto error; | |
2333 | } | |
7591bab1 MD |
2334 | connection_ht_add(viewer_connections_ht, conn); |
2335 | DBG("Connection socket %d added to poll", conn->sock->fd); | |
03e43155 MD |
2336 | } else if (revents & (LPOLLERR | LPOLLHUP | LPOLLRDHUP)) { |
2337 | ERR("Relay live pipe error"); | |
2338 | goto error; | |
2339 | } else { | |
2340 | ERR("Unexpected poll events %u for sock %d", revents, pollfd); | |
2341 | goto error; | |
d3e2ba59 | 2342 | } |
58eb9381 | 2343 | } else { |
7591bab1 | 2344 | /* Connection activity. */ |
302d8906 JG |
2345 | struct relay_connection *conn; |
2346 | ||
7591bab1 MD |
2347 | conn = connection_get_by_sock(viewer_connections_ht, pollfd); |
2348 | if (!conn) { | |
2349 | continue; | |
2350 | } | |
58eb9381 | 2351 | |
03e43155 | 2352 | if (revents & LPOLLIN) { |
58eb9381 DG |
2353 | ret = conn->sock->ops->recvmsg(conn->sock, &recv_hdr, |
2354 | sizeof(recv_hdr), 0); | |
d3e2ba59 | 2355 | if (ret <= 0) { |
7591bab1 | 2356 | /* Connection closed. */ |
58eb9381 | 2357 | cleanup_connection_pollfd(&events, pollfd); |
7591bab1 MD |
2358 | /* Put "create" ownership reference. */ |
2359 | connection_put(conn); | |
58eb9381 | 2360 | DBG("Viewer control conn closed with %d", pollfd); |
d3e2ba59 | 2361 | } else { |
58eb9381 | 2362 | ret = process_control(&recv_hdr, conn); |
d3e2ba59 JD |
2363 | if (ret < 0) { |
2364 | /* Clear the session on error. */ | |
58eb9381 | 2365 | cleanup_connection_pollfd(&events, pollfd); |
7591bab1 MD |
2366 | /* Put "create" ownership reference. */ |
2367 | connection_put(conn); | |
d3e2ba59 JD |
2368 | DBG("Viewer connection closed with %d", pollfd); |
2369 | } | |
2370 | } | |
03e43155 MD |
2371 | } else if (revents & (LPOLLERR | LPOLLHUP | LPOLLRDHUP)) { |
2372 | cleanup_connection_pollfd(&events, pollfd); | |
2373 | /* Put "create" ownership reference. */ | |
2374 | connection_put(conn); | |
2375 | } else { | |
2376 | ERR("Unexpected poll events %u for sock %d", revents, pollfd); | |
2377 | connection_put(conn); | |
2378 | goto error; | |
d3e2ba59 | 2379 | } |
7591bab1 MD |
2380 | /* Put local "get_by_sock" reference. */ |
2381 | connection_put(conn); | |
d3e2ba59 JD |
2382 | } |
2383 | } | |
2384 | } | |
2385 | ||
2386 | exit: | |
2387 | error: | |
35bc1f58 | 2388 | (void) fd_tracker_util_poll_clean(the_fd_tracker, &events); |
d3e2ba59 | 2389 | |
71efa8ef | 2390 | /* Cleanup remaining connection object. */ |
d3e2ba59 | 2391 | rcu_read_lock(); |
7591bab1 | 2392 | cds_lfht_for_each_entry(viewer_connections_ht->ht, &iter.iter, |
302d8906 | 2393 | destroy_conn, |
58eb9381 | 2394 | sock_n.node) { |
eea7556c | 2395 | health_code_update(); |
7591bab1 | 2396 | connection_put(destroy_conn); |
d3e2ba59 JD |
2397 | } |
2398 | rcu_read_unlock(); | |
2399 | error_poll_create: | |
7591bab1 MD |
2400 | lttng_ht_destroy(viewer_connections_ht); |
2401 | viewer_connections_ht_error: | |
58eb9381 | 2402 | /* Close relay conn pipes */ |
87bcbe91 | 2403 | (void) fd_tracker_util_pipe_close(the_fd_tracker, live_conn_pipe); |
d3e2ba59 JD |
2404 | if (err) { |
2405 | DBG("Viewer worker thread exited with error"); | |
2406 | } | |
2407 | DBG("Viewer worker thread cleanup complete"); | |
9b5e0863 | 2408 | error_testpoint: |
eea7556c MD |
2409 | if (err) { |
2410 | health_error(); | |
2411 | ERR("Health error occurred in %s", __func__); | |
2412 | } | |
2413 | health_unregister(health_relayd); | |
b4aacfdc MD |
2414 | if (lttng_relay_stop_threads()) { |
2415 | ERR("Error stopping threads"); | |
178a0557 | 2416 | } |
d3e2ba59 JD |
2417 | rcu_unregister_thread(); |
2418 | return NULL; | |
2419 | } | |
2420 | ||
2421 | /* | |
2422 | * Create the relay command pipe to wake thread_manage_apps. | |
2423 | * Closed in cleanup(). | |
2424 | */ | |
58eb9381 | 2425 | static int create_conn_pipe(void) |
d3e2ba59 | 2426 | { |
87bcbe91 JG |
2427 | return fd_tracker_util_pipe_open_cloexec(the_fd_tracker, |
2428 | "Live connection pipe", live_conn_pipe); | |
178a0557 | 2429 | } |
d3e2ba59 | 2430 | |
178a0557 | 2431 | int relayd_live_join(void) |
d3e2ba59 | 2432 | { |
178a0557 | 2433 | int ret, retval = 0; |
d3e2ba59 JD |
2434 | void *status; |
2435 | ||
d3e2ba59 | 2436 | ret = pthread_join(live_listener_thread, &status); |
178a0557 MD |
2437 | if (ret) { |
2438 | errno = ret; | |
d3e2ba59 | 2439 | PERROR("pthread_join live listener"); |
178a0557 | 2440 | retval = -1; |
d3e2ba59 JD |
2441 | } |
2442 | ||
2443 | ret = pthread_join(live_worker_thread, &status); | |
178a0557 MD |
2444 | if (ret) { |
2445 | errno = ret; | |
d3e2ba59 | 2446 | PERROR("pthread_join live worker"); |
178a0557 | 2447 | retval = -1; |
d3e2ba59 JD |
2448 | } |
2449 | ||
2450 | ret = pthread_join(live_dispatcher_thread, &status); | |
178a0557 MD |
2451 | if (ret) { |
2452 | errno = ret; | |
d3e2ba59 | 2453 | PERROR("pthread_join live dispatcher"); |
178a0557 | 2454 | retval = -1; |
d3e2ba59 JD |
2455 | } |
2456 | ||
178a0557 | 2457 | cleanup_relayd_live(); |
d3e2ba59 | 2458 | |
178a0557 | 2459 | return retval; |
d3e2ba59 JD |
2460 | } |
2461 | ||
2462 | /* | |
2463 | * main | |
2464 | */ | |
7591bab1 | 2465 | int relayd_live_create(struct lttng_uri *uri) |
d3e2ba59 | 2466 | { |
178a0557 | 2467 | int ret = 0, retval = 0; |
d3e2ba59 JD |
2468 | void *status; |
2469 | int is_root; | |
2470 | ||
178a0557 MD |
2471 | if (!uri) { |
2472 | retval = -1; | |
2473 | goto exit_init_data; | |
2474 | } | |
d3e2ba59 JD |
2475 | live_uri = uri; |
2476 | ||
d3e2ba59 JD |
2477 | /* Check if daemon is UID = 0 */ |
2478 | is_root = !getuid(); | |
2479 | ||
2480 | if (!is_root) { | |
2481 | if (live_uri->port < 1024) { | |
2482 | ERR("Need to be root to use ports < 1024"); | |
178a0557 MD |
2483 | retval = -1; |
2484 | goto exit_init_data; | |
d3e2ba59 JD |
2485 | } |
2486 | } | |
2487 | ||
2488 | /* Setup the thread apps communication pipe. */ | |
178a0557 MD |
2489 | if (create_conn_pipe()) { |
2490 | retval = -1; | |
2491 | goto exit_init_data; | |
d3e2ba59 JD |
2492 | } |
2493 | ||
2494 | /* Init relay command queue. */ | |
8bdee6e2 | 2495 | cds_wfcq_init(&viewer_conn_queue.head, &viewer_conn_queue.tail); |
d3e2ba59 JD |
2496 | |
2497 | /* Set up max poll set size */ | |
25b397f9 MD |
2498 | if (lttng_poll_set_max_size()) { |
2499 | retval = -1; | |
2500 | goto exit_init_data; | |
2501 | } | |
d3e2ba59 JD |
2502 | |
2503 | /* Setup the dispatcher thread */ | |
1a1a34b4 | 2504 | ret = pthread_create(&live_dispatcher_thread, default_pthread_attr(), |
d3e2ba59 | 2505 | thread_dispatcher, (void *) NULL); |
178a0557 MD |
2506 | if (ret) { |
2507 | errno = ret; | |
d3e2ba59 | 2508 | PERROR("pthread_create viewer dispatcher"); |
178a0557 MD |
2509 | retval = -1; |
2510 | goto exit_dispatcher_thread; | |
d3e2ba59 JD |
2511 | } |
2512 | ||
2513 | /* Setup the worker thread */ | |
1a1a34b4 | 2514 | ret = pthread_create(&live_worker_thread, default_pthread_attr(), |
7591bab1 | 2515 | thread_worker, NULL); |
178a0557 MD |
2516 | if (ret) { |
2517 | errno = ret; | |
d3e2ba59 | 2518 | PERROR("pthread_create viewer worker"); |
178a0557 MD |
2519 | retval = -1; |
2520 | goto exit_worker_thread; | |
d3e2ba59 JD |
2521 | } |
2522 | ||
2523 | /* Setup the listener thread */ | |
1a1a34b4 | 2524 | ret = pthread_create(&live_listener_thread, default_pthread_attr(), |
d3e2ba59 | 2525 | thread_listener, (void *) NULL); |
178a0557 MD |
2526 | if (ret) { |
2527 | errno = ret; | |
d3e2ba59 | 2528 | PERROR("pthread_create viewer listener"); |
178a0557 MD |
2529 | retval = -1; |
2530 | goto exit_listener_thread; | |
d3e2ba59 JD |
2531 | } |
2532 | ||
178a0557 MD |
2533 | /* |
2534 | * All OK, started all threads. | |
2535 | */ | |
2536 | return retval; | |
2537 | ||
9911d21b JG |
2538 | /* |
2539 | * Join on the live_listener_thread should anything be added after | |
2540 | * the live_listener thread's creation. | |
2541 | */ | |
d3e2ba59 | 2542 | |
178a0557 | 2543 | exit_listener_thread: |
d3e2ba59 | 2544 | |
d3e2ba59 | 2545 | ret = pthread_join(live_worker_thread, &status); |
178a0557 MD |
2546 | if (ret) { |
2547 | errno = ret; | |
d3e2ba59 | 2548 | PERROR("pthread_join live worker"); |
178a0557 | 2549 | retval = -1; |
d3e2ba59 | 2550 | } |
178a0557 | 2551 | exit_worker_thread: |
d3e2ba59 | 2552 | |
d3e2ba59 | 2553 | ret = pthread_join(live_dispatcher_thread, &status); |
178a0557 MD |
2554 | if (ret) { |
2555 | errno = ret; | |
d3e2ba59 | 2556 | PERROR("pthread_join live dispatcher"); |
178a0557 | 2557 | retval = -1; |
d3e2ba59 | 2558 | } |
178a0557 | 2559 | exit_dispatcher_thread: |
d3e2ba59 | 2560 | |
178a0557 MD |
2561 | exit_init_data: |
2562 | cleanup_relayd_live(); | |
d3e2ba59 | 2563 | |
178a0557 | 2564 | return retval; |
d3e2ba59 | 2565 | } |