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