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