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