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