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