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