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