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: | |
5af40280 | 1128 | reply.handle = htobe64(stream->stream_handle); |
b8aa1682 JD |
1129 | /* send the session id to the client or a negative return code on error */ |
1130 | if (ret < 0) { | |
f73fabfd | 1131 | reply.ret_code = htobe32(LTTNG_ERR_UNK); |
5af40280 CB |
1132 | /* stream was not properly added to the ht, so free it */ |
1133 | free(stream); | |
b8aa1682 | 1134 | } else { |
f73fabfd | 1135 | reply.ret_code = htobe32(LTTNG_OK); |
b8aa1682 | 1136 | } |
5af40280 | 1137 | |
e85cdca9 | 1138 | send_ret = conn->sock->ops->sendmsg(conn->sock, &reply, |
b8aa1682 JD |
1139 | sizeof(struct lttcomm_relayd_status_stream), 0); |
1140 | if (send_ret < 0) { | |
1141 | ERR("Relay sending stream id"); | |
4169f5ad | 1142 | ret = send_ret; |
b8aa1682 | 1143 | } |
9d1bbf21 | 1144 | rcu_read_unlock(); |
b8aa1682 JD |
1145 | |
1146 | end_no_session: | |
1147 | return ret; | |
0f907de1 JD |
1148 | |
1149 | err_free_stream: | |
1150 | free(stream->path_name); | |
1151 | free(stream->channel_name); | |
1152 | free(stream); | |
1153 | return ret; | |
b8aa1682 JD |
1154 | } |
1155 | ||
173af62f DG |
1156 | /* |
1157 | * relay_close_stream: close a specific stream | |
1158 | */ | |
1159 | static | |
1160 | int relay_close_stream(struct lttcomm_relayd_hdr *recv_hdr, | |
e85cdca9 | 1161 | struct relay_connection *conn) |
173af62f | 1162 | { |
94d49140 | 1163 | int ret, send_ret; |
e85cdca9 | 1164 | struct relay_session *session = conn->session; |
173af62f DG |
1165 | struct lttcomm_relayd_close_stream stream_info; |
1166 | struct lttcomm_relayd_generic_reply reply; | |
1167 | struct relay_stream *stream; | |
173af62f DG |
1168 | |
1169 | DBG("Close stream received"); | |
1170 | ||
e85cdca9 | 1171 | if (!session || conn->version_check_done == 0) { |
173af62f DG |
1172 | ERR("Trying to close a stream before version check"); |
1173 | ret = -1; | |
1174 | goto end_no_session; | |
1175 | } | |
1176 | ||
e85cdca9 | 1177 | ret = conn->sock->ops->recvmsg(conn->sock, &stream_info, |
7c5aef62 | 1178 | sizeof(struct lttcomm_relayd_close_stream), 0); |
173af62f | 1179 | if (ret < sizeof(struct lttcomm_relayd_close_stream)) { |
a6cd2b97 DG |
1180 | if (ret == 0) { |
1181 | /* Orderly shutdown. Not necessary to print an error. */ | |
e85cdca9 | 1182 | DBG("Socket %d did an orderly shutdown", conn->sock->fd); |
a6cd2b97 DG |
1183 | } else { |
1184 | ERR("Relay didn't receive valid add_stream struct size : %d", ret); | |
1185 | } | |
173af62f DG |
1186 | ret = -1; |
1187 | goto end_no_session; | |
1188 | } | |
1189 | ||
1190 | rcu_read_lock(); | |
eb702af5 DG |
1191 | stream = stream_find_by_id(relay_streams_ht, |
1192 | be64toh(stream_info.stream_id)); | |
173af62f DG |
1193 | if (!stream) { |
1194 | ret = -1; | |
1195 | goto end_unlock; | |
1196 | } | |
1197 | ||
8e2583a4 | 1198 | stream->last_net_seq_num = be64toh(stream_info.last_net_seq_num); |
173af62f | 1199 | stream->close_flag = 1; |
87b576ec JD |
1200 | session->stream_count--; |
1201 | assert(session->stream_count >= 0); | |
173af62f | 1202 | |
eb702af5 DG |
1203 | /* Check if we can close it or else the data will do it. */ |
1204 | try_close_stream(session, stream); | |
173af62f DG |
1205 | |
1206 | end_unlock: | |
1207 | rcu_read_unlock(); | |
1208 | ||
1209 | if (ret < 0) { | |
f73fabfd | 1210 | reply.ret_code = htobe32(LTTNG_ERR_UNK); |
173af62f | 1211 | } else { |
f73fabfd | 1212 | reply.ret_code = htobe32(LTTNG_OK); |
173af62f | 1213 | } |
e85cdca9 | 1214 | send_ret = conn->sock->ops->sendmsg(conn->sock, &reply, |
173af62f DG |
1215 | sizeof(struct lttcomm_relayd_generic_reply), 0); |
1216 | if (send_ret < 0) { | |
1217 | ERR("Relay sending stream id"); | |
4169f5ad | 1218 | ret = send_ret; |
173af62f DG |
1219 | } |
1220 | ||
1221 | end_no_session: | |
1222 | return ret; | |
1223 | } | |
1224 | ||
b8aa1682 JD |
1225 | /* |
1226 | * relay_unknown_command: send -1 if received unknown command | |
1227 | */ | |
1228 | static | |
e85cdca9 | 1229 | void relay_unknown_command(struct relay_connection *conn) |
b8aa1682 JD |
1230 | { |
1231 | struct lttcomm_relayd_generic_reply reply; | |
1232 | int ret; | |
1233 | ||
f73fabfd | 1234 | reply.ret_code = htobe32(LTTNG_ERR_UNK); |
e85cdca9 | 1235 | ret = conn->sock->ops->sendmsg(conn->sock, &reply, |
b8aa1682 JD |
1236 | sizeof(struct lttcomm_relayd_generic_reply), 0); |
1237 | if (ret < 0) { | |
1238 | ERR("Relay sending unknown command"); | |
1239 | } | |
1240 | } | |
1241 | ||
1242 | /* | |
1243 | * relay_start: send an acknowledgment to the client to tell if we are | |
1244 | * ready to receive data. We are ready if a session is established. | |
1245 | */ | |
1246 | static | |
1247 | int relay_start(struct lttcomm_relayd_hdr *recv_hdr, | |
e85cdca9 | 1248 | struct relay_connection *conn) |
b8aa1682 | 1249 | { |
f73fabfd | 1250 | int ret = htobe32(LTTNG_OK); |
b8aa1682 | 1251 | struct lttcomm_relayd_generic_reply reply; |
e85cdca9 | 1252 | struct relay_session *session = conn->session; |
b8aa1682 JD |
1253 | |
1254 | if (!session) { | |
1255 | DBG("Trying to start the streaming without a session established"); | |
f73fabfd | 1256 | ret = htobe32(LTTNG_ERR_UNK); |
b8aa1682 JD |
1257 | } |
1258 | ||
1259 | reply.ret_code = ret; | |
e85cdca9 | 1260 | ret = conn->sock->ops->sendmsg(conn->sock, &reply, |
b8aa1682 JD |
1261 | sizeof(struct lttcomm_relayd_generic_reply), 0); |
1262 | if (ret < 0) { | |
1263 | ERR("Relay sending start ack"); | |
1264 | } | |
1265 | ||
1266 | return ret; | |
1267 | } | |
1268 | ||
1d4dfdef DG |
1269 | /* |
1270 | * Append padding to the file pointed by the file descriptor fd. | |
1271 | */ | |
1272 | static int write_padding_to_file(int fd, uint32_t size) | |
1273 | { | |
6cd525e8 | 1274 | ssize_t ret = 0; |
1d4dfdef DG |
1275 | char *zeros; |
1276 | ||
1277 | if (size == 0) { | |
1278 | goto end; | |
1279 | } | |
1280 | ||
1281 | zeros = zmalloc(size); | |
1282 | if (zeros == NULL) { | |
1283 | PERROR("zmalloc zeros for padding"); | |
1284 | ret = -1; | |
1285 | goto end; | |
1286 | } | |
1287 | ||
6cd525e8 MD |
1288 | ret = lttng_write(fd, zeros, size); |
1289 | if (ret < size) { | |
1d4dfdef DG |
1290 | PERROR("write padding to file"); |
1291 | } | |
1292 | ||
e986c7a1 DG |
1293 | free(zeros); |
1294 | ||
1d4dfdef DG |
1295 | end: |
1296 | return ret; | |
1297 | } | |
1298 | ||
b8aa1682 JD |
1299 | /* |
1300 | * relay_recv_metadata: receive the metada for the session. | |
1301 | */ | |
1302 | static | |
1303 | int relay_recv_metadata(struct lttcomm_relayd_hdr *recv_hdr, | |
e85cdca9 | 1304 | struct relay_connection *conn) |
b8aa1682 | 1305 | { |
f73fabfd | 1306 | int ret = htobe32(LTTNG_OK); |
6cd525e8 | 1307 | ssize_t size_ret; |
e85cdca9 | 1308 | struct relay_session *session = conn->session; |
b8aa1682 JD |
1309 | struct lttcomm_relayd_metadata_payload *metadata_struct; |
1310 | struct relay_stream *metadata_stream; | |
1311 | uint64_t data_size, payload_size; | |
eb702af5 | 1312 | struct ctf_trace *ctf_trace; |
b8aa1682 JD |
1313 | |
1314 | if (!session) { | |
1315 | ERR("Metadata sent before version check"); | |
1316 | ret = -1; | |
1317 | goto end; | |
1318 | } | |
1319 | ||
f6416125 MD |
1320 | data_size = payload_size = be64toh(recv_hdr->data_size); |
1321 | if (data_size < sizeof(struct lttcomm_relayd_metadata_payload)) { | |
1322 | ERR("Incorrect data size"); | |
1323 | ret = -1; | |
1324 | goto end; | |
1325 | } | |
1326 | payload_size -= sizeof(struct lttcomm_relayd_metadata_payload); | |
1327 | ||
b8aa1682 | 1328 | if (data_buffer_size < data_size) { |
d7b3776f | 1329 | /* In case the realloc fails, we can free the memory */ |
c617c0c6 MD |
1330 | char *tmp_data_ptr; |
1331 | ||
1332 | tmp_data_ptr = realloc(data_buffer, data_size); | |
1333 | if (!tmp_data_ptr) { | |
b8aa1682 | 1334 | ERR("Allocating data buffer"); |
c617c0c6 | 1335 | free(data_buffer); |
b8aa1682 JD |
1336 | ret = -1; |
1337 | goto end; | |
1338 | } | |
c617c0c6 | 1339 | data_buffer = tmp_data_ptr; |
b8aa1682 JD |
1340 | data_buffer_size = data_size; |
1341 | } | |
1342 | memset(data_buffer, 0, data_size); | |
77c7c900 | 1343 | DBG2("Relay receiving metadata, waiting for %" PRIu64 " bytes", data_size); |
e85cdca9 | 1344 | ret = conn->sock->ops->recvmsg(conn->sock, data_buffer, data_size, 0); |
b8aa1682 | 1345 | if (ret < 0 || ret != data_size) { |
a6cd2b97 DG |
1346 | if (ret == 0) { |
1347 | /* Orderly shutdown. Not necessary to print an error. */ | |
e85cdca9 | 1348 | DBG("Socket %d did an orderly shutdown", conn->sock->fd); |
a6cd2b97 DG |
1349 | } else { |
1350 | ERR("Relay didn't receive the whole metadata"); | |
1351 | } | |
b8aa1682 | 1352 | ret = -1; |
b8aa1682 JD |
1353 | goto end; |
1354 | } | |
1355 | metadata_struct = (struct lttcomm_relayd_metadata_payload *) data_buffer; | |
9d1bbf21 MD |
1356 | |
1357 | rcu_read_lock(); | |
eb702af5 | 1358 | metadata_stream = stream_find_by_id(relay_streams_ht, |
d3e2ba59 | 1359 | be64toh(metadata_struct->stream_id)); |
b8aa1682 JD |
1360 | if (!metadata_stream) { |
1361 | ret = -1; | |
9d1bbf21 | 1362 | goto end_unlock; |
b8aa1682 JD |
1363 | } |
1364 | ||
6cd525e8 MD |
1365 | size_ret = lttng_write(metadata_stream->fd, metadata_struct->payload, |
1366 | payload_size); | |
1367 | if (size_ret < payload_size) { | |
b8aa1682 JD |
1368 | ERR("Relay error writing metadata on file"); |
1369 | ret = -1; | |
9d1bbf21 | 1370 | goto end_unlock; |
b8aa1682 | 1371 | } |
1d4dfdef DG |
1372 | |
1373 | ret = write_padding_to_file(metadata_stream->fd, | |
1374 | be32toh(metadata_struct->padding_size)); | |
1375 | if (ret < 0) { | |
1376 | goto end_unlock; | |
1377 | } | |
eb702af5 DG |
1378 | |
1379 | ctf_trace = ctf_trace_find_by_path(session->ctf_traces_ht, | |
1380 | metadata_stream->path_name); | |
1381 | assert(ctf_trace); | |
1382 | ctf_trace->metadata_received += | |
d3e2ba59 | 1383 | payload_size + be32toh(metadata_struct->padding_size); |
1d4dfdef | 1384 | |
b8aa1682 JD |
1385 | DBG2("Relay metadata written"); |
1386 | ||
9d1bbf21 | 1387 | end_unlock: |
6e3c5836 | 1388 | rcu_read_unlock(); |
b8aa1682 JD |
1389 | end: |
1390 | return ret; | |
1391 | } | |
1392 | ||
1393 | /* | |
1394 | * relay_send_version: send relayd version number | |
1395 | */ | |
1396 | static | |
1397 | int relay_send_version(struct lttcomm_relayd_hdr *recv_hdr, | |
e85cdca9 | 1398 | struct relay_connection *conn) |
b8aa1682 | 1399 | { |
7f51dcba | 1400 | int ret; |
092b6259 | 1401 | struct lttcomm_relayd_version reply, msg; |
b8aa1682 | 1402 | |
e85cdca9 | 1403 | assert(conn); |
c5b6f4f0 | 1404 | |
e85cdca9 | 1405 | conn->version_check_done = 1; |
b8aa1682 | 1406 | |
092b6259 | 1407 | /* Get version from the other side. */ |
e85cdca9 | 1408 | ret = conn->sock->ops->recvmsg(conn->sock, &msg, sizeof(msg), 0); |
092b6259 | 1409 | if (ret < 0 || ret != sizeof(msg)) { |
a6cd2b97 DG |
1410 | if (ret == 0) { |
1411 | /* Orderly shutdown. Not necessary to print an error. */ | |
e85cdca9 | 1412 | DBG("Socket %d did an orderly shutdown", conn->sock->fd); |
a6cd2b97 DG |
1413 | } else { |
1414 | ERR("Relay failed to receive the version values."); | |
1415 | } | |
092b6259 | 1416 | ret = -1; |
092b6259 DG |
1417 | goto end; |
1418 | } | |
1419 | ||
d83a952c MD |
1420 | reply.major = RELAYD_VERSION_COMM_MAJOR; |
1421 | reply.minor = RELAYD_VERSION_COMM_MINOR; | |
d4519fa3 JD |
1422 | |
1423 | /* Major versions must be the same */ | |
1424 | if (reply.major != be32toh(msg.major)) { | |
6151a90f JD |
1425 | DBG("Incompatible major versions (%u vs %u), deleting session", |
1426 | reply.major, be32toh(msg.major)); | |
e85cdca9 | 1427 | destroy_session(conn->session, conn->sessions_ht); |
d4519fa3 JD |
1428 | ret = 0; |
1429 | goto end; | |
1430 | } | |
1431 | ||
e85cdca9 | 1432 | conn->major = reply.major; |
0f907de1 JD |
1433 | /* We adapt to the lowest compatible version */ |
1434 | if (reply.minor <= be32toh(msg.minor)) { | |
e85cdca9 | 1435 | conn->minor = reply.minor; |
0f907de1 | 1436 | } else { |
e85cdca9 | 1437 | conn->minor = be32toh(msg.minor); |
0f907de1 JD |
1438 | } |
1439 | ||
6151a90f JD |
1440 | reply.major = htobe32(reply.major); |
1441 | reply.minor = htobe32(reply.minor); | |
e85cdca9 | 1442 | ret = conn->sock->ops->sendmsg(conn->sock, &reply, |
6151a90f JD |
1443 | sizeof(struct lttcomm_relayd_version), 0); |
1444 | if (ret < 0) { | |
1445 | ERR("Relay sending version"); | |
1446 | } | |
1447 | ||
e85cdca9 DG |
1448 | DBG("Version check done using protocol %u.%u", conn->major, |
1449 | conn->minor); | |
b8aa1682 JD |
1450 | |
1451 | end: | |
1452 | return ret; | |
1453 | } | |
1454 | ||
c8f59ee5 | 1455 | /* |
6d805429 | 1456 | * Check for data pending for a given stream id from the session daemon. |
c8f59ee5 DG |
1457 | */ |
1458 | static | |
6d805429 | 1459 | int relay_data_pending(struct lttcomm_relayd_hdr *recv_hdr, |
e85cdca9 | 1460 | struct relay_connection *conn) |
c8f59ee5 | 1461 | { |
e85cdca9 | 1462 | struct relay_session *session = conn->session; |
6d805429 | 1463 | struct lttcomm_relayd_data_pending msg; |
c8f59ee5 DG |
1464 | struct lttcomm_relayd_generic_reply reply; |
1465 | struct relay_stream *stream; | |
1466 | int ret; | |
c8f59ee5 DG |
1467 | uint64_t last_net_seq_num, stream_id; |
1468 | ||
6d805429 | 1469 | DBG("Data pending command received"); |
c8f59ee5 | 1470 | |
e85cdca9 | 1471 | if (!session || conn->version_check_done == 0) { |
c8f59ee5 DG |
1472 | ERR("Trying to check for data before version check"); |
1473 | ret = -1; | |
1474 | goto end_no_session; | |
1475 | } | |
1476 | ||
e85cdca9 | 1477 | ret = conn->sock->ops->recvmsg(conn->sock, &msg, sizeof(msg), 0); |
c8f59ee5 | 1478 | if (ret < sizeof(msg)) { |
a6cd2b97 DG |
1479 | if (ret == 0) { |
1480 | /* Orderly shutdown. Not necessary to print an error. */ | |
e85cdca9 | 1481 | DBG("Socket %d did an orderly shutdown", conn->sock->fd); |
a6cd2b97 DG |
1482 | } else { |
1483 | ERR("Relay didn't receive valid data_pending struct size : %d", | |
1484 | ret); | |
1485 | } | |
c8f59ee5 DG |
1486 | ret = -1; |
1487 | goto end_no_session; | |
1488 | } | |
1489 | ||
1490 | stream_id = be64toh(msg.stream_id); | |
1491 | last_net_seq_num = be64toh(msg.last_net_seq_num); | |
1492 | ||
1493 | rcu_read_lock(); | |
eb702af5 | 1494 | stream = stream_find_by_id(relay_streams_ht, stream_id); |
de91f48a | 1495 | if (stream == NULL) { |
c8f59ee5 DG |
1496 | ret = -1; |
1497 | goto end_unlock; | |
1498 | } | |
1499 | ||
6d805429 | 1500 | DBG("Data pending for stream id %" PRIu64 " prev_seq %" PRIu64 |
c8f59ee5 DG |
1501 | " and last_seq %" PRIu64, stream_id, stream->prev_seq, |
1502 | last_net_seq_num); | |
1503 | ||
33832e64 | 1504 | /* Avoid wrapping issue */ |
39df6d9f | 1505 | if (((int64_t) (stream->prev_seq - last_net_seq_num)) >= 0) { |
6d805429 | 1506 | /* Data has in fact been written and is NOT pending */ |
c8f59ee5 | 1507 | ret = 0; |
6d805429 DG |
1508 | } else { |
1509 | /* Data still being streamed thus pending */ | |
1510 | ret = 1; | |
c8f59ee5 DG |
1511 | } |
1512 | ||
f7079f67 DG |
1513 | /* Pending check is now done. */ |
1514 | stream->data_pending_check_done = 1; | |
1515 | ||
c8f59ee5 DG |
1516 | end_unlock: |
1517 | rcu_read_unlock(); | |
1518 | ||
1519 | reply.ret_code = htobe32(ret); | |
e85cdca9 | 1520 | ret = conn->sock->ops->sendmsg(conn->sock, &reply, sizeof(reply), 0); |
c8f59ee5 | 1521 | if (ret < 0) { |
6d805429 | 1522 | ERR("Relay data pending ret code failed"); |
c8f59ee5 DG |
1523 | } |
1524 | ||
1525 | end_no_session: | |
1526 | return ret; | |
1527 | } | |
1528 | ||
1529 | /* | |
1530 | * Wait for the control socket to reach a quiescent state. | |
1531 | * | |
1532 | * Note that for now, when receiving this command from the session daemon, this | |
1533 | * means that every subsequent commands or data received on the control socket | |
1534 | * has been handled. So, this is why we simply return OK here. | |
1535 | */ | |
1536 | static | |
1537 | int relay_quiescent_control(struct lttcomm_relayd_hdr *recv_hdr, | |
e85cdca9 | 1538 | struct relay_connection *conn) |
c8f59ee5 DG |
1539 | { |
1540 | int ret; | |
ad7051c0 DG |
1541 | uint64_t stream_id; |
1542 | struct relay_stream *stream; | |
1543 | struct lttng_ht_iter iter; | |
1544 | struct lttcomm_relayd_quiescent_control msg; | |
c8f59ee5 DG |
1545 | struct lttcomm_relayd_generic_reply reply; |
1546 | ||
1547 | DBG("Checking quiescent state on control socket"); | |
1548 | ||
e85cdca9 | 1549 | if (!conn->session || conn->version_check_done == 0) { |
ad7051c0 DG |
1550 | ERR("Trying to check for data before version check"); |
1551 | ret = -1; | |
1552 | goto end_no_session; | |
1553 | } | |
1554 | ||
e85cdca9 | 1555 | ret = conn->sock->ops->recvmsg(conn->sock, &msg, sizeof(msg), 0); |
ad7051c0 | 1556 | if (ret < sizeof(msg)) { |
a6cd2b97 DG |
1557 | if (ret == 0) { |
1558 | /* Orderly shutdown. Not necessary to print an error. */ | |
e85cdca9 | 1559 | DBG("Socket %d did an orderly shutdown", conn->sock->fd); |
a6cd2b97 DG |
1560 | } else { |
1561 | ERR("Relay didn't receive valid begin data_pending struct size: %d", | |
1562 | ret); | |
1563 | } | |
ad7051c0 DG |
1564 | ret = -1; |
1565 | goto end_no_session; | |
1566 | } | |
1567 | ||
1568 | stream_id = be64toh(msg.stream_id); | |
1569 | ||
1570 | rcu_read_lock(); | |
d3e2ba59 | 1571 | cds_lfht_for_each_entry(relay_streams_ht->ht, &iter.iter, stream, |
eb702af5 | 1572 | node.node) { |
ad7051c0 DG |
1573 | if (stream->stream_handle == stream_id) { |
1574 | stream->data_pending_check_done = 1; | |
1575 | DBG("Relay quiescent control pending flag set to %" PRIu64, | |
1576 | stream_id); | |
1577 | break; | |
1578 | } | |
1579 | } | |
1580 | rcu_read_unlock(); | |
1581 | ||
c8f59ee5 | 1582 | reply.ret_code = htobe32(LTTNG_OK); |
e85cdca9 | 1583 | ret = conn->sock->ops->sendmsg(conn->sock, &reply, sizeof(reply), 0); |
c8f59ee5 | 1584 | if (ret < 0) { |
6d805429 | 1585 | ERR("Relay data quiescent control ret code failed"); |
c8f59ee5 DG |
1586 | } |
1587 | ||
ad7051c0 | 1588 | end_no_session: |
c8f59ee5 DG |
1589 | return ret; |
1590 | } | |
1591 | ||
f7079f67 DG |
1592 | /* |
1593 | * Initialize a data pending command. This means that a client is about to ask | |
1594 | * for data pending for each stream he/she holds. Simply iterate over all | |
1595 | * streams of a session and set the data_pending_check_done flag. | |
1596 | * | |
1597 | * This command returns to the client a LTTNG_OK code. | |
1598 | */ | |
1599 | static | |
1600 | int relay_begin_data_pending(struct lttcomm_relayd_hdr *recv_hdr, | |
e85cdca9 | 1601 | struct relay_connection *conn) |
f7079f67 DG |
1602 | { |
1603 | int ret; | |
1604 | struct lttng_ht_iter iter; | |
1605 | struct lttcomm_relayd_begin_data_pending msg; | |
1606 | struct lttcomm_relayd_generic_reply reply; | |
1607 | struct relay_stream *stream; | |
1608 | uint64_t session_id; | |
1609 | ||
1610 | assert(recv_hdr); | |
e85cdca9 | 1611 | assert(conn); |
f7079f67 DG |
1612 | |
1613 | DBG("Init streams for data pending"); | |
1614 | ||
e85cdca9 | 1615 | if (!conn->session || conn->version_check_done == 0) { |
f7079f67 DG |
1616 | ERR("Trying to check for data before version check"); |
1617 | ret = -1; | |
1618 | goto end_no_session; | |
1619 | } | |
1620 | ||
e85cdca9 | 1621 | ret = conn->sock->ops->recvmsg(conn->sock, &msg, sizeof(msg), 0); |
f7079f67 | 1622 | if (ret < sizeof(msg)) { |
a6cd2b97 DG |
1623 | if (ret == 0) { |
1624 | /* Orderly shutdown. Not necessary to print an error. */ | |
e85cdca9 | 1625 | DBG("Socket %d did an orderly shutdown", conn->sock->fd); |
a6cd2b97 DG |
1626 | } else { |
1627 | ERR("Relay didn't receive valid begin data_pending struct size: %d", | |
1628 | ret); | |
1629 | } | |
f7079f67 DG |
1630 | ret = -1; |
1631 | goto end_no_session; | |
1632 | } | |
1633 | ||
1634 | session_id = be64toh(msg.session_id); | |
1635 | ||
1636 | /* | |
1637 | * Iterate over all streams to set the begin data pending flag. For now, the | |
1638 | * streams are indexed by stream handle so we have to iterate over all | |
1639 | * streams to find the one associated with the right session_id. | |
1640 | */ | |
1641 | rcu_read_lock(); | |
d3e2ba59 | 1642 | cds_lfht_for_each_entry(relay_streams_ht->ht, &iter.iter, stream, |
eb702af5 DG |
1643 | node.node) { |
1644 | if (stream->session_id == session_id) { | |
f7079f67 DG |
1645 | stream->data_pending_check_done = 0; |
1646 | DBG("Set begin data pending flag to stream %" PRIu64, | |
1647 | stream->stream_handle); | |
1648 | } | |
1649 | } | |
1650 | rcu_read_unlock(); | |
1651 | ||
1652 | /* All good, send back reply. */ | |
1653 | reply.ret_code = htobe32(LTTNG_OK); | |
1654 | ||
e85cdca9 | 1655 | ret = conn->sock->ops->sendmsg(conn->sock, &reply, sizeof(reply), 0); |
f7079f67 DG |
1656 | if (ret < 0) { |
1657 | ERR("Relay begin data pending send reply failed"); | |
1658 | } | |
1659 | ||
1660 | end_no_session: | |
1661 | return ret; | |
1662 | } | |
1663 | ||
1664 | /* | |
1665 | * End data pending command. This will check, for a given session id, if each | |
1666 | * stream associated with it has its data_pending_check_done flag set. If not, | |
1667 | * this means that the client lost track of the stream but the data is still | |
1668 | * being streamed on our side. In this case, we inform the client that data is | |
1669 | * inflight. | |
1670 | * | |
1671 | * Return to the client if there is data in flight or not with a ret_code. | |
1672 | */ | |
1673 | static | |
1674 | int relay_end_data_pending(struct lttcomm_relayd_hdr *recv_hdr, | |
e85cdca9 | 1675 | struct relay_connection *conn) |
f7079f67 DG |
1676 | { |
1677 | int ret; | |
1678 | struct lttng_ht_iter iter; | |
1679 | struct lttcomm_relayd_end_data_pending msg; | |
1680 | struct lttcomm_relayd_generic_reply reply; | |
1681 | struct relay_stream *stream; | |
1682 | uint64_t session_id; | |
1683 | uint32_t is_data_inflight = 0; | |
1684 | ||
1685 | assert(recv_hdr); | |
e85cdca9 | 1686 | assert(conn); |
f7079f67 DG |
1687 | |
1688 | DBG("End data pending command"); | |
1689 | ||
e85cdca9 | 1690 | if (!conn->session || conn->version_check_done == 0) { |
f7079f67 DG |
1691 | ERR("Trying to check for data before version check"); |
1692 | ret = -1; | |
1693 | goto end_no_session; | |
1694 | } | |
1695 | ||
e85cdca9 | 1696 | ret = conn->sock->ops->recvmsg(conn->sock, &msg, sizeof(msg), 0); |
f7079f67 | 1697 | if (ret < sizeof(msg)) { |
a6cd2b97 DG |
1698 | if (ret == 0) { |
1699 | /* Orderly shutdown. Not necessary to print an error. */ | |
e85cdca9 | 1700 | DBG("Socket %d did an orderly shutdown", conn->sock->fd); |
a6cd2b97 DG |
1701 | } else { |
1702 | ERR("Relay didn't receive valid end data_pending struct size: %d", | |
1703 | ret); | |
1704 | } | |
f7079f67 DG |
1705 | ret = -1; |
1706 | goto end_no_session; | |
1707 | } | |
1708 | ||
1709 | session_id = be64toh(msg.session_id); | |
1710 | ||
1711 | /* Iterate over all streams to see if the begin data pending flag is set. */ | |
1712 | rcu_read_lock(); | |
d3e2ba59 | 1713 | cds_lfht_for_each_entry(relay_streams_ht->ht, &iter.iter, stream, |
eb702af5 DG |
1714 | node.node) { |
1715 | if (stream->session_id == session_id && | |
50979bcd | 1716 | !stream->data_pending_check_done && !stream->terminated_flag) { |
f7079f67 DG |
1717 | is_data_inflight = 1; |
1718 | DBG("Data is still in flight for stream %" PRIu64, | |
1719 | stream->stream_handle); | |
1720 | break; | |
1721 | } | |
1722 | } | |
1723 | rcu_read_unlock(); | |
1724 | ||
1725 | /* All good, send back reply. */ | |
1726 | reply.ret_code = htobe32(is_data_inflight); | |
1727 | ||
e85cdca9 | 1728 | ret = conn->sock->ops->sendmsg(conn->sock, &reply, sizeof(reply), 0); |
f7079f67 DG |
1729 | if (ret < 0) { |
1730 | ERR("Relay end data pending send reply failed"); | |
1731 | } | |
1732 | ||
1733 | end_no_session: | |
1734 | return ret; | |
1735 | } | |
1736 | ||
1c20f0e2 JD |
1737 | /* |
1738 | * Receive an index for a specific stream. | |
1739 | * | |
1740 | * Return 0 on success else a negative value. | |
1741 | */ | |
1742 | static | |
1743 | int relay_recv_index(struct lttcomm_relayd_hdr *recv_hdr, | |
e85cdca9 | 1744 | struct relay_connection *conn) |
1c20f0e2 JD |
1745 | { |
1746 | int ret, send_ret, index_created = 0; | |
e85cdca9 | 1747 | struct relay_session *session = conn->session; |
1c20f0e2 JD |
1748 | struct lttcomm_relayd_index index_info; |
1749 | struct relay_index *index, *wr_index = NULL; | |
1750 | struct lttcomm_relayd_generic_reply reply; | |
1751 | struct relay_stream *stream; | |
1752 | uint64_t net_seq_num; | |
1753 | ||
e85cdca9 | 1754 | assert(conn); |
1c20f0e2 JD |
1755 | |
1756 | DBG("Relay receiving index"); | |
1757 | ||
e85cdca9 | 1758 | if (!session || conn->version_check_done == 0) { |
1c20f0e2 JD |
1759 | ERR("Trying to close a stream before version check"); |
1760 | ret = -1; | |
1761 | goto end_no_session; | |
1762 | } | |
1763 | ||
e85cdca9 | 1764 | ret = conn->sock->ops->recvmsg(conn->sock, &index_info, |
1c20f0e2 JD |
1765 | sizeof(index_info), 0); |
1766 | if (ret < sizeof(index_info)) { | |
1767 | if (ret == 0) { | |
1768 | /* Orderly shutdown. Not necessary to print an error. */ | |
e85cdca9 | 1769 | DBG("Socket %d did an orderly shutdown", conn->sock->fd); |
1c20f0e2 JD |
1770 | } else { |
1771 | ERR("Relay didn't receive valid index struct size : %d", ret); | |
1772 | } | |
1773 | ret = -1; | |
1774 | goto end_no_session; | |
1775 | } | |
1776 | ||
1777 | net_seq_num = be64toh(index_info.net_seq_num); | |
1778 | ||
1779 | rcu_read_lock(); | |
eb702af5 DG |
1780 | stream = stream_find_by_id(relay_streams_ht, |
1781 | be64toh(index_info.relay_stream_id)); | |
1c20f0e2 JD |
1782 | if (!stream) { |
1783 | ret = -1; | |
1784 | goto end_rcu_unlock; | |
1785 | } | |
1786 | ||
d3e2ba59 JD |
1787 | /* Live beacon handling */ |
1788 | if (index_info.packet_size == 0) { | |
1789 | DBG("Received live beacon for stream %" PRIu64, stream->stream_handle); | |
1790 | ||
1791 | /* | |
1792 | * Only flag a stream inactive when it has already received data. | |
1793 | */ | |
1794 | if (stream->total_index_received > 0) { | |
1795 | stream->beacon_ts_end = be64toh(index_info.timestamp_end); | |
1796 | } | |
1797 | ret = 0; | |
1798 | goto end_rcu_unlock; | |
1799 | } else { | |
1800 | stream->beacon_ts_end = -1ULL; | |
1801 | } | |
1802 | ||
0a6518b0 | 1803 | index = relay_index_find(stream->stream_handle, net_seq_num); |
1c20f0e2 JD |
1804 | if (!index) { |
1805 | /* A successful creation will add the object to the HT. */ | |
1806 | index = relay_index_create(stream->stream_handle, net_seq_num); | |
1807 | if (!index) { | |
1808 | goto end_rcu_unlock; | |
1809 | } | |
1810 | index_created = 1; | |
1811 | } | |
1812 | ||
1813 | copy_index_control_data(index, &index_info); | |
1814 | ||
1815 | if (index_created) { | |
1816 | /* | |
1817 | * Try to add the relay index object to the hash table. If an object | |
1818 | * already exist, destroy back the index created, set the data in this | |
1819 | * object and write it on disk. | |
1820 | */ | |
0a6518b0 | 1821 | relay_index_add(index, &wr_index); |
1c20f0e2 JD |
1822 | if (wr_index) { |
1823 | copy_index_control_data(wr_index, &index_info); | |
1824 | free(index); | |
1825 | } | |
1826 | } else { | |
1827 | /* The index already exists so write it on disk. */ | |
1828 | wr_index = index; | |
1829 | } | |
1830 | ||
1831 | /* Do we have a writable ready index to write on disk. */ | |
1832 | if (wr_index) { | |
1833 | /* Starting at 2.4, create the index file if none available. */ | |
e85cdca9 | 1834 | if (conn->minor >= 4 && stream->index_fd < 0) { |
1c20f0e2 JD |
1835 | ret = index_create_file(stream->path_name, stream->channel_name, |
1836 | relayd_uid, relayd_gid, stream->tracefile_size, | |
1837 | stream->tracefile_count_current); | |
1838 | if (ret < 0) { | |
1839 | goto end_rcu_unlock; | |
1840 | } | |
1841 | stream->index_fd = ret; | |
1842 | } | |
1843 | ||
0a6518b0 | 1844 | ret = relay_index_write(wr_index->fd, wr_index); |
1c20f0e2 JD |
1845 | if (ret < 0) { |
1846 | goto end_rcu_unlock; | |
1847 | } | |
d3e2ba59 | 1848 | stream->total_index_received++; |
1c20f0e2 JD |
1849 | } |
1850 | ||
1851 | end_rcu_unlock: | |
1852 | rcu_read_unlock(); | |
1853 | ||
1854 | if (ret < 0) { | |
1855 | reply.ret_code = htobe32(LTTNG_ERR_UNK); | |
1856 | } else { | |
1857 | reply.ret_code = htobe32(LTTNG_OK); | |
1858 | } | |
e85cdca9 | 1859 | send_ret = conn->sock->ops->sendmsg(conn->sock, &reply, sizeof(reply), 0); |
1c20f0e2 JD |
1860 | if (send_ret < 0) { |
1861 | ERR("Relay sending close index id reply"); | |
1862 | ret = send_ret; | |
1863 | } | |
1864 | ||
1865 | end_no_session: | |
1866 | return ret; | |
1867 | } | |
1868 | ||
814fcae4 JD |
1869 | /* |
1870 | * Receive the streams_sent message. | |
1871 | * | |
1872 | * Return 0 on success else a negative value. | |
1873 | */ | |
1874 | static | |
1875 | int relay_streams_sent(struct lttcomm_relayd_hdr *recv_hdr, | |
e85cdca9 | 1876 | struct relay_connection *conn) |
814fcae4 JD |
1877 | { |
1878 | int ret, send_ret; | |
1879 | struct lttcomm_relayd_generic_reply reply; | |
1880 | ||
e85cdca9 | 1881 | assert(conn); |
814fcae4 JD |
1882 | |
1883 | DBG("Relay receiving streams_sent"); | |
1884 | ||
e85cdca9 | 1885 | if (!conn->session || conn->version_check_done == 0) { |
814fcae4 JD |
1886 | ERR("Trying to close a stream before version check"); |
1887 | ret = -1; | |
1888 | goto end_no_session; | |
1889 | } | |
1890 | ||
1891 | /* | |
1892 | * Flag every pending stream in the connection recv list that they are | |
1893 | * ready to be used by the viewer. | |
1894 | */ | |
e85cdca9 | 1895 | set_viewer_ready_flag(conn); |
814fcae4 | 1896 | |
829007d9 JD |
1897 | /* |
1898 | * Inform the viewer that there are new streams in the session. | |
1899 | */ | |
e85cdca9 | 1900 | uatomic_set(&conn->session->new_streams, 1); |
829007d9 | 1901 | |
814fcae4 | 1902 | reply.ret_code = htobe32(LTTNG_OK); |
e85cdca9 | 1903 | send_ret = conn->sock->ops->sendmsg(conn->sock, &reply, sizeof(reply), 0); |
814fcae4 JD |
1904 | if (send_ret < 0) { |
1905 | ERR("Relay sending sent_stream reply"); | |
1906 | ret = send_ret; | |
1907 | } else { | |
1908 | /* Success. */ | |
1909 | ret = 0; | |
1910 | } | |
1911 | ||
1912 | end_no_session: | |
1913 | return ret; | |
1914 | } | |
1915 | ||
b8aa1682 | 1916 | /* |
d3e2ba59 | 1917 | * Process the commands received on the control socket |
b8aa1682 JD |
1918 | */ |
1919 | static | |
1920 | int relay_process_control(struct lttcomm_relayd_hdr *recv_hdr, | |
e85cdca9 | 1921 | struct relay_connection *conn) |
b8aa1682 JD |
1922 | { |
1923 | int ret = 0; | |
1924 | ||
1925 | switch (be32toh(recv_hdr->cmd)) { | |
b8aa1682 | 1926 | case RELAYD_CREATE_SESSION: |
e85cdca9 | 1927 | ret = relay_create_session(recv_hdr, conn); |
b8aa1682 | 1928 | break; |
b8aa1682 | 1929 | case RELAYD_ADD_STREAM: |
e85cdca9 | 1930 | ret = relay_add_stream(recv_hdr, conn); |
b8aa1682 JD |
1931 | break; |
1932 | case RELAYD_START_DATA: | |
e85cdca9 | 1933 | ret = relay_start(recv_hdr, conn); |
b8aa1682 JD |
1934 | break; |
1935 | case RELAYD_SEND_METADATA: | |
e85cdca9 | 1936 | ret = relay_recv_metadata(recv_hdr, conn); |
b8aa1682 JD |
1937 | break; |
1938 | case RELAYD_VERSION: | |
e85cdca9 | 1939 | ret = relay_send_version(recv_hdr, conn); |
b8aa1682 | 1940 | break; |
173af62f | 1941 | case RELAYD_CLOSE_STREAM: |
e85cdca9 | 1942 | ret = relay_close_stream(recv_hdr, conn); |
173af62f | 1943 | break; |
6d805429 | 1944 | case RELAYD_DATA_PENDING: |
e85cdca9 | 1945 | ret = relay_data_pending(recv_hdr, conn); |
c8f59ee5 DG |
1946 | break; |
1947 | case RELAYD_QUIESCENT_CONTROL: | |
e85cdca9 | 1948 | ret = relay_quiescent_control(recv_hdr, conn); |
c8f59ee5 | 1949 | break; |
f7079f67 | 1950 | case RELAYD_BEGIN_DATA_PENDING: |
e85cdca9 | 1951 | ret = relay_begin_data_pending(recv_hdr, conn); |
f7079f67 DG |
1952 | break; |
1953 | case RELAYD_END_DATA_PENDING: | |
e85cdca9 | 1954 | ret = relay_end_data_pending(recv_hdr, conn); |
f7079f67 | 1955 | break; |
1c20f0e2 | 1956 | case RELAYD_SEND_INDEX: |
e85cdca9 | 1957 | ret = relay_recv_index(recv_hdr, conn); |
1c20f0e2 | 1958 | break; |
814fcae4 | 1959 | case RELAYD_STREAMS_SENT: |
e85cdca9 | 1960 | ret = relay_streams_sent(recv_hdr, conn); |
814fcae4 | 1961 | break; |
b8aa1682 JD |
1962 | case RELAYD_UPDATE_SYNC_INFO: |
1963 | default: | |
1964 | ERR("Received unknown command (%u)", be32toh(recv_hdr->cmd)); | |
e85cdca9 | 1965 | relay_unknown_command(conn); |
b8aa1682 JD |
1966 | ret = -1; |
1967 | goto end; | |
1968 | } | |
1969 | ||
1970 | end: | |
1971 | return ret; | |
1972 | } | |
1973 | ||
7d2f7452 DG |
1974 | /* |
1975 | * Handle index for a data stream. | |
1976 | * | |
1977 | * RCU read side lock MUST be acquired. | |
1978 | * | |
1979 | * Return 0 on success else a negative value. | |
1980 | */ | |
1981 | static int handle_index_data(struct relay_stream *stream, uint64_t net_seq_num, | |
1982 | int rotate_index) | |
1983 | { | |
1984 | int ret = 0, index_created = 0; | |
1985 | uint64_t stream_id, data_offset; | |
1986 | struct relay_index *index, *wr_index = NULL; | |
1987 | ||
1988 | assert(stream); | |
1989 | ||
1990 | stream_id = stream->stream_handle; | |
1991 | /* Get data offset because we are about to update the index. */ | |
1992 | data_offset = htobe64(stream->tracefile_size_current); | |
1993 | ||
1994 | /* | |
1995 | * Lookup for an existing index for that stream id/sequence number. If on | |
1996 | * exists, the control thread already received the data for it thus we need | |
1997 | * to write it on disk. | |
1998 | */ | |
1999 | index = relay_index_find(stream_id, net_seq_num); | |
2000 | if (!index) { | |
2001 | /* A successful creation will add the object to the HT. */ | |
2002 | index = relay_index_create(stream_id, net_seq_num); | |
2003 | if (!index) { | |
2004 | ret = -1; | |
2005 | goto error; | |
2006 | } | |
2007 | index_created = 1; | |
2008 | } | |
2009 | ||
2010 | if (rotate_index || stream->index_fd < 0) { | |
2011 | index->to_close_fd = stream->index_fd; | |
2012 | ret = index_create_file(stream->path_name, stream->channel_name, | |
2013 | relayd_uid, relayd_gid, stream->tracefile_size, | |
2014 | stream->tracefile_count_current); | |
2015 | if (ret < 0) { | |
2016 | /* This will close the stream's index fd if one. */ | |
2017 | relay_index_free_safe(index); | |
2018 | goto error; | |
2019 | } | |
2020 | stream->index_fd = ret; | |
2021 | } | |
2022 | index->fd = stream->index_fd; | |
2023 | index->index_data.offset = data_offset; | |
2024 | ||
2025 | if (index_created) { | |
2026 | /* | |
2027 | * Try to add the relay index object to the hash table. If an object | |
2028 | * already exist, destroy back the index created and set the data. | |
2029 | */ | |
2030 | relay_index_add(index, &wr_index); | |
2031 | if (wr_index) { | |
2032 | /* Copy back data from the created index. */ | |
2033 | wr_index->fd = index->fd; | |
2034 | wr_index->to_close_fd = index->to_close_fd; | |
2035 | wr_index->index_data.offset = data_offset; | |
2036 | free(index); | |
2037 | } | |
2038 | } else { | |
2039 | /* The index already exists so write it on disk. */ | |
2040 | wr_index = index; | |
2041 | } | |
2042 | ||
2043 | /* Do we have a writable ready index to write on disk. */ | |
2044 | if (wr_index) { | |
2045 | ret = relay_index_write(wr_index->fd, wr_index); | |
2046 | if (ret < 0) { | |
2047 | goto error; | |
2048 | } | |
2049 | stream->total_index_received++; | |
2050 | } | |
2051 | ||
2052 | error: | |
2053 | return ret; | |
2054 | } | |
2055 | ||
b8aa1682 JD |
2056 | /* |
2057 | * relay_process_data: Process the data received on the data socket | |
2058 | */ | |
2059 | static | |
e85cdca9 | 2060 | int relay_process_data(struct relay_connection *conn) |
b8aa1682 | 2061 | { |
7d2f7452 | 2062 | int ret = 0, rotate_index = 0; |
6cd525e8 | 2063 | ssize_t size_ret; |
b8aa1682 JD |
2064 | struct relay_stream *stream; |
2065 | struct lttcomm_relayd_data_hdr data_hdr; | |
7d2f7452 | 2066 | uint64_t stream_id; |
173af62f | 2067 | uint64_t net_seq_num; |
b8aa1682 | 2068 | uint32_t data_size; |
eb702af5 | 2069 | struct relay_session *session; |
b8aa1682 | 2070 | |
e85cdca9 DG |
2071 | assert(conn); |
2072 | ||
2073 | ret = conn->sock->ops->recvmsg(conn->sock, &data_hdr, | |
7c5aef62 | 2074 | sizeof(struct lttcomm_relayd_data_hdr), 0); |
b8aa1682 | 2075 | if (ret <= 0) { |
a6cd2b97 DG |
2076 | if (ret == 0) { |
2077 | /* Orderly shutdown. Not necessary to print an error. */ | |
e85cdca9 | 2078 | DBG("Socket %d did an orderly shutdown", conn->sock->fd); |
a6cd2b97 | 2079 | } else { |
e85cdca9 | 2080 | ERR("Unable to receive data header on sock %d", conn->sock->fd); |
a6cd2b97 | 2081 | } |
b8aa1682 JD |
2082 | ret = -1; |
2083 | goto end; | |
2084 | } | |
2085 | ||
2086 | stream_id = be64toh(data_hdr.stream_id); | |
9d1bbf21 MD |
2087 | |
2088 | rcu_read_lock(); | |
eb702af5 | 2089 | stream = stream_find_by_id(relay_streams_ht, stream_id); |
b8aa1682 JD |
2090 | if (!stream) { |
2091 | ret = -1; | |
1c20f0e2 | 2092 | goto end_rcu_unlock; |
b8aa1682 JD |
2093 | } |
2094 | ||
e85cdca9 | 2095 | session = session_find_by_id(conn->sessions_ht, stream->session_id); |
eb702af5 DG |
2096 | assert(session); |
2097 | ||
b8aa1682 JD |
2098 | data_size = be32toh(data_hdr.data_size); |
2099 | if (data_buffer_size < data_size) { | |
c617c0c6 MD |
2100 | char *tmp_data_ptr; |
2101 | ||
2102 | tmp_data_ptr = realloc(data_buffer, data_size); | |
2103 | if (!tmp_data_ptr) { | |
b8aa1682 | 2104 | ERR("Allocating data buffer"); |
c617c0c6 | 2105 | free(data_buffer); |
b8aa1682 | 2106 | ret = -1; |
1c20f0e2 | 2107 | goto end_rcu_unlock; |
b8aa1682 | 2108 | } |
c617c0c6 | 2109 | data_buffer = tmp_data_ptr; |
b8aa1682 JD |
2110 | data_buffer_size = data_size; |
2111 | } | |
2112 | memset(data_buffer, 0, data_size); | |
2113 | ||
173af62f DG |
2114 | net_seq_num = be64toh(data_hdr.net_seq_num); |
2115 | ||
77c7c900 | 2116 | DBG3("Receiving data of size %u for stream id %" PRIu64 " seqnum %" PRIu64, |
173af62f | 2117 | data_size, stream_id, net_seq_num); |
e85cdca9 | 2118 | ret = conn->sock->ops->recvmsg(conn->sock, data_buffer, data_size, 0); |
b8aa1682 | 2119 | if (ret <= 0) { |
a6cd2b97 DG |
2120 | if (ret == 0) { |
2121 | /* Orderly shutdown. Not necessary to print an error. */ | |
e85cdca9 | 2122 | DBG("Socket %d did an orderly shutdown", conn->sock->fd); |
a6cd2b97 | 2123 | } |
b8aa1682 | 2124 | ret = -1; |
1c20f0e2 | 2125 | goto end_rcu_unlock; |
b8aa1682 JD |
2126 | } |
2127 | ||
1c20f0e2 | 2128 | /* Check if a rotation is needed. */ |
0f907de1 JD |
2129 | if (stream->tracefile_size > 0 && |
2130 | (stream->tracefile_size_current + data_size) > | |
2131 | stream->tracefile_size) { | |
6b6b9a5a JD |
2132 | struct relay_viewer_stream *vstream; |
2133 | uint64_t new_id; | |
2134 | ||
2135 | new_id = (stream->tracefile_count_current + 1) % | |
2136 | stream->tracefile_count; | |
2137 | /* | |
2138 | * When we wrap-around back to 0, we start overwriting old | |
2139 | * trace data. | |
2140 | */ | |
2141 | if (!stream->tracefile_overwrite && new_id == 0) { | |
2142 | stream->tracefile_overwrite = 1; | |
2143 | } | |
2144 | pthread_mutex_lock(&stream->viewer_stream_rotation_lock); | |
2145 | if (stream->tracefile_overwrite) { | |
2146 | stream->oldest_tracefile_id = | |
2147 | (stream->oldest_tracefile_id + 1) % | |
2148 | stream->tracefile_count; | |
2149 | } | |
991adae2 | 2150 | vstream = viewer_stream_find_by_id(stream->stream_handle); |
6b6b9a5a JD |
2151 | if (vstream) { |
2152 | /* | |
2153 | * The viewer is reading a file about to be | |
2154 | * overwritten. Close the FDs it is | |
2155 | * currently using and let it handle the fault. | |
2156 | */ | |
2157 | if (vstream->tracefile_count_current == new_id) { | |
cef0f7d5 | 2158 | pthread_mutex_lock(&vstream->overwrite_lock); |
6b6b9a5a | 2159 | vstream->abort_flag = 1; |
cef0f7d5 | 2160 | pthread_mutex_unlock(&vstream->overwrite_lock); |
6b6b9a5a JD |
2161 | DBG("Streaming side setting abort_flag on stream %s_%lu\n", |
2162 | stream->channel_name, new_id); | |
2163 | } else if (vstream->tracefile_count_current == | |
2164 | stream->tracefile_count_current) { | |
2165 | /* | |
2166 | * The reader and writer were in the | |
2167 | * same trace file, inform the viewer | |
2168 | * that no new index will ever be added | |
2169 | * to this file. | |
2170 | */ | |
2171 | vstream->close_write_flag = 1; | |
2172 | } | |
2173 | } | |
1c20f0e2 JD |
2174 | ret = utils_rotate_stream_file(stream->path_name, stream->channel_name, |
2175 | stream->tracefile_size, stream->tracefile_count, | |
2176 | relayd_uid, relayd_gid, stream->fd, | |
2177 | &(stream->tracefile_count_current), &stream->fd); | |
cef0f7d5 | 2178 | stream->total_index_received = 0; |
6b6b9a5a | 2179 | pthread_mutex_unlock(&stream->viewer_stream_rotation_lock); |
0f907de1 | 2180 | if (ret < 0) { |
1c20f0e2 JD |
2181 | ERR("Rotating stream output file"); |
2182 | goto end_rcu_unlock; | |
0f907de1 | 2183 | } |
a6976990 DG |
2184 | /* Reset current size because we just perform a stream rotation. */ |
2185 | stream->tracefile_size_current = 0; | |
1c20f0e2 JD |
2186 | rotate_index = 1; |
2187 | } | |
2188 | ||
1c20f0e2 | 2189 | /* |
7d2f7452 DG |
2190 | * Index are handled in protocol version 2.4 and above. Also, snapshot and |
2191 | * index are NOT supported. | |
1c20f0e2 | 2192 | */ |
eb702af5 | 2193 | if (session->minor >= 4 && !session->snapshot) { |
7d2f7452 | 2194 | ret = handle_index_data(stream, net_seq_num, rotate_index); |
1c20f0e2 | 2195 | if (ret < 0) { |
1c20f0e2 JD |
2196 | goto end_rcu_unlock; |
2197 | } | |
1c20f0e2 JD |
2198 | } |
2199 | ||
7d2f7452 | 2200 | /* Write data to stream output fd. */ |
6cd525e8 MD |
2201 | size_ret = lttng_write(stream->fd, data_buffer, data_size); |
2202 | if (size_ret < data_size) { | |
b8aa1682 JD |
2203 | ERR("Relay error writing data to file"); |
2204 | ret = -1; | |
1c20f0e2 | 2205 | goto end_rcu_unlock; |
b8aa1682 | 2206 | } |
1d4dfdef | 2207 | |
5ab7344e JD |
2208 | DBG2("Relay wrote %d bytes to tracefile for stream id %" PRIu64, |
2209 | ret, stream->stream_handle); | |
2210 | ||
1d4dfdef DG |
2211 | ret = write_padding_to_file(stream->fd, be32toh(data_hdr.padding_size)); |
2212 | if (ret < 0) { | |
1c20f0e2 | 2213 | goto end_rcu_unlock; |
1d4dfdef | 2214 | } |
1c20f0e2 | 2215 | stream->tracefile_size_current += data_size + be32toh(data_hdr.padding_size); |
1d4dfdef | 2216 | |
173af62f DG |
2217 | stream->prev_seq = net_seq_num; |
2218 | ||
eb702af5 | 2219 | try_close_stream(session, stream); |
173af62f | 2220 | |
1c20f0e2 | 2221 | end_rcu_unlock: |
9d1bbf21 | 2222 | rcu_read_unlock(); |
b8aa1682 JD |
2223 | end: |
2224 | return ret; | |
2225 | } | |
2226 | ||
2227 | static | |
e85cdca9 | 2228 | void cleanup_connection_pollfd(struct lttng_poll_event *events, int pollfd) |
b8aa1682 JD |
2229 | { |
2230 | int ret; | |
2231 | ||
e85cdca9 DG |
2232 | assert(events); |
2233 | ||
2234 | (void) lttng_poll_del(events, pollfd); | |
b8aa1682 JD |
2235 | |
2236 | ret = close(pollfd); | |
2237 | if (ret < 0) { | |
2238 | ERR("Closing pollfd %d", pollfd); | |
2239 | } | |
2240 | } | |
2241 | ||
e85cdca9 DG |
2242 | static void destroy_connection(struct lttng_ht *relay_connections_ht, |
2243 | struct relay_connection *conn) | |
9d1bbf21 | 2244 | { |
e85cdca9 DG |
2245 | assert(relay_connections_ht); |
2246 | assert(conn); | |
eb702af5 | 2247 | |
e85cdca9 | 2248 | connection_delete(relay_connections_ht, conn); |
eb702af5 | 2249 | |
e85cdca9 DG |
2250 | /* For the control socket, we try to destroy the session. */ |
2251 | if (conn->type == RELAY_CONTROL) { | |
2252 | destroy_session(conn->session, conn->sessions_ht); | |
9d1bbf21 | 2253 | } |
5b6d8097 | 2254 | |
e85cdca9 | 2255 | connection_destroy(conn); |
b8aa1682 JD |
2256 | } |
2257 | ||
2258 | /* | |
2259 | * This thread does the actual work | |
2260 | */ | |
2261 | static | |
2262 | void *relay_thread_worker(void *data) | |
2263 | { | |
beaad64c DG |
2264 | int ret, err = -1, last_seen_data_fd = -1; |
2265 | uint32_t nb_fd; | |
e85cdca9 | 2266 | struct relay_connection *conn; |
b8aa1682 JD |
2267 | struct lttng_poll_event events; |
2268 | struct lttng_ht *relay_connections_ht; | |
b8aa1682 | 2269 | struct lttng_ht_iter iter; |
b8aa1682 | 2270 | struct lttcomm_relayd_hdr recv_hdr; |
d3e2ba59 JD |
2271 | struct relay_local_data *relay_ctx = (struct relay_local_data *) data; |
2272 | struct lttng_ht *sessions_ht = relay_ctx->sessions_ht; | |
b8aa1682 JD |
2273 | |
2274 | DBG("[thread] Relay worker started"); | |
2275 | ||
9d1bbf21 MD |
2276 | rcu_register_thread(); |
2277 | ||
55706a7d MD |
2278 | health_register(health_relayd, HEALTH_RELAYD_TYPE_WORKER); |
2279 | ||
4e5b8b82 MD |
2280 | if (testpoint(relayd_thread_worker)) { |
2281 | goto error_testpoint; | |
2282 | } | |
2283 | ||
f385ae0a MD |
2284 | health_code_update(); |
2285 | ||
b8aa1682 JD |
2286 | /* table of connections indexed on socket */ |
2287 | relay_connections_ht = lttng_ht_new(0, LTTNG_HT_TYPE_ULONG); | |
095a4ae5 MD |
2288 | if (!relay_connections_ht) { |
2289 | goto relay_connections_ht_error; | |
2290 | } | |
b8aa1682 | 2291 | |
1c20f0e2 JD |
2292 | /* Tables of received indexes indexed by index handle and net_seq_num. */ |
2293 | indexes_ht = lttng_ht_new(0, LTTNG_HT_TYPE_TWO_U64); | |
2294 | if (!indexes_ht) { | |
2295 | goto indexes_ht_error; | |
2296 | } | |
2297 | ||
b8aa1682 JD |
2298 | ret = create_thread_poll_set(&events, 2); |
2299 | if (ret < 0) { | |
2300 | goto error_poll_create; | |
2301 | } | |
2302 | ||
e85cdca9 | 2303 | ret = lttng_poll_add(&events, relay_conn_pipe[0], LPOLLIN | LPOLLRDHUP); |
b8aa1682 JD |
2304 | if (ret < 0) { |
2305 | goto error; | |
2306 | } | |
2307 | ||
beaad64c | 2308 | restart: |
b8aa1682 | 2309 | while (1) { |
beaad64c DG |
2310 | int idx = -1, i, seen_control = 0, last_notdel_data_fd = -1; |
2311 | ||
f385ae0a MD |
2312 | health_code_update(); |
2313 | ||
b8aa1682 | 2314 | /* Infinite blocking call, waiting for transmission */ |
87c1611d | 2315 | DBG3("Relayd worker thread polling..."); |
f385ae0a | 2316 | health_poll_entry(); |
b8aa1682 | 2317 | ret = lttng_poll_wait(&events, -1); |
f385ae0a | 2318 | health_poll_exit(); |
b8aa1682 JD |
2319 | if (ret < 0) { |
2320 | /* | |
2321 | * Restart interrupted system call. | |
2322 | */ | |
2323 | if (errno == EINTR) { | |
2324 | goto restart; | |
2325 | } | |
2326 | goto error; | |
2327 | } | |
2328 | ||
0d9c5d77 DG |
2329 | nb_fd = ret; |
2330 | ||
beaad64c DG |
2331 | /* |
2332 | * Process control. The control connection is prioritised so we don't | |
2333 | * starve it with high throughout put tracing data on the data | |
2334 | * connection. | |
2335 | */ | |
b8aa1682 JD |
2336 | for (i = 0; i < nb_fd; i++) { |
2337 | /* Fetch once the poll data */ | |
beaad64c DG |
2338 | uint32_t revents = LTTNG_POLL_GETEV(&events, i); |
2339 | int pollfd = LTTNG_POLL_GETFD(&events, i); | |
b8aa1682 | 2340 | |
f385ae0a MD |
2341 | health_code_update(); |
2342 | ||
b8aa1682 JD |
2343 | /* Thread quit pipe has been closed. Killing thread. */ |
2344 | ret = check_thread_quit_pipe(pollfd, revents); | |
2345 | if (ret) { | |
095a4ae5 MD |
2346 | err = 0; |
2347 | goto exit; | |
b8aa1682 JD |
2348 | } |
2349 | ||
e85cdca9 DG |
2350 | /* Inspect the relay conn pipe for new connection */ |
2351 | if (pollfd == relay_conn_pipe[0]) { | |
b8aa1682 | 2352 | if (revents & (LPOLLERR | LPOLLHUP | LPOLLRDHUP)) { |
e85cdca9 | 2353 | ERR("Relay connection pipe error"); |
b8aa1682 JD |
2354 | goto error; |
2355 | } else if (revents & LPOLLIN) { | |
e85cdca9 | 2356 | ret = lttng_read(relay_conn_pipe[0], &conn, sizeof(conn)); |
b8aa1682 JD |
2357 | if (ret < 0) { |
2358 | goto error; | |
2359 | } | |
e85cdca9 DG |
2360 | conn->sessions_ht = sessions_ht; |
2361 | connection_init(conn); | |
2362 | lttng_poll_add(&events, conn->sock->fd, | |
2363 | LPOLLIN | LPOLLRDHUP); | |
2364 | rcu_read_lock(); | |
2365 | lttng_ht_add_unique_ulong(relay_connections_ht, | |
2366 | &conn->sock_n); | |
9d1bbf21 | 2367 | rcu_read_unlock(); |
e85cdca9 | 2368 | DBG("Connection socket %d added", conn->sock->fd); |
b8aa1682 | 2369 | } |
e85cdca9 DG |
2370 | } else { |
2371 | rcu_read_lock(); | |
2372 | conn = connection_find_by_sock(relay_connections_ht, pollfd); | |
2373 | /* If not found, there is a synchronization issue. */ | |
2374 | assert(conn); | |
2375 | ||
2376 | if (revents & (LPOLLERR | LPOLLHUP | LPOLLRDHUP)) { | |
2377 | cleanup_connection_pollfd(&events, pollfd); | |
2378 | destroy_connection(relay_connections_ht, conn); | |
beaad64c DG |
2379 | if (last_seen_data_fd == pollfd) { |
2380 | last_seen_data_fd = last_notdel_data_fd; | |
2381 | } | |
b8aa1682 | 2382 | } else if (revents & LPOLLIN) { |
e85cdca9 DG |
2383 | if (conn->type == RELAY_CONTROL) { |
2384 | ret = conn->sock->ops->recvmsg(conn->sock, &recv_hdr, | |
2385 | sizeof(recv_hdr), 0); | |
b8aa1682 | 2386 | if (ret <= 0) { |
e85cdca9 DG |
2387 | /* Connection closed */ |
2388 | cleanup_connection_pollfd(&events, pollfd); | |
2389 | destroy_connection(relay_connections_ht, conn); | |
b8aa1682 JD |
2390 | DBG("Control connection closed with %d", pollfd); |
2391 | } else { | |
e85cdca9 | 2392 | ret = relay_process_control(&recv_hdr, conn); |
b8aa1682 | 2393 | if (ret < 0) { |
beaad64c | 2394 | /* Clear the session on error. */ |
e85cdca9 DG |
2395 | cleanup_connection_pollfd(&events, pollfd); |
2396 | destroy_connection(relay_connections_ht, conn); | |
b8aa1682 JD |
2397 | DBG("Connection closed with %d", pollfd); |
2398 | } | |
beaad64c | 2399 | seen_control = 1; |
b8aa1682 | 2400 | } |
beaad64c DG |
2401 | } else { |
2402 | /* | |
2403 | * Flag the last seen data fd not deleted. It will be | |
2404 | * used as the last seen fd if any fd gets deleted in | |
2405 | * this first loop. | |
2406 | */ | |
2407 | last_notdel_data_fd = pollfd; | |
2408 | } | |
e85cdca9 DG |
2409 | } else { |
2410 | ERR("Unknown poll events %u for sock %d", revents, pollfd); | |
beaad64c DG |
2411 | } |
2412 | rcu_read_unlock(); | |
2413 | } | |
2414 | } | |
2415 | ||
2416 | /* | |
2417 | * The last loop handled a control request, go back to poll to make | |
2418 | * sure we prioritise the control socket. | |
2419 | */ | |
2420 | if (seen_control) { | |
2421 | continue; | |
2422 | } | |
2423 | ||
2424 | if (last_seen_data_fd >= 0) { | |
2425 | for (i = 0; i < nb_fd; i++) { | |
2426 | int pollfd = LTTNG_POLL_GETFD(&events, i); | |
f385ae0a MD |
2427 | |
2428 | health_code_update(); | |
2429 | ||
beaad64c DG |
2430 | if (last_seen_data_fd == pollfd) { |
2431 | idx = i; | |
2432 | break; | |
2433 | } | |
2434 | } | |
2435 | } | |
2436 | ||
2437 | /* Process data connection. */ | |
2438 | for (i = idx + 1; i < nb_fd; i++) { | |
2439 | /* Fetch the poll data. */ | |
2440 | uint32_t revents = LTTNG_POLL_GETEV(&events, i); | |
2441 | int pollfd = LTTNG_POLL_GETFD(&events, i); | |
2442 | ||
f385ae0a MD |
2443 | health_code_update(); |
2444 | ||
beaad64c | 2445 | /* Skip the command pipe. It's handled in the first loop. */ |
e85cdca9 | 2446 | if (pollfd == relay_conn_pipe[0]) { |
beaad64c DG |
2447 | continue; |
2448 | } | |
2449 | ||
2450 | if (revents) { | |
2451 | rcu_read_lock(); | |
e85cdca9 DG |
2452 | conn = connection_find_by_sock(relay_connections_ht, pollfd); |
2453 | if (!conn) { | |
beaad64c DG |
2454 | /* Skip it. Might be removed before. */ |
2455 | rcu_read_unlock(); | |
2456 | continue; | |
2457 | } | |
beaad64c DG |
2458 | |
2459 | if (revents & LPOLLIN) { | |
e85cdca9 | 2460 | if (conn->type != RELAY_DATA) { |
beaad64c DG |
2461 | continue; |
2462 | } | |
2463 | ||
e85cdca9 DG |
2464 | ret = relay_process_data(conn); |
2465 | /* Connection closed */ | |
beaad64c | 2466 | if (ret < 0) { |
e85cdca9 DG |
2467 | cleanup_connection_pollfd(&events, pollfd); |
2468 | destroy_connection(relay_connections_ht, conn); | |
beaad64c DG |
2469 | DBG("Data connection closed with %d", pollfd); |
2470 | /* | |
2471 | * Every goto restart call sets the last seen fd where | |
2472 | * here we don't really care since we gracefully | |
2473 | * continue the loop after the connection is deleted. | |
2474 | */ | |
2475 | } else { | |
2476 | /* Keep last seen port. */ | |
2477 | last_seen_data_fd = pollfd; | |
2478 | rcu_read_unlock(); | |
2479 | goto restart; | |
b8aa1682 JD |
2480 | } |
2481 | } | |
9d1bbf21 | 2482 | rcu_read_unlock(); |
b8aa1682 JD |
2483 | } |
2484 | } | |
beaad64c | 2485 | last_seen_data_fd = -1; |
b8aa1682 JD |
2486 | } |
2487 | ||
f385ae0a MD |
2488 | /* Normal exit, no error */ |
2489 | ret = 0; | |
2490 | ||
095a4ae5 | 2491 | exit: |
b8aa1682 JD |
2492 | error: |
2493 | lttng_poll_clean(&events); | |
2494 | ||
e85cdca9 | 2495 | /* Cleanup reamaining connection object. */ |
9d1bbf21 | 2496 | rcu_read_lock(); |
e85cdca9 DG |
2497 | cds_lfht_for_each_entry(relay_connections_ht->ht, &iter.iter, conn, |
2498 | sock_n.node) { | |
f385ae0a | 2499 | health_code_update(); |
e85cdca9 | 2500 | destroy_connection(relay_connections_ht, conn); |
b8aa1682 | 2501 | } |
94d49140 | 2502 | rcu_read_unlock(); |
7d2f7452 DG |
2503 | error_poll_create: |
2504 | lttng_ht_destroy(indexes_ht); | |
1c20f0e2 | 2505 | indexes_ht_error: |
b8aa1682 | 2506 | lttng_ht_destroy(relay_connections_ht); |
095a4ae5 | 2507 | relay_connections_ht_error: |
e85cdca9 DG |
2508 | /* Close relay conn pipes */ |
2509 | utils_close_pipe(relay_conn_pipe); | |
095a4ae5 MD |
2510 | if (err) { |
2511 | DBG("Thread exited with error"); | |
2512 | } | |
b8aa1682 | 2513 | DBG("Worker thread cleanup complete"); |
095a4ae5 | 2514 | free(data_buffer); |
4e5b8b82 | 2515 | error_testpoint: |
f385ae0a MD |
2516 | if (err) { |
2517 | health_error(); | |
2518 | ERR("Health error occurred in %s", __func__); | |
2519 | } | |
2520 | health_unregister(health_relayd); | |
9d1bbf21 | 2521 | rcu_unregister_thread(); |
f385ae0a | 2522 | stop_threads(); |
b8aa1682 JD |
2523 | return NULL; |
2524 | } | |
2525 | ||
2526 | /* | |
2527 | * Create the relay command pipe to wake thread_manage_apps. | |
2528 | * Closed in cleanup(). | |
2529 | */ | |
e85cdca9 | 2530 | static int create_relay_conn_pipe(void) |
b8aa1682 | 2531 | { |
a02de639 | 2532 | int ret; |
b8aa1682 | 2533 | |
e85cdca9 | 2534 | ret = utils_create_pipe_cloexec(relay_conn_pipe); |
b8aa1682 | 2535 | |
b8aa1682 JD |
2536 | return ret; |
2537 | } | |
2538 | ||
2539 | /* | |
2540 | * main | |
2541 | */ | |
2542 | int main(int argc, char **argv) | |
2543 | { | |
2544 | int ret = 0; | |
2545 | void *status; | |
d3e2ba59 | 2546 | struct relay_local_data *relay_ctx; |
b8aa1682 | 2547 | |
b8aa1682 JD |
2548 | /* Parse arguments */ |
2549 | progname = argv[0]; | |
c617c0c6 | 2550 | if ((ret = parse_args(argc, argv)) < 0) { |
a02de639 | 2551 | goto exit; |
b8aa1682 JD |
2552 | } |
2553 | ||
2554 | if ((ret = set_signal_handler()) < 0) { | |
2555 | goto exit; | |
2556 | } | |
2557 | ||
4d513a50 DG |
2558 | /* Try to create directory if -o, --output is specified. */ |
2559 | if (opt_output_path) { | |
994fa64f DG |
2560 | if (*opt_output_path != '/') { |
2561 | ERR("Please specify an absolute path for -o, --output PATH"); | |
2562 | goto exit; | |
2563 | } | |
2564 | ||
4d513a50 DG |
2565 | ret = utils_mkdir_recursive(opt_output_path, S_IRWXU | S_IRWXG); |
2566 | if (ret < 0) { | |
2567 | ERR("Unable to create %s", opt_output_path); | |
2568 | goto exit; | |
2569 | } | |
2570 | } | |
2571 | ||
b8aa1682 | 2572 | /* Daemonize */ |
d1a3048a | 2573 | if (opt_daemon || opt_background) { |
02a6bb53 MD |
2574 | int i; |
2575 | ||
2576 | ret = lttng_daemonize(&child_ppid, &recv_child_signal, | |
2577 | !opt_background); | |
b8aa1682 | 2578 | if (ret < 0) { |
a02de639 | 2579 | goto exit; |
b8aa1682 | 2580 | } |
02a6bb53 MD |
2581 | |
2582 | /* | |
2583 | * We are in the child. Make sure all other file | |
2584 | * descriptors are closed, in case we are called with | |
2585 | * more opened file descriptors than the standard ones. | |
2586 | */ | |
2587 | for (i = 3; i < sysconf(_SC_OPEN_MAX); i++) { | |
2588 | (void) close(i); | |
2589 | } | |
2590 | } | |
2591 | ||
2592 | /* Create thread quit pipe */ | |
2593 | if ((ret = init_thread_quit_pipe()) < 0) { | |
2594 | goto error; | |
b8aa1682 JD |
2595 | } |
2596 | ||
1c20f0e2 JD |
2597 | /* We need those values for the file/dir creation. */ |
2598 | relayd_uid = getuid(); | |
2599 | relayd_gid = getgid(); | |
b8aa1682 | 2600 | |
1c20f0e2 JD |
2601 | /* Check if daemon is UID = 0 */ |
2602 | if (relayd_uid == 0) { | |
a6a062a4 AM |
2603 | if (control_uri->port < 1024 || data_uri->port < 1024 || |
2604 | live_uri->port < 1024) { | |
b8aa1682 JD |
2605 | ERR("Need to be root to use ports < 1024"); |
2606 | ret = -1; | |
a02de639 | 2607 | goto exit; |
b8aa1682 JD |
2608 | } |
2609 | } | |
2610 | ||
2611 | /* Setup the thread apps communication pipe. */ | |
e85cdca9 | 2612 | if ((ret = create_relay_conn_pipe()) < 0) { |
b8aa1682 JD |
2613 | goto exit; |
2614 | } | |
2615 | ||
2616 | /* Init relay command queue. */ | |
e85cdca9 | 2617 | cds_wfq_init(&relay_conn_queue.queue); |
b8aa1682 JD |
2618 | |
2619 | /* Set up max poll set size */ | |
2620 | lttng_poll_set_max_size(); | |
2621 | ||
554831e7 MD |
2622 | /* Initialize communication library */ |
2623 | lttcomm_init(); | |
d5a5e58f | 2624 | lttcomm_inet_init(); |
554831e7 | 2625 | |
d3e2ba59 JD |
2626 | relay_ctx = zmalloc(sizeof(struct relay_local_data)); |
2627 | if (!relay_ctx) { | |
2628 | PERROR("relay_ctx"); | |
2629 | goto exit; | |
2630 | } | |
2631 | ||
2632 | /* tables of sessions indexed by session ID */ | |
eb702af5 | 2633 | relay_ctx->sessions_ht = lttng_ht_new(0, LTTNG_HT_TYPE_U64); |
d3e2ba59 JD |
2634 | if (!relay_ctx->sessions_ht) { |
2635 | goto exit_relay_ctx_sessions; | |
2636 | } | |
2637 | ||
2638 | /* tables of streams indexed by stream ID */ | |
eb702af5 | 2639 | relay_streams_ht = lttng_ht_new(0, LTTNG_HT_TYPE_U64); |
d3e2ba59 JD |
2640 | if (!relay_streams_ht) { |
2641 | goto exit_relay_ctx_streams; | |
2642 | } | |
2643 | ||
2644 | /* tables of streams indexed by stream ID */ | |
92c6ca54 DG |
2645 | viewer_streams_ht = lttng_ht_new(0, LTTNG_HT_TYPE_U64); |
2646 | if (!viewer_streams_ht) { | |
d3e2ba59 JD |
2647 | goto exit_relay_ctx_viewer_streams; |
2648 | } | |
2649 | ||
55706a7d MD |
2650 | /* Initialize thread health monitoring */ |
2651 | health_relayd = health_app_create(NR_HEALTH_RELAYD_TYPES); | |
2652 | if (!health_relayd) { | |
2653 | PERROR("health_app_create error"); | |
2654 | goto exit_health_app_create; | |
2655 | } | |
2656 | ||
65931c8b MD |
2657 | ret = utils_create_pipe(health_quit_pipe); |
2658 | if (ret < 0) { | |
2659 | goto error_health_pipe; | |
2660 | } | |
2661 | ||
2662 | /* Create thread to manage the client socket */ | |
2663 | ret = pthread_create(&health_thread, NULL, | |
2664 | thread_manage_health, (void *) NULL); | |
2665 | if (ret != 0) { | |
2666 | PERROR("pthread_create health"); | |
2667 | goto health_error; | |
2668 | } | |
2669 | ||
b8aa1682 JD |
2670 | /* Setup the dispatcher thread */ |
2671 | ret = pthread_create(&dispatcher_thread, NULL, | |
2672 | relay_thread_dispatcher, (void *) NULL); | |
2673 | if (ret != 0) { | |
2674 | PERROR("pthread_create dispatcher"); | |
2675 | goto exit_dispatcher; | |
2676 | } | |
2677 | ||
2678 | /* Setup the worker thread */ | |
2679 | ret = pthread_create(&worker_thread, NULL, | |
d3e2ba59 | 2680 | relay_thread_worker, (void *) relay_ctx); |
b8aa1682 JD |
2681 | if (ret != 0) { |
2682 | PERROR("pthread_create worker"); | |
2683 | goto exit_worker; | |
2684 | } | |
2685 | ||
2686 | /* Setup the listener thread */ | |
2687 | ret = pthread_create(&listener_thread, NULL, | |
2688 | relay_thread_listener, (void *) NULL); | |
2689 | if (ret != 0) { | |
2690 | PERROR("pthread_create listener"); | |
2691 | goto exit_listener; | |
2692 | } | |
2693 | ||
3557d456 | 2694 | ret = live_start_threads(live_uri, relay_ctx); |
d3e2ba59 JD |
2695 | if (ret != 0) { |
2696 | ERR("Starting live viewer threads"); | |
50138f51 | 2697 | goto exit_live; |
d3e2ba59 JD |
2698 | } |
2699 | ||
50138f51 | 2700 | exit_live: |
b8aa1682 JD |
2701 | ret = pthread_join(listener_thread, &status); |
2702 | if (ret != 0) { | |
2703 | PERROR("pthread_join"); | |
2704 | goto error; /* join error, exit without cleanup */ | |
2705 | } | |
2706 | ||
50138f51 | 2707 | exit_listener: |
b8aa1682 JD |
2708 | ret = pthread_join(worker_thread, &status); |
2709 | if (ret != 0) { | |
2710 | PERROR("pthread_join"); | |
2711 | goto error; /* join error, exit without cleanup */ | |
2712 | } | |
2713 | ||
50138f51 | 2714 | exit_worker: |
b8aa1682 JD |
2715 | ret = pthread_join(dispatcher_thread, &status); |
2716 | if (ret != 0) { | |
2717 | PERROR("pthread_join"); | |
2718 | goto error; /* join error, exit without cleanup */ | |
2719 | } | |
42415026 | 2720 | |
50138f51 | 2721 | exit_dispatcher: |
65931c8b MD |
2722 | ret = pthread_join(health_thread, &status); |
2723 | if (ret != 0) { | |
2724 | PERROR("pthread_join health thread"); | |
2725 | goto error; /* join error, exit without cleanup */ | |
2726 | } | |
2727 | ||
bd11e201 MD |
2728 | /* |
2729 | * Stop live threads only after joining other threads. | |
2730 | */ | |
2731 | live_stop_threads(); | |
2732 | ||
65931c8b MD |
2733 | health_error: |
2734 | utils_close_pipe(health_quit_pipe); | |
2735 | ||
2736 | error_health_pipe: | |
55706a7d MD |
2737 | health_app_destroy(health_relayd); |
2738 | ||
2739 | exit_health_app_create: | |
92c6ca54 | 2740 | lttng_ht_destroy(viewer_streams_ht); |
d3e2ba59 JD |
2741 | |
2742 | exit_relay_ctx_viewer_streams: | |
2743 | lttng_ht_destroy(relay_streams_ht); | |
2744 | ||
2745 | exit_relay_ctx_streams: | |
2746 | lttng_ht_destroy(relay_ctx->sessions_ht); | |
2747 | ||
2748 | exit_relay_ctx_sessions: | |
2749 | free(relay_ctx); | |
b8aa1682 JD |
2750 | |
2751 | exit: | |
2752 | cleanup(); | |
2753 | if (!ret) { | |
2754 | exit(EXIT_SUCCESS); | |
2755 | } | |
a02de639 | 2756 | |
b8aa1682 JD |
2757 | error: |
2758 | exit(EXIT_FAILURE); | |
2759 | } |