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