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> |
7591bab1 | 5 | * 2015 - Mathieu Desnoyers <mathieu.desnoyers@efficios.com> |
b8aa1682 JD |
6 | * |
7 | * This program is free software; you can redistribute it and/or modify | |
8 | * it under the terms of the GNU General Public License, version 2 only, | |
9 | * as published by the Free Software Foundation. | |
10 | * | |
11 | * This program is distributed in the hope that it will be useful, but WITHOUT | |
12 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or | |
13 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for | |
14 | * more details. | |
15 | * | |
16 | * You should have received a copy of the GNU General Public License along | |
17 | * with this program; if not, write to the Free Software Foundation, Inc., | |
18 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. | |
19 | */ | |
20 | ||
6c1c0768 | 21 | #define _LGPL_SOURCE |
b8aa1682 JD |
22 | #include <getopt.h> |
23 | #include <grp.h> | |
24 | #include <limits.h> | |
25 | #include <pthread.h> | |
26 | #include <signal.h> | |
27 | #include <stdio.h> | |
28 | #include <stdlib.h> | |
29 | #include <string.h> | |
30 | #include <sys/mman.h> | |
31 | #include <sys/mount.h> | |
32 | #include <sys/resource.h> | |
33 | #include <sys/socket.h> | |
34 | #include <sys/stat.h> | |
35 | #include <sys/types.h> | |
36 | #include <sys/wait.h> | |
a2cc5ee8 | 37 | #include <sys/resource.h> |
173af62f | 38 | #include <inttypes.h> |
b8aa1682 JD |
39 | #include <urcu/futex.h> |
40 | #include <urcu/uatomic.h> | |
41 | #include <unistd.h> | |
42 | #include <fcntl.h> | |
a2cc5ee8 | 43 | #include <ctype.h> |
b8aa1682 JD |
44 | |
45 | #include <lttng/lttng.h> | |
46 | #include <common/common.h> | |
47 | #include <common/compat/poll.h> | |
48 | #include <common/compat/socket.h> | |
f263b7fd | 49 | #include <common/compat/endian.h> |
e8fa9fb0 | 50 | #include <common/compat/getenv.h> |
b8aa1682 | 51 | #include <common/defaults.h> |
3fd27398 | 52 | #include <common/daemonize.h> |
b8aa1682 JD |
53 | #include <common/futex.h> |
54 | #include <common/sessiond-comm/sessiond-comm.h> | |
55 | #include <common/sessiond-comm/inet.h> | |
b8aa1682 JD |
56 | #include <common/sessiond-comm/relayd.h> |
57 | #include <common/uri.h> | |
a02de639 | 58 | #include <common/utils.h> |
f40ef1d5 | 59 | #include <common/config/session-config.h> |
44a2fbf1 JG |
60 | #include <common/dynamic-buffer.h> |
61 | #include <common/buffer-view.h> | |
945459f8 | 62 | #include <common/fd-tracker/utils.h> |
7591bab1 | 63 | #include <urcu/rculist.h> |
b8aa1682 | 64 | |
8ff7624a | 65 | #include "version.h" |
0f907de1 | 66 | #include "cmd.h" |
d3e2ba59 | 67 | #include "ctf-trace.h" |
1c20f0e2 | 68 | #include "index.h" |
0f907de1 | 69 | #include "utils.h" |
b8aa1682 | 70 | #include "lttng-relayd.h" |
d3e2ba59 | 71 | #include "live.h" |
55706a7d | 72 | #include "health-relayd.h" |
9b5e0863 | 73 | #include "testpoint.h" |
2f8f53af | 74 | #include "viewer-stream.h" |
2a174661 DG |
75 | #include "session.h" |
76 | #include "stream.h" | |
58eb9381 | 77 | #include "connection.h" |
a44ca2ca | 78 | #include "tracefile-array.h" |
2fc6b1ab | 79 | #include "tcp_keep_alive.h" |
b8aa1682 | 80 | |
44a2fbf1 JG |
81 | enum relay_connection_status { |
82 | RELAY_CONNECTION_STATUS_OK, | |
83 | /* An error occured while processing an event on the connection. */ | |
84 | RELAY_CONNECTION_STATUS_ERROR, | |
85 | /* Connection closed/shutdown cleanly. */ | |
86 | RELAY_CONNECTION_STATUS_CLOSED, | |
87 | }; | |
88 | ||
b8aa1682 | 89 | /* command line options */ |
76375580 | 90 | char *opt_output_path, *opt_working_directory; |
8ff7624a | 91 | static int opt_daemon, opt_background, opt_print_version; |
b4d507ca JR |
92 | int opt_group_output_by_session; |
93 | int opt_group_output_by_host; | |
3fd27398 MD |
94 | |
95 | /* | |
96 | * We need to wait for listener and live listener threads, as well as | |
97 | * health check thread, before being ready to signal readiness. | |
98 | */ | |
99 | #define NR_LTTNG_RELAY_READY 3 | |
100 | static int lttng_relay_ready = NR_LTTNG_RELAY_READY; | |
0848dba7 MD |
101 | |
102 | /* Size of receive buffer. */ | |
103 | #define RECV_DATA_BUFFER_SIZE 65536 | |
104 | ||
3fd27398 MD |
105 | static int recv_child_signal; /* Set to 1 when a SIGUSR1 signal is received. */ |
106 | static pid_t child_ppid; /* Internal parent PID use with daemonize. */ | |
107 | ||
095a4ae5 MD |
108 | static struct lttng_uri *control_uri; |
109 | static struct lttng_uri *data_uri; | |
d3e2ba59 | 110 | static struct lttng_uri *live_uri; |
b8aa1682 JD |
111 | |
112 | const char *progname; | |
b8aa1682 | 113 | |
65931c8b | 114 | const char *tracing_group_name = DEFAULT_TRACING_GROUP; |
cd60b05a JG |
115 | static int tracing_group_name_override; |
116 | ||
117 | const char * const config_section_name = "relayd"; | |
65931c8b | 118 | |
b8aa1682 JD |
119 | /* |
120 | * Quit pipe for all threads. This permits a single cancellation point | |
121 | * for all threads when receiving an event on the pipe. | |
122 | */ | |
0b242f62 | 123 | int thread_quit_pipe[2] = { -1, -1 }; |
b8aa1682 JD |
124 | |
125 | /* | |
126 | * This pipe is used to inform the worker thread that a command is queued and | |
127 | * ready to be processed. | |
128 | */ | |
58eb9381 | 129 | static int relay_conn_pipe[2] = { -1, -1 }; |
b8aa1682 | 130 | |
26c9d55e | 131 | /* Shared between threads */ |
b8aa1682 JD |
132 | static int dispatch_thread_exit; |
133 | ||
134 | static pthread_t listener_thread; | |
135 | static pthread_t dispatcher_thread; | |
136 | static pthread_t worker_thread; | |
65931c8b | 137 | static pthread_t health_thread; |
b8aa1682 | 138 | |
7591bab1 MD |
139 | /* |
140 | * last_relay_stream_id_lock protects last_relay_stream_id increment | |
141 | * atomicity on 32-bit architectures. | |
142 | */ | |
143 | static pthread_mutex_t last_relay_stream_id_lock = PTHREAD_MUTEX_INITIALIZER; | |
095a4ae5 | 144 | static uint64_t last_relay_stream_id; |
b8aa1682 JD |
145 | |
146 | /* | |
147 | * Relay command queue. | |
148 | * | |
149 | * The relay_thread_listener and relay_thread_dispatcher communicate with this | |
150 | * queue. | |
151 | */ | |
58eb9381 | 152 | static struct relay_conn_queue relay_conn_queue; |
b8aa1682 | 153 | |
a2cc5ee8 JG |
154 | /* Cap of file desriptors to be in simultaneous use by the relay daemon. */ |
155 | static unsigned int lttng_opt_fd_cap; | |
156 | ||
d3e2ba59 JD |
157 | /* Global relay stream hash table. */ |
158 | struct lttng_ht *relay_streams_ht; | |
159 | ||
92c6ca54 DG |
160 | /* Global relay viewer stream hash table. */ |
161 | struct lttng_ht *viewer_streams_ht; | |
162 | ||
7591bab1 MD |
163 | /* Global relay sessions hash table. */ |
164 | struct lttng_ht *sessions_ht; | |
0a6518b0 | 165 | |
55706a7d | 166 | /* Relayd health monitoring */ |
eea7556c | 167 | struct health_app *health_relayd; |
55706a7d | 168 | |
841c095c JG |
169 | /* Global fd tracker. */ |
170 | struct fd_tracker *the_fd_tracker; | |
171 | ||
cd60b05a JG |
172 | static struct option long_options[] = { |
173 | { "control-port", 1, 0, 'C', }, | |
174 | { "data-port", 1, 0, 'D', }, | |
8d5c808e | 175 | { "live-port", 1, 0, 'L', }, |
cd60b05a | 176 | { "daemonize", 0, 0, 'd', }, |
b5218ffb | 177 | { "background", 0, 0, 'b', }, |
cd60b05a | 178 | { "group", 1, 0, 'g', }, |
a2cc5ee8 | 179 | { "fd-cap", 1, 0, '\0', }, |
cd60b05a JG |
180 | { "help", 0, 0, 'h', }, |
181 | { "output", 1, 0, 'o', }, | |
182 | { "verbose", 0, 0, 'v', }, | |
183 | { "config", 1, 0, 'f' }, | |
297af7ff | 184 | { "version", 0, 0, 'V' }, |
76375580 | 185 | { "working-directory", 1, 0, 'w', }, |
b4d507ca JR |
186 | { "group-output-by-session", 0, 0, 's', }, |
187 | { "group-output-by-host", 0, 0, 'p', }, | |
cd60b05a JG |
188 | { NULL, 0, 0, 0, }, |
189 | }; | |
190 | ||
297af7ff | 191 | static const char *config_ignore_options[] = { "help", "config", "version" }; |
cd60b05a | 192 | |
8ff7624a JR |
193 | static void print_version(void) { |
194 | fprintf(stdout, "%s\n", VERSION); | |
195 | } | |
196 | ||
197 | static void relayd_config_log(void) | |
198 | { | |
199 | DBG("LTTng-relayd " VERSION " - " VERSION_NAME "%s%s", | |
200 | GIT_VERSION[0] == '\0' ? "" : " - " GIT_VERSION, | |
201 | EXTRA_VERSION_NAME[0] == '\0' ? "" : " - " EXTRA_VERSION_NAME); | |
202 | if (EXTRA_VERSION_DESCRIPTION[0] != '\0') { | |
203 | DBG("LTTng-relayd extra version description:\n\t" EXTRA_VERSION_DESCRIPTION "\n"); | |
204 | } | |
2987f328 JR |
205 | if (EXTRA_VERSION_PATCHES[0] != '\0') { |
206 | DBG("LTTng-relayd extra patches:\n\t" EXTRA_VERSION_PATCHES "\n"); | |
207 | } | |
8ff7624a JR |
208 | } |
209 | ||
cd60b05a JG |
210 | /* |
211 | * Take an option from the getopt output and set it in the right variable to be | |
212 | * used later. | |
213 | * | |
214 | * Return 0 on success else a negative value. | |
215 | */ | |
7591bab1 | 216 | static int set_option(int opt, const char *arg, const char *optname) |
b8aa1682 | 217 | { |
cd60b05a JG |
218 | int ret; |
219 | ||
220 | switch (opt) { | |
221 | case 0: | |
a2cc5ee8 JG |
222 | if (!strcmp(optname, "fd-cap")) { |
223 | unsigned long v; | |
224 | ||
225 | errno = 0; | |
226 | v = strtoul(arg, NULL, 0); | |
227 | if (errno != 0 || !isdigit(arg[0])) { | |
228 | ERR("Wrong value in --fd-cap parameter: %s", arg); | |
229 | ret = -1; | |
230 | goto end; | |
231 | } | |
232 | if (v < DEFAULT_RELAYD_MINIMAL_FD_CAP) { | |
233 | ERR("File descriptor cap must be set to at least %d", | |
234 | DEFAULT_RELAYD_MINIMAL_FD_CAP); | |
235 | } | |
236 | if (v >= UINT_MAX) { | |
237 | ERR("File descriptor cap overflow in --fd-cap parameter: %s", arg); | |
238 | ret = -1; | |
239 | goto end; | |
240 | } | |
241 | lttng_opt_fd_cap = (unsigned int) v; | |
242 | DBG3("File descriptor cap set to %u", lttng_opt_fd_cap); | |
243 | ||
244 | } else { | |
245 | fprintf(stderr, "unknown option %s", optname); | |
246 | if (arg) { | |
247 | fprintf(stderr, " with arg %s\n", arg); | |
248 | } | |
cd60b05a JG |
249 | } |
250 | break; | |
251 | case 'C': | |
e8fa9fb0 MD |
252 | if (lttng_is_setuid_setgid()) { |
253 | WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.", | |
254 | "-C, --control-port"); | |
255 | } else { | |
256 | ret = uri_parse(arg, &control_uri); | |
257 | if (ret < 0) { | |
258 | ERR("Invalid control URI specified"); | |
259 | goto end; | |
260 | } | |
261 | if (control_uri->port == 0) { | |
262 | control_uri->port = DEFAULT_NETWORK_CONTROL_PORT; | |
263 | } | |
cd60b05a JG |
264 | } |
265 | break; | |
266 | case 'D': | |
e8fa9fb0 MD |
267 | if (lttng_is_setuid_setgid()) { |
268 | WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.", | |
269 | "-D, -data-port"); | |
270 | } else { | |
271 | ret = uri_parse(arg, &data_uri); | |
272 | if (ret < 0) { | |
273 | ERR("Invalid data URI specified"); | |
274 | goto end; | |
275 | } | |
276 | if (data_uri->port == 0) { | |
277 | data_uri->port = DEFAULT_NETWORK_DATA_PORT; | |
278 | } | |
cd60b05a JG |
279 | } |
280 | break; | |
8d5c808e | 281 | case 'L': |
e8fa9fb0 MD |
282 | if (lttng_is_setuid_setgid()) { |
283 | WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.", | |
284 | "-L, -live-port"); | |
285 | } else { | |
286 | ret = uri_parse(arg, &live_uri); | |
287 | if (ret < 0) { | |
288 | ERR("Invalid live URI specified"); | |
289 | goto end; | |
290 | } | |
291 | if (live_uri->port == 0) { | |
292 | live_uri->port = DEFAULT_NETWORK_VIEWER_PORT; | |
293 | } | |
8d5c808e AM |
294 | } |
295 | break; | |
cd60b05a JG |
296 | case 'd': |
297 | opt_daemon = 1; | |
298 | break; | |
b5218ffb MD |
299 | case 'b': |
300 | opt_background = 1; | |
301 | break; | |
cd60b05a | 302 | case 'g': |
e8fa9fb0 MD |
303 | if (lttng_is_setuid_setgid()) { |
304 | WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.", | |
305 | "-g, --group"); | |
306 | } else { | |
307 | tracing_group_name = strdup(arg); | |
308 | if (tracing_group_name == NULL) { | |
309 | ret = -errno; | |
310 | PERROR("strdup"); | |
311 | goto end; | |
312 | } | |
313 | tracing_group_name_override = 1; | |
330a40bb | 314 | } |
cd60b05a JG |
315 | break; |
316 | case 'h': | |
655b5cc1 PP |
317 | ret = utils_show_man_page(8, "lttng-relayd"); |
318 | if (ret) { | |
319 | ERR("Cannot view man page lttng-relayd(8)"); | |
320 | perror("exec"); | |
321 | } | |
cd60b05a | 322 | exit(EXIT_FAILURE); |
297af7ff | 323 | case 'V': |
8ff7624a JR |
324 | opt_print_version = 1; |
325 | break; | |
cd60b05a | 326 | case 'o': |
e8fa9fb0 MD |
327 | if (lttng_is_setuid_setgid()) { |
328 | WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.", | |
329 | "-o, --output"); | |
330 | } else { | |
331 | ret = asprintf(&opt_output_path, "%s", arg); | |
332 | if (ret < 0) { | |
333 | ret = -errno; | |
334 | PERROR("asprintf opt_output_path"); | |
335 | goto end; | |
336 | } | |
cd60b05a JG |
337 | } |
338 | break; | |
76375580 JR |
339 | case 'w': |
340 | if (lttng_is_setuid_setgid()) { | |
341 | WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.", | |
342 | "-w, --working-directory"); | |
343 | } else { | |
344 | ret = asprintf(&opt_working_directory, "%s", arg); | |
345 | if (ret < 0) { | |
346 | ret = -errno; | |
347 | PERROR("asprintf working_directory"); | |
348 | goto end; | |
349 | } | |
350 | } | |
351 | break; | |
352 | ||
cd60b05a JG |
353 | case 'v': |
354 | /* Verbose level can increase using multiple -v */ | |
355 | if (arg) { | |
356 | lttng_opt_verbose = config_parse_value(arg); | |
357 | } else { | |
849e5b7b DG |
358 | /* Only 3 level of verbosity (-vvv). */ |
359 | if (lttng_opt_verbose < 3) { | |
360 | lttng_opt_verbose += 1; | |
361 | } | |
cd60b05a JG |
362 | } |
363 | break; | |
b4d507ca JR |
364 | case 's': |
365 | if (opt_group_output_by_host) { | |
366 | ERR("Cannot set --group-output-by-session, --group-output-by-host already defined"); | |
367 | exit(EXIT_FAILURE); | |
368 | } | |
369 | opt_group_output_by_session = 1; | |
370 | break; | |
371 | case 'p': | |
372 | if (opt_group_output_by_session) { | |
373 | ERR("Cannot set --group-output-by-host, --group-output-by-session already defined"); | |
374 | exit(EXIT_FAILURE); | |
375 | } | |
376 | opt_group_output_by_host = 1; | |
377 | break; | |
cd60b05a JG |
378 | default: |
379 | /* Unknown option or other error. | |
380 | * Error is printed by getopt, just return */ | |
381 | ret = -1; | |
382 | goto end; | |
383 | } | |
384 | ||
385 | /* All good. */ | |
386 | ret = 0; | |
387 | ||
388 | end: | |
389 | return ret; | |
390 | } | |
391 | ||
392 | /* | |
393 | * config_entry_handler_cb used to handle options read from a config file. | |
f40ef1d5 | 394 | * See config_entry_handler_cb comment in common/config/session-config.h for the |
cd60b05a JG |
395 | * return value conventions. |
396 | */ | |
7591bab1 | 397 | static int config_entry_handler(const struct config_entry *entry, void *unused) |
cd60b05a JG |
398 | { |
399 | int ret = 0, i; | |
400 | ||
401 | if (!entry || !entry->name || !entry->value) { | |
402 | ret = -EINVAL; | |
403 | goto end; | |
404 | } | |
405 | ||
406 | /* Check if the option is to be ignored */ | |
407 | for (i = 0; i < sizeof(config_ignore_options) / sizeof(char *); i++) { | |
408 | if (!strcmp(entry->name, config_ignore_options[i])) { | |
409 | goto end; | |
410 | } | |
411 | } | |
412 | ||
413 | for (i = 0; i < (sizeof(long_options) / sizeof(struct option)) - 1; i++) { | |
414 | /* Ignore if entry name is not fully matched. */ | |
415 | if (strcmp(entry->name, long_options[i].name)) { | |
416 | continue; | |
417 | } | |
418 | ||
419 | /* | |
7591bab1 MD |
420 | * If the option takes no argument on the command line, |
421 | * we have to check if the value is "true". We support | |
422 | * non-zero numeric values, true, on and yes. | |
cd60b05a JG |
423 | */ |
424 | if (!long_options[i].has_arg) { | |
425 | ret = config_parse_value(entry->value); | |
426 | if (ret <= 0) { | |
427 | if (ret) { | |
428 | WARN("Invalid configuration value \"%s\" for option %s", | |
429 | entry->value, entry->name); | |
430 | } | |
431 | /* False, skip boolean config option. */ | |
432 | goto end; | |
433 | } | |
434 | } | |
435 | ||
436 | ret = set_option(long_options[i].val, entry->value, entry->name); | |
437 | goto end; | |
438 | } | |
439 | ||
440 | WARN("Unrecognized option \"%s\" in daemon configuration file.", | |
441 | entry->name); | |
442 | ||
443 | end: | |
444 | return ret; | |
445 | } | |
446 | ||
c7cc870e JR |
447 | static void parse_env_options(void) |
448 | { | |
449 | char *value = NULL; | |
450 | ||
451 | value = lttng_secure_getenv(DEFAULT_LTTNG_RELAYD_WORKING_DIRECTORY_ENV); | |
452 | if (value) { | |
453 | opt_working_directory = value; | |
454 | } | |
455 | } | |
456 | ||
7591bab1 | 457 | static int set_options(int argc, char **argv) |
cd60b05a | 458 | { |
178a0557 | 459 | int c, ret = 0, option_index = 0, retval = 0; |
cd60b05a JG |
460 | int orig_optopt = optopt, orig_optind = optind; |
461 | char *default_address, *optstring; | |
462 | const char *config_path = NULL; | |
463 | ||
464 | optstring = utils_generate_optstring(long_options, | |
465 | sizeof(long_options) / sizeof(struct option)); | |
466 | if (!optstring) { | |
178a0557 | 467 | retval = -ENOMEM; |
cd60b05a JG |
468 | goto exit; |
469 | } | |
470 | ||
471 | /* Check for the --config option */ | |
472 | ||
473 | while ((c = getopt_long(argc, argv, optstring, long_options, | |
474 | &option_index)) != -1) { | |
475 | if (c == '?') { | |
178a0557 | 476 | retval = -EINVAL; |
cd60b05a JG |
477 | goto exit; |
478 | } else if (c != 'f') { | |
479 | continue; | |
480 | } | |
481 | ||
e8fa9fb0 MD |
482 | if (lttng_is_setuid_setgid()) { |
483 | WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.", | |
484 | "-f, --config"); | |
485 | } else { | |
486 | config_path = utils_expand_path(optarg); | |
487 | if (!config_path) { | |
488 | ERR("Failed to resolve path: %s", optarg); | |
489 | } | |
cd60b05a JG |
490 | } |
491 | } | |
492 | ||
493 | ret = config_get_section_entries(config_path, config_section_name, | |
494 | config_entry_handler, NULL); | |
495 | if (ret) { | |
496 | if (ret > 0) { | |
497 | ERR("Invalid configuration option at line %i", ret); | |
cd60b05a | 498 | } |
178a0557 | 499 | retval = -1; |
cd60b05a JG |
500 | goto exit; |
501 | } | |
b8aa1682 | 502 | |
cd60b05a JG |
503 | /* Reset getopt's global state */ |
504 | optopt = orig_optopt; | |
505 | optind = orig_optind; | |
b8aa1682 | 506 | while (1) { |
cd60b05a | 507 | c = getopt_long(argc, argv, optstring, long_options, &option_index); |
b8aa1682 JD |
508 | if (c == -1) { |
509 | break; | |
510 | } | |
511 | ||
cd60b05a JG |
512 | ret = set_option(c, optarg, long_options[option_index].name); |
513 | if (ret < 0) { | |
178a0557 | 514 | retval = -1; |
b8aa1682 JD |
515 | goto exit; |
516 | } | |
517 | } | |
518 | ||
519 | /* assign default values */ | |
520 | if (control_uri == NULL) { | |
fa91dc52 MD |
521 | ret = asprintf(&default_address, |
522 | "tcp://" DEFAULT_NETWORK_CONTROL_BIND_ADDRESS ":%d", | |
523 | DEFAULT_NETWORK_CONTROL_PORT); | |
b8aa1682 JD |
524 | if (ret < 0) { |
525 | PERROR("asprintf default data address"); | |
178a0557 | 526 | retval = -1; |
b8aa1682 JD |
527 | goto exit; |
528 | } | |
529 | ||
530 | ret = uri_parse(default_address, &control_uri); | |
531 | free(default_address); | |
532 | if (ret < 0) { | |
533 | ERR("Invalid control URI specified"); | |
178a0557 | 534 | retval = -1; |
b8aa1682 JD |
535 | goto exit; |
536 | } | |
537 | } | |
538 | if (data_uri == NULL) { | |
fa91dc52 MD |
539 | ret = asprintf(&default_address, |
540 | "tcp://" DEFAULT_NETWORK_DATA_BIND_ADDRESS ":%d", | |
541 | DEFAULT_NETWORK_DATA_PORT); | |
b8aa1682 JD |
542 | if (ret < 0) { |
543 | PERROR("asprintf default data address"); | |
178a0557 | 544 | retval = -1; |
b8aa1682 JD |
545 | goto exit; |
546 | } | |
547 | ||
548 | ret = uri_parse(default_address, &data_uri); | |
549 | free(default_address); | |
550 | if (ret < 0) { | |
551 | ERR("Invalid data URI specified"); | |
178a0557 | 552 | retval = -1; |
b8aa1682 JD |
553 | goto exit; |
554 | } | |
555 | } | |
d3e2ba59 | 556 | if (live_uri == NULL) { |
fa91dc52 MD |
557 | ret = asprintf(&default_address, |
558 | "tcp://" DEFAULT_NETWORK_VIEWER_BIND_ADDRESS ":%d", | |
559 | DEFAULT_NETWORK_VIEWER_PORT); | |
d3e2ba59 JD |
560 | if (ret < 0) { |
561 | PERROR("asprintf default viewer control address"); | |
178a0557 | 562 | retval = -1; |
d3e2ba59 JD |
563 | goto exit; |
564 | } | |
565 | ||
566 | ret = uri_parse(default_address, &live_uri); | |
567 | free(default_address); | |
568 | if (ret < 0) { | |
569 | ERR("Invalid viewer control URI specified"); | |
178a0557 | 570 | retval = -1; |
d3e2ba59 JD |
571 | goto exit; |
572 | } | |
573 | } | |
a2cc5ee8 JG |
574 | if (lttng_opt_fd_cap == 0) { |
575 | int ret; | |
576 | struct rlimit rlimit; | |
577 | ||
578 | ret = getrlimit(RLIMIT_NOFILE, &rlimit); | |
579 | if (ret) { | |
580 | PERROR("Failed to get file descriptor limit"); | |
581 | retval = -1; | |
582 | } | |
583 | ||
584 | lttng_opt_fd_cap = rlimit.rlim_cur; | |
585 | } | |
b8aa1682 | 586 | |
b4d507ca JR |
587 | if (!opt_group_output_by_session && !opt_group_output_by_host) { |
588 | /* Group by host by default */ | |
589 | opt_group_output_by_host = 1; | |
590 | } | |
591 | ||
b8aa1682 | 592 | exit: |
cd60b05a | 593 | free(optstring); |
178a0557 | 594 | return retval; |
b8aa1682 JD |
595 | } |
596 | ||
7591bab1 MD |
597 | static void print_global_objects(void) |
598 | { | |
7591bab1 MD |
599 | print_viewer_streams(); |
600 | print_relay_streams(); | |
601 | print_sessions(); | |
7591bab1 MD |
602 | } |
603 | ||
b8aa1682 JD |
604 | /* |
605 | * Cleanup the daemon | |
606 | */ | |
7591bab1 | 607 | static void relayd_cleanup(void) |
b8aa1682 | 608 | { |
7591bab1 MD |
609 | print_global_objects(); |
610 | ||
b8aa1682 JD |
611 | DBG("Cleaning up"); |
612 | ||
178a0557 MD |
613 | if (viewer_streams_ht) |
614 | lttng_ht_destroy(viewer_streams_ht); | |
615 | if (relay_streams_ht) | |
616 | lttng_ht_destroy(relay_streams_ht); | |
7591bab1 MD |
617 | if (sessions_ht) |
618 | lttng_ht_destroy(sessions_ht); | |
178a0557 | 619 | |
095a4ae5 MD |
620 | /* free the dynamically allocated opt_output_path */ |
621 | free(opt_output_path); | |
622 | ||
a02de639 | 623 | /* Close thread quit pipes */ |
e5e4e274 | 624 | (void) fd_tracker_util_pipe_close(the_fd_tracker, thread_quit_pipe); |
a02de639 | 625 | |
710c1f73 DG |
626 | uri_free(control_uri); |
627 | uri_free(data_uri); | |
8d5c808e | 628 | /* Live URI is freed in the live thread. */ |
cd60b05a JG |
629 | |
630 | if (tracing_group_name_override) { | |
631 | free((void *) tracing_group_name); | |
632 | } | |
b8aa1682 JD |
633 | } |
634 | ||
635 | /* | |
636 | * Write to writable pipe used to notify a thread. | |
637 | */ | |
7591bab1 | 638 | static int notify_thread_pipe(int wpipe) |
b8aa1682 | 639 | { |
6cd525e8 | 640 | ssize_t ret; |
b8aa1682 | 641 | |
6cd525e8 MD |
642 | ret = lttng_write(wpipe, "!", 1); |
643 | if (ret < 1) { | |
b8aa1682 | 644 | PERROR("write poll pipe"); |
b4aacfdc | 645 | goto end; |
b8aa1682 | 646 | } |
b4aacfdc MD |
647 | ret = 0; |
648 | end: | |
b8aa1682 JD |
649 | return ret; |
650 | } | |
651 | ||
7591bab1 | 652 | static int notify_health_quit_pipe(int *pipe) |
65931c8b | 653 | { |
6cd525e8 | 654 | ssize_t ret; |
65931c8b | 655 | |
6cd525e8 MD |
656 | ret = lttng_write(pipe[1], "4", 1); |
657 | if (ret < 1) { | |
65931c8b | 658 | PERROR("write relay health quit"); |
b4aacfdc | 659 | goto end; |
65931c8b | 660 | } |
b4aacfdc MD |
661 | ret = 0; |
662 | end: | |
663 | return ret; | |
65931c8b MD |
664 | } |
665 | ||
b8aa1682 | 666 | /* |
b4aacfdc | 667 | * Stop all relayd and relayd-live threads. |
b8aa1682 | 668 | */ |
b4aacfdc | 669 | int lttng_relay_stop_threads(void) |
b8aa1682 | 670 | { |
b4aacfdc | 671 | int retval = 0; |
b8aa1682 JD |
672 | |
673 | /* Stopping all threads */ | |
674 | DBG("Terminating all threads"); | |
b4aacfdc | 675 | if (notify_thread_pipe(thread_quit_pipe[1])) { |
b8aa1682 | 676 | ERR("write error on thread quit pipe"); |
b4aacfdc | 677 | retval = -1; |
b8aa1682 JD |
678 | } |
679 | ||
b4aacfdc MD |
680 | if (notify_health_quit_pipe(health_quit_pipe)) { |
681 | ERR("write error on health quit pipe"); | |
682 | } | |
65931c8b | 683 | |
b8aa1682 | 684 | /* Dispatch thread */ |
26c9d55e | 685 | CMM_STORE_SHARED(dispatch_thread_exit, 1); |
58eb9381 | 686 | futex_nto1_wake(&relay_conn_queue.futex); |
178a0557 | 687 | |
b4aacfdc | 688 | if (relayd_live_stop()) { |
178a0557 | 689 | ERR("Error stopping live threads"); |
b4aacfdc | 690 | retval = -1; |
178a0557 | 691 | } |
b4aacfdc | 692 | return retval; |
b8aa1682 JD |
693 | } |
694 | ||
695 | /* | |
696 | * Signal handler for the daemon | |
697 | * | |
698 | * Simply stop all worker threads, leaving main() return gracefully after | |
699 | * joining all threads and calling cleanup(). | |
700 | */ | |
7591bab1 | 701 | static void sighandler(int sig) |
b8aa1682 JD |
702 | { |
703 | switch (sig) { | |
b8aa1682 JD |
704 | case SIGINT: |
705 | DBG("SIGINT caught"); | |
b4aacfdc MD |
706 | if (lttng_relay_stop_threads()) { |
707 | ERR("Error stopping threads"); | |
708 | } | |
b8aa1682 JD |
709 | break; |
710 | case SIGTERM: | |
711 | DBG("SIGTERM caught"); | |
b4aacfdc MD |
712 | if (lttng_relay_stop_threads()) { |
713 | ERR("Error stopping threads"); | |
714 | } | |
b8aa1682 | 715 | break; |
3fd27398 MD |
716 | case SIGUSR1: |
717 | CMM_STORE_SHARED(recv_child_signal, 1); | |
718 | break; | |
b8aa1682 JD |
719 | default: |
720 | break; | |
721 | } | |
722 | } | |
723 | ||
724 | /* | |
725 | * Setup signal handler for : | |
726 | * SIGINT, SIGTERM, SIGPIPE | |
727 | */ | |
7591bab1 | 728 | static int set_signal_handler(void) |
b8aa1682 JD |
729 | { |
730 | int ret = 0; | |
731 | struct sigaction sa; | |
732 | sigset_t sigset; | |
733 | ||
734 | if ((ret = sigemptyset(&sigset)) < 0) { | |
735 | PERROR("sigemptyset"); | |
736 | return ret; | |
737 | } | |
738 | ||
b8aa1682 JD |
739 | sa.sa_mask = sigset; |
740 | sa.sa_flags = 0; | |
0072e5e2 MD |
741 | |
742 | sa.sa_handler = sighandler; | |
b8aa1682 JD |
743 | if ((ret = sigaction(SIGTERM, &sa, NULL)) < 0) { |
744 | PERROR("sigaction"); | |
745 | return ret; | |
746 | } | |
747 | ||
748 | if ((ret = sigaction(SIGINT, &sa, NULL)) < 0) { | |
749 | PERROR("sigaction"); | |
750 | return ret; | |
751 | } | |
752 | ||
0072e5e2 | 753 | if ((ret = sigaction(SIGUSR1, &sa, NULL)) < 0) { |
b8aa1682 JD |
754 | PERROR("sigaction"); |
755 | return ret; | |
756 | } | |
757 | ||
0072e5e2 MD |
758 | sa.sa_handler = SIG_IGN; |
759 | if ((ret = sigaction(SIGPIPE, &sa, NULL)) < 0) { | |
3fd27398 MD |
760 | PERROR("sigaction"); |
761 | return ret; | |
762 | } | |
763 | ||
764 | DBG("Signal handler set for SIGTERM, SIGUSR1, SIGPIPE and SIGINT"); | |
b8aa1682 JD |
765 | |
766 | return ret; | |
767 | } | |
768 | ||
3fd27398 MD |
769 | void lttng_relay_notify_ready(void) |
770 | { | |
771 | /* Notify the parent of the fork() process that we are ready. */ | |
772 | if (opt_daemon || opt_background) { | |
773 | if (uatomic_sub_return(<tng_relay_ready, 1) == 0) { | |
774 | kill(child_ppid, SIGUSR1); | |
775 | } | |
776 | } | |
777 | } | |
778 | ||
b8aa1682 JD |
779 | /* |
780 | * Init thread quit pipe. | |
781 | * | |
782 | * Return -1 on error or 0 if all pipes are created. | |
783 | */ | |
7591bab1 | 784 | static int init_thread_quit_pipe(void) |
b8aa1682 | 785 | { |
e5e4e274 JG |
786 | return fd_tracker_util_pipe_open_cloexec(the_fd_tracker, |
787 | "Quit pipe", thread_quit_pipe); | |
b8aa1682 JD |
788 | } |
789 | ||
98398092 JG |
790 | /* |
791 | * Init health quit pipe. | |
792 | * | |
793 | * Return -1 on error or 0 if all pipes are created. | |
794 | */ | |
795 | static int init_health_quit_pipe(void) | |
796 | { | |
797 | return fd_tracker_util_pipe_open_cloexec(the_fd_tracker, | |
798 | "Health quit pipe", health_quit_pipe); | |
799 | } | |
800 | ||
b8aa1682 JD |
801 | /* |
802 | * Create a poll set with O_CLOEXEC and add the thread quit pipe to the set. | |
803 | */ | |
945459f8 JG |
804 | static int create_named_thread_poll_set(struct lttng_poll_event *events, |
805 | int size, const char *name) | |
b8aa1682 JD |
806 | { |
807 | int ret; | |
808 | ||
809 | if (events == NULL || size == 0) { | |
810 | ret = -1; | |
811 | goto error; | |
812 | } | |
813 | ||
945459f8 JG |
814 | ret = fd_tracker_util_poll_create(the_fd_tracker, |
815 | name, events, 1, LTTNG_CLOEXEC); | |
b8aa1682 JD |
816 | |
817 | /* Add quit pipe */ | |
c7759e6a | 818 | ret = lttng_poll_add(events, thread_quit_pipe[0], LPOLLIN | LPOLLERR); |
b8aa1682 JD |
819 | if (ret < 0) { |
820 | goto error; | |
821 | } | |
822 | ||
823 | return 0; | |
824 | ||
825 | error: | |
826 | return ret; | |
827 | } | |
828 | ||
829 | /* | |
830 | * Check if the thread quit pipe was triggered. | |
831 | * | |
832 | * Return 1 if it was triggered else 0; | |
833 | */ | |
7591bab1 | 834 | static int check_thread_quit_pipe(int fd, uint32_t events) |
b8aa1682 JD |
835 | { |
836 | if (fd == thread_quit_pipe[0] && (events & LPOLLIN)) { | |
837 | return 1; | |
838 | } | |
839 | ||
840 | return 0; | |
841 | } | |
842 | ||
843 | /* | |
844 | * Create and init socket from uri. | |
845 | */ | |
7591bab1 | 846 | static struct lttcomm_sock *relay_socket_create(struct lttng_uri *uri) |
b8aa1682 JD |
847 | { |
848 | int ret; | |
849 | struct lttcomm_sock *sock = NULL; | |
850 | ||
851 | sock = lttcomm_alloc_sock_from_uri(uri); | |
852 | if (sock == NULL) { | |
853 | ERR("Allocating socket"); | |
854 | goto error; | |
855 | } | |
856 | ||
857 | ret = lttcomm_create_sock(sock); | |
858 | if (ret < 0) { | |
859 | goto error; | |
860 | } | |
861 | DBG("Listening on sock %d", sock->fd); | |
862 | ||
863 | ret = sock->ops->bind(sock); | |
864 | if (ret < 0) { | |
865 | goto error; | |
866 | } | |
867 | ||
868 | ret = sock->ops->listen(sock, -1); | |
869 | if (ret < 0) { | |
870 | goto error; | |
871 | ||
872 | } | |
873 | ||
874 | return sock; | |
875 | ||
876 | error: | |
877 | if (sock) { | |
878 | lttcomm_destroy_sock(sock); | |
879 | } | |
880 | return NULL; | |
881 | } | |
882 | ||
883 | /* | |
884 | * This thread manages the listening for new connections on the network | |
885 | */ | |
7591bab1 | 886 | static void *relay_thread_listener(void *data) |
b8aa1682 | 887 | { |
095a4ae5 | 888 | int i, ret, pollfd, err = -1; |
b8aa1682 JD |
889 | uint32_t revents, nb_fd; |
890 | struct lttng_poll_event events; | |
891 | struct lttcomm_sock *control_sock, *data_sock; | |
892 | ||
b8aa1682 JD |
893 | DBG("[thread] Relay listener started"); |
894 | ||
55706a7d MD |
895 | health_register(health_relayd, HEALTH_RELAYD_TYPE_LISTENER); |
896 | ||
f385ae0a MD |
897 | health_code_update(); |
898 | ||
7591bab1 | 899 | control_sock = relay_socket_create(control_uri); |
b8aa1682 | 900 | if (!control_sock) { |
095a4ae5 | 901 | goto error_sock_control; |
b8aa1682 JD |
902 | } |
903 | ||
7591bab1 | 904 | data_sock = relay_socket_create(data_uri); |
b8aa1682 | 905 | if (!data_sock) { |
095a4ae5 | 906 | goto error_sock_relay; |
b8aa1682 JD |
907 | } |
908 | ||
909 | /* | |
7591bab1 MD |
910 | * Pass 3 as size here for the thread quit pipe, control and |
911 | * data socket. | |
b8aa1682 | 912 | */ |
0a9eff09 | 913 | ret = create_named_thread_poll_set(&events, 3, "Listener thread epoll"); |
b8aa1682 JD |
914 | if (ret < 0) { |
915 | goto error_create_poll; | |
916 | } | |
917 | ||
918 | /* Add the control socket */ | |
919 | ret = lttng_poll_add(&events, control_sock->fd, LPOLLIN | LPOLLRDHUP); | |
920 | if (ret < 0) { | |
921 | goto error_poll_add; | |
922 | } | |
923 | ||
924 | /* Add the data socket */ | |
925 | ret = lttng_poll_add(&events, data_sock->fd, LPOLLIN | LPOLLRDHUP); | |
926 | if (ret < 0) { | |
927 | goto error_poll_add; | |
928 | } | |
929 | ||
3fd27398 MD |
930 | lttng_relay_notify_ready(); |
931 | ||
9b5e0863 MD |
932 | if (testpoint(relayd_thread_listener)) { |
933 | goto error_testpoint; | |
934 | } | |
935 | ||
b8aa1682 | 936 | while (1) { |
f385ae0a MD |
937 | health_code_update(); |
938 | ||
b8aa1682 JD |
939 | DBG("Listener accepting connections"); |
940 | ||
b8aa1682 | 941 | restart: |
f385ae0a | 942 | health_poll_entry(); |
b8aa1682 | 943 | ret = lttng_poll_wait(&events, -1); |
f385ae0a | 944 | health_poll_exit(); |
b8aa1682 JD |
945 | if (ret < 0) { |
946 | /* | |
947 | * Restart interrupted system call. | |
948 | */ | |
949 | if (errno == EINTR) { | |
950 | goto restart; | |
951 | } | |
952 | goto error; | |
953 | } | |
954 | ||
0d9c5d77 DG |
955 | nb_fd = ret; |
956 | ||
b8aa1682 JD |
957 | DBG("Relay new connection received"); |
958 | for (i = 0; i < nb_fd; i++) { | |
f385ae0a MD |
959 | health_code_update(); |
960 | ||
b8aa1682 JD |
961 | /* Fetch once the poll data */ |
962 | revents = LTTNG_POLL_GETEV(&events, i); | |
963 | pollfd = LTTNG_POLL_GETFD(&events, i); | |
964 | ||
fd20dac9 | 965 | if (!revents) { |
7591bab1 MD |
966 | /* |
967 | * No activity for this FD (poll | |
968 | * implementation). | |
969 | */ | |
fd20dac9 MD |
970 | continue; |
971 | } | |
972 | ||
b8aa1682 JD |
973 | /* Thread quit pipe has been closed. Killing thread. */ |
974 | ret = check_thread_quit_pipe(pollfd, revents); | |
975 | if (ret) { | |
095a4ae5 MD |
976 | err = 0; |
977 | goto exit; | |
b8aa1682 JD |
978 | } |
979 | ||
03e43155 | 980 | if (revents & LPOLLIN) { |
4b7f17b2 | 981 | /* |
7591bab1 MD |
982 | * A new connection is requested, therefore a |
983 | * sessiond/consumerd connection is allocated in | |
984 | * this thread, enqueued to a global queue and | |
985 | * dequeued (and freed) in the worker thread. | |
4b7f17b2 | 986 | */ |
58eb9381 DG |
987 | int val = 1; |
988 | struct relay_connection *new_conn; | |
4b7f17b2 | 989 | struct lttcomm_sock *newsock; |
7591bab1 | 990 | enum connection_type type; |
b8aa1682 JD |
991 | |
992 | if (pollfd == data_sock->fd) { | |
7591bab1 | 993 | type = RELAY_DATA; |
b8aa1682 | 994 | newsock = data_sock->ops->accept(data_sock); |
58eb9381 DG |
995 | DBG("Relay data connection accepted, socket %d", |
996 | newsock->fd); | |
4b7f17b2 MD |
997 | } else { |
998 | assert(pollfd == control_sock->fd); | |
7591bab1 | 999 | type = RELAY_CONTROL; |
b8aa1682 | 1000 | newsock = control_sock->ops->accept(control_sock); |
58eb9381 DG |
1001 | DBG("Relay control connection accepted, socket %d", |
1002 | newsock->fd); | |
b8aa1682 | 1003 | } |
58eb9381 DG |
1004 | if (!newsock) { |
1005 | PERROR("accepting sock"); | |
58eb9381 DG |
1006 | goto error; |
1007 | } | |
1008 | ||
1009 | ret = setsockopt(newsock->fd, SOL_SOCKET, SO_REUSEADDR, &val, | |
1010 | sizeof(val)); | |
b8aa1682 JD |
1011 | if (ret < 0) { |
1012 | PERROR("setsockopt inet"); | |
4b7f17b2 | 1013 | lttcomm_destroy_sock(newsock); |
b8aa1682 JD |
1014 | goto error; |
1015 | } | |
2fc6b1ab JR |
1016 | |
1017 | ret = socket_apply_keep_alive_config(newsock->fd); | |
1018 | if (ret < 0) { | |
1019 | ERR("Failed to apply TCP keep-alive configuration on socket (%i)", | |
1020 | newsock->fd); | |
1021 | lttcomm_destroy_sock(newsock); | |
1022 | goto error; | |
1023 | } | |
1024 | ||
7591bab1 MD |
1025 | new_conn = connection_create(newsock, type); |
1026 | if (!new_conn) { | |
1027 | lttcomm_destroy_sock(newsock); | |
1028 | goto error; | |
1029 | } | |
58eb9381 DG |
1030 | |
1031 | /* Enqueue request for the dispatcher thread. */ | |
8bdee6e2 SM |
1032 | cds_wfcq_enqueue(&relay_conn_queue.head, &relay_conn_queue.tail, |
1033 | &new_conn->qnode); | |
b8aa1682 JD |
1034 | |
1035 | /* | |
7591bab1 MD |
1036 | * Wake the dispatch queue futex. |
1037 | * Implicit memory barrier with the | |
1038 | * exchange in cds_wfcq_enqueue. | |
b8aa1682 | 1039 | */ |
58eb9381 | 1040 | futex_nto1_wake(&relay_conn_queue.futex); |
03e43155 MD |
1041 | } else if (revents & (LPOLLERR | LPOLLHUP | LPOLLRDHUP)) { |
1042 | ERR("socket poll error"); | |
1043 | goto error; | |
1044 | } else { | |
1045 | ERR("Unexpected poll events %u for sock %d", revents, pollfd); | |
1046 | goto error; | |
b8aa1682 JD |
1047 | } |
1048 | } | |
1049 | } | |
1050 | ||
095a4ae5 | 1051 | exit: |
b8aa1682 JD |
1052 | error: |
1053 | error_poll_add: | |
9b5e0863 | 1054 | error_testpoint: |
0a9eff09 | 1055 | (void) fd_tracker_util_poll_clean(the_fd_tracker, &events); |
b8aa1682 | 1056 | error_create_poll: |
095a4ae5 MD |
1057 | if (data_sock->fd >= 0) { |
1058 | ret = data_sock->ops->close(data_sock); | |
b8aa1682 JD |
1059 | if (ret) { |
1060 | PERROR("close"); | |
1061 | } | |
b8aa1682 | 1062 | } |
095a4ae5 MD |
1063 | lttcomm_destroy_sock(data_sock); |
1064 | error_sock_relay: | |
1065 | if (control_sock->fd >= 0) { | |
1066 | ret = control_sock->ops->close(control_sock); | |
b8aa1682 JD |
1067 | if (ret) { |
1068 | PERROR("close"); | |
1069 | } | |
b8aa1682 | 1070 | } |
095a4ae5 MD |
1071 | lttcomm_destroy_sock(control_sock); |
1072 | error_sock_control: | |
1073 | if (err) { | |
f385ae0a MD |
1074 | health_error(); |
1075 | ERR("Health error occurred in %s", __func__); | |
095a4ae5 | 1076 | } |
55706a7d | 1077 | health_unregister(health_relayd); |
b8aa1682 | 1078 | DBG("Relay listener thread cleanup complete"); |
b4aacfdc | 1079 | lttng_relay_stop_threads(); |
b8aa1682 JD |
1080 | return NULL; |
1081 | } | |
1082 | ||
1083 | /* | |
1084 | * This thread manages the dispatching of the requests to worker threads | |
1085 | */ | |
7591bab1 | 1086 | static void *relay_thread_dispatcher(void *data) |
b8aa1682 | 1087 | { |
6cd525e8 MD |
1088 | int err = -1; |
1089 | ssize_t ret; | |
8bdee6e2 | 1090 | struct cds_wfcq_node *node; |
58eb9381 | 1091 | struct relay_connection *new_conn = NULL; |
b8aa1682 JD |
1092 | |
1093 | DBG("[thread] Relay dispatcher started"); | |
1094 | ||
55706a7d MD |
1095 | health_register(health_relayd, HEALTH_RELAYD_TYPE_DISPATCHER); |
1096 | ||
9b5e0863 MD |
1097 | if (testpoint(relayd_thread_dispatcher)) { |
1098 | goto error_testpoint; | |
1099 | } | |
1100 | ||
f385ae0a MD |
1101 | health_code_update(); |
1102 | ||
68924dc1 | 1103 | for (;;) { |
f385ae0a MD |
1104 | health_code_update(); |
1105 | ||
b8aa1682 | 1106 | /* Atomically prepare the queue futex */ |
58eb9381 | 1107 | futex_nto1_prepare(&relay_conn_queue.futex); |
b8aa1682 | 1108 | |
68924dc1 MD |
1109 | if (CMM_LOAD_SHARED(dispatch_thread_exit)) { |
1110 | break; | |
1111 | } | |
1112 | ||
b8aa1682 | 1113 | do { |
f385ae0a MD |
1114 | health_code_update(); |
1115 | ||
b8aa1682 | 1116 | /* Dequeue commands */ |
8bdee6e2 SM |
1117 | node = cds_wfcq_dequeue_blocking(&relay_conn_queue.head, |
1118 | &relay_conn_queue.tail); | |
b8aa1682 JD |
1119 | if (node == NULL) { |
1120 | DBG("Woken up but nothing in the relay command queue"); | |
1121 | /* Continue thread execution */ | |
1122 | break; | |
1123 | } | |
58eb9381 | 1124 | new_conn = caa_container_of(node, struct relay_connection, qnode); |
b8aa1682 | 1125 | |
58eb9381 | 1126 | DBG("Dispatching request waiting on sock %d", new_conn->sock->fd); |
b8aa1682 JD |
1127 | |
1128 | /* | |
7591bab1 MD |
1129 | * Inform worker thread of the new request. This |
1130 | * call is blocking so we can be assured that | |
1131 | * the data will be read at some point in time | |
1132 | * or wait to the end of the world :) | |
b8aa1682 | 1133 | */ |
58eb9381 DG |
1134 | ret = lttng_write(relay_conn_pipe[1], &new_conn, sizeof(new_conn)); |
1135 | if (ret < 0) { | |
1136 | PERROR("write connection pipe"); | |
7591bab1 | 1137 | connection_put(new_conn); |
b8aa1682 JD |
1138 | goto error; |
1139 | } | |
1140 | } while (node != NULL); | |
1141 | ||
1142 | /* Futex wait on queue. Blocking call on futex() */ | |
f385ae0a | 1143 | health_poll_entry(); |
58eb9381 | 1144 | futex_nto1_wait(&relay_conn_queue.futex); |
f385ae0a | 1145 | health_poll_exit(); |
b8aa1682 JD |
1146 | } |
1147 | ||
f385ae0a MD |
1148 | /* Normal exit, no error */ |
1149 | err = 0; | |
1150 | ||
b8aa1682 | 1151 | error: |
9b5e0863 | 1152 | error_testpoint: |
f385ae0a MD |
1153 | if (err) { |
1154 | health_error(); | |
1155 | ERR("Health error occurred in %s", __func__); | |
1156 | } | |
55706a7d | 1157 | health_unregister(health_relayd); |
b8aa1682 | 1158 | DBG("Dispatch thread dying"); |
b4aacfdc | 1159 | lttng_relay_stop_threads(); |
b8aa1682 JD |
1160 | return NULL; |
1161 | } | |
1162 | ||
b8aa1682 | 1163 | /* |
7591bab1 | 1164 | * Set index data from the control port to a given index object. |
b8aa1682 | 1165 | */ |
7591bab1 | 1166 | static int set_index_control_data(struct relay_index *index, |
234cd636 JD |
1167 | struct lttcomm_relayd_index *data, |
1168 | struct relay_connection *conn) | |
1c20f0e2 | 1169 | { |
7591bab1 | 1170 | struct ctf_packet_index index_data; |
1c20f0e2 JD |
1171 | |
1172 | /* | |
44a2fbf1 | 1173 | * The index on disk is encoded in big endian. |
1c20f0e2 | 1174 | */ |
44a2fbf1 JG |
1175 | index_data.packet_size = htobe64(data->packet_size); |
1176 | index_data.content_size = htobe64(data->content_size); | |
1177 | index_data.timestamp_begin = htobe64(data->timestamp_begin); | |
1178 | index_data.timestamp_end = htobe64(data->timestamp_end); | |
1179 | index_data.events_discarded = htobe64(data->events_discarded); | |
1180 | index_data.stream_id = htobe64(data->stream_id); | |
234cd636 JD |
1181 | |
1182 | if (conn->minor >= 8) { | |
44a2fbf1 JG |
1183 | index->index_data.stream_instance_id = htobe64(data->stream_instance_id); |
1184 | index->index_data.packet_seq_num = htobe64(data->packet_seq_num); | |
234cd636 JD |
1185 | } |
1186 | ||
7591bab1 | 1187 | return relay_index_set_data(index, &index_data); |
1c20f0e2 JD |
1188 | } |
1189 | ||
c5b6f4f0 DG |
1190 | /* |
1191 | * Handle the RELAYD_CREATE_SESSION command. | |
1192 | * | |
1193 | * On success, send back the session id or else return a negative value. | |
1194 | */ | |
44a2fbf1 JG |
1195 | static int relay_create_session(const struct lttcomm_relayd_hdr *recv_hdr, |
1196 | struct relay_connection *conn, | |
1197 | const struct lttng_buffer_view *payload) | |
c5b6f4f0 | 1198 | { |
44a2fbf1 JG |
1199 | int ret = 0; |
1200 | ssize_t send_ret; | |
c5b6f4f0 DG |
1201 | struct relay_session *session; |
1202 | struct lttcomm_relayd_status_session reply; | |
36d2e35d | 1203 | char session_name[LTTNG_NAME_MAX]; |
9cf3eb20 | 1204 | char hostname[LTTNG_HOST_NAME_MAX]; |
7591bab1 MD |
1205 | uint32_t live_timer = 0; |
1206 | bool snapshot = false; | |
c5b6f4f0 | 1207 | |
36d2e35d | 1208 | memset(session_name, 0, LTTNG_NAME_MAX); |
9cf3eb20 | 1209 | memset(hostname, 0, LTTNG_HOST_NAME_MAX); |
c5b6f4f0 DG |
1210 | |
1211 | memset(&reply, 0, sizeof(reply)); | |
1212 | ||
58eb9381 | 1213 | switch (conn->minor) { |
2a174661 DG |
1214 | case 1: |
1215 | case 2: | |
1216 | case 3: | |
1217 | break; | |
1218 | case 4: /* LTTng sessiond 2.4 */ | |
1219 | default: | |
44a2fbf1 | 1220 | ret = cmd_create_session_2_4(payload, session_name, |
7591bab1 MD |
1221 | hostname, &live_timer, &snapshot); |
1222 | } | |
1223 | if (ret < 0) { | |
1224 | goto send_reply; | |
d3e2ba59 JD |
1225 | } |
1226 | ||
7591bab1 MD |
1227 | session = session_create(session_name, hostname, live_timer, |
1228 | snapshot, conn->major, conn->minor); | |
1229 | if (!session) { | |
1230 | ret = -1; | |
1231 | goto send_reply; | |
1232 | } | |
1233 | assert(!conn->session); | |
1234 | conn->session = session; | |
c5b6f4f0 DG |
1235 | DBG("Created session %" PRIu64, session->id); |
1236 | ||
7591bab1 MD |
1237 | reply.session_id = htobe64(session->id); |
1238 | ||
1239 | send_reply: | |
c5b6f4f0 DG |
1240 | if (ret < 0) { |
1241 | reply.ret_code = htobe32(LTTNG_ERR_FATAL); | |
1242 | } else { | |
1243 | reply.ret_code = htobe32(LTTNG_OK); | |
1244 | } | |
1245 | ||
58eb9381 | 1246 | send_ret = conn->sock->ops->sendmsg(conn->sock, &reply, sizeof(reply), 0); |
44a2fbf1 JG |
1247 | if (send_ret < (ssize_t) sizeof(reply)) { |
1248 | ERR("Failed to send \"create session\" command reply (ret = %zd)", | |
1249 | send_ret); | |
1250 | ret = -1; | |
c5b6f4f0 DG |
1251 | } |
1252 | ||
1253 | return ret; | |
1254 | } | |
1255 | ||
a4baae1b JD |
1256 | /* |
1257 | * When we have received all the streams and the metadata for a channel, | |
1258 | * we make them visible to the viewer threads. | |
1259 | */ | |
7591bab1 | 1260 | static void publish_connection_local_streams(struct relay_connection *conn) |
a4baae1b | 1261 | { |
7591bab1 MD |
1262 | struct relay_stream *stream; |
1263 | struct relay_session *session = conn->session; | |
a4baae1b | 1264 | |
7591bab1 MD |
1265 | /* |
1266 | * We publish all streams belonging to a session atomically wrt | |
1267 | * session lock. | |
1268 | */ | |
1269 | pthread_mutex_lock(&session->lock); | |
1270 | rcu_read_lock(); | |
1271 | cds_list_for_each_entry_rcu(stream, &session->recv_list, | |
1272 | recv_node) { | |
1273 | stream_publish(stream); | |
a4baae1b | 1274 | } |
7591bab1 | 1275 | rcu_read_unlock(); |
a4baae1b | 1276 | |
7591bab1 MD |
1277 | /* |
1278 | * Inform the viewer that there are new streams in the session. | |
1279 | */ | |
1280 | if (session->viewer_attached) { | |
1281 | uatomic_set(&session->new_streams, 1); | |
1282 | } | |
1283 | pthread_mutex_unlock(&session->lock); | |
a4baae1b JD |
1284 | } |
1285 | ||
b8aa1682 JD |
1286 | /* |
1287 | * relay_add_stream: allocate a new stream for a session | |
1288 | */ | |
44a2fbf1 JG |
1289 | static int relay_add_stream(const struct lttcomm_relayd_hdr *recv_hdr, |
1290 | struct relay_connection *conn, | |
1291 | const struct lttng_buffer_view *payload) | |
b8aa1682 | 1292 | { |
7591bab1 MD |
1293 | int ret; |
1294 | ssize_t send_ret; | |
58eb9381 | 1295 | struct relay_session *session = conn->session; |
b8aa1682 JD |
1296 | struct relay_stream *stream = NULL; |
1297 | struct lttcomm_relayd_status_stream reply; | |
4030a636 | 1298 | struct ctf_trace *trace = NULL; |
7591bab1 MD |
1299 | uint64_t stream_handle = -1ULL; |
1300 | char *path_name = NULL, *channel_name = NULL; | |
1301 | uint64_t tracefile_size = 0, tracefile_count = 0; | |
b8aa1682 | 1302 | |
44a2fbf1 | 1303 | if (!session || !conn->version_check_done) { |
b8aa1682 JD |
1304 | ERR("Trying to add a stream before version check"); |
1305 | ret = -1; | |
1306 | goto end_no_session; | |
1307 | } | |
1308 | ||
7591bab1 MD |
1309 | switch (session->minor) { |
1310 | case 1: /* LTTng sessiond 2.1. Allocates path_name and channel_name. */ | |
44a2fbf1 | 1311 | ret = cmd_recv_stream_2_1(payload, &path_name, |
b4d507ca | 1312 | &channel_name, session); |
0f907de1 | 1313 | break; |
7591bab1 | 1314 | case 2: /* LTTng sessiond 2.2. Allocates path_name and channel_name. */ |
0f907de1 | 1315 | default: |
44a2fbf1 | 1316 | ret = cmd_recv_stream_2_2(payload, &path_name, |
b4d507ca JR |
1317 | &channel_name, &tracefile_size, &tracefile_count, |
1318 | session); | |
0f907de1 JD |
1319 | break; |
1320 | } | |
1321 | if (ret < 0) { | |
7591bab1 | 1322 | goto send_reply; |
b8aa1682 JD |
1323 | } |
1324 | ||
7591bab1 | 1325 | trace = ctf_trace_get_by_path_or_create(session, path_name); |
2a174661 | 1326 | if (!trace) { |
7591bab1 | 1327 | goto send_reply; |
2a174661 | 1328 | } |
7591bab1 | 1329 | /* This stream here has one reference on the trace. */ |
2a174661 | 1330 | |
7591bab1 MD |
1331 | pthread_mutex_lock(&last_relay_stream_id_lock); |
1332 | stream_handle = ++last_relay_stream_id; | |
1333 | pthread_mutex_unlock(&last_relay_stream_id_lock); | |
d3e2ba59 | 1334 | |
7591bab1 MD |
1335 | /* We pass ownership of path_name and channel_name. */ |
1336 | stream = stream_create(trace, stream_handle, path_name, | |
1337 | channel_name, tracefile_size, tracefile_count); | |
1338 | path_name = NULL; | |
1339 | channel_name = NULL; | |
a4baae1b | 1340 | |
2a174661 | 1341 | /* |
7591bab1 MD |
1342 | * Streams are the owners of their trace. Reference to trace is |
1343 | * kept within stream_create(). | |
2a174661 | 1344 | */ |
7591bab1 | 1345 | ctf_trace_put(trace); |
d3e2ba59 | 1346 | |
7591bab1 | 1347 | send_reply: |
53efb85a | 1348 | memset(&reply, 0, sizeof(reply)); |
7591bab1 MD |
1349 | reply.handle = htobe64(stream_handle); |
1350 | if (!stream) { | |
f73fabfd | 1351 | reply.ret_code = htobe32(LTTNG_ERR_UNK); |
b8aa1682 | 1352 | } else { |
f73fabfd | 1353 | reply.ret_code = htobe32(LTTNG_OK); |
b8aa1682 | 1354 | } |
5af40280 | 1355 | |
58eb9381 | 1356 | send_ret = conn->sock->ops->sendmsg(conn->sock, &reply, |
b8aa1682 | 1357 | sizeof(struct lttcomm_relayd_status_stream), 0); |
44a2fbf1 JG |
1358 | if (send_ret < (ssize_t) sizeof(reply)) { |
1359 | ERR("Failed to send \"add stream\" command reply (ret = %zd)", | |
1360 | send_ret); | |
1361 | ret = -1; | |
b8aa1682 JD |
1362 | } |
1363 | ||
1364 | end_no_session: | |
7591bab1 MD |
1365 | free(path_name); |
1366 | free(channel_name); | |
0f907de1 | 1367 | return ret; |
b8aa1682 JD |
1368 | } |
1369 | ||
173af62f DG |
1370 | /* |
1371 | * relay_close_stream: close a specific stream | |
1372 | */ | |
44a2fbf1 JG |
1373 | static int relay_close_stream(const struct lttcomm_relayd_hdr *recv_hdr, |
1374 | struct relay_connection *conn, | |
1375 | const struct lttng_buffer_view *payload) | |
173af62f | 1376 | { |
44a2fbf1 JG |
1377 | int ret; |
1378 | ssize_t send_ret; | |
58eb9381 | 1379 | struct relay_session *session = conn->session; |
173af62f DG |
1380 | struct lttcomm_relayd_close_stream stream_info; |
1381 | struct lttcomm_relayd_generic_reply reply; | |
1382 | struct relay_stream *stream; | |
173af62f DG |
1383 | |
1384 | DBG("Close stream received"); | |
1385 | ||
44a2fbf1 | 1386 | if (!session || !conn->version_check_done) { |
173af62f DG |
1387 | ERR("Trying to close a stream before version check"); |
1388 | ret = -1; | |
1389 | goto end_no_session; | |
1390 | } | |
1391 | ||
44a2fbf1 JG |
1392 | if (payload->size < sizeof(stream_info)) { |
1393 | ERR("Unexpected payload size in \"relay_close_stream\": expected >= %zu bytes, got %zu bytes", | |
1394 | sizeof(stream_info), payload->size); | |
173af62f DG |
1395 | ret = -1; |
1396 | goto end_no_session; | |
1397 | } | |
44a2fbf1 JG |
1398 | memcpy(&stream_info, payload->data, sizeof(stream_info)); |
1399 | stream_info.stream_id = be64toh(stream_info.stream_id); | |
1400 | stream_info.last_net_seq_num = be64toh(stream_info.last_net_seq_num); | |
173af62f | 1401 | |
44a2fbf1 | 1402 | stream = stream_get_by_id(stream_info.stream_id); |
173af62f DG |
1403 | if (!stream) { |
1404 | ret = -1; | |
7591bab1 | 1405 | goto end; |
173af62f | 1406 | } |
77f7bd85 MD |
1407 | |
1408 | /* | |
1409 | * Set last_net_seq_num before the close flag. Required by data | |
1410 | * pending check. | |
1411 | */ | |
7591bab1 | 1412 | pthread_mutex_lock(&stream->lock); |
44a2fbf1 | 1413 | stream->last_net_seq_num = stream_info.last_net_seq_num; |
77f7bd85 MD |
1414 | pthread_mutex_unlock(&stream->lock); |
1415 | ||
bda7c7b9 JG |
1416 | /* |
1417 | * This is one of the conditions which may trigger a stream close | |
1418 | * with the others being: | |
1419 | * 1) A close command is received for a stream | |
1420 | * 2) The control connection owning the stream is closed | |
1421 | * 3) We have received all of the stream's data _after_ a close | |
1422 | * request. | |
1423 | */ | |
1424 | try_stream_close(stream); | |
7591bab1 MD |
1425 | if (stream->is_metadata) { |
1426 | struct relay_viewer_stream *vstream; | |
173af62f | 1427 | |
7591bab1 MD |
1428 | vstream = viewer_stream_get_by_id(stream->stream_handle); |
1429 | if (vstream) { | |
1430 | if (vstream->metadata_sent == stream->metadata_received) { | |
1431 | /* | |
1432 | * Since all the metadata has been sent to the | |
1433 | * viewer and that we have a request to close | |
1434 | * its stream, we can safely teardown the | |
1435 | * corresponding metadata viewer stream. | |
1436 | */ | |
1437 | viewer_stream_put(vstream); | |
1438 | } | |
1439 | /* Put local reference. */ | |
1440 | viewer_stream_put(vstream); | |
1441 | } | |
1442 | } | |
7591bab1 | 1443 | stream_put(stream); |
44a2fbf1 | 1444 | ret = 0; |
173af62f | 1445 | |
7591bab1 | 1446 | end: |
53efb85a | 1447 | memset(&reply, 0, sizeof(reply)); |
173af62f | 1448 | if (ret < 0) { |
f73fabfd | 1449 | reply.ret_code = htobe32(LTTNG_ERR_UNK); |
173af62f | 1450 | } else { |
f73fabfd | 1451 | reply.ret_code = htobe32(LTTNG_OK); |
173af62f | 1452 | } |
58eb9381 | 1453 | send_ret = conn->sock->ops->sendmsg(conn->sock, &reply, |
173af62f | 1454 | sizeof(struct lttcomm_relayd_generic_reply), 0); |
44a2fbf1 JG |
1455 | if (send_ret < (ssize_t) sizeof(reply)) { |
1456 | ERR("Failed to send \"close stream\" command reply (ret = %zd)", | |
1457 | send_ret); | |
1458 | ret = -1; | |
173af62f DG |
1459 | } |
1460 | ||
1461 | end_no_session: | |
1462 | return ret; | |
1463 | } | |
1464 | ||
93ec662e JD |
1465 | /* |
1466 | * relay_reset_metadata: reset a metadata stream | |
1467 | */ | |
1468 | static | |
44a2fbf1 JG |
1469 | int relay_reset_metadata(const struct lttcomm_relayd_hdr *recv_hdr, |
1470 | struct relay_connection *conn, | |
1471 | const struct lttng_buffer_view *payload) | |
93ec662e | 1472 | { |
44a2fbf1 JG |
1473 | int ret; |
1474 | ssize_t send_ret; | |
93ec662e JD |
1475 | struct relay_session *session = conn->session; |
1476 | struct lttcomm_relayd_reset_metadata stream_info; | |
1477 | struct lttcomm_relayd_generic_reply reply; | |
1478 | struct relay_stream *stream; | |
1479 | ||
1480 | DBG("Reset metadata received"); | |
1481 | ||
44a2fbf1 | 1482 | if (!session || !conn->version_check_done) { |
93ec662e JD |
1483 | ERR("Trying to reset a metadata stream before version check"); |
1484 | ret = -1; | |
1485 | goto end_no_session; | |
1486 | } | |
1487 | ||
44a2fbf1 JG |
1488 | if (payload->size < sizeof(stream_info)) { |
1489 | ERR("Unexpected payload size in \"relay_reset_metadata\": expected >= %zu bytes, got %zu bytes", | |
1490 | sizeof(stream_info), payload->size); | |
93ec662e JD |
1491 | ret = -1; |
1492 | goto end_no_session; | |
1493 | } | |
44a2fbf1 JG |
1494 | memcpy(&stream_info, payload->data, sizeof(stream_info)); |
1495 | stream_info.stream_id = be64toh(stream_info.stream_id); | |
1496 | stream_info.version = be64toh(stream_info.version); | |
1497 | ||
1498 | DBG("Update metadata to version %" PRIu64, stream_info.version); | |
93ec662e JD |
1499 | |
1500 | /* Unsupported for live sessions for now. */ | |
1501 | if (session->live_timer != 0) { | |
1502 | ret = -1; | |
1503 | goto end; | |
1504 | } | |
1505 | ||
44a2fbf1 | 1506 | stream = stream_get_by_id(stream_info.stream_id); |
93ec662e JD |
1507 | if (!stream) { |
1508 | ret = -1; | |
1509 | goto end; | |
1510 | } | |
1511 | pthread_mutex_lock(&stream->lock); | |
1512 | if (!stream->is_metadata) { | |
1513 | ret = -1; | |
1514 | goto end_unlock; | |
1515 | } | |
1516 | ||
1517 | ret = utils_rotate_stream_file(stream->path_name, stream->channel_name, | |
1518 | 0, 0, -1, -1, stream->stream_fd->fd, NULL, | |
1519 | &stream->stream_fd->fd); | |
1520 | if (ret < 0) { | |
1521 | ERR("Failed to rotate metadata file %s of channel %s", | |
1522 | stream->path_name, stream->channel_name); | |
1523 | goto end_unlock; | |
1524 | } | |
1525 | ||
1526 | end_unlock: | |
1527 | pthread_mutex_unlock(&stream->lock); | |
1528 | stream_put(stream); | |
1529 | ||
1530 | end: | |
1531 | memset(&reply, 0, sizeof(reply)); | |
1532 | if (ret < 0) { | |
1533 | reply.ret_code = htobe32(LTTNG_ERR_UNK); | |
1534 | } else { | |
1535 | reply.ret_code = htobe32(LTTNG_OK); | |
1536 | } | |
1537 | send_ret = conn->sock->ops->sendmsg(conn->sock, &reply, | |
1538 | sizeof(struct lttcomm_relayd_generic_reply), 0); | |
44a2fbf1 JG |
1539 | if (send_ret < (ssize_t) sizeof(reply)) { |
1540 | ERR("Failed to send \"reset metadata\" command reply (ret = %zd)", | |
1541 | send_ret); | |
1542 | ret = -1; | |
93ec662e JD |
1543 | } |
1544 | ||
1545 | end_no_session: | |
1546 | return ret; | |
1547 | } | |
1548 | ||
b8aa1682 JD |
1549 | /* |
1550 | * relay_unknown_command: send -1 if received unknown command | |
1551 | */ | |
7591bab1 | 1552 | static void relay_unknown_command(struct relay_connection *conn) |
b8aa1682 JD |
1553 | { |
1554 | struct lttcomm_relayd_generic_reply reply; | |
44a2fbf1 | 1555 | ssize_t send_ret; |
b8aa1682 | 1556 | |
53efb85a | 1557 | memset(&reply, 0, sizeof(reply)); |
f73fabfd | 1558 | reply.ret_code = htobe32(LTTNG_ERR_UNK); |
44a2fbf1 JG |
1559 | send_ret = conn->sock->ops->sendmsg(conn->sock, &reply, sizeof(reply), 0); |
1560 | if (send_ret < sizeof(reply)) { | |
1561 | ERR("Failed to send \"unknown command\" command reply (ret = %zd)", send_ret); | |
b8aa1682 JD |
1562 | } |
1563 | } | |
1564 | ||
1565 | /* | |
1566 | * relay_start: send an acknowledgment to the client to tell if we are | |
1567 | * ready to receive data. We are ready if a session is established. | |
1568 | */ | |
44a2fbf1 JG |
1569 | static int relay_start(const struct lttcomm_relayd_hdr *recv_hdr, |
1570 | struct relay_connection *conn, | |
1571 | const struct lttng_buffer_view *payload) | |
b8aa1682 | 1572 | { |
44a2fbf1 JG |
1573 | int ret = 0; |
1574 | ssize_t send_ret; | |
b8aa1682 | 1575 | struct lttcomm_relayd_generic_reply reply; |
58eb9381 | 1576 | struct relay_session *session = conn->session; |
b8aa1682 JD |
1577 | |
1578 | if (!session) { | |
1579 | DBG("Trying to start the streaming without a session established"); | |
f73fabfd | 1580 | ret = htobe32(LTTNG_ERR_UNK); |
b8aa1682 JD |
1581 | } |
1582 | ||
53efb85a | 1583 | memset(&reply, 0, sizeof(reply)); |
44a2fbf1 JG |
1584 | reply.ret_code = htobe32(LTTNG_OK); |
1585 | send_ret = conn->sock->ops->sendmsg(conn->sock, &reply, | |
1586 | sizeof(reply), 0); | |
1587 | if (send_ret < (ssize_t) sizeof(reply)) { | |
1588 | ERR("Failed to send \"relay_start\" command reply (ret = %zd)", | |
1589 | send_ret); | |
1590 | ret = -1; | |
b8aa1682 JD |
1591 | } |
1592 | ||
1593 | return ret; | |
1594 | } | |
1595 | ||
1d4dfdef DG |
1596 | /* |
1597 | * Append padding to the file pointed by the file descriptor fd. | |
1598 | */ | |
1599 | static int write_padding_to_file(int fd, uint32_t size) | |
1600 | { | |
6cd525e8 | 1601 | ssize_t ret = 0; |
1d4dfdef DG |
1602 | char *zeros; |
1603 | ||
1604 | if (size == 0) { | |
1605 | goto end; | |
1606 | } | |
1607 | ||
1608 | zeros = zmalloc(size); | |
1609 | if (zeros == NULL) { | |
1610 | PERROR("zmalloc zeros for padding"); | |
1611 | ret = -1; | |
1612 | goto end; | |
1613 | } | |
1614 | ||
6cd525e8 MD |
1615 | ret = lttng_write(fd, zeros, size); |
1616 | if (ret < size) { | |
1d4dfdef DG |
1617 | PERROR("write padding to file"); |
1618 | } | |
1619 | ||
e986c7a1 DG |
1620 | free(zeros); |
1621 | ||
1d4dfdef DG |
1622 | end: |
1623 | return ret; | |
1624 | } | |
1625 | ||
b8aa1682 | 1626 | /* |
7591bab1 | 1627 | * relay_recv_metadata: receive the metadata for the session. |
b8aa1682 | 1628 | */ |
44a2fbf1 JG |
1629 | static int relay_recv_metadata(const struct lttcomm_relayd_hdr *recv_hdr, |
1630 | struct relay_connection *conn, | |
1631 | const struct lttng_buffer_view *payload) | |
b8aa1682 | 1632 | { |
32d1569c | 1633 | int ret = 0; |
6cd525e8 | 1634 | ssize_t size_ret; |
58eb9381 | 1635 | struct relay_session *session = conn->session; |
44a2fbf1 | 1636 | struct lttcomm_relayd_metadata_payload metadata_payload_header; |
b8aa1682 | 1637 | struct relay_stream *metadata_stream; |
44a2fbf1 | 1638 | uint64_t metadata_payload_size; |
b8aa1682 JD |
1639 | |
1640 | if (!session) { | |
1641 | ERR("Metadata sent before version check"); | |
1642 | ret = -1; | |
1643 | goto end; | |
1644 | } | |
1645 | ||
44a2fbf1 | 1646 | if (recv_hdr->data_size < sizeof(struct lttcomm_relayd_metadata_payload)) { |
f6416125 MD |
1647 | ERR("Incorrect data size"); |
1648 | ret = -1; | |
1649 | goto end; | |
1650 | } | |
44a2fbf1 JG |
1651 | metadata_payload_size = recv_hdr->data_size - |
1652 | sizeof(struct lttcomm_relayd_metadata_payload); | |
f6416125 | 1653 | |
44a2fbf1 JG |
1654 | memcpy(&metadata_payload_header, payload->data, |
1655 | sizeof(metadata_payload_header)); | |
1656 | metadata_payload_header.stream_id = be64toh( | |
1657 | metadata_payload_header.stream_id); | |
1658 | metadata_payload_header.padding_size = be32toh( | |
1659 | metadata_payload_header.padding_size); | |
c617c0c6 | 1660 | |
44a2fbf1 | 1661 | metadata_stream = stream_get_by_id(metadata_payload_header.stream_id); |
b8aa1682 JD |
1662 | if (!metadata_stream) { |
1663 | ret = -1; | |
7591bab1 | 1664 | goto end; |
b8aa1682 JD |
1665 | } |
1666 | ||
7591bab1 MD |
1667 | pthread_mutex_lock(&metadata_stream->lock); |
1668 | ||
44a2fbf1 JG |
1669 | size_ret = lttng_write(metadata_stream->stream_fd->fd, |
1670 | payload->data + sizeof(metadata_payload_header), | |
1671 | metadata_payload_size); | |
1672 | if (size_ret < metadata_payload_size) { | |
b8aa1682 JD |
1673 | ERR("Relay error writing metadata on file"); |
1674 | ret = -1; | |
7591bab1 | 1675 | goto end_put; |
b8aa1682 | 1676 | } |
1d4dfdef | 1677 | |
32d1569c | 1678 | size_ret = write_padding_to_file(metadata_stream->stream_fd->fd, |
44a2fbf1 JG |
1679 | metadata_payload_header.padding_size); |
1680 | if (size_ret < (int64_t) metadata_payload_header.padding_size) { | |
1681 | ret = -1; | |
7591bab1 | 1682 | goto end_put; |
1d4dfdef | 1683 | } |
2a174661 | 1684 | |
7591bab1 | 1685 | metadata_stream->metadata_received += |
44a2fbf1 | 1686 | metadata_payload_size + metadata_payload_header.padding_size; |
7591bab1 MD |
1687 | DBG2("Relay metadata written. Updated metadata_received %" PRIu64, |
1688 | metadata_stream->metadata_received); | |
1d4dfdef | 1689 | |
7591bab1 MD |
1690 | end_put: |
1691 | pthread_mutex_unlock(&metadata_stream->lock); | |
1692 | stream_put(metadata_stream); | |
b8aa1682 JD |
1693 | end: |
1694 | return ret; | |
1695 | } | |
1696 | ||
1697 | /* | |
1698 | * relay_send_version: send relayd version number | |
1699 | */ | |
44a2fbf1 JG |
1700 | static int relay_send_version(const struct lttcomm_relayd_hdr *recv_hdr, |
1701 | struct relay_connection *conn, | |
1702 | const struct lttng_buffer_view *payload) | |
b8aa1682 | 1703 | { |
7f51dcba | 1704 | int ret; |
44a2fbf1 | 1705 | ssize_t send_ret; |
092b6259 | 1706 | struct lttcomm_relayd_version reply, msg; |
40096142 | 1707 | bool compatible = true; |
b8aa1682 | 1708 | |
44a2fbf1 | 1709 | conn->version_check_done = true; |
b8aa1682 | 1710 | |
092b6259 | 1711 | /* Get version from the other side. */ |
44a2fbf1 JG |
1712 | if (payload->size < sizeof(msg)) { |
1713 | ERR("Unexpected payload size in \"relay_send_version\": expected >= %zu bytes, got %zu bytes", | |
1714 | sizeof(msg), payload->size); | |
092b6259 | 1715 | ret = -1; |
092b6259 DG |
1716 | goto end; |
1717 | } | |
1718 | ||
44a2fbf1 JG |
1719 | memcpy(&msg, payload->data, sizeof(msg)); |
1720 | msg.major = be32toh(msg.major); | |
1721 | msg.minor = be32toh(msg.minor); | |
1722 | ||
53efb85a | 1723 | memset(&reply, 0, sizeof(reply)); |
d83a952c MD |
1724 | reply.major = RELAYD_VERSION_COMM_MAJOR; |
1725 | reply.minor = RELAYD_VERSION_COMM_MINOR; | |
d4519fa3 JD |
1726 | |
1727 | /* Major versions must be the same */ | |
44a2fbf1 | 1728 | if (reply.major != msg.major) { |
6151a90f | 1729 | DBG("Incompatible major versions (%u vs %u), deleting session", |
44a2fbf1 | 1730 | reply.major, msg.major); |
40096142 | 1731 | compatible = false; |
d4519fa3 JD |
1732 | } |
1733 | ||
58eb9381 | 1734 | conn->major = reply.major; |
0f907de1 | 1735 | /* We adapt to the lowest compatible version */ |
44a2fbf1 | 1736 | if (reply.minor <= msg.minor) { |
58eb9381 | 1737 | conn->minor = reply.minor; |
0f907de1 | 1738 | } else { |
44a2fbf1 | 1739 | conn->minor = msg.minor; |
0f907de1 JD |
1740 | } |
1741 | ||
6151a90f JD |
1742 | reply.major = htobe32(reply.major); |
1743 | reply.minor = htobe32(reply.minor); | |
44a2fbf1 JG |
1744 | send_ret = conn->sock->ops->sendmsg(conn->sock, &reply, |
1745 | sizeof(reply), 0); | |
1746 | if (send_ret < (ssize_t) sizeof(reply)) { | |
1747 | ERR("Failed to send \"send version\" command reply (ret = %zd)", | |
1748 | send_ret); | |
1749 | ret = -1; | |
1750 | goto end; | |
1751 | } else { | |
1752 | ret = 0; | |
6151a90f JD |
1753 | } |
1754 | ||
40096142 JD |
1755 | if (!compatible) { |
1756 | ret = -1; | |
1757 | goto end; | |
1758 | } | |
1759 | ||
58eb9381 DG |
1760 | DBG("Version check done using protocol %u.%u", conn->major, |
1761 | conn->minor); | |
b8aa1682 JD |
1762 | |
1763 | end: | |
1764 | return ret; | |
1765 | } | |
1766 | ||
c8f59ee5 | 1767 | /* |
6d805429 | 1768 | * Check for data pending for a given stream id from the session daemon. |
c8f59ee5 | 1769 | */ |
44a2fbf1 JG |
1770 | static int relay_data_pending(const struct lttcomm_relayd_hdr *recv_hdr, |
1771 | struct relay_connection *conn, | |
1772 | const struct lttng_buffer_view *payload) | |
c8f59ee5 | 1773 | { |
58eb9381 | 1774 | struct relay_session *session = conn->session; |
6d805429 | 1775 | struct lttcomm_relayd_data_pending msg; |
c8f59ee5 DG |
1776 | struct lttcomm_relayd_generic_reply reply; |
1777 | struct relay_stream *stream; | |
44a2fbf1 | 1778 | ssize_t send_ret; |
c8f59ee5 | 1779 | int ret; |
c8f59ee5 | 1780 | |
6d805429 | 1781 | DBG("Data pending command received"); |
c8f59ee5 | 1782 | |
44a2fbf1 | 1783 | if (!session || !conn->version_check_done) { |
c8f59ee5 DG |
1784 | ERR("Trying to check for data before version check"); |
1785 | ret = -1; | |
1786 | goto end_no_session; | |
1787 | } | |
1788 | ||
44a2fbf1 JG |
1789 | if (payload->size < sizeof(msg)) { |
1790 | ERR("Unexpected payload size in \"relay_data_pending\": expected >= %zu bytes, got %zu bytes", | |
1791 | sizeof(msg), payload->size); | |
c8f59ee5 DG |
1792 | ret = -1; |
1793 | goto end_no_session; | |
1794 | } | |
44a2fbf1 JG |
1795 | memcpy(&msg, payload->data, sizeof(msg)); |
1796 | msg.stream_id = be64toh(msg.stream_id); | |
1797 | msg.last_net_seq_num = be64toh(msg.last_net_seq_num); | |
c8f59ee5 | 1798 | |
44a2fbf1 | 1799 | stream = stream_get_by_id(msg.stream_id); |
de91f48a | 1800 | if (stream == NULL) { |
c8f59ee5 | 1801 | ret = -1; |
7591bab1 | 1802 | goto end; |
c8f59ee5 DG |
1803 | } |
1804 | ||
7591bab1 MD |
1805 | pthread_mutex_lock(&stream->lock); |
1806 | ||
6d805429 | 1807 | DBG("Data pending for stream id %" PRIu64 " prev_seq %" PRIu64 |
44a2fbf1 JG |
1808 | " and last_seq %" PRIu64, msg.stream_id, |
1809 | stream->prev_seq, msg.last_net_seq_num); | |
c8f59ee5 | 1810 | |
33832e64 | 1811 | /* Avoid wrapping issue */ |
44a2fbf1 | 1812 | if (((int64_t) (stream->prev_seq - msg.last_net_seq_num)) >= 0) { |
6d805429 | 1813 | /* Data has in fact been written and is NOT pending */ |
c8f59ee5 | 1814 | ret = 0; |
6d805429 DG |
1815 | } else { |
1816 | /* Data still being streamed thus pending */ | |
1817 | ret = 1; | |
c8f59ee5 DG |
1818 | } |
1819 | ||
7591bab1 MD |
1820 | stream->data_pending_check_done = true; |
1821 | pthread_mutex_unlock(&stream->lock); | |
f7079f67 | 1822 | |
7591bab1 MD |
1823 | stream_put(stream); |
1824 | end: | |
c8f59ee5 | 1825 | |
53efb85a | 1826 | memset(&reply, 0, sizeof(reply)); |
c8f59ee5 | 1827 | reply.ret_code = htobe32(ret); |
44a2fbf1 JG |
1828 | send_ret = conn->sock->ops->sendmsg(conn->sock, &reply, sizeof(reply), 0); |
1829 | if (send_ret < (ssize_t) sizeof(reply)) { | |
1830 | ERR("Failed to send \"data pending\" command reply (ret = %zd)", | |
1831 | send_ret); | |
1832 | ret = -1; | |
c8f59ee5 DG |
1833 | } |
1834 | ||
1835 | end_no_session: | |
1836 | return ret; | |
1837 | } | |
1838 | ||
1839 | /* | |
1840 | * Wait for the control socket to reach a quiescent state. | |
1841 | * | |
7591bab1 MD |
1842 | * Note that for now, when receiving this command from the session |
1843 | * daemon, this means that every subsequent commands or data received on | |
1844 | * the control socket has been handled. So, this is why we simply return | |
1845 | * OK here. | |
c8f59ee5 | 1846 | */ |
44a2fbf1 JG |
1847 | static int relay_quiescent_control(const struct lttcomm_relayd_hdr *recv_hdr, |
1848 | struct relay_connection *conn, | |
1849 | const struct lttng_buffer_view *payload) | |
c8f59ee5 DG |
1850 | { |
1851 | int ret; | |
44a2fbf1 | 1852 | ssize_t send_ret; |
ad7051c0 | 1853 | struct relay_stream *stream; |
ad7051c0 | 1854 | struct lttcomm_relayd_quiescent_control msg; |
c8f59ee5 DG |
1855 | struct lttcomm_relayd_generic_reply reply; |
1856 | ||
1857 | DBG("Checking quiescent state on control socket"); | |
1858 | ||
44a2fbf1 | 1859 | if (!conn->session || !conn->version_check_done) { |
ad7051c0 DG |
1860 | ERR("Trying to check for data before version check"); |
1861 | ret = -1; | |
1862 | goto end_no_session; | |
1863 | } | |
1864 | ||
44a2fbf1 JG |
1865 | if (payload->size < sizeof(msg)) { |
1866 | ERR("Unexpected payload size in \"relay_quiescent_control\": expected >= %zu bytes, got %zu bytes", | |
1867 | sizeof(msg), payload->size); | |
ad7051c0 DG |
1868 | ret = -1; |
1869 | goto end_no_session; | |
1870 | } | |
44a2fbf1 JG |
1871 | memcpy(&msg, payload->data, sizeof(msg)); |
1872 | msg.stream_id = be64toh(msg.stream_id); | |
ad7051c0 | 1873 | |
44a2fbf1 | 1874 | stream = stream_get_by_id(msg.stream_id); |
7591bab1 MD |
1875 | if (!stream) { |
1876 | goto reply; | |
1877 | } | |
1878 | pthread_mutex_lock(&stream->lock); | |
1879 | stream->data_pending_check_done = true; | |
1880 | pthread_mutex_unlock(&stream->lock); | |
44a2fbf1 JG |
1881 | |
1882 | DBG("Relay quiescent control pending flag set to %" PRIu64, msg.stream_id); | |
7591bab1 MD |
1883 | stream_put(stream); |
1884 | reply: | |
53efb85a | 1885 | memset(&reply, 0, sizeof(reply)); |
c8f59ee5 | 1886 | reply.ret_code = htobe32(LTTNG_OK); |
44a2fbf1 JG |
1887 | send_ret = conn->sock->ops->sendmsg(conn->sock, &reply, sizeof(reply), 0); |
1888 | if (send_ret < (ssize_t) sizeof(reply)) { | |
1889 | ERR("Failed to send \"quiescent control\" command reply (ret = %zd)", | |
1890 | send_ret); | |
1891 | ret = -1; | |
1892 | } else { | |
1893 | ret = 0; | |
c8f59ee5 DG |
1894 | } |
1895 | ||
ad7051c0 | 1896 | end_no_session: |
c8f59ee5 DG |
1897 | return ret; |
1898 | } | |
1899 | ||
f7079f67 | 1900 | /* |
7591bab1 MD |
1901 | * Initialize a data pending command. This means that a consumer is about |
1902 | * to ask for data pending for each stream it holds. Simply iterate over | |
1903 | * all streams of a session and set the data_pending_check_done flag. | |
f7079f67 DG |
1904 | * |
1905 | * This command returns to the client a LTTNG_OK code. | |
1906 | */ | |
44a2fbf1 JG |
1907 | static int relay_begin_data_pending(const struct lttcomm_relayd_hdr *recv_hdr, |
1908 | struct relay_connection *conn, | |
1909 | const struct lttng_buffer_view *payload) | |
f7079f67 DG |
1910 | { |
1911 | int ret; | |
44a2fbf1 | 1912 | ssize_t send_ret; |
f7079f67 DG |
1913 | struct lttng_ht_iter iter; |
1914 | struct lttcomm_relayd_begin_data_pending msg; | |
1915 | struct lttcomm_relayd_generic_reply reply; | |
1916 | struct relay_stream *stream; | |
f7079f67 DG |
1917 | |
1918 | assert(recv_hdr); | |
58eb9381 | 1919 | assert(conn); |
f7079f67 DG |
1920 | |
1921 | DBG("Init streams for data pending"); | |
1922 | ||
44a2fbf1 | 1923 | if (!conn->session || !conn->version_check_done) { |
f7079f67 DG |
1924 | ERR("Trying to check for data before version check"); |
1925 | ret = -1; | |
1926 | goto end_no_session; | |
1927 | } | |
1928 | ||
44a2fbf1 JG |
1929 | if (payload->size < sizeof(msg)) { |
1930 | ERR("Unexpected payload size in \"relay_begin_data_pending\": expected >= %zu bytes, got %zu bytes", | |
1931 | sizeof(msg), payload->size); | |
f7079f67 DG |
1932 | ret = -1; |
1933 | goto end_no_session; | |
1934 | } | |
44a2fbf1 JG |
1935 | memcpy(&msg, payload->data, sizeof(msg)); |
1936 | msg.session_id = be64toh(msg.session_id); | |
f7079f67 DG |
1937 | |
1938 | /* | |
7591bab1 MD |
1939 | * Iterate over all streams to set the begin data pending flag. |
1940 | * For now, the streams are indexed by stream handle so we have | |
1941 | * to iterate over all streams to find the one associated with | |
1942 | * the right session_id. | |
f7079f67 DG |
1943 | */ |
1944 | rcu_read_lock(); | |
d3e2ba59 | 1945 | cds_lfht_for_each_entry(relay_streams_ht->ht, &iter.iter, stream, |
2a174661 | 1946 | node.node) { |
7591bab1 MD |
1947 | if (!stream_get(stream)) { |
1948 | continue; | |
1949 | } | |
44a2fbf1 | 1950 | if (stream->trace->session->id == msg.session_id) { |
7591bab1 MD |
1951 | pthread_mutex_lock(&stream->lock); |
1952 | stream->data_pending_check_done = false; | |
1953 | pthread_mutex_unlock(&stream->lock); | |
f7079f67 DG |
1954 | DBG("Set begin data pending flag to stream %" PRIu64, |
1955 | stream->stream_handle); | |
1956 | } | |
7591bab1 | 1957 | stream_put(stream); |
f7079f67 DG |
1958 | } |
1959 | rcu_read_unlock(); | |
1960 | ||
53efb85a | 1961 | memset(&reply, 0, sizeof(reply)); |
f7079f67 DG |
1962 | /* All good, send back reply. */ |
1963 | reply.ret_code = htobe32(LTTNG_OK); | |
1964 | ||
44a2fbf1 JG |
1965 | send_ret = conn->sock->ops->sendmsg(conn->sock, &reply, sizeof(reply), 0); |
1966 | if (send_ret < (ssize_t) sizeof(reply)) { | |
1967 | ERR("Failed to send \"begin data pending\" command reply (ret = %zd)", | |
1968 | send_ret); | |
1969 | ret = -1; | |
1970 | } else { | |
1971 | ret = 0; | |
f7079f67 DG |
1972 | } |
1973 | ||
1974 | end_no_session: | |
1975 | return ret; | |
1976 | } | |
1977 | ||
1978 | /* | |
7591bab1 MD |
1979 | * End data pending command. This will check, for a given session id, if |
1980 | * each stream associated with it has its data_pending_check_done flag | |
1981 | * set. If not, this means that the client lost track of the stream but | |
1982 | * the data is still being streamed on our side. In this case, we inform | |
1983 | * the client that data is in flight. | |
f7079f67 DG |
1984 | * |
1985 | * Return to the client if there is data in flight or not with a ret_code. | |
1986 | */ | |
44a2fbf1 JG |
1987 | static int relay_end_data_pending(const struct lttcomm_relayd_hdr *recv_hdr, |
1988 | struct relay_connection *conn, | |
1989 | const struct lttng_buffer_view *payload) | |
f7079f67 DG |
1990 | { |
1991 | int ret; | |
44a2fbf1 | 1992 | ssize_t send_ret; |
f7079f67 DG |
1993 | struct lttng_ht_iter iter; |
1994 | struct lttcomm_relayd_end_data_pending msg; | |
1995 | struct lttcomm_relayd_generic_reply reply; | |
1996 | struct relay_stream *stream; | |
f7079f67 DG |
1997 | uint32_t is_data_inflight = 0; |
1998 | ||
f7079f67 DG |
1999 | DBG("End data pending command"); |
2000 | ||
44a2fbf1 | 2001 | if (!conn->session || !conn->version_check_done) { |
f7079f67 DG |
2002 | ERR("Trying to check for data before version check"); |
2003 | ret = -1; | |
2004 | goto end_no_session; | |
2005 | } | |
2006 | ||
44a2fbf1 JG |
2007 | if (payload->size < sizeof(msg)) { |
2008 | ERR("Unexpected payload size in \"relay_end_data_pending\": expected >= %zu bytes, got %zu bytes", | |
2009 | sizeof(msg), payload->size); | |
f7079f67 DG |
2010 | ret = -1; |
2011 | goto end_no_session; | |
2012 | } | |
44a2fbf1 JG |
2013 | memcpy(&msg, payload->data, sizeof(msg)); |
2014 | msg.session_id = be64toh(msg.session_id); | |
f7079f67 | 2015 | |
7591bab1 MD |
2016 | /* |
2017 | * Iterate over all streams to see if the begin data pending | |
2018 | * flag is set. | |
2019 | */ | |
f7079f67 | 2020 | rcu_read_lock(); |
d3e2ba59 | 2021 | cds_lfht_for_each_entry(relay_streams_ht->ht, &iter.iter, stream, |
2a174661 | 2022 | node.node) { |
7591bab1 MD |
2023 | if (!stream_get(stream)) { |
2024 | continue; | |
2025 | } | |
44a2fbf1 | 2026 | if (stream->trace->session->id != msg.session_id) { |
7591bab1 MD |
2027 | stream_put(stream); |
2028 | continue; | |
2029 | } | |
2030 | pthread_mutex_lock(&stream->lock); | |
2031 | if (!stream->data_pending_check_done) { | |
2032 | if (!stream->closed || !(((int64_t) (stream->prev_seq - stream->last_net_seq_num)) >= 0)) { | |
2033 | is_data_inflight = 1; | |
2034 | DBG("Data is still in flight for stream %" PRIu64, | |
2035 | stream->stream_handle); | |
2036 | pthread_mutex_unlock(&stream->lock); | |
2037 | stream_put(stream); | |
2038 | break; | |
2039 | } | |
f7079f67 | 2040 | } |
7591bab1 MD |
2041 | pthread_mutex_unlock(&stream->lock); |
2042 | stream_put(stream); | |
f7079f67 DG |
2043 | } |
2044 | rcu_read_unlock(); | |
2045 | ||
53efb85a | 2046 | memset(&reply, 0, sizeof(reply)); |
f7079f67 DG |
2047 | /* All good, send back reply. */ |
2048 | reply.ret_code = htobe32(is_data_inflight); | |
2049 | ||
44a2fbf1 JG |
2050 | send_ret = conn->sock->ops->sendmsg(conn->sock, &reply, sizeof(reply), 0); |
2051 | if (send_ret < (ssize_t) sizeof(reply)) { | |
2052 | ERR("Failed to send \"end data pending\" command reply (ret = %zd)", | |
2053 | send_ret); | |
2054 | ret = -1; | |
2055 | } else { | |
2056 | ret = 0; | |
f7079f67 DG |
2057 | } |
2058 | ||
2059 | end_no_session: | |
2060 | return ret; | |
2061 | } | |
2062 | ||
1c20f0e2 JD |
2063 | /* |
2064 | * Receive an index for a specific stream. | |
2065 | * | |
2066 | * Return 0 on success else a negative value. | |
2067 | */ | |
44a2fbf1 JG |
2068 | static int relay_recv_index(const struct lttcomm_relayd_hdr *recv_hdr, |
2069 | struct relay_connection *conn, | |
2070 | const struct lttng_buffer_view *payload) | |
1c20f0e2 | 2071 | { |
44a2fbf1 JG |
2072 | int ret; |
2073 | ssize_t send_ret; | |
58eb9381 | 2074 | struct relay_session *session = conn->session; |
1c20f0e2 | 2075 | struct lttcomm_relayd_index index_info; |
7591bab1 | 2076 | struct relay_index *index; |
1c20f0e2 JD |
2077 | struct lttcomm_relayd_generic_reply reply; |
2078 | struct relay_stream *stream; | |
e0547b83 | 2079 | size_t msg_len; |
1c20f0e2 | 2080 | |
58eb9381 | 2081 | assert(conn); |
1c20f0e2 JD |
2082 | |
2083 | DBG("Relay receiving index"); | |
2084 | ||
44a2fbf1 | 2085 | if (!session || !conn->version_check_done) { |
1c20f0e2 JD |
2086 | ERR("Trying to close a stream before version check"); |
2087 | ret = -1; | |
2088 | goto end_no_session; | |
2089 | } | |
2090 | ||
e0547b83 MD |
2091 | msg_len = lttcomm_relayd_index_len( |
2092 | lttng_to_index_major(conn->major, conn->minor), | |
2093 | lttng_to_index_minor(conn->major, conn->minor)); | |
44a2fbf1 JG |
2094 | if (payload->size < msg_len) { |
2095 | ERR("Unexpected payload size in \"relay_recv_index\": expected >= %zu bytes, got %zu bytes", | |
2096 | msg_len, payload->size); | |
1c20f0e2 JD |
2097 | ret = -1; |
2098 | goto end_no_session; | |
2099 | } | |
44a2fbf1 JG |
2100 | memcpy(&index_info, payload->data, msg_len); |
2101 | index_info.relay_stream_id = be64toh(index_info.relay_stream_id); | |
2102 | index_info.net_seq_num = be64toh(index_info.net_seq_num); | |
2103 | index_info.packet_size = be64toh(index_info.packet_size); | |
2104 | index_info.content_size = be64toh(index_info.content_size); | |
2105 | index_info.timestamp_begin = be64toh(index_info.timestamp_begin); | |
2106 | index_info.timestamp_end = be64toh(index_info.timestamp_end); | |
2107 | index_info.events_discarded = be64toh(index_info.events_discarded); | |
2108 | index_info.stream_id = be64toh(index_info.stream_id); | |
1c20f0e2 | 2109 | |
44a2fbf1 JG |
2110 | if (conn->minor >= 8) { |
2111 | index_info.stream_instance_id = | |
2112 | be64toh(index_info.stream_instance_id); | |
2113 | index_info.packet_seq_num = be64toh(index_info.packet_seq_num); | |
2114 | } | |
1c20f0e2 | 2115 | |
44a2fbf1 | 2116 | stream = stream_get_by_id(index_info.relay_stream_id); |
1c20f0e2 | 2117 | if (!stream) { |
7591bab1 | 2118 | ERR("stream_get_by_id not found"); |
1c20f0e2 | 2119 | ret = -1; |
7591bab1 | 2120 | goto end; |
1c20f0e2 | 2121 | } |
7591bab1 | 2122 | pthread_mutex_lock(&stream->lock); |
1c20f0e2 | 2123 | |
d3e2ba59 JD |
2124 | /* Live beacon handling */ |
2125 | if (index_info.packet_size == 0) { | |
7591bab1 MD |
2126 | DBG("Received live beacon for stream %" PRIu64, |
2127 | stream->stream_handle); | |
d3e2ba59 JD |
2128 | |
2129 | /* | |
7591bab1 MD |
2130 | * Only flag a stream inactive when it has already |
2131 | * received data and no indexes are in flight. | |
d3e2ba59 | 2132 | */ |
a44ca2ca | 2133 | if (stream->index_received_seqcount > 0 |
7591bab1 | 2134 | && stream->indexes_in_flight == 0) { |
44a2fbf1 | 2135 | stream->beacon_ts_end = index_info.timestamp_end; |
d3e2ba59 JD |
2136 | } |
2137 | ret = 0; | |
7591bab1 | 2138 | goto end_stream_put; |
d3e2ba59 JD |
2139 | } else { |
2140 | stream->beacon_ts_end = -1ULL; | |
2141 | } | |
2142 | ||
528f2ffa | 2143 | if (stream->ctf_stream_id == -1ULL) { |
44a2fbf1 | 2144 | stream->ctf_stream_id = index_info.stream_id; |
528f2ffa | 2145 | } |
44a2fbf1 | 2146 | index = relay_index_get_by_id_or_create(stream, index_info.net_seq_num); |
7591bab1 MD |
2147 | if (!index) { |
2148 | ret = -1; | |
2149 | ERR("relay_index_get_by_id_or_create index NULL"); | |
2150 | goto end_stream_put; | |
1c20f0e2 | 2151 | } |
234cd636 | 2152 | if (set_index_control_data(index, &index_info, conn)) { |
7591bab1 MD |
2153 | ERR("set_index_control_data error"); |
2154 | relay_index_put(index); | |
2155 | ret = -1; | |
2156 | goto end_stream_put; | |
2157 | } | |
2158 | ret = relay_index_try_flush(index); | |
2159 | if (ret == 0) { | |
a44ca2ca MD |
2160 | tracefile_array_commit_seq(stream->tfa); |
2161 | stream->index_received_seqcount++; | |
7591bab1 MD |
2162 | } else if (ret > 0) { |
2163 | /* no flush. */ | |
2164 | ret = 0; | |
2165 | } else { | |
2166 | ERR("relay_index_try_flush error %d", ret); | |
2167 | relay_index_put(index); | |
2168 | ret = -1; | |
1c20f0e2 JD |
2169 | } |
2170 | ||
7591bab1 MD |
2171 | end_stream_put: |
2172 | pthread_mutex_unlock(&stream->lock); | |
2173 | stream_put(stream); | |
2174 | ||
2175 | end: | |
1c20f0e2 | 2176 | |
53efb85a | 2177 | memset(&reply, 0, sizeof(reply)); |
1c20f0e2 JD |
2178 | if (ret < 0) { |
2179 | reply.ret_code = htobe32(LTTNG_ERR_UNK); | |
2180 | } else { | |
2181 | reply.ret_code = htobe32(LTTNG_OK); | |
2182 | } | |
58eb9381 | 2183 | send_ret = conn->sock->ops->sendmsg(conn->sock, &reply, sizeof(reply), 0); |
44a2fbf1 JG |
2184 | if (send_ret < (ssize_t) sizeof(reply)) { |
2185 | ERR("Failed to send \"recv index\" command reply (ret = %zd)", send_ret); | |
2186 | ret = -1; | |
1c20f0e2 JD |
2187 | } |
2188 | ||
2189 | end_no_session: | |
2190 | return ret; | |
2191 | } | |
2192 | ||
a4baae1b JD |
2193 | /* |
2194 | * Receive the streams_sent message. | |
2195 | * | |
2196 | * Return 0 on success else a negative value. | |
2197 | */ | |
44a2fbf1 JG |
2198 | static int relay_streams_sent(const struct lttcomm_relayd_hdr *recv_hdr, |
2199 | struct relay_connection *conn, | |
2200 | const struct lttng_buffer_view *payload) | |
a4baae1b | 2201 | { |
44a2fbf1 JG |
2202 | int ret; |
2203 | ssize_t send_ret; | |
a4baae1b JD |
2204 | struct lttcomm_relayd_generic_reply reply; |
2205 | ||
58eb9381 | 2206 | assert(conn); |
a4baae1b JD |
2207 | |
2208 | DBG("Relay receiving streams_sent"); | |
2209 | ||
44a2fbf1 | 2210 | if (!conn->session || !conn->version_check_done) { |
a4baae1b JD |
2211 | ERR("Trying to close a stream before version check"); |
2212 | ret = -1; | |
2213 | goto end_no_session; | |
2214 | } | |
2215 | ||
2216 | /* | |
7591bab1 MD |
2217 | * Publish every pending stream in the connection recv list which are |
2218 | * now ready to be used by the viewer. | |
4a9daf17 | 2219 | */ |
7591bab1 | 2220 | publish_connection_local_streams(conn); |
4a9daf17 | 2221 | |
53efb85a | 2222 | memset(&reply, 0, sizeof(reply)); |
a4baae1b | 2223 | reply.ret_code = htobe32(LTTNG_OK); |
58eb9381 | 2224 | send_ret = conn->sock->ops->sendmsg(conn->sock, &reply, sizeof(reply), 0); |
44a2fbf1 JG |
2225 | if (send_ret < (ssize_t) sizeof(reply)) { |
2226 | ERR("Failed to send \"streams sent\" command reply (ret = %zd)", | |
2227 | send_ret); | |
2228 | ret = -1; | |
a4baae1b JD |
2229 | } else { |
2230 | /* Success. */ | |
2231 | ret = 0; | |
2232 | } | |
2233 | ||
2234 | end_no_session: | |
2235 | return ret; | |
2236 | } | |
2237 | ||
44a2fbf1 JG |
2238 | #define DBG_CMD(cmd_name, conn) \ |
2239 | DBG3("Processing \"%s\" command for socket %i", cmd_name, conn->sock->fd); | |
2240 | ||
2241 | static int relay_process_control_command(struct relay_connection *conn, | |
2242 | const struct lttcomm_relayd_hdr *header, | |
2243 | const struct lttng_buffer_view *payload) | |
b8aa1682 JD |
2244 | { |
2245 | int ret = 0; | |
2246 | ||
44a2fbf1 | 2247 | switch (header->cmd) { |
b8aa1682 | 2248 | case RELAYD_CREATE_SESSION: |
44a2fbf1 JG |
2249 | DBG_CMD("RELAYD_CREATE_SESSION", conn); |
2250 | ret = relay_create_session(header, conn, payload); | |
b8aa1682 | 2251 | break; |
b8aa1682 | 2252 | case RELAYD_ADD_STREAM: |
44a2fbf1 JG |
2253 | DBG_CMD("RELAYD_ADD_STREAM", conn); |
2254 | ret = relay_add_stream(header, conn, payload); | |
b8aa1682 JD |
2255 | break; |
2256 | case RELAYD_START_DATA: | |
44a2fbf1 JG |
2257 | DBG_CMD("RELAYD_START_DATA", conn); |
2258 | ret = relay_start(header, conn, payload); | |
b8aa1682 JD |
2259 | break; |
2260 | case RELAYD_SEND_METADATA: | |
44a2fbf1 JG |
2261 | DBG_CMD("RELAYD_SEND_METADATA", conn); |
2262 | ret = relay_recv_metadata(header, conn, payload); | |
b8aa1682 JD |
2263 | break; |
2264 | case RELAYD_VERSION: | |
44a2fbf1 JG |
2265 | DBG_CMD("RELAYD_VERSION", conn); |
2266 | ret = relay_send_version(header, conn, payload); | |
b8aa1682 | 2267 | break; |
173af62f | 2268 | case RELAYD_CLOSE_STREAM: |
44a2fbf1 JG |
2269 | DBG_CMD("RELAYD_CLOSE_STREAM", conn); |
2270 | ret = relay_close_stream(header, conn, payload); | |
173af62f | 2271 | break; |
6d805429 | 2272 | case RELAYD_DATA_PENDING: |
44a2fbf1 JG |
2273 | DBG_CMD("RELAYD_DATA_PENDING", conn); |
2274 | ret = relay_data_pending(header, conn, payload); | |
c8f59ee5 DG |
2275 | break; |
2276 | case RELAYD_QUIESCENT_CONTROL: | |
44a2fbf1 JG |
2277 | DBG_CMD("RELAYD_QUIESCENT_CONTROL", conn); |
2278 | ret = relay_quiescent_control(header, conn, payload); | |
c8f59ee5 | 2279 | break; |
f7079f67 | 2280 | case RELAYD_BEGIN_DATA_PENDING: |
44a2fbf1 JG |
2281 | DBG_CMD("RELAYD_BEGIN_DATA_PENDING", conn); |
2282 | ret = relay_begin_data_pending(header, conn, payload); | |
f7079f67 DG |
2283 | break; |
2284 | case RELAYD_END_DATA_PENDING: | |
44a2fbf1 JG |
2285 | DBG_CMD("RELAYD_END_DATA_PENDING", conn); |
2286 | ret = relay_end_data_pending(header, conn, payload); | |
f7079f67 | 2287 | break; |
1c20f0e2 | 2288 | case RELAYD_SEND_INDEX: |
44a2fbf1 JG |
2289 | DBG_CMD("RELAYD_SEND_INDEX", conn); |
2290 | ret = relay_recv_index(header, conn, payload); | |
1c20f0e2 | 2291 | break; |
a4baae1b | 2292 | case RELAYD_STREAMS_SENT: |
44a2fbf1 JG |
2293 | DBG_CMD("RELAYD_STREAMS_SENT", conn); |
2294 | ret = relay_streams_sent(header, conn, payload); | |
a4baae1b | 2295 | break; |
93ec662e | 2296 | case RELAYD_RESET_METADATA: |
44a2fbf1 JG |
2297 | DBG_CMD("RELAYD_RESET_METADATA", conn); |
2298 | ret = relay_reset_metadata(header, conn, payload); | |
93ec662e | 2299 | break; |
b8aa1682 JD |
2300 | case RELAYD_UPDATE_SYNC_INFO: |
2301 | default: | |
44a2fbf1 | 2302 | ERR("Received unknown command (%u)", header->cmd); |
58eb9381 | 2303 | relay_unknown_command(conn); |
b8aa1682 JD |
2304 | ret = -1; |
2305 | goto end; | |
2306 | } | |
2307 | ||
2308 | end: | |
2309 | return ret; | |
2310 | } | |
2311 | ||
44a2fbf1 JG |
2312 | static enum relay_connection_status relay_process_control_receive_payload( |
2313 | struct relay_connection *conn) | |
2314 | { | |
2315 | int ret = 0; | |
2316 | enum relay_connection_status status = RELAY_CONNECTION_STATUS_OK; | |
2317 | struct lttng_dynamic_buffer *reception_buffer = | |
2318 | &conn->protocol.ctrl.reception_buffer; | |
2319 | struct ctrl_connection_state_receive_payload *state = | |
2320 | &conn->protocol.ctrl.state.receive_payload; | |
2321 | struct lttng_buffer_view payload_view; | |
2322 | ||
2323 | if (state->left_to_receive == 0) { | |
2324 | /* Short-circuit for payload-less commands. */ | |
2325 | goto reception_complete; | |
2326 | } | |
2327 | ret = conn->sock->ops->recvmsg(conn->sock, | |
2328 | reception_buffer->data + state->received, | |
2329 | state->left_to_receive, MSG_DONTWAIT); | |
2330 | if (ret < 0) { | |
2331 | if (errno != EAGAIN && errno != EWOULDBLOCK) { | |
2332 | PERROR("Unable to receive command payload on sock %d", | |
2333 | conn->sock->fd); | |
2334 | status = RELAY_CONNECTION_STATUS_ERROR; | |
2335 | } | |
2336 | goto end; | |
2337 | } else if (ret == 0) { | |
2338 | DBG("Socket %d performed an orderly shutdown (received EOF)", conn->sock->fd); | |
2339 | status = RELAY_CONNECTION_STATUS_CLOSED; | |
2340 | goto end; | |
2341 | } | |
2342 | ||
2343 | assert(ret > 0); | |
2344 | assert(ret <= state->left_to_receive); | |
2345 | ||
2346 | state->left_to_receive -= ret; | |
2347 | state->received += ret; | |
2348 | ||
2349 | if (state->left_to_receive > 0) { | |
2350 | /* | |
2351 | * Can't transition to the protocol's next state, wait to | |
2352 | * receive the rest of the header. | |
2353 | */ | |
2354 | DBG3("Partial reception of control connection protocol payload (received %" PRIu64 " bytes, %" PRIu64 " bytes left to receive, fd = %i)", | |
2355 | state->received, state->left_to_receive, | |
2356 | conn->sock->fd); | |
2357 | goto end; | |
2358 | } | |
2359 | ||
2360 | reception_complete: | |
2361 | DBG("Done receiving control command payload: fd = %i, payload size = %" PRIu64 " bytes", | |
2362 | conn->sock->fd, state->received); | |
2363 | /* | |
2364 | * The payload required to process the command has been received. | |
2365 | * A view to the reception buffer is forwarded to the various | |
2366 | * commands and the state of the control is reset on success. | |
2367 | * | |
2368 | * Commands are responsible for sending their reply to the peer. | |
2369 | */ | |
2370 | payload_view = lttng_buffer_view_from_dynamic_buffer(reception_buffer, | |
2371 | 0, -1); | |
2372 | ret = relay_process_control_command(conn, | |
2373 | &state->header, &payload_view); | |
2374 | if (ret < 0) { | |
2375 | status = RELAY_CONNECTION_STATUS_ERROR; | |
2376 | goto end; | |
2377 | } | |
2378 | ||
2379 | ret = connection_reset_protocol_state(conn); | |
2380 | if (ret) { | |
2381 | status = RELAY_CONNECTION_STATUS_ERROR; | |
2382 | } | |
2383 | end: | |
2384 | return status; | |
2385 | } | |
2386 | ||
2387 | static enum relay_connection_status relay_process_control_receive_header( | |
2388 | struct relay_connection *conn) | |
2389 | { | |
2390 | int ret = 0; | |
2391 | enum relay_connection_status status = RELAY_CONNECTION_STATUS_OK; | |
2392 | struct lttcomm_relayd_hdr header; | |
2393 | struct lttng_dynamic_buffer *reception_buffer = | |
2394 | &conn->protocol.ctrl.reception_buffer; | |
2395 | struct ctrl_connection_state_receive_header *state = | |
2396 | &conn->protocol.ctrl.state.receive_header; | |
2397 | ||
2398 | assert(state->left_to_receive != 0); | |
2399 | ||
2400 | ret = conn->sock->ops->recvmsg(conn->sock, | |
2401 | reception_buffer->data + state->received, | |
2402 | state->left_to_receive, MSG_DONTWAIT); | |
2403 | if (ret < 0) { | |
2404 | if (errno != EAGAIN && errno != EWOULDBLOCK) { | |
2405 | PERROR("Unable to receive control command header on sock %d", | |
2406 | conn->sock->fd); | |
2407 | status = RELAY_CONNECTION_STATUS_ERROR; | |
2408 | } | |
2409 | goto end; | |
2410 | } else if (ret == 0) { | |
2411 | DBG("Socket %d performed an orderly shutdown (received EOF)", conn->sock->fd); | |
2412 | status = RELAY_CONNECTION_STATUS_CLOSED; | |
2413 | goto end; | |
2414 | } | |
2415 | ||
2416 | assert(ret > 0); | |
2417 | assert(ret <= state->left_to_receive); | |
2418 | ||
2419 | state->left_to_receive -= ret; | |
2420 | state->received += ret; | |
2421 | ||
2422 | if (state->left_to_receive > 0) { | |
2423 | /* | |
2424 | * Can't transition to the protocol's next state, wait to | |
2425 | * receive the rest of the header. | |
2426 | */ | |
2427 | DBG3("Partial reception of control connection protocol header (received %" PRIu64 " bytes, %" PRIu64 " bytes left to receive, fd = %i)", | |
2428 | state->received, state->left_to_receive, | |
2429 | conn->sock->fd); | |
2430 | goto end; | |
2431 | } | |
2432 | ||
2433 | /* Transition to next state: receiving the command's payload. */ | |
2434 | conn->protocol.ctrl.state_id = | |
2435 | CTRL_CONNECTION_STATE_RECEIVE_PAYLOAD; | |
2436 | memcpy(&header, reception_buffer->data, sizeof(header)); | |
2437 | header.circuit_id = be64toh(header.circuit_id); | |
2438 | header.data_size = be64toh(header.data_size); | |
2439 | header.cmd = be32toh(header.cmd); | |
2440 | header.cmd_version = be32toh(header.cmd_version); | |
2441 | memcpy(&conn->protocol.ctrl.state.receive_payload.header, | |
2442 | &header, sizeof(header)); | |
2443 | ||
2444 | DBG("Done receiving control command header: fd = %i, cmd = %" PRIu32 ", cmd_version = %" PRIu32 ", payload size = %" PRIu64 " bytes", | |
2445 | conn->sock->fd, header.cmd, header.cmd_version, | |
2446 | header.data_size); | |
2447 | ||
2448 | if (header.data_size > DEFAULT_NETWORK_RELAYD_CTRL_MAX_PAYLOAD_SIZE) { | |
2449 | ERR("Command header indicates a payload (%" PRIu64 " bytes) that exceeds the maximal payload size allowed on a control connection.", | |
2450 | header.data_size); | |
2451 | status = RELAY_CONNECTION_STATUS_ERROR; | |
2452 | goto end; | |
2453 | } | |
2454 | ||
2455 | conn->protocol.ctrl.state.receive_payload.left_to_receive = | |
2456 | header.data_size; | |
2457 | conn->protocol.ctrl.state.receive_payload.received = 0; | |
2458 | ret = lttng_dynamic_buffer_set_size(reception_buffer, | |
2459 | header.data_size); | |
2460 | if (ret) { | |
2461 | status = RELAY_CONNECTION_STATUS_ERROR; | |
2462 | goto end; | |
2463 | } | |
2464 | ||
2465 | if (header.data_size == 0) { | |
2466 | /* | |
2467 | * Manually invoke the next state as the poll loop | |
2468 | * will not wake-up to allow us to proceed further. | |
2469 | */ | |
2470 | status = relay_process_control_receive_payload(conn); | |
2471 | } | |
2472 | end: | |
2473 | return status; | |
2474 | } | |
2475 | ||
2476 | /* | |
2477 | * Process the commands received on the control socket | |
2478 | */ | |
2479 | static enum relay_connection_status relay_process_control( | |
2480 | struct relay_connection *conn) | |
2481 | { | |
2482 | enum relay_connection_status status; | |
2483 | ||
2484 | switch (conn->protocol.ctrl.state_id) { | |
2485 | case CTRL_CONNECTION_STATE_RECEIVE_HEADER: | |
2486 | status = relay_process_control_receive_header(conn); | |
2487 | break; | |
2488 | case CTRL_CONNECTION_STATE_RECEIVE_PAYLOAD: | |
2489 | status = relay_process_control_receive_payload(conn); | |
2490 | break; | |
2491 | default: | |
2492 | ERR("Unknown control connection protocol state encountered."); | |
2493 | abort(); | |
2494 | } | |
2495 | ||
2496 | return status; | |
2497 | } | |
2498 | ||
7d2f7452 DG |
2499 | /* |
2500 | * Handle index for a data stream. | |
2501 | * | |
7591bab1 | 2502 | * Called with the stream lock held. |
7d2f7452 DG |
2503 | * |
2504 | * Return 0 on success else a negative value. | |
2505 | */ | |
2506 | static int handle_index_data(struct relay_stream *stream, uint64_t net_seq_num, | |
44a2fbf1 | 2507 | bool rotate_index) |
7d2f7452 | 2508 | { |
7591bab1 MD |
2509 | int ret = 0; |
2510 | uint64_t data_offset; | |
2511 | struct relay_index *index; | |
7d2f7452 | 2512 | |
7d2f7452 DG |
2513 | /* Get data offset because we are about to update the index. */ |
2514 | data_offset = htobe64(stream->tracefile_size_current); | |
2515 | ||
65d66b19 MD |
2516 | DBG("handle_index_data: stream %" PRIu64 " net_seq_num %" PRIu64 " data offset %" PRIu64, |
2517 | stream->stream_handle, net_seq_num, stream->tracefile_size_current); | |
7591bab1 | 2518 | |
7d2f7452 | 2519 | /* |
7591bab1 MD |
2520 | * Lookup for an existing index for that stream id/sequence |
2521 | * number. If it exists, the control thread has already received the | |
2522 | * data for it, thus we need to write it to disk. | |
7d2f7452 | 2523 | */ |
7591bab1 | 2524 | index = relay_index_get_by_id_or_create(stream, net_seq_num); |
7d2f7452 | 2525 | if (!index) { |
7591bab1 MD |
2526 | ret = -1; |
2527 | goto end; | |
7d2f7452 DG |
2528 | } |
2529 | ||
e0547b83 MD |
2530 | if (rotate_index || !stream->index_file) { |
2531 | uint32_t major, minor; | |
7591bab1 | 2532 | |
e0547b83 MD |
2533 | /* Put ref on previous index_file. */ |
2534 | if (stream->index_file) { | |
2535 | lttng_index_file_put(stream->index_file); | |
2536 | stream->index_file = NULL; | |
7d2f7452 | 2537 | } |
e0547b83 MD |
2538 | major = stream->trace->session->major; |
2539 | minor = stream->trace->session->minor; | |
2540 | stream->index_file = lttng_index_file_create(stream->path_name, | |
2541 | stream->channel_name, | |
7591bab1 | 2542 | -1, -1, stream->tracefile_size, |
e0547b83 MD |
2543 | tracefile_array_get_file_index_head(stream->tfa), |
2544 | lttng_to_index_major(major, minor), | |
2545 | lttng_to_index_minor(major, minor)); | |
2546 | if (!stream->index_file) { | |
7591bab1 MD |
2547 | ret = -1; |
2548 | /* Put self-ref for this index due to error. */ | |
2549 | relay_index_put(index); | |
e0547b83 | 2550 | index = NULL; |
7591bab1 | 2551 | goto end; |
7d2f7452 | 2552 | } |
7d2f7452 DG |
2553 | } |
2554 | ||
e0547b83 | 2555 | if (relay_index_set_file(index, stream->index_file, data_offset)) { |
7591bab1 MD |
2556 | ret = -1; |
2557 | /* Put self-ref for this index due to error. */ | |
2558 | relay_index_put(index); | |
e0547b83 | 2559 | index = NULL; |
7591bab1 | 2560 | goto end; |
7d2f7452 DG |
2561 | } |
2562 | ||
7591bab1 MD |
2563 | ret = relay_index_try_flush(index); |
2564 | if (ret == 0) { | |
a44ca2ca MD |
2565 | tracefile_array_commit_seq(stream->tfa); |
2566 | stream->index_received_seqcount++; | |
7591bab1 MD |
2567 | } else if (ret > 0) { |
2568 | /* No flush. */ | |
2569 | ret = 0; | |
2570 | } else { | |
2571 | /* Put self-ref for this index due to error. */ | |
2572 | relay_index_put(index); | |
e0547b83 | 2573 | index = NULL; |
7591bab1 MD |
2574 | ret = -1; |
2575 | } | |
2576 | end: | |
7d2f7452 DG |
2577 | return ret; |
2578 | } | |
2579 | ||
44a2fbf1 JG |
2580 | static enum relay_connection_status relay_process_data_receive_header( |
2581 | struct relay_connection *conn) | |
b8aa1682 | 2582 | { |
44a2fbf1 JG |
2583 | int ret; |
2584 | enum relay_connection_status status = RELAY_CONNECTION_STATUS_OK; | |
2585 | struct data_connection_state_receive_header *state = | |
2586 | &conn->protocol.data.state.receive_header; | |
2587 | struct lttcomm_relayd_data_hdr header; | |
b8aa1682 | 2588 | struct relay_stream *stream; |
b8aa1682 | 2589 | |
44a2fbf1 JG |
2590 | assert(state->left_to_receive != 0); |
2591 | ||
2592 | ret = conn->sock->ops->recvmsg(conn->sock, | |
2593 | state->header_reception_buffer + state->received, | |
2594 | state->left_to_receive, MSG_DONTWAIT); | |
2595 | if (ret < 0) { | |
2596 | if (errno != EAGAIN && errno != EWOULDBLOCK) { | |
2597 | PERROR("Unable to receive data header on sock %d", conn->sock->fd); | |
2598 | status = RELAY_CONNECTION_STATUS_ERROR; | |
a6cd2b97 | 2599 | } |
44a2fbf1 JG |
2600 | goto end; |
2601 | } else if (ret == 0) { | |
2602 | /* Orderly shutdown. Not necessary to print an error. */ | |
2603 | DBG("Socket %d performed an orderly shutdown (received EOF)", conn->sock->fd); | |
2604 | status = RELAY_CONNECTION_STATUS_CLOSED; | |
b8aa1682 JD |
2605 | goto end; |
2606 | } | |
2607 | ||
44a2fbf1 JG |
2608 | assert(ret > 0); |
2609 | assert(ret <= state->left_to_receive); | |
2610 | ||
2611 | state->left_to_receive -= ret; | |
2612 | state->received += ret; | |
2613 | ||
2614 | if (state->left_to_receive > 0) { | |
2615 | /* | |
2616 | * Can't transition to the protocol's next state, wait to | |
2617 | * receive the rest of the header. | |
2618 | */ | |
2619 | DBG3("Partial reception of data connection header (received %" PRIu64 " bytes, %" PRIu64 " bytes left to receive, fd = %i)", | |
2620 | state->received, state->left_to_receive, | |
2621 | conn->sock->fd); | |
2622 | ret = 0; | |
7591bab1 | 2623 | goto end; |
b8aa1682 | 2624 | } |
b8aa1682 | 2625 | |
44a2fbf1 JG |
2626 | /* Transition to next state: receiving the payload. */ |
2627 | conn->protocol.data.state_id = DATA_CONNECTION_STATE_RECEIVE_PAYLOAD; | |
173af62f | 2628 | |
44a2fbf1 JG |
2629 | memcpy(&header, state->header_reception_buffer, sizeof(header)); |
2630 | header.circuit_id = be64toh(header.circuit_id); | |
2631 | header.stream_id = be64toh(header.stream_id); | |
2632 | header.data_size = be32toh(header.data_size); | |
2633 | header.net_seq_num = be64toh(header.net_seq_num); | |
2634 | header.padding_size = be32toh(header.padding_size); | |
2635 | memcpy(&conn->protocol.data.state.receive_payload.header, &header, sizeof(header)); | |
2636 | ||
2637 | conn->protocol.data.state.receive_payload.left_to_receive = | |
2638 | header.data_size; | |
2639 | conn->protocol.data.state.receive_payload.received = 0; | |
2640 | conn->protocol.data.state.receive_payload.rotate_index = false; | |
2641 | ||
2642 | DBG("Received data connection header on fd %i: circuit_id = %" PRIu64 ", stream_id = %" PRIu64 ", data_size = %" PRIu32 ", net_seq_num = %" PRIu64 ", padding_size = %" PRIu32, | |
2643 | conn->sock->fd, header.circuit_id, | |
2644 | header.stream_id, header.data_size, | |
2645 | header.net_seq_num, header.padding_size); | |
2646 | ||
2647 | stream = stream_get_by_id(header.stream_id); | |
2648 | if (!stream) { | |
2649 | DBG("relay_process_data_receive_payload: Cannot find stream %" PRIu64, | |
2650 | header.stream_id); | |
2651 | /* Protocol error. */ | |
2652 | status = RELAY_CONNECTION_STATUS_ERROR; | |
2653 | goto end; | |
2654 | } | |
b8aa1682 | 2655 | |
7591bab1 MD |
2656 | pthread_mutex_lock(&stream->lock); |
2657 | ||
1c20f0e2 | 2658 | /* Check if a rotation is needed. */ |
0f907de1 | 2659 | if (stream->tracefile_size > 0 && |
44a2fbf1 | 2660 | (stream->tracefile_size_current + header.data_size) > |
0f907de1 | 2661 | stream->tracefile_size) { |
a44ca2ca MD |
2662 | uint64_t old_id, new_id; |
2663 | ||
2664 | old_id = tracefile_array_get_file_index_head(stream->tfa); | |
2665 | tracefile_array_file_rotate(stream->tfa); | |
2666 | ||
2667 | /* new_id is updated by utils_rotate_stream_file. */ | |
2668 | new_id = old_id; | |
6b6b9a5a | 2669 | |
7591bab1 MD |
2670 | ret = utils_rotate_stream_file(stream->path_name, |
2671 | stream->channel_name, stream->tracefile_size, | |
2672 | stream->tracefile_count, -1, | |
2673 | -1, stream->stream_fd->fd, | |
a44ca2ca | 2674 | &new_id, &stream->stream_fd->fd); |
0f907de1 | 2675 | if (ret < 0) { |
44a2fbf1 JG |
2676 | ERR("Failed to rotate stream output file"); |
2677 | status = RELAY_CONNECTION_STATUS_ERROR; | |
7591bab1 MD |
2678 | goto end_stream_unlock; |
2679 | } | |
44a2fbf1 | 2680 | |
7591bab1 MD |
2681 | /* |
2682 | * Reset current size because we just performed a stream | |
2683 | * rotation. | |
2684 | */ | |
a6976990 | 2685 | stream->tracefile_size_current = 0; |
44a2fbf1 | 2686 | conn->protocol.data.state.receive_payload.rotate_index = true; |
1c20f0e2 JD |
2687 | } |
2688 | ||
44a2fbf1 JG |
2689 | ret = 0; |
2690 | end_stream_unlock: | |
2691 | pthread_mutex_unlock(&stream->lock); | |
2692 | stream_put(stream); | |
2693 | end: | |
2694 | return status; | |
2695 | } | |
2696 | ||
2697 | static enum relay_connection_status relay_process_data_receive_payload( | |
2698 | struct relay_connection *conn) | |
2699 | { | |
2700 | int ret; | |
2701 | enum relay_connection_status status = RELAY_CONNECTION_STATUS_OK; | |
2702 | struct relay_stream *stream; | |
2703 | struct data_connection_state_receive_payload *state = | |
2704 | &conn->protocol.data.state.receive_payload; | |
2705 | const size_t chunk_size = RECV_DATA_BUFFER_SIZE; | |
2706 | char data_buffer[chunk_size]; | |
2707 | bool partial_recv = false; | |
2708 | bool new_stream = false, close_requested = false; | |
2709 | uint64_t left_to_receive = state->left_to_receive; | |
2710 | struct relay_session *session; | |
2711 | ||
2712 | DBG3("Receiving data for stream id %" PRIu64 " seqnum %" PRIu64 ", %" PRIu64" bytes received, %" PRIu64 " bytes left to receive", | |
2713 | state->header.stream_id, state->header.net_seq_num, | |
2714 | state->received, left_to_receive); | |
2715 | ||
2716 | stream = stream_get_by_id(state->header.stream_id); | |
2717 | if (!stream) { | |
2718 | /* Protocol error. */ | |
2719 | ERR("relay_process_data_receive_payload: cannot find stream %" PRIu64, | |
2720 | state->header.stream_id); | |
2721 | status = RELAY_CONNECTION_STATUS_ERROR; | |
2722 | goto end; | |
2723 | } | |
2724 | ||
2725 | pthread_mutex_lock(&stream->lock); | |
2726 | session = stream->trace->session; | |
2727 | if (!conn->session) { | |
2728 | ret = connection_set_session(conn, session); | |
2729 | if (ret) { | |
2730 | status = RELAY_CONNECTION_STATUS_ERROR; | |
7591bab1 | 2731 | goto end_stream_unlock; |
1c20f0e2 | 2732 | } |
1c20f0e2 JD |
2733 | } |
2734 | ||
44a2fbf1 JG |
2735 | /* |
2736 | * The size of the "chunk" received on any iteration is bounded by: | |
2737 | * - the data left to receive, | |
2738 | * - the data immediately available on the socket, | |
2739 | * - the on-stack data buffer | |
2740 | */ | |
2741 | while (left_to_receive > 0 && !partial_recv) { | |
2742 | ssize_t write_ret; | |
2743 | size_t recv_size = min(left_to_receive, chunk_size); | |
1d4dfdef | 2744 | |
44a2fbf1 JG |
2745 | ret = conn->sock->ops->recvmsg(conn->sock, data_buffer, |
2746 | recv_size, MSG_DONTWAIT); | |
2747 | if (ret < 0) { | |
2748 | if (errno != EAGAIN && errno != EWOULDBLOCK) { | |
2749 | PERROR("Socket %d error", conn->sock->fd); | |
2750 | status = RELAY_CONNECTION_STATUS_ERROR; | |
0848dba7 | 2751 | } |
0848dba7 | 2752 | goto end_stream_unlock; |
44a2fbf1 JG |
2753 | } else if (ret == 0) { |
2754 | /* No more data ready to be consumed on socket. */ | |
2755 | DBG3("No more data ready for consumption on data socket of stream id %" PRIu64, | |
2756 | state->header.stream_id); | |
2757 | status = RELAY_CONNECTION_STATUS_CLOSED; | |
2758 | break; | |
2759 | } else if (ret < (int) recv_size) { | |
2760 | /* | |
2761 | * All the data available on the socket has been | |
2762 | * consumed. | |
2763 | */ | |
2764 | partial_recv = true; | |
0848dba7 MD |
2765 | } |
2766 | ||
44a2fbf1 JG |
2767 | recv_size = ret; |
2768 | ||
0848dba7 | 2769 | /* Write data to stream output fd. */ |
44a2fbf1 | 2770 | write_ret = lttng_write(stream->stream_fd->fd, data_buffer, |
0848dba7 | 2771 | recv_size); |
44a2fbf1 | 2772 | if (write_ret < (ssize_t) recv_size) { |
0848dba7 | 2773 | ERR("Relay error writing data to file"); |
44a2fbf1 | 2774 | status = RELAY_CONNECTION_STATUS_ERROR; |
0848dba7 MD |
2775 | goto end_stream_unlock; |
2776 | } | |
2777 | ||
44a2fbf1 JG |
2778 | left_to_receive -= recv_size; |
2779 | state->received += recv_size; | |
2780 | state->left_to_receive = left_to_receive; | |
2781 | ||
0848dba7 | 2782 | DBG2("Relay wrote %zd bytes to tracefile for stream id %" PRIu64, |
44a2fbf1 JG |
2783 | write_ret, stream->stream_handle); |
2784 | } | |
2785 | ||
2786 | if (state->left_to_receive > 0) { | |
2787 | /* | |
2788 | * Did not receive all the data expected, wait for more data to | |
2789 | * become available on the socket. | |
2790 | */ | |
2791 | DBG3("Partial receive on data connection of stream id %" PRIu64 ", %" PRIu64 " bytes received, %" PRIu64 " bytes left to receive", | |
2792 | state->header.stream_id, state->received, | |
2793 | state->left_to_receive); | |
2794 | goto end_stream_unlock; | |
0848dba7 | 2795 | } |
5ab7344e | 2796 | |
7591bab1 | 2797 | ret = write_padding_to_file(stream->stream_fd->fd, |
44a2fbf1 JG |
2798 | state->header.padding_size); |
2799 | if ((int64_t) ret < (int64_t) state->header.padding_size) { | |
65d66b19 | 2800 | ERR("write_padding_to_file: fail stream %" PRIu64 " net_seq_num %" PRIu64 " ret %d", |
44a2fbf1 JG |
2801 | stream->stream_handle, |
2802 | state->header.net_seq_num, ret); | |
2803 | status = RELAY_CONNECTION_STATUS_ERROR; | |
7591bab1 | 2804 | goto end_stream_unlock; |
1d4dfdef | 2805 | } |
44a2fbf1 JG |
2806 | |
2807 | ||
2808 | if (session->minor >= 4 && !session->snapshot) { | |
2809 | ret = handle_index_data(stream, state->header.net_seq_num, | |
2810 | state->rotate_index); | |
2811 | if (ret < 0) { | |
2812 | ERR("handle_index_data: fail stream %" PRIu64 " net_seq_num %" PRIu64 " ret %d", | |
2813 | stream->stream_handle, | |
2814 | state->header.net_seq_num, ret); | |
2815 | status = RELAY_CONNECTION_STATUS_ERROR; | |
2816 | goto end_stream_unlock; | |
2817 | } | |
2818 | } | |
2819 | ||
2820 | stream->tracefile_size_current += state->header.data_size + | |
2821 | state->header.padding_size; | |
2822 | ||
c0bae11d MD |
2823 | if (stream->prev_seq == -1ULL) { |
2824 | new_stream = true; | |
2825 | } | |
2826 | ||
44a2fbf1 JG |
2827 | stream->prev_seq = state->header.net_seq_num; |
2828 | ||
2829 | /* | |
2830 | * Resetting the protocol state (to RECEIVE_HEADER) will trash the | |
2831 | * contents of *state which are aliased (union) to the same location as | |
2832 | * the new state. Don't use it beyond this point. | |
2833 | */ | |
2834 | connection_reset_protocol_state(conn); | |
2835 | state = NULL; | |
173af62f | 2836 | |
7591bab1 | 2837 | end_stream_unlock: |
bda7c7b9 | 2838 | close_requested = stream->close_requested; |
7591bab1 | 2839 | pthread_mutex_unlock(&stream->lock); |
44a2fbf1 | 2840 | if (close_requested && left_to_receive == 0) { |
bda7c7b9 JG |
2841 | try_stream_close(stream); |
2842 | } | |
2843 | ||
c0bae11d MD |
2844 | if (new_stream) { |
2845 | pthread_mutex_lock(&session->lock); | |
2846 | uatomic_set(&session->new_streams, 1); | |
2847 | pthread_mutex_unlock(&session->lock); | |
2848 | } | |
44a2fbf1 | 2849 | |
7591bab1 | 2850 | stream_put(stream); |
b8aa1682 | 2851 | end: |
44a2fbf1 JG |
2852 | return status; |
2853 | } | |
2854 | ||
2855 | /* | |
2856 | * relay_process_data: Process the data received on the data socket | |
2857 | */ | |
2858 | static enum relay_connection_status relay_process_data( | |
2859 | struct relay_connection *conn) | |
2860 | { | |
2861 | enum relay_connection_status status; | |
2862 | ||
2863 | switch (conn->protocol.data.state_id) { | |
2864 | case DATA_CONNECTION_STATE_RECEIVE_HEADER: | |
2865 | status = relay_process_data_receive_header(conn); | |
2866 | break; | |
2867 | case DATA_CONNECTION_STATE_RECEIVE_PAYLOAD: | |
2868 | status = relay_process_data_receive_payload(conn); | |
2869 | break; | |
2870 | default: | |
2871 | ERR("Unexpected data connection communication state."); | |
2872 | abort(); | |
2873 | } | |
2874 | ||
2875 | return status; | |
b8aa1682 JD |
2876 | } |
2877 | ||
7591bab1 | 2878 | static void cleanup_connection_pollfd(struct lttng_poll_event *events, int pollfd) |
b8aa1682 JD |
2879 | { |
2880 | int ret; | |
2881 | ||
58eb9381 | 2882 | (void) lttng_poll_del(events, pollfd); |
b8aa1682 JD |
2883 | |
2884 | ret = close(pollfd); | |
2885 | if (ret < 0) { | |
2886 | ERR("Closing pollfd %d", pollfd); | |
2887 | } | |
2888 | } | |
2889 | ||
7591bab1 MD |
2890 | static void relay_thread_close_connection(struct lttng_poll_event *events, |
2891 | int pollfd, struct relay_connection *conn) | |
9d1bbf21 | 2892 | { |
7591bab1 | 2893 | const char *type_str; |
2a174661 | 2894 | |
7591bab1 MD |
2895 | switch (conn->type) { |
2896 | case RELAY_DATA: | |
2897 | type_str = "Data"; | |
2898 | break; | |
2899 | case RELAY_CONTROL: | |
2900 | type_str = "Control"; | |
2901 | break; | |
2902 | case RELAY_VIEWER_COMMAND: | |
2903 | type_str = "Viewer Command"; | |
2904 | break; | |
2905 | case RELAY_VIEWER_NOTIFICATION: | |
2906 | type_str = "Viewer Notification"; | |
2907 | break; | |
2908 | default: | |
2909 | type_str = "Unknown"; | |
9d1bbf21 | 2910 | } |
7591bab1 MD |
2911 | cleanup_connection_pollfd(events, pollfd); |
2912 | connection_put(conn); | |
2913 | DBG("%s connection closed with %d", type_str, pollfd); | |
b8aa1682 JD |
2914 | } |
2915 | ||
2916 | /* | |
2917 | * This thread does the actual work | |
2918 | */ | |
7591bab1 | 2919 | static void *relay_thread_worker(void *data) |
b8aa1682 | 2920 | { |
beaad64c DG |
2921 | int ret, err = -1, last_seen_data_fd = -1; |
2922 | uint32_t nb_fd; | |
b8aa1682 JD |
2923 | struct lttng_poll_event events; |
2924 | struct lttng_ht *relay_connections_ht; | |
b8aa1682 | 2925 | struct lttng_ht_iter iter; |
90e7d72f | 2926 | struct relay_connection *destroy_conn = NULL; |
b8aa1682 JD |
2927 | |
2928 | DBG("[thread] Relay worker started"); | |
2929 | ||
9d1bbf21 MD |
2930 | rcu_register_thread(); |
2931 | ||
55706a7d MD |
2932 | health_register(health_relayd, HEALTH_RELAYD_TYPE_WORKER); |
2933 | ||
9b5e0863 MD |
2934 | if (testpoint(relayd_thread_worker)) { |
2935 | goto error_testpoint; | |
2936 | } | |
2937 | ||
f385ae0a MD |
2938 | health_code_update(); |
2939 | ||
b8aa1682 JD |
2940 | /* table of connections indexed on socket */ |
2941 | relay_connections_ht = lttng_ht_new(0, LTTNG_HT_TYPE_ULONG); | |
095a4ae5 MD |
2942 | if (!relay_connections_ht) { |
2943 | goto relay_connections_ht_error; | |
2944 | } | |
b8aa1682 | 2945 | |
945459f8 | 2946 | ret = create_named_thread_poll_set(&events, 2, "Worker thread epoll"); |
b8aa1682 JD |
2947 | if (ret < 0) { |
2948 | goto error_poll_create; | |
2949 | } | |
2950 | ||
58eb9381 | 2951 | ret = lttng_poll_add(&events, relay_conn_pipe[0], LPOLLIN | LPOLLRDHUP); |
b8aa1682 JD |
2952 | if (ret < 0) { |
2953 | goto error; | |
2954 | } | |
2955 | ||
beaad64c | 2956 | restart: |
b8aa1682 | 2957 | while (1) { |
beaad64c DG |
2958 | int idx = -1, i, seen_control = 0, last_notdel_data_fd = -1; |
2959 | ||
f385ae0a MD |
2960 | health_code_update(); |
2961 | ||
b8aa1682 | 2962 | /* Infinite blocking call, waiting for transmission */ |
87c1611d | 2963 | DBG3("Relayd worker thread polling..."); |
f385ae0a | 2964 | health_poll_entry(); |
b8aa1682 | 2965 | ret = lttng_poll_wait(&events, -1); |
f385ae0a | 2966 | health_poll_exit(); |
b8aa1682 JD |
2967 | if (ret < 0) { |
2968 | /* | |
2969 | * Restart interrupted system call. | |
2970 | */ | |
2971 | if (errno == EINTR) { | |
2972 | goto restart; | |
2973 | } | |
2974 | goto error; | |
2975 | } | |
2976 | ||
0d9c5d77 DG |
2977 | nb_fd = ret; |
2978 | ||
beaad64c | 2979 | /* |
7591bab1 MD |
2980 | * Process control. The control connection is |
2981 | * prioritized so we don't starve it with high | |
2982 | * throughput tracing data on the data connection. | |
beaad64c | 2983 | */ |
b8aa1682 JD |
2984 | for (i = 0; i < nb_fd; i++) { |
2985 | /* Fetch once the poll data */ | |
beaad64c DG |
2986 | uint32_t revents = LTTNG_POLL_GETEV(&events, i); |
2987 | int pollfd = LTTNG_POLL_GETFD(&events, i); | |
b8aa1682 | 2988 | |
f385ae0a MD |
2989 | health_code_update(); |
2990 | ||
fd20dac9 | 2991 | if (!revents) { |
7591bab1 MD |
2992 | /* |
2993 | * No activity for this FD (poll | |
2994 | * implementation). | |
2995 | */ | |
fd20dac9 MD |
2996 | continue; |
2997 | } | |
2998 | ||
b8aa1682 JD |
2999 | /* Thread quit pipe has been closed. Killing thread. */ |
3000 | ret = check_thread_quit_pipe(pollfd, revents); | |
3001 | if (ret) { | |
095a4ae5 MD |
3002 | err = 0; |
3003 | goto exit; | |
b8aa1682 JD |
3004 | } |
3005 | ||
58eb9381 DG |
3006 | /* Inspect the relay conn pipe for new connection */ |
3007 | if (pollfd == relay_conn_pipe[0]) { | |
03e43155 | 3008 | if (revents & LPOLLIN) { |
90e7d72f JG |
3009 | struct relay_connection *conn; |
3010 | ||
58eb9381 | 3011 | ret = lttng_read(relay_conn_pipe[0], &conn, sizeof(conn)); |
b8aa1682 JD |
3012 | if (ret < 0) { |
3013 | goto error; | |
3014 | } | |
58eb9381 DG |
3015 | lttng_poll_add(&events, conn->sock->fd, |
3016 | LPOLLIN | LPOLLRDHUP); | |
7591bab1 | 3017 | connection_ht_add(relay_connections_ht, conn); |
58eb9381 | 3018 | DBG("Connection socket %d added", conn->sock->fd); |
03e43155 MD |
3019 | } else if (revents & (LPOLLERR | LPOLLHUP | LPOLLRDHUP)) { |
3020 | ERR("Relay connection pipe error"); | |
3021 | goto error; | |
3022 | } else { | |
3023 | ERR("Unexpected poll events %u for sock %d", revents, pollfd); | |
3024 | goto error; | |
b8aa1682 | 3025 | } |
58eb9381 | 3026 | } else { |
90e7d72f JG |
3027 | struct relay_connection *ctrl_conn; |
3028 | ||
7591bab1 | 3029 | ctrl_conn = connection_get_by_sock(relay_connections_ht, pollfd); |
58eb9381 | 3030 | /* If not found, there is a synchronization issue. */ |
90e7d72f | 3031 | assert(ctrl_conn); |
58eb9381 | 3032 | |
03e43155 MD |
3033 | if (ctrl_conn->type == RELAY_DATA) { |
3034 | if (revents & LPOLLIN) { | |
beaad64c DG |
3035 | /* |
3036 | * Flag the last seen data fd not deleted. It will be | |
3037 | * used as the last seen fd if any fd gets deleted in | |
3038 | * this first loop. | |
3039 | */ | |
3040 | last_notdel_data_fd = pollfd; | |
3041 | } | |
03e43155 MD |
3042 | goto put_ctrl_connection; |
3043 | } | |
3044 | assert(ctrl_conn->type == RELAY_CONTROL); | |
3045 | ||
3046 | if (revents & LPOLLIN) { | |
44a2fbf1 JG |
3047 | enum relay_connection_status status; |
3048 | ||
3049 | status = relay_process_control(ctrl_conn); | |
3050 | if (status != RELAY_CONNECTION_STATUS_OK) { | |
3051 | /* | |
3052 | * On socket error flag the session as aborted to force | |
3053 | * the cleanup of its stream otherwise it can leak | |
3054 | * during the lifetime of the relayd. | |
3055 | * | |
3056 | * This prevents situations in which streams can be | |
3057 | * left opened because an index was received, the | |
3058 | * control connection is closed, and the data | |
3059 | * connection is closed (uncleanly) before the packet's | |
3060 | * data provided. | |
3061 | * | |
3062 | * Since the control connection encountered an error, | |
3063 | * it is okay to be conservative and close the | |
3064 | * session right now as we can't rely on the protocol | |
3065 | * being respected anymore. | |
3066 | */ | |
3067 | if (status == RELAY_CONNECTION_STATUS_ERROR) { | |
3068 | session_abort(ctrl_conn->session); | |
03e43155 | 3069 | } |
44a2fbf1 JG |
3070 | |
3071 | /* Clear the connection on error or close. */ | |
3072 | relay_thread_close_connection(&events, | |
3073 | pollfd, | |
3074 | ctrl_conn); | |
03e43155 | 3075 | } |
44a2fbf1 | 3076 | seen_control = 1; |
03e43155 MD |
3077 | } else if (revents & (LPOLLERR | LPOLLHUP | LPOLLRDHUP)) { |
3078 | relay_thread_close_connection(&events, | |
3079 | pollfd, ctrl_conn); | |
3080 | if (last_seen_data_fd == pollfd) { | |
3081 | last_seen_data_fd = last_notdel_data_fd; | |
3082 | } | |
58eb9381 | 3083 | } else { |
03e43155 MD |
3084 | ERR("Unexpected poll events %u for control sock %d", |
3085 | revents, pollfd); | |
3086 | connection_put(ctrl_conn); | |
3087 | goto error; | |
beaad64c | 3088 | } |
03e43155 | 3089 | put_ctrl_connection: |
7591bab1 | 3090 | connection_put(ctrl_conn); |
beaad64c DG |
3091 | } |
3092 | } | |
3093 | ||
3094 | /* | |
3095 | * The last loop handled a control request, go back to poll to make | |
3096 | * sure we prioritise the control socket. | |
3097 | */ | |
3098 | if (seen_control) { | |
3099 | continue; | |
3100 | } | |
3101 | ||
3102 | if (last_seen_data_fd >= 0) { | |
3103 | for (i = 0; i < nb_fd; i++) { | |
3104 | int pollfd = LTTNG_POLL_GETFD(&events, i); | |
f385ae0a MD |
3105 | |
3106 | health_code_update(); | |
3107 | ||
beaad64c DG |
3108 | if (last_seen_data_fd == pollfd) { |
3109 | idx = i; | |
3110 | break; | |
3111 | } | |
3112 | } | |
3113 | } | |
3114 | ||
3115 | /* Process data connection. */ | |
3116 | for (i = idx + 1; i < nb_fd; i++) { | |
3117 | /* Fetch the poll data. */ | |
3118 | uint32_t revents = LTTNG_POLL_GETEV(&events, i); | |
3119 | int pollfd = LTTNG_POLL_GETFD(&events, i); | |
90e7d72f | 3120 | struct relay_connection *data_conn; |
beaad64c | 3121 | |
f385ae0a MD |
3122 | health_code_update(); |
3123 | ||
fd20dac9 MD |
3124 | if (!revents) { |
3125 | /* No activity for this FD (poll implementation). */ | |
3126 | continue; | |
3127 | } | |
3128 | ||
beaad64c | 3129 | /* Skip the command pipe. It's handled in the first loop. */ |
58eb9381 | 3130 | if (pollfd == relay_conn_pipe[0]) { |
beaad64c DG |
3131 | continue; |
3132 | } | |
3133 | ||
7591bab1 | 3134 | data_conn = connection_get_by_sock(relay_connections_ht, pollfd); |
90e7d72f | 3135 | if (!data_conn) { |
fd20dac9 | 3136 | /* Skip it. Might be removed before. */ |
fd20dac9 MD |
3137 | continue; |
3138 | } | |
03e43155 MD |
3139 | if (data_conn->type == RELAY_CONTROL) { |
3140 | goto put_data_connection; | |
3141 | } | |
3142 | assert(data_conn->type == RELAY_DATA); | |
fd20dac9 MD |
3143 | |
3144 | if (revents & LPOLLIN) { | |
44a2fbf1 JG |
3145 | enum relay_connection_status status; |
3146 | ||
3147 | status = relay_process_data(data_conn); | |
3148 | /* Connection closed or error. */ | |
3149 | if (status != RELAY_CONNECTION_STATUS_OK) { | |
3150 | /* | |
3151 | * On socket error flag the session as aborted to force | |
3152 | * the cleanup of its stream otherwise it can leak | |
3153 | * during the lifetime of the relayd. | |
3154 | * | |
3155 | * This prevents situations in which streams can be | |
3156 | * left opened because an index was received, the | |
3157 | * control connection is closed, and the data | |
3158 | * connection is closed (uncleanly) before the packet's | |
3159 | * data provided. | |
3160 | * | |
3161 | * Since the data connection encountered an error, | |
3162 | * it is okay to be conservative and close the | |
3163 | * session right now as we can't rely on the protocol | |
3164 | * being respected anymore. | |
3165 | */ | |
3166 | if (status == RELAY_CONNECTION_STATUS_ERROR) { | |
3167 | session_abort(data_conn->session); | |
3168 | } | |
7591bab1 | 3169 | relay_thread_close_connection(&events, pollfd, |
03e43155 | 3170 | data_conn); |
fd20dac9 MD |
3171 | /* |
3172 | * Every goto restart call sets the last seen fd where | |
3173 | * here we don't really care since we gracefully | |
3174 | * continue the loop after the connection is deleted. | |
3175 | */ | |
3176 | } else { | |
3177 | /* Keep last seen port. */ | |
3178 | last_seen_data_fd = pollfd; | |
7591bab1 | 3179 | connection_put(data_conn); |
fd20dac9 | 3180 | goto restart; |
b8aa1682 | 3181 | } |
03e43155 MD |
3182 | } else if (revents & (LPOLLERR | LPOLLHUP | LPOLLRDHUP)) { |
3183 | relay_thread_close_connection(&events, pollfd, | |
3184 | data_conn); | |
3185 | } else { | |
3186 | ERR("Unknown poll events %u for data sock %d", | |
3187 | revents, pollfd); | |
b8aa1682 | 3188 | } |
03e43155 | 3189 | put_data_connection: |
7591bab1 | 3190 | connection_put(data_conn); |
b8aa1682 | 3191 | } |
beaad64c | 3192 | last_seen_data_fd = -1; |
b8aa1682 JD |
3193 | } |
3194 | ||
f385ae0a MD |
3195 | /* Normal exit, no error */ |
3196 | ret = 0; | |
3197 | ||
095a4ae5 | 3198 | exit: |
b8aa1682 | 3199 | error: |
58eb9381 | 3200 | /* Cleanup reamaining connection object. */ |
9d1bbf21 | 3201 | rcu_read_lock(); |
90e7d72f JG |
3202 | cds_lfht_for_each_entry(relay_connections_ht->ht, &iter.iter, |
3203 | destroy_conn, | |
58eb9381 | 3204 | sock_n.node) { |
f385ae0a | 3205 | health_code_update(); |
06a68401 | 3206 | |
44a2fbf1 | 3207 | session_abort(destroy_conn->session); |
06a68401 | 3208 | |
7591bab1 MD |
3209 | /* |
3210 | * No need to grab another ref, because we own | |
3211 | * destroy_conn. | |
3212 | */ | |
3213 | relay_thread_close_connection(&events, destroy_conn->sock->fd, | |
3214 | destroy_conn); | |
b8aa1682 | 3215 | } |
94d49140 | 3216 | rcu_read_unlock(); |
7591bab1 | 3217 | |
945459f8 | 3218 | (void) fd_tracker_util_poll_clean(the_fd_tracker, &events); |
7d2f7452 | 3219 | error_poll_create: |
b8aa1682 | 3220 | lttng_ht_destroy(relay_connections_ht); |
095a4ae5 | 3221 | relay_connections_ht_error: |
58eb9381 | 3222 | /* Close relay conn pipes */ |
faa951f4 JG |
3223 | (void) fd_tracker_util_pipe_close(the_fd_tracker, |
3224 | relay_conn_pipe); | |
095a4ae5 MD |
3225 | if (err) { |
3226 | DBG("Thread exited with error"); | |
3227 | } | |
b8aa1682 | 3228 | DBG("Worker thread cleanup complete"); |
9b5e0863 | 3229 | error_testpoint: |
f385ae0a MD |
3230 | if (err) { |
3231 | health_error(); | |
3232 | ERR("Health error occurred in %s", __func__); | |
3233 | } | |
3234 | health_unregister(health_relayd); | |
9d1bbf21 | 3235 | rcu_unregister_thread(); |
b4aacfdc | 3236 | lttng_relay_stop_threads(); |
b8aa1682 JD |
3237 | return NULL; |
3238 | } | |
3239 | ||
3240 | /* | |
3241 | * Create the relay command pipe to wake thread_manage_apps. | |
3242 | * Closed in cleanup(). | |
3243 | */ | |
58eb9381 | 3244 | static int create_relay_conn_pipe(void) |
b8aa1682 | 3245 | { |
faa951f4 JG |
3246 | return fd_tracker_util_pipe_open_cloexec(the_fd_tracker, |
3247 | "Relayd connection pipe", relay_conn_pipe); | |
b8aa1682 JD |
3248 | } |
3249 | ||
ae0a0b54 JG |
3250 | static |
3251 | int stdio_open(void *data, int *fds) | |
3252 | { | |
3253 | fds[0] = fileno(stdout); | |
3254 | fds[1] = fileno(stderr); | |
3255 | return 0; | |
3256 | } | |
3257 | ||
3258 | static | |
3259 | int noop_close(void *data, int *fds) | |
3260 | { | |
3261 | return 0; | |
3262 | } | |
3263 | ||
3264 | static | |
3265 | int track_stdio(void) | |
3266 | { | |
3267 | int fds[2]; | |
3268 | const char *names[] = { "stdout", "stderr" }; | |
3269 | ||
3270 | return fd_tracker_open_unsuspendable_fd(the_fd_tracker, fds, | |
3271 | names, 2, stdio_open, NULL); | |
3272 | } | |
3273 | ||
3274 | static | |
3275 | void untrack_stdio(void) | |
3276 | { | |
3277 | int fds[] = { fileno(stdout), fileno(stderr) }; | |
3278 | ||
3279 | /* | |
3280 | * noop_close is used since we don't really want to close | |
3281 | * the stdio output fds; we merely want to stop tracking them. | |
3282 | */ | |
3283 | (void) fd_tracker_close_unsuspendable_fd(the_fd_tracker, | |
3284 | fds, 2, noop_close, NULL); | |
3285 | } | |
3286 | ||
b8aa1682 JD |
3287 | /* |
3288 | * main | |
3289 | */ | |
3290 | int main(int argc, char **argv) | |
3291 | { | |
178a0557 | 3292 | int ret = 0, retval = 0; |
b8aa1682 JD |
3293 | void *status; |
3294 | ||
c7cc870e JR |
3295 | /* Parse environment variables */ |
3296 | parse_env_options(); | |
3297 | ||
3298 | /* | |
3299 | * Parse arguments. | |
3300 | * Command line arguments overwrite environment. | |
3301 | */ | |
b8aa1682 | 3302 | progname = argv[0]; |
178a0557 MD |
3303 | if (set_options(argc, argv)) { |
3304 | retval = -1; | |
3305 | goto exit_options; | |
b8aa1682 JD |
3306 | } |
3307 | ||
178a0557 MD |
3308 | if (set_signal_handler()) { |
3309 | retval = -1; | |
3310 | goto exit_options; | |
b8aa1682 JD |
3311 | } |
3312 | ||
8ff7624a JR |
3313 | relayd_config_log(); |
3314 | ||
3315 | if (opt_print_version) { | |
3316 | print_version(); | |
3317 | retval = 0; | |
3318 | goto exit_options; | |
3319 | } | |
3320 | ||
75d29f13 JG |
3321 | ret = fclose(stdin); |
3322 | if (ret) { | |
3323 | PERROR("Failed to close stdin"); | |
3324 | goto exit_options; | |
3325 | } | |
4d513a50 DG |
3326 | /* Try to create directory if -o, --output is specified. */ |
3327 | if (opt_output_path) { | |
994fa64f DG |
3328 | if (*opt_output_path != '/') { |
3329 | ERR("Please specify an absolute path for -o, --output PATH"); | |
178a0557 MD |
3330 | retval = -1; |
3331 | goto exit_options; | |
994fa64f DG |
3332 | } |
3333 | ||
d77dded2 JG |
3334 | ret = utils_mkdir_recursive(opt_output_path, S_IRWXU | S_IRWXG, |
3335 | -1, -1); | |
4d513a50 DG |
3336 | if (ret < 0) { |
3337 | ERR("Unable to create %s", opt_output_path); | |
178a0557 MD |
3338 | retval = -1; |
3339 | goto exit_options; | |
4d513a50 DG |
3340 | } |
3341 | } | |
3342 | ||
b8aa1682 | 3343 | /* Daemonize */ |
b5218ffb | 3344 | if (opt_daemon || opt_background) { |
3fd27398 MD |
3345 | ret = lttng_daemonize(&child_ppid, &recv_child_signal, |
3346 | !opt_background); | |
b8aa1682 | 3347 | if (ret < 0) { |
178a0557 MD |
3348 | retval = -1; |
3349 | goto exit_options; | |
b8aa1682 | 3350 | } |
3fd27398 MD |
3351 | } |
3352 | ||
76375580 JR |
3353 | if (opt_working_directory) { |
3354 | ret = utils_change_working_dir(opt_working_directory); | |
3355 | if (ret) { | |
3356 | ERR("Changing working directory"); | |
3357 | goto exit_options; | |
3358 | } | |
3359 | } | |
841c095c JG |
3360 | /* |
3361 | * The RCU thread registration (and use, through the fd-tracker's | |
3362 | * creation) is done after the daemonization to allow us to not | |
3363 | * deal with liburcu's fork() management as the call RCU needs to | |
3364 | * be restored. | |
3365 | */ | |
3366 | rcu_register_thread(); | |
3367 | ||
3368 | the_fd_tracker = fd_tracker_create(lttng_opt_fd_cap); | |
3369 | if (!the_fd_tracker) { | |
3370 | retval = -1; | |
3371 | goto exit_options; | |
3372 | } | |
76375580 | 3373 | |
ae0a0b54 JG |
3374 | ret = track_stdio(); |
3375 | if (ret) { | |
3376 | retval = -1; | |
3377 | goto exit_options; | |
3378 | } | |
3379 | ||
178a0557 MD |
3380 | /* Initialize thread health monitoring */ |
3381 | health_relayd = health_app_create(NR_HEALTH_RELAYD_TYPES); | |
3382 | if (!health_relayd) { | |
3383 | PERROR("health_app_create error"); | |
3384 | retval = -1; | |
3385 | goto exit_health_app_create; | |
3386 | } | |
3387 | ||
3fd27398 | 3388 | /* Create thread quit pipe */ |
178a0557 MD |
3389 | if (init_thread_quit_pipe()) { |
3390 | retval = -1; | |
3391 | goto exit_init_data; | |
b8aa1682 JD |
3392 | } |
3393 | ||
b8aa1682 | 3394 | /* Setup the thread apps communication pipe. */ |
178a0557 MD |
3395 | if (create_relay_conn_pipe()) { |
3396 | retval = -1; | |
3397 | goto exit_init_data; | |
b8aa1682 JD |
3398 | } |
3399 | ||
3400 | /* Init relay command queue. */ | |
8bdee6e2 | 3401 | cds_wfcq_init(&relay_conn_queue.head, &relay_conn_queue.tail); |
b8aa1682 | 3402 | |
554831e7 MD |
3403 | /* Initialize communication library */ |
3404 | lttcomm_init(); | |
87e45c13 | 3405 | lttcomm_inet_init(); |
554831e7 | 3406 | |
d3e2ba59 | 3407 | /* tables of sessions indexed by session ID */ |
7591bab1 MD |
3408 | sessions_ht = lttng_ht_new(0, LTTNG_HT_TYPE_U64); |
3409 | if (!sessions_ht) { | |
178a0557 MD |
3410 | retval = -1; |
3411 | goto exit_init_data; | |
d3e2ba59 JD |
3412 | } |
3413 | ||
3414 | /* tables of streams indexed by stream ID */ | |
2a174661 | 3415 | relay_streams_ht = lttng_ht_new(0, LTTNG_HT_TYPE_U64); |
d3e2ba59 | 3416 | if (!relay_streams_ht) { |
178a0557 MD |
3417 | retval = -1; |
3418 | goto exit_init_data; | |
d3e2ba59 JD |
3419 | } |
3420 | ||
3421 | /* tables of streams indexed by stream ID */ | |
92c6ca54 DG |
3422 | viewer_streams_ht = lttng_ht_new(0, LTTNG_HT_TYPE_U64); |
3423 | if (!viewer_streams_ht) { | |
178a0557 MD |
3424 | retval = -1; |
3425 | goto exit_init_data; | |
55706a7d MD |
3426 | } |
3427 | ||
98398092 | 3428 | ret = init_health_quit_pipe(); |
178a0557 MD |
3429 | if (ret) { |
3430 | retval = -1; | |
3431 | goto exit_health_quit_pipe; | |
65931c8b MD |
3432 | } |
3433 | ||
3434 | /* Create thread to manage the client socket */ | |
1a1a34b4 | 3435 | ret = pthread_create(&health_thread, default_pthread_attr(), |
65931c8b | 3436 | thread_manage_health, (void *) NULL); |
178a0557 MD |
3437 | if (ret) { |
3438 | errno = ret; | |
65931c8b | 3439 | PERROR("pthread_create health"); |
178a0557 MD |
3440 | retval = -1; |
3441 | goto exit_health_thread; | |
65931c8b MD |
3442 | } |
3443 | ||
b8aa1682 | 3444 | /* Setup the dispatcher thread */ |
1a1a34b4 | 3445 | ret = pthread_create(&dispatcher_thread, default_pthread_attr(), |
b8aa1682 | 3446 | relay_thread_dispatcher, (void *) NULL); |
178a0557 MD |
3447 | if (ret) { |
3448 | errno = ret; | |
b8aa1682 | 3449 | PERROR("pthread_create dispatcher"); |
178a0557 MD |
3450 | retval = -1; |
3451 | goto exit_dispatcher_thread; | |
b8aa1682 JD |
3452 | } |
3453 | ||
3454 | /* Setup the worker thread */ | |
1a1a34b4 | 3455 | ret = pthread_create(&worker_thread, default_pthread_attr(), |
7591bab1 | 3456 | relay_thread_worker, NULL); |
178a0557 MD |
3457 | if (ret) { |
3458 | errno = ret; | |
b8aa1682 | 3459 | PERROR("pthread_create worker"); |
178a0557 MD |
3460 | retval = -1; |
3461 | goto exit_worker_thread; | |
b8aa1682 JD |
3462 | } |
3463 | ||
3464 | /* Setup the listener thread */ | |
1a1a34b4 | 3465 | ret = pthread_create(&listener_thread, default_pthread_attr(), |
b8aa1682 | 3466 | relay_thread_listener, (void *) NULL); |
178a0557 MD |
3467 | if (ret) { |
3468 | errno = ret; | |
b8aa1682 | 3469 | PERROR("pthread_create listener"); |
178a0557 MD |
3470 | retval = -1; |
3471 | goto exit_listener_thread; | |
b8aa1682 JD |
3472 | } |
3473 | ||
7591bab1 | 3474 | ret = relayd_live_create(live_uri); |
178a0557 | 3475 | if (ret) { |
d3e2ba59 | 3476 | ERR("Starting live viewer threads"); |
178a0557 | 3477 | retval = -1; |
50138f51 | 3478 | goto exit_live; |
d3e2ba59 JD |
3479 | } |
3480 | ||
178a0557 MD |
3481 | /* |
3482 | * This is where we start awaiting program completion (e.g. through | |
3483 | * signal that asks threads to teardown). | |
3484 | */ | |
3485 | ||
3486 | ret = relayd_live_join(); | |
3487 | if (ret) { | |
3488 | retval = -1; | |
3489 | } | |
50138f51 | 3490 | exit_live: |
178a0557 | 3491 | |
b8aa1682 | 3492 | ret = pthread_join(listener_thread, &status); |
178a0557 MD |
3493 | if (ret) { |
3494 | errno = ret; | |
3495 | PERROR("pthread_join listener_thread"); | |
3496 | retval = -1; | |
b8aa1682 JD |
3497 | } |
3498 | ||
178a0557 | 3499 | exit_listener_thread: |
b8aa1682 | 3500 | ret = pthread_join(worker_thread, &status); |
178a0557 MD |
3501 | if (ret) { |
3502 | errno = ret; | |
3503 | PERROR("pthread_join worker_thread"); | |
3504 | retval = -1; | |
b8aa1682 JD |
3505 | } |
3506 | ||
178a0557 | 3507 | exit_worker_thread: |
b8aa1682 | 3508 | ret = pthread_join(dispatcher_thread, &status); |
178a0557 MD |
3509 | if (ret) { |
3510 | errno = ret; | |
3511 | PERROR("pthread_join dispatcher_thread"); | |
3512 | retval = -1; | |
b8aa1682 | 3513 | } |
178a0557 | 3514 | exit_dispatcher_thread: |
42415026 | 3515 | |
65931c8b | 3516 | ret = pthread_join(health_thread, &status); |
178a0557 MD |
3517 | if (ret) { |
3518 | errno = ret; | |
3519 | PERROR("pthread_join health_thread"); | |
3520 | retval = -1; | |
65931c8b | 3521 | } |
178a0557 | 3522 | exit_health_thread: |
65931c8b | 3523 | |
98398092 | 3524 | (void) fd_tracker_util_pipe_close(the_fd_tracker, health_quit_pipe); |
178a0557 | 3525 | exit_health_quit_pipe: |
65931c8b | 3526 | |
178a0557 | 3527 | exit_init_data: |
55706a7d | 3528 | health_app_destroy(health_relayd); |
55706a7d | 3529 | exit_health_app_create: |
178a0557 | 3530 | exit_options: |
2668465a MD |
3531 | /* |
3532 | * Wait for all pending call_rcu work to complete before tearing | |
3533 | * down data structures. call_rcu worker may be trying to | |
3534 | * perform lookups in those structures. | |
3535 | */ | |
3536 | rcu_barrier(); | |
7591bab1 MD |
3537 | relayd_cleanup(); |
3538 | ||
3539 | /* Ensure all prior call_rcu are done. */ | |
3540 | rcu_barrier(); | |
d3e2ba59 | 3541 | |
ae0a0b54 JG |
3542 | untrack_stdio(); |
3543 | /* | |
3544 | * fd_tracker_destroy() will log the contents of the fd-tracker | |
3545 | * if a leak is detected. | |
3546 | */ | |
841c095c JG |
3547 | fd_tracker_destroy(the_fd_tracker); |
3548 | rcu_unregister_thread(); | |
3549 | ||
178a0557 | 3550 | if (!retval) { |
b8aa1682 | 3551 | exit(EXIT_SUCCESS); |
178a0557 MD |
3552 | } else { |
3553 | exit(EXIT_FAILURE); | |
b8aa1682 | 3554 | } |
b8aa1682 | 3555 | } |