Commit | Line | Data |
---|---|---|
b8aa1682 JD |
1 | /* |
2 | * Copyright (C) 2012 - 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> | |
173af62f | 35 | #include <inttypes.h> |
b8aa1682 JD |
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> | |
02a6bb53 | 47 | #include <common/daemonize.h> |
b8aa1682 JD |
48 | #include <common/futex.h> |
49 | #include <common/sessiond-comm/sessiond-comm.h> | |
50 | #include <common/sessiond-comm/inet.h> | |
b8aa1682 JD |
51 | #include <common/sessiond-comm/relayd.h> |
52 | #include <common/uri.h> | |
a02de639 | 53 | #include <common/utils.h> |
b8aa1682 | 54 | |
0f907de1 | 55 | #include "cmd.h" |
d3e2ba59 | 56 | #include "ctf-trace.h" |
1c20f0e2 | 57 | #include "index.h" |
0f907de1 | 58 | #include "utils.h" |
b8aa1682 | 59 | #include "lttng-relayd.h" |
d3e2ba59 | 60 | #include "live.h" |
55706a7d | 61 | #include "health-relayd.h" |
4e5b8b82 | 62 | #include "testpoint.h" |
991adae2 | 63 | #include "viewer-stream.h" |
eb702af5 DG |
64 | #include "session.h" |
65 | #include "stream.h" | |
e85cdca9 | 66 | #include "connection.h" |
b8aa1682 JD |
67 | |
68 | /* command line options */ | |
0f907de1 | 69 | char *opt_output_path; |
d1a3048a | 70 | static int opt_daemon, opt_background; |
02a6bb53 MD |
71 | |
72 | /* | |
73 | * We need to wait for listener and live listener threads, as well as | |
74 | * health check thread, before being ready to signal readiness. | |
75 | */ | |
76 | #define NR_LTTNG_RELAY_READY 3 | |
77 | static int lttng_relay_ready = NR_LTTNG_RELAY_READY; | |
78 | static int recv_child_signal; /* Set to 1 when a SIGUSR1 signal is received. */ | |
79 | static pid_t child_ppid; /* Internal parent PID use with daemonize. */ | |
80 | ||
095a4ae5 MD |
81 | static struct lttng_uri *control_uri; |
82 | static struct lttng_uri *data_uri; | |
d3e2ba59 | 83 | static struct lttng_uri *live_uri; |
b8aa1682 JD |
84 | |
85 | const char *progname; | |
b8aa1682 | 86 | |
65931c8b MD |
87 | const char *tracing_group_name = DEFAULT_TRACING_GROUP; |
88 | ||
b8aa1682 JD |
89 | /* |
90 | * Quit pipe for all threads. This permits a single cancellation point | |
91 | * for all threads when receiving an event on the pipe. | |
92 | */ | |
3557d456 | 93 | int thread_quit_pipe[2] = { -1, -1 }; |
b8aa1682 JD |
94 | |
95 | /* | |
96 | * This pipe is used to inform the worker thread that a command is queued and | |
97 | * ready to be processed. | |
98 | */ | |
e85cdca9 | 99 | static int relay_conn_pipe[2] = { -1, -1 }; |
b8aa1682 | 100 | |
26c9d55e | 101 | /* Shared between threads */ |
b8aa1682 JD |
102 | static int dispatch_thread_exit; |
103 | ||
104 | static pthread_t listener_thread; | |
105 | static pthread_t dispatcher_thread; | |
106 | static pthread_t worker_thread; | |
65931c8b | 107 | static pthread_t health_thread; |
b8aa1682 | 108 | |
095a4ae5 | 109 | static uint64_t last_relay_stream_id; |
b8aa1682 JD |
110 | |
111 | /* | |
112 | * Relay command queue. | |
113 | * | |
114 | * The relay_thread_listener and relay_thread_dispatcher communicate with this | |
115 | * queue. | |
116 | */ | |
e85cdca9 | 117 | static struct relay_conn_queue relay_conn_queue; |
b8aa1682 JD |
118 | |
119 | /* buffer allocated at startup, used to store the trace data */ | |
095a4ae5 MD |
120 | static char *data_buffer; |
121 | static unsigned int data_buffer_size; | |
b8aa1682 | 122 | |
1c20f0e2 JD |
123 | /* We need those values for the file/dir creation. */ |
124 | static uid_t relayd_uid; | |
125 | static gid_t relayd_gid; | |
126 | ||
d3e2ba59 JD |
127 | /* Global relay stream hash table. */ |
128 | struct lttng_ht *relay_streams_ht; | |
129 | ||
92c6ca54 DG |
130 | /* Global relay viewer stream hash table. */ |
131 | struct lttng_ht *viewer_streams_ht; | |
132 | ||
0a6518b0 DG |
133 | /* Global hash table that stores relay index object. */ |
134 | struct lttng_ht *indexes_ht; | |
135 | ||
55706a7d | 136 | /* Relayd health monitoring */ |
eea7556c | 137 | struct health_app *health_relayd; |
55706a7d | 138 | |
b8aa1682 JD |
139 | /* |
140 | * usage function on stderr | |
141 | */ | |
142 | static | |
143 | void usage(void) | |
144 | { | |
145 | fprintf(stderr, "Usage: %s OPTIONS\n\nOptions:\n", progname); | |
994fa64f DG |
146 | fprintf(stderr, " -h, --help Display this usage.\n"); |
147 | fprintf(stderr, " -d, --daemonize Start as a daemon.\n"); | |
d1a3048a | 148 | fprintf(stderr, " -b, --background Start as a daemon, keeping console open.\n"); |
994fa64f DG |
149 | fprintf(stderr, " -C, --control-port URL Control port listening.\n"); |
150 | fprintf(stderr, " -D, --data-port URL Data port listening.\n"); | |
a6a062a4 | 151 | fprintf(stderr, " -L, --live-port URL Live view port listening.\n"); |
994fa64f DG |
152 | fprintf(stderr, " -o, --output PATH Output path for traces. Must use an absolute path.\n"); |
153 | fprintf(stderr, " -v, --verbose Verbose mode. Activate DBG() macro.\n"); | |
65931c8b | 154 | fprintf(stderr, " -g, --group NAME Specify the tracing group name. (default: tracing)\n"); |
b8aa1682 JD |
155 | } |
156 | ||
157 | static | |
158 | int parse_args(int argc, char **argv) | |
159 | { | |
160 | int c; | |
161 | int ret = 0; | |
162 | char *default_address; | |
163 | ||
164 | static struct option long_options[] = { | |
e3678fd8 MD |
165 | { "control-port", 1, 0, 'C', }, |
166 | { "data-port", 1, 0, 'D', }, | |
167 | { "daemonize", 0, 0, 'd', }, | |
65931c8b | 168 | { "group", 1, 0, 'g', }, |
e3678fd8 MD |
169 | { "help", 0, 0, 'h', }, |
170 | { "output", 1, 0, 'o', }, | |
171 | { "verbose", 0, 0, 'v', }, | |
06b55dbb | 172 | { "background", 0, 0, 'b' }, |
095a4ae5 | 173 | { NULL, 0, 0, 0, }, |
b8aa1682 JD |
174 | }; |
175 | ||
176 | while (1) { | |
177 | int option_index = 0; | |
06b55dbb | 178 | c = getopt_long(argc, argv, "dhv" "C:D:L:o:g:b", |
b8aa1682 JD |
179 | long_options, &option_index); |
180 | if (c == -1) { | |
181 | break; | |
182 | } | |
183 | ||
184 | switch (c) { | |
185 | case 0: | |
186 | fprintf(stderr, "option %s", long_options[option_index].name); | |
187 | if (optarg) { | |
188 | fprintf(stderr, " with arg %s\n", optarg); | |
189 | } | |
190 | break; | |
191 | case 'C': | |
192 | ret = uri_parse(optarg, &control_uri); | |
193 | if (ret < 0) { | |
194 | ERR("Invalid control URI specified"); | |
195 | goto exit; | |
196 | } | |
197 | if (control_uri->port == 0) { | |
198 | control_uri->port = DEFAULT_NETWORK_CONTROL_PORT; | |
199 | } | |
200 | break; | |
201 | case 'D': | |
202 | ret = uri_parse(optarg, &data_uri); | |
203 | if (ret < 0) { | |
204 | ERR("Invalid data URI specified"); | |
205 | goto exit; | |
206 | } | |
207 | if (data_uri->port == 0) { | |
208 | data_uri->port = DEFAULT_NETWORK_DATA_PORT; | |
209 | } | |
210 | break; | |
a6a062a4 AM |
211 | case 'L': |
212 | ret = uri_parse(optarg, &live_uri); | |
213 | if (ret < 0) { | |
214 | ERR("Invalid live URI specified"); | |
215 | goto exit; | |
216 | } | |
217 | if (live_uri->port == 0) { | |
218 | live_uri->port = DEFAULT_NETWORK_VIEWER_PORT; | |
219 | } | |
220 | break; | |
b8aa1682 JD |
221 | case 'd': |
222 | opt_daemon = 1; | |
223 | break; | |
06b55dbb DG |
224 | case 'b': |
225 | opt_background = 1; | |
226 | break; | |
65931c8b MD |
227 | case 'g': |
228 | tracing_group_name = optarg; | |
229 | break; | |
b8aa1682 JD |
230 | case 'h': |
231 | usage(); | |
232 | exit(EXIT_FAILURE); | |
233 | case 'o': | |
234 | ret = asprintf(&opt_output_path, "%s", optarg); | |
235 | if (ret < 0) { | |
236 | PERROR("asprintf opt_output_path"); | |
237 | goto exit; | |
238 | } | |
239 | break; | |
240 | case 'v': | |
241 | /* Verbose level can increase using multiple -v */ | |
242 | lttng_opt_verbose += 1; | |
243 | break; | |
244 | default: | |
245 | /* Unknown option or other error. | |
246 | * Error is printed by getopt, just return */ | |
247 | ret = -1; | |
248 | goto exit; | |
249 | } | |
250 | } | |
251 | ||
252 | /* assign default values */ | |
253 | if (control_uri == NULL) { | |
254 | ret = asprintf(&default_address, "tcp://0.0.0.0:%d", | |
255 | DEFAULT_NETWORK_CONTROL_PORT); | |
256 | if (ret < 0) { | |
257 | PERROR("asprintf default data address"); | |
258 | goto exit; | |
259 | } | |
260 | ||
261 | ret = uri_parse(default_address, &control_uri); | |
262 | free(default_address); | |
263 | if (ret < 0) { | |
264 | ERR("Invalid control URI specified"); | |
265 | goto exit; | |
266 | } | |
267 | } | |
268 | if (data_uri == NULL) { | |
269 | ret = asprintf(&default_address, "tcp://0.0.0.0:%d", | |
270 | DEFAULT_NETWORK_DATA_PORT); | |
271 | if (ret < 0) { | |
272 | PERROR("asprintf default data address"); | |
273 | goto exit; | |
274 | } | |
275 | ||
276 | ret = uri_parse(default_address, &data_uri); | |
277 | free(default_address); | |
278 | if (ret < 0) { | |
279 | ERR("Invalid data URI specified"); | |
280 | goto exit; | |
281 | } | |
282 | } | |
d3e2ba59 JD |
283 | if (live_uri == NULL) { |
284 | ret = asprintf(&default_address, "tcp://0.0.0.0:%d", | |
285 | DEFAULT_NETWORK_VIEWER_PORT); | |
286 | if (ret < 0) { | |
287 | PERROR("asprintf default viewer control address"); | |
288 | goto exit; | |
289 | } | |
290 | ||
291 | ret = uri_parse(default_address, &live_uri); | |
292 | free(default_address); | |
293 | if (ret < 0) { | |
294 | ERR("Invalid viewer control URI specified"); | |
295 | goto exit; | |
296 | } | |
297 | } | |
b8aa1682 JD |
298 | |
299 | exit: | |
300 | return ret; | |
301 | } | |
302 | ||
303 | /* | |
304 | * Cleanup the daemon | |
305 | */ | |
306 | static | |
307 | void cleanup(void) | |
308 | { | |
b8aa1682 JD |
309 | DBG("Cleaning up"); |
310 | ||
095a4ae5 MD |
311 | /* free the dynamically allocated opt_output_path */ |
312 | free(opt_output_path); | |
313 | ||
a02de639 CB |
314 | /* Close thread quit pipes */ |
315 | utils_close_pipe(thread_quit_pipe); | |
316 | ||
710c1f73 DG |
317 | uri_free(control_uri); |
318 | uri_free(data_uri); | |
a6a062a4 | 319 | /* Live URI is freed in the live thread. */ |
b8aa1682 JD |
320 | } |
321 | ||
322 | /* | |
323 | * Write to writable pipe used to notify a thread. | |
324 | */ | |
325 | static | |
326 | int notify_thread_pipe(int wpipe) | |
327 | { | |
6cd525e8 | 328 | ssize_t ret; |
b8aa1682 | 329 | |
6cd525e8 MD |
330 | ret = lttng_write(wpipe, "!", 1); |
331 | if (ret < 1) { | |
b8aa1682 JD |
332 | PERROR("write poll pipe"); |
333 | } | |
334 | ||
335 | return ret; | |
336 | } | |
337 | ||
65931c8b MD |
338 | static void notify_health_quit_pipe(int *pipe) |
339 | { | |
6cd525e8 | 340 | ssize_t ret; |
65931c8b | 341 | |
6cd525e8 MD |
342 | ret = lttng_write(pipe[1], "4", 1); |
343 | if (ret < 1) { | |
65931c8b MD |
344 | PERROR("write relay health quit"); |
345 | } | |
346 | } | |
347 | ||
b8aa1682 JD |
348 | /* |
349 | * Stop all threads by closing the thread quit pipe. | |
350 | */ | |
351 | static | |
352 | void stop_threads(void) | |
353 | { | |
354 | int ret; | |
355 | ||
356 | /* Stopping all threads */ | |
357 | DBG("Terminating all threads"); | |
358 | ret = notify_thread_pipe(thread_quit_pipe[1]); | |
359 | if (ret < 0) { | |
360 | ERR("write error on thread quit pipe"); | |
361 | } | |
362 | ||
65931c8b MD |
363 | notify_health_quit_pipe(health_quit_pipe); |
364 | ||
b8aa1682 | 365 | /* Dispatch thread */ |
26c9d55e | 366 | CMM_STORE_SHARED(dispatch_thread_exit, 1); |
e85cdca9 | 367 | futex_nto1_wake(&relay_conn_queue.futex); |
b8aa1682 JD |
368 | } |
369 | ||
370 | /* | |
371 | * Signal handler for the daemon | |
372 | * | |
373 | * Simply stop all worker threads, leaving main() return gracefully after | |
374 | * joining all threads and calling cleanup(). | |
375 | */ | |
376 | static | |
377 | void sighandler(int sig) | |
378 | { | |
379 | switch (sig) { | |
380 | case SIGPIPE: | |
381 | DBG("SIGPIPE caught"); | |
382 | return; | |
383 | case SIGINT: | |
384 | DBG("SIGINT caught"); | |
385 | stop_threads(); | |
386 | break; | |
387 | case SIGTERM: | |
388 | DBG("SIGTERM caught"); | |
389 | stop_threads(); | |
390 | break; | |
02a6bb53 MD |
391 | case SIGUSR1: |
392 | CMM_STORE_SHARED(recv_child_signal, 1); | |
393 | break; | |
b8aa1682 JD |
394 | default: |
395 | break; | |
396 | } | |
397 | } | |
398 | ||
399 | /* | |
400 | * Setup signal handler for : | |
401 | * SIGINT, SIGTERM, SIGPIPE | |
402 | */ | |
403 | static | |
404 | int set_signal_handler(void) | |
405 | { | |
406 | int ret = 0; | |
407 | struct sigaction sa; | |
408 | sigset_t sigset; | |
409 | ||
410 | if ((ret = sigemptyset(&sigset)) < 0) { | |
411 | PERROR("sigemptyset"); | |
412 | return ret; | |
413 | } | |
414 | ||
415 | sa.sa_handler = sighandler; | |
416 | sa.sa_mask = sigset; | |
417 | sa.sa_flags = 0; | |
418 | if ((ret = sigaction(SIGTERM, &sa, NULL)) < 0) { | |
419 | PERROR("sigaction"); | |
420 | return ret; | |
421 | } | |
422 | ||
423 | if ((ret = sigaction(SIGINT, &sa, NULL)) < 0) { | |
424 | PERROR("sigaction"); | |
425 | return ret; | |
426 | } | |
427 | ||
428 | if ((ret = sigaction(SIGPIPE, &sa, NULL)) < 0) { | |
429 | PERROR("sigaction"); | |
430 | return ret; | |
431 | } | |
432 | ||
02a6bb53 MD |
433 | if ((ret = sigaction(SIGUSR1, &sa, NULL)) < 0) { |
434 | PERROR("sigaction"); | |
435 | return ret; | |
436 | } | |
437 | ||
438 | DBG("Signal handler set for SIGTERM, SIGUSR1, SIGPIPE and SIGINT"); | |
b8aa1682 JD |
439 | |
440 | return ret; | |
441 | } | |
442 | ||
02a6bb53 MD |
443 | void lttng_relay_notify_ready(void) |
444 | { | |
445 | /* Notify the parent of the fork() process that we are ready. */ | |
446 | if (opt_daemon || opt_background) { | |
447 | if (uatomic_sub_return(<tng_relay_ready, 1) == 0) { | |
448 | kill(child_ppid, SIGUSR1); | |
449 | } | |
450 | } | |
451 | } | |
452 | ||
b8aa1682 JD |
453 | /* |
454 | * Init thread quit pipe. | |
455 | * | |
456 | * Return -1 on error or 0 if all pipes are created. | |
457 | */ | |
458 | static | |
459 | int init_thread_quit_pipe(void) | |
460 | { | |
a02de639 | 461 | int ret; |
b8aa1682 | 462 | |
a02de639 | 463 | ret = utils_create_pipe_cloexec(thread_quit_pipe); |
b8aa1682 | 464 | |
b8aa1682 JD |
465 | return ret; |
466 | } | |
467 | ||
468 | /* | |
469 | * Create a poll set with O_CLOEXEC and add the thread quit pipe to the set. | |
470 | */ | |
471 | static | |
472 | int create_thread_poll_set(struct lttng_poll_event *events, int size) | |
473 | { | |
474 | int ret; | |
475 | ||
476 | if (events == NULL || size == 0) { | |
477 | ret = -1; | |
478 | goto error; | |
479 | } | |
480 | ||
481 | ret = lttng_poll_create(events, size, LTTNG_CLOEXEC); | |
482 | if (ret < 0) { | |
483 | goto error; | |
484 | } | |
485 | ||
486 | /* Add quit pipe */ | |
532ed517 | 487 | ret = lttng_poll_add(events, thread_quit_pipe[0], LPOLLIN | LPOLLERR); |
b8aa1682 JD |
488 | if (ret < 0) { |
489 | goto error; | |
490 | } | |
491 | ||
492 | return 0; | |
493 | ||
494 | error: | |
495 | return ret; | |
496 | } | |
497 | ||
498 | /* | |
499 | * Check if the thread quit pipe was triggered. | |
500 | * | |
501 | * Return 1 if it was triggered else 0; | |
502 | */ | |
503 | static | |
504 | int check_thread_quit_pipe(int fd, uint32_t events) | |
505 | { | |
506 | if (fd == thread_quit_pipe[0] && (events & LPOLLIN)) { | |
507 | return 1; | |
508 | } | |
509 | ||
510 | return 0; | |
511 | } | |
512 | ||
513 | /* | |
514 | * Create and init socket from uri. | |
515 | */ | |
516 | static | |
517 | struct lttcomm_sock *relay_init_sock(struct lttng_uri *uri) | |
518 | { | |
519 | int ret; | |
520 | struct lttcomm_sock *sock = NULL; | |
521 | ||
522 | sock = lttcomm_alloc_sock_from_uri(uri); | |
523 | if (sock == NULL) { | |
524 | ERR("Allocating socket"); | |
525 | goto error; | |
526 | } | |
527 | ||
528 | ret = lttcomm_create_sock(sock); | |
529 | if (ret < 0) { | |
530 | goto error; | |
531 | } | |
532 | DBG("Listening on sock %d", sock->fd); | |
533 | ||
534 | ret = sock->ops->bind(sock); | |
535 | if (ret < 0) { | |
536 | goto error; | |
537 | } | |
538 | ||
539 | ret = sock->ops->listen(sock, -1); | |
540 | if (ret < 0) { | |
541 | goto error; | |
542 | ||
543 | } | |
544 | ||
545 | return sock; | |
546 | ||
547 | error: | |
548 | if (sock) { | |
549 | lttcomm_destroy_sock(sock); | |
550 | } | |
551 | return NULL; | |
552 | } | |
553 | ||
173af62f DG |
554 | /* |
555 | * Return nonzero if stream needs to be closed. | |
556 | */ | |
557 | static | |
558 | int close_stream_check(struct relay_stream *stream) | |
559 | { | |
173af62f | 560 | if (stream->close_flag && stream->prev_seq == stream->last_net_seq_num) { |
f7079f67 DG |
561 | /* |
562 | * We are about to close the stream so set the data pending flag to 1 | |
563 | * which will make the end data pending command skip the stream which | |
564 | * is now closed and ready. Note that after proceeding to a file close, | |
565 | * the written file is ready for reading. | |
566 | */ | |
567 | stream->data_pending_check_done = 1; | |
173af62f DG |
568 | return 1; |
569 | } | |
570 | return 0; | |
571 | } | |
572 | ||
eb702af5 DG |
573 | static void try_close_stream(struct relay_session *session, |
574 | struct relay_stream *stream) | |
575 | { | |
576 | int ret; | |
577 | struct ctf_trace *ctf_trace; | |
578 | ||
579 | assert(session); | |
580 | assert(stream); | |
581 | ||
582 | if (!close_stream_check(stream)) { | |
583 | /* Can't close it, not ready for that. */ | |
584 | goto end; | |
585 | } | |
586 | ||
587 | ctf_trace = ctf_trace_find_by_path(session->ctf_traces_ht, | |
588 | stream->path_name); | |
589 | assert(ctf_trace); | |
590 | ||
591 | pthread_mutex_lock(&session->viewer_ready_lock); | |
592 | ctf_trace->invalid_flag = 1; | |
593 | pthread_mutex_unlock(&session->viewer_ready_lock); | |
594 | ||
595 | ret = stream_close(session, stream); | |
50979bcd | 596 | if (ret || session->snapshot) { |
eb702af5 DG |
597 | /* Already close thus the ctf trace is being or has been destroyed. */ |
598 | goto end; | |
599 | } | |
600 | ||
601 | ctf_trace_try_destroy(session, ctf_trace); | |
602 | ||
603 | end: | |
604 | return; | |
605 | } | |
606 | ||
b8aa1682 JD |
607 | /* |
608 | * This thread manages the listening for new connections on the network | |
609 | */ | |
610 | static | |
611 | void *relay_thread_listener(void *data) | |
612 | { | |
095a4ae5 | 613 | int i, ret, pollfd, err = -1; |
b8aa1682 JD |
614 | uint32_t revents, nb_fd; |
615 | struct lttng_poll_event events; | |
616 | struct lttcomm_sock *control_sock, *data_sock; | |
617 | ||
b8aa1682 JD |
618 | DBG("[thread] Relay listener started"); |
619 | ||
55706a7d MD |
620 | health_register(health_relayd, HEALTH_RELAYD_TYPE_LISTENER); |
621 | ||
f385ae0a MD |
622 | health_code_update(); |
623 | ||
b8aa1682 JD |
624 | control_sock = relay_init_sock(control_uri); |
625 | if (!control_sock) { | |
095a4ae5 | 626 | goto error_sock_control; |
b8aa1682 JD |
627 | } |
628 | ||
629 | data_sock = relay_init_sock(data_uri); | |
630 | if (!data_sock) { | |
095a4ae5 | 631 | goto error_sock_relay; |
b8aa1682 JD |
632 | } |
633 | ||
634 | /* | |
635 | * Pass 3 as size here for the thread quit pipe, control and data socket. | |
636 | */ | |
637 | ret = create_thread_poll_set(&events, 3); | |
638 | if (ret < 0) { | |
639 | goto error_create_poll; | |
640 | } | |
641 | ||
642 | /* Add the control socket */ | |
643 | ret = lttng_poll_add(&events, control_sock->fd, LPOLLIN | LPOLLRDHUP); | |
644 | if (ret < 0) { | |
645 | goto error_poll_add; | |
646 | } | |
647 | ||
648 | /* Add the data socket */ | |
649 | ret = lttng_poll_add(&events, data_sock->fd, LPOLLIN | LPOLLRDHUP); | |
650 | if (ret < 0) { | |
651 | goto error_poll_add; | |
652 | } | |
653 | ||
02a6bb53 MD |
654 | lttng_relay_notify_ready(); |
655 | ||
4e5b8b82 MD |
656 | if (testpoint(relayd_thread_listener)) { |
657 | goto error_testpoint; | |
658 | } | |
659 | ||
b8aa1682 | 660 | while (1) { |
f385ae0a MD |
661 | health_code_update(); |
662 | ||
b8aa1682 JD |
663 | DBG("Listener accepting connections"); |
664 | ||
b8aa1682 | 665 | restart: |
f385ae0a | 666 | health_poll_entry(); |
b8aa1682 | 667 | ret = lttng_poll_wait(&events, -1); |
f385ae0a | 668 | health_poll_exit(); |
b8aa1682 JD |
669 | if (ret < 0) { |
670 | /* | |
671 | * Restart interrupted system call. | |
672 | */ | |
673 | if (errno == EINTR) { | |
674 | goto restart; | |
675 | } | |
676 | goto error; | |
677 | } | |
678 | ||
0d9c5d77 DG |
679 | nb_fd = ret; |
680 | ||
b8aa1682 JD |
681 | DBG("Relay new connection received"); |
682 | for (i = 0; i < nb_fd; i++) { | |
f385ae0a MD |
683 | health_code_update(); |
684 | ||
b8aa1682 JD |
685 | /* Fetch once the poll data */ |
686 | revents = LTTNG_POLL_GETEV(&events, i); | |
687 | pollfd = LTTNG_POLL_GETFD(&events, i); | |
688 | ||
689 | /* Thread quit pipe has been closed. Killing thread. */ | |
690 | ret = check_thread_quit_pipe(pollfd, revents); | |
691 | if (ret) { | |
095a4ae5 MD |
692 | err = 0; |
693 | goto exit; | |
b8aa1682 JD |
694 | } |
695 | ||
696 | if (revents & (LPOLLERR | LPOLLHUP | LPOLLRDHUP)) { | |
697 | ERR("socket poll error"); | |
698 | goto error; | |
699 | } else if (revents & LPOLLIN) { | |
4b7f17b2 | 700 | /* |
e85cdca9 DG |
701 | * Get allocated in this thread, enqueued to a global queue, |
702 | * dequeued and freed in the worker thread. | |
4b7f17b2 | 703 | */ |
e85cdca9 DG |
704 | int val = 1; |
705 | struct relay_connection *new_conn; | |
4b7f17b2 | 706 | struct lttcomm_sock *newsock; |
b8aa1682 | 707 | |
e85cdca9 DG |
708 | new_conn = connection_create(); |
709 | if (!new_conn) { | |
b8aa1682 JD |
710 | goto error; |
711 | } | |
712 | ||
713 | if (pollfd == data_sock->fd) { | |
e85cdca9 | 714 | new_conn->type = RELAY_DATA; |
b8aa1682 | 715 | newsock = data_sock->ops->accept(data_sock); |
e85cdca9 DG |
716 | DBG("Relay data connection accepted, socket %d", |
717 | newsock->fd); | |
4b7f17b2 MD |
718 | } else { |
719 | assert(pollfd == control_sock->fd); | |
e85cdca9 | 720 | new_conn->type = RELAY_CONTROL; |
b8aa1682 | 721 | newsock = control_sock->ops->accept(control_sock); |
e85cdca9 DG |
722 | DBG("Relay control connection accepted, socket %d", |
723 | newsock->fd); | |
b8aa1682 | 724 | } |
e85cdca9 DG |
725 | if (!newsock) { |
726 | PERROR("accepting sock"); | |
727 | connection_free(new_conn); | |
728 | goto error; | |
729 | } | |
730 | ||
731 | ret = setsockopt(newsock->fd, SOL_SOCKET, SO_REUSEADDR, &val, | |
732 | sizeof(val)); | |
b8aa1682 JD |
733 | if (ret < 0) { |
734 | PERROR("setsockopt inet"); | |
4b7f17b2 | 735 | lttcomm_destroy_sock(newsock); |
e85cdca9 | 736 | connection_free(new_conn); |
b8aa1682 JD |
737 | goto error; |
738 | } | |
e85cdca9 DG |
739 | new_conn->sock = newsock; |
740 | ||
741 | /* Enqueue request for the dispatcher thread. */ | |
742 | cds_wfq_enqueue(&relay_conn_queue.queue, &new_conn->qnode); | |
b8aa1682 JD |
743 | |
744 | /* | |
e85cdca9 DG |
745 | * Wake the dispatch queue futex. Implicit memory barrier with |
746 | * the exchange in cds_wfq_enqueue. | |
b8aa1682 | 747 | */ |
e85cdca9 | 748 | futex_nto1_wake(&relay_conn_queue.futex); |
b8aa1682 JD |
749 | } |
750 | } | |
751 | } | |
752 | ||
095a4ae5 | 753 | exit: |
b8aa1682 JD |
754 | error: |
755 | error_poll_add: | |
4e5b8b82 | 756 | error_testpoint: |
b8aa1682 JD |
757 | lttng_poll_clean(&events); |
758 | error_create_poll: | |
095a4ae5 MD |
759 | if (data_sock->fd >= 0) { |
760 | ret = data_sock->ops->close(data_sock); | |
b8aa1682 JD |
761 | if (ret) { |
762 | PERROR("close"); | |
763 | } | |
b8aa1682 | 764 | } |
095a4ae5 MD |
765 | lttcomm_destroy_sock(data_sock); |
766 | error_sock_relay: | |
767 | if (control_sock->fd >= 0) { | |
768 | ret = control_sock->ops->close(control_sock); | |
b8aa1682 JD |
769 | if (ret) { |
770 | PERROR("close"); | |
771 | } | |
b8aa1682 | 772 | } |
095a4ae5 MD |
773 | lttcomm_destroy_sock(control_sock); |
774 | error_sock_control: | |
775 | if (err) { | |
f385ae0a MD |
776 | health_error(); |
777 | ERR("Health error occurred in %s", __func__); | |
095a4ae5 | 778 | } |
55706a7d | 779 | health_unregister(health_relayd); |
b8aa1682 JD |
780 | DBG("Relay listener thread cleanup complete"); |
781 | stop_threads(); | |
b8aa1682 JD |
782 | return NULL; |
783 | } | |
784 | ||
785 | /* | |
786 | * This thread manages the dispatching of the requests to worker threads | |
787 | */ | |
788 | static | |
789 | void *relay_thread_dispatcher(void *data) | |
790 | { | |
6cd525e8 MD |
791 | int err = -1; |
792 | ssize_t ret; | |
b8aa1682 | 793 | struct cds_wfq_node *node; |
e85cdca9 | 794 | struct relay_connection *new_conn = NULL; |
b8aa1682 JD |
795 | |
796 | DBG("[thread] Relay dispatcher started"); | |
797 | ||
55706a7d MD |
798 | health_register(health_relayd, HEALTH_RELAYD_TYPE_DISPATCHER); |
799 | ||
4e5b8b82 MD |
800 | if (testpoint(relayd_thread_dispatcher)) { |
801 | goto error_testpoint; | |
802 | } | |
803 | ||
f385ae0a MD |
804 | health_code_update(); |
805 | ||
26c9d55e | 806 | while (!CMM_LOAD_SHARED(dispatch_thread_exit)) { |
f385ae0a MD |
807 | health_code_update(); |
808 | ||
b8aa1682 | 809 | /* Atomically prepare the queue futex */ |
e85cdca9 | 810 | futex_nto1_prepare(&relay_conn_queue.futex); |
b8aa1682 JD |
811 | |
812 | do { | |
f385ae0a MD |
813 | health_code_update(); |
814 | ||
b8aa1682 | 815 | /* Dequeue commands */ |
e85cdca9 | 816 | node = cds_wfq_dequeue_blocking(&relay_conn_queue.queue); |
b8aa1682 JD |
817 | if (node == NULL) { |
818 | DBG("Woken up but nothing in the relay command queue"); | |
819 | /* Continue thread execution */ | |
820 | break; | |
821 | } | |
e85cdca9 | 822 | new_conn = caa_container_of(node, struct relay_connection, qnode); |
b8aa1682 | 823 | |
e85cdca9 | 824 | DBG("Dispatching request waiting on sock %d", new_conn->sock->fd); |
b8aa1682 JD |
825 | |
826 | /* | |
e85cdca9 DG |
827 | * Inform worker thread of the new request. This call is blocking |
828 | * so we can be assured that the data will be read at some point in | |
829 | * time or wait to the end of the world :) | |
b8aa1682 | 830 | */ |
e85cdca9 DG |
831 | ret = lttng_write(relay_conn_pipe[1], &new_conn, sizeof(new_conn)); |
832 | if (ret < 0) { | |
833 | PERROR("write connection pipe"); | |
834 | connection_destroy(new_conn); | |
b8aa1682 JD |
835 | goto error; |
836 | } | |
837 | } while (node != NULL); | |
838 | ||
839 | /* Futex wait on queue. Blocking call on futex() */ | |
f385ae0a | 840 | health_poll_entry(); |
e85cdca9 | 841 | futex_nto1_wait(&relay_conn_queue.futex); |
f385ae0a | 842 | health_poll_exit(); |
b8aa1682 JD |
843 | } |
844 | ||
f385ae0a MD |
845 | /* Normal exit, no error */ |
846 | err = 0; | |
847 | ||
b8aa1682 | 848 | error: |
4e5b8b82 | 849 | error_testpoint: |
f385ae0a MD |
850 | if (err) { |
851 | health_error(); | |
852 | ERR("Health error occurred in %s", __func__); | |
853 | } | |
55706a7d | 854 | health_unregister(health_relayd); |
b8aa1682 JD |
855 | DBG("Dispatch thread dying"); |
856 | stop_threads(); | |
857 | return NULL; | |
858 | } | |
859 | ||
eb702af5 | 860 | static void try_close_streams(struct relay_session *session) |
d3e2ba59 | 861 | { |
eb702af5 | 862 | struct ctf_trace *ctf_trace; |
94d49140 JD |
863 | struct lttng_ht_iter iter; |
864 | ||
eb702af5 | 865 | assert(session); |
94d49140 | 866 | |
eb702af5 DG |
867 | pthread_mutex_lock(&session->viewer_ready_lock); |
868 | rcu_read_lock(); | |
869 | cds_lfht_for_each_entry(session->ctf_traces_ht->ht, &iter.iter, ctf_trace, | |
870 | node.node) { | |
871 | struct relay_stream *stream; | |
94d49140 | 872 | |
eb702af5 DG |
873 | /* Close streams. */ |
874 | cds_list_for_each_entry(stream, &ctf_trace->stream_list, trace_list) { | |
875 | stream_close(session, stream); | |
94d49140 | 876 | } |
94d49140 | 877 | |
eb702af5 DG |
878 | ctf_trace->invalid_flag = 1; |
879 | ctf_trace_try_destroy(session, ctf_trace); | |
157df586 | 880 | } |
eb702af5 DG |
881 | rcu_read_unlock(); |
882 | pthread_mutex_unlock(&session->viewer_ready_lock); | |
94d49140 JD |
883 | } |
884 | ||
b8aa1682 | 885 | /* |
eb702af5 | 886 | * Try to destroy a session within a connection. |
b8aa1682 | 887 | */ |
e85cdca9 | 888 | static void destroy_session(struct relay_session *session, |
d3e2ba59 | 889 | struct lttng_ht *sessions_ht) |
b8aa1682 | 890 | { |
e85cdca9 | 891 | assert(session); |
eb702af5 | 892 | assert(sessions_ht); |
b8aa1682 | 893 | |
eb702af5 | 894 | /* Indicate that this session can be destroyed from now on. */ |
e85cdca9 | 895 | session->close_flag = 1; |
b8aa1682 | 896 | |
e85cdca9 | 897 | try_close_streams(session); |
5b6d8097 | 898 | |
eb702af5 DG |
899 | /* |
900 | * This will try to delete and destroy the session if no viewer is attached | |
901 | * to it meaning the refcount is down to zero. | |
902 | */ | |
e85cdca9 | 903 | session_try_destroy(sessions_ht, session); |
b8aa1682 JD |
904 | } |
905 | ||
1c20f0e2 JD |
906 | /* |
907 | * Copy index data from the control port to a given index object. | |
908 | */ | |
909 | static void copy_index_control_data(struct relay_index *index, | |
910 | struct lttcomm_relayd_index *data) | |
911 | { | |
912 | assert(index); | |
913 | assert(data); | |
914 | ||
915 | /* | |
916 | * The index on disk is encoded in big endian, so we don't need to convert | |
917 | * the data received on the network. The data_offset value is NEVER | |
918 | * modified here and is updated by the data thread. | |
919 | */ | |
920 | index->index_data.packet_size = data->packet_size; | |
921 | index->index_data.content_size = data->content_size; | |
922 | index->index_data.timestamp_begin = data->timestamp_begin; | |
923 | index->index_data.timestamp_end = data->timestamp_end; | |
924 | index->index_data.events_discarded = data->events_discarded; | |
925 | index->index_data.stream_id = data->stream_id; | |
926 | } | |
927 | ||
c5b6f4f0 DG |
928 | /* |
929 | * Handle the RELAYD_CREATE_SESSION command. | |
930 | * | |
931 | * On success, send back the session id or else return a negative value. | |
932 | */ | |
933 | static | |
934 | int relay_create_session(struct lttcomm_relayd_hdr *recv_hdr, | |
e85cdca9 | 935 | struct relay_connection *conn) |
c5b6f4f0 DG |
936 | { |
937 | int ret = 0, send_ret; | |
938 | struct relay_session *session; | |
939 | struct lttcomm_relayd_status_session reply; | |
940 | ||
941 | assert(recv_hdr); | |
e85cdca9 | 942 | assert(conn); |
c5b6f4f0 DG |
943 | |
944 | memset(&reply, 0, sizeof(reply)); | |
945 | ||
eb702af5 DG |
946 | session = session_create(); |
947 | if (!session) { | |
c5b6f4f0 DG |
948 | ret = -1; |
949 | goto error; | |
950 | } | |
e85cdca9 DG |
951 | session->minor = conn->minor; |
952 | session->major = conn->major; | |
953 | conn->session_id = session->id; | |
954 | conn->session = session; | |
c5b6f4f0 DG |
955 | |
956 | reply.session_id = htobe64(session->id); | |
957 | ||
e85cdca9 | 958 | switch (conn->minor) { |
eb702af5 DG |
959 | case 1: |
960 | case 2: | |
961 | case 3: | |
962 | break; | |
963 | case 4: /* LTTng sessiond 2.4 */ | |
964 | default: | |
e85cdca9 | 965 | ret = cmd_create_session_2_4(conn, session); |
d3e2ba59 JD |
966 | } |
967 | ||
e85cdca9 | 968 | lttng_ht_add_unique_u64(conn->sessions_ht, &session->session_n); |
c5b6f4f0 DG |
969 | DBG("Created session %" PRIu64, session->id); |
970 | ||
971 | error: | |
972 | if (ret < 0) { | |
973 | reply.ret_code = htobe32(LTTNG_ERR_FATAL); | |
974 | } else { | |
975 | reply.ret_code = htobe32(LTTNG_OK); | |
976 | } | |
977 | ||
e85cdca9 | 978 | send_ret = conn->sock->ops->sendmsg(conn->sock, &reply, sizeof(reply), 0); |
c5b6f4f0 DG |
979 | if (send_ret < 0) { |
980 | ERR("Relayd sending session id"); | |
4169f5ad | 981 | ret = send_ret; |
c5b6f4f0 DG |
982 | } |
983 | ||
984 | return ret; | |
985 | } | |
986 | ||
814fcae4 JD |
987 | /* |
988 | * When we have received all the streams and the metadata for a channel, | |
989 | * we make them visible to the viewer threads. | |
990 | */ | |
991 | static | |
e85cdca9 | 992 | void set_viewer_ready_flag(struct relay_connection *conn) |
814fcae4 | 993 | { |
eb702af5 | 994 | struct relay_stream *stream, *tmp_stream; |
814fcae4 | 995 | |
e85cdca9 DG |
996 | pthread_mutex_lock(&conn->session->viewer_ready_lock); |
997 | cds_list_for_each_entry_safe(stream, tmp_stream, &conn->recv_head, | |
eb702af5 | 998 | recv_list) { |
814fcae4 | 999 | stream->viewer_ready = 1; |
eb702af5 | 1000 | cds_list_del(&stream->recv_list); |
814fcae4 | 1001 | } |
e85cdca9 | 1002 | pthread_mutex_unlock(&conn->session->viewer_ready_lock); |
814fcae4 JD |
1003 | return; |
1004 | } | |
1005 | ||
1006 | /* | |
1007 | * Add a recv handle node to the connection recv list with the given stream | |
1008 | * handle. A new node is allocated thus must be freed when the node is deleted | |
1009 | * from the list. | |
1010 | */ | |
e85cdca9 DG |
1011 | static void queue_stream(struct relay_stream *stream, |
1012 | struct relay_connection *conn) | |
814fcae4 | 1013 | { |
e85cdca9 | 1014 | assert(conn); |
eb702af5 | 1015 | assert(stream); |
814fcae4 | 1016 | |
e85cdca9 | 1017 | cds_list_add(&stream->recv_list, &conn->recv_head); |
814fcae4 JD |
1018 | } |
1019 | ||
b8aa1682 JD |
1020 | /* |
1021 | * relay_add_stream: allocate a new stream for a session | |
1022 | */ | |
1023 | static | |
1024 | int relay_add_stream(struct lttcomm_relayd_hdr *recv_hdr, | |
e85cdca9 | 1025 | struct relay_connection *conn) |
b8aa1682 | 1026 | { |
eb702af5 | 1027 | int ret, send_ret; |
e85cdca9 | 1028 | struct relay_session *session = conn->session; |
b8aa1682 JD |
1029 | struct relay_stream *stream = NULL; |
1030 | struct lttcomm_relayd_status_stream reply; | |
eb702af5 | 1031 | struct ctf_trace *trace; |
b8aa1682 | 1032 | |
e85cdca9 | 1033 | if (!session || conn->version_check_done == 0) { |
b8aa1682 JD |
1034 | ERR("Trying to add a stream before version check"); |
1035 | ret = -1; | |
1036 | goto end_no_session; | |
1037 | } | |
1038 | ||
b8aa1682 JD |
1039 | stream = zmalloc(sizeof(struct relay_stream)); |
1040 | if (stream == NULL) { | |
1041 | PERROR("relay stream zmalloc"); | |
1042 | ret = -1; | |
1043 | goto end_no_session; | |
1044 | } | |
1045 | ||
e85cdca9 | 1046 | switch (conn->minor) { |
0f907de1 | 1047 | case 1: /* LTTng sessiond 2.1 */ |
e85cdca9 | 1048 | ret = cmd_recv_stream_2_1(conn, stream); |
0f907de1 JD |
1049 | break; |
1050 | case 2: /* LTTng sessiond 2.2 */ | |
1051 | default: | |
e85cdca9 | 1052 | ret = cmd_recv_stream_2_2(conn, stream); |
0f907de1 JD |
1053 | break; |
1054 | } | |
1055 | if (ret < 0) { | |
1056 | goto err_free_stream; | |
1057 | } | |
1058 | ||
9d1bbf21 | 1059 | rcu_read_lock(); |
b8aa1682 | 1060 | stream->stream_handle = ++last_relay_stream_id; |
173af62f | 1061 | stream->prev_seq = -1ULL; |
eb702af5 | 1062 | stream->session_id = session->id; |
1c20f0e2 | 1063 | stream->index_fd = -1; |
d3e2ba59 | 1064 | stream->read_index_fd = -1; |
eb702af5 | 1065 | lttng_ht_node_init_u64(&stream->node, stream->stream_handle); |
d3e2ba59 | 1066 | pthread_mutex_init(&stream->lock, NULL); |
b8aa1682 | 1067 | |
0f907de1 | 1068 | ret = utils_mkdir_recursive(stream->path_name, S_IRWXU | S_IRWXG); |
b8aa1682 | 1069 | if (ret < 0) { |
b8aa1682 JD |
1070 | ERR("relay creating output directory"); |
1071 | goto end; | |
1072 | } | |
1073 | ||
be96a7d1 DG |
1074 | /* |
1075 | * No need to use run_as API here because whatever we receives, the relayd | |
1076 | * uses its own credentials for the stream files. | |
1077 | */ | |
0f907de1 | 1078 | ret = utils_create_stream_file(stream->path_name, stream->channel_name, |
1c20f0e2 | 1079 | stream->tracefile_size, 0, relayd_uid, relayd_gid, NULL); |
b8aa1682 | 1080 | if (ret < 0) { |
0f907de1 | 1081 | ERR("Create output file"); |
b8aa1682 JD |
1082 | goto end; |
1083 | } | |
b8aa1682 | 1084 | stream->fd = ret; |
0f907de1 JD |
1085 | if (stream->tracefile_size) { |
1086 | DBG("Tracefile %s/%s_0 created", stream->path_name, stream->channel_name); | |
1087 | } else { | |
1088 | DBG("Tracefile %s/%s created", stream->path_name, stream->channel_name); | |
1089 | } | |
b8aa1682 | 1090 | |
eb702af5 DG |
1091 | trace = ctf_trace_find_by_path(session->ctf_traces_ht, stream->path_name); |
1092 | if (!trace) { | |
1093 | trace = ctf_trace_create(stream->path_name); | |
1094 | if (!trace) { | |
d3e2ba59 JD |
1095 | ret = -1; |
1096 | goto end; | |
1097 | } | |
eb702af5 DG |
1098 | ctf_trace_add(session->ctf_traces_ht, trace); |
1099 | } | |
1100 | ctf_trace_get_ref(trace); | |
1101 | ||
1102 | if (!strncmp(stream->channel_name, DEFAULT_METADATA_NAME, NAME_MAX)) { | |
1103 | stream->metadata_flag = 1; | |
1104 | /* Assign quick reference to the metadata stream in the trace. */ | |
1105 | trace->metadata_stream = stream; | |
d3e2ba59 | 1106 | } |
d3e2ba59 | 1107 | |
814fcae4 | 1108 | /* |
eb702af5 DG |
1109 | * Add the stream in the recv list of the connection. Once the end stream |
1110 | * message is received, this list is emptied and streams are set with the | |
1111 | * viewer ready flag. | |
814fcae4 | 1112 | */ |
e85cdca9 | 1113 | queue_stream(stream, conn); |
814fcae4 | 1114 | |
eb702af5 DG |
1115 | /* |
1116 | * Both in the ctf_trace object and the global stream ht since the data | |
1117 | * side of the relayd does not have the concept of session. | |
1118 | */ | |
1119 | lttng_ht_add_unique_u64(relay_streams_ht, &stream->node); | |
1120 | cds_list_add_tail(&stream->trace_list, &trace->stream_list); | |
b8aa1682 | 1121 | |
87b576ec | 1122 | session->stream_count++; |
d3e2ba59 | 1123 | |
1c20f0e2 JD |
1124 | DBG("Relay new stream added %s with ID %" PRIu64, stream->channel_name, |
1125 | stream->stream_handle); | |
b8aa1682 JD |
1126 | |
1127 | end: | |
239f3aec | 1128 | memset(&reply, 0, sizeof(reply)); |
5af40280 | 1129 | reply.handle = htobe64(stream->stream_handle); |
b8aa1682 JD |
1130 | /* send the session id to the client or a negative return code on error */ |
1131 | if (ret < 0) { | |
f73fabfd | 1132 | reply.ret_code = htobe32(LTTNG_ERR_UNK); |
5af40280 CB |
1133 | /* stream was not properly added to the ht, so free it */ |
1134 | free(stream); | |
b8aa1682 | 1135 | } else { |
f73fabfd | 1136 | reply.ret_code = htobe32(LTTNG_OK); |
b8aa1682 | 1137 | } |
5af40280 | 1138 | |
e85cdca9 | 1139 | send_ret = conn->sock->ops->sendmsg(conn->sock, &reply, |
b8aa1682 JD |
1140 | sizeof(struct lttcomm_relayd_status_stream), 0); |
1141 | if (send_ret < 0) { | |
1142 | ERR("Relay sending stream id"); | |
4169f5ad | 1143 | ret = send_ret; |
b8aa1682 | 1144 | } |
9d1bbf21 | 1145 | rcu_read_unlock(); |
b8aa1682 JD |
1146 | |
1147 | end_no_session: | |
1148 | return ret; | |
0f907de1 JD |
1149 | |
1150 | err_free_stream: | |
1151 | free(stream->path_name); | |
1152 | free(stream->channel_name); | |
1153 | free(stream); | |
1154 | return ret; | |
b8aa1682 JD |
1155 | } |
1156 | ||
173af62f DG |
1157 | /* |
1158 | * relay_close_stream: close a specific stream | |
1159 | */ | |
1160 | static | |
1161 | int relay_close_stream(struct lttcomm_relayd_hdr *recv_hdr, | |
e85cdca9 | 1162 | struct relay_connection *conn) |
173af62f | 1163 | { |
94d49140 | 1164 | int ret, send_ret; |
e85cdca9 | 1165 | struct relay_session *session = conn->session; |
173af62f DG |
1166 | struct lttcomm_relayd_close_stream stream_info; |
1167 | struct lttcomm_relayd_generic_reply reply; | |
1168 | struct relay_stream *stream; | |
173af62f DG |
1169 | |
1170 | DBG("Close stream received"); | |
1171 | ||
e85cdca9 | 1172 | if (!session || conn->version_check_done == 0) { |
173af62f DG |
1173 | ERR("Trying to close a stream before version check"); |
1174 | ret = -1; | |
1175 | goto end_no_session; | |
1176 | } | |
1177 | ||
e85cdca9 | 1178 | ret = conn->sock->ops->recvmsg(conn->sock, &stream_info, |
7c5aef62 | 1179 | sizeof(struct lttcomm_relayd_close_stream), 0); |
173af62f | 1180 | if (ret < sizeof(struct lttcomm_relayd_close_stream)) { |
a6cd2b97 DG |
1181 | if (ret == 0) { |
1182 | /* Orderly shutdown. Not necessary to print an error. */ | |
e85cdca9 | 1183 | DBG("Socket %d did an orderly shutdown", conn->sock->fd); |
a6cd2b97 DG |
1184 | } else { |
1185 | ERR("Relay didn't receive valid add_stream struct size : %d", ret); | |
1186 | } | |
173af62f DG |
1187 | ret = -1; |
1188 | goto end_no_session; | |
1189 | } | |
1190 | ||
1191 | rcu_read_lock(); | |
eb702af5 DG |
1192 | stream = stream_find_by_id(relay_streams_ht, |
1193 | be64toh(stream_info.stream_id)); | |
173af62f DG |
1194 | if (!stream) { |
1195 | ret = -1; | |
1196 | goto end_unlock; | |
1197 | } | |
1198 | ||
8e2583a4 | 1199 | stream->last_net_seq_num = be64toh(stream_info.last_net_seq_num); |
173af62f | 1200 | stream->close_flag = 1; |
87b576ec JD |
1201 | session->stream_count--; |
1202 | assert(session->stream_count >= 0); | |
173af62f | 1203 | |
eb702af5 DG |
1204 | /* Check if we can close it or else the data will do it. */ |
1205 | try_close_stream(session, stream); | |
173af62f DG |
1206 | |
1207 | end_unlock: | |
1208 | rcu_read_unlock(); | |
1209 | ||
239f3aec | 1210 | memset(&reply, 0, sizeof(reply)); |
173af62f | 1211 | if (ret < 0) { |
f73fabfd | 1212 | reply.ret_code = htobe32(LTTNG_ERR_UNK); |
173af62f | 1213 | } else { |
f73fabfd | 1214 | reply.ret_code = htobe32(LTTNG_OK); |
173af62f | 1215 | } |
e85cdca9 | 1216 | send_ret = conn->sock->ops->sendmsg(conn->sock, &reply, |
173af62f DG |
1217 | sizeof(struct lttcomm_relayd_generic_reply), 0); |
1218 | if (send_ret < 0) { | |
1219 | ERR("Relay sending stream id"); | |
4169f5ad | 1220 | ret = send_ret; |
173af62f DG |
1221 | } |
1222 | ||
1223 | end_no_session: | |
1224 | return ret; | |
1225 | } | |
1226 | ||
b8aa1682 JD |
1227 | /* |
1228 | * relay_unknown_command: send -1 if received unknown command | |
1229 | */ | |
1230 | static | |
e85cdca9 | 1231 | void relay_unknown_command(struct relay_connection *conn) |
b8aa1682 JD |
1232 | { |
1233 | struct lttcomm_relayd_generic_reply reply; | |
1234 | int ret; | |
1235 | ||
239f3aec | 1236 | memset(&reply, 0, sizeof(reply)); |
f73fabfd | 1237 | reply.ret_code = htobe32(LTTNG_ERR_UNK); |
e85cdca9 | 1238 | ret = conn->sock->ops->sendmsg(conn->sock, &reply, |
b8aa1682 JD |
1239 | sizeof(struct lttcomm_relayd_generic_reply), 0); |
1240 | if (ret < 0) { | |
1241 | ERR("Relay sending unknown command"); | |
1242 | } | |
1243 | } | |
1244 | ||
1245 | /* | |
1246 | * relay_start: send an acknowledgment to the client to tell if we are | |
1247 | * ready to receive data. We are ready if a session is established. | |
1248 | */ | |
1249 | static | |
1250 | int relay_start(struct lttcomm_relayd_hdr *recv_hdr, | |
e85cdca9 | 1251 | struct relay_connection *conn) |
b8aa1682 | 1252 | { |
f73fabfd | 1253 | int ret = htobe32(LTTNG_OK); |
b8aa1682 | 1254 | struct lttcomm_relayd_generic_reply reply; |
e85cdca9 | 1255 | struct relay_session *session = conn->session; |
b8aa1682 JD |
1256 | |
1257 | if (!session) { | |
1258 | DBG("Trying to start the streaming without a session established"); | |
f73fabfd | 1259 | ret = htobe32(LTTNG_ERR_UNK); |
b8aa1682 JD |
1260 | } |
1261 | ||
239f3aec | 1262 | memset(&reply, 0, sizeof(reply)); |
b8aa1682 | 1263 | reply.ret_code = ret; |
e85cdca9 | 1264 | ret = conn->sock->ops->sendmsg(conn->sock, &reply, |
b8aa1682 JD |
1265 | sizeof(struct lttcomm_relayd_generic_reply), 0); |
1266 | if (ret < 0) { | |
1267 | ERR("Relay sending start ack"); | |
1268 | } | |
1269 | ||
1270 | return ret; | |
1271 | } | |
1272 | ||
1d4dfdef DG |
1273 | /* |
1274 | * Append padding to the file pointed by the file descriptor fd. | |
1275 | */ | |
1276 | static int write_padding_to_file(int fd, uint32_t size) | |
1277 | { | |
6cd525e8 | 1278 | ssize_t ret = 0; |
1d4dfdef DG |
1279 | char *zeros; |
1280 | ||
1281 | if (size == 0) { | |
1282 | goto end; | |
1283 | } | |
1284 | ||
1285 | zeros = zmalloc(size); | |
1286 | if (zeros == NULL) { | |
1287 | PERROR("zmalloc zeros for padding"); | |
1288 | ret = -1; | |
1289 | goto end; | |
1290 | } | |
1291 | ||
6cd525e8 MD |
1292 | ret = lttng_write(fd, zeros, size); |
1293 | if (ret < size) { | |
1d4dfdef DG |
1294 | PERROR("write padding to file"); |
1295 | } | |
1296 | ||
e986c7a1 DG |
1297 | free(zeros); |
1298 | ||
1d4dfdef DG |
1299 | end: |
1300 | return ret; | |
1301 | } | |
1302 | ||
b8aa1682 JD |
1303 | /* |
1304 | * relay_recv_metadata: receive the metada for the session. | |
1305 | */ | |
1306 | static | |
1307 | int relay_recv_metadata(struct lttcomm_relayd_hdr *recv_hdr, | |
e85cdca9 | 1308 | struct relay_connection *conn) |
b8aa1682 | 1309 | { |
f73fabfd | 1310 | int ret = htobe32(LTTNG_OK); |
6cd525e8 | 1311 | ssize_t size_ret; |
e85cdca9 | 1312 | struct relay_session *session = conn->session; |
b8aa1682 JD |
1313 | struct lttcomm_relayd_metadata_payload *metadata_struct; |
1314 | struct relay_stream *metadata_stream; | |
1315 | uint64_t data_size, payload_size; | |
eb702af5 | 1316 | struct ctf_trace *ctf_trace; |
b8aa1682 JD |
1317 | |
1318 | if (!session) { | |
1319 | ERR("Metadata sent before version check"); | |
1320 | ret = -1; | |
1321 | goto end; | |
1322 | } | |
1323 | ||
f6416125 MD |
1324 | data_size = payload_size = be64toh(recv_hdr->data_size); |
1325 | if (data_size < sizeof(struct lttcomm_relayd_metadata_payload)) { | |
1326 | ERR("Incorrect data size"); | |
1327 | ret = -1; | |
1328 | goto end; | |
1329 | } | |
1330 | payload_size -= sizeof(struct lttcomm_relayd_metadata_payload); | |
1331 | ||
b8aa1682 | 1332 | if (data_buffer_size < data_size) { |
d7b3776f | 1333 | /* In case the realloc fails, we can free the memory */ |
c617c0c6 MD |
1334 | char *tmp_data_ptr; |
1335 | ||
1336 | tmp_data_ptr = realloc(data_buffer, data_size); | |
1337 | if (!tmp_data_ptr) { | |
b8aa1682 | 1338 | ERR("Allocating data buffer"); |
c617c0c6 | 1339 | free(data_buffer); |
b8aa1682 JD |
1340 | ret = -1; |
1341 | goto end; | |
1342 | } | |
c617c0c6 | 1343 | data_buffer = tmp_data_ptr; |
b8aa1682 JD |
1344 | data_buffer_size = data_size; |
1345 | } | |
1346 | memset(data_buffer, 0, data_size); | |
77c7c900 | 1347 | DBG2("Relay receiving metadata, waiting for %" PRIu64 " bytes", data_size); |
e85cdca9 | 1348 | ret = conn->sock->ops->recvmsg(conn->sock, data_buffer, data_size, 0); |
b8aa1682 | 1349 | if (ret < 0 || ret != data_size) { |
a6cd2b97 DG |
1350 | if (ret == 0) { |
1351 | /* Orderly shutdown. Not necessary to print an error. */ | |
e85cdca9 | 1352 | DBG("Socket %d did an orderly shutdown", conn->sock->fd); |
a6cd2b97 DG |
1353 | } else { |
1354 | ERR("Relay didn't receive the whole metadata"); | |
1355 | } | |
b8aa1682 | 1356 | ret = -1; |
b8aa1682 JD |
1357 | goto end; |
1358 | } | |
1359 | metadata_struct = (struct lttcomm_relayd_metadata_payload *) data_buffer; | |
9d1bbf21 MD |
1360 | |
1361 | rcu_read_lock(); | |
eb702af5 | 1362 | metadata_stream = stream_find_by_id(relay_streams_ht, |
d3e2ba59 | 1363 | be64toh(metadata_struct->stream_id)); |
b8aa1682 JD |
1364 | if (!metadata_stream) { |
1365 | ret = -1; | |
9d1bbf21 | 1366 | goto end_unlock; |
b8aa1682 JD |
1367 | } |
1368 | ||
6cd525e8 MD |
1369 | size_ret = lttng_write(metadata_stream->fd, metadata_struct->payload, |
1370 | payload_size); | |
1371 | if (size_ret < payload_size) { | |
b8aa1682 JD |
1372 | ERR("Relay error writing metadata on file"); |
1373 | ret = -1; | |
9d1bbf21 | 1374 | goto end_unlock; |
b8aa1682 | 1375 | } |
1d4dfdef DG |
1376 | |
1377 | ret = write_padding_to_file(metadata_stream->fd, | |
1378 | be32toh(metadata_struct->padding_size)); | |
1379 | if (ret < 0) { | |
1380 | goto end_unlock; | |
1381 | } | |
eb702af5 DG |
1382 | |
1383 | ctf_trace = ctf_trace_find_by_path(session->ctf_traces_ht, | |
1384 | metadata_stream->path_name); | |
1385 | assert(ctf_trace); | |
1386 | ctf_trace->metadata_received += | |
d3e2ba59 | 1387 | payload_size + be32toh(metadata_struct->padding_size); |
1d4dfdef | 1388 | |
b8aa1682 JD |
1389 | DBG2("Relay metadata written"); |
1390 | ||
9d1bbf21 | 1391 | end_unlock: |
6e3c5836 | 1392 | rcu_read_unlock(); |
b8aa1682 JD |
1393 | end: |
1394 | return ret; | |
1395 | } | |
1396 | ||
1397 | /* | |
1398 | * relay_send_version: send relayd version number | |
1399 | */ | |
1400 | static | |
1401 | int relay_send_version(struct lttcomm_relayd_hdr *recv_hdr, | |
e85cdca9 | 1402 | struct relay_connection *conn) |
b8aa1682 | 1403 | { |
7f51dcba | 1404 | int ret; |
092b6259 | 1405 | struct lttcomm_relayd_version reply, msg; |
b8aa1682 | 1406 | |
e85cdca9 | 1407 | assert(conn); |
c5b6f4f0 | 1408 | |
e85cdca9 | 1409 | conn->version_check_done = 1; |
b8aa1682 | 1410 | |
092b6259 | 1411 | /* Get version from the other side. */ |
e85cdca9 | 1412 | ret = conn->sock->ops->recvmsg(conn->sock, &msg, sizeof(msg), 0); |
092b6259 | 1413 | if (ret < 0 || ret != sizeof(msg)) { |
a6cd2b97 DG |
1414 | if (ret == 0) { |
1415 | /* Orderly shutdown. Not necessary to print an error. */ | |
e85cdca9 | 1416 | DBG("Socket %d did an orderly shutdown", conn->sock->fd); |
a6cd2b97 DG |
1417 | } else { |
1418 | ERR("Relay failed to receive the version values."); | |
1419 | } | |
092b6259 | 1420 | ret = -1; |
092b6259 DG |
1421 | goto end; |
1422 | } | |
1423 | ||
239f3aec | 1424 | memset(&reply, 0, sizeof(reply)); |
d83a952c MD |
1425 | reply.major = RELAYD_VERSION_COMM_MAJOR; |
1426 | reply.minor = RELAYD_VERSION_COMM_MINOR; | |
d4519fa3 JD |
1427 | |
1428 | /* Major versions must be the same */ | |
1429 | if (reply.major != be32toh(msg.major)) { | |
6151a90f JD |
1430 | DBG("Incompatible major versions (%u vs %u), deleting session", |
1431 | reply.major, be32toh(msg.major)); | |
e85cdca9 | 1432 | destroy_session(conn->session, conn->sessions_ht); |
d4519fa3 JD |
1433 | ret = 0; |
1434 | goto end; | |
1435 | } | |
1436 | ||
e85cdca9 | 1437 | conn->major = reply.major; |
0f907de1 JD |
1438 | /* We adapt to the lowest compatible version */ |
1439 | if (reply.minor <= be32toh(msg.minor)) { | |
e85cdca9 | 1440 | conn->minor = reply.minor; |
0f907de1 | 1441 | } else { |
e85cdca9 | 1442 | conn->minor = be32toh(msg.minor); |
0f907de1 JD |
1443 | } |
1444 | ||
6151a90f JD |
1445 | reply.major = htobe32(reply.major); |
1446 | reply.minor = htobe32(reply.minor); | |
e85cdca9 | 1447 | ret = conn->sock->ops->sendmsg(conn->sock, &reply, |
6151a90f JD |
1448 | sizeof(struct lttcomm_relayd_version), 0); |
1449 | if (ret < 0) { | |
1450 | ERR("Relay sending version"); | |
1451 | } | |
1452 | ||
e85cdca9 DG |
1453 | DBG("Version check done using protocol %u.%u", conn->major, |
1454 | conn->minor); | |
b8aa1682 JD |
1455 | |
1456 | end: | |
1457 | return ret; | |
1458 | } | |
1459 | ||
c8f59ee5 | 1460 | /* |
6d805429 | 1461 | * Check for data pending for a given stream id from the session daemon. |
c8f59ee5 DG |
1462 | */ |
1463 | static | |
6d805429 | 1464 | int relay_data_pending(struct lttcomm_relayd_hdr *recv_hdr, |
e85cdca9 | 1465 | struct relay_connection *conn) |
c8f59ee5 | 1466 | { |
e85cdca9 | 1467 | struct relay_session *session = conn->session; |
6d805429 | 1468 | struct lttcomm_relayd_data_pending msg; |
c8f59ee5 DG |
1469 | struct lttcomm_relayd_generic_reply reply; |
1470 | struct relay_stream *stream; | |
1471 | int ret; | |
c8f59ee5 DG |
1472 | uint64_t last_net_seq_num, stream_id; |
1473 | ||
6d805429 | 1474 | DBG("Data pending command received"); |
c8f59ee5 | 1475 | |
e85cdca9 | 1476 | if (!session || conn->version_check_done == 0) { |
c8f59ee5 DG |
1477 | ERR("Trying to check for data before version check"); |
1478 | ret = -1; | |
1479 | goto end_no_session; | |
1480 | } | |
1481 | ||
e85cdca9 | 1482 | ret = conn->sock->ops->recvmsg(conn->sock, &msg, sizeof(msg), 0); |
c8f59ee5 | 1483 | if (ret < sizeof(msg)) { |
a6cd2b97 DG |
1484 | if (ret == 0) { |
1485 | /* Orderly shutdown. Not necessary to print an error. */ | |
e85cdca9 | 1486 | DBG("Socket %d did an orderly shutdown", conn->sock->fd); |
a6cd2b97 DG |
1487 | } else { |
1488 | ERR("Relay didn't receive valid data_pending struct size : %d", | |
1489 | ret); | |
1490 | } | |
c8f59ee5 DG |
1491 | ret = -1; |
1492 | goto end_no_session; | |
1493 | } | |
1494 | ||
1495 | stream_id = be64toh(msg.stream_id); | |
1496 | last_net_seq_num = be64toh(msg.last_net_seq_num); | |
1497 | ||
1498 | rcu_read_lock(); | |
eb702af5 | 1499 | stream = stream_find_by_id(relay_streams_ht, stream_id); |
de91f48a | 1500 | if (stream == NULL) { |
c8f59ee5 DG |
1501 | ret = -1; |
1502 | goto end_unlock; | |
1503 | } | |
1504 | ||
6d805429 | 1505 | DBG("Data pending for stream id %" PRIu64 " prev_seq %" PRIu64 |
c8f59ee5 DG |
1506 | " and last_seq %" PRIu64, stream_id, stream->prev_seq, |
1507 | last_net_seq_num); | |
1508 | ||
33832e64 | 1509 | /* Avoid wrapping issue */ |
39df6d9f | 1510 | if (((int64_t) (stream->prev_seq - last_net_seq_num)) >= 0) { |
6d805429 | 1511 | /* Data has in fact been written and is NOT pending */ |
c8f59ee5 | 1512 | ret = 0; |
6d805429 DG |
1513 | } else { |
1514 | /* Data still being streamed thus pending */ | |
1515 | ret = 1; | |
c8f59ee5 DG |
1516 | } |
1517 | ||
f7079f67 DG |
1518 | /* Pending check is now done. */ |
1519 | stream->data_pending_check_done = 1; | |
1520 | ||
c8f59ee5 DG |
1521 | end_unlock: |
1522 | rcu_read_unlock(); | |
1523 | ||
239f3aec | 1524 | memset(&reply, 0, sizeof(reply)); |
c8f59ee5 | 1525 | reply.ret_code = htobe32(ret); |
e85cdca9 | 1526 | ret = conn->sock->ops->sendmsg(conn->sock, &reply, sizeof(reply), 0); |
c8f59ee5 | 1527 | if (ret < 0) { |
6d805429 | 1528 | ERR("Relay data pending ret code failed"); |
c8f59ee5 DG |
1529 | } |
1530 | ||
1531 | end_no_session: | |
1532 | return ret; | |
1533 | } | |
1534 | ||
1535 | /* | |
1536 | * Wait for the control socket to reach a quiescent state. | |
1537 | * | |
1538 | * Note that for now, when receiving this command from the session daemon, this | |
1539 | * means that every subsequent commands or data received on the control socket | |
1540 | * has been handled. So, this is why we simply return OK here. | |
1541 | */ | |
1542 | static | |
1543 | int relay_quiescent_control(struct lttcomm_relayd_hdr *recv_hdr, | |
e85cdca9 | 1544 | struct relay_connection *conn) |
c8f59ee5 DG |
1545 | { |
1546 | int ret; | |
ad7051c0 DG |
1547 | uint64_t stream_id; |
1548 | struct relay_stream *stream; | |
1549 | struct lttng_ht_iter iter; | |
1550 | struct lttcomm_relayd_quiescent_control msg; | |
c8f59ee5 DG |
1551 | struct lttcomm_relayd_generic_reply reply; |
1552 | ||
1553 | DBG("Checking quiescent state on control socket"); | |
1554 | ||
e85cdca9 | 1555 | if (!conn->session || conn->version_check_done == 0) { |
ad7051c0 DG |
1556 | ERR("Trying to check for data before version check"); |
1557 | ret = -1; | |
1558 | goto end_no_session; | |
1559 | } | |
1560 | ||
e85cdca9 | 1561 | ret = conn->sock->ops->recvmsg(conn->sock, &msg, sizeof(msg), 0); |
ad7051c0 | 1562 | if (ret < sizeof(msg)) { |
a6cd2b97 DG |
1563 | if (ret == 0) { |
1564 | /* Orderly shutdown. Not necessary to print an error. */ | |
e85cdca9 | 1565 | DBG("Socket %d did an orderly shutdown", conn->sock->fd); |
a6cd2b97 DG |
1566 | } else { |
1567 | ERR("Relay didn't receive valid begin data_pending struct size: %d", | |
1568 | ret); | |
1569 | } | |
ad7051c0 DG |
1570 | ret = -1; |
1571 | goto end_no_session; | |
1572 | } | |
1573 | ||
1574 | stream_id = be64toh(msg.stream_id); | |
1575 | ||
1576 | rcu_read_lock(); | |
d3e2ba59 | 1577 | cds_lfht_for_each_entry(relay_streams_ht->ht, &iter.iter, stream, |
eb702af5 | 1578 | node.node) { |
ad7051c0 DG |
1579 | if (stream->stream_handle == stream_id) { |
1580 | stream->data_pending_check_done = 1; | |
1581 | DBG("Relay quiescent control pending flag set to %" PRIu64, | |
1582 | stream_id); | |
1583 | break; | |
1584 | } | |
1585 | } | |
1586 | rcu_read_unlock(); | |
1587 | ||
239f3aec | 1588 | memset(&reply, 0, sizeof(reply)); |
c8f59ee5 | 1589 | reply.ret_code = htobe32(LTTNG_OK); |
e85cdca9 | 1590 | ret = conn->sock->ops->sendmsg(conn->sock, &reply, sizeof(reply), 0); |
c8f59ee5 | 1591 | if (ret < 0) { |
6d805429 | 1592 | ERR("Relay data quiescent control ret code failed"); |
c8f59ee5 DG |
1593 | } |
1594 | ||
ad7051c0 | 1595 | end_no_session: |
c8f59ee5 DG |
1596 | return ret; |
1597 | } | |
1598 | ||
f7079f67 DG |
1599 | /* |
1600 | * Initialize a data pending command. This means that a client is about to ask | |
1601 | * for data pending for each stream he/she holds. Simply iterate over all | |
1602 | * streams of a session and set the data_pending_check_done flag. | |
1603 | * | |
1604 | * This command returns to the client a LTTNG_OK code. | |
1605 | */ | |
1606 | static | |
1607 | int relay_begin_data_pending(struct lttcomm_relayd_hdr *recv_hdr, | |
e85cdca9 | 1608 | struct relay_connection *conn) |
f7079f67 DG |
1609 | { |
1610 | int ret; | |
1611 | struct lttng_ht_iter iter; | |
1612 | struct lttcomm_relayd_begin_data_pending msg; | |
1613 | struct lttcomm_relayd_generic_reply reply; | |
1614 | struct relay_stream *stream; | |
1615 | uint64_t session_id; | |
1616 | ||
1617 | assert(recv_hdr); | |
e85cdca9 | 1618 | assert(conn); |
f7079f67 DG |
1619 | |
1620 | DBG("Init streams for data pending"); | |
1621 | ||
e85cdca9 | 1622 | if (!conn->session || conn->version_check_done == 0) { |
f7079f67 DG |
1623 | ERR("Trying to check for data before version check"); |
1624 | ret = -1; | |
1625 | goto end_no_session; | |
1626 | } | |
1627 | ||
e85cdca9 | 1628 | ret = conn->sock->ops->recvmsg(conn->sock, &msg, sizeof(msg), 0); |
f7079f67 | 1629 | if (ret < sizeof(msg)) { |
a6cd2b97 DG |
1630 | if (ret == 0) { |
1631 | /* Orderly shutdown. Not necessary to print an error. */ | |
e85cdca9 | 1632 | DBG("Socket %d did an orderly shutdown", conn->sock->fd); |
a6cd2b97 DG |
1633 | } else { |
1634 | ERR("Relay didn't receive valid begin data_pending struct size: %d", | |
1635 | ret); | |
1636 | } | |
f7079f67 DG |
1637 | ret = -1; |
1638 | goto end_no_session; | |
1639 | } | |
1640 | ||
1641 | session_id = be64toh(msg.session_id); | |
1642 | ||
1643 | /* | |
1644 | * Iterate over all streams to set the begin data pending flag. For now, the | |
1645 | * streams are indexed by stream handle so we have to iterate over all | |
1646 | * streams to find the one associated with the right session_id. | |
1647 | */ | |
1648 | rcu_read_lock(); | |
d3e2ba59 | 1649 | cds_lfht_for_each_entry(relay_streams_ht->ht, &iter.iter, stream, |
eb702af5 DG |
1650 | node.node) { |
1651 | if (stream->session_id == session_id) { | |
f7079f67 DG |
1652 | stream->data_pending_check_done = 0; |
1653 | DBG("Set begin data pending flag to stream %" PRIu64, | |
1654 | stream->stream_handle); | |
1655 | } | |
1656 | } | |
1657 | rcu_read_unlock(); | |
1658 | ||
239f3aec | 1659 | memset(&reply, 0, sizeof(reply)); |
f7079f67 DG |
1660 | /* All good, send back reply. */ |
1661 | reply.ret_code = htobe32(LTTNG_OK); | |
1662 | ||
e85cdca9 | 1663 | ret = conn->sock->ops->sendmsg(conn->sock, &reply, sizeof(reply), 0); |
f7079f67 DG |
1664 | if (ret < 0) { |
1665 | ERR("Relay begin data pending send reply failed"); | |
1666 | } | |
1667 | ||
1668 | end_no_session: | |
1669 | return ret; | |
1670 | } | |
1671 | ||
1672 | /* | |
1673 | * End data pending command. This will check, for a given session id, if each | |
1674 | * stream associated with it has its data_pending_check_done flag set. If not, | |
1675 | * this means that the client lost track of the stream but the data is still | |
1676 | * being streamed on our side. In this case, we inform the client that data is | |
1677 | * inflight. | |
1678 | * | |
1679 | * Return to the client if there is data in flight or not with a ret_code. | |
1680 | */ | |
1681 | static | |
1682 | int relay_end_data_pending(struct lttcomm_relayd_hdr *recv_hdr, | |
e85cdca9 | 1683 | struct relay_connection *conn) |
f7079f67 DG |
1684 | { |
1685 | int ret; | |
1686 | struct lttng_ht_iter iter; | |
1687 | struct lttcomm_relayd_end_data_pending msg; | |
1688 | struct lttcomm_relayd_generic_reply reply; | |
1689 | struct relay_stream *stream; | |
1690 | uint64_t session_id; | |
1691 | uint32_t is_data_inflight = 0; | |
1692 | ||
1693 | assert(recv_hdr); | |
e85cdca9 | 1694 | assert(conn); |
f7079f67 DG |
1695 | |
1696 | DBG("End data pending command"); | |
1697 | ||
e85cdca9 | 1698 | if (!conn->session || conn->version_check_done == 0) { |
f7079f67 DG |
1699 | ERR("Trying to check for data before version check"); |
1700 | ret = -1; | |
1701 | goto end_no_session; | |
1702 | } | |
1703 | ||
e85cdca9 | 1704 | ret = conn->sock->ops->recvmsg(conn->sock, &msg, sizeof(msg), 0); |
f7079f67 | 1705 | if (ret < sizeof(msg)) { |
a6cd2b97 DG |
1706 | if (ret == 0) { |
1707 | /* Orderly shutdown. Not necessary to print an error. */ | |
e85cdca9 | 1708 | DBG("Socket %d did an orderly shutdown", conn->sock->fd); |
a6cd2b97 DG |
1709 | } else { |
1710 | ERR("Relay didn't receive valid end data_pending struct size: %d", | |
1711 | ret); | |
1712 | } | |
f7079f67 DG |
1713 | ret = -1; |
1714 | goto end_no_session; | |
1715 | } | |
1716 | ||
1717 | session_id = be64toh(msg.session_id); | |
1718 | ||
1719 | /* Iterate over all streams to see if the begin data pending flag is set. */ | |
1720 | rcu_read_lock(); | |
d3e2ba59 | 1721 | cds_lfht_for_each_entry(relay_streams_ht->ht, &iter.iter, stream, |
eb702af5 DG |
1722 | node.node) { |
1723 | if (stream->session_id == session_id && | |
50979bcd | 1724 | !stream->data_pending_check_done && !stream->terminated_flag) { |
f7079f67 DG |
1725 | is_data_inflight = 1; |
1726 | DBG("Data is still in flight for stream %" PRIu64, | |
1727 | stream->stream_handle); | |
1728 | break; | |
1729 | } | |
1730 | } | |
1731 | rcu_read_unlock(); | |
1732 | ||
239f3aec | 1733 | memset(&reply, 0, sizeof(reply)); |
f7079f67 DG |
1734 | /* All good, send back reply. */ |
1735 | reply.ret_code = htobe32(is_data_inflight); | |
1736 | ||
e85cdca9 | 1737 | ret = conn->sock->ops->sendmsg(conn->sock, &reply, sizeof(reply), 0); |
f7079f67 DG |
1738 | if (ret < 0) { |
1739 | ERR("Relay end data pending send reply failed"); | |
1740 | } | |
1741 | ||
1742 | end_no_session: | |
1743 | return ret; | |
1744 | } | |
1745 | ||
1c20f0e2 JD |
1746 | /* |
1747 | * Receive an index for a specific stream. | |
1748 | * | |
1749 | * Return 0 on success else a negative value. | |
1750 | */ | |
1751 | static | |
1752 | int relay_recv_index(struct lttcomm_relayd_hdr *recv_hdr, | |
e85cdca9 | 1753 | struct relay_connection *conn) |
1c20f0e2 JD |
1754 | { |
1755 | int ret, send_ret, index_created = 0; | |
e85cdca9 | 1756 | struct relay_session *session = conn->session; |
1c20f0e2 JD |
1757 | struct lttcomm_relayd_index index_info; |
1758 | struct relay_index *index, *wr_index = NULL; | |
1759 | struct lttcomm_relayd_generic_reply reply; | |
1760 | struct relay_stream *stream; | |
1761 | uint64_t net_seq_num; | |
1762 | ||
e85cdca9 | 1763 | assert(conn); |
1c20f0e2 JD |
1764 | |
1765 | DBG("Relay receiving index"); | |
1766 | ||
e85cdca9 | 1767 | if (!session || conn->version_check_done == 0) { |
1c20f0e2 JD |
1768 | ERR("Trying to close a stream before version check"); |
1769 | ret = -1; | |
1770 | goto end_no_session; | |
1771 | } | |
1772 | ||
e85cdca9 | 1773 | ret = conn->sock->ops->recvmsg(conn->sock, &index_info, |
1c20f0e2 JD |
1774 | sizeof(index_info), 0); |
1775 | if (ret < sizeof(index_info)) { | |
1776 | if (ret == 0) { | |
1777 | /* Orderly shutdown. Not necessary to print an error. */ | |
e85cdca9 | 1778 | DBG("Socket %d did an orderly shutdown", conn->sock->fd); |
1c20f0e2 JD |
1779 | } else { |
1780 | ERR("Relay didn't receive valid index struct size : %d", ret); | |
1781 | } | |
1782 | ret = -1; | |
1783 | goto end_no_session; | |
1784 | } | |
1785 | ||
1786 | net_seq_num = be64toh(index_info.net_seq_num); | |
1787 | ||
1788 | rcu_read_lock(); | |
eb702af5 DG |
1789 | stream = stream_find_by_id(relay_streams_ht, |
1790 | be64toh(index_info.relay_stream_id)); | |
1c20f0e2 JD |
1791 | if (!stream) { |
1792 | ret = -1; | |
1793 | goto end_rcu_unlock; | |
1794 | } | |
1795 | ||
d3e2ba59 JD |
1796 | /* Live beacon handling */ |
1797 | if (index_info.packet_size == 0) { | |
1798 | DBG("Received live beacon for stream %" PRIu64, stream->stream_handle); | |
1799 | ||
1800 | /* | |
1801 | * Only flag a stream inactive when it has already received data. | |
1802 | */ | |
1803 | if (stream->total_index_received > 0) { | |
1804 | stream->beacon_ts_end = be64toh(index_info.timestamp_end); | |
1805 | } | |
1806 | ret = 0; | |
1807 | goto end_rcu_unlock; | |
1808 | } else { | |
1809 | stream->beacon_ts_end = -1ULL; | |
1810 | } | |
1811 | ||
0a6518b0 | 1812 | index = relay_index_find(stream->stream_handle, net_seq_num); |
1c20f0e2 JD |
1813 | if (!index) { |
1814 | /* A successful creation will add the object to the HT. */ | |
1815 | index = relay_index_create(stream->stream_handle, net_seq_num); | |
1816 | if (!index) { | |
1817 | goto end_rcu_unlock; | |
1818 | } | |
1819 | index_created = 1; | |
1820 | } | |
1821 | ||
1822 | copy_index_control_data(index, &index_info); | |
1823 | ||
1824 | if (index_created) { | |
1825 | /* | |
1826 | * Try to add the relay index object to the hash table. If an object | |
1827 | * already exist, destroy back the index created, set the data in this | |
1828 | * object and write it on disk. | |
1829 | */ | |
0a6518b0 | 1830 | relay_index_add(index, &wr_index); |
1c20f0e2 JD |
1831 | if (wr_index) { |
1832 | copy_index_control_data(wr_index, &index_info); | |
1833 | free(index); | |
1834 | } | |
1835 | } else { | |
1836 | /* The index already exists so write it on disk. */ | |
1837 | wr_index = index; | |
1838 | } | |
1839 | ||
1840 | /* Do we have a writable ready index to write on disk. */ | |
1841 | if (wr_index) { | |
0a6518b0 | 1842 | ret = relay_index_write(wr_index->fd, wr_index); |
1c20f0e2 JD |
1843 | if (ret < 0) { |
1844 | goto end_rcu_unlock; | |
1845 | } | |
d3e2ba59 | 1846 | stream->total_index_received++; |
1c20f0e2 JD |
1847 | } |
1848 | ||
1849 | end_rcu_unlock: | |
1850 | rcu_read_unlock(); | |
1851 | ||
239f3aec | 1852 | memset(&reply, 0, sizeof(reply)); |
1c20f0e2 JD |
1853 | if (ret < 0) { |
1854 | reply.ret_code = htobe32(LTTNG_ERR_UNK); | |
1855 | } else { | |
1856 | reply.ret_code = htobe32(LTTNG_OK); | |
1857 | } | |
e85cdca9 | 1858 | send_ret = conn->sock->ops->sendmsg(conn->sock, &reply, sizeof(reply), 0); |
1c20f0e2 JD |
1859 | if (send_ret < 0) { |
1860 | ERR("Relay sending close index id reply"); | |
1861 | ret = send_ret; | |
1862 | } | |
1863 | ||
1864 | end_no_session: | |
1865 | return ret; | |
1866 | } | |
1867 | ||
814fcae4 JD |
1868 | /* |
1869 | * Receive the streams_sent message. | |
1870 | * | |
1871 | * Return 0 on success else a negative value. | |
1872 | */ | |
1873 | static | |
1874 | int relay_streams_sent(struct lttcomm_relayd_hdr *recv_hdr, | |
e85cdca9 | 1875 | struct relay_connection *conn) |
814fcae4 JD |
1876 | { |
1877 | int ret, send_ret; | |
1878 | struct lttcomm_relayd_generic_reply reply; | |
1879 | ||
e85cdca9 | 1880 | assert(conn); |
814fcae4 JD |
1881 | |
1882 | DBG("Relay receiving streams_sent"); | |
1883 | ||
e85cdca9 | 1884 | if (!conn->session || conn->version_check_done == 0) { |
814fcae4 JD |
1885 | ERR("Trying to close a stream before version check"); |
1886 | ret = -1; | |
1887 | goto end_no_session; | |
1888 | } | |
1889 | ||
1890 | /* | |
1891 | * Flag every pending stream in the connection recv list that they are | |
1892 | * ready to be used by the viewer. | |
1893 | */ | |
e85cdca9 | 1894 | set_viewer_ready_flag(conn); |
814fcae4 | 1895 | |
829007d9 JD |
1896 | /* |
1897 | * Inform the viewer that there are new streams in the session. | |
1898 | */ | |
750c7a85 JD |
1899 | if (conn->session->viewer_refcount) { |
1900 | uatomic_set(&conn->session->new_streams, 1); | |
1901 | } | |
829007d9 | 1902 | |
239f3aec | 1903 | memset(&reply, 0, sizeof(reply)); |
814fcae4 | 1904 | reply.ret_code = htobe32(LTTNG_OK); |
e85cdca9 | 1905 | send_ret = conn->sock->ops->sendmsg(conn->sock, &reply, sizeof(reply), 0); |
814fcae4 JD |
1906 | if (send_ret < 0) { |
1907 | ERR("Relay sending sent_stream reply"); | |
1908 | ret = send_ret; | |
1909 | } else { | |
1910 | /* Success. */ | |
1911 | ret = 0; | |
1912 | } | |
1913 | ||
1914 | end_no_session: | |
1915 | return ret; | |
1916 | } | |
1917 | ||
b8aa1682 | 1918 | /* |
d3e2ba59 | 1919 | * Process the commands received on the control socket |
b8aa1682 JD |
1920 | */ |
1921 | static | |
1922 | int relay_process_control(struct lttcomm_relayd_hdr *recv_hdr, | |
e85cdca9 | 1923 | struct relay_connection *conn) |
b8aa1682 JD |
1924 | { |
1925 | int ret = 0; | |
1926 | ||
1927 | switch (be32toh(recv_hdr->cmd)) { | |
b8aa1682 | 1928 | case RELAYD_CREATE_SESSION: |
e85cdca9 | 1929 | ret = relay_create_session(recv_hdr, conn); |
b8aa1682 | 1930 | break; |
b8aa1682 | 1931 | case RELAYD_ADD_STREAM: |
e85cdca9 | 1932 | ret = relay_add_stream(recv_hdr, conn); |
b8aa1682 JD |
1933 | break; |
1934 | case RELAYD_START_DATA: | |
e85cdca9 | 1935 | ret = relay_start(recv_hdr, conn); |
b8aa1682 JD |
1936 | break; |
1937 | case RELAYD_SEND_METADATA: | |
e85cdca9 | 1938 | ret = relay_recv_metadata(recv_hdr, conn); |
b8aa1682 JD |
1939 | break; |
1940 | case RELAYD_VERSION: | |
e85cdca9 | 1941 | ret = relay_send_version(recv_hdr, conn); |
b8aa1682 | 1942 | break; |
173af62f | 1943 | case RELAYD_CLOSE_STREAM: |
e85cdca9 | 1944 | ret = relay_close_stream(recv_hdr, conn); |
173af62f | 1945 | break; |
6d805429 | 1946 | case RELAYD_DATA_PENDING: |
e85cdca9 | 1947 | ret = relay_data_pending(recv_hdr, conn); |
c8f59ee5 DG |
1948 | break; |
1949 | case RELAYD_QUIESCENT_CONTROL: | |
e85cdca9 | 1950 | ret = relay_quiescent_control(recv_hdr, conn); |
c8f59ee5 | 1951 | break; |
f7079f67 | 1952 | case RELAYD_BEGIN_DATA_PENDING: |
e85cdca9 | 1953 | ret = relay_begin_data_pending(recv_hdr, conn); |
f7079f67 DG |
1954 | break; |
1955 | case RELAYD_END_DATA_PENDING: | |
e85cdca9 | 1956 | ret = relay_end_data_pending(recv_hdr, conn); |
f7079f67 | 1957 | break; |
1c20f0e2 | 1958 | case RELAYD_SEND_INDEX: |
e85cdca9 | 1959 | ret = relay_recv_index(recv_hdr, conn); |
1c20f0e2 | 1960 | break; |
814fcae4 | 1961 | case RELAYD_STREAMS_SENT: |
e85cdca9 | 1962 | ret = relay_streams_sent(recv_hdr, conn); |
814fcae4 | 1963 | break; |
b8aa1682 JD |
1964 | case RELAYD_UPDATE_SYNC_INFO: |
1965 | default: | |
1966 | ERR("Received unknown command (%u)", be32toh(recv_hdr->cmd)); | |
e85cdca9 | 1967 | relay_unknown_command(conn); |
b8aa1682 JD |
1968 | ret = -1; |
1969 | goto end; | |
1970 | } | |
1971 | ||
1972 | end: | |
1973 | return ret; | |
1974 | } | |
1975 | ||
7d2f7452 DG |
1976 | /* |
1977 | * Handle index for a data stream. | |
1978 | * | |
1979 | * RCU read side lock MUST be acquired. | |
1980 | * | |
1981 | * Return 0 on success else a negative value. | |
1982 | */ | |
1983 | static int handle_index_data(struct relay_stream *stream, uint64_t net_seq_num, | |
1984 | int rotate_index) | |
1985 | { | |
1986 | int ret = 0, index_created = 0; | |
1987 | uint64_t stream_id, data_offset; | |
1988 | struct relay_index *index, *wr_index = NULL; | |
1989 | ||
1990 | assert(stream); | |
1991 | ||
1992 | stream_id = stream->stream_handle; | |
1993 | /* Get data offset because we are about to update the index. */ | |
1994 | data_offset = htobe64(stream->tracefile_size_current); | |
1995 | ||
1996 | /* | |
1997 | * Lookup for an existing index for that stream id/sequence number. If on | |
1998 | * exists, the control thread already received the data for it thus we need | |
1999 | * to write it on disk. | |
2000 | */ | |
2001 | index = relay_index_find(stream_id, net_seq_num); | |
2002 | if (!index) { | |
2003 | /* A successful creation will add the object to the HT. */ | |
2004 | index = relay_index_create(stream_id, net_seq_num); | |
2005 | if (!index) { | |
2006 | ret = -1; | |
2007 | goto error; | |
2008 | } | |
2009 | index_created = 1; | |
2010 | } | |
2011 | ||
2012 | if (rotate_index || stream->index_fd < 0) { | |
2013 | index->to_close_fd = stream->index_fd; | |
2014 | ret = index_create_file(stream->path_name, stream->channel_name, | |
2015 | relayd_uid, relayd_gid, stream->tracefile_size, | |
2016 | stream->tracefile_count_current); | |
2017 | if (ret < 0) { | |
2018 | /* This will close the stream's index fd if one. */ | |
2019 | relay_index_free_safe(index); | |
2020 | goto error; | |
2021 | } | |
2022 | stream->index_fd = ret; | |
2023 | } | |
2024 | index->fd = stream->index_fd; | |
2025 | index->index_data.offset = data_offset; | |
2026 | ||
2027 | if (index_created) { | |
2028 | /* | |
2029 | * Try to add the relay index object to the hash table. If an object | |
2030 | * already exist, destroy back the index created and set the data. | |
2031 | */ | |
2032 | relay_index_add(index, &wr_index); | |
2033 | if (wr_index) { | |
2034 | /* Copy back data from the created index. */ | |
2035 | wr_index->fd = index->fd; | |
2036 | wr_index->to_close_fd = index->to_close_fd; | |
2037 | wr_index->index_data.offset = data_offset; | |
2038 | free(index); | |
2039 | } | |
2040 | } else { | |
2041 | /* The index already exists so write it on disk. */ | |
2042 | wr_index = index; | |
2043 | } | |
2044 | ||
2045 | /* Do we have a writable ready index to write on disk. */ | |
2046 | if (wr_index) { | |
2047 | ret = relay_index_write(wr_index->fd, wr_index); | |
2048 | if (ret < 0) { | |
2049 | goto error; | |
2050 | } | |
2051 | stream->total_index_received++; | |
2052 | } | |
2053 | ||
2054 | error: | |
2055 | return ret; | |
2056 | } | |
2057 | ||
b8aa1682 JD |
2058 | /* |
2059 | * relay_process_data: Process the data received on the data socket | |
2060 | */ | |
2061 | static | |
e85cdca9 | 2062 | int relay_process_data(struct relay_connection *conn) |
b8aa1682 | 2063 | { |
7d2f7452 | 2064 | int ret = 0, rotate_index = 0; |
6cd525e8 | 2065 | ssize_t size_ret; |
b8aa1682 JD |
2066 | struct relay_stream *stream; |
2067 | struct lttcomm_relayd_data_hdr data_hdr; | |
7d2f7452 | 2068 | uint64_t stream_id; |
173af62f | 2069 | uint64_t net_seq_num; |
b8aa1682 | 2070 | uint32_t data_size; |
eb702af5 | 2071 | struct relay_session *session; |
b8aa1682 | 2072 | |
e85cdca9 DG |
2073 | assert(conn); |
2074 | ||
2075 | ret = conn->sock->ops->recvmsg(conn->sock, &data_hdr, | |
7c5aef62 | 2076 | sizeof(struct lttcomm_relayd_data_hdr), 0); |
b8aa1682 | 2077 | if (ret <= 0) { |
a6cd2b97 DG |
2078 | if (ret == 0) { |
2079 | /* Orderly shutdown. Not necessary to print an error. */ | |
e85cdca9 | 2080 | DBG("Socket %d did an orderly shutdown", conn->sock->fd); |
a6cd2b97 | 2081 | } else { |
e85cdca9 | 2082 | ERR("Unable to receive data header on sock %d", conn->sock->fd); |
a6cd2b97 | 2083 | } |
b8aa1682 JD |
2084 | ret = -1; |
2085 | goto end; | |
2086 | } | |
2087 | ||
2088 | stream_id = be64toh(data_hdr.stream_id); | |
9d1bbf21 MD |
2089 | |
2090 | rcu_read_lock(); | |
eb702af5 | 2091 | stream = stream_find_by_id(relay_streams_ht, stream_id); |
b8aa1682 JD |
2092 | if (!stream) { |
2093 | ret = -1; | |
1c20f0e2 | 2094 | goto end_rcu_unlock; |
b8aa1682 JD |
2095 | } |
2096 | ||
e85cdca9 | 2097 | session = session_find_by_id(conn->sessions_ht, stream->session_id); |
eb702af5 DG |
2098 | assert(session); |
2099 | ||
b8aa1682 JD |
2100 | data_size = be32toh(data_hdr.data_size); |
2101 | if (data_buffer_size < data_size) { | |
c617c0c6 MD |
2102 | char *tmp_data_ptr; |
2103 | ||
2104 | tmp_data_ptr = realloc(data_buffer, data_size); | |
2105 | if (!tmp_data_ptr) { | |
b8aa1682 | 2106 | ERR("Allocating data buffer"); |
c617c0c6 | 2107 | free(data_buffer); |
b8aa1682 | 2108 | ret = -1; |
1c20f0e2 | 2109 | goto end_rcu_unlock; |
b8aa1682 | 2110 | } |
c617c0c6 | 2111 | data_buffer = tmp_data_ptr; |
b8aa1682 JD |
2112 | data_buffer_size = data_size; |
2113 | } | |
2114 | memset(data_buffer, 0, data_size); | |
2115 | ||
173af62f DG |
2116 | net_seq_num = be64toh(data_hdr.net_seq_num); |
2117 | ||
77c7c900 | 2118 | DBG3("Receiving data of size %u for stream id %" PRIu64 " seqnum %" PRIu64, |
173af62f | 2119 | data_size, stream_id, net_seq_num); |
e85cdca9 | 2120 | ret = conn->sock->ops->recvmsg(conn->sock, data_buffer, data_size, 0); |
b8aa1682 | 2121 | if (ret <= 0) { |
a6cd2b97 DG |
2122 | if (ret == 0) { |
2123 | /* Orderly shutdown. Not necessary to print an error. */ | |
e85cdca9 | 2124 | DBG("Socket %d did an orderly shutdown", conn->sock->fd); |
a6cd2b97 | 2125 | } |
b8aa1682 | 2126 | ret = -1; |
1c20f0e2 | 2127 | goto end_rcu_unlock; |
b8aa1682 JD |
2128 | } |
2129 | ||
1c20f0e2 | 2130 | /* Check if a rotation is needed. */ |
0f907de1 JD |
2131 | if (stream->tracefile_size > 0 && |
2132 | (stream->tracefile_size_current + data_size) > | |
2133 | stream->tracefile_size) { | |
6b6b9a5a JD |
2134 | struct relay_viewer_stream *vstream; |
2135 | uint64_t new_id; | |
2136 | ||
2137 | new_id = (stream->tracefile_count_current + 1) % | |
2138 | stream->tracefile_count; | |
2139 | /* | |
2140 | * When we wrap-around back to 0, we start overwriting old | |
2141 | * trace data. | |
2142 | */ | |
2143 | if (!stream->tracefile_overwrite && new_id == 0) { | |
2144 | stream->tracefile_overwrite = 1; | |
2145 | } | |
2146 | pthread_mutex_lock(&stream->viewer_stream_rotation_lock); | |
2147 | if (stream->tracefile_overwrite) { | |
2148 | stream->oldest_tracefile_id = | |
2149 | (stream->oldest_tracefile_id + 1) % | |
2150 | stream->tracefile_count; | |
2151 | } | |
991adae2 | 2152 | vstream = viewer_stream_find_by_id(stream->stream_handle); |
6b6b9a5a JD |
2153 | if (vstream) { |
2154 | /* | |
2155 | * The viewer is reading a file about to be | |
2156 | * overwritten. Close the FDs it is | |
2157 | * currently using and let it handle the fault. | |
2158 | */ | |
2159 | if (vstream->tracefile_count_current == new_id) { | |
cef0f7d5 | 2160 | pthread_mutex_lock(&vstream->overwrite_lock); |
6b6b9a5a | 2161 | vstream->abort_flag = 1; |
cef0f7d5 | 2162 | pthread_mutex_unlock(&vstream->overwrite_lock); |
6b6b9a5a JD |
2163 | DBG("Streaming side setting abort_flag on stream %s_%lu\n", |
2164 | stream->channel_name, new_id); | |
2165 | } else if (vstream->tracefile_count_current == | |
2166 | stream->tracefile_count_current) { | |
2167 | /* | |
2168 | * The reader and writer were in the | |
2169 | * same trace file, inform the viewer | |
2170 | * that no new index will ever be added | |
2171 | * to this file. | |
2172 | */ | |
2173 | vstream->close_write_flag = 1; | |
2174 | } | |
2175 | } | |
1c20f0e2 JD |
2176 | ret = utils_rotate_stream_file(stream->path_name, stream->channel_name, |
2177 | stream->tracefile_size, stream->tracefile_count, | |
2178 | relayd_uid, relayd_gid, stream->fd, | |
2179 | &(stream->tracefile_count_current), &stream->fd); | |
cef0f7d5 | 2180 | stream->total_index_received = 0; |
6b6b9a5a | 2181 | pthread_mutex_unlock(&stream->viewer_stream_rotation_lock); |
0f907de1 | 2182 | if (ret < 0) { |
1c20f0e2 JD |
2183 | ERR("Rotating stream output file"); |
2184 | goto end_rcu_unlock; | |
0f907de1 | 2185 | } |
a6976990 DG |
2186 | /* Reset current size because we just perform a stream rotation. */ |
2187 | stream->tracefile_size_current = 0; | |
1c20f0e2 JD |
2188 | rotate_index = 1; |
2189 | } | |
2190 | ||
1c20f0e2 | 2191 | /* |
7d2f7452 DG |
2192 | * Index are handled in protocol version 2.4 and above. Also, snapshot and |
2193 | * index are NOT supported. | |
1c20f0e2 | 2194 | */ |
eb702af5 | 2195 | if (session->minor >= 4 && !session->snapshot) { |
7d2f7452 | 2196 | ret = handle_index_data(stream, net_seq_num, rotate_index); |
1c20f0e2 | 2197 | if (ret < 0) { |
1c20f0e2 JD |
2198 | goto end_rcu_unlock; |
2199 | } | |
1c20f0e2 JD |
2200 | } |
2201 | ||
7d2f7452 | 2202 | /* Write data to stream output fd. */ |
6cd525e8 MD |
2203 | size_ret = lttng_write(stream->fd, data_buffer, data_size); |
2204 | if (size_ret < data_size) { | |
b8aa1682 JD |
2205 | ERR("Relay error writing data to file"); |
2206 | ret = -1; | |
1c20f0e2 | 2207 | goto end_rcu_unlock; |
b8aa1682 | 2208 | } |
1d4dfdef | 2209 | |
5ab7344e JD |
2210 | DBG2("Relay wrote %d bytes to tracefile for stream id %" PRIu64, |
2211 | ret, stream->stream_handle); | |
2212 | ||
1d4dfdef DG |
2213 | ret = write_padding_to_file(stream->fd, be32toh(data_hdr.padding_size)); |
2214 | if (ret < 0) { | |
1c20f0e2 | 2215 | goto end_rcu_unlock; |
1d4dfdef | 2216 | } |
1c20f0e2 | 2217 | stream->tracefile_size_current += data_size + be32toh(data_hdr.padding_size); |
1d4dfdef | 2218 | |
173af62f DG |
2219 | stream->prev_seq = net_seq_num; |
2220 | ||
eb702af5 | 2221 | try_close_stream(session, stream); |
173af62f | 2222 | |
1c20f0e2 | 2223 | end_rcu_unlock: |
9d1bbf21 | 2224 | rcu_read_unlock(); |
b8aa1682 JD |
2225 | end: |
2226 | return ret; | |
2227 | } | |
2228 | ||
2229 | static | |
e85cdca9 | 2230 | void cleanup_connection_pollfd(struct lttng_poll_event *events, int pollfd) |
b8aa1682 JD |
2231 | { |
2232 | int ret; | |
2233 | ||
e85cdca9 DG |
2234 | assert(events); |
2235 | ||
2236 | (void) lttng_poll_del(events, pollfd); | |
b8aa1682 JD |
2237 | |
2238 | ret = close(pollfd); | |
2239 | if (ret < 0) { | |
2240 | ERR("Closing pollfd %d", pollfd); | |
2241 | } | |
2242 | } | |
2243 | ||
e85cdca9 DG |
2244 | static void destroy_connection(struct lttng_ht *relay_connections_ht, |
2245 | struct relay_connection *conn) | |
9d1bbf21 | 2246 | { |
e85cdca9 DG |
2247 | assert(relay_connections_ht); |
2248 | assert(conn); | |
eb702af5 | 2249 | |
e85cdca9 | 2250 | connection_delete(relay_connections_ht, conn); |
eb702af5 | 2251 | |
e85cdca9 DG |
2252 | /* For the control socket, we try to destroy the session. */ |
2253 | if (conn->type == RELAY_CONTROL) { | |
2254 | destroy_session(conn->session, conn->sessions_ht); | |
9d1bbf21 | 2255 | } |
5b6d8097 | 2256 | |
e85cdca9 | 2257 | connection_destroy(conn); |
b8aa1682 JD |
2258 | } |
2259 | ||
2260 | /* | |
2261 | * This thread does the actual work | |
2262 | */ | |
2263 | static | |
2264 | void *relay_thread_worker(void *data) | |
2265 | { | |
beaad64c DG |
2266 | int ret, err = -1, last_seen_data_fd = -1; |
2267 | uint32_t nb_fd; | |
e85cdca9 | 2268 | struct relay_connection *conn; |
b8aa1682 JD |
2269 | struct lttng_poll_event events; |
2270 | struct lttng_ht *relay_connections_ht; | |
b8aa1682 | 2271 | struct lttng_ht_iter iter; |
b8aa1682 | 2272 | struct lttcomm_relayd_hdr recv_hdr; |
d3e2ba59 JD |
2273 | struct relay_local_data *relay_ctx = (struct relay_local_data *) data; |
2274 | struct lttng_ht *sessions_ht = relay_ctx->sessions_ht; | |
b8aa1682 JD |
2275 | |
2276 | DBG("[thread] Relay worker started"); | |
2277 | ||
9d1bbf21 MD |
2278 | rcu_register_thread(); |
2279 | ||
55706a7d MD |
2280 | health_register(health_relayd, HEALTH_RELAYD_TYPE_WORKER); |
2281 | ||
4e5b8b82 MD |
2282 | if (testpoint(relayd_thread_worker)) { |
2283 | goto error_testpoint; | |
2284 | } | |
2285 | ||
f385ae0a MD |
2286 | health_code_update(); |
2287 | ||
b8aa1682 JD |
2288 | /* table of connections indexed on socket */ |
2289 | relay_connections_ht = lttng_ht_new(0, LTTNG_HT_TYPE_ULONG); | |
095a4ae5 MD |
2290 | if (!relay_connections_ht) { |
2291 | goto relay_connections_ht_error; | |
2292 | } | |
b8aa1682 | 2293 | |
1c20f0e2 JD |
2294 | /* Tables of received indexes indexed by index handle and net_seq_num. */ |
2295 | indexes_ht = lttng_ht_new(0, LTTNG_HT_TYPE_TWO_U64); | |
2296 | if (!indexes_ht) { | |
2297 | goto indexes_ht_error; | |
2298 | } | |
2299 | ||
b8aa1682 JD |
2300 | ret = create_thread_poll_set(&events, 2); |
2301 | if (ret < 0) { | |
2302 | goto error_poll_create; | |
2303 | } | |
2304 | ||
e85cdca9 | 2305 | ret = lttng_poll_add(&events, relay_conn_pipe[0], LPOLLIN | LPOLLRDHUP); |
b8aa1682 JD |
2306 | if (ret < 0) { |
2307 | goto error; | |
2308 | } | |
2309 | ||
beaad64c | 2310 | restart: |
b8aa1682 | 2311 | while (1) { |
beaad64c DG |
2312 | int idx = -1, i, seen_control = 0, last_notdel_data_fd = -1; |
2313 | ||
f385ae0a MD |
2314 | health_code_update(); |
2315 | ||
b8aa1682 | 2316 | /* Infinite blocking call, waiting for transmission */ |
87c1611d | 2317 | DBG3("Relayd worker thread polling..."); |
f385ae0a | 2318 | health_poll_entry(); |
b8aa1682 | 2319 | ret = lttng_poll_wait(&events, -1); |
f385ae0a | 2320 | health_poll_exit(); |
b8aa1682 JD |
2321 | if (ret < 0) { |
2322 | /* | |
2323 | * Restart interrupted system call. | |
2324 | */ | |
2325 | if (errno == EINTR) { | |
2326 | goto restart; | |
2327 | } | |
2328 | goto error; | |
2329 | } | |
2330 | ||
0d9c5d77 DG |
2331 | nb_fd = ret; |
2332 | ||
beaad64c DG |
2333 | /* |
2334 | * Process control. The control connection is prioritised so we don't | |
2335 | * starve it with high throughout put tracing data on the data | |
2336 | * connection. | |
2337 | */ | |
b8aa1682 JD |
2338 | for (i = 0; i < nb_fd; i++) { |
2339 | /* Fetch once the poll data */ | |
beaad64c DG |
2340 | uint32_t revents = LTTNG_POLL_GETEV(&events, i); |
2341 | int pollfd = LTTNG_POLL_GETFD(&events, i); | |
b8aa1682 | 2342 | |
f385ae0a MD |
2343 | health_code_update(); |
2344 | ||
b8aa1682 JD |
2345 | /* Thread quit pipe has been closed. Killing thread. */ |
2346 | ret = check_thread_quit_pipe(pollfd, revents); | |
2347 | if (ret) { | |
095a4ae5 MD |
2348 | err = 0; |
2349 | goto exit; | |
b8aa1682 JD |
2350 | } |
2351 | ||
e85cdca9 DG |
2352 | /* Inspect the relay conn pipe for new connection */ |
2353 | if (pollfd == relay_conn_pipe[0]) { | |
b8aa1682 | 2354 | if (revents & (LPOLLERR | LPOLLHUP | LPOLLRDHUP)) { |
e85cdca9 | 2355 | ERR("Relay connection pipe error"); |
b8aa1682 JD |
2356 | goto error; |
2357 | } else if (revents & LPOLLIN) { | |
e85cdca9 | 2358 | ret = lttng_read(relay_conn_pipe[0], &conn, sizeof(conn)); |
b8aa1682 JD |
2359 | if (ret < 0) { |
2360 | goto error; | |
2361 | } | |
e85cdca9 DG |
2362 | conn->sessions_ht = sessions_ht; |
2363 | connection_init(conn); | |
2364 | lttng_poll_add(&events, conn->sock->fd, | |
2365 | LPOLLIN | LPOLLRDHUP); | |
2366 | rcu_read_lock(); | |
2367 | lttng_ht_add_unique_ulong(relay_connections_ht, | |
2368 | &conn->sock_n); | |
9d1bbf21 | 2369 | rcu_read_unlock(); |
e85cdca9 | 2370 | DBG("Connection socket %d added", conn->sock->fd); |
b8aa1682 | 2371 | } |
e85cdca9 DG |
2372 | } else { |
2373 | rcu_read_lock(); | |
2374 | conn = connection_find_by_sock(relay_connections_ht, pollfd); | |
2375 | /* If not found, there is a synchronization issue. */ | |
2376 | assert(conn); | |
2377 | ||
2378 | if (revents & (LPOLLERR | LPOLLHUP | LPOLLRDHUP)) { | |
2379 | cleanup_connection_pollfd(&events, pollfd); | |
2380 | destroy_connection(relay_connections_ht, conn); | |
beaad64c DG |
2381 | if (last_seen_data_fd == pollfd) { |
2382 | last_seen_data_fd = last_notdel_data_fd; | |
2383 | } | |
b8aa1682 | 2384 | } else if (revents & LPOLLIN) { |
e85cdca9 DG |
2385 | if (conn->type == RELAY_CONTROL) { |
2386 | ret = conn->sock->ops->recvmsg(conn->sock, &recv_hdr, | |
2387 | sizeof(recv_hdr), 0); | |
b8aa1682 | 2388 | if (ret <= 0) { |
e85cdca9 DG |
2389 | /* Connection closed */ |
2390 | cleanup_connection_pollfd(&events, pollfd); | |
2391 | destroy_connection(relay_connections_ht, conn); | |
b8aa1682 JD |
2392 | DBG("Control connection closed with %d", pollfd); |
2393 | } else { | |
e85cdca9 | 2394 | ret = relay_process_control(&recv_hdr, conn); |
b8aa1682 | 2395 | if (ret < 0) { |
beaad64c | 2396 | /* Clear the session on error. */ |
e85cdca9 DG |
2397 | cleanup_connection_pollfd(&events, pollfd); |
2398 | destroy_connection(relay_connections_ht, conn); | |
b8aa1682 JD |
2399 | DBG("Connection closed with %d", pollfd); |
2400 | } | |
beaad64c | 2401 | seen_control = 1; |
b8aa1682 | 2402 | } |
beaad64c DG |
2403 | } else { |
2404 | /* | |
2405 | * Flag the last seen data fd not deleted. It will be | |
2406 | * used as the last seen fd if any fd gets deleted in | |
2407 | * this first loop. | |
2408 | */ | |
2409 | last_notdel_data_fd = pollfd; | |
2410 | } | |
e85cdca9 DG |
2411 | } else { |
2412 | ERR("Unknown poll events %u for sock %d", revents, pollfd); | |
beaad64c DG |
2413 | } |
2414 | rcu_read_unlock(); | |
2415 | } | |
2416 | } | |
2417 | ||
2418 | /* | |
2419 | * The last loop handled a control request, go back to poll to make | |
2420 | * sure we prioritise the control socket. | |
2421 | */ | |
2422 | if (seen_control) { | |
2423 | continue; | |
2424 | } | |
2425 | ||
2426 | if (last_seen_data_fd >= 0) { | |
2427 | for (i = 0; i < nb_fd; i++) { | |
2428 | int pollfd = LTTNG_POLL_GETFD(&events, i); | |
f385ae0a MD |
2429 | |
2430 | health_code_update(); | |
2431 | ||
beaad64c DG |
2432 | if (last_seen_data_fd == pollfd) { |
2433 | idx = i; | |
2434 | break; | |
2435 | } | |
2436 | } | |
2437 | } | |
2438 | ||
2439 | /* Process data connection. */ | |
2440 | for (i = idx + 1; i < nb_fd; i++) { | |
2441 | /* Fetch the poll data. */ | |
2442 | uint32_t revents = LTTNG_POLL_GETEV(&events, i); | |
2443 | int pollfd = LTTNG_POLL_GETFD(&events, i); | |
2444 | ||
f385ae0a MD |
2445 | health_code_update(); |
2446 | ||
beaad64c | 2447 | /* Skip the command pipe. It's handled in the first loop. */ |
e85cdca9 | 2448 | if (pollfd == relay_conn_pipe[0]) { |
beaad64c DG |
2449 | continue; |
2450 | } | |
2451 | ||
2452 | if (revents) { | |
2453 | rcu_read_lock(); | |
e85cdca9 DG |
2454 | conn = connection_find_by_sock(relay_connections_ht, pollfd); |
2455 | if (!conn) { | |
beaad64c DG |
2456 | /* Skip it. Might be removed before. */ |
2457 | rcu_read_unlock(); | |
2458 | continue; | |
2459 | } | |
beaad64c DG |
2460 | |
2461 | if (revents & LPOLLIN) { | |
e85cdca9 | 2462 | if (conn->type != RELAY_DATA) { |
beaad64c DG |
2463 | continue; |
2464 | } | |
2465 | ||
e85cdca9 DG |
2466 | ret = relay_process_data(conn); |
2467 | /* Connection closed */ | |
beaad64c | 2468 | if (ret < 0) { |
e85cdca9 DG |
2469 | cleanup_connection_pollfd(&events, pollfd); |
2470 | destroy_connection(relay_connections_ht, conn); | |
beaad64c DG |
2471 | DBG("Data connection closed with %d", pollfd); |
2472 | /* | |
2473 | * Every goto restart call sets the last seen fd where | |
2474 | * here we don't really care since we gracefully | |
2475 | * continue the loop after the connection is deleted. | |
2476 | */ | |
2477 | } else { | |
2478 | /* Keep last seen port. */ | |
2479 | last_seen_data_fd = pollfd; | |
2480 | rcu_read_unlock(); | |
2481 | goto restart; | |
b8aa1682 JD |
2482 | } |
2483 | } | |
9d1bbf21 | 2484 | rcu_read_unlock(); |
b8aa1682 JD |
2485 | } |
2486 | } | |
beaad64c | 2487 | last_seen_data_fd = -1; |
b8aa1682 JD |
2488 | } |
2489 | ||
f385ae0a MD |
2490 | /* Normal exit, no error */ |
2491 | ret = 0; | |
2492 | ||
095a4ae5 | 2493 | exit: |
b8aa1682 JD |
2494 | error: |
2495 | lttng_poll_clean(&events); | |
2496 | ||
e85cdca9 | 2497 | /* Cleanup reamaining connection object. */ |
9d1bbf21 | 2498 | rcu_read_lock(); |
e85cdca9 DG |
2499 | cds_lfht_for_each_entry(relay_connections_ht->ht, &iter.iter, conn, |
2500 | sock_n.node) { | |
f385ae0a | 2501 | health_code_update(); |
e85cdca9 | 2502 | destroy_connection(relay_connections_ht, conn); |
b8aa1682 | 2503 | } |
94d49140 | 2504 | rcu_read_unlock(); |
7d2f7452 DG |
2505 | error_poll_create: |
2506 | lttng_ht_destroy(indexes_ht); | |
1c20f0e2 | 2507 | indexes_ht_error: |
b8aa1682 | 2508 | lttng_ht_destroy(relay_connections_ht); |
095a4ae5 | 2509 | relay_connections_ht_error: |
e85cdca9 DG |
2510 | /* Close relay conn pipes */ |
2511 | utils_close_pipe(relay_conn_pipe); | |
095a4ae5 MD |
2512 | if (err) { |
2513 | DBG("Thread exited with error"); | |
2514 | } | |
b8aa1682 | 2515 | DBG("Worker thread cleanup complete"); |
095a4ae5 | 2516 | free(data_buffer); |
4e5b8b82 | 2517 | error_testpoint: |
f385ae0a MD |
2518 | if (err) { |
2519 | health_error(); | |
2520 | ERR("Health error occurred in %s", __func__); | |
2521 | } | |
2522 | health_unregister(health_relayd); | |
9d1bbf21 | 2523 | rcu_unregister_thread(); |
f385ae0a | 2524 | stop_threads(); |
b8aa1682 JD |
2525 | return NULL; |
2526 | } | |
2527 | ||
2528 | /* | |
2529 | * Create the relay command pipe to wake thread_manage_apps. | |
2530 | * Closed in cleanup(). | |
2531 | */ | |
e85cdca9 | 2532 | static int create_relay_conn_pipe(void) |
b8aa1682 | 2533 | { |
a02de639 | 2534 | int ret; |
b8aa1682 | 2535 | |
e85cdca9 | 2536 | ret = utils_create_pipe_cloexec(relay_conn_pipe); |
b8aa1682 | 2537 | |
b8aa1682 JD |
2538 | return ret; |
2539 | } | |
2540 | ||
2541 | /* | |
2542 | * main | |
2543 | */ | |
2544 | int main(int argc, char **argv) | |
2545 | { | |
2546 | int ret = 0; | |
2547 | void *status; | |
d3e2ba59 | 2548 | struct relay_local_data *relay_ctx; |
b8aa1682 | 2549 | |
b8aa1682 JD |
2550 | /* Parse arguments */ |
2551 | progname = argv[0]; | |
c617c0c6 | 2552 | if ((ret = parse_args(argc, argv)) < 0) { |
a02de639 | 2553 | goto exit; |
b8aa1682 JD |
2554 | } |
2555 | ||
2556 | if ((ret = set_signal_handler()) < 0) { | |
2557 | goto exit; | |
2558 | } | |
2559 | ||
4d513a50 DG |
2560 | /* Try to create directory if -o, --output is specified. */ |
2561 | if (opt_output_path) { | |
994fa64f DG |
2562 | if (*opt_output_path != '/') { |
2563 | ERR("Please specify an absolute path for -o, --output PATH"); | |
2564 | goto exit; | |
2565 | } | |
2566 | ||
4d513a50 DG |
2567 | ret = utils_mkdir_recursive(opt_output_path, S_IRWXU | S_IRWXG); |
2568 | if (ret < 0) { | |
2569 | ERR("Unable to create %s", opt_output_path); | |
2570 | goto exit; | |
2571 | } | |
2572 | } | |
2573 | ||
b8aa1682 | 2574 | /* Daemonize */ |
d1a3048a | 2575 | if (opt_daemon || opt_background) { |
02a6bb53 MD |
2576 | int i; |
2577 | ||
2578 | ret = lttng_daemonize(&child_ppid, &recv_child_signal, | |
2579 | !opt_background); | |
b8aa1682 | 2580 | if (ret < 0) { |
a02de639 | 2581 | goto exit; |
b8aa1682 | 2582 | } |
02a6bb53 MD |
2583 | |
2584 | /* | |
2585 | * We are in the child. Make sure all other file | |
2586 | * descriptors are closed, in case we are called with | |
2587 | * more opened file descriptors than the standard ones. | |
2588 | */ | |
2589 | for (i = 3; i < sysconf(_SC_OPEN_MAX); i++) { | |
2590 | (void) close(i); | |
2591 | } | |
2592 | } | |
2593 | ||
2594 | /* Create thread quit pipe */ | |
2595 | if ((ret = init_thread_quit_pipe()) < 0) { | |
2596 | goto error; | |
b8aa1682 JD |
2597 | } |
2598 | ||
1c20f0e2 JD |
2599 | /* We need those values for the file/dir creation. */ |
2600 | relayd_uid = getuid(); | |
2601 | relayd_gid = getgid(); | |
b8aa1682 | 2602 | |
1c20f0e2 JD |
2603 | /* Check if daemon is UID = 0 */ |
2604 | if (relayd_uid == 0) { | |
a6a062a4 AM |
2605 | if (control_uri->port < 1024 || data_uri->port < 1024 || |
2606 | live_uri->port < 1024) { | |
b8aa1682 JD |
2607 | ERR("Need to be root to use ports < 1024"); |
2608 | ret = -1; | |
a02de639 | 2609 | goto exit; |
b8aa1682 JD |
2610 | } |
2611 | } | |
2612 | ||
2613 | /* Setup the thread apps communication pipe. */ | |
e85cdca9 | 2614 | if ((ret = create_relay_conn_pipe()) < 0) { |
b8aa1682 JD |
2615 | goto exit; |
2616 | } | |
2617 | ||
2618 | /* Init relay command queue. */ | |
e85cdca9 | 2619 | cds_wfq_init(&relay_conn_queue.queue); |
b8aa1682 JD |
2620 | |
2621 | /* Set up max poll set size */ | |
2622 | lttng_poll_set_max_size(); | |
2623 | ||
554831e7 MD |
2624 | /* Initialize communication library */ |
2625 | lttcomm_init(); | |
d5a5e58f | 2626 | lttcomm_inet_init(); |
554831e7 | 2627 | |
d3e2ba59 JD |
2628 | relay_ctx = zmalloc(sizeof(struct relay_local_data)); |
2629 | if (!relay_ctx) { | |
2630 | PERROR("relay_ctx"); | |
2631 | goto exit; | |
2632 | } | |
2633 | ||
2634 | /* tables of sessions indexed by session ID */ | |
eb702af5 | 2635 | relay_ctx->sessions_ht = lttng_ht_new(0, LTTNG_HT_TYPE_U64); |
d3e2ba59 JD |
2636 | if (!relay_ctx->sessions_ht) { |
2637 | goto exit_relay_ctx_sessions; | |
2638 | } | |
2639 | ||
2640 | /* tables of streams indexed by stream ID */ | |
eb702af5 | 2641 | relay_streams_ht = lttng_ht_new(0, LTTNG_HT_TYPE_U64); |
d3e2ba59 JD |
2642 | if (!relay_streams_ht) { |
2643 | goto exit_relay_ctx_streams; | |
2644 | } | |
2645 | ||
2646 | /* tables of streams indexed by stream ID */ | |
92c6ca54 DG |
2647 | viewer_streams_ht = lttng_ht_new(0, LTTNG_HT_TYPE_U64); |
2648 | if (!viewer_streams_ht) { | |
d3e2ba59 JD |
2649 | goto exit_relay_ctx_viewer_streams; |
2650 | } | |
2651 | ||
55706a7d MD |
2652 | /* Initialize thread health monitoring */ |
2653 | health_relayd = health_app_create(NR_HEALTH_RELAYD_TYPES); | |
2654 | if (!health_relayd) { | |
2655 | PERROR("health_app_create error"); | |
2656 | goto exit_health_app_create; | |
2657 | } | |
2658 | ||
65931c8b MD |
2659 | ret = utils_create_pipe(health_quit_pipe); |
2660 | if (ret < 0) { | |
2661 | goto error_health_pipe; | |
2662 | } | |
2663 | ||
2664 | /* Create thread to manage the client socket */ | |
2665 | ret = pthread_create(&health_thread, NULL, | |
2666 | thread_manage_health, (void *) NULL); | |
2667 | if (ret != 0) { | |
2668 | PERROR("pthread_create health"); | |
2669 | goto health_error; | |
2670 | } | |
2671 | ||
b8aa1682 JD |
2672 | /* Setup the dispatcher thread */ |
2673 | ret = pthread_create(&dispatcher_thread, NULL, | |
2674 | relay_thread_dispatcher, (void *) NULL); | |
2675 | if (ret != 0) { | |
2676 | PERROR("pthread_create dispatcher"); | |
2677 | goto exit_dispatcher; | |
2678 | } | |
2679 | ||
2680 | /* Setup the worker thread */ | |
2681 | ret = pthread_create(&worker_thread, NULL, | |
d3e2ba59 | 2682 | relay_thread_worker, (void *) relay_ctx); |
b8aa1682 JD |
2683 | if (ret != 0) { |
2684 | PERROR("pthread_create worker"); | |
2685 | goto exit_worker; | |
2686 | } | |
2687 | ||
2688 | /* Setup the listener thread */ | |
2689 | ret = pthread_create(&listener_thread, NULL, | |
2690 | relay_thread_listener, (void *) NULL); | |
2691 | if (ret != 0) { | |
2692 | PERROR("pthread_create listener"); | |
2693 | goto exit_listener; | |
2694 | } | |
2695 | ||
3557d456 | 2696 | ret = live_start_threads(live_uri, relay_ctx); |
d3e2ba59 JD |
2697 | if (ret != 0) { |
2698 | ERR("Starting live viewer threads"); | |
50138f51 | 2699 | goto exit_live; |
d3e2ba59 JD |
2700 | } |
2701 | ||
50138f51 | 2702 | exit_live: |
b8aa1682 JD |
2703 | ret = pthread_join(listener_thread, &status); |
2704 | if (ret != 0) { | |
2705 | PERROR("pthread_join"); | |
2706 | goto error; /* join error, exit without cleanup */ | |
2707 | } | |
2708 | ||
50138f51 | 2709 | exit_listener: |
b8aa1682 JD |
2710 | ret = pthread_join(worker_thread, &status); |
2711 | if (ret != 0) { | |
2712 | PERROR("pthread_join"); | |
2713 | goto error; /* join error, exit without cleanup */ | |
2714 | } | |
2715 | ||
50138f51 | 2716 | exit_worker: |
b8aa1682 JD |
2717 | ret = pthread_join(dispatcher_thread, &status); |
2718 | if (ret != 0) { | |
2719 | PERROR("pthread_join"); | |
2720 | goto error; /* join error, exit without cleanup */ | |
2721 | } | |
42415026 | 2722 | |
50138f51 | 2723 | exit_dispatcher: |
65931c8b MD |
2724 | ret = pthread_join(health_thread, &status); |
2725 | if (ret != 0) { | |
2726 | PERROR("pthread_join health thread"); | |
2727 | goto error; /* join error, exit without cleanup */ | |
2728 | } | |
2729 | ||
bd11e201 MD |
2730 | /* |
2731 | * Stop live threads only after joining other threads. | |
2732 | */ | |
2733 | live_stop_threads(); | |
2734 | ||
65931c8b MD |
2735 | health_error: |
2736 | utils_close_pipe(health_quit_pipe); | |
2737 | ||
2738 | error_health_pipe: | |
55706a7d MD |
2739 | health_app_destroy(health_relayd); |
2740 | ||
2741 | exit_health_app_create: | |
92c6ca54 | 2742 | lttng_ht_destroy(viewer_streams_ht); |
d3e2ba59 JD |
2743 | |
2744 | exit_relay_ctx_viewer_streams: | |
2745 | lttng_ht_destroy(relay_streams_ht); | |
2746 | ||
2747 | exit_relay_ctx_streams: | |
2748 | lttng_ht_destroy(relay_ctx->sessions_ht); | |
2749 | ||
2750 | exit_relay_ctx_sessions: | |
2751 | free(relay_ctx); | |
b8aa1682 JD |
2752 | |
2753 | exit: | |
2754 | cleanup(); | |
2755 | if (!ret) { | |
2756 | exit(EXIT_SUCCESS); | |
2757 | } | |
a02de639 | 2758 | |
b8aa1682 JD |
2759 | error: |
2760 | exit(EXIT_FAILURE); | |
2761 | } |