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