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