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); | |
28e9df91 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 | ||
6550503d 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. */ | |
895 | cds_wfq_enqueue(&relay_conn_queue.queue, &new_conn->qnode); | |
b8aa1682 JD |
896 | |
897 | /* | |
58eb9381 DG |
898 | * Wake the dispatch queue futex. Implicit memory barrier with |
899 | * the exchange in cds_wfq_enqueue. | |
b8aa1682 | 900 | */ |
58eb9381 | 901 | futex_nto1_wake(&relay_conn_queue.futex); |
b8aa1682 JD |
902 | } |
903 | } | |
904 | } | |
905 | ||
095a4ae5 | 906 | exit: |
b8aa1682 JD |
907 | error: |
908 | error_poll_add: | |
9b5e0863 | 909 | error_testpoint: |
b8aa1682 JD |
910 | lttng_poll_clean(&events); |
911 | error_create_poll: | |
095a4ae5 MD |
912 | if (data_sock->fd >= 0) { |
913 | ret = data_sock->ops->close(data_sock); | |
b8aa1682 JD |
914 | if (ret) { |
915 | PERROR("close"); | |
916 | } | |
b8aa1682 | 917 | } |
095a4ae5 MD |
918 | lttcomm_destroy_sock(data_sock); |
919 | error_sock_relay: | |
920 | if (control_sock->fd >= 0) { | |
921 | ret = control_sock->ops->close(control_sock); | |
b8aa1682 JD |
922 | if (ret) { |
923 | PERROR("close"); | |
924 | } | |
b8aa1682 | 925 | } |
095a4ae5 MD |
926 | lttcomm_destroy_sock(control_sock); |
927 | error_sock_control: | |
928 | if (err) { | |
f385ae0a MD |
929 | health_error(); |
930 | ERR("Health error occurred in %s", __func__); | |
095a4ae5 | 931 | } |
55706a7d | 932 | health_unregister(health_relayd); |
b8aa1682 JD |
933 | DBG("Relay listener thread cleanup complete"); |
934 | stop_threads(); | |
b8aa1682 JD |
935 | return NULL; |
936 | } | |
937 | ||
938 | /* | |
939 | * This thread manages the dispatching of the requests to worker threads | |
940 | */ | |
941 | static | |
942 | void *relay_thread_dispatcher(void *data) | |
943 | { | |
6cd525e8 MD |
944 | int err = -1; |
945 | ssize_t ret; | |
b8aa1682 | 946 | struct cds_wfq_node *node; |
58eb9381 | 947 | struct relay_connection *new_conn = NULL; |
b8aa1682 JD |
948 | |
949 | DBG("[thread] Relay dispatcher started"); | |
950 | ||
55706a7d MD |
951 | health_register(health_relayd, HEALTH_RELAYD_TYPE_DISPATCHER); |
952 | ||
9b5e0863 MD |
953 | if (testpoint(relayd_thread_dispatcher)) { |
954 | goto error_testpoint; | |
955 | } | |
956 | ||
f385ae0a MD |
957 | health_code_update(); |
958 | ||
26c9d55e | 959 | while (!CMM_LOAD_SHARED(dispatch_thread_exit)) { |
f385ae0a MD |
960 | health_code_update(); |
961 | ||
b8aa1682 | 962 | /* Atomically prepare the queue futex */ |
58eb9381 | 963 | futex_nto1_prepare(&relay_conn_queue.futex); |
b8aa1682 JD |
964 | |
965 | do { | |
f385ae0a MD |
966 | health_code_update(); |
967 | ||
b8aa1682 | 968 | /* Dequeue commands */ |
58eb9381 | 969 | node = cds_wfq_dequeue_blocking(&relay_conn_queue.queue); |
b8aa1682 JD |
970 | if (node == NULL) { |
971 | DBG("Woken up but nothing in the relay command queue"); | |
972 | /* Continue thread execution */ | |
973 | break; | |
974 | } | |
58eb9381 | 975 | new_conn = caa_container_of(node, struct relay_connection, qnode); |
b8aa1682 | 976 | |
58eb9381 | 977 | DBG("Dispatching request waiting on sock %d", new_conn->sock->fd); |
b8aa1682 JD |
978 | |
979 | /* | |
58eb9381 DG |
980 | * Inform worker thread of the new request. This call is blocking |
981 | * so we can be assured that the data will be read at some point in | |
982 | * time or wait to the end of the world :) | |
b8aa1682 | 983 | */ |
58eb9381 DG |
984 | ret = lttng_write(relay_conn_pipe[1], &new_conn, sizeof(new_conn)); |
985 | if (ret < 0) { | |
986 | PERROR("write connection pipe"); | |
987 | connection_destroy(new_conn); | |
b8aa1682 JD |
988 | goto error; |
989 | } | |
990 | } while (node != NULL); | |
991 | ||
992 | /* Futex wait on queue. Blocking call on futex() */ | |
f385ae0a | 993 | health_poll_entry(); |
58eb9381 | 994 | futex_nto1_wait(&relay_conn_queue.futex); |
f385ae0a | 995 | health_poll_exit(); |
b8aa1682 JD |
996 | } |
997 | ||
f385ae0a MD |
998 | /* Normal exit, no error */ |
999 | err = 0; | |
1000 | ||
b8aa1682 | 1001 | error: |
9b5e0863 | 1002 | error_testpoint: |
f385ae0a MD |
1003 | if (err) { |
1004 | health_error(); | |
1005 | ERR("Health error occurred in %s", __func__); | |
1006 | } | |
55706a7d | 1007 | health_unregister(health_relayd); |
b8aa1682 JD |
1008 | DBG("Dispatch thread dying"); |
1009 | stop_threads(); | |
1010 | return NULL; | |
1011 | } | |
1012 | ||
2a174661 | 1013 | static void try_close_streams(struct relay_session *session) |
d3e2ba59 | 1014 | { |
2a174661 | 1015 | struct ctf_trace *ctf_trace; |
94d49140 JD |
1016 | struct lttng_ht_iter iter; |
1017 | ||
2a174661 | 1018 | assert(session); |
94d49140 | 1019 | |
2a174661 DG |
1020 | pthread_mutex_lock(&session->viewer_ready_lock); |
1021 | rcu_read_lock(); | |
1022 | cds_lfht_for_each_entry(session->ctf_traces_ht->ht, &iter.iter, ctf_trace, | |
1023 | node.node) { | |
1024 | struct relay_stream *stream; | |
94d49140 | 1025 | |
2a174661 DG |
1026 | /* Close streams. */ |
1027 | cds_list_for_each_entry(stream, &ctf_trace->stream_list, trace_list) { | |
1028 | stream_close(session, stream); | |
94d49140 | 1029 | } |
94d49140 | 1030 | |
2a174661 DG |
1031 | ctf_trace->invalid_flag = 1; |
1032 | ctf_trace_try_destroy(session, ctf_trace); | |
157df586 | 1033 | } |
2a174661 DG |
1034 | rcu_read_unlock(); |
1035 | pthread_mutex_unlock(&session->viewer_ready_lock); | |
94d49140 JD |
1036 | } |
1037 | ||
b8aa1682 | 1038 | /* |
2a174661 | 1039 | * Try to destroy a session within a connection. |
b8aa1682 | 1040 | */ |
58eb9381 | 1041 | static void destroy_session(struct relay_session *session, |
d3e2ba59 | 1042 | struct lttng_ht *sessions_ht) |
b8aa1682 | 1043 | { |
58eb9381 | 1044 | assert(session); |
2a174661 | 1045 | assert(sessions_ht); |
b8aa1682 | 1046 | |
2a174661 | 1047 | /* Indicate that this session can be destroyed from now on. */ |
58eb9381 | 1048 | session->close_flag = 1; |
b8aa1682 | 1049 | |
58eb9381 | 1050 | try_close_streams(session); |
5b6d8097 | 1051 | |
2a174661 DG |
1052 | /* |
1053 | * This will try to delete and destroy the session if no viewer is attached | |
1054 | * to it meaning the refcount is down to zero. | |
1055 | */ | |
58eb9381 | 1056 | session_try_destroy(sessions_ht, session); |
b8aa1682 JD |
1057 | } |
1058 | ||
1c20f0e2 JD |
1059 | /* |
1060 | * Copy index data from the control port to a given index object. | |
1061 | */ | |
1062 | static void copy_index_control_data(struct relay_index *index, | |
1063 | struct lttcomm_relayd_index *data) | |
1064 | { | |
1065 | assert(index); | |
1066 | assert(data); | |
1067 | ||
1068 | /* | |
1069 | * The index on disk is encoded in big endian, so we don't need to convert | |
1070 | * the data received on the network. The data_offset value is NEVER | |
1071 | * modified here and is updated by the data thread. | |
1072 | */ | |
1073 | index->index_data.packet_size = data->packet_size; | |
1074 | index->index_data.content_size = data->content_size; | |
1075 | index->index_data.timestamp_begin = data->timestamp_begin; | |
1076 | index->index_data.timestamp_end = data->timestamp_end; | |
1077 | index->index_data.events_discarded = data->events_discarded; | |
1078 | index->index_data.stream_id = data->stream_id; | |
1079 | } | |
1080 | ||
c5b6f4f0 DG |
1081 | /* |
1082 | * Handle the RELAYD_CREATE_SESSION command. | |
1083 | * | |
1084 | * On success, send back the session id or else return a negative value. | |
1085 | */ | |
1086 | static | |
1087 | int relay_create_session(struct lttcomm_relayd_hdr *recv_hdr, | |
58eb9381 | 1088 | struct relay_connection *conn) |
c5b6f4f0 DG |
1089 | { |
1090 | int ret = 0, send_ret; | |
1091 | struct relay_session *session; | |
1092 | struct lttcomm_relayd_status_session reply; | |
1093 | ||
1094 | assert(recv_hdr); | |
58eb9381 | 1095 | assert(conn); |
c5b6f4f0 DG |
1096 | |
1097 | memset(&reply, 0, sizeof(reply)); | |
1098 | ||
2a174661 DG |
1099 | session = session_create(); |
1100 | if (!session) { | |
c5b6f4f0 DG |
1101 | ret = -1; |
1102 | goto error; | |
1103 | } | |
58eb9381 DG |
1104 | session->minor = conn->minor; |
1105 | session->major = conn->major; | |
1106 | conn->session_id = session->id; | |
1107 | conn->session = session; | |
c5b6f4f0 DG |
1108 | |
1109 | reply.session_id = htobe64(session->id); | |
1110 | ||
58eb9381 | 1111 | switch (conn->minor) { |
2a174661 DG |
1112 | case 1: |
1113 | case 2: | |
1114 | case 3: | |
1115 | break; | |
1116 | case 4: /* LTTng sessiond 2.4 */ | |
1117 | default: | |
58eb9381 | 1118 | ret = cmd_create_session_2_4(conn, session); |
d3e2ba59 JD |
1119 | } |
1120 | ||
58eb9381 | 1121 | lttng_ht_add_unique_u64(conn->sessions_ht, &session->session_n); |
c5b6f4f0 DG |
1122 | DBG("Created session %" PRIu64, session->id); |
1123 | ||
1124 | error: | |
1125 | if (ret < 0) { | |
1126 | reply.ret_code = htobe32(LTTNG_ERR_FATAL); | |
1127 | } else { | |
1128 | reply.ret_code = htobe32(LTTNG_OK); | |
1129 | } | |
1130 | ||
58eb9381 | 1131 | send_ret = conn->sock->ops->sendmsg(conn->sock, &reply, sizeof(reply), 0); |
c5b6f4f0 DG |
1132 | if (send_ret < 0) { |
1133 | ERR("Relayd sending session id"); | |
4169f5ad | 1134 | ret = send_ret; |
c5b6f4f0 DG |
1135 | } |
1136 | ||
1137 | return ret; | |
1138 | } | |
1139 | ||
a4baae1b JD |
1140 | /* |
1141 | * When we have received all the streams and the metadata for a channel, | |
1142 | * we make them visible to the viewer threads. | |
1143 | */ | |
1144 | static | |
58eb9381 | 1145 | void set_viewer_ready_flag(struct relay_connection *conn) |
a4baae1b | 1146 | { |
2a174661 | 1147 | struct relay_stream *stream, *tmp_stream; |
a4baae1b | 1148 | |
58eb9381 DG |
1149 | pthread_mutex_lock(&conn->session->viewer_ready_lock); |
1150 | cds_list_for_each_entry_safe(stream, tmp_stream, &conn->recv_head, | |
2a174661 | 1151 | recv_list) { |
a4baae1b | 1152 | stream->viewer_ready = 1; |
2a174661 | 1153 | cds_list_del(&stream->recv_list); |
a4baae1b | 1154 | } |
58eb9381 | 1155 | pthread_mutex_unlock(&conn->session->viewer_ready_lock); |
a4baae1b JD |
1156 | return; |
1157 | } | |
1158 | ||
1159 | /* | |
1160 | * Add a recv handle node to the connection recv list with the given stream | |
1161 | * handle. A new node is allocated thus must be freed when the node is deleted | |
1162 | * from the list. | |
1163 | */ | |
58eb9381 DG |
1164 | static void queue_stream(struct relay_stream *stream, |
1165 | struct relay_connection *conn) | |
a4baae1b | 1166 | { |
58eb9381 | 1167 | assert(conn); |
2a174661 | 1168 | assert(stream); |
a4baae1b | 1169 | |
58eb9381 | 1170 | cds_list_add(&stream->recv_list, &conn->recv_head); |
a4baae1b JD |
1171 | } |
1172 | ||
b8aa1682 JD |
1173 | /* |
1174 | * relay_add_stream: allocate a new stream for a session | |
1175 | */ | |
1176 | static | |
1177 | int relay_add_stream(struct lttcomm_relayd_hdr *recv_hdr, | |
58eb9381 | 1178 | struct relay_connection *conn) |
b8aa1682 | 1179 | { |
2a174661 | 1180 | int ret, send_ret; |
58eb9381 | 1181 | struct relay_session *session = conn->session; |
b8aa1682 JD |
1182 | struct relay_stream *stream = NULL; |
1183 | struct lttcomm_relayd_status_stream reply; | |
2a174661 | 1184 | struct ctf_trace *trace; |
b8aa1682 | 1185 | |
58eb9381 | 1186 | if (!session || conn->version_check_done == 0) { |
b8aa1682 JD |
1187 | ERR("Trying to add a stream before version check"); |
1188 | ret = -1; | |
1189 | goto end_no_session; | |
1190 | } | |
1191 | ||
b8aa1682 JD |
1192 | stream = zmalloc(sizeof(struct relay_stream)); |
1193 | if (stream == NULL) { | |
1194 | PERROR("relay stream zmalloc"); | |
1195 | ret = -1; | |
1196 | goto end_no_session; | |
1197 | } | |
1198 | ||
58eb9381 | 1199 | switch (conn->minor) { |
0f907de1 | 1200 | case 1: /* LTTng sessiond 2.1 */ |
58eb9381 | 1201 | ret = cmd_recv_stream_2_1(conn, stream); |
0f907de1 JD |
1202 | break; |
1203 | case 2: /* LTTng sessiond 2.2 */ | |
1204 | default: | |
58eb9381 | 1205 | ret = cmd_recv_stream_2_2(conn, stream); |
0f907de1 JD |
1206 | break; |
1207 | } | |
1208 | if (ret < 0) { | |
1209 | goto err_free_stream; | |
1210 | } | |
1211 | ||
9d1bbf21 | 1212 | rcu_read_lock(); |
b8aa1682 | 1213 | stream->stream_handle = ++last_relay_stream_id; |
173af62f | 1214 | stream->prev_seq = -1ULL; |
2a174661 | 1215 | stream->session_id = session->id; |
1c20f0e2 | 1216 | stream->index_fd = -1; |
d3e2ba59 | 1217 | stream->read_index_fd = -1; |
bb8cf449 | 1218 | stream->ctf_stream_id = -1ULL; |
2a174661 | 1219 | lttng_ht_node_init_u64(&stream->node, stream->stream_handle); |
d3e2ba59 | 1220 | pthread_mutex_init(&stream->lock, NULL); |
b8aa1682 | 1221 | |
0f907de1 | 1222 | ret = utils_mkdir_recursive(stream->path_name, S_IRWXU | S_IRWXG); |
b8aa1682 | 1223 | if (ret < 0) { |
b8aa1682 | 1224 | ERR("relay creating output directory"); |
f0938f47 | 1225 | goto err_free_stream; |
b8aa1682 JD |
1226 | } |
1227 | ||
be96a7d1 DG |
1228 | /* |
1229 | * No need to use run_as API here because whatever we receives, the relayd | |
1230 | * uses its own credentials for the stream files. | |
1231 | */ | |
0f907de1 | 1232 | ret = utils_create_stream_file(stream->path_name, stream->channel_name, |
1c20f0e2 | 1233 | stream->tracefile_size, 0, relayd_uid, relayd_gid, NULL); |
b8aa1682 | 1234 | if (ret < 0) { |
0f907de1 | 1235 | ERR("Create output file"); |
74961765 | 1236 | goto err_free_stream; |
b8aa1682 | 1237 | } |
b8aa1682 | 1238 | stream->fd = ret; |
0f907de1 JD |
1239 | if (stream->tracefile_size) { |
1240 | DBG("Tracefile %s/%s_0 created", stream->path_name, stream->channel_name); | |
1241 | } else { | |
1242 | DBG("Tracefile %s/%s created", stream->path_name, stream->channel_name); | |
1243 | } | |
b8aa1682 | 1244 | |
2a174661 DG |
1245 | trace = ctf_trace_find_by_path(session->ctf_traces_ht, stream->path_name); |
1246 | if (!trace) { | |
1247 | trace = ctf_trace_create(stream->path_name); | |
1248 | if (!trace) { | |
d3e2ba59 JD |
1249 | ret = -1; |
1250 | goto end; | |
1251 | } | |
2a174661 DG |
1252 | ctf_trace_add(session->ctf_traces_ht, trace); |
1253 | } | |
1254 | ctf_trace_get_ref(trace); | |
1255 | ||
1256 | if (!strncmp(stream->channel_name, DEFAULT_METADATA_NAME, NAME_MAX)) { | |
1257 | stream->metadata_flag = 1; | |
1258 | /* Assign quick reference to the metadata stream in the trace. */ | |
1259 | trace->metadata_stream = stream; | |
d3e2ba59 | 1260 | } |
d3e2ba59 | 1261 | |
a4baae1b | 1262 | /* |
2a174661 DG |
1263 | * Add the stream in the recv list of the connection. Once the end stream |
1264 | * message is received, this list is emptied and streams are set with the | |
1265 | * viewer ready flag. | |
a4baae1b | 1266 | */ |
58eb9381 | 1267 | queue_stream(stream, conn); |
a4baae1b | 1268 | |
2a174661 DG |
1269 | /* |
1270 | * Both in the ctf_trace object and the global stream ht since the data | |
1271 | * side of the relayd does not have the concept of session. | |
1272 | */ | |
1273 | lttng_ht_add_unique_u64(relay_streams_ht, &stream->node); | |
1274 | cds_list_add_tail(&stream->trace_list, &trace->stream_list); | |
b8aa1682 | 1275 | |
87b576ec | 1276 | session->stream_count++; |
d3e2ba59 | 1277 | |
1c20f0e2 JD |
1278 | DBG("Relay new stream added %s with ID %" PRIu64, stream->channel_name, |
1279 | stream->stream_handle); | |
b8aa1682 JD |
1280 | |
1281 | end: | |
53efb85a | 1282 | memset(&reply, 0, sizeof(reply)); |
5af40280 | 1283 | reply.handle = htobe64(stream->stream_handle); |
b8aa1682 JD |
1284 | /* send the session id to the client or a negative return code on error */ |
1285 | if (ret < 0) { | |
f73fabfd | 1286 | reply.ret_code = htobe32(LTTNG_ERR_UNK); |
5af40280 | 1287 | /* stream was not properly added to the ht, so free it */ |
198d75ca | 1288 | stream_destroy(stream); |
b8aa1682 | 1289 | } else { |
f73fabfd | 1290 | reply.ret_code = htobe32(LTTNG_OK); |
b8aa1682 | 1291 | } |
5af40280 | 1292 | |
58eb9381 | 1293 | send_ret = conn->sock->ops->sendmsg(conn->sock, &reply, |
b8aa1682 JD |
1294 | sizeof(struct lttcomm_relayd_status_stream), 0); |
1295 | if (send_ret < 0) { | |
1296 | ERR("Relay sending stream id"); | |
4169f5ad | 1297 | ret = send_ret; |
b8aa1682 | 1298 | } |
9d1bbf21 | 1299 | rcu_read_unlock(); |
b8aa1682 JD |
1300 | |
1301 | end_no_session: | |
1302 | return ret; | |
0f907de1 JD |
1303 | |
1304 | err_free_stream: | |
198d75ca | 1305 | stream_destroy(stream); |
0f907de1 | 1306 | return ret; |
b8aa1682 JD |
1307 | } |
1308 | ||
173af62f DG |
1309 | /* |
1310 | * relay_close_stream: close a specific stream | |
1311 | */ | |
1312 | static | |
1313 | int relay_close_stream(struct lttcomm_relayd_hdr *recv_hdr, | |
58eb9381 | 1314 | struct relay_connection *conn) |
173af62f | 1315 | { |
94d49140 | 1316 | int ret, send_ret; |
58eb9381 | 1317 | struct relay_session *session = conn->session; |
173af62f DG |
1318 | struct lttcomm_relayd_close_stream stream_info; |
1319 | struct lttcomm_relayd_generic_reply reply; | |
1320 | struct relay_stream *stream; | |
173af62f DG |
1321 | |
1322 | DBG("Close stream received"); | |
1323 | ||
58eb9381 | 1324 | if (!session || conn->version_check_done == 0) { |
173af62f DG |
1325 | ERR("Trying to close a stream before version check"); |
1326 | ret = -1; | |
1327 | goto end_no_session; | |
1328 | } | |
1329 | ||
58eb9381 | 1330 | ret = conn->sock->ops->recvmsg(conn->sock, &stream_info, |
7c5aef62 | 1331 | sizeof(struct lttcomm_relayd_close_stream), 0); |
173af62f | 1332 | if (ret < sizeof(struct lttcomm_relayd_close_stream)) { |
a6cd2b97 DG |
1333 | if (ret == 0) { |
1334 | /* Orderly shutdown. Not necessary to print an error. */ | |
58eb9381 | 1335 | DBG("Socket %d did an orderly shutdown", conn->sock->fd); |
a6cd2b97 DG |
1336 | } else { |
1337 | ERR("Relay didn't receive valid add_stream struct size : %d", ret); | |
1338 | } | |
173af62f DG |
1339 | ret = -1; |
1340 | goto end_no_session; | |
1341 | } | |
1342 | ||
1343 | rcu_read_lock(); | |
2a174661 DG |
1344 | stream = stream_find_by_id(relay_streams_ht, |
1345 | be64toh(stream_info.stream_id)); | |
173af62f DG |
1346 | if (!stream) { |
1347 | ret = -1; | |
1348 | goto end_unlock; | |
1349 | } | |
1350 | ||
8e2583a4 | 1351 | stream->last_net_seq_num = be64toh(stream_info.last_net_seq_num); |
173af62f | 1352 | stream->close_flag = 1; |
87b576ec JD |
1353 | session->stream_count--; |
1354 | assert(session->stream_count >= 0); | |
173af62f | 1355 | |
2a174661 DG |
1356 | /* Check if we can close it or else the data will do it. */ |
1357 | try_close_stream(session, stream); | |
173af62f DG |
1358 | |
1359 | end_unlock: | |
1360 | rcu_read_unlock(); | |
1361 | ||
53efb85a | 1362 | memset(&reply, 0, sizeof(reply)); |
173af62f | 1363 | if (ret < 0) { |
f73fabfd | 1364 | reply.ret_code = htobe32(LTTNG_ERR_UNK); |
173af62f | 1365 | } else { |
f73fabfd | 1366 | reply.ret_code = htobe32(LTTNG_OK); |
173af62f | 1367 | } |
58eb9381 | 1368 | send_ret = conn->sock->ops->sendmsg(conn->sock, &reply, |
173af62f DG |
1369 | sizeof(struct lttcomm_relayd_generic_reply), 0); |
1370 | if (send_ret < 0) { | |
1371 | ERR("Relay sending stream id"); | |
4169f5ad | 1372 | ret = send_ret; |
173af62f DG |
1373 | } |
1374 | ||
1375 | end_no_session: | |
1376 | return ret; | |
1377 | } | |
1378 | ||
b8aa1682 JD |
1379 | /* |
1380 | * relay_unknown_command: send -1 if received unknown command | |
1381 | */ | |
1382 | static | |
58eb9381 | 1383 | void relay_unknown_command(struct relay_connection *conn) |
b8aa1682 JD |
1384 | { |
1385 | struct lttcomm_relayd_generic_reply reply; | |
1386 | int ret; | |
1387 | ||
53efb85a | 1388 | memset(&reply, 0, sizeof(reply)); |
f73fabfd | 1389 | reply.ret_code = htobe32(LTTNG_ERR_UNK); |
58eb9381 | 1390 | ret = conn->sock->ops->sendmsg(conn->sock, &reply, |
b8aa1682 JD |
1391 | sizeof(struct lttcomm_relayd_generic_reply), 0); |
1392 | if (ret < 0) { | |
1393 | ERR("Relay sending unknown command"); | |
1394 | } | |
1395 | } | |
1396 | ||
1397 | /* | |
1398 | * relay_start: send an acknowledgment to the client to tell if we are | |
1399 | * ready to receive data. We are ready if a session is established. | |
1400 | */ | |
1401 | static | |
1402 | int relay_start(struct lttcomm_relayd_hdr *recv_hdr, | |
58eb9381 | 1403 | struct relay_connection *conn) |
b8aa1682 | 1404 | { |
f73fabfd | 1405 | int ret = htobe32(LTTNG_OK); |
b8aa1682 | 1406 | struct lttcomm_relayd_generic_reply reply; |
58eb9381 | 1407 | struct relay_session *session = conn->session; |
b8aa1682 JD |
1408 | |
1409 | if (!session) { | |
1410 | DBG("Trying to start the streaming without a session established"); | |
f73fabfd | 1411 | ret = htobe32(LTTNG_ERR_UNK); |
b8aa1682 JD |
1412 | } |
1413 | ||
53efb85a | 1414 | memset(&reply, 0, sizeof(reply)); |
b8aa1682 | 1415 | reply.ret_code = ret; |
58eb9381 | 1416 | ret = conn->sock->ops->sendmsg(conn->sock, &reply, |
b8aa1682 JD |
1417 | sizeof(struct lttcomm_relayd_generic_reply), 0); |
1418 | if (ret < 0) { | |
1419 | ERR("Relay sending start ack"); | |
1420 | } | |
1421 | ||
1422 | return ret; | |
1423 | } | |
1424 | ||
1d4dfdef DG |
1425 | /* |
1426 | * Append padding to the file pointed by the file descriptor fd. | |
1427 | */ | |
1428 | static int write_padding_to_file(int fd, uint32_t size) | |
1429 | { | |
6cd525e8 | 1430 | ssize_t ret = 0; |
1d4dfdef DG |
1431 | char *zeros; |
1432 | ||
1433 | if (size == 0) { | |
1434 | goto end; | |
1435 | } | |
1436 | ||
1437 | zeros = zmalloc(size); | |
1438 | if (zeros == NULL) { | |
1439 | PERROR("zmalloc zeros for padding"); | |
1440 | ret = -1; | |
1441 | goto end; | |
1442 | } | |
1443 | ||
6cd525e8 MD |
1444 | ret = lttng_write(fd, zeros, size); |
1445 | if (ret < size) { | |
1d4dfdef DG |
1446 | PERROR("write padding to file"); |
1447 | } | |
1448 | ||
e986c7a1 DG |
1449 | free(zeros); |
1450 | ||
1d4dfdef DG |
1451 | end: |
1452 | return ret; | |
1453 | } | |
1454 | ||
b8aa1682 JD |
1455 | /* |
1456 | * relay_recv_metadata: receive the metada for the session. | |
1457 | */ | |
1458 | static | |
1459 | int relay_recv_metadata(struct lttcomm_relayd_hdr *recv_hdr, | |
58eb9381 | 1460 | struct relay_connection *conn) |
b8aa1682 | 1461 | { |
f73fabfd | 1462 | int ret = htobe32(LTTNG_OK); |
6cd525e8 | 1463 | ssize_t size_ret; |
58eb9381 | 1464 | struct relay_session *session = conn->session; |
b8aa1682 JD |
1465 | struct lttcomm_relayd_metadata_payload *metadata_struct; |
1466 | struct relay_stream *metadata_stream; | |
1467 | uint64_t data_size, payload_size; | |
2a174661 | 1468 | struct ctf_trace *ctf_trace; |
b8aa1682 JD |
1469 | |
1470 | if (!session) { | |
1471 | ERR("Metadata sent before version check"); | |
1472 | ret = -1; | |
1473 | goto end; | |
1474 | } | |
1475 | ||
f6416125 MD |
1476 | data_size = payload_size = be64toh(recv_hdr->data_size); |
1477 | if (data_size < sizeof(struct lttcomm_relayd_metadata_payload)) { | |
1478 | ERR("Incorrect data size"); | |
1479 | ret = -1; | |
1480 | goto end; | |
1481 | } | |
1482 | payload_size -= sizeof(struct lttcomm_relayd_metadata_payload); | |
1483 | ||
b8aa1682 | 1484 | if (data_buffer_size < data_size) { |
d7b3776f | 1485 | /* In case the realloc fails, we can free the memory */ |
c617c0c6 MD |
1486 | char *tmp_data_ptr; |
1487 | ||
1488 | tmp_data_ptr = realloc(data_buffer, data_size); | |
1489 | if (!tmp_data_ptr) { | |
b8aa1682 | 1490 | ERR("Allocating data buffer"); |
c617c0c6 | 1491 | free(data_buffer); |
b8aa1682 JD |
1492 | ret = -1; |
1493 | goto end; | |
1494 | } | |
c617c0c6 | 1495 | data_buffer = tmp_data_ptr; |
b8aa1682 JD |
1496 | data_buffer_size = data_size; |
1497 | } | |
1498 | memset(data_buffer, 0, data_size); | |
77c7c900 | 1499 | DBG2("Relay receiving metadata, waiting for %" PRIu64 " bytes", data_size); |
58eb9381 | 1500 | ret = conn->sock->ops->recvmsg(conn->sock, data_buffer, data_size, 0); |
b8aa1682 | 1501 | if (ret < 0 || ret != data_size) { |
a6cd2b97 DG |
1502 | if (ret == 0) { |
1503 | /* Orderly shutdown. Not necessary to print an error. */ | |
58eb9381 | 1504 | DBG("Socket %d did an orderly shutdown", conn->sock->fd); |
a6cd2b97 DG |
1505 | } else { |
1506 | ERR("Relay didn't receive the whole metadata"); | |
1507 | } | |
b8aa1682 | 1508 | ret = -1; |
b8aa1682 JD |
1509 | goto end; |
1510 | } | |
1511 | metadata_struct = (struct lttcomm_relayd_metadata_payload *) data_buffer; | |
9d1bbf21 MD |
1512 | |
1513 | rcu_read_lock(); | |
2a174661 | 1514 | metadata_stream = stream_find_by_id(relay_streams_ht, |
d3e2ba59 | 1515 | be64toh(metadata_struct->stream_id)); |
b8aa1682 JD |
1516 | if (!metadata_stream) { |
1517 | ret = -1; | |
9d1bbf21 | 1518 | goto end_unlock; |
b8aa1682 JD |
1519 | } |
1520 | ||
6cd525e8 MD |
1521 | size_ret = lttng_write(metadata_stream->fd, metadata_struct->payload, |
1522 | payload_size); | |
1523 | if (size_ret < payload_size) { | |
b8aa1682 JD |
1524 | ERR("Relay error writing metadata on file"); |
1525 | ret = -1; | |
9d1bbf21 | 1526 | goto end_unlock; |
b8aa1682 | 1527 | } |
1d4dfdef DG |
1528 | |
1529 | ret = write_padding_to_file(metadata_stream->fd, | |
1530 | be32toh(metadata_struct->padding_size)); | |
1531 | if (ret < 0) { | |
1532 | goto end_unlock; | |
1533 | } | |
2a174661 DG |
1534 | |
1535 | ctf_trace = ctf_trace_find_by_path(session->ctf_traces_ht, | |
1536 | metadata_stream->path_name); | |
1537 | assert(ctf_trace); | |
1538 | ctf_trace->metadata_received += | |
d3e2ba59 | 1539 | payload_size + be32toh(metadata_struct->padding_size); |
1d4dfdef | 1540 | |
b8aa1682 JD |
1541 | DBG2("Relay metadata written"); |
1542 | ||
9d1bbf21 | 1543 | end_unlock: |
6e3c5836 | 1544 | rcu_read_unlock(); |
b8aa1682 JD |
1545 | end: |
1546 | return ret; | |
1547 | } | |
1548 | ||
1549 | /* | |
1550 | * relay_send_version: send relayd version number | |
1551 | */ | |
1552 | static | |
1553 | int relay_send_version(struct lttcomm_relayd_hdr *recv_hdr, | |
58eb9381 | 1554 | struct relay_connection *conn) |
b8aa1682 | 1555 | { |
7f51dcba | 1556 | int ret; |
092b6259 | 1557 | struct lttcomm_relayd_version reply, msg; |
b8aa1682 | 1558 | |
58eb9381 | 1559 | assert(conn); |
c5b6f4f0 | 1560 | |
58eb9381 | 1561 | conn->version_check_done = 1; |
b8aa1682 | 1562 | |
092b6259 | 1563 | /* Get version from the other side. */ |
58eb9381 | 1564 | ret = conn->sock->ops->recvmsg(conn->sock, &msg, sizeof(msg), 0); |
092b6259 | 1565 | if (ret < 0 || ret != sizeof(msg)) { |
a6cd2b97 DG |
1566 | if (ret == 0) { |
1567 | /* Orderly shutdown. Not necessary to print an error. */ | |
58eb9381 | 1568 | DBG("Socket %d did an orderly shutdown", conn->sock->fd); |
a6cd2b97 DG |
1569 | } else { |
1570 | ERR("Relay failed to receive the version values."); | |
1571 | } | |
092b6259 | 1572 | ret = -1; |
092b6259 DG |
1573 | goto end; |
1574 | } | |
1575 | ||
53efb85a | 1576 | memset(&reply, 0, sizeof(reply)); |
d83a952c MD |
1577 | reply.major = RELAYD_VERSION_COMM_MAJOR; |
1578 | reply.minor = RELAYD_VERSION_COMM_MINOR; | |
d4519fa3 JD |
1579 | |
1580 | /* Major versions must be the same */ | |
1581 | if (reply.major != be32toh(msg.major)) { | |
6151a90f JD |
1582 | DBG("Incompatible major versions (%u vs %u), deleting session", |
1583 | reply.major, be32toh(msg.major)); | |
58eb9381 | 1584 | destroy_session(conn->session, conn->sessions_ht); |
d4519fa3 JD |
1585 | ret = 0; |
1586 | goto end; | |
1587 | } | |
1588 | ||
58eb9381 | 1589 | conn->major = reply.major; |
0f907de1 JD |
1590 | /* We adapt to the lowest compatible version */ |
1591 | if (reply.minor <= be32toh(msg.minor)) { | |
58eb9381 | 1592 | conn->minor = reply.minor; |
0f907de1 | 1593 | } else { |
58eb9381 | 1594 | conn->minor = be32toh(msg.minor); |
0f907de1 JD |
1595 | } |
1596 | ||
6151a90f JD |
1597 | reply.major = htobe32(reply.major); |
1598 | reply.minor = htobe32(reply.minor); | |
58eb9381 | 1599 | ret = conn->sock->ops->sendmsg(conn->sock, &reply, |
6151a90f JD |
1600 | sizeof(struct lttcomm_relayd_version), 0); |
1601 | if (ret < 0) { | |
1602 | ERR("Relay sending version"); | |
1603 | } | |
1604 | ||
58eb9381 DG |
1605 | DBG("Version check done using protocol %u.%u", conn->major, |
1606 | conn->minor); | |
b8aa1682 JD |
1607 | |
1608 | end: | |
1609 | return ret; | |
1610 | } | |
1611 | ||
c8f59ee5 | 1612 | /* |
6d805429 | 1613 | * Check for data pending for a given stream id from the session daemon. |
c8f59ee5 DG |
1614 | */ |
1615 | static | |
6d805429 | 1616 | int relay_data_pending(struct lttcomm_relayd_hdr *recv_hdr, |
58eb9381 | 1617 | struct relay_connection *conn) |
c8f59ee5 | 1618 | { |
58eb9381 | 1619 | struct relay_session *session = conn->session; |
6d805429 | 1620 | struct lttcomm_relayd_data_pending msg; |
c8f59ee5 DG |
1621 | struct lttcomm_relayd_generic_reply reply; |
1622 | struct relay_stream *stream; | |
1623 | int ret; | |
c8f59ee5 DG |
1624 | uint64_t last_net_seq_num, stream_id; |
1625 | ||
6d805429 | 1626 | DBG("Data pending command received"); |
c8f59ee5 | 1627 | |
58eb9381 | 1628 | if (!session || conn->version_check_done == 0) { |
c8f59ee5 DG |
1629 | ERR("Trying to check for data before version check"); |
1630 | ret = -1; | |
1631 | goto end_no_session; | |
1632 | } | |
1633 | ||
58eb9381 | 1634 | ret = conn->sock->ops->recvmsg(conn->sock, &msg, sizeof(msg), 0); |
c8f59ee5 | 1635 | if (ret < sizeof(msg)) { |
a6cd2b97 DG |
1636 | if (ret == 0) { |
1637 | /* Orderly shutdown. Not necessary to print an error. */ | |
58eb9381 | 1638 | DBG("Socket %d did an orderly shutdown", conn->sock->fd); |
a6cd2b97 DG |
1639 | } else { |
1640 | ERR("Relay didn't receive valid data_pending struct size : %d", | |
1641 | ret); | |
1642 | } | |
c8f59ee5 DG |
1643 | ret = -1; |
1644 | goto end_no_session; | |
1645 | } | |
1646 | ||
1647 | stream_id = be64toh(msg.stream_id); | |
1648 | last_net_seq_num = be64toh(msg.last_net_seq_num); | |
1649 | ||
1650 | rcu_read_lock(); | |
2a174661 | 1651 | stream = stream_find_by_id(relay_streams_ht, stream_id); |
de91f48a | 1652 | if (stream == NULL) { |
c8f59ee5 DG |
1653 | ret = -1; |
1654 | goto end_unlock; | |
1655 | } | |
1656 | ||
6d805429 | 1657 | DBG("Data pending for stream id %" PRIu64 " prev_seq %" PRIu64 |
c8f59ee5 DG |
1658 | " and last_seq %" PRIu64, stream_id, stream->prev_seq, |
1659 | last_net_seq_num); | |
1660 | ||
33832e64 | 1661 | /* Avoid wrapping issue */ |
39df6d9f | 1662 | if (((int64_t) (stream->prev_seq - last_net_seq_num)) >= 0) { |
6d805429 | 1663 | /* Data has in fact been written and is NOT pending */ |
c8f59ee5 | 1664 | ret = 0; |
6d805429 DG |
1665 | } else { |
1666 | /* Data still being streamed thus pending */ | |
1667 | ret = 1; | |
c8f59ee5 DG |
1668 | } |
1669 | ||
f7079f67 DG |
1670 | /* Pending check is now done. */ |
1671 | stream->data_pending_check_done = 1; | |
1672 | ||
c8f59ee5 DG |
1673 | end_unlock: |
1674 | rcu_read_unlock(); | |
1675 | ||
53efb85a | 1676 | memset(&reply, 0, sizeof(reply)); |
c8f59ee5 | 1677 | reply.ret_code = htobe32(ret); |
58eb9381 | 1678 | ret = conn->sock->ops->sendmsg(conn->sock, &reply, sizeof(reply), 0); |
c8f59ee5 | 1679 | if (ret < 0) { |
6d805429 | 1680 | ERR("Relay data pending ret code failed"); |
c8f59ee5 DG |
1681 | } |
1682 | ||
1683 | end_no_session: | |
1684 | return ret; | |
1685 | } | |
1686 | ||
1687 | /* | |
1688 | * Wait for the control socket to reach a quiescent state. | |
1689 | * | |
1690 | * Note that for now, when receiving this command from the session daemon, this | |
1691 | * means that every subsequent commands or data received on the control socket | |
1692 | * has been handled. So, this is why we simply return OK here. | |
1693 | */ | |
1694 | static | |
1695 | int relay_quiescent_control(struct lttcomm_relayd_hdr *recv_hdr, | |
58eb9381 | 1696 | struct relay_connection *conn) |
c8f59ee5 DG |
1697 | { |
1698 | int ret; | |
ad7051c0 DG |
1699 | uint64_t stream_id; |
1700 | struct relay_stream *stream; | |
1701 | struct lttng_ht_iter iter; | |
1702 | struct lttcomm_relayd_quiescent_control msg; | |
c8f59ee5 DG |
1703 | struct lttcomm_relayd_generic_reply reply; |
1704 | ||
1705 | DBG("Checking quiescent state on control socket"); | |
1706 | ||
58eb9381 | 1707 | if (!conn->session || conn->version_check_done == 0) { |
ad7051c0 DG |
1708 | ERR("Trying to check for data before version check"); |
1709 | ret = -1; | |
1710 | goto end_no_session; | |
1711 | } | |
1712 | ||
58eb9381 | 1713 | ret = conn->sock->ops->recvmsg(conn->sock, &msg, sizeof(msg), 0); |
ad7051c0 | 1714 | if (ret < sizeof(msg)) { |
a6cd2b97 DG |
1715 | if (ret == 0) { |
1716 | /* Orderly shutdown. Not necessary to print an error. */ | |
58eb9381 | 1717 | DBG("Socket %d did an orderly shutdown", conn->sock->fd); |
a6cd2b97 DG |
1718 | } else { |
1719 | ERR("Relay didn't receive valid begin data_pending struct size: %d", | |
1720 | ret); | |
1721 | } | |
ad7051c0 DG |
1722 | ret = -1; |
1723 | goto end_no_session; | |
1724 | } | |
1725 | ||
1726 | stream_id = be64toh(msg.stream_id); | |
1727 | ||
1728 | rcu_read_lock(); | |
d3e2ba59 | 1729 | cds_lfht_for_each_entry(relay_streams_ht->ht, &iter.iter, stream, |
2a174661 | 1730 | node.node) { |
ad7051c0 DG |
1731 | if (stream->stream_handle == stream_id) { |
1732 | stream->data_pending_check_done = 1; | |
1733 | DBG("Relay quiescent control pending flag set to %" PRIu64, | |
1734 | stream_id); | |
1735 | break; | |
1736 | } | |
1737 | } | |
1738 | rcu_read_unlock(); | |
1739 | ||
53efb85a | 1740 | memset(&reply, 0, sizeof(reply)); |
c8f59ee5 | 1741 | reply.ret_code = htobe32(LTTNG_OK); |
58eb9381 | 1742 | ret = conn->sock->ops->sendmsg(conn->sock, &reply, sizeof(reply), 0); |
c8f59ee5 | 1743 | if (ret < 0) { |
6d805429 | 1744 | ERR("Relay data quiescent control ret code failed"); |
c8f59ee5 DG |
1745 | } |
1746 | ||
ad7051c0 | 1747 | end_no_session: |
c8f59ee5 DG |
1748 | return ret; |
1749 | } | |
1750 | ||
f7079f67 DG |
1751 | /* |
1752 | * Initialize a data pending command. This means that a client is about to ask | |
1753 | * for data pending for each stream he/she holds. Simply iterate over all | |
1754 | * streams of a session and set the data_pending_check_done flag. | |
1755 | * | |
1756 | * This command returns to the client a LTTNG_OK code. | |
1757 | */ | |
1758 | static | |
1759 | int relay_begin_data_pending(struct lttcomm_relayd_hdr *recv_hdr, | |
58eb9381 | 1760 | struct relay_connection *conn) |
f7079f67 DG |
1761 | { |
1762 | int ret; | |
1763 | struct lttng_ht_iter iter; | |
1764 | struct lttcomm_relayd_begin_data_pending msg; | |
1765 | struct lttcomm_relayd_generic_reply reply; | |
1766 | struct relay_stream *stream; | |
1767 | uint64_t session_id; | |
1768 | ||
1769 | assert(recv_hdr); | |
58eb9381 | 1770 | assert(conn); |
f7079f67 DG |
1771 | |
1772 | DBG("Init streams for data pending"); | |
1773 | ||
58eb9381 | 1774 | if (!conn->session || conn->version_check_done == 0) { |
f7079f67 DG |
1775 | ERR("Trying to check for data before version check"); |
1776 | ret = -1; | |
1777 | goto end_no_session; | |
1778 | } | |
1779 | ||
58eb9381 | 1780 | ret = conn->sock->ops->recvmsg(conn->sock, &msg, sizeof(msg), 0); |
f7079f67 | 1781 | if (ret < sizeof(msg)) { |
a6cd2b97 DG |
1782 | if (ret == 0) { |
1783 | /* Orderly shutdown. Not necessary to print an error. */ | |
58eb9381 | 1784 | DBG("Socket %d did an orderly shutdown", conn->sock->fd); |
a6cd2b97 DG |
1785 | } else { |
1786 | ERR("Relay didn't receive valid begin data_pending struct size: %d", | |
1787 | ret); | |
1788 | } | |
f7079f67 DG |
1789 | ret = -1; |
1790 | goto end_no_session; | |
1791 | } | |
1792 | ||
1793 | session_id = be64toh(msg.session_id); | |
1794 | ||
1795 | /* | |
1796 | * Iterate over all streams to set the begin data pending flag. For now, the | |
1797 | * streams are indexed by stream handle so we have to iterate over all | |
1798 | * streams to find the one associated with the right session_id. | |
1799 | */ | |
1800 | rcu_read_lock(); | |
d3e2ba59 | 1801 | cds_lfht_for_each_entry(relay_streams_ht->ht, &iter.iter, stream, |
2a174661 DG |
1802 | node.node) { |
1803 | if (stream->session_id == session_id) { | |
f7079f67 DG |
1804 | stream->data_pending_check_done = 0; |
1805 | DBG("Set begin data pending flag to stream %" PRIu64, | |
1806 | stream->stream_handle); | |
1807 | } | |
1808 | } | |
1809 | rcu_read_unlock(); | |
1810 | ||
53efb85a | 1811 | memset(&reply, 0, sizeof(reply)); |
f7079f67 DG |
1812 | /* All good, send back reply. */ |
1813 | reply.ret_code = htobe32(LTTNG_OK); | |
1814 | ||
58eb9381 | 1815 | ret = conn->sock->ops->sendmsg(conn->sock, &reply, sizeof(reply), 0); |
f7079f67 DG |
1816 | if (ret < 0) { |
1817 | ERR("Relay begin data pending send reply failed"); | |
1818 | } | |
1819 | ||
1820 | end_no_session: | |
1821 | return ret; | |
1822 | } | |
1823 | ||
1824 | /* | |
1825 | * End data pending command. This will check, for a given session id, if each | |
1826 | * stream associated with it has its data_pending_check_done flag set. If not, | |
1827 | * this means that the client lost track of the stream but the data is still | |
1828 | * being streamed on our side. In this case, we inform the client that data is | |
1829 | * inflight. | |
1830 | * | |
1831 | * Return to the client if there is data in flight or not with a ret_code. | |
1832 | */ | |
1833 | static | |
1834 | int relay_end_data_pending(struct lttcomm_relayd_hdr *recv_hdr, | |
58eb9381 | 1835 | struct relay_connection *conn) |
f7079f67 DG |
1836 | { |
1837 | int ret; | |
1838 | struct lttng_ht_iter iter; | |
1839 | struct lttcomm_relayd_end_data_pending msg; | |
1840 | struct lttcomm_relayd_generic_reply reply; | |
1841 | struct relay_stream *stream; | |
1842 | uint64_t session_id; | |
1843 | uint32_t is_data_inflight = 0; | |
1844 | ||
1845 | assert(recv_hdr); | |
58eb9381 | 1846 | assert(conn); |
f7079f67 DG |
1847 | |
1848 | DBG("End data pending command"); | |
1849 | ||
58eb9381 | 1850 | if (!conn->session || conn->version_check_done == 0) { |
f7079f67 DG |
1851 | ERR("Trying to check for data before version check"); |
1852 | ret = -1; | |
1853 | goto end_no_session; | |
1854 | } | |
1855 | ||
58eb9381 | 1856 | ret = conn->sock->ops->recvmsg(conn->sock, &msg, sizeof(msg), 0); |
f7079f67 | 1857 | if (ret < sizeof(msg)) { |
a6cd2b97 DG |
1858 | if (ret == 0) { |
1859 | /* Orderly shutdown. Not necessary to print an error. */ | |
58eb9381 | 1860 | DBG("Socket %d did an orderly shutdown", conn->sock->fd); |
a6cd2b97 DG |
1861 | } else { |
1862 | ERR("Relay didn't receive valid end data_pending struct size: %d", | |
1863 | ret); | |
1864 | } | |
f7079f67 DG |
1865 | ret = -1; |
1866 | goto end_no_session; | |
1867 | } | |
1868 | ||
1869 | session_id = be64toh(msg.session_id); | |
1870 | ||
1871 | /* Iterate over all streams to see if the begin data pending flag is set. */ | |
1872 | rcu_read_lock(); | |
d3e2ba59 | 1873 | cds_lfht_for_each_entry(relay_streams_ht->ht, &iter.iter, stream, |
2a174661 DG |
1874 | node.node) { |
1875 | if (stream->session_id == session_id && | |
f94b19e6 | 1876 | !stream->data_pending_check_done && !stream->terminated_flag) { |
f7079f67 DG |
1877 | is_data_inflight = 1; |
1878 | DBG("Data is still in flight for stream %" PRIu64, | |
1879 | stream->stream_handle); | |
1880 | break; | |
1881 | } | |
1882 | } | |
1883 | rcu_read_unlock(); | |
1884 | ||
53efb85a | 1885 | memset(&reply, 0, sizeof(reply)); |
f7079f67 DG |
1886 | /* All good, send back reply. */ |
1887 | reply.ret_code = htobe32(is_data_inflight); | |
1888 | ||
58eb9381 | 1889 | ret = conn->sock->ops->sendmsg(conn->sock, &reply, sizeof(reply), 0); |
f7079f67 DG |
1890 | if (ret < 0) { |
1891 | ERR("Relay end data pending send reply failed"); | |
1892 | } | |
1893 | ||
1894 | end_no_session: | |
1895 | return ret; | |
1896 | } | |
1897 | ||
1c20f0e2 JD |
1898 | /* |
1899 | * Receive an index for a specific stream. | |
1900 | * | |
1901 | * Return 0 on success else a negative value. | |
1902 | */ | |
1903 | static | |
1904 | int relay_recv_index(struct lttcomm_relayd_hdr *recv_hdr, | |
58eb9381 | 1905 | struct relay_connection *conn) |
1c20f0e2 JD |
1906 | { |
1907 | int ret, send_ret, index_created = 0; | |
58eb9381 | 1908 | struct relay_session *session = conn->session; |
1c20f0e2 JD |
1909 | struct lttcomm_relayd_index index_info; |
1910 | struct relay_index *index, *wr_index = NULL; | |
1911 | struct lttcomm_relayd_generic_reply reply; | |
1912 | struct relay_stream *stream; | |
1913 | uint64_t net_seq_num; | |
1914 | ||
58eb9381 | 1915 | assert(conn); |
1c20f0e2 JD |
1916 | |
1917 | DBG("Relay receiving index"); | |
1918 | ||
58eb9381 | 1919 | if (!session || conn->version_check_done == 0) { |
1c20f0e2 JD |
1920 | ERR("Trying to close a stream before version check"); |
1921 | ret = -1; | |
1922 | goto end_no_session; | |
1923 | } | |
1924 | ||
58eb9381 | 1925 | ret = conn->sock->ops->recvmsg(conn->sock, &index_info, |
1c20f0e2 JD |
1926 | sizeof(index_info), 0); |
1927 | if (ret < sizeof(index_info)) { | |
1928 | if (ret == 0) { | |
1929 | /* Orderly shutdown. Not necessary to print an error. */ | |
58eb9381 | 1930 | DBG("Socket %d did an orderly shutdown", conn->sock->fd); |
1c20f0e2 JD |
1931 | } else { |
1932 | ERR("Relay didn't receive valid index struct size : %d", ret); | |
1933 | } | |
1934 | ret = -1; | |
1935 | goto end_no_session; | |
1936 | } | |
1937 | ||
1938 | net_seq_num = be64toh(index_info.net_seq_num); | |
1939 | ||
1940 | rcu_read_lock(); | |
2a174661 DG |
1941 | stream = stream_find_by_id(relay_streams_ht, |
1942 | be64toh(index_info.relay_stream_id)); | |
1c20f0e2 JD |
1943 | if (!stream) { |
1944 | ret = -1; | |
1945 | goto end_rcu_unlock; | |
1946 | } | |
1947 | ||
d3e2ba59 JD |
1948 | /* Live beacon handling */ |
1949 | if (index_info.packet_size == 0) { | |
1950 | DBG("Received live beacon for stream %" PRIu64, stream->stream_handle); | |
1951 | ||
1952 | /* | |
6f6e4c37 JD |
1953 | * Only flag a stream inactive when it has already received data |
1954 | * and no indexes are in flight. | |
d3e2ba59 | 1955 | */ |
6f6e4c37 | 1956 | if (stream->total_index_received > 0 && stream->indexes_in_flight == 0) { |
d3e2ba59 JD |
1957 | stream->beacon_ts_end = be64toh(index_info.timestamp_end); |
1958 | } | |
1959 | ret = 0; | |
1960 | goto end_rcu_unlock; | |
1961 | } else { | |
1962 | stream->beacon_ts_end = -1ULL; | |
1963 | } | |
1964 | ||
0a6518b0 | 1965 | index = relay_index_find(stream->stream_handle, net_seq_num); |
1c20f0e2 JD |
1966 | if (!index) { |
1967 | /* A successful creation will add the object to the HT. */ | |
1968 | index = relay_index_create(stream->stream_handle, net_seq_num); | |
1969 | if (!index) { | |
1970 | goto end_rcu_unlock; | |
1971 | } | |
1972 | index_created = 1; | |
6f6e4c37 | 1973 | stream->indexes_in_flight++; |
1c20f0e2 JD |
1974 | } |
1975 | ||
1976 | copy_index_control_data(index, &index_info); | |
bb8cf449 JD |
1977 | if (stream->ctf_stream_id == -1ULL) { |
1978 | stream->ctf_stream_id = be64toh(index_info.stream_id); | |
1979 | } | |
1c20f0e2 JD |
1980 | |
1981 | if (index_created) { | |
1982 | /* | |
1983 | * Try to add the relay index object to the hash table. If an object | |
1984 | * already exist, destroy back the index created, set the data in this | |
1985 | * object and write it on disk. | |
1986 | */ | |
0a6518b0 | 1987 | relay_index_add(index, &wr_index); |
1c20f0e2 JD |
1988 | if (wr_index) { |
1989 | copy_index_control_data(wr_index, &index_info); | |
1990 | free(index); | |
1991 | } | |
1992 | } else { | |
1993 | /* The index already exists so write it on disk. */ | |
1994 | wr_index = index; | |
1995 | } | |
1996 | ||
1997 | /* Do we have a writable ready index to write on disk. */ | |
1998 | if (wr_index) { | |
0a6518b0 | 1999 | ret = relay_index_write(wr_index->fd, wr_index); |
1c20f0e2 JD |
2000 | if (ret < 0) { |
2001 | goto end_rcu_unlock; | |
2002 | } | |
d3e2ba59 | 2003 | stream->total_index_received++; |
6f6e4c37 JD |
2004 | stream->indexes_in_flight--; |
2005 | assert(stream->indexes_in_flight >= 0); | |
1c20f0e2 JD |
2006 | } |
2007 | ||
2008 | end_rcu_unlock: | |
2009 | rcu_read_unlock(); | |
2010 | ||
53efb85a | 2011 | memset(&reply, 0, sizeof(reply)); |
1c20f0e2 JD |
2012 | if (ret < 0) { |
2013 | reply.ret_code = htobe32(LTTNG_ERR_UNK); | |
2014 | } else { | |
2015 | reply.ret_code = htobe32(LTTNG_OK); | |
2016 | } | |
58eb9381 | 2017 | send_ret = conn->sock->ops->sendmsg(conn->sock, &reply, sizeof(reply), 0); |
1c20f0e2 JD |
2018 | if (send_ret < 0) { |
2019 | ERR("Relay sending close index id reply"); | |
2020 | ret = send_ret; | |
2021 | } | |
2022 | ||
2023 | end_no_session: | |
2024 | return ret; | |
2025 | } | |
2026 | ||
a4baae1b JD |
2027 | /* |
2028 | * Receive the streams_sent message. | |
2029 | * | |
2030 | * Return 0 on success else a negative value. | |
2031 | */ | |
2032 | static | |
2033 | int relay_streams_sent(struct lttcomm_relayd_hdr *recv_hdr, | |
58eb9381 | 2034 | struct relay_connection *conn) |
a4baae1b JD |
2035 | { |
2036 | int ret, send_ret; | |
2037 | struct lttcomm_relayd_generic_reply reply; | |
2038 | ||
58eb9381 | 2039 | assert(conn); |
a4baae1b JD |
2040 | |
2041 | DBG("Relay receiving streams_sent"); | |
2042 | ||
58eb9381 | 2043 | if (!conn->session || conn->version_check_done == 0) { |
a4baae1b JD |
2044 | ERR("Trying to close a stream before version check"); |
2045 | ret = -1; | |
2046 | goto end_no_session; | |
2047 | } | |
2048 | ||
2049 | /* | |
2050 | * Flag every pending stream in the connection recv list that they are | |
2051 | * ready to be used by the viewer. | |
2052 | */ | |
58eb9381 | 2053 | set_viewer_ready_flag(conn); |
a4baae1b | 2054 | |
4a9daf17 JD |
2055 | /* |
2056 | * Inform the viewer that there are new streams in the session. | |
2057 | */ | |
c5141fe5 JD |
2058 | if (conn->session->viewer_refcount) { |
2059 | uatomic_set(&conn->session->new_streams, 1); | |
2060 | } | |
4a9daf17 | 2061 | |
53efb85a | 2062 | memset(&reply, 0, sizeof(reply)); |
a4baae1b | 2063 | reply.ret_code = htobe32(LTTNG_OK); |
58eb9381 | 2064 | send_ret = conn->sock->ops->sendmsg(conn->sock, &reply, sizeof(reply), 0); |
a4baae1b JD |
2065 | if (send_ret < 0) { |
2066 | ERR("Relay sending sent_stream reply"); | |
2067 | ret = send_ret; | |
2068 | } else { | |
2069 | /* Success. */ | |
2070 | ret = 0; | |
2071 | } | |
2072 | ||
2073 | end_no_session: | |
2074 | return ret; | |
2075 | } | |
2076 | ||
b8aa1682 | 2077 | /* |
d3e2ba59 | 2078 | * Process the commands received on the control socket |
b8aa1682 JD |
2079 | */ |
2080 | static | |
2081 | int relay_process_control(struct lttcomm_relayd_hdr *recv_hdr, | |
58eb9381 | 2082 | struct relay_connection *conn) |
b8aa1682 JD |
2083 | { |
2084 | int ret = 0; | |
2085 | ||
2086 | switch (be32toh(recv_hdr->cmd)) { | |
b8aa1682 | 2087 | case RELAYD_CREATE_SESSION: |
58eb9381 | 2088 | ret = relay_create_session(recv_hdr, conn); |
b8aa1682 | 2089 | break; |
b8aa1682 | 2090 | case RELAYD_ADD_STREAM: |
58eb9381 | 2091 | ret = relay_add_stream(recv_hdr, conn); |
b8aa1682 JD |
2092 | break; |
2093 | case RELAYD_START_DATA: | |
58eb9381 | 2094 | ret = relay_start(recv_hdr, conn); |
b8aa1682 JD |
2095 | break; |
2096 | case RELAYD_SEND_METADATA: | |
58eb9381 | 2097 | ret = relay_recv_metadata(recv_hdr, conn); |
b8aa1682 JD |
2098 | break; |
2099 | case RELAYD_VERSION: | |
58eb9381 | 2100 | ret = relay_send_version(recv_hdr, conn); |
b8aa1682 | 2101 | break; |
173af62f | 2102 | case RELAYD_CLOSE_STREAM: |
58eb9381 | 2103 | ret = relay_close_stream(recv_hdr, conn); |
173af62f | 2104 | break; |
6d805429 | 2105 | case RELAYD_DATA_PENDING: |
58eb9381 | 2106 | ret = relay_data_pending(recv_hdr, conn); |
c8f59ee5 DG |
2107 | break; |
2108 | case RELAYD_QUIESCENT_CONTROL: | |
58eb9381 | 2109 | ret = relay_quiescent_control(recv_hdr, conn); |
c8f59ee5 | 2110 | break; |
f7079f67 | 2111 | case RELAYD_BEGIN_DATA_PENDING: |
58eb9381 | 2112 | ret = relay_begin_data_pending(recv_hdr, conn); |
f7079f67 DG |
2113 | break; |
2114 | case RELAYD_END_DATA_PENDING: | |
58eb9381 | 2115 | ret = relay_end_data_pending(recv_hdr, conn); |
f7079f67 | 2116 | break; |
1c20f0e2 | 2117 | case RELAYD_SEND_INDEX: |
58eb9381 | 2118 | ret = relay_recv_index(recv_hdr, conn); |
1c20f0e2 | 2119 | break; |
a4baae1b | 2120 | case RELAYD_STREAMS_SENT: |
58eb9381 | 2121 | ret = relay_streams_sent(recv_hdr, conn); |
a4baae1b | 2122 | break; |
b8aa1682 JD |
2123 | case RELAYD_UPDATE_SYNC_INFO: |
2124 | default: | |
2125 | ERR("Received unknown command (%u)", be32toh(recv_hdr->cmd)); | |
58eb9381 | 2126 | relay_unknown_command(conn); |
b8aa1682 JD |
2127 | ret = -1; |
2128 | goto end; | |
2129 | } | |
2130 | ||
2131 | end: | |
2132 | return ret; | |
2133 | } | |
2134 | ||
7d2f7452 DG |
2135 | /* |
2136 | * Handle index for a data stream. | |
2137 | * | |
2138 | * RCU read side lock MUST be acquired. | |
2139 | * | |
2140 | * Return 0 on success else a negative value. | |
2141 | */ | |
2142 | static int handle_index_data(struct relay_stream *stream, uint64_t net_seq_num, | |
2143 | int rotate_index) | |
2144 | { | |
2145 | int ret = 0, index_created = 0; | |
2146 | uint64_t stream_id, data_offset; | |
2147 | struct relay_index *index, *wr_index = NULL; | |
2148 | ||
2149 | assert(stream); | |
2150 | ||
2151 | stream_id = stream->stream_handle; | |
2152 | /* Get data offset because we are about to update the index. */ | |
2153 | data_offset = htobe64(stream->tracefile_size_current); | |
2154 | ||
2155 | /* | |
2156 | * Lookup for an existing index for that stream id/sequence number. If on | |
2157 | * exists, the control thread already received the data for it thus we need | |
2158 | * to write it on disk. | |
2159 | */ | |
2160 | index = relay_index_find(stream_id, net_seq_num); | |
2161 | if (!index) { | |
2162 | /* A successful creation will add the object to the HT. */ | |
2163 | index = relay_index_create(stream_id, net_seq_num); | |
2164 | if (!index) { | |
2165 | ret = -1; | |
2166 | goto error; | |
2167 | } | |
2168 | index_created = 1; | |
6f6e4c37 | 2169 | stream->indexes_in_flight++; |
7d2f7452 DG |
2170 | } |
2171 | ||
2172 | if (rotate_index || stream->index_fd < 0) { | |
2173 | index->to_close_fd = stream->index_fd; | |
2174 | ret = index_create_file(stream->path_name, stream->channel_name, | |
2175 | relayd_uid, relayd_gid, stream->tracefile_size, | |
2176 | stream->tracefile_count_current); | |
2177 | if (ret < 0) { | |
2178 | /* This will close the stream's index fd if one. */ | |
2179 | relay_index_free_safe(index); | |
2180 | goto error; | |
2181 | } | |
2182 | stream->index_fd = ret; | |
2183 | } | |
2184 | index->fd = stream->index_fd; | |
2185 | index->index_data.offset = data_offset; | |
2186 | ||
2187 | if (index_created) { | |
2188 | /* | |
2189 | * Try to add the relay index object to the hash table. If an object | |
2190 | * already exist, destroy back the index created and set the data. | |
2191 | */ | |
2192 | relay_index_add(index, &wr_index); | |
2193 | if (wr_index) { | |
2194 | /* Copy back data from the created index. */ | |
2195 | wr_index->fd = index->fd; | |
2196 | wr_index->to_close_fd = index->to_close_fd; | |
2197 | wr_index->index_data.offset = data_offset; | |
2198 | free(index); | |
2199 | } | |
2200 | } else { | |
2201 | /* The index already exists so write it on disk. */ | |
2202 | wr_index = index; | |
2203 | } | |
2204 | ||
2205 | /* Do we have a writable ready index to write on disk. */ | |
2206 | if (wr_index) { | |
2207 | ret = relay_index_write(wr_index->fd, wr_index); | |
2208 | if (ret < 0) { | |
2209 | goto error; | |
2210 | } | |
2211 | stream->total_index_received++; | |
6f6e4c37 JD |
2212 | stream->indexes_in_flight--; |
2213 | assert(stream->indexes_in_flight >= 0); | |
7d2f7452 DG |
2214 | } |
2215 | ||
2216 | error: | |
2217 | return ret; | |
2218 | } | |
2219 | ||
b8aa1682 JD |
2220 | /* |
2221 | * relay_process_data: Process the data received on the data socket | |
2222 | */ | |
2223 | static | |
58eb9381 | 2224 | int relay_process_data(struct relay_connection *conn) |
b8aa1682 | 2225 | { |
7d2f7452 | 2226 | int ret = 0, rotate_index = 0; |
6cd525e8 | 2227 | ssize_t size_ret; |
b8aa1682 JD |
2228 | struct relay_stream *stream; |
2229 | struct lttcomm_relayd_data_hdr data_hdr; | |
7d2f7452 | 2230 | uint64_t stream_id; |
173af62f | 2231 | uint64_t net_seq_num; |
b8aa1682 | 2232 | uint32_t data_size; |
2a174661 | 2233 | struct relay_session *session; |
b8aa1682 | 2234 | |
58eb9381 DG |
2235 | assert(conn); |
2236 | ||
2237 | ret = conn->sock->ops->recvmsg(conn->sock, &data_hdr, | |
7c5aef62 | 2238 | sizeof(struct lttcomm_relayd_data_hdr), 0); |
b8aa1682 | 2239 | if (ret <= 0) { |
a6cd2b97 DG |
2240 | if (ret == 0) { |
2241 | /* Orderly shutdown. Not necessary to print an error. */ | |
58eb9381 | 2242 | DBG("Socket %d did an orderly shutdown", conn->sock->fd); |
a6cd2b97 | 2243 | } else { |
58eb9381 | 2244 | ERR("Unable to receive data header on sock %d", conn->sock->fd); |
a6cd2b97 | 2245 | } |
b8aa1682 JD |
2246 | ret = -1; |
2247 | goto end; | |
2248 | } | |
2249 | ||
2250 | stream_id = be64toh(data_hdr.stream_id); | |
9d1bbf21 MD |
2251 | |
2252 | rcu_read_lock(); | |
2a174661 | 2253 | stream = stream_find_by_id(relay_streams_ht, stream_id); |
b8aa1682 JD |
2254 | if (!stream) { |
2255 | ret = -1; | |
1c20f0e2 | 2256 | goto end_rcu_unlock; |
b8aa1682 JD |
2257 | } |
2258 | ||
58eb9381 | 2259 | session = session_find_by_id(conn->sessions_ht, stream->session_id); |
2a174661 DG |
2260 | assert(session); |
2261 | ||
b8aa1682 JD |
2262 | data_size = be32toh(data_hdr.data_size); |
2263 | if (data_buffer_size < data_size) { | |
c617c0c6 MD |
2264 | char *tmp_data_ptr; |
2265 | ||
2266 | tmp_data_ptr = realloc(data_buffer, data_size); | |
2267 | if (!tmp_data_ptr) { | |
b8aa1682 | 2268 | ERR("Allocating data buffer"); |
c617c0c6 | 2269 | free(data_buffer); |
b8aa1682 | 2270 | ret = -1; |
1c20f0e2 | 2271 | goto end_rcu_unlock; |
b8aa1682 | 2272 | } |
c617c0c6 | 2273 | data_buffer = tmp_data_ptr; |
b8aa1682 JD |
2274 | data_buffer_size = data_size; |
2275 | } | |
2276 | memset(data_buffer, 0, data_size); | |
2277 | ||
173af62f DG |
2278 | net_seq_num = be64toh(data_hdr.net_seq_num); |
2279 | ||
77c7c900 | 2280 | DBG3("Receiving data of size %u for stream id %" PRIu64 " seqnum %" PRIu64, |
173af62f | 2281 | data_size, stream_id, net_seq_num); |
58eb9381 | 2282 | ret = conn->sock->ops->recvmsg(conn->sock, data_buffer, data_size, 0); |
b8aa1682 | 2283 | if (ret <= 0) { |
a6cd2b97 DG |
2284 | if (ret == 0) { |
2285 | /* Orderly shutdown. Not necessary to print an error. */ | |
58eb9381 | 2286 | DBG("Socket %d did an orderly shutdown", conn->sock->fd); |
a6cd2b97 | 2287 | } |
b8aa1682 | 2288 | ret = -1; |
1c20f0e2 | 2289 | goto end_rcu_unlock; |
b8aa1682 JD |
2290 | } |
2291 | ||
1c20f0e2 | 2292 | /* Check if a rotation is needed. */ |
0f907de1 JD |
2293 | if (stream->tracefile_size > 0 && |
2294 | (stream->tracefile_size_current + data_size) > | |
2295 | stream->tracefile_size) { | |
6b6b9a5a JD |
2296 | struct relay_viewer_stream *vstream; |
2297 | uint64_t new_id; | |
2298 | ||
2299 | new_id = (stream->tracefile_count_current + 1) % | |
2300 | stream->tracefile_count; | |
2301 | /* | |
2302 | * When we wrap-around back to 0, we start overwriting old | |
2303 | * trace data. | |
2304 | */ | |
2305 | if (!stream->tracefile_overwrite && new_id == 0) { | |
2306 | stream->tracefile_overwrite = 1; | |
2307 | } | |
2308 | pthread_mutex_lock(&stream->viewer_stream_rotation_lock); | |
2309 | if (stream->tracefile_overwrite) { | |
2310 | stream->oldest_tracefile_id = | |
2311 | (stream->oldest_tracefile_id + 1) % | |
2312 | stream->tracefile_count; | |
2313 | } | |
2f8f53af | 2314 | vstream = viewer_stream_find_by_id(stream->stream_handle); |
6b6b9a5a JD |
2315 | if (vstream) { |
2316 | /* | |
2317 | * The viewer is reading a file about to be | |
2318 | * overwritten. Close the FDs it is | |
2319 | * currently using and let it handle the fault. | |
2320 | */ | |
2321 | if (vstream->tracefile_count_current == new_id) { | |
cef0f7d5 | 2322 | pthread_mutex_lock(&vstream->overwrite_lock); |
6b6b9a5a | 2323 | vstream->abort_flag = 1; |
cef0f7d5 | 2324 | pthread_mutex_unlock(&vstream->overwrite_lock); |
2e181755 | 2325 | DBG("Streaming side setting abort_flag on stream %s_%" PRIu64 "\n", |
6b6b9a5a JD |
2326 | stream->channel_name, new_id); |
2327 | } else if (vstream->tracefile_count_current == | |
2328 | stream->tracefile_count_current) { | |
2329 | /* | |
2330 | * The reader and writer were in the | |
2331 | * same trace file, inform the viewer | |
2332 | * that no new index will ever be added | |
2333 | * to this file. | |
2334 | */ | |
2335 | vstream->close_write_flag = 1; | |
2336 | } | |
2337 | } | |
1c20f0e2 JD |
2338 | ret = utils_rotate_stream_file(stream->path_name, stream->channel_name, |
2339 | stream->tracefile_size, stream->tracefile_count, | |
2340 | relayd_uid, relayd_gid, stream->fd, | |
2341 | &(stream->tracefile_count_current), &stream->fd); | |
6b6b9a5a | 2342 | pthread_mutex_unlock(&stream->viewer_stream_rotation_lock); |
0f907de1 | 2343 | if (ret < 0) { |
1c20f0e2 JD |
2344 | ERR("Rotating stream output file"); |
2345 | goto end_rcu_unlock; | |
0f907de1 | 2346 | } |
a6976990 DG |
2347 | /* Reset current size because we just perform a stream rotation. */ |
2348 | stream->tracefile_size_current = 0; | |
1c20f0e2 JD |
2349 | rotate_index = 1; |
2350 | } | |
2351 | ||
1c20f0e2 | 2352 | /* |
7d2f7452 DG |
2353 | * Index are handled in protocol version 2.4 and above. Also, snapshot and |
2354 | * index are NOT supported. | |
1c20f0e2 | 2355 | */ |
2a174661 | 2356 | if (session->minor >= 4 && !session->snapshot) { |
7d2f7452 | 2357 | ret = handle_index_data(stream, net_seq_num, rotate_index); |
1c20f0e2 | 2358 | if (ret < 0) { |
1c20f0e2 JD |
2359 | goto end_rcu_unlock; |
2360 | } | |
1c20f0e2 JD |
2361 | } |
2362 | ||
7d2f7452 | 2363 | /* Write data to stream output fd. */ |
6cd525e8 MD |
2364 | size_ret = lttng_write(stream->fd, data_buffer, data_size); |
2365 | if (size_ret < data_size) { | |
b8aa1682 JD |
2366 | ERR("Relay error writing data to file"); |
2367 | ret = -1; | |
1c20f0e2 | 2368 | goto end_rcu_unlock; |
b8aa1682 | 2369 | } |
1d4dfdef | 2370 | |
5ab7344e JD |
2371 | DBG2("Relay wrote %d bytes to tracefile for stream id %" PRIu64, |
2372 | ret, stream->stream_handle); | |
2373 | ||
1d4dfdef DG |
2374 | ret = write_padding_to_file(stream->fd, be32toh(data_hdr.padding_size)); |
2375 | if (ret < 0) { | |
1c20f0e2 | 2376 | goto end_rcu_unlock; |
1d4dfdef | 2377 | } |
1c20f0e2 | 2378 | stream->tracefile_size_current += data_size + be32toh(data_hdr.padding_size); |
1d4dfdef | 2379 | |
173af62f DG |
2380 | stream->prev_seq = net_seq_num; |
2381 | ||
2a174661 | 2382 | try_close_stream(session, stream); |
173af62f | 2383 | |
1c20f0e2 | 2384 | end_rcu_unlock: |
9d1bbf21 | 2385 | rcu_read_unlock(); |
b8aa1682 JD |
2386 | end: |
2387 | return ret; | |
2388 | } | |
2389 | ||
2390 | static | |
58eb9381 | 2391 | void cleanup_connection_pollfd(struct lttng_poll_event *events, int pollfd) |
b8aa1682 JD |
2392 | { |
2393 | int ret; | |
2394 | ||
58eb9381 DG |
2395 | assert(events); |
2396 | ||
2397 | (void) lttng_poll_del(events, pollfd); | |
b8aa1682 JD |
2398 | |
2399 | ret = close(pollfd); | |
2400 | if (ret < 0) { | |
2401 | ERR("Closing pollfd %d", pollfd); | |
2402 | } | |
2403 | } | |
2404 | ||
58eb9381 DG |
2405 | static void destroy_connection(struct lttng_ht *relay_connections_ht, |
2406 | struct relay_connection *conn) | |
9d1bbf21 | 2407 | { |
58eb9381 DG |
2408 | assert(relay_connections_ht); |
2409 | assert(conn); | |
2a174661 | 2410 | |
58eb9381 | 2411 | connection_delete(relay_connections_ht, conn); |
2a174661 | 2412 | |
58eb9381 | 2413 | /* For the control socket, we try to destroy the session. */ |
fd6d7293 | 2414 | if (conn->type == RELAY_CONTROL && conn->session) { |
58eb9381 | 2415 | destroy_session(conn->session, conn->sessions_ht); |
9d1bbf21 | 2416 | } |
5b6d8097 | 2417 | |
58eb9381 | 2418 | connection_destroy(conn); |
b8aa1682 JD |
2419 | } |
2420 | ||
2421 | /* | |
2422 | * This thread does the actual work | |
2423 | */ | |
2424 | static | |
2425 | void *relay_thread_worker(void *data) | |
2426 | { | |
beaad64c DG |
2427 | int ret, err = -1, last_seen_data_fd = -1; |
2428 | uint32_t nb_fd; | |
58eb9381 | 2429 | struct relay_connection *conn; |
b8aa1682 JD |
2430 | struct lttng_poll_event events; |
2431 | struct lttng_ht *relay_connections_ht; | |
b8aa1682 | 2432 | struct lttng_ht_iter iter; |
b8aa1682 | 2433 | struct lttcomm_relayd_hdr recv_hdr; |
d3e2ba59 JD |
2434 | struct relay_local_data *relay_ctx = (struct relay_local_data *) data; |
2435 | struct lttng_ht *sessions_ht = relay_ctx->sessions_ht; | |
aa7102d7 | 2436 | struct relay_index *index; |
b8aa1682 JD |
2437 | |
2438 | DBG("[thread] Relay worker started"); | |
2439 | ||
9d1bbf21 MD |
2440 | rcu_register_thread(); |
2441 | ||
55706a7d MD |
2442 | health_register(health_relayd, HEALTH_RELAYD_TYPE_WORKER); |
2443 | ||
9b5e0863 MD |
2444 | if (testpoint(relayd_thread_worker)) { |
2445 | goto error_testpoint; | |
2446 | } | |
2447 | ||
f385ae0a MD |
2448 | health_code_update(); |
2449 | ||
b8aa1682 JD |
2450 | /* table of connections indexed on socket */ |
2451 | relay_connections_ht = lttng_ht_new(0, LTTNG_HT_TYPE_ULONG); | |
095a4ae5 MD |
2452 | if (!relay_connections_ht) { |
2453 | goto relay_connections_ht_error; | |
2454 | } | |
b8aa1682 | 2455 | |
1c20f0e2 JD |
2456 | /* Tables of received indexes indexed by index handle and net_seq_num. */ |
2457 | indexes_ht = lttng_ht_new(0, LTTNG_HT_TYPE_TWO_U64); | |
2458 | if (!indexes_ht) { | |
2459 | goto indexes_ht_error; | |
2460 | } | |
2461 | ||
b8aa1682 JD |
2462 | ret = create_thread_poll_set(&events, 2); |
2463 | if (ret < 0) { | |
2464 | goto error_poll_create; | |
2465 | } | |
2466 | ||
58eb9381 | 2467 | ret = lttng_poll_add(&events, relay_conn_pipe[0], LPOLLIN | LPOLLRDHUP); |
b8aa1682 JD |
2468 | if (ret < 0) { |
2469 | goto error; | |
2470 | } | |
2471 | ||
beaad64c | 2472 | restart: |
b8aa1682 | 2473 | while (1) { |
beaad64c DG |
2474 | int idx = -1, i, seen_control = 0, last_notdel_data_fd = -1; |
2475 | ||
f385ae0a MD |
2476 | health_code_update(); |
2477 | ||
b8aa1682 | 2478 | /* Infinite blocking call, waiting for transmission */ |
87c1611d | 2479 | DBG3("Relayd worker thread polling..."); |
f385ae0a | 2480 | health_poll_entry(); |
b8aa1682 | 2481 | ret = lttng_poll_wait(&events, -1); |
f385ae0a | 2482 | health_poll_exit(); |
b8aa1682 JD |
2483 | if (ret < 0) { |
2484 | /* | |
2485 | * Restart interrupted system call. | |
2486 | */ | |
2487 | if (errno == EINTR) { | |
2488 | goto restart; | |
2489 | } | |
2490 | goto error; | |
2491 | } | |
2492 | ||
0d9c5d77 DG |
2493 | nb_fd = ret; |
2494 | ||
beaad64c DG |
2495 | /* |
2496 | * Process control. The control connection is prioritised so we don't | |
2497 | * starve it with high throughout put tracing data on the data | |
2498 | * connection. | |
2499 | */ | |
b8aa1682 JD |
2500 | for (i = 0; i < nb_fd; i++) { |
2501 | /* Fetch once the poll data */ | |
beaad64c DG |
2502 | uint32_t revents = LTTNG_POLL_GETEV(&events, i); |
2503 | int pollfd = LTTNG_POLL_GETFD(&events, i); | |
b8aa1682 | 2504 | |
f385ae0a MD |
2505 | health_code_update(); |
2506 | ||
6550503d MD |
2507 | if (!revents) { |
2508 | /* No activity for this FD (poll implementation). */ | |
2509 | continue; | |
2510 | } | |
2511 | ||
b8aa1682 JD |
2512 | /* Thread quit pipe has been closed. Killing thread. */ |
2513 | ret = check_thread_quit_pipe(pollfd, revents); | |
2514 | if (ret) { | |
095a4ae5 MD |
2515 | err = 0; |
2516 | goto exit; | |
b8aa1682 JD |
2517 | } |
2518 | ||
58eb9381 DG |
2519 | /* Inspect the relay conn pipe for new connection */ |
2520 | if (pollfd == relay_conn_pipe[0]) { | |
b8aa1682 | 2521 | if (revents & (LPOLLERR | LPOLLHUP | LPOLLRDHUP)) { |
58eb9381 | 2522 | ERR("Relay connection pipe error"); |
b8aa1682 JD |
2523 | goto error; |
2524 | } else if (revents & LPOLLIN) { | |
58eb9381 | 2525 | ret = lttng_read(relay_conn_pipe[0], &conn, sizeof(conn)); |
b8aa1682 JD |
2526 | if (ret < 0) { |
2527 | goto error; | |
2528 | } | |
58eb9381 DG |
2529 | conn->sessions_ht = sessions_ht; |
2530 | connection_init(conn); | |
2531 | lttng_poll_add(&events, conn->sock->fd, | |
2532 | LPOLLIN | LPOLLRDHUP); | |
2533 | rcu_read_lock(); | |
2534 | lttng_ht_add_unique_ulong(relay_connections_ht, | |
2535 | &conn->sock_n); | |
9d1bbf21 | 2536 | rcu_read_unlock(); |
58eb9381 | 2537 | DBG("Connection socket %d added", conn->sock->fd); |
b8aa1682 | 2538 | } |
58eb9381 DG |
2539 | } else { |
2540 | rcu_read_lock(); | |
2541 | conn = connection_find_by_sock(relay_connections_ht, pollfd); | |
2542 | /* If not found, there is a synchronization issue. */ | |
2543 | assert(conn); | |
2544 | ||
2545 | if (revents & (LPOLLERR | LPOLLHUP | LPOLLRDHUP)) { | |
2546 | cleanup_connection_pollfd(&events, pollfd); | |
2547 | destroy_connection(relay_connections_ht, conn); | |
beaad64c DG |
2548 | if (last_seen_data_fd == pollfd) { |
2549 | last_seen_data_fd = last_notdel_data_fd; | |
2550 | } | |
b8aa1682 | 2551 | } else if (revents & LPOLLIN) { |
58eb9381 DG |
2552 | if (conn->type == RELAY_CONTROL) { |
2553 | ret = conn->sock->ops->recvmsg(conn->sock, &recv_hdr, | |
2554 | sizeof(recv_hdr), 0); | |
b8aa1682 | 2555 | if (ret <= 0) { |
58eb9381 DG |
2556 | /* Connection closed */ |
2557 | cleanup_connection_pollfd(&events, pollfd); | |
2558 | destroy_connection(relay_connections_ht, conn); | |
b8aa1682 JD |
2559 | DBG("Control connection closed with %d", pollfd); |
2560 | } else { | |
58eb9381 | 2561 | ret = relay_process_control(&recv_hdr, conn); |
b8aa1682 | 2562 | if (ret < 0) { |
beaad64c | 2563 | /* Clear the session on error. */ |
58eb9381 DG |
2564 | cleanup_connection_pollfd(&events, pollfd); |
2565 | destroy_connection(relay_connections_ht, conn); | |
b8aa1682 JD |
2566 | DBG("Connection closed with %d", pollfd); |
2567 | } | |
beaad64c | 2568 | seen_control = 1; |
b8aa1682 | 2569 | } |
beaad64c DG |
2570 | } else { |
2571 | /* | |
2572 | * Flag the last seen data fd not deleted. It will be | |
2573 | * used as the last seen fd if any fd gets deleted in | |
2574 | * this first loop. | |
2575 | */ | |
2576 | last_notdel_data_fd = pollfd; | |
2577 | } | |
58eb9381 DG |
2578 | } else { |
2579 | ERR("Unknown poll events %u for sock %d", revents, pollfd); | |
beaad64c DG |
2580 | } |
2581 | rcu_read_unlock(); | |
2582 | } | |
2583 | } | |
2584 | ||
2585 | /* | |
2586 | * The last loop handled a control request, go back to poll to make | |
2587 | * sure we prioritise the control socket. | |
2588 | */ | |
2589 | if (seen_control) { | |
2590 | continue; | |
2591 | } | |
2592 | ||
2593 | if (last_seen_data_fd >= 0) { | |
2594 | for (i = 0; i < nb_fd; i++) { | |
2595 | int pollfd = LTTNG_POLL_GETFD(&events, i); | |
f385ae0a MD |
2596 | |
2597 | health_code_update(); | |
2598 | ||
beaad64c DG |
2599 | if (last_seen_data_fd == pollfd) { |
2600 | idx = i; | |
2601 | break; | |
2602 | } | |
2603 | } | |
2604 | } | |
2605 | ||
2606 | /* Process data connection. */ | |
2607 | for (i = idx + 1; i < nb_fd; i++) { | |
2608 | /* Fetch the poll data. */ | |
2609 | uint32_t revents = LTTNG_POLL_GETEV(&events, i); | |
2610 | int pollfd = LTTNG_POLL_GETFD(&events, i); | |
2611 | ||
f385ae0a MD |
2612 | health_code_update(); |
2613 | ||
6550503d MD |
2614 | if (!revents) { |
2615 | /* No activity for this FD (poll implementation). */ | |
2616 | continue; | |
2617 | } | |
2618 | ||
beaad64c | 2619 | /* Skip the command pipe. It's handled in the first loop. */ |
58eb9381 | 2620 | if (pollfd == relay_conn_pipe[0]) { |
beaad64c DG |
2621 | continue; |
2622 | } | |
2623 | ||
6550503d MD |
2624 | rcu_read_lock(); |
2625 | conn = connection_find_by_sock(relay_connections_ht, pollfd); | |
2626 | if (!conn) { | |
2627 | /* Skip it. Might be removed before. */ | |
2628 | rcu_read_unlock(); | |
2629 | continue; | |
2630 | } | |
2631 | ||
2632 | if (revents & LPOLLIN) { | |
2633 | if (conn->type != RELAY_DATA) { | |
beaad64c DG |
2634 | rcu_read_unlock(); |
2635 | continue; | |
2636 | } | |
beaad64c | 2637 | |
6550503d MD |
2638 | ret = relay_process_data(conn); |
2639 | /* Connection closed */ | |
2640 | if (ret < 0) { | |
2641 | cleanup_connection_pollfd(&events, pollfd); | |
2642 | destroy_connection(relay_connections_ht, conn); | |
2643 | DBG("Data connection closed with %d", pollfd); | |
2644 | /* | |
2645 | * Every goto restart call sets the last seen fd where | |
2646 | * here we don't really care since we gracefully | |
2647 | * continue the loop after the connection is deleted. | |
2648 | */ | |
2649 | } else { | |
2650 | /* Keep last seen port. */ | |
2651 | last_seen_data_fd = pollfd; | |
2652 | rcu_read_unlock(); | |
2653 | goto restart; | |
b8aa1682 JD |
2654 | } |
2655 | } | |
6550503d | 2656 | rcu_read_unlock(); |
b8aa1682 | 2657 | } |
beaad64c | 2658 | last_seen_data_fd = -1; |
b8aa1682 JD |
2659 | } |
2660 | ||
f385ae0a MD |
2661 | /* Normal exit, no error */ |
2662 | ret = 0; | |
2663 | ||
095a4ae5 | 2664 | exit: |
b8aa1682 JD |
2665 | error: |
2666 | lttng_poll_clean(&events); | |
2667 | ||
58eb9381 | 2668 | /* Cleanup reamaining connection object. */ |
9d1bbf21 | 2669 | rcu_read_lock(); |
58eb9381 DG |
2670 | cds_lfht_for_each_entry(relay_connections_ht->ht, &iter.iter, conn, |
2671 | sock_n.node) { | |
f385ae0a | 2672 | health_code_update(); |
58eb9381 | 2673 | destroy_connection(relay_connections_ht, conn); |
b8aa1682 | 2674 | } |
94d49140 | 2675 | rcu_read_unlock(); |
7d2f7452 | 2676 | error_poll_create: |
aa7102d7 JD |
2677 | rcu_read_lock(); |
2678 | cds_lfht_for_each_entry(indexes_ht->ht, &iter.iter, index, | |
2679 | index_n.node) { | |
2680 | health_code_update(); | |
2681 | relay_index_delete(index); | |
2682 | relay_index_free_safe(index); | |
2683 | } | |
2684 | rcu_read_unlock(); | |
7d2f7452 | 2685 | lttng_ht_destroy(indexes_ht); |
1c20f0e2 | 2686 | indexes_ht_error: |
b8aa1682 | 2687 | lttng_ht_destroy(relay_connections_ht); |
095a4ae5 | 2688 | relay_connections_ht_error: |
58eb9381 DG |
2689 | /* Close relay conn pipes */ |
2690 | utils_close_pipe(relay_conn_pipe); | |
095a4ae5 MD |
2691 | if (err) { |
2692 | DBG("Thread exited with error"); | |
2693 | } | |
b8aa1682 | 2694 | DBG("Worker thread cleanup complete"); |
095a4ae5 | 2695 | free(data_buffer); |
9b5e0863 | 2696 | error_testpoint: |
f385ae0a MD |
2697 | if (err) { |
2698 | health_error(); | |
2699 | ERR("Health error occurred in %s", __func__); | |
2700 | } | |
2701 | health_unregister(health_relayd); | |
9d1bbf21 | 2702 | rcu_unregister_thread(); |
f385ae0a | 2703 | stop_threads(); |
b8aa1682 JD |
2704 | return NULL; |
2705 | } | |
2706 | ||
2707 | /* | |
2708 | * Create the relay command pipe to wake thread_manage_apps. | |
2709 | * Closed in cleanup(). | |
2710 | */ | |
58eb9381 | 2711 | static int create_relay_conn_pipe(void) |
b8aa1682 | 2712 | { |
a02de639 | 2713 | int ret; |
b8aa1682 | 2714 | |
58eb9381 | 2715 | ret = utils_create_pipe_cloexec(relay_conn_pipe); |
b8aa1682 | 2716 | |
b8aa1682 JD |
2717 | return ret; |
2718 | } | |
2719 | ||
2720 | /* | |
2721 | * main | |
2722 | */ | |
2723 | int main(int argc, char **argv) | |
2724 | { | |
2725 | int ret = 0; | |
2726 | void *status; | |
d3e2ba59 | 2727 | struct relay_local_data *relay_ctx; |
b8aa1682 | 2728 | |
b8aa1682 JD |
2729 | /* Parse arguments */ |
2730 | progname = argv[0]; | |
cd60b05a | 2731 | if ((ret = set_options(argc, argv)) < 0) { |
a02de639 | 2732 | goto exit; |
b8aa1682 JD |
2733 | } |
2734 | ||
2735 | if ((ret = set_signal_handler()) < 0) { | |
2736 | goto exit; | |
2737 | } | |
2738 | ||
4d513a50 DG |
2739 | /* Try to create directory if -o, --output is specified. */ |
2740 | if (opt_output_path) { | |
994fa64f DG |
2741 | if (*opt_output_path != '/') { |
2742 | ERR("Please specify an absolute path for -o, --output PATH"); | |
2743 | goto exit; | |
2744 | } | |
2745 | ||
4d513a50 DG |
2746 | ret = utils_mkdir_recursive(opt_output_path, S_IRWXU | S_IRWXG); |
2747 | if (ret < 0) { | |
2748 | ERR("Unable to create %s", opt_output_path); | |
2749 | goto exit; | |
2750 | } | |
2751 | } | |
2752 | ||
b8aa1682 | 2753 | /* Daemonize */ |
b5218ffb | 2754 | if (opt_daemon || opt_background) { |
3fd27398 MD |
2755 | int i; |
2756 | ||
2757 | ret = lttng_daemonize(&child_ppid, &recv_child_signal, | |
2758 | !opt_background); | |
b8aa1682 | 2759 | if (ret < 0) { |
a02de639 | 2760 | goto exit; |
b8aa1682 | 2761 | } |
3fd27398 MD |
2762 | |
2763 | /* | |
2764 | * We are in the child. Make sure all other file | |
2765 | * descriptors are closed, in case we are called with | |
2766 | * more opened file descriptors than the standard ones. | |
2767 | */ | |
2768 | for (i = 3; i < sysconf(_SC_OPEN_MAX); i++) { | |
2769 | (void) close(i); | |
2770 | } | |
2771 | } | |
2772 | ||
2773 | /* Create thread quit pipe */ | |
2774 | if ((ret = init_thread_quit_pipe()) < 0) { | |
2775 | goto error; | |
b8aa1682 JD |
2776 | } |
2777 | ||
1c20f0e2 JD |
2778 | /* We need those values for the file/dir creation. */ |
2779 | relayd_uid = getuid(); | |
2780 | relayd_gid = getgid(); | |
b8aa1682 | 2781 | |
1c20f0e2 JD |
2782 | /* Check if daemon is UID = 0 */ |
2783 | if (relayd_uid == 0) { | |
8d5c808e | 2784 | if (control_uri->port < 1024 || data_uri->port < 1024 || live_uri->port < 1024) { |
b8aa1682 JD |
2785 | ERR("Need to be root to use ports < 1024"); |
2786 | ret = -1; | |
a02de639 | 2787 | goto exit; |
b8aa1682 JD |
2788 | } |
2789 | } | |
2790 | ||
2791 | /* Setup the thread apps communication pipe. */ | |
58eb9381 | 2792 | if ((ret = create_relay_conn_pipe()) < 0) { |
b8aa1682 JD |
2793 | goto exit; |
2794 | } | |
2795 | ||
2796 | /* Init relay command queue. */ | |
58eb9381 | 2797 | cds_wfq_init(&relay_conn_queue.queue); |
b8aa1682 JD |
2798 | |
2799 | /* Set up max poll set size */ | |
2800 | lttng_poll_set_max_size(); | |
2801 | ||
554831e7 MD |
2802 | /* Initialize communication library */ |
2803 | lttcomm_init(); | |
87e45c13 | 2804 | lttcomm_inet_init(); |
554831e7 | 2805 | |
d3e2ba59 JD |
2806 | relay_ctx = zmalloc(sizeof(struct relay_local_data)); |
2807 | if (!relay_ctx) { | |
2808 | PERROR("relay_ctx"); | |
2809 | goto exit; | |
2810 | } | |
2811 | ||
2812 | /* tables of sessions indexed by session ID */ | |
2a174661 | 2813 | relay_ctx->sessions_ht = lttng_ht_new(0, LTTNG_HT_TYPE_U64); |
d3e2ba59 JD |
2814 | if (!relay_ctx->sessions_ht) { |
2815 | goto exit_relay_ctx_sessions; | |
2816 | } | |
2817 | ||
2818 | /* tables of streams indexed by stream ID */ | |
2a174661 | 2819 | relay_streams_ht = lttng_ht_new(0, LTTNG_HT_TYPE_U64); |
d3e2ba59 JD |
2820 | if (!relay_streams_ht) { |
2821 | goto exit_relay_ctx_streams; | |
2822 | } | |
2823 | ||
2824 | /* tables of streams indexed by stream ID */ | |
92c6ca54 DG |
2825 | viewer_streams_ht = lttng_ht_new(0, LTTNG_HT_TYPE_U64); |
2826 | if (!viewer_streams_ht) { | |
d3e2ba59 JD |
2827 | goto exit_relay_ctx_viewer_streams; |
2828 | } | |
2829 | ||
55706a7d MD |
2830 | /* Initialize thread health monitoring */ |
2831 | health_relayd = health_app_create(NR_HEALTH_RELAYD_TYPES); | |
2832 | if (!health_relayd) { | |
2833 | PERROR("health_app_create error"); | |
2834 | goto exit_health_app_create; | |
2835 | } | |
2836 | ||
65931c8b MD |
2837 | ret = utils_create_pipe(health_quit_pipe); |
2838 | if (ret < 0) { | |
2839 | goto error_health_pipe; | |
2840 | } | |
2841 | ||
2842 | /* Create thread to manage the client socket */ | |
2843 | ret = pthread_create(&health_thread, NULL, | |
2844 | thread_manage_health, (void *) NULL); | |
2845 | if (ret != 0) { | |
2846 | PERROR("pthread_create health"); | |
2847 | goto health_error; | |
2848 | } | |
2849 | ||
b8aa1682 JD |
2850 | /* Setup the dispatcher thread */ |
2851 | ret = pthread_create(&dispatcher_thread, NULL, | |
2852 | relay_thread_dispatcher, (void *) NULL); | |
2853 | if (ret != 0) { | |
2854 | PERROR("pthread_create dispatcher"); | |
2855 | goto exit_dispatcher; | |
2856 | } | |
2857 | ||
2858 | /* Setup the worker thread */ | |
2859 | ret = pthread_create(&worker_thread, NULL, | |
d3e2ba59 | 2860 | relay_thread_worker, (void *) relay_ctx); |
b8aa1682 JD |
2861 | if (ret != 0) { |
2862 | PERROR("pthread_create worker"); | |
2863 | goto exit_worker; | |
2864 | } | |
2865 | ||
2866 | /* Setup the listener thread */ | |
2867 | ret = pthread_create(&listener_thread, NULL, | |
2868 | relay_thread_listener, (void *) NULL); | |
2869 | if (ret != 0) { | |
2870 | PERROR("pthread_create listener"); | |
2871 | goto exit_listener; | |
2872 | } | |
2873 | ||
0b242f62 | 2874 | ret = live_start_threads(live_uri, relay_ctx); |
d3e2ba59 JD |
2875 | if (ret != 0) { |
2876 | ERR("Starting live viewer threads"); | |
50138f51 | 2877 | goto exit_live; |
d3e2ba59 JD |
2878 | } |
2879 | ||
50138f51 | 2880 | exit_live: |
b8aa1682 JD |
2881 | ret = pthread_join(listener_thread, &status); |
2882 | if (ret != 0) { | |
2883 | PERROR("pthread_join"); | |
2884 | goto error; /* join error, exit without cleanup */ | |
2885 | } | |
2886 | ||
50138f51 | 2887 | exit_listener: |
b8aa1682 JD |
2888 | ret = pthread_join(worker_thread, &status); |
2889 | if (ret != 0) { | |
2890 | PERROR("pthread_join"); | |
2891 | goto error; /* join error, exit without cleanup */ | |
2892 | } | |
2893 | ||
50138f51 | 2894 | exit_worker: |
b8aa1682 JD |
2895 | ret = pthread_join(dispatcher_thread, &status); |
2896 | if (ret != 0) { | |
2897 | PERROR("pthread_join"); | |
2898 | goto error; /* join error, exit without cleanup */ | |
2899 | } | |
42415026 | 2900 | |
50138f51 | 2901 | exit_dispatcher: |
65931c8b MD |
2902 | ret = pthread_join(health_thread, &status); |
2903 | if (ret != 0) { | |
2904 | PERROR("pthread_join health thread"); | |
2905 | goto error; /* join error, exit without cleanup */ | |
2906 | } | |
2907 | ||
bd11e201 MD |
2908 | /* |
2909 | * Stop live threads only after joining other threads. | |
2910 | */ | |
2911 | live_stop_threads(); | |
2912 | ||
65931c8b MD |
2913 | health_error: |
2914 | utils_close_pipe(health_quit_pipe); | |
2915 | ||
2916 | error_health_pipe: | |
55706a7d MD |
2917 | health_app_destroy(health_relayd); |
2918 | ||
2919 | exit_health_app_create: | |
92c6ca54 | 2920 | lttng_ht_destroy(viewer_streams_ht); |
d3e2ba59 JD |
2921 | |
2922 | exit_relay_ctx_viewer_streams: | |
2923 | lttng_ht_destroy(relay_streams_ht); | |
2924 | ||
2925 | exit_relay_ctx_streams: | |
2926 | lttng_ht_destroy(relay_ctx->sessions_ht); | |
2927 | ||
2928 | exit_relay_ctx_sessions: | |
2929 | free(relay_ctx); | |
b8aa1682 JD |
2930 | |
2931 | exit: | |
2932 | cleanup(); | |
2933 | if (!ret) { | |
2934 | exit(EXIT_SUCCESS); | |
2935 | } | |
a02de639 | 2936 | |
b8aa1682 JD |
2937 | error: |
2938 | exit(EXIT_FAILURE); | |
2939 | } |