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