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