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