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