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