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