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