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