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