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