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> | |
72489970 | 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> | |
72489970 | 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> |
a8cad461 JG |
60 | #include <common/dynamic-buffer.h> |
61 | #include <common/buffer-view.h> | |
c38fd28d | 62 | #include <common/fd-tracker/utils.h> |
7591bab1 | 63 | #include <urcu/rculist.h> |
b8aa1682 | 64 | |
cdd566a2 | 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 | |
a8cad461 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 */ |
a174e6fc | 90 | char *opt_output_path, *opt_working_directory; |
cdd566a2 | 91 | static int opt_daemon, opt_background, opt_print_version; |
4d27e2b4 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 | |
72489970 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 | |
bf5e21fd 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', }, |
72489970 | 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' }, |
a174e6fc | 185 | { "working-directory", 1, 0, 'w', }, |
4d27e2b4 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 | |
cdd566a2 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 | } | |
b8b6fadb JR |
205 | if (EXTRA_VERSION_PATCHES[0] != '\0') { |
206 | DBG("LTTng-relayd extra patches:\n\t" EXTRA_VERSION_PATCHES "\n"); | |
207 | } | |
cdd566a2 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: | |
72489970 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': |
cdd566a2 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; | |
a174e6fc 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; | |
4d27e2b4 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 | ||
b967f1cc 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 | } | |
72489970 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 | |
4d27e2b4 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 */ |
c3e83e3d | 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 | { |
c3e83e3d JG |
786 | return fd_tracker_util_pipe_open_cloexec(the_fd_tracker, |
787 | "Quit pipe", thread_quit_pipe); | |
b8aa1682 JD |
788 | } |
789 | ||
2b56bd01 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 | */ | |
c38fd28d 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 | ||
c38fd28d 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 | ||
7b84b4a8 JG |
843 | static int create_sock(void *data, int *out_fd) |
844 | { | |
845 | int ret; | |
846 | struct lttcomm_sock *sock = data; | |
847 | ||
848 | ret = lttcomm_create_sock(sock); | |
849 | if (ret < 0) { | |
850 | goto end; | |
851 | } | |
852 | ||
853 | *out_fd = sock->fd; | |
854 | end: | |
855 | return ret; | |
856 | } | |
857 | ||
858 | static int close_sock(void *data, int *in_fd) | |
859 | { | |
860 | struct lttcomm_sock *sock = data; | |
861 | ||
862 | return sock->ops->close(sock); | |
863 | } | |
864 | ||
b8aa1682 JD |
865 | /* |
866 | * Create and init socket from uri. | |
867 | */ | |
7b84b4a8 JG |
868 | static struct lttcomm_sock *relay_socket_create(struct lttng_uri *uri, |
869 | const char *name) | |
b8aa1682 | 870 | { |
7b84b4a8 | 871 | int ret, sock_fd; |
b8aa1682 | 872 | struct lttcomm_sock *sock = NULL; |
7b84b4a8 JG |
873 | char uri_str[PATH_MAX]; |
874 | char *formated_name = NULL; | |
b8aa1682 JD |
875 | |
876 | sock = lttcomm_alloc_sock_from_uri(uri); | |
877 | if (sock == NULL) { | |
878 | ERR("Allocating socket"); | |
879 | goto error; | |
880 | } | |
881 | ||
7b84b4a8 JG |
882 | /* |
883 | * Don't fail to create the socket if the name can't be built as it is | |
884 | * only used for debugging purposes. | |
885 | */ | |
886 | ret = uri_to_str_url(uri, uri_str, sizeof(uri_str)); | |
887 | uri_str[sizeof(uri_str) - 1] = '\0'; | |
888 | if (ret >= 0) { | |
889 | ret = asprintf(&formated_name, "%s socket @ %s", name, | |
890 | uri_str); | |
891 | if (ret < 0) { | |
892 | formated_name = NULL; | |
893 | } | |
b8aa1682 | 894 | } |
7b84b4a8 JG |
895 | |
896 | ret = fd_tracker_open_unsuspendable_fd(the_fd_tracker, &sock_fd, | |
897 | (const char **) (formated_name ? &formated_name : NULL), | |
898 | 1, create_sock, sock); | |
899 | free(formated_name); | |
900 | DBG("Listening on %s socket %d", name, sock->fd); | |
b8aa1682 JD |
901 | |
902 | ret = sock->ops->bind(sock); | |
903 | if (ret < 0) { | |
904 | goto error; | |
905 | } | |
906 | ||
907 | ret = sock->ops->listen(sock, -1); | |
908 | if (ret < 0) { | |
909 | goto error; | |
910 | ||
911 | } | |
912 | ||
913 | return sock; | |
914 | ||
915 | error: | |
916 | if (sock) { | |
917 | lttcomm_destroy_sock(sock); | |
918 | } | |
919 | return NULL; | |
920 | } | |
921 | ||
922 | /* | |
923 | * This thread manages the listening for new connections on the network | |
924 | */ | |
7591bab1 | 925 | static void *relay_thread_listener(void *data) |
b8aa1682 | 926 | { |
095a4ae5 | 927 | int i, ret, pollfd, err = -1; |
b8aa1682 JD |
928 | uint32_t revents, nb_fd; |
929 | struct lttng_poll_event events; | |
930 | struct lttcomm_sock *control_sock, *data_sock; | |
931 | ||
b8aa1682 JD |
932 | DBG("[thread] Relay listener started"); |
933 | ||
55706a7d MD |
934 | health_register(health_relayd, HEALTH_RELAYD_TYPE_LISTENER); |
935 | ||
f385ae0a MD |
936 | health_code_update(); |
937 | ||
7b84b4a8 | 938 | control_sock = relay_socket_create(control_uri, "Control listener"); |
b8aa1682 | 939 | if (!control_sock) { |
095a4ae5 | 940 | goto error_sock_control; |
b8aa1682 JD |
941 | } |
942 | ||
7656e89b | 943 | data_sock = relay_socket_create(data_uri, "Data listener"); |
b8aa1682 | 944 | if (!data_sock) { |
095a4ae5 | 945 | goto error_sock_relay; |
b8aa1682 JD |
946 | } |
947 | ||
948 | /* | |
7591bab1 MD |
949 | * Pass 3 as size here for the thread quit pipe, control and |
950 | * data socket. | |
b8aa1682 | 951 | */ |
6c53231c | 952 | ret = create_named_thread_poll_set(&events, 3, "Listener thread epoll"); |
b8aa1682 JD |
953 | if (ret < 0) { |
954 | goto error_create_poll; | |
955 | } | |
956 | ||
957 | /* Add the control socket */ | |
958 | ret = lttng_poll_add(&events, control_sock->fd, LPOLLIN | LPOLLRDHUP); | |
959 | if (ret < 0) { | |
960 | goto error_poll_add; | |
961 | } | |
962 | ||
963 | /* Add the data socket */ | |
964 | ret = lttng_poll_add(&events, data_sock->fd, LPOLLIN | LPOLLRDHUP); | |
965 | if (ret < 0) { | |
966 | goto error_poll_add; | |
967 | } | |
968 | ||
3fd27398 MD |
969 | lttng_relay_notify_ready(); |
970 | ||
9b5e0863 MD |
971 | if (testpoint(relayd_thread_listener)) { |
972 | goto error_testpoint; | |
973 | } | |
974 | ||
b8aa1682 | 975 | while (1) { |
f385ae0a MD |
976 | health_code_update(); |
977 | ||
b8aa1682 JD |
978 | DBG("Listener accepting connections"); |
979 | ||
b8aa1682 | 980 | restart: |
f385ae0a | 981 | health_poll_entry(); |
b8aa1682 | 982 | ret = lttng_poll_wait(&events, -1); |
f385ae0a | 983 | health_poll_exit(); |
b8aa1682 JD |
984 | if (ret < 0) { |
985 | /* | |
986 | * Restart interrupted system call. | |
987 | */ | |
988 | if (errno == EINTR) { | |
989 | goto restart; | |
990 | } | |
991 | goto error; | |
992 | } | |
993 | ||
0d9c5d77 DG |
994 | nb_fd = ret; |
995 | ||
b8aa1682 JD |
996 | DBG("Relay new connection received"); |
997 | for (i = 0; i < nb_fd; i++) { | |
f385ae0a MD |
998 | health_code_update(); |
999 | ||
b8aa1682 JD |
1000 | /* Fetch once the poll data */ |
1001 | revents = LTTNG_POLL_GETEV(&events, i); | |
1002 | pollfd = LTTNG_POLL_GETFD(&events, i); | |
1003 | ||
fd20dac9 | 1004 | if (!revents) { |
7591bab1 MD |
1005 | /* |
1006 | * No activity for this FD (poll | |
1007 | * implementation). | |
1008 | */ | |
fd20dac9 MD |
1009 | continue; |
1010 | } | |
1011 | ||
b8aa1682 JD |
1012 | /* Thread quit pipe has been closed. Killing thread. */ |
1013 | ret = check_thread_quit_pipe(pollfd, revents); | |
1014 | if (ret) { | |
095a4ae5 MD |
1015 | err = 0; |
1016 | goto exit; | |
b8aa1682 JD |
1017 | } |
1018 | ||
03e43155 | 1019 | if (revents & LPOLLIN) { |
4b7f17b2 | 1020 | /* |
7591bab1 MD |
1021 | * A new connection is requested, therefore a |
1022 | * sessiond/consumerd connection is allocated in | |
1023 | * this thread, enqueued to a global queue and | |
1024 | * dequeued (and freed) in the worker thread. | |
4b7f17b2 | 1025 | */ |
58eb9381 DG |
1026 | int val = 1; |
1027 | struct relay_connection *new_conn; | |
4b7f17b2 | 1028 | struct lttcomm_sock *newsock; |
7591bab1 | 1029 | enum connection_type type; |
b8aa1682 JD |
1030 | |
1031 | if (pollfd == data_sock->fd) { | |
7591bab1 | 1032 | type = RELAY_DATA; |
b8aa1682 | 1033 | newsock = data_sock->ops->accept(data_sock); |
58eb9381 DG |
1034 | DBG("Relay data connection accepted, socket %d", |
1035 | newsock->fd); | |
4b7f17b2 MD |
1036 | } else { |
1037 | assert(pollfd == control_sock->fd); | |
7591bab1 | 1038 | type = RELAY_CONTROL; |
b8aa1682 | 1039 | newsock = control_sock->ops->accept(control_sock); |
58eb9381 DG |
1040 | DBG("Relay control connection accepted, socket %d", |
1041 | newsock->fd); | |
b8aa1682 | 1042 | } |
58eb9381 DG |
1043 | if (!newsock) { |
1044 | PERROR("accepting sock"); | |
58eb9381 DG |
1045 | goto error; |
1046 | } | |
1047 | ||
1048 | ret = setsockopt(newsock->fd, SOL_SOCKET, SO_REUSEADDR, &val, | |
1049 | sizeof(val)); | |
b8aa1682 JD |
1050 | if (ret < 0) { |
1051 | PERROR("setsockopt inet"); | |
4b7f17b2 | 1052 | lttcomm_destroy_sock(newsock); |
b8aa1682 JD |
1053 | goto error; |
1054 | } | |
2fc6b1ab JR |
1055 | |
1056 | ret = socket_apply_keep_alive_config(newsock->fd); | |
1057 | if (ret < 0) { | |
1058 | ERR("Failed to apply TCP keep-alive configuration on socket (%i)", | |
1059 | newsock->fd); | |
1060 | lttcomm_destroy_sock(newsock); | |
1061 | goto error; | |
1062 | } | |
1063 | ||
7591bab1 MD |
1064 | new_conn = connection_create(newsock, type); |
1065 | if (!new_conn) { | |
1066 | lttcomm_destroy_sock(newsock); | |
1067 | goto error; | |
1068 | } | |
58eb9381 DG |
1069 | |
1070 | /* Enqueue request for the dispatcher thread. */ | |
8bdee6e2 SM |
1071 | cds_wfcq_enqueue(&relay_conn_queue.head, &relay_conn_queue.tail, |
1072 | &new_conn->qnode); | |
b8aa1682 JD |
1073 | |
1074 | /* | |
7591bab1 MD |
1075 | * Wake the dispatch queue futex. |
1076 | * Implicit memory barrier with the | |
1077 | * exchange in cds_wfcq_enqueue. | |
b8aa1682 | 1078 | */ |
58eb9381 | 1079 | futex_nto1_wake(&relay_conn_queue.futex); |
03e43155 MD |
1080 | } else if (revents & (LPOLLERR | LPOLLHUP | LPOLLRDHUP)) { |
1081 | ERR("socket poll error"); | |
1082 | goto error; | |
1083 | } else { | |
1084 | ERR("Unexpected poll events %u for sock %d", revents, pollfd); | |
1085 | goto error; | |
b8aa1682 JD |
1086 | } |
1087 | } | |
1088 | } | |
1089 | ||
095a4ae5 | 1090 | exit: |
b8aa1682 JD |
1091 | error: |
1092 | error_poll_add: | |
9b5e0863 | 1093 | error_testpoint: |
6c53231c | 1094 | (void) fd_tracker_util_poll_clean(the_fd_tracker, &events); |
b8aa1682 | 1095 | error_create_poll: |
095a4ae5 | 1096 | if (data_sock->fd >= 0) { |
7656e89b JG |
1097 | ret = fd_tracker_close_unsuspendable_fd(the_fd_tracker, |
1098 | &data_sock->fd, 1, close_sock, | |
1099 | data_sock); | |
b8aa1682 JD |
1100 | if (ret) { |
1101 | PERROR("close"); | |
1102 | } | |
b8aa1682 | 1103 | } |
095a4ae5 MD |
1104 | lttcomm_destroy_sock(data_sock); |
1105 | error_sock_relay: | |
1106 | if (control_sock->fd >= 0) { | |
7b84b4a8 JG |
1107 | ret = fd_tracker_close_unsuspendable_fd(the_fd_tracker, |
1108 | &control_sock->fd, 1, close_sock, | |
1109 | control_sock); | |
b8aa1682 JD |
1110 | if (ret) { |
1111 | PERROR("close"); | |
1112 | } | |
b8aa1682 | 1113 | } |
095a4ae5 MD |
1114 | lttcomm_destroy_sock(control_sock); |
1115 | error_sock_control: | |
1116 | if (err) { | |
f385ae0a MD |
1117 | health_error(); |
1118 | ERR("Health error occurred in %s", __func__); | |
095a4ae5 | 1119 | } |
55706a7d | 1120 | health_unregister(health_relayd); |
b8aa1682 | 1121 | DBG("Relay listener thread cleanup complete"); |
b4aacfdc | 1122 | lttng_relay_stop_threads(); |
b8aa1682 JD |
1123 | return NULL; |
1124 | } | |
1125 | ||
1126 | /* | |
1127 | * This thread manages the dispatching of the requests to worker threads | |
1128 | */ | |
7591bab1 | 1129 | static void *relay_thread_dispatcher(void *data) |
b8aa1682 | 1130 | { |
6cd525e8 MD |
1131 | int err = -1; |
1132 | ssize_t ret; | |
8bdee6e2 | 1133 | struct cds_wfcq_node *node; |
58eb9381 | 1134 | struct relay_connection *new_conn = NULL; |
b8aa1682 JD |
1135 | |
1136 | DBG("[thread] Relay dispatcher started"); | |
1137 | ||
55706a7d MD |
1138 | health_register(health_relayd, HEALTH_RELAYD_TYPE_DISPATCHER); |
1139 | ||
9b5e0863 MD |
1140 | if (testpoint(relayd_thread_dispatcher)) { |
1141 | goto error_testpoint; | |
1142 | } | |
1143 | ||
f385ae0a MD |
1144 | health_code_update(); |
1145 | ||
68924dc1 | 1146 | for (;;) { |
f385ae0a MD |
1147 | health_code_update(); |
1148 | ||
b8aa1682 | 1149 | /* Atomically prepare the queue futex */ |
58eb9381 | 1150 | futex_nto1_prepare(&relay_conn_queue.futex); |
b8aa1682 | 1151 | |
68924dc1 MD |
1152 | if (CMM_LOAD_SHARED(dispatch_thread_exit)) { |
1153 | break; | |
1154 | } | |
1155 | ||
b8aa1682 | 1156 | do { |
f385ae0a MD |
1157 | health_code_update(); |
1158 | ||
b8aa1682 | 1159 | /* Dequeue commands */ |
8bdee6e2 SM |
1160 | node = cds_wfcq_dequeue_blocking(&relay_conn_queue.head, |
1161 | &relay_conn_queue.tail); | |
b8aa1682 JD |
1162 | if (node == NULL) { |
1163 | DBG("Woken up but nothing in the relay command queue"); | |
1164 | /* Continue thread execution */ | |
1165 | break; | |
1166 | } | |
58eb9381 | 1167 | new_conn = caa_container_of(node, struct relay_connection, qnode); |
b8aa1682 | 1168 | |
58eb9381 | 1169 | DBG("Dispatching request waiting on sock %d", new_conn->sock->fd); |
b8aa1682 JD |
1170 | |
1171 | /* | |
7591bab1 MD |
1172 | * Inform worker thread of the new request. This |
1173 | * call is blocking so we can be assured that | |
1174 | * the data will be read at some point in time | |
1175 | * or wait to the end of the world :) | |
b8aa1682 | 1176 | */ |
58eb9381 DG |
1177 | ret = lttng_write(relay_conn_pipe[1], &new_conn, sizeof(new_conn)); |
1178 | if (ret < 0) { | |
1179 | PERROR("write connection pipe"); | |
7591bab1 | 1180 | connection_put(new_conn); |
b8aa1682 JD |
1181 | goto error; |
1182 | } | |
1183 | } while (node != NULL); | |
1184 | ||
1185 | /* Futex wait on queue. Blocking call on futex() */ | |
f385ae0a | 1186 | health_poll_entry(); |
58eb9381 | 1187 | futex_nto1_wait(&relay_conn_queue.futex); |
f385ae0a | 1188 | health_poll_exit(); |
b8aa1682 JD |
1189 | } |
1190 | ||
f385ae0a MD |
1191 | /* Normal exit, no error */ |
1192 | err = 0; | |
1193 | ||
b8aa1682 | 1194 | error: |
9b5e0863 | 1195 | error_testpoint: |
f385ae0a MD |
1196 | if (err) { |
1197 | health_error(); | |
1198 | ERR("Health error occurred in %s", __func__); | |
1199 | } | |
55706a7d | 1200 | health_unregister(health_relayd); |
b8aa1682 | 1201 | DBG("Dispatch thread dying"); |
b4aacfdc | 1202 | lttng_relay_stop_threads(); |
b8aa1682 JD |
1203 | return NULL; |
1204 | } | |
1205 | ||
b8aa1682 | 1206 | /* |
7591bab1 | 1207 | * Set index data from the control port to a given index object. |
b8aa1682 | 1208 | */ |
7591bab1 | 1209 | static int set_index_control_data(struct relay_index *index, |
234cd636 JD |
1210 | struct lttcomm_relayd_index *data, |
1211 | struct relay_connection *conn) | |
1c20f0e2 | 1212 | { |
7591bab1 | 1213 | struct ctf_packet_index index_data; |
1c20f0e2 JD |
1214 | |
1215 | /* | |
a8cad461 | 1216 | * The index on disk is encoded in big endian. |
1c20f0e2 | 1217 | */ |
a8cad461 JG |
1218 | index_data.packet_size = htobe64(data->packet_size); |
1219 | index_data.content_size = htobe64(data->content_size); | |
1220 | index_data.timestamp_begin = htobe64(data->timestamp_begin); | |
1221 | index_data.timestamp_end = htobe64(data->timestamp_end); | |
1222 | index_data.events_discarded = htobe64(data->events_discarded); | |
1223 | index_data.stream_id = htobe64(data->stream_id); | |
234cd636 JD |
1224 | |
1225 | if (conn->minor >= 8) { | |
a8cad461 JG |
1226 | index->index_data.stream_instance_id = htobe64(data->stream_instance_id); |
1227 | index->index_data.packet_seq_num = htobe64(data->packet_seq_num); | |
234cd636 JD |
1228 | } |
1229 | ||
7591bab1 | 1230 | return relay_index_set_data(index, &index_data); |
1c20f0e2 JD |
1231 | } |
1232 | ||
c5b6f4f0 DG |
1233 | /* |
1234 | * Handle the RELAYD_CREATE_SESSION command. | |
1235 | * | |
1236 | * On success, send back the session id or else return a negative value. | |
1237 | */ | |
a8cad461 JG |
1238 | static int relay_create_session(const struct lttcomm_relayd_hdr *recv_hdr, |
1239 | struct relay_connection *conn, | |
1240 | const struct lttng_buffer_view *payload) | |
c5b6f4f0 | 1241 | { |
a8cad461 JG |
1242 | int ret = 0; |
1243 | ssize_t send_ret; | |
c5b6f4f0 DG |
1244 | struct relay_session *session; |
1245 | struct lttcomm_relayd_status_session reply; | |
36d2e35d | 1246 | char session_name[LTTNG_NAME_MAX]; |
9cf3eb20 | 1247 | char hostname[LTTNG_HOST_NAME_MAX]; |
7591bab1 MD |
1248 | uint32_t live_timer = 0; |
1249 | bool snapshot = false; | |
c5b6f4f0 | 1250 | |
36d2e35d | 1251 | memset(session_name, 0, LTTNG_NAME_MAX); |
9cf3eb20 | 1252 | memset(hostname, 0, LTTNG_HOST_NAME_MAX); |
c5b6f4f0 DG |
1253 | |
1254 | memset(&reply, 0, sizeof(reply)); | |
1255 | ||
58eb9381 | 1256 | switch (conn->minor) { |
2a174661 DG |
1257 | case 1: |
1258 | case 2: | |
1259 | case 3: | |
1260 | break; | |
1261 | case 4: /* LTTng sessiond 2.4 */ | |
1262 | default: | |
a8cad461 | 1263 | ret = cmd_create_session_2_4(payload, session_name, |
7591bab1 MD |
1264 | hostname, &live_timer, &snapshot); |
1265 | } | |
1266 | if (ret < 0) { | |
1267 | goto send_reply; | |
d3e2ba59 JD |
1268 | } |
1269 | ||
7591bab1 MD |
1270 | session = session_create(session_name, hostname, live_timer, |
1271 | snapshot, conn->major, conn->minor); | |
1272 | if (!session) { | |
1273 | ret = -1; | |
1274 | goto send_reply; | |
1275 | } | |
1276 | assert(!conn->session); | |
1277 | conn->session = session; | |
c5b6f4f0 DG |
1278 | DBG("Created session %" PRIu64, session->id); |
1279 | ||
7591bab1 MD |
1280 | reply.session_id = htobe64(session->id); |
1281 | ||
1282 | send_reply: | |
c5b6f4f0 DG |
1283 | if (ret < 0) { |
1284 | reply.ret_code = htobe32(LTTNG_ERR_FATAL); | |
1285 | } else { | |
1286 | reply.ret_code = htobe32(LTTNG_OK); | |
1287 | } | |
1288 | ||
58eb9381 | 1289 | send_ret = conn->sock->ops->sendmsg(conn->sock, &reply, sizeof(reply), 0); |
a8cad461 JG |
1290 | if (send_ret < (ssize_t) sizeof(reply)) { |
1291 | ERR("Failed to send \"create session\" command reply (ret = %zd)", | |
1292 | send_ret); | |
1293 | ret = -1; | |
c5b6f4f0 DG |
1294 | } |
1295 | ||
1296 | return ret; | |
1297 | } | |
1298 | ||
a4baae1b JD |
1299 | /* |
1300 | * When we have received all the streams and the metadata for a channel, | |
1301 | * we make them visible to the viewer threads. | |
1302 | */ | |
7591bab1 | 1303 | static void publish_connection_local_streams(struct relay_connection *conn) |
a4baae1b | 1304 | { |
7591bab1 MD |
1305 | struct relay_stream *stream; |
1306 | struct relay_session *session = conn->session; | |
a4baae1b | 1307 | |
7591bab1 MD |
1308 | /* |
1309 | * We publish all streams belonging to a session atomically wrt | |
1310 | * session lock. | |
1311 | */ | |
1312 | pthread_mutex_lock(&session->lock); | |
1313 | rcu_read_lock(); | |
1314 | cds_list_for_each_entry_rcu(stream, &session->recv_list, | |
1315 | recv_node) { | |
1316 | stream_publish(stream); | |
a4baae1b | 1317 | } |
7591bab1 | 1318 | rcu_read_unlock(); |
a4baae1b | 1319 | |
7591bab1 MD |
1320 | /* |
1321 | * Inform the viewer that there are new streams in the session. | |
1322 | */ | |
1323 | if (session->viewer_attached) { | |
1324 | uatomic_set(&session->new_streams, 1); | |
1325 | } | |
1326 | pthread_mutex_unlock(&session->lock); | |
a4baae1b JD |
1327 | } |
1328 | ||
b8aa1682 JD |
1329 | /* |
1330 | * relay_add_stream: allocate a new stream for a session | |
1331 | */ | |
a8cad461 JG |
1332 | static int relay_add_stream(const struct lttcomm_relayd_hdr *recv_hdr, |
1333 | struct relay_connection *conn, | |
1334 | const struct lttng_buffer_view *payload) | |
b8aa1682 | 1335 | { |
7591bab1 MD |
1336 | int ret; |
1337 | ssize_t send_ret; | |
58eb9381 | 1338 | struct relay_session *session = conn->session; |
b8aa1682 JD |
1339 | struct relay_stream *stream = NULL; |
1340 | struct lttcomm_relayd_status_stream reply; | |
4030a636 | 1341 | struct ctf_trace *trace = NULL; |
7591bab1 MD |
1342 | uint64_t stream_handle = -1ULL; |
1343 | char *path_name = NULL, *channel_name = NULL; | |
1344 | uint64_t tracefile_size = 0, tracefile_count = 0; | |
b8aa1682 | 1345 | |
a8cad461 | 1346 | if (!session || !conn->version_check_done) { |
b8aa1682 JD |
1347 | ERR("Trying to add a stream before version check"); |
1348 | ret = -1; | |
1349 | goto end_no_session; | |
1350 | } | |
1351 | ||
7591bab1 MD |
1352 | switch (session->minor) { |
1353 | case 1: /* LTTng sessiond 2.1. Allocates path_name and channel_name. */ | |
a8cad461 | 1354 | ret = cmd_recv_stream_2_1(payload, &path_name, |
4d27e2b4 | 1355 | &channel_name, session); |
0f907de1 | 1356 | break; |
7591bab1 | 1357 | case 2: /* LTTng sessiond 2.2. Allocates path_name and channel_name. */ |
0f907de1 | 1358 | default: |
a8cad461 | 1359 | ret = cmd_recv_stream_2_2(payload, &path_name, |
4d27e2b4 JR |
1360 | &channel_name, &tracefile_size, &tracefile_count, |
1361 | session); | |
0f907de1 JD |
1362 | break; |
1363 | } | |
1364 | if (ret < 0) { | |
7591bab1 | 1365 | goto send_reply; |
b8aa1682 JD |
1366 | } |
1367 | ||
7591bab1 | 1368 | trace = ctf_trace_get_by_path_or_create(session, path_name); |
2a174661 | 1369 | if (!trace) { |
7591bab1 | 1370 | goto send_reply; |
2a174661 | 1371 | } |
7591bab1 | 1372 | /* This stream here has one reference on the trace. */ |
2a174661 | 1373 | |
7591bab1 MD |
1374 | pthread_mutex_lock(&last_relay_stream_id_lock); |
1375 | stream_handle = ++last_relay_stream_id; | |
1376 | pthread_mutex_unlock(&last_relay_stream_id_lock); | |
d3e2ba59 | 1377 | |
7591bab1 MD |
1378 | /* We pass ownership of path_name and channel_name. */ |
1379 | stream = stream_create(trace, stream_handle, path_name, | |
1380 | channel_name, tracefile_size, tracefile_count); | |
1381 | path_name = NULL; | |
1382 | channel_name = NULL; | |
a4baae1b | 1383 | |
2a174661 | 1384 | /* |
7591bab1 MD |
1385 | * Streams are the owners of their trace. Reference to trace is |
1386 | * kept within stream_create(). | |
2a174661 | 1387 | */ |
7591bab1 | 1388 | ctf_trace_put(trace); |
d3e2ba59 | 1389 | |
7591bab1 | 1390 | send_reply: |
53efb85a | 1391 | memset(&reply, 0, sizeof(reply)); |
7591bab1 MD |
1392 | reply.handle = htobe64(stream_handle); |
1393 | if (!stream) { | |
f73fabfd | 1394 | reply.ret_code = htobe32(LTTNG_ERR_UNK); |
b8aa1682 | 1395 | } else { |
f73fabfd | 1396 | reply.ret_code = htobe32(LTTNG_OK); |
b8aa1682 | 1397 | } |
5af40280 | 1398 | |
58eb9381 | 1399 | send_ret = conn->sock->ops->sendmsg(conn->sock, &reply, |
b8aa1682 | 1400 | sizeof(struct lttcomm_relayd_status_stream), 0); |
a8cad461 JG |
1401 | if (send_ret < (ssize_t) sizeof(reply)) { |
1402 | ERR("Failed to send \"add stream\" command reply (ret = %zd)", | |
1403 | send_ret); | |
1404 | ret = -1; | |
b8aa1682 JD |
1405 | } |
1406 | ||
1407 | end_no_session: | |
7591bab1 MD |
1408 | free(path_name); |
1409 | free(channel_name); | |
0f907de1 | 1410 | return ret; |
b8aa1682 JD |
1411 | } |
1412 | ||
173af62f DG |
1413 | /* |
1414 | * relay_close_stream: close a specific stream | |
1415 | */ | |
a8cad461 JG |
1416 | static int relay_close_stream(const struct lttcomm_relayd_hdr *recv_hdr, |
1417 | struct relay_connection *conn, | |
1418 | const struct lttng_buffer_view *payload) | |
173af62f | 1419 | { |
a8cad461 JG |
1420 | int ret; |
1421 | ssize_t send_ret; | |
58eb9381 | 1422 | struct relay_session *session = conn->session; |
173af62f DG |
1423 | struct lttcomm_relayd_close_stream stream_info; |
1424 | struct lttcomm_relayd_generic_reply reply; | |
1425 | struct relay_stream *stream; | |
173af62f DG |
1426 | |
1427 | DBG("Close stream received"); | |
1428 | ||
a8cad461 | 1429 | if (!session || !conn->version_check_done) { |
173af62f DG |
1430 | ERR("Trying to close a stream before version check"); |
1431 | ret = -1; | |
1432 | goto end_no_session; | |
1433 | } | |
1434 | ||
a8cad461 JG |
1435 | if (payload->size < sizeof(stream_info)) { |
1436 | ERR("Unexpected payload size in \"relay_close_stream\": expected >= %zu bytes, got %zu bytes", | |
1437 | sizeof(stream_info), payload->size); | |
173af62f DG |
1438 | ret = -1; |
1439 | goto end_no_session; | |
1440 | } | |
a8cad461 JG |
1441 | memcpy(&stream_info, payload->data, sizeof(stream_info)); |
1442 | stream_info.stream_id = be64toh(stream_info.stream_id); | |
1443 | stream_info.last_net_seq_num = be64toh(stream_info.last_net_seq_num); | |
173af62f | 1444 | |
a8cad461 | 1445 | stream = stream_get_by_id(stream_info.stream_id); |
173af62f DG |
1446 | if (!stream) { |
1447 | ret = -1; | |
7591bab1 | 1448 | goto end; |
173af62f | 1449 | } |
77f7bd85 MD |
1450 | |
1451 | /* | |
1452 | * Set last_net_seq_num before the close flag. Required by data | |
1453 | * pending check. | |
1454 | */ | |
7591bab1 | 1455 | pthread_mutex_lock(&stream->lock); |
a8cad461 | 1456 | stream->last_net_seq_num = stream_info.last_net_seq_num; |
77f7bd85 MD |
1457 | pthread_mutex_unlock(&stream->lock); |
1458 | ||
bda7c7b9 JG |
1459 | /* |
1460 | * This is one of the conditions which may trigger a stream close | |
1461 | * with the others being: | |
1462 | * 1) A close command is received for a stream | |
1463 | * 2) The control connection owning the stream is closed | |
1464 | * 3) We have received all of the stream's data _after_ a close | |
1465 | * request. | |
1466 | */ | |
1467 | try_stream_close(stream); | |
7591bab1 MD |
1468 | if (stream->is_metadata) { |
1469 | struct relay_viewer_stream *vstream; | |
173af62f | 1470 | |
7591bab1 MD |
1471 | vstream = viewer_stream_get_by_id(stream->stream_handle); |
1472 | if (vstream) { | |
1473 | if (vstream->metadata_sent == stream->metadata_received) { | |
1474 | /* | |
1475 | * Since all the metadata has been sent to the | |
1476 | * viewer and that we have a request to close | |
1477 | * its stream, we can safely teardown the | |
1478 | * corresponding metadata viewer stream. | |
1479 | */ | |
1480 | viewer_stream_put(vstream); | |
1481 | } | |
1482 | /* Put local reference. */ | |
1483 | viewer_stream_put(vstream); | |
1484 | } | |
1485 | } | |
7591bab1 | 1486 | stream_put(stream); |
a8cad461 | 1487 | ret = 0; |
173af62f | 1488 | |
7591bab1 | 1489 | end: |
53efb85a | 1490 | memset(&reply, 0, sizeof(reply)); |
173af62f | 1491 | if (ret < 0) { |
f73fabfd | 1492 | reply.ret_code = htobe32(LTTNG_ERR_UNK); |
173af62f | 1493 | } else { |
f73fabfd | 1494 | reply.ret_code = htobe32(LTTNG_OK); |
173af62f | 1495 | } |
58eb9381 | 1496 | send_ret = conn->sock->ops->sendmsg(conn->sock, &reply, |
173af62f | 1497 | sizeof(struct lttcomm_relayd_generic_reply), 0); |
a8cad461 JG |
1498 | if (send_ret < (ssize_t) sizeof(reply)) { |
1499 | ERR("Failed to send \"close stream\" command reply (ret = %zd)", | |
1500 | send_ret); | |
1501 | ret = -1; | |
173af62f DG |
1502 | } |
1503 | ||
1504 | end_no_session: | |
1505 | return ret; | |
1506 | } | |
1507 | ||
93ec662e JD |
1508 | /* |
1509 | * relay_reset_metadata: reset a metadata stream | |
1510 | */ | |
1511 | static | |
a8cad461 JG |
1512 | int relay_reset_metadata(const struct lttcomm_relayd_hdr *recv_hdr, |
1513 | struct relay_connection *conn, | |
1514 | const struct lttng_buffer_view *payload) | |
93ec662e | 1515 | { |
a8cad461 JG |
1516 | int ret; |
1517 | ssize_t send_ret; | |
93ec662e JD |
1518 | struct relay_session *session = conn->session; |
1519 | struct lttcomm_relayd_reset_metadata stream_info; | |
1520 | struct lttcomm_relayd_generic_reply reply; | |
1521 | struct relay_stream *stream; | |
1522 | ||
1523 | DBG("Reset metadata received"); | |
1524 | ||
a8cad461 | 1525 | if (!session || !conn->version_check_done) { |
93ec662e JD |
1526 | ERR("Trying to reset a metadata stream before version check"); |
1527 | ret = -1; | |
1528 | goto end_no_session; | |
1529 | } | |
1530 | ||
a8cad461 JG |
1531 | if (payload->size < sizeof(stream_info)) { |
1532 | ERR("Unexpected payload size in \"relay_reset_metadata\": expected >= %zu bytes, got %zu bytes", | |
1533 | sizeof(stream_info), payload->size); | |
93ec662e JD |
1534 | ret = -1; |
1535 | goto end_no_session; | |
1536 | } | |
a8cad461 JG |
1537 | memcpy(&stream_info, payload->data, sizeof(stream_info)); |
1538 | stream_info.stream_id = be64toh(stream_info.stream_id); | |
1539 | stream_info.version = be64toh(stream_info.version); | |
1540 | ||
1541 | DBG("Update metadata to version %" PRIu64, stream_info.version); | |
93ec662e JD |
1542 | |
1543 | /* Unsupported for live sessions for now. */ | |
1544 | if (session->live_timer != 0) { | |
1545 | ret = -1; | |
1546 | goto end; | |
1547 | } | |
1548 | ||
a8cad461 | 1549 | stream = stream_get_by_id(stream_info.stream_id); |
93ec662e JD |
1550 | if (!stream) { |
1551 | ret = -1; | |
1552 | goto end; | |
1553 | } | |
1554 | pthread_mutex_lock(&stream->lock); | |
1555 | if (!stream->is_metadata) { | |
1556 | ret = -1; | |
1557 | goto end_unlock; | |
1558 | } | |
1559 | ||
1560 | ret = utils_rotate_stream_file(stream->path_name, stream->channel_name, | |
1561 | 0, 0, -1, -1, stream->stream_fd->fd, NULL, | |
1562 | &stream->stream_fd->fd); | |
1563 | if (ret < 0) { | |
1564 | ERR("Failed to rotate metadata file %s of channel %s", | |
1565 | stream->path_name, stream->channel_name); | |
1566 | goto end_unlock; | |
1567 | } | |
1568 | ||
1569 | end_unlock: | |
1570 | pthread_mutex_unlock(&stream->lock); | |
1571 | stream_put(stream); | |
1572 | ||
1573 | end: | |
1574 | memset(&reply, 0, sizeof(reply)); | |
1575 | if (ret < 0) { | |
1576 | reply.ret_code = htobe32(LTTNG_ERR_UNK); | |
1577 | } else { | |
1578 | reply.ret_code = htobe32(LTTNG_OK); | |
1579 | } | |
1580 | send_ret = conn->sock->ops->sendmsg(conn->sock, &reply, | |
1581 | sizeof(struct lttcomm_relayd_generic_reply), 0); | |
a8cad461 JG |
1582 | if (send_ret < (ssize_t) sizeof(reply)) { |
1583 | ERR("Failed to send \"reset metadata\" command reply (ret = %zd)", | |
1584 | send_ret); | |
1585 | ret = -1; | |
93ec662e JD |
1586 | } |
1587 | ||
1588 | end_no_session: | |
1589 | return ret; | |
1590 | } | |
1591 | ||
b8aa1682 JD |
1592 | /* |
1593 | * relay_unknown_command: send -1 if received unknown command | |
1594 | */ | |
7591bab1 | 1595 | static void relay_unknown_command(struct relay_connection *conn) |
b8aa1682 JD |
1596 | { |
1597 | struct lttcomm_relayd_generic_reply reply; | |
a8cad461 | 1598 | ssize_t send_ret; |
b8aa1682 | 1599 | |
53efb85a | 1600 | memset(&reply, 0, sizeof(reply)); |
f73fabfd | 1601 | reply.ret_code = htobe32(LTTNG_ERR_UNK); |
a8cad461 JG |
1602 | send_ret = conn->sock->ops->sendmsg(conn->sock, &reply, sizeof(reply), 0); |
1603 | if (send_ret < sizeof(reply)) { | |
1604 | ERR("Failed to send \"unknown command\" command reply (ret = %zd)", send_ret); | |
b8aa1682 JD |
1605 | } |
1606 | } | |
1607 | ||
1608 | /* | |
1609 | * relay_start: send an acknowledgment to the client to tell if we are | |
1610 | * ready to receive data. We are ready if a session is established. | |
1611 | */ | |
a8cad461 JG |
1612 | static int relay_start(const struct lttcomm_relayd_hdr *recv_hdr, |
1613 | struct relay_connection *conn, | |
1614 | const struct lttng_buffer_view *payload) | |
b8aa1682 | 1615 | { |
a8cad461 JG |
1616 | int ret = 0; |
1617 | ssize_t send_ret; | |
b8aa1682 | 1618 | struct lttcomm_relayd_generic_reply reply; |
58eb9381 | 1619 | struct relay_session *session = conn->session; |
b8aa1682 JD |
1620 | |
1621 | if (!session) { | |
1622 | DBG("Trying to start the streaming without a session established"); | |
f73fabfd | 1623 | ret = htobe32(LTTNG_ERR_UNK); |
b8aa1682 JD |
1624 | } |
1625 | ||
53efb85a | 1626 | memset(&reply, 0, sizeof(reply)); |
a8cad461 JG |
1627 | reply.ret_code = htobe32(LTTNG_OK); |
1628 | send_ret = conn->sock->ops->sendmsg(conn->sock, &reply, | |
1629 | sizeof(reply), 0); | |
1630 | if (send_ret < (ssize_t) sizeof(reply)) { | |
1631 | ERR("Failed to send \"relay_start\" command reply (ret = %zd)", | |
1632 | send_ret); | |
1633 | ret = -1; | |
b8aa1682 JD |
1634 | } |
1635 | ||
1636 | return ret; | |
1637 | } | |
1638 | ||
1d4dfdef DG |
1639 | /* |
1640 | * Append padding to the file pointed by the file descriptor fd. | |
1641 | */ | |
1642 | static int write_padding_to_file(int fd, uint32_t size) | |
1643 | { | |
6cd525e8 | 1644 | ssize_t ret = 0; |
1d4dfdef DG |
1645 | char *zeros; |
1646 | ||
1647 | if (size == 0) { | |
1648 | goto end; | |
1649 | } | |
1650 | ||
1651 | zeros = zmalloc(size); | |
1652 | if (zeros == NULL) { | |
1653 | PERROR("zmalloc zeros for padding"); | |
1654 | ret = -1; | |
1655 | goto end; | |
1656 | } | |
1657 | ||
6cd525e8 MD |
1658 | ret = lttng_write(fd, zeros, size); |
1659 | if (ret < size) { | |
1d4dfdef DG |
1660 | PERROR("write padding to file"); |
1661 | } | |
1662 | ||
e986c7a1 DG |
1663 | free(zeros); |
1664 | ||
1d4dfdef DG |
1665 | end: |
1666 | return ret; | |
1667 | } | |
1668 | ||
b8aa1682 | 1669 | /* |
7591bab1 | 1670 | * relay_recv_metadata: receive the metadata for the session. |
b8aa1682 | 1671 | */ |
a8cad461 JG |
1672 | static int relay_recv_metadata(const struct lttcomm_relayd_hdr *recv_hdr, |
1673 | struct relay_connection *conn, | |
1674 | const struct lttng_buffer_view *payload) | |
b8aa1682 | 1675 | { |
32d1569c | 1676 | int ret = 0; |
6cd525e8 | 1677 | ssize_t size_ret; |
58eb9381 | 1678 | struct relay_session *session = conn->session; |
a8cad461 | 1679 | struct lttcomm_relayd_metadata_payload metadata_payload_header; |
b8aa1682 | 1680 | struct relay_stream *metadata_stream; |
a8cad461 | 1681 | uint64_t metadata_payload_size; |
b8aa1682 JD |
1682 | |
1683 | if (!session) { | |
1684 | ERR("Metadata sent before version check"); | |
1685 | ret = -1; | |
1686 | goto end; | |
1687 | } | |
1688 | ||
a8cad461 | 1689 | if (recv_hdr->data_size < sizeof(struct lttcomm_relayd_metadata_payload)) { |
f6416125 MD |
1690 | ERR("Incorrect data size"); |
1691 | ret = -1; | |
1692 | goto end; | |
1693 | } | |
a8cad461 JG |
1694 | metadata_payload_size = recv_hdr->data_size - |
1695 | sizeof(struct lttcomm_relayd_metadata_payload); | |
f6416125 | 1696 | |
a8cad461 JG |
1697 | memcpy(&metadata_payload_header, payload->data, |
1698 | sizeof(metadata_payload_header)); | |
1699 | metadata_payload_header.stream_id = be64toh( | |
1700 | metadata_payload_header.stream_id); | |
1701 | metadata_payload_header.padding_size = be32toh( | |
1702 | metadata_payload_header.padding_size); | |
c617c0c6 | 1703 | |
a8cad461 | 1704 | metadata_stream = stream_get_by_id(metadata_payload_header.stream_id); |
b8aa1682 JD |
1705 | if (!metadata_stream) { |
1706 | ret = -1; | |
7591bab1 | 1707 | goto end; |
b8aa1682 JD |
1708 | } |
1709 | ||
7591bab1 MD |
1710 | pthread_mutex_lock(&metadata_stream->lock); |
1711 | ||
a8cad461 JG |
1712 | size_ret = lttng_write(metadata_stream->stream_fd->fd, |
1713 | payload->data + sizeof(metadata_payload_header), | |
1714 | metadata_payload_size); | |
1715 | if (size_ret < metadata_payload_size) { | |
b8aa1682 JD |
1716 | ERR("Relay error writing metadata on file"); |
1717 | ret = -1; | |
7591bab1 | 1718 | goto end_put; |
b8aa1682 | 1719 | } |
1d4dfdef | 1720 | |
32d1569c | 1721 | size_ret = write_padding_to_file(metadata_stream->stream_fd->fd, |
a8cad461 JG |
1722 | metadata_payload_header.padding_size); |
1723 | if (size_ret < (int64_t) metadata_payload_header.padding_size) { | |
1724 | ret = -1; | |
7591bab1 | 1725 | goto end_put; |
1d4dfdef | 1726 | } |
2a174661 | 1727 | |
7591bab1 | 1728 | metadata_stream->metadata_received += |
a8cad461 | 1729 | metadata_payload_size + metadata_payload_header.padding_size; |
7591bab1 MD |
1730 | DBG2("Relay metadata written. Updated metadata_received %" PRIu64, |
1731 | metadata_stream->metadata_received); | |
1d4dfdef | 1732 | |
7591bab1 MD |
1733 | end_put: |
1734 | pthread_mutex_unlock(&metadata_stream->lock); | |
1735 | stream_put(metadata_stream); | |
b8aa1682 JD |
1736 | end: |
1737 | return ret; | |
1738 | } | |
1739 | ||
1740 | /* | |
1741 | * relay_send_version: send relayd version number | |
1742 | */ | |
a8cad461 JG |
1743 | static int relay_send_version(const struct lttcomm_relayd_hdr *recv_hdr, |
1744 | struct relay_connection *conn, | |
1745 | const struct lttng_buffer_view *payload) | |
b8aa1682 | 1746 | { |
7f51dcba | 1747 | int ret; |
a8cad461 | 1748 | ssize_t send_ret; |
092b6259 | 1749 | struct lttcomm_relayd_version reply, msg; |
40096142 | 1750 | bool compatible = true; |
b8aa1682 | 1751 | |
a8cad461 | 1752 | conn->version_check_done = true; |
b8aa1682 | 1753 | |
092b6259 | 1754 | /* Get version from the other side. */ |
a8cad461 JG |
1755 | if (payload->size < sizeof(msg)) { |
1756 | ERR("Unexpected payload size in \"relay_send_version\": expected >= %zu bytes, got %zu bytes", | |
1757 | sizeof(msg), payload->size); | |
092b6259 | 1758 | ret = -1; |
092b6259 DG |
1759 | goto end; |
1760 | } | |
1761 | ||
a8cad461 JG |
1762 | memcpy(&msg, payload->data, sizeof(msg)); |
1763 | msg.major = be32toh(msg.major); | |
1764 | msg.minor = be32toh(msg.minor); | |
1765 | ||
53efb85a | 1766 | memset(&reply, 0, sizeof(reply)); |
d83a952c MD |
1767 | reply.major = RELAYD_VERSION_COMM_MAJOR; |
1768 | reply.minor = RELAYD_VERSION_COMM_MINOR; | |
d4519fa3 JD |
1769 | |
1770 | /* Major versions must be the same */ | |
a8cad461 | 1771 | if (reply.major != msg.major) { |
6151a90f | 1772 | DBG("Incompatible major versions (%u vs %u), deleting session", |
a8cad461 | 1773 | reply.major, msg.major); |
40096142 | 1774 | compatible = false; |
d4519fa3 JD |
1775 | } |
1776 | ||
58eb9381 | 1777 | conn->major = reply.major; |
0f907de1 | 1778 | /* We adapt to the lowest compatible version */ |
a8cad461 | 1779 | if (reply.minor <= msg.minor) { |
58eb9381 | 1780 | conn->minor = reply.minor; |
0f907de1 | 1781 | } else { |
a8cad461 | 1782 | conn->minor = msg.minor; |
0f907de1 JD |
1783 | } |
1784 | ||
6151a90f JD |
1785 | reply.major = htobe32(reply.major); |
1786 | reply.minor = htobe32(reply.minor); | |
a8cad461 JG |
1787 | send_ret = conn->sock->ops->sendmsg(conn->sock, &reply, |
1788 | sizeof(reply), 0); | |
1789 | if (send_ret < (ssize_t) sizeof(reply)) { | |
1790 | ERR("Failed to send \"send version\" command reply (ret = %zd)", | |
1791 | send_ret); | |
1792 | ret = -1; | |
1793 | goto end; | |
1794 | } else { | |
1795 | ret = 0; | |
6151a90f JD |
1796 | } |
1797 | ||
40096142 JD |
1798 | if (!compatible) { |
1799 | ret = -1; | |
1800 | goto end; | |
1801 | } | |
1802 | ||
58eb9381 DG |
1803 | DBG("Version check done using protocol %u.%u", conn->major, |
1804 | conn->minor); | |
b8aa1682 JD |
1805 | |
1806 | end: | |
1807 | return ret; | |
1808 | } | |
1809 | ||
c8f59ee5 | 1810 | /* |
6d805429 | 1811 | * Check for data pending for a given stream id from the session daemon. |
c8f59ee5 | 1812 | */ |
a8cad461 JG |
1813 | static int relay_data_pending(const struct lttcomm_relayd_hdr *recv_hdr, |
1814 | struct relay_connection *conn, | |
1815 | const struct lttng_buffer_view *payload) | |
c8f59ee5 | 1816 | { |
58eb9381 | 1817 | struct relay_session *session = conn->session; |
6d805429 | 1818 | struct lttcomm_relayd_data_pending msg; |
c8f59ee5 DG |
1819 | struct lttcomm_relayd_generic_reply reply; |
1820 | struct relay_stream *stream; | |
a8cad461 | 1821 | ssize_t send_ret; |
c8f59ee5 | 1822 | int ret; |
c8f59ee5 | 1823 | |
6d805429 | 1824 | DBG("Data pending command received"); |
c8f59ee5 | 1825 | |
a8cad461 | 1826 | if (!session || !conn->version_check_done) { |
c8f59ee5 DG |
1827 | ERR("Trying to check for data before version check"); |
1828 | ret = -1; | |
1829 | goto end_no_session; | |
1830 | } | |
1831 | ||
a8cad461 JG |
1832 | if (payload->size < sizeof(msg)) { |
1833 | ERR("Unexpected payload size in \"relay_data_pending\": expected >= %zu bytes, got %zu bytes", | |
1834 | sizeof(msg), payload->size); | |
c8f59ee5 DG |
1835 | ret = -1; |
1836 | goto end_no_session; | |
1837 | } | |
a8cad461 JG |
1838 | memcpy(&msg, payload->data, sizeof(msg)); |
1839 | msg.stream_id = be64toh(msg.stream_id); | |
1840 | msg.last_net_seq_num = be64toh(msg.last_net_seq_num); | |
c8f59ee5 | 1841 | |
a8cad461 | 1842 | stream = stream_get_by_id(msg.stream_id); |
de91f48a | 1843 | if (stream == NULL) { |
c8f59ee5 | 1844 | ret = -1; |
7591bab1 | 1845 | goto end; |
c8f59ee5 DG |
1846 | } |
1847 | ||
7591bab1 MD |
1848 | pthread_mutex_lock(&stream->lock); |
1849 | ||
6d805429 | 1850 | DBG("Data pending for stream id %" PRIu64 " prev_seq %" PRIu64 |
a8cad461 JG |
1851 | " and last_seq %" PRIu64, msg.stream_id, |
1852 | stream->prev_seq, msg.last_net_seq_num); | |
c8f59ee5 | 1853 | |
33832e64 | 1854 | /* Avoid wrapping issue */ |
a8cad461 | 1855 | if (((int64_t) (stream->prev_seq - msg.last_net_seq_num)) >= 0) { |
6d805429 | 1856 | /* Data has in fact been written and is NOT pending */ |
c8f59ee5 | 1857 | ret = 0; |
6d805429 DG |
1858 | } else { |
1859 | /* Data still being streamed thus pending */ | |
1860 | ret = 1; | |
c8f59ee5 DG |
1861 | } |
1862 | ||
7591bab1 MD |
1863 | stream->data_pending_check_done = true; |
1864 | pthread_mutex_unlock(&stream->lock); | |
f7079f67 | 1865 | |
7591bab1 MD |
1866 | stream_put(stream); |
1867 | end: | |
c8f59ee5 | 1868 | |
53efb85a | 1869 | memset(&reply, 0, sizeof(reply)); |
c8f59ee5 | 1870 | reply.ret_code = htobe32(ret); |
a8cad461 JG |
1871 | send_ret = conn->sock->ops->sendmsg(conn->sock, &reply, sizeof(reply), 0); |
1872 | if (send_ret < (ssize_t) sizeof(reply)) { | |
1873 | ERR("Failed to send \"data pending\" command reply (ret = %zd)", | |
1874 | send_ret); | |
1875 | ret = -1; | |
c8f59ee5 DG |
1876 | } |
1877 | ||
1878 | end_no_session: | |
1879 | return ret; | |
1880 | } | |
1881 | ||
1882 | /* | |
1883 | * Wait for the control socket to reach a quiescent state. | |
1884 | * | |
7591bab1 MD |
1885 | * Note that for now, when receiving this command from the session |
1886 | * daemon, this means that every subsequent commands or data received on | |
1887 | * the control socket has been handled. So, this is why we simply return | |
1888 | * OK here. | |
c8f59ee5 | 1889 | */ |
a8cad461 JG |
1890 | static int relay_quiescent_control(const struct lttcomm_relayd_hdr *recv_hdr, |
1891 | struct relay_connection *conn, | |
1892 | const struct lttng_buffer_view *payload) | |
c8f59ee5 DG |
1893 | { |
1894 | int ret; | |
a8cad461 | 1895 | ssize_t send_ret; |
ad7051c0 | 1896 | struct relay_stream *stream; |
ad7051c0 | 1897 | struct lttcomm_relayd_quiescent_control msg; |
c8f59ee5 DG |
1898 | struct lttcomm_relayd_generic_reply reply; |
1899 | ||
1900 | DBG("Checking quiescent state on control socket"); | |
1901 | ||
a8cad461 | 1902 | if (!conn->session || !conn->version_check_done) { |
ad7051c0 DG |
1903 | ERR("Trying to check for data before version check"); |
1904 | ret = -1; | |
1905 | goto end_no_session; | |
1906 | } | |
1907 | ||
a8cad461 JG |
1908 | if (payload->size < sizeof(msg)) { |
1909 | ERR("Unexpected payload size in \"relay_quiescent_control\": expected >= %zu bytes, got %zu bytes", | |
1910 | sizeof(msg), payload->size); | |
ad7051c0 DG |
1911 | ret = -1; |
1912 | goto end_no_session; | |
1913 | } | |
a8cad461 JG |
1914 | memcpy(&msg, payload->data, sizeof(msg)); |
1915 | msg.stream_id = be64toh(msg.stream_id); | |
ad7051c0 | 1916 | |
a8cad461 | 1917 | stream = stream_get_by_id(msg.stream_id); |
7591bab1 MD |
1918 | if (!stream) { |
1919 | goto reply; | |
1920 | } | |
1921 | pthread_mutex_lock(&stream->lock); | |
1922 | stream->data_pending_check_done = true; | |
1923 | pthread_mutex_unlock(&stream->lock); | |
a8cad461 JG |
1924 | |
1925 | DBG("Relay quiescent control pending flag set to %" PRIu64, msg.stream_id); | |
7591bab1 MD |
1926 | stream_put(stream); |
1927 | reply: | |
53efb85a | 1928 | memset(&reply, 0, sizeof(reply)); |
c8f59ee5 | 1929 | reply.ret_code = htobe32(LTTNG_OK); |
a8cad461 JG |
1930 | send_ret = conn->sock->ops->sendmsg(conn->sock, &reply, sizeof(reply), 0); |
1931 | if (send_ret < (ssize_t) sizeof(reply)) { | |
1932 | ERR("Failed to send \"quiescent control\" command reply (ret = %zd)", | |
1933 | send_ret); | |
1934 | ret = -1; | |
1935 | } else { | |
1936 | ret = 0; | |
c8f59ee5 DG |
1937 | } |
1938 | ||
ad7051c0 | 1939 | end_no_session: |
c8f59ee5 DG |
1940 | return ret; |
1941 | } | |
1942 | ||
f7079f67 | 1943 | /* |
7591bab1 MD |
1944 | * Initialize a data pending command. This means that a consumer is about |
1945 | * to ask for data pending for each stream it holds. Simply iterate over | |
1946 | * all streams of a session and set the data_pending_check_done flag. | |
f7079f67 DG |
1947 | * |
1948 | * This command returns to the client a LTTNG_OK code. | |
1949 | */ | |
a8cad461 JG |
1950 | static int relay_begin_data_pending(const struct lttcomm_relayd_hdr *recv_hdr, |
1951 | struct relay_connection *conn, | |
1952 | const struct lttng_buffer_view *payload) | |
f7079f67 DG |
1953 | { |
1954 | int ret; | |
a8cad461 | 1955 | ssize_t send_ret; |
f7079f67 DG |
1956 | struct lttng_ht_iter iter; |
1957 | struct lttcomm_relayd_begin_data_pending msg; | |
1958 | struct lttcomm_relayd_generic_reply reply; | |
1959 | struct relay_stream *stream; | |
f7079f67 DG |
1960 | |
1961 | assert(recv_hdr); | |
58eb9381 | 1962 | assert(conn); |
f7079f67 DG |
1963 | |
1964 | DBG("Init streams for data pending"); | |
1965 | ||
a8cad461 | 1966 | if (!conn->session || !conn->version_check_done) { |
f7079f67 DG |
1967 | ERR("Trying to check for data before version check"); |
1968 | ret = -1; | |
1969 | goto end_no_session; | |
1970 | } | |
1971 | ||
a8cad461 JG |
1972 | if (payload->size < sizeof(msg)) { |
1973 | ERR("Unexpected payload size in \"relay_begin_data_pending\": expected >= %zu bytes, got %zu bytes", | |
1974 | sizeof(msg), payload->size); | |
f7079f67 DG |
1975 | ret = -1; |
1976 | goto end_no_session; | |
1977 | } | |
a8cad461 JG |
1978 | memcpy(&msg, payload->data, sizeof(msg)); |
1979 | msg.session_id = be64toh(msg.session_id); | |
f7079f67 DG |
1980 | |
1981 | /* | |
7591bab1 MD |
1982 | * Iterate over all streams to set the begin data pending flag. |
1983 | * For now, the streams are indexed by stream handle so we have | |
1984 | * to iterate over all streams to find the one associated with | |
1985 | * the right session_id. | |
f7079f67 DG |
1986 | */ |
1987 | rcu_read_lock(); | |
d3e2ba59 | 1988 | cds_lfht_for_each_entry(relay_streams_ht->ht, &iter.iter, stream, |
2a174661 | 1989 | node.node) { |
7591bab1 MD |
1990 | if (!stream_get(stream)) { |
1991 | continue; | |
1992 | } | |
a8cad461 | 1993 | if (stream->trace->session->id == msg.session_id) { |
7591bab1 MD |
1994 | pthread_mutex_lock(&stream->lock); |
1995 | stream->data_pending_check_done = false; | |
1996 | pthread_mutex_unlock(&stream->lock); | |
f7079f67 DG |
1997 | DBG("Set begin data pending flag to stream %" PRIu64, |
1998 | stream->stream_handle); | |
1999 | } | |
7591bab1 | 2000 | stream_put(stream); |
f7079f67 DG |
2001 | } |
2002 | rcu_read_unlock(); | |
2003 | ||
53efb85a | 2004 | memset(&reply, 0, sizeof(reply)); |
f7079f67 DG |
2005 | /* All good, send back reply. */ |
2006 | reply.ret_code = htobe32(LTTNG_OK); | |
2007 | ||
a8cad461 JG |
2008 | send_ret = conn->sock->ops->sendmsg(conn->sock, &reply, sizeof(reply), 0); |
2009 | if (send_ret < (ssize_t) sizeof(reply)) { | |
2010 | ERR("Failed to send \"begin data pending\" command reply (ret = %zd)", | |
2011 | send_ret); | |
2012 | ret = -1; | |
2013 | } else { | |
2014 | ret = 0; | |
f7079f67 DG |
2015 | } |
2016 | ||
2017 | end_no_session: | |
2018 | return ret; | |
2019 | } | |
2020 | ||
2021 | /* | |
7591bab1 MD |
2022 | * End data pending command. This will check, for a given session id, if |
2023 | * each stream associated with it has its data_pending_check_done flag | |
2024 | * set. If not, this means that the client lost track of the stream but | |
2025 | * the data is still being streamed on our side. In this case, we inform | |
2026 | * the client that data is in flight. | |
f7079f67 DG |
2027 | * |
2028 | * Return to the client if there is data in flight or not with a ret_code. | |
2029 | */ | |
a8cad461 JG |
2030 | static int relay_end_data_pending(const struct lttcomm_relayd_hdr *recv_hdr, |
2031 | struct relay_connection *conn, | |
2032 | const struct lttng_buffer_view *payload) | |
f7079f67 DG |
2033 | { |
2034 | int ret; | |
a8cad461 | 2035 | ssize_t send_ret; |
f7079f67 DG |
2036 | struct lttng_ht_iter iter; |
2037 | struct lttcomm_relayd_end_data_pending msg; | |
2038 | struct lttcomm_relayd_generic_reply reply; | |
2039 | struct relay_stream *stream; | |
f7079f67 DG |
2040 | uint32_t is_data_inflight = 0; |
2041 | ||
f7079f67 DG |
2042 | DBG("End data pending command"); |
2043 | ||
a8cad461 | 2044 | if (!conn->session || !conn->version_check_done) { |
f7079f67 DG |
2045 | ERR("Trying to check for data before version check"); |
2046 | ret = -1; | |
2047 | goto end_no_session; | |
2048 | } | |
2049 | ||
a8cad461 JG |
2050 | if (payload->size < sizeof(msg)) { |
2051 | ERR("Unexpected payload size in \"relay_end_data_pending\": expected >= %zu bytes, got %zu bytes", | |
2052 | sizeof(msg), payload->size); | |
f7079f67 DG |
2053 | ret = -1; |
2054 | goto end_no_session; | |
2055 | } | |
a8cad461 JG |
2056 | memcpy(&msg, payload->data, sizeof(msg)); |
2057 | msg.session_id = be64toh(msg.session_id); | |
f7079f67 | 2058 | |
7591bab1 MD |
2059 | /* |
2060 | * Iterate over all streams to see if the begin data pending | |
2061 | * flag is set. | |
2062 | */ | |
f7079f67 | 2063 | rcu_read_lock(); |
d3e2ba59 | 2064 | cds_lfht_for_each_entry(relay_streams_ht->ht, &iter.iter, stream, |
2a174661 | 2065 | node.node) { |
7591bab1 MD |
2066 | if (!stream_get(stream)) { |
2067 | continue; | |
2068 | } | |
a8cad461 | 2069 | if (stream->trace->session->id != msg.session_id) { |
7591bab1 MD |
2070 | stream_put(stream); |
2071 | continue; | |
2072 | } | |
2073 | pthread_mutex_lock(&stream->lock); | |
2074 | if (!stream->data_pending_check_done) { | |
2075 | if (!stream->closed || !(((int64_t) (stream->prev_seq - stream->last_net_seq_num)) >= 0)) { | |
2076 | is_data_inflight = 1; | |
2077 | DBG("Data is still in flight for stream %" PRIu64, | |
2078 | stream->stream_handle); | |
2079 | pthread_mutex_unlock(&stream->lock); | |
2080 | stream_put(stream); | |
2081 | break; | |
2082 | } | |
f7079f67 | 2083 | } |
7591bab1 MD |
2084 | pthread_mutex_unlock(&stream->lock); |
2085 | stream_put(stream); | |
f7079f67 DG |
2086 | } |
2087 | rcu_read_unlock(); | |
2088 | ||
53efb85a | 2089 | memset(&reply, 0, sizeof(reply)); |
f7079f67 DG |
2090 | /* All good, send back reply. */ |
2091 | reply.ret_code = htobe32(is_data_inflight); | |
2092 | ||
a8cad461 JG |
2093 | send_ret = conn->sock->ops->sendmsg(conn->sock, &reply, sizeof(reply), 0); |
2094 | if (send_ret < (ssize_t) sizeof(reply)) { | |
2095 | ERR("Failed to send \"end data pending\" command reply (ret = %zd)", | |
2096 | send_ret); | |
2097 | ret = -1; | |
2098 | } else { | |
2099 | ret = 0; | |
f7079f67 DG |
2100 | } |
2101 | ||
2102 | end_no_session: | |
2103 | return ret; | |
2104 | } | |
2105 | ||
1c20f0e2 JD |
2106 | /* |
2107 | * Receive an index for a specific stream. | |
2108 | * | |
2109 | * Return 0 on success else a negative value. | |
2110 | */ | |
a8cad461 JG |
2111 | static int relay_recv_index(const struct lttcomm_relayd_hdr *recv_hdr, |
2112 | struct relay_connection *conn, | |
2113 | const struct lttng_buffer_view *payload) | |
1c20f0e2 | 2114 | { |
a8cad461 JG |
2115 | int ret; |
2116 | ssize_t send_ret; | |
58eb9381 | 2117 | struct relay_session *session = conn->session; |
1c20f0e2 | 2118 | struct lttcomm_relayd_index index_info; |
7591bab1 | 2119 | struct relay_index *index; |
1c20f0e2 JD |
2120 | struct lttcomm_relayd_generic_reply reply; |
2121 | struct relay_stream *stream; | |
e0547b83 | 2122 | size_t msg_len; |
1c20f0e2 | 2123 | |
58eb9381 | 2124 | assert(conn); |
1c20f0e2 JD |
2125 | |
2126 | DBG("Relay receiving index"); | |
2127 | ||
a8cad461 | 2128 | if (!session || !conn->version_check_done) { |
1c20f0e2 JD |
2129 | ERR("Trying to close a stream before version check"); |
2130 | ret = -1; | |
2131 | goto end_no_session; | |
2132 | } | |
2133 | ||
e0547b83 MD |
2134 | msg_len = lttcomm_relayd_index_len( |
2135 | lttng_to_index_major(conn->major, conn->minor), | |
2136 | lttng_to_index_minor(conn->major, conn->minor)); | |
a8cad461 JG |
2137 | if (payload->size < msg_len) { |
2138 | ERR("Unexpected payload size in \"relay_recv_index\": expected >= %zu bytes, got %zu bytes", | |
2139 | msg_len, payload->size); | |
1c20f0e2 JD |
2140 | ret = -1; |
2141 | goto end_no_session; | |
2142 | } | |
a8cad461 JG |
2143 | memcpy(&index_info, payload->data, msg_len); |
2144 | index_info.relay_stream_id = be64toh(index_info.relay_stream_id); | |
2145 | index_info.net_seq_num = be64toh(index_info.net_seq_num); | |
2146 | index_info.packet_size = be64toh(index_info.packet_size); | |
2147 | index_info.content_size = be64toh(index_info.content_size); | |
2148 | index_info.timestamp_begin = be64toh(index_info.timestamp_begin); | |
2149 | index_info.timestamp_end = be64toh(index_info.timestamp_end); | |
2150 | index_info.events_discarded = be64toh(index_info.events_discarded); | |
2151 | index_info.stream_id = be64toh(index_info.stream_id); | |
1c20f0e2 | 2152 | |
a8cad461 JG |
2153 | if (conn->minor >= 8) { |
2154 | index_info.stream_instance_id = | |
2155 | be64toh(index_info.stream_instance_id); | |
2156 | index_info.packet_seq_num = be64toh(index_info.packet_seq_num); | |
2157 | } | |
1c20f0e2 | 2158 | |
a8cad461 | 2159 | stream = stream_get_by_id(index_info.relay_stream_id); |
1c20f0e2 | 2160 | if (!stream) { |
7591bab1 | 2161 | ERR("stream_get_by_id not found"); |
1c20f0e2 | 2162 | ret = -1; |
7591bab1 | 2163 | goto end; |
1c20f0e2 | 2164 | } |
7591bab1 | 2165 | pthread_mutex_lock(&stream->lock); |
1c20f0e2 | 2166 | |
d3e2ba59 JD |
2167 | /* Live beacon handling */ |
2168 | if (index_info.packet_size == 0) { | |
7591bab1 MD |
2169 | DBG("Received live beacon for stream %" PRIu64, |
2170 | stream->stream_handle); | |
d3e2ba59 JD |
2171 | |
2172 | /* | |
7591bab1 MD |
2173 | * Only flag a stream inactive when it has already |
2174 | * received data and no indexes are in flight. | |
d3e2ba59 | 2175 | */ |
a44ca2ca | 2176 | if (stream->index_received_seqcount > 0 |
7591bab1 | 2177 | && stream->indexes_in_flight == 0) { |
a8cad461 | 2178 | stream->beacon_ts_end = index_info.timestamp_end; |
d3e2ba59 JD |
2179 | } |
2180 | ret = 0; | |
7591bab1 | 2181 | goto end_stream_put; |
d3e2ba59 JD |
2182 | } else { |
2183 | stream->beacon_ts_end = -1ULL; | |
2184 | } | |
2185 | ||
528f2ffa | 2186 | if (stream->ctf_stream_id == -1ULL) { |
a8cad461 | 2187 | stream->ctf_stream_id = index_info.stream_id; |
528f2ffa | 2188 | } |
a8cad461 | 2189 | index = relay_index_get_by_id_or_create(stream, index_info.net_seq_num); |
7591bab1 MD |
2190 | if (!index) { |
2191 | ret = -1; | |
2192 | ERR("relay_index_get_by_id_or_create index NULL"); | |
2193 | goto end_stream_put; | |
1c20f0e2 | 2194 | } |
234cd636 | 2195 | if (set_index_control_data(index, &index_info, conn)) { |
7591bab1 MD |
2196 | ERR("set_index_control_data error"); |
2197 | relay_index_put(index); | |
2198 | ret = -1; | |
2199 | goto end_stream_put; | |
2200 | } | |
2201 | ret = relay_index_try_flush(index); | |
2202 | if (ret == 0) { | |
a44ca2ca MD |
2203 | tracefile_array_commit_seq(stream->tfa); |
2204 | stream->index_received_seqcount++; | |
7591bab1 MD |
2205 | } else if (ret > 0) { |
2206 | /* no flush. */ | |
2207 | ret = 0; | |
2208 | } else { | |
2209 | ERR("relay_index_try_flush error %d", ret); | |
2210 | relay_index_put(index); | |
2211 | ret = -1; | |
1c20f0e2 JD |
2212 | } |
2213 | ||
7591bab1 MD |
2214 | end_stream_put: |
2215 | pthread_mutex_unlock(&stream->lock); | |
2216 | stream_put(stream); | |
2217 | ||
2218 | end: | |
1c20f0e2 | 2219 | |
53efb85a | 2220 | memset(&reply, 0, sizeof(reply)); |
1c20f0e2 JD |
2221 | if (ret < 0) { |
2222 | reply.ret_code = htobe32(LTTNG_ERR_UNK); | |
2223 | } else { | |
2224 | reply.ret_code = htobe32(LTTNG_OK); | |
2225 | } | |
58eb9381 | 2226 | send_ret = conn->sock->ops->sendmsg(conn->sock, &reply, sizeof(reply), 0); |
a8cad461 JG |
2227 | if (send_ret < (ssize_t) sizeof(reply)) { |
2228 | ERR("Failed to send \"recv index\" command reply (ret = %zd)", send_ret); | |
2229 | ret = -1; | |
1c20f0e2 JD |
2230 | } |
2231 | ||
2232 | end_no_session: | |
2233 | return ret; | |
2234 | } | |
2235 | ||
a4baae1b JD |
2236 | /* |
2237 | * Receive the streams_sent message. | |
2238 | * | |
2239 | * Return 0 on success else a negative value. | |
2240 | */ | |
a8cad461 JG |
2241 | static int relay_streams_sent(const struct lttcomm_relayd_hdr *recv_hdr, |
2242 | struct relay_connection *conn, | |
2243 | const struct lttng_buffer_view *payload) | |
a4baae1b | 2244 | { |
a8cad461 JG |
2245 | int ret; |
2246 | ssize_t send_ret; | |
a4baae1b JD |
2247 | struct lttcomm_relayd_generic_reply reply; |
2248 | ||
58eb9381 | 2249 | assert(conn); |
a4baae1b JD |
2250 | |
2251 | DBG("Relay receiving streams_sent"); | |
2252 | ||
a8cad461 | 2253 | if (!conn->session || !conn->version_check_done) { |
a4baae1b JD |
2254 | ERR("Trying to close a stream before version check"); |
2255 | ret = -1; | |
2256 | goto end_no_session; | |
2257 | } | |
2258 | ||
2259 | /* | |
7591bab1 MD |
2260 | * Publish every pending stream in the connection recv list which are |
2261 | * now ready to be used by the viewer. | |
4a9daf17 | 2262 | */ |
7591bab1 | 2263 | publish_connection_local_streams(conn); |
4a9daf17 | 2264 | |
53efb85a | 2265 | memset(&reply, 0, sizeof(reply)); |
a4baae1b | 2266 | reply.ret_code = htobe32(LTTNG_OK); |
58eb9381 | 2267 | send_ret = conn->sock->ops->sendmsg(conn->sock, &reply, sizeof(reply), 0); |
a8cad461 JG |
2268 | if (send_ret < (ssize_t) sizeof(reply)) { |
2269 | ERR("Failed to send \"streams sent\" command reply (ret = %zd)", | |
2270 | send_ret); | |
2271 | ret = -1; | |
a4baae1b JD |
2272 | } else { |
2273 | /* Success. */ | |
2274 | ret = 0; | |
2275 | } | |
2276 | ||
2277 | end_no_session: | |
2278 | return ret; | |
2279 | } | |
2280 | ||
a8cad461 JG |
2281 | #define DBG_CMD(cmd_name, conn) \ |
2282 | DBG3("Processing \"%s\" command for socket %i", cmd_name, conn->sock->fd); | |
2283 | ||
2284 | static int relay_process_control_command(struct relay_connection *conn, | |
2285 | const struct lttcomm_relayd_hdr *header, | |
2286 | const struct lttng_buffer_view *payload) | |
b8aa1682 JD |
2287 | { |
2288 | int ret = 0; | |
2289 | ||
a8cad461 | 2290 | switch (header->cmd) { |
b8aa1682 | 2291 | case RELAYD_CREATE_SESSION: |
a8cad461 JG |
2292 | DBG_CMD("RELAYD_CREATE_SESSION", conn); |
2293 | ret = relay_create_session(header, conn, payload); | |
b8aa1682 | 2294 | break; |
b8aa1682 | 2295 | case RELAYD_ADD_STREAM: |
a8cad461 JG |
2296 | DBG_CMD("RELAYD_ADD_STREAM", conn); |
2297 | ret = relay_add_stream(header, conn, payload); | |
b8aa1682 JD |
2298 | break; |
2299 | case RELAYD_START_DATA: | |
a8cad461 JG |
2300 | DBG_CMD("RELAYD_START_DATA", conn); |
2301 | ret = relay_start(header, conn, payload); | |
b8aa1682 JD |
2302 | break; |
2303 | case RELAYD_SEND_METADATA: | |
a8cad461 JG |
2304 | DBG_CMD("RELAYD_SEND_METADATA", conn); |
2305 | ret = relay_recv_metadata(header, conn, payload); | |
b8aa1682 JD |
2306 | break; |
2307 | case RELAYD_VERSION: | |
a8cad461 JG |
2308 | DBG_CMD("RELAYD_VERSION", conn); |
2309 | ret = relay_send_version(header, conn, payload); | |
b8aa1682 | 2310 | break; |
173af62f | 2311 | case RELAYD_CLOSE_STREAM: |
a8cad461 JG |
2312 | DBG_CMD("RELAYD_CLOSE_STREAM", conn); |
2313 | ret = relay_close_stream(header, conn, payload); | |
173af62f | 2314 | break; |
6d805429 | 2315 | case RELAYD_DATA_PENDING: |
a8cad461 JG |
2316 | DBG_CMD("RELAYD_DATA_PENDING", conn); |
2317 | ret = relay_data_pending(header, conn, payload); | |
c8f59ee5 DG |
2318 | break; |
2319 | case RELAYD_QUIESCENT_CONTROL: | |
a8cad461 JG |
2320 | DBG_CMD("RELAYD_QUIESCENT_CONTROL", conn); |
2321 | ret = relay_quiescent_control(header, conn, payload); | |
c8f59ee5 | 2322 | break; |
f7079f67 | 2323 | case RELAYD_BEGIN_DATA_PENDING: |
a8cad461 JG |
2324 | DBG_CMD("RELAYD_BEGIN_DATA_PENDING", conn); |
2325 | ret = relay_begin_data_pending(header, conn, payload); | |
f7079f67 DG |
2326 | break; |
2327 | case RELAYD_END_DATA_PENDING: | |
a8cad461 JG |
2328 | DBG_CMD("RELAYD_END_DATA_PENDING", conn); |
2329 | ret = relay_end_data_pending(header, conn, payload); | |
f7079f67 | 2330 | break; |
1c20f0e2 | 2331 | case RELAYD_SEND_INDEX: |
a8cad461 JG |
2332 | DBG_CMD("RELAYD_SEND_INDEX", conn); |
2333 | ret = relay_recv_index(header, conn, payload); | |
1c20f0e2 | 2334 | break; |
a4baae1b | 2335 | case RELAYD_STREAMS_SENT: |
a8cad461 JG |
2336 | DBG_CMD("RELAYD_STREAMS_SENT", conn); |
2337 | ret = relay_streams_sent(header, conn, payload); | |
a4baae1b | 2338 | break; |
93ec662e | 2339 | case RELAYD_RESET_METADATA: |
a8cad461 JG |
2340 | DBG_CMD("RELAYD_RESET_METADATA", conn); |
2341 | ret = relay_reset_metadata(header, conn, payload); | |
93ec662e | 2342 | break; |
b8aa1682 JD |
2343 | case RELAYD_UPDATE_SYNC_INFO: |
2344 | default: | |
a8cad461 | 2345 | ERR("Received unknown command (%u)", header->cmd); |
58eb9381 | 2346 | relay_unknown_command(conn); |
b8aa1682 JD |
2347 | ret = -1; |
2348 | goto end; | |
2349 | } | |
2350 | ||
2351 | end: | |
2352 | return ret; | |
2353 | } | |
2354 | ||
a8cad461 JG |
2355 | static enum relay_connection_status relay_process_control_receive_payload( |
2356 | struct relay_connection *conn) | |
2357 | { | |
2358 | int ret = 0; | |
2359 | enum relay_connection_status status = RELAY_CONNECTION_STATUS_OK; | |
2360 | struct lttng_dynamic_buffer *reception_buffer = | |
2361 | &conn->protocol.ctrl.reception_buffer; | |
2362 | struct ctrl_connection_state_receive_payload *state = | |
2363 | &conn->protocol.ctrl.state.receive_payload; | |
2364 | struct lttng_buffer_view payload_view; | |
2365 | ||
2366 | if (state->left_to_receive == 0) { | |
2367 | /* Short-circuit for payload-less commands. */ | |
2368 | goto reception_complete; | |
2369 | } | |
2370 | ret = conn->sock->ops->recvmsg(conn->sock, | |
2371 | reception_buffer->data + state->received, | |
2372 | state->left_to_receive, MSG_DONTWAIT); | |
2373 | if (ret < 0) { | |
2374 | if (errno != EAGAIN && errno != EWOULDBLOCK) { | |
2375 | PERROR("Unable to receive command payload on sock %d", | |
2376 | conn->sock->fd); | |
2377 | status = RELAY_CONNECTION_STATUS_ERROR; | |
2378 | } | |
2379 | goto end; | |
2380 | } else if (ret == 0) { | |
2381 | DBG("Socket %d performed an orderly shutdown (received EOF)", conn->sock->fd); | |
2382 | status = RELAY_CONNECTION_STATUS_CLOSED; | |
2383 | goto end; | |
2384 | } | |
2385 | ||
2386 | assert(ret > 0); | |
2387 | assert(ret <= state->left_to_receive); | |
2388 | ||
2389 | state->left_to_receive -= ret; | |
2390 | state->received += ret; | |
2391 | ||
2392 | if (state->left_to_receive > 0) { | |
2393 | /* | |
2394 | * Can't transition to the protocol's next state, wait to | |
2395 | * receive the rest of the header. | |
2396 | */ | |
2397 | DBG3("Partial reception of control connection protocol payload (received %" PRIu64 " bytes, %" PRIu64 " bytes left to receive, fd = %i)", | |
2398 | state->received, state->left_to_receive, | |
2399 | conn->sock->fd); | |
2400 | goto end; | |
2401 | } | |
2402 | ||
2403 | reception_complete: | |
2404 | DBG("Done receiving control command payload: fd = %i, payload size = %" PRIu64 " bytes", | |
2405 | conn->sock->fd, state->received); | |
2406 | /* | |
2407 | * The payload required to process the command has been received. | |
2408 | * A view to the reception buffer is forwarded to the various | |
2409 | * commands and the state of the control is reset on success. | |
2410 | * | |
2411 | * Commands are responsible for sending their reply to the peer. | |
2412 | */ | |
2413 | payload_view = lttng_buffer_view_from_dynamic_buffer(reception_buffer, | |
2414 | 0, -1); | |
2415 | ret = relay_process_control_command(conn, | |
2416 | &state->header, &payload_view); | |
2417 | if (ret < 0) { | |
2418 | status = RELAY_CONNECTION_STATUS_ERROR; | |
2419 | goto end; | |
2420 | } | |
2421 | ||
2422 | ret = connection_reset_protocol_state(conn); | |
2423 | if (ret) { | |
2424 | status = RELAY_CONNECTION_STATUS_ERROR; | |
2425 | } | |
2426 | end: | |
2427 | return status; | |
2428 | } | |
2429 | ||
2430 | static enum relay_connection_status relay_process_control_receive_header( | |
2431 | struct relay_connection *conn) | |
2432 | { | |
2433 | int ret = 0; | |
2434 | enum relay_connection_status status = RELAY_CONNECTION_STATUS_OK; | |
2435 | struct lttcomm_relayd_hdr header; | |
2436 | struct lttng_dynamic_buffer *reception_buffer = | |
2437 | &conn->protocol.ctrl.reception_buffer; | |
2438 | struct ctrl_connection_state_receive_header *state = | |
2439 | &conn->protocol.ctrl.state.receive_header; | |
2440 | ||
2441 | assert(state->left_to_receive != 0); | |
2442 | ||
2443 | ret = conn->sock->ops->recvmsg(conn->sock, | |
2444 | reception_buffer->data + state->received, | |
2445 | state->left_to_receive, MSG_DONTWAIT); | |
2446 | if (ret < 0) { | |
2447 | if (errno != EAGAIN && errno != EWOULDBLOCK) { | |
2448 | PERROR("Unable to receive control command header on sock %d", | |
2449 | conn->sock->fd); | |
2450 | status = RELAY_CONNECTION_STATUS_ERROR; | |
2451 | } | |
2452 | goto end; | |
2453 | } else if (ret == 0) { | |
2454 | DBG("Socket %d performed an orderly shutdown (received EOF)", conn->sock->fd); | |
2455 | status = RELAY_CONNECTION_STATUS_CLOSED; | |
2456 | goto end; | |
2457 | } | |
2458 | ||
2459 | assert(ret > 0); | |
2460 | assert(ret <= state->left_to_receive); | |
2461 | ||
2462 | state->left_to_receive -= ret; | |
2463 | state->received += ret; | |
2464 | ||
2465 | if (state->left_to_receive > 0) { | |
2466 | /* | |
2467 | * Can't transition to the protocol's next state, wait to | |
2468 | * receive the rest of the header. | |
2469 | */ | |
2470 | DBG3("Partial reception of control connection protocol header (received %" PRIu64 " bytes, %" PRIu64 " bytes left to receive, fd = %i)", | |
2471 | state->received, state->left_to_receive, | |
2472 | conn->sock->fd); | |
2473 | goto end; | |
2474 | } | |
2475 | ||
2476 | /* Transition to next state: receiving the command's payload. */ | |
2477 | conn->protocol.ctrl.state_id = | |
2478 | CTRL_CONNECTION_STATE_RECEIVE_PAYLOAD; | |
2479 | memcpy(&header, reception_buffer->data, sizeof(header)); | |
2480 | header.circuit_id = be64toh(header.circuit_id); | |
2481 | header.data_size = be64toh(header.data_size); | |
2482 | header.cmd = be32toh(header.cmd); | |
2483 | header.cmd_version = be32toh(header.cmd_version); | |
2484 | memcpy(&conn->protocol.ctrl.state.receive_payload.header, | |
2485 | &header, sizeof(header)); | |
2486 | ||
2487 | DBG("Done receiving control command header: fd = %i, cmd = %" PRIu32 ", cmd_version = %" PRIu32 ", payload size = %" PRIu64 " bytes", | |
2488 | conn->sock->fd, header.cmd, header.cmd_version, | |
2489 | header.data_size); | |
2490 | ||
2491 | if (header.data_size > DEFAULT_NETWORK_RELAYD_CTRL_MAX_PAYLOAD_SIZE) { | |
2492 | ERR("Command header indicates a payload (%" PRIu64 " bytes) that exceeds the maximal payload size allowed on a control connection.", | |
2493 | header.data_size); | |
2494 | status = RELAY_CONNECTION_STATUS_ERROR; | |
2495 | goto end; | |
2496 | } | |
2497 | ||
2498 | conn->protocol.ctrl.state.receive_payload.left_to_receive = | |
2499 | header.data_size; | |
2500 | conn->protocol.ctrl.state.receive_payload.received = 0; | |
2501 | ret = lttng_dynamic_buffer_set_size(reception_buffer, | |
2502 | header.data_size); | |
2503 | if (ret) { | |
2504 | status = RELAY_CONNECTION_STATUS_ERROR; | |
2505 | goto end; | |
2506 | } | |
2507 | ||
2508 | if (header.data_size == 0) { | |
2509 | /* | |
2510 | * Manually invoke the next state as the poll loop | |
2511 | * will not wake-up to allow us to proceed further. | |
2512 | */ | |
2513 | status = relay_process_control_receive_payload(conn); | |
2514 | } | |
2515 | end: | |
2516 | return status; | |
2517 | } | |
2518 | ||
2519 | /* | |
2520 | * Process the commands received on the control socket | |
2521 | */ | |
2522 | static enum relay_connection_status relay_process_control( | |
2523 | struct relay_connection *conn) | |
2524 | { | |
2525 | enum relay_connection_status status; | |
2526 | ||
2527 | switch (conn->protocol.ctrl.state_id) { | |
2528 | case CTRL_CONNECTION_STATE_RECEIVE_HEADER: | |
2529 | status = relay_process_control_receive_header(conn); | |
2530 | break; | |
2531 | case CTRL_CONNECTION_STATE_RECEIVE_PAYLOAD: | |
2532 | status = relay_process_control_receive_payload(conn); | |
2533 | break; | |
2534 | default: | |
2535 | ERR("Unknown control connection protocol state encountered."); | |
2536 | abort(); | |
2537 | } | |
2538 | ||
2539 | return status; | |
2540 | } | |
2541 | ||
7d2f7452 DG |
2542 | /* |
2543 | * Handle index for a data stream. | |
2544 | * | |
7591bab1 | 2545 | * Called with the stream lock held. |
7d2f7452 DG |
2546 | * |
2547 | * Return 0 on success else a negative value. | |
2548 | */ | |
2549 | static int handle_index_data(struct relay_stream *stream, uint64_t net_seq_num, | |
a8cad461 | 2550 | bool rotate_index) |
7d2f7452 | 2551 | { |
7591bab1 MD |
2552 | int ret = 0; |
2553 | uint64_t data_offset; | |
2554 | struct relay_index *index; | |
7d2f7452 | 2555 | |
7d2f7452 DG |
2556 | /* Get data offset because we are about to update the index. */ |
2557 | data_offset = htobe64(stream->tracefile_size_current); | |
2558 | ||
65d66b19 MD |
2559 | DBG("handle_index_data: stream %" PRIu64 " net_seq_num %" PRIu64 " data offset %" PRIu64, |
2560 | stream->stream_handle, net_seq_num, stream->tracefile_size_current); | |
7591bab1 | 2561 | |
7d2f7452 | 2562 | /* |
7591bab1 MD |
2563 | * Lookup for an existing index for that stream id/sequence |
2564 | * number. If it exists, the control thread has already received the | |
2565 | * data for it, thus we need to write it to disk. | |
7d2f7452 | 2566 | */ |
7591bab1 | 2567 | index = relay_index_get_by_id_or_create(stream, net_seq_num); |
7d2f7452 | 2568 | if (!index) { |
7591bab1 MD |
2569 | ret = -1; |
2570 | goto end; | |
7d2f7452 DG |
2571 | } |
2572 | ||
e0547b83 MD |
2573 | if (rotate_index || !stream->index_file) { |
2574 | uint32_t major, minor; | |
7591bab1 | 2575 | |
e0547b83 MD |
2576 | /* Put ref on previous index_file. */ |
2577 | if (stream->index_file) { | |
2578 | lttng_index_file_put(stream->index_file); | |
2579 | stream->index_file = NULL; | |
7d2f7452 | 2580 | } |
e0547b83 MD |
2581 | major = stream->trace->session->major; |
2582 | minor = stream->trace->session->minor; | |
2583 | stream->index_file = lttng_index_file_create(stream->path_name, | |
2584 | stream->channel_name, | |
7591bab1 | 2585 | -1, -1, stream->tracefile_size, |
e0547b83 MD |
2586 | tracefile_array_get_file_index_head(stream->tfa), |
2587 | lttng_to_index_major(major, minor), | |
2588 | lttng_to_index_minor(major, minor)); | |
2589 | if (!stream->index_file) { | |
7591bab1 MD |
2590 | ret = -1; |
2591 | /* Put self-ref for this index due to error. */ | |
2592 | relay_index_put(index); | |
e0547b83 | 2593 | index = NULL; |
7591bab1 | 2594 | goto end; |
7d2f7452 | 2595 | } |
7d2f7452 DG |
2596 | } |
2597 | ||
e0547b83 | 2598 | if (relay_index_set_file(index, stream->index_file, data_offset)) { |
7591bab1 MD |
2599 | ret = -1; |
2600 | /* Put self-ref for this index due to error. */ | |
2601 | relay_index_put(index); | |
e0547b83 | 2602 | index = NULL; |
7591bab1 | 2603 | goto end; |
7d2f7452 DG |
2604 | } |
2605 | ||
7591bab1 MD |
2606 | ret = relay_index_try_flush(index); |
2607 | if (ret == 0) { | |
a44ca2ca MD |
2608 | tracefile_array_commit_seq(stream->tfa); |
2609 | stream->index_received_seqcount++; | |
7591bab1 MD |
2610 | } else if (ret > 0) { |
2611 | /* No flush. */ | |
2612 | ret = 0; | |
2613 | } else { | |
2614 | /* Put self-ref for this index due to error. */ | |
2615 | relay_index_put(index); | |
e0547b83 | 2616 | index = NULL; |
7591bab1 MD |
2617 | ret = -1; |
2618 | } | |
2619 | end: | |
7d2f7452 DG |
2620 | return ret; |
2621 | } | |
2622 | ||
a8cad461 JG |
2623 | static enum relay_connection_status relay_process_data_receive_header( |
2624 | struct relay_connection *conn) | |
b8aa1682 | 2625 | { |
a8cad461 JG |
2626 | int ret; |
2627 | enum relay_connection_status status = RELAY_CONNECTION_STATUS_OK; | |
2628 | struct data_connection_state_receive_header *state = | |
2629 | &conn->protocol.data.state.receive_header; | |
2630 | struct lttcomm_relayd_data_hdr header; | |
b8aa1682 | 2631 | struct relay_stream *stream; |
b8aa1682 | 2632 | |
a8cad461 JG |
2633 | assert(state->left_to_receive != 0); |
2634 | ||
2635 | ret = conn->sock->ops->recvmsg(conn->sock, | |
2636 | state->header_reception_buffer + state->received, | |
2637 | state->left_to_receive, MSG_DONTWAIT); | |
2638 | if (ret < 0) { | |
2639 | if (errno != EAGAIN && errno != EWOULDBLOCK) { | |
2640 | PERROR("Unable to receive data header on sock %d", conn->sock->fd); | |
2641 | status = RELAY_CONNECTION_STATUS_ERROR; | |
a6cd2b97 | 2642 | } |
a8cad461 JG |
2643 | goto end; |
2644 | } else if (ret == 0) { | |
2645 | /* Orderly shutdown. Not necessary to print an error. */ | |
2646 | DBG("Socket %d performed an orderly shutdown (received EOF)", conn->sock->fd); | |
2647 | status = RELAY_CONNECTION_STATUS_CLOSED; | |
b8aa1682 JD |
2648 | goto end; |
2649 | } | |
2650 | ||
a8cad461 JG |
2651 | assert(ret > 0); |
2652 | assert(ret <= state->left_to_receive); | |
2653 | ||
2654 | state->left_to_receive -= ret; | |
2655 | state->received += ret; | |
2656 | ||
2657 | if (state->left_to_receive > 0) { | |
2658 | /* | |
2659 | * Can't transition to the protocol's next state, wait to | |
2660 | * receive the rest of the header. | |
2661 | */ | |
2662 | DBG3("Partial reception of data connection header (received %" PRIu64 " bytes, %" PRIu64 " bytes left to receive, fd = %i)", | |
2663 | state->received, state->left_to_receive, | |
2664 | conn->sock->fd); | |
2665 | ret = 0; | |
7591bab1 | 2666 | goto end; |
b8aa1682 | 2667 | } |
b8aa1682 | 2668 | |
a8cad461 JG |
2669 | /* Transition to next state: receiving the payload. */ |
2670 | conn->protocol.data.state_id = DATA_CONNECTION_STATE_RECEIVE_PAYLOAD; | |
173af62f | 2671 | |
a8cad461 JG |
2672 | memcpy(&header, state->header_reception_buffer, sizeof(header)); |
2673 | header.circuit_id = be64toh(header.circuit_id); | |
2674 | header.stream_id = be64toh(header.stream_id); | |
2675 | header.data_size = be32toh(header.data_size); | |
2676 | header.net_seq_num = be64toh(header.net_seq_num); | |
2677 | header.padding_size = be32toh(header.padding_size); | |
2678 | memcpy(&conn->protocol.data.state.receive_payload.header, &header, sizeof(header)); | |
2679 | ||
2680 | conn->protocol.data.state.receive_payload.left_to_receive = | |
2681 | header.data_size; | |
2682 | conn->protocol.data.state.receive_payload.received = 0; | |
2683 | conn->protocol.data.state.receive_payload.rotate_index = false; | |
2684 | ||
2685 | DBG("Received data connection header on fd %i: circuit_id = %" PRIu64 ", stream_id = %" PRIu64 ", data_size = %" PRIu32 ", net_seq_num = %" PRIu64 ", padding_size = %" PRIu32, | |
2686 | conn->sock->fd, header.circuit_id, | |
2687 | header.stream_id, header.data_size, | |
2688 | header.net_seq_num, header.padding_size); | |
2689 | ||
2690 | stream = stream_get_by_id(header.stream_id); | |
2691 | if (!stream) { | |
2692 | DBG("relay_process_data_receive_payload: Cannot find stream %" PRIu64, | |
2693 | header.stream_id); | |
2694 | /* Protocol error. */ | |
2695 | status = RELAY_CONNECTION_STATUS_ERROR; | |
2696 | goto end; | |
2697 | } | |
b8aa1682 | 2698 | |
7591bab1 MD |
2699 | pthread_mutex_lock(&stream->lock); |
2700 | ||
1c20f0e2 | 2701 | /* Check if a rotation is needed. */ |
0f907de1 | 2702 | if (stream->tracefile_size > 0 && |
a8cad461 | 2703 | (stream->tracefile_size_current + header.data_size) > |
0f907de1 | 2704 | stream->tracefile_size) { |
a44ca2ca MD |
2705 | uint64_t old_id, new_id; |
2706 | ||
2707 | old_id = tracefile_array_get_file_index_head(stream->tfa); | |
2708 | tracefile_array_file_rotate(stream->tfa); | |
2709 | ||
2710 | /* new_id is updated by utils_rotate_stream_file. */ | |
2711 | new_id = old_id; | |
6b6b9a5a | 2712 | |
7591bab1 MD |
2713 | ret = utils_rotate_stream_file(stream->path_name, |
2714 | stream->channel_name, stream->tracefile_size, | |
2715 | stream->tracefile_count, -1, | |
2716 | -1, stream->stream_fd->fd, | |
a44ca2ca | 2717 | &new_id, &stream->stream_fd->fd); |
0f907de1 | 2718 | if (ret < 0) { |
a8cad461 JG |
2719 | ERR("Failed to rotate stream output file"); |
2720 | status = RELAY_CONNECTION_STATUS_ERROR; | |
7591bab1 MD |
2721 | goto end_stream_unlock; |
2722 | } | |
a8cad461 | 2723 | |
7591bab1 MD |
2724 | /* |
2725 | * Reset current size because we just performed a stream | |
2726 | * rotation. | |
2727 | */ | |
a6976990 | 2728 | stream->tracefile_size_current = 0; |
a8cad461 | 2729 | conn->protocol.data.state.receive_payload.rotate_index = true; |
1c20f0e2 JD |
2730 | } |
2731 | ||
a8cad461 JG |
2732 | ret = 0; |
2733 | end_stream_unlock: | |
2734 | pthread_mutex_unlock(&stream->lock); | |
2735 | stream_put(stream); | |
2736 | end: | |
2737 | return status; | |
2738 | } | |
2739 | ||
2740 | static enum relay_connection_status relay_process_data_receive_payload( | |
2741 | struct relay_connection *conn) | |
2742 | { | |
2743 | int ret; | |
2744 | enum relay_connection_status status = RELAY_CONNECTION_STATUS_OK; | |
2745 | struct relay_stream *stream; | |
2746 | struct data_connection_state_receive_payload *state = | |
2747 | &conn->protocol.data.state.receive_payload; | |
2748 | const size_t chunk_size = RECV_DATA_BUFFER_SIZE; | |
2749 | char data_buffer[chunk_size]; | |
2750 | bool partial_recv = false; | |
2751 | bool new_stream = false, close_requested = false; | |
2752 | uint64_t left_to_receive = state->left_to_receive; | |
2753 | struct relay_session *session; | |
2754 | ||
2755 | DBG3("Receiving data for stream id %" PRIu64 " seqnum %" PRIu64 ", %" PRIu64" bytes received, %" PRIu64 " bytes left to receive", | |
2756 | state->header.stream_id, state->header.net_seq_num, | |
2757 | state->received, left_to_receive); | |
2758 | ||
2759 | stream = stream_get_by_id(state->header.stream_id); | |
2760 | if (!stream) { | |
2761 | /* Protocol error. */ | |
2762 | ERR("relay_process_data_receive_payload: cannot find stream %" PRIu64, | |
2763 | state->header.stream_id); | |
2764 | status = RELAY_CONNECTION_STATUS_ERROR; | |
2765 | goto end; | |
2766 | } | |
2767 | ||
2768 | pthread_mutex_lock(&stream->lock); | |
2769 | session = stream->trace->session; | |
2770 | if (!conn->session) { | |
2771 | ret = connection_set_session(conn, session); | |
2772 | if (ret) { | |
2773 | status = RELAY_CONNECTION_STATUS_ERROR; | |
7591bab1 | 2774 | goto end_stream_unlock; |
1c20f0e2 | 2775 | } |
1c20f0e2 JD |
2776 | } |
2777 | ||
a8cad461 JG |
2778 | /* |
2779 | * The size of the "chunk" received on any iteration is bounded by: | |
2780 | * - the data left to receive, | |
2781 | * - the data immediately available on the socket, | |
2782 | * - the on-stack data buffer | |
2783 | */ | |
2784 | while (left_to_receive > 0 && !partial_recv) { | |
2785 | ssize_t write_ret; | |
2786 | size_t recv_size = min(left_to_receive, chunk_size); | |
1d4dfdef | 2787 | |
a8cad461 JG |
2788 | ret = conn->sock->ops->recvmsg(conn->sock, data_buffer, |
2789 | recv_size, MSG_DONTWAIT); | |
2790 | if (ret < 0) { | |
2791 | if (errno != EAGAIN && errno != EWOULDBLOCK) { | |
2792 | PERROR("Socket %d error", conn->sock->fd); | |
2793 | status = RELAY_CONNECTION_STATUS_ERROR; | |
0848dba7 | 2794 | } |
0848dba7 | 2795 | goto end_stream_unlock; |
a8cad461 JG |
2796 | } else if (ret == 0) { |
2797 | /* No more data ready to be consumed on socket. */ | |
2798 | DBG3("No more data ready for consumption on data socket of stream id %" PRIu64, | |
2799 | state->header.stream_id); | |
2800 | status = RELAY_CONNECTION_STATUS_CLOSED; | |
2801 | break; | |
2802 | } else if (ret < (int) recv_size) { | |
2803 | /* | |
2804 | * All the data available on the socket has been | |
2805 | * consumed. | |
2806 | */ | |
2807 | partial_recv = true; | |
0848dba7 MD |
2808 | } |
2809 | ||
a8cad461 JG |
2810 | recv_size = ret; |
2811 | ||
0848dba7 | 2812 | /* Write data to stream output fd. */ |
a8cad461 | 2813 | write_ret = lttng_write(stream->stream_fd->fd, data_buffer, |
0848dba7 | 2814 | recv_size); |
a8cad461 | 2815 | if (write_ret < (ssize_t) recv_size) { |
0848dba7 | 2816 | ERR("Relay error writing data to file"); |
a8cad461 | 2817 | status = RELAY_CONNECTION_STATUS_ERROR; |
0848dba7 MD |
2818 | goto end_stream_unlock; |
2819 | } | |
2820 | ||
a8cad461 JG |
2821 | left_to_receive -= recv_size; |
2822 | state->received += recv_size; | |
2823 | state->left_to_receive = left_to_receive; | |
2824 | ||
0848dba7 | 2825 | DBG2("Relay wrote %zd bytes to tracefile for stream id %" PRIu64, |
a8cad461 JG |
2826 | write_ret, stream->stream_handle); |
2827 | } | |
2828 | ||
2829 | if (state->left_to_receive > 0) { | |
2830 | /* | |
2831 | * Did not receive all the data expected, wait for more data to | |
2832 | * become available on the socket. | |
2833 | */ | |
2834 | DBG3("Partial receive on data connection of stream id %" PRIu64 ", %" PRIu64 " bytes received, %" PRIu64 " bytes left to receive", | |
2835 | state->header.stream_id, state->received, | |
2836 | state->left_to_receive); | |
2837 | goto end_stream_unlock; | |
0848dba7 | 2838 | } |
5ab7344e | 2839 | |
7591bab1 | 2840 | ret = write_padding_to_file(stream->stream_fd->fd, |
a8cad461 JG |
2841 | state->header.padding_size); |
2842 | if ((int64_t) ret < (int64_t) state->header.padding_size) { | |
65d66b19 | 2843 | ERR("write_padding_to_file: fail stream %" PRIu64 " net_seq_num %" PRIu64 " ret %d", |
a8cad461 JG |
2844 | stream->stream_handle, |
2845 | state->header.net_seq_num, ret); | |
2846 | status = RELAY_CONNECTION_STATUS_ERROR; | |
7591bab1 | 2847 | goto end_stream_unlock; |
1d4dfdef | 2848 | } |
a8cad461 JG |
2849 | |
2850 | ||
2851 | if (session->minor >= 4 && !session->snapshot) { | |
2852 | ret = handle_index_data(stream, state->header.net_seq_num, | |
2853 | state->rotate_index); | |
2854 | if (ret < 0) { | |
2855 | ERR("handle_index_data: fail stream %" PRIu64 " net_seq_num %" PRIu64 " ret %d", | |
2856 | stream->stream_handle, | |
2857 | state->header.net_seq_num, ret); | |
2858 | status = RELAY_CONNECTION_STATUS_ERROR; | |
2859 | goto end_stream_unlock; | |
2860 | } | |
2861 | } | |
2862 | ||
2863 | stream->tracefile_size_current += state->header.data_size + | |
2864 | state->header.padding_size; | |
2865 | ||
c0bae11d MD |
2866 | if (stream->prev_seq == -1ULL) { |
2867 | new_stream = true; | |
2868 | } | |
2869 | ||
a8cad461 JG |
2870 | stream->prev_seq = state->header.net_seq_num; |
2871 | ||
2872 | /* | |
2873 | * Resetting the protocol state (to RECEIVE_HEADER) will trash the | |
2874 | * contents of *state which are aliased (union) to the same location as | |
2875 | * the new state. Don't use it beyond this point. | |
2876 | */ | |
2877 | connection_reset_protocol_state(conn); | |
2878 | state = NULL; | |
173af62f | 2879 | |
7591bab1 | 2880 | end_stream_unlock: |
bda7c7b9 | 2881 | close_requested = stream->close_requested; |
7591bab1 | 2882 | pthread_mutex_unlock(&stream->lock); |
a8cad461 | 2883 | if (close_requested && left_to_receive == 0) { |
bda7c7b9 JG |
2884 | try_stream_close(stream); |
2885 | } | |
2886 | ||
c0bae11d MD |
2887 | if (new_stream) { |
2888 | pthread_mutex_lock(&session->lock); | |
2889 | uatomic_set(&session->new_streams, 1); | |
2890 | pthread_mutex_unlock(&session->lock); | |
2891 | } | |
a8cad461 | 2892 | |
7591bab1 | 2893 | stream_put(stream); |
b8aa1682 | 2894 | end: |
a8cad461 JG |
2895 | return status; |
2896 | } | |
2897 | ||
2898 | /* | |
2899 | * relay_process_data: Process the data received on the data socket | |
2900 | */ | |
2901 | static enum relay_connection_status relay_process_data( | |
2902 | struct relay_connection *conn) | |
2903 | { | |
2904 | enum relay_connection_status status; | |
2905 | ||
2906 | switch (conn->protocol.data.state_id) { | |
2907 | case DATA_CONNECTION_STATE_RECEIVE_HEADER: | |
2908 | status = relay_process_data_receive_header(conn); | |
2909 | break; | |
2910 | case DATA_CONNECTION_STATE_RECEIVE_PAYLOAD: | |
2911 | status = relay_process_data_receive_payload(conn); | |
2912 | break; | |
2913 | default: | |
2914 | ERR("Unexpected data connection communication state."); | |
2915 | abort(); | |
2916 | } | |
2917 | ||
2918 | return status; | |
b8aa1682 JD |
2919 | } |
2920 | ||
7591bab1 | 2921 | static void cleanup_connection_pollfd(struct lttng_poll_event *events, int pollfd) |
b8aa1682 JD |
2922 | { |
2923 | int ret; | |
2924 | ||
58eb9381 | 2925 | (void) lttng_poll_del(events, pollfd); |
b8aa1682 JD |
2926 | |
2927 | ret = close(pollfd); | |
2928 | if (ret < 0) { | |
2929 | ERR("Closing pollfd %d", pollfd); | |
2930 | } | |
2931 | } | |
2932 | ||
7591bab1 MD |
2933 | static void relay_thread_close_connection(struct lttng_poll_event *events, |
2934 | int pollfd, struct relay_connection *conn) | |
9d1bbf21 | 2935 | { |
7591bab1 | 2936 | const char *type_str; |
2a174661 | 2937 | |
7591bab1 MD |
2938 | switch (conn->type) { |
2939 | case RELAY_DATA: | |
2940 | type_str = "Data"; | |
2941 | break; | |
2942 | case RELAY_CONTROL: | |
2943 | type_str = "Control"; | |
2944 | break; | |
2945 | case RELAY_VIEWER_COMMAND: | |
2946 | type_str = "Viewer Command"; | |
2947 | break; | |
2948 | case RELAY_VIEWER_NOTIFICATION: | |
2949 | type_str = "Viewer Notification"; | |
2950 | break; | |
2951 | default: | |
2952 | type_str = "Unknown"; | |
9d1bbf21 | 2953 | } |
7591bab1 MD |
2954 | cleanup_connection_pollfd(events, pollfd); |
2955 | connection_put(conn); | |
2956 | DBG("%s connection closed with %d", type_str, pollfd); | |
b8aa1682 JD |
2957 | } |
2958 | ||
2959 | /* | |
2960 | * This thread does the actual work | |
2961 | */ | |
7591bab1 | 2962 | static void *relay_thread_worker(void *data) |
b8aa1682 | 2963 | { |
beaad64c DG |
2964 | int ret, err = -1, last_seen_data_fd = -1; |
2965 | uint32_t nb_fd; | |
b8aa1682 JD |
2966 | struct lttng_poll_event events; |
2967 | struct lttng_ht *relay_connections_ht; | |
b8aa1682 | 2968 | struct lttng_ht_iter iter; |
90e7d72f | 2969 | struct relay_connection *destroy_conn = NULL; |
b8aa1682 JD |
2970 | |
2971 | DBG("[thread] Relay worker started"); | |
2972 | ||
9d1bbf21 MD |
2973 | rcu_register_thread(); |
2974 | ||
55706a7d MD |
2975 | health_register(health_relayd, HEALTH_RELAYD_TYPE_WORKER); |
2976 | ||
9b5e0863 MD |
2977 | if (testpoint(relayd_thread_worker)) { |
2978 | goto error_testpoint; | |
2979 | } | |
2980 | ||
f385ae0a MD |
2981 | health_code_update(); |
2982 | ||
b8aa1682 JD |
2983 | /* table of connections indexed on socket */ |
2984 | relay_connections_ht = lttng_ht_new(0, LTTNG_HT_TYPE_ULONG); | |
095a4ae5 MD |
2985 | if (!relay_connections_ht) { |
2986 | goto relay_connections_ht_error; | |
2987 | } | |
b8aa1682 | 2988 | |
c38fd28d | 2989 | ret = create_named_thread_poll_set(&events, 2, "Worker thread epoll"); |
b8aa1682 JD |
2990 | if (ret < 0) { |
2991 | goto error_poll_create; | |
2992 | } | |
2993 | ||
58eb9381 | 2994 | ret = lttng_poll_add(&events, relay_conn_pipe[0], LPOLLIN | LPOLLRDHUP); |
b8aa1682 JD |
2995 | if (ret < 0) { |
2996 | goto error; | |
2997 | } | |
2998 | ||
beaad64c | 2999 | restart: |
b8aa1682 | 3000 | while (1) { |
beaad64c DG |
3001 | int idx = -1, i, seen_control = 0, last_notdel_data_fd = -1; |
3002 | ||
f385ae0a MD |
3003 | health_code_update(); |
3004 | ||
b8aa1682 | 3005 | /* Infinite blocking call, waiting for transmission */ |
87c1611d | 3006 | DBG3("Relayd worker thread polling..."); |
f385ae0a | 3007 | health_poll_entry(); |
b8aa1682 | 3008 | ret = lttng_poll_wait(&events, -1); |
f385ae0a | 3009 | health_poll_exit(); |
b8aa1682 JD |
3010 | if (ret < 0) { |
3011 | /* | |
3012 | * Restart interrupted system call. | |
3013 | */ | |
3014 | if (errno == EINTR) { | |
3015 | goto restart; | |
3016 | } | |
3017 | goto error; | |
3018 | } | |
3019 | ||
0d9c5d77 DG |
3020 | nb_fd = ret; |
3021 | ||
beaad64c | 3022 | /* |
7591bab1 MD |
3023 | * Process control. The control connection is |
3024 | * prioritized so we don't starve it with high | |
3025 | * throughput tracing data on the data connection. | |
beaad64c | 3026 | */ |
b8aa1682 JD |
3027 | for (i = 0; i < nb_fd; i++) { |
3028 | /* Fetch once the poll data */ | |
beaad64c DG |
3029 | uint32_t revents = LTTNG_POLL_GETEV(&events, i); |
3030 | int pollfd = LTTNG_POLL_GETFD(&events, i); | |
b8aa1682 | 3031 | |
f385ae0a MD |
3032 | health_code_update(); |
3033 | ||
fd20dac9 | 3034 | if (!revents) { |
7591bab1 MD |
3035 | /* |
3036 | * No activity for this FD (poll | |
3037 | * implementation). | |
3038 | */ | |
fd20dac9 MD |
3039 | continue; |
3040 | } | |
3041 | ||
b8aa1682 JD |
3042 | /* Thread quit pipe has been closed. Killing thread. */ |
3043 | ret = check_thread_quit_pipe(pollfd, revents); | |
3044 | if (ret) { | |
095a4ae5 MD |
3045 | err = 0; |
3046 | goto exit; | |
b8aa1682 JD |
3047 | } |
3048 | ||
58eb9381 DG |
3049 | /* Inspect the relay conn pipe for new connection */ |
3050 | if (pollfd == relay_conn_pipe[0]) { | |
03e43155 | 3051 | if (revents & LPOLLIN) { |
90e7d72f JG |
3052 | struct relay_connection *conn; |
3053 | ||
58eb9381 | 3054 | ret = lttng_read(relay_conn_pipe[0], &conn, sizeof(conn)); |
b8aa1682 JD |
3055 | if (ret < 0) { |
3056 | goto error; | |
3057 | } | |
58eb9381 DG |
3058 | lttng_poll_add(&events, conn->sock->fd, |
3059 | LPOLLIN | LPOLLRDHUP); | |
7591bab1 | 3060 | connection_ht_add(relay_connections_ht, conn); |
58eb9381 | 3061 | DBG("Connection socket %d added", conn->sock->fd); |
03e43155 MD |
3062 | } else if (revents & (LPOLLERR | LPOLLHUP | LPOLLRDHUP)) { |
3063 | ERR("Relay connection pipe error"); | |
3064 | goto error; | |
3065 | } else { | |
3066 | ERR("Unexpected poll events %u for sock %d", revents, pollfd); | |
3067 | goto error; | |
b8aa1682 | 3068 | } |
58eb9381 | 3069 | } else { |
90e7d72f JG |
3070 | struct relay_connection *ctrl_conn; |
3071 | ||
7591bab1 | 3072 | ctrl_conn = connection_get_by_sock(relay_connections_ht, pollfd); |
58eb9381 | 3073 | /* If not found, there is a synchronization issue. */ |
90e7d72f | 3074 | assert(ctrl_conn); |
58eb9381 | 3075 | |
03e43155 MD |
3076 | if (ctrl_conn->type == RELAY_DATA) { |
3077 | if (revents & LPOLLIN) { | |
beaad64c DG |
3078 | /* |
3079 | * Flag the last seen data fd not deleted. It will be | |
3080 | * used as the last seen fd if any fd gets deleted in | |
3081 | * this first loop. | |
3082 | */ | |
3083 | last_notdel_data_fd = pollfd; | |
3084 | } | |
03e43155 MD |
3085 | goto put_ctrl_connection; |
3086 | } | |
3087 | assert(ctrl_conn->type == RELAY_CONTROL); | |
3088 | ||
3089 | if (revents & LPOLLIN) { | |
a8cad461 JG |
3090 | enum relay_connection_status status; |
3091 | ||
3092 | status = relay_process_control(ctrl_conn); | |
3093 | if (status != RELAY_CONNECTION_STATUS_OK) { | |
3094 | /* | |
3095 | * On socket error flag the session as aborted to force | |
3096 | * the cleanup of its stream otherwise it can leak | |
3097 | * during the lifetime of the relayd. | |
3098 | * | |
3099 | * This prevents situations in which streams can be | |
3100 | * left opened because an index was received, the | |
3101 | * control connection is closed, and the data | |
3102 | * connection is closed (uncleanly) before the packet's | |
3103 | * data provided. | |
3104 | * | |
3105 | * Since the control connection encountered an error, | |
3106 | * it is okay to be conservative and close the | |
3107 | * session right now as we can't rely on the protocol | |
3108 | * being respected anymore. | |
3109 | */ | |
3110 | if (status == RELAY_CONNECTION_STATUS_ERROR) { | |
3111 | session_abort(ctrl_conn->session); | |
03e43155 | 3112 | } |
a8cad461 JG |
3113 | |
3114 | /* Clear the connection on error or close. */ | |
3115 | relay_thread_close_connection(&events, | |
3116 | pollfd, | |
3117 | ctrl_conn); | |
03e43155 | 3118 | } |
a8cad461 | 3119 | seen_control = 1; |
03e43155 MD |
3120 | } else if (revents & (LPOLLERR | LPOLLHUP | LPOLLRDHUP)) { |
3121 | relay_thread_close_connection(&events, | |
3122 | pollfd, ctrl_conn); | |
3123 | if (last_seen_data_fd == pollfd) { | |
3124 | last_seen_data_fd = last_notdel_data_fd; | |
3125 | } | |
58eb9381 | 3126 | } else { |
03e43155 MD |
3127 | ERR("Unexpected poll events %u for control sock %d", |
3128 | revents, pollfd); | |
3129 | connection_put(ctrl_conn); | |
3130 | goto error; | |
beaad64c | 3131 | } |
03e43155 | 3132 | put_ctrl_connection: |
7591bab1 | 3133 | connection_put(ctrl_conn); |
beaad64c DG |
3134 | } |
3135 | } | |
3136 | ||
3137 | /* | |
3138 | * The last loop handled a control request, go back to poll to make | |
3139 | * sure we prioritise the control socket. | |
3140 | */ | |
3141 | if (seen_control) { | |
3142 | continue; | |
3143 | } | |
3144 | ||
3145 | if (last_seen_data_fd >= 0) { | |
3146 | for (i = 0; i < nb_fd; i++) { | |
3147 | int pollfd = LTTNG_POLL_GETFD(&events, i); | |
f385ae0a MD |
3148 | |
3149 | health_code_update(); | |
3150 | ||
beaad64c DG |
3151 | if (last_seen_data_fd == pollfd) { |
3152 | idx = i; | |
3153 | break; | |
3154 | } | |
3155 | } | |
3156 | } | |
3157 | ||
3158 | /* Process data connection. */ | |
3159 | for (i = idx + 1; i < nb_fd; i++) { | |
3160 | /* Fetch the poll data. */ | |
3161 | uint32_t revents = LTTNG_POLL_GETEV(&events, i); | |
3162 | int pollfd = LTTNG_POLL_GETFD(&events, i); | |
90e7d72f | 3163 | struct relay_connection *data_conn; |
beaad64c | 3164 | |
f385ae0a MD |
3165 | health_code_update(); |
3166 | ||
fd20dac9 MD |
3167 | if (!revents) { |
3168 | /* No activity for this FD (poll implementation). */ | |
3169 | continue; | |
3170 | } | |
3171 | ||
beaad64c | 3172 | /* Skip the command pipe. It's handled in the first loop. */ |
58eb9381 | 3173 | if (pollfd == relay_conn_pipe[0]) { |
beaad64c DG |
3174 | continue; |
3175 | } | |
3176 | ||
7591bab1 | 3177 | data_conn = connection_get_by_sock(relay_connections_ht, pollfd); |
90e7d72f | 3178 | if (!data_conn) { |
fd20dac9 | 3179 | /* Skip it. Might be removed before. */ |
fd20dac9 MD |
3180 | continue; |
3181 | } | |
03e43155 MD |
3182 | if (data_conn->type == RELAY_CONTROL) { |
3183 | goto put_data_connection; | |
3184 | } | |
3185 | assert(data_conn->type == RELAY_DATA); | |
fd20dac9 MD |
3186 | |
3187 | if (revents & LPOLLIN) { | |
a8cad461 JG |
3188 | enum relay_connection_status status; |
3189 | ||
3190 | status = relay_process_data(data_conn); | |
3191 | /* Connection closed or error. */ | |
3192 | if (status != RELAY_CONNECTION_STATUS_OK) { | |
3193 | /* | |
3194 | * On socket error flag the session as aborted to force | |
3195 | * the cleanup of its stream otherwise it can leak | |
3196 | * during the lifetime of the relayd. | |
3197 | * | |
3198 | * This prevents situations in which streams can be | |
3199 | * left opened because an index was received, the | |
3200 | * control connection is closed, and the data | |
3201 | * connection is closed (uncleanly) before the packet's | |
3202 | * data provided. | |
3203 | * | |
3204 | * Since the data connection encountered an error, | |
3205 | * it is okay to be conservative and close the | |
3206 | * session right now as we can't rely on the protocol | |
3207 | * being respected anymore. | |
3208 | */ | |
3209 | if (status == RELAY_CONNECTION_STATUS_ERROR) { | |
3210 | session_abort(data_conn->session); | |
3211 | } | |
7591bab1 | 3212 | relay_thread_close_connection(&events, pollfd, |
03e43155 | 3213 | data_conn); |
fd20dac9 MD |
3214 | /* |
3215 | * Every goto restart call sets the last seen fd where | |
3216 | * here we don't really care since we gracefully | |
3217 | * continue the loop after the connection is deleted. | |
3218 | */ | |
3219 | } else { | |
3220 | /* Keep last seen port. */ | |
3221 | last_seen_data_fd = pollfd; | |
7591bab1 | 3222 | connection_put(data_conn); |
fd20dac9 | 3223 | goto restart; |
b8aa1682 | 3224 | } |
03e43155 MD |
3225 | } else if (revents & (LPOLLERR | LPOLLHUP | LPOLLRDHUP)) { |
3226 | relay_thread_close_connection(&events, pollfd, | |
3227 | data_conn); | |
3228 | } else { | |
3229 | ERR("Unknown poll events %u for data sock %d", | |
3230 | revents, pollfd); | |
b8aa1682 | 3231 | } |
03e43155 | 3232 | put_data_connection: |
7591bab1 | 3233 | connection_put(data_conn); |
b8aa1682 | 3234 | } |
beaad64c | 3235 | last_seen_data_fd = -1; |
b8aa1682 JD |
3236 | } |
3237 | ||
f385ae0a MD |
3238 | /* Normal exit, no error */ |
3239 | ret = 0; | |
3240 | ||
095a4ae5 | 3241 | exit: |
b8aa1682 | 3242 | error: |
58eb9381 | 3243 | /* Cleanup reamaining connection object. */ |
9d1bbf21 | 3244 | rcu_read_lock(); |
90e7d72f JG |
3245 | cds_lfht_for_each_entry(relay_connections_ht->ht, &iter.iter, |
3246 | destroy_conn, | |
58eb9381 | 3247 | sock_n.node) { |
f385ae0a | 3248 | health_code_update(); |
06a68401 | 3249 | |
a8cad461 | 3250 | session_abort(destroy_conn->session); |
06a68401 | 3251 | |
7591bab1 MD |
3252 | /* |
3253 | * No need to grab another ref, because we own | |
3254 | * destroy_conn. | |
3255 | */ | |
3256 | relay_thread_close_connection(&events, destroy_conn->sock->fd, | |
3257 | destroy_conn); | |
b8aa1682 | 3258 | } |
94d49140 | 3259 | rcu_read_unlock(); |
7591bab1 | 3260 | |
c38fd28d | 3261 | (void) fd_tracker_util_poll_clean(the_fd_tracker, &events); |
7d2f7452 | 3262 | error_poll_create: |
b8aa1682 | 3263 | lttng_ht_destroy(relay_connections_ht); |
095a4ae5 | 3264 | relay_connections_ht_error: |
58eb9381 | 3265 | /* Close relay conn pipes */ |
33f8df9f JG |
3266 | (void) fd_tracker_util_pipe_close(the_fd_tracker, |
3267 | relay_conn_pipe); | |
095a4ae5 MD |
3268 | if (err) { |
3269 | DBG("Thread exited with error"); | |
3270 | } | |
b8aa1682 | 3271 | DBG("Worker thread cleanup complete"); |
9b5e0863 | 3272 | error_testpoint: |
f385ae0a MD |
3273 | if (err) { |
3274 | health_error(); | |
3275 | ERR("Health error occurred in %s", __func__); | |
3276 | } | |
3277 | health_unregister(health_relayd); | |
9d1bbf21 | 3278 | rcu_unregister_thread(); |
b4aacfdc | 3279 | lttng_relay_stop_threads(); |
b8aa1682 JD |
3280 | return NULL; |
3281 | } | |
3282 | ||
3283 | /* | |
3284 | * Create the relay command pipe to wake thread_manage_apps. | |
3285 | * Closed in cleanup(). | |
3286 | */ | |
58eb9381 | 3287 | static int create_relay_conn_pipe(void) |
b8aa1682 | 3288 | { |
33f8df9f JG |
3289 | return fd_tracker_util_pipe_open_cloexec(the_fd_tracker, |
3290 | "Relayd connection pipe", relay_conn_pipe); | |
b8aa1682 JD |
3291 | } |
3292 | ||
ba1e629d JG |
3293 | static |
3294 | int stdio_open(void *data, int *fds) | |
3295 | { | |
3296 | fds[0] = fileno(stdout); | |
3297 | fds[1] = fileno(stderr); | |
3298 | return 0; | |
3299 | } | |
3300 | ||
3301 | static | |
3302 | int noop_close(void *data, int *fds) | |
3303 | { | |
3304 | return 0; | |
3305 | } | |
3306 | ||
3307 | static | |
3308 | int track_stdio(void) | |
3309 | { | |
3310 | int fds[2]; | |
3311 | const char *names[] = { "stdout", "stderr" }; | |
3312 | ||
3313 | return fd_tracker_open_unsuspendable_fd(the_fd_tracker, fds, | |
3314 | names, 2, stdio_open, NULL); | |
3315 | } | |
3316 | ||
3317 | static | |
3318 | void untrack_stdio(void) | |
3319 | { | |
3320 | int fds[] = { fileno(stdout), fileno(stderr) }; | |
3321 | ||
3322 | /* | |
3323 | * noop_close is used since we don't really want to close | |
3324 | * the stdio output fds; we merely want to stop tracking them. | |
3325 | */ | |
3326 | (void) fd_tracker_close_unsuspendable_fd(the_fd_tracker, | |
3327 | fds, 2, noop_close, NULL); | |
3328 | } | |
3329 | ||
b8aa1682 JD |
3330 | /* |
3331 | * main | |
3332 | */ | |
3333 | int main(int argc, char **argv) | |
3334 | { | |
178a0557 | 3335 | int ret = 0, retval = 0; |
b8aa1682 JD |
3336 | void *status; |
3337 | ||
b967f1cc JR |
3338 | /* Parse environment variables */ |
3339 | parse_env_options(); | |
3340 | ||
3341 | /* | |
3342 | * Parse arguments. | |
3343 | * Command line arguments overwrite environment. | |
3344 | */ | |
b8aa1682 | 3345 | progname = argv[0]; |
178a0557 MD |
3346 | if (set_options(argc, argv)) { |
3347 | retval = -1; | |
3348 | goto exit_options; | |
b8aa1682 JD |
3349 | } |
3350 | ||
178a0557 MD |
3351 | if (set_signal_handler()) { |
3352 | retval = -1; | |
3353 | goto exit_options; | |
b8aa1682 JD |
3354 | } |
3355 | ||
cdd566a2 JR |
3356 | relayd_config_log(); |
3357 | ||
3358 | if (opt_print_version) { | |
3359 | print_version(); | |
3360 | retval = 0; | |
3361 | goto exit_options; | |
3362 | } | |
3363 | ||
23c5429a JG |
3364 | ret = fclose(stdin); |
3365 | if (ret) { | |
3366 | PERROR("Failed to close stdin"); | |
3367 | goto exit_options; | |
3368 | } | |
4d513a50 DG |
3369 | /* Try to create directory if -o, --output is specified. */ |
3370 | if (opt_output_path) { | |
994fa64f DG |
3371 | if (*opt_output_path != '/') { |
3372 | ERR("Please specify an absolute path for -o, --output PATH"); | |
178a0557 MD |
3373 | retval = -1; |
3374 | goto exit_options; | |
994fa64f DG |
3375 | } |
3376 | ||
d77dded2 JG |
3377 | ret = utils_mkdir_recursive(opt_output_path, S_IRWXU | S_IRWXG, |
3378 | -1, -1); | |
4d513a50 DG |
3379 | if (ret < 0) { |
3380 | ERR("Unable to create %s", opt_output_path); | |
178a0557 MD |
3381 | retval = -1; |
3382 | goto exit_options; | |
4d513a50 DG |
3383 | } |
3384 | } | |
3385 | ||
b8aa1682 | 3386 | /* Daemonize */ |
b5218ffb | 3387 | if (opt_daemon || opt_background) { |
3fd27398 MD |
3388 | ret = lttng_daemonize(&child_ppid, &recv_child_signal, |
3389 | !opt_background); | |
b8aa1682 | 3390 | if (ret < 0) { |
178a0557 MD |
3391 | retval = -1; |
3392 | goto exit_options; | |
b8aa1682 | 3393 | } |
3fd27398 MD |
3394 | } |
3395 | ||
a174e6fc JR |
3396 | if (opt_working_directory) { |
3397 | ret = utils_change_working_dir(opt_working_directory); | |
3398 | if (ret) { | |
3399 | ERR("Changing working directory"); | |
3400 | goto exit_options; | |
3401 | } | |
3402 | } | |
bf5e21fd JG |
3403 | /* |
3404 | * The RCU thread registration (and use, through the fd-tracker's | |
3405 | * creation) is done after the daemonization to allow us to not | |
3406 | * deal with liburcu's fork() management as the call RCU needs to | |
3407 | * be restored. | |
3408 | */ | |
3409 | rcu_register_thread(); | |
3410 | ||
3411 | the_fd_tracker = fd_tracker_create(lttng_opt_fd_cap); | |
3412 | if (!the_fd_tracker) { | |
3413 | retval = -1; | |
3414 | goto exit_options; | |
3415 | } | |
a174e6fc | 3416 | |
ba1e629d JG |
3417 | ret = track_stdio(); |
3418 | if (ret) { | |
3419 | retval = -1; | |
3420 | goto exit_options; | |
3421 | } | |
3422 | ||
178a0557 MD |
3423 | /* Initialize thread health monitoring */ |
3424 | health_relayd = health_app_create(NR_HEALTH_RELAYD_TYPES); | |
3425 | if (!health_relayd) { | |
3426 | PERROR("health_app_create error"); | |
3427 | retval = -1; | |
3428 | goto exit_health_app_create; | |
3429 | } | |
3430 | ||
3fd27398 | 3431 | /* Create thread quit pipe */ |
178a0557 MD |
3432 | if (init_thread_quit_pipe()) { |
3433 | retval = -1; | |
3434 | goto exit_init_data; | |
b8aa1682 JD |
3435 | } |
3436 | ||
b8aa1682 | 3437 | /* Setup the thread apps communication pipe. */ |
178a0557 MD |
3438 | if (create_relay_conn_pipe()) { |
3439 | retval = -1; | |
3440 | goto exit_init_data; | |
b8aa1682 JD |
3441 | } |
3442 | ||
3443 | /* Init relay command queue. */ | |
8bdee6e2 | 3444 | cds_wfcq_init(&relay_conn_queue.head, &relay_conn_queue.tail); |
b8aa1682 | 3445 | |
554831e7 MD |
3446 | /* Initialize communication library */ |
3447 | lttcomm_init(); | |
87e45c13 | 3448 | lttcomm_inet_init(); |
554831e7 | 3449 | |
d3e2ba59 | 3450 | /* tables of sessions indexed by session ID */ |
7591bab1 MD |
3451 | sessions_ht = lttng_ht_new(0, LTTNG_HT_TYPE_U64); |
3452 | if (!sessions_ht) { | |
178a0557 MD |
3453 | retval = -1; |
3454 | goto exit_init_data; | |
d3e2ba59 JD |
3455 | } |
3456 | ||
3457 | /* tables of streams indexed by stream ID */ | |
2a174661 | 3458 | relay_streams_ht = lttng_ht_new(0, LTTNG_HT_TYPE_U64); |
d3e2ba59 | 3459 | if (!relay_streams_ht) { |
178a0557 MD |
3460 | retval = -1; |
3461 | goto exit_init_data; | |
d3e2ba59 JD |
3462 | } |
3463 | ||
3464 | /* tables of streams indexed by stream ID */ | |
92c6ca54 DG |
3465 | viewer_streams_ht = lttng_ht_new(0, LTTNG_HT_TYPE_U64); |
3466 | if (!viewer_streams_ht) { | |
178a0557 MD |
3467 | retval = -1; |
3468 | goto exit_init_data; | |
55706a7d MD |
3469 | } |
3470 | ||
2b56bd01 | 3471 | ret = init_health_quit_pipe(); |
178a0557 MD |
3472 | if (ret) { |
3473 | retval = -1; | |
3474 | goto exit_health_quit_pipe; | |
65931c8b MD |
3475 | } |
3476 | ||
3477 | /* Create thread to manage the client socket */ | |
1a1a34b4 | 3478 | ret = pthread_create(&health_thread, default_pthread_attr(), |
65931c8b | 3479 | thread_manage_health, (void *) NULL); |
178a0557 MD |
3480 | if (ret) { |
3481 | errno = ret; | |
65931c8b | 3482 | PERROR("pthread_create health"); |
178a0557 MD |
3483 | retval = -1; |
3484 | goto exit_health_thread; | |
65931c8b MD |
3485 | } |
3486 | ||
b8aa1682 | 3487 | /* Setup the dispatcher thread */ |
1a1a34b4 | 3488 | ret = pthread_create(&dispatcher_thread, default_pthread_attr(), |
b8aa1682 | 3489 | relay_thread_dispatcher, (void *) NULL); |
178a0557 MD |
3490 | if (ret) { |
3491 | errno = ret; | |
b8aa1682 | 3492 | PERROR("pthread_create dispatcher"); |
178a0557 MD |
3493 | retval = -1; |
3494 | goto exit_dispatcher_thread; | |
b8aa1682 JD |
3495 | } |
3496 | ||
3497 | /* Setup the worker thread */ | |
1a1a34b4 | 3498 | ret = pthread_create(&worker_thread, default_pthread_attr(), |
7591bab1 | 3499 | relay_thread_worker, NULL); |
178a0557 MD |
3500 | if (ret) { |
3501 | errno = ret; | |
b8aa1682 | 3502 | PERROR("pthread_create worker"); |
178a0557 MD |
3503 | retval = -1; |
3504 | goto exit_worker_thread; | |
b8aa1682 JD |
3505 | } |
3506 | ||
3507 | /* Setup the listener thread */ | |
1a1a34b4 | 3508 | ret = pthread_create(&listener_thread, default_pthread_attr(), |
b8aa1682 | 3509 | relay_thread_listener, (void *) NULL); |
178a0557 MD |
3510 | if (ret) { |
3511 | errno = ret; | |
b8aa1682 | 3512 | PERROR("pthread_create listener"); |
178a0557 MD |
3513 | retval = -1; |
3514 | goto exit_listener_thread; | |
b8aa1682 JD |
3515 | } |
3516 | ||
7591bab1 | 3517 | ret = relayd_live_create(live_uri); |
178a0557 | 3518 | if (ret) { |
d3e2ba59 | 3519 | ERR("Starting live viewer threads"); |
178a0557 | 3520 | retval = -1; |
50138f51 | 3521 | goto exit_live; |
d3e2ba59 JD |
3522 | } |
3523 | ||
178a0557 MD |
3524 | /* |
3525 | * This is where we start awaiting program completion (e.g. through | |
3526 | * signal that asks threads to teardown). | |
3527 | */ | |
3528 | ||
3529 | ret = relayd_live_join(); | |
3530 | if (ret) { | |
3531 | retval = -1; | |
3532 | } | |
50138f51 | 3533 | exit_live: |
178a0557 | 3534 | |
b8aa1682 | 3535 | ret = pthread_join(listener_thread, &status); |
178a0557 MD |
3536 | if (ret) { |
3537 | errno = ret; | |
3538 | PERROR("pthread_join listener_thread"); | |
3539 | retval = -1; | |
b8aa1682 JD |
3540 | } |
3541 | ||
178a0557 | 3542 | exit_listener_thread: |
b8aa1682 | 3543 | ret = pthread_join(worker_thread, &status); |
178a0557 MD |
3544 | if (ret) { |
3545 | errno = ret; | |
3546 | PERROR("pthread_join worker_thread"); | |
3547 | retval = -1; | |
b8aa1682 JD |
3548 | } |
3549 | ||
178a0557 | 3550 | exit_worker_thread: |
b8aa1682 | 3551 | ret = pthread_join(dispatcher_thread, &status); |
178a0557 MD |
3552 | if (ret) { |
3553 | errno = ret; | |
3554 | PERROR("pthread_join dispatcher_thread"); | |
3555 | retval = -1; | |
b8aa1682 | 3556 | } |
178a0557 | 3557 | exit_dispatcher_thread: |
42415026 | 3558 | |
65931c8b | 3559 | ret = pthread_join(health_thread, &status); |
178a0557 MD |
3560 | if (ret) { |
3561 | errno = ret; | |
3562 | PERROR("pthread_join health_thread"); | |
3563 | retval = -1; | |
65931c8b | 3564 | } |
178a0557 | 3565 | exit_health_thread: |
65931c8b | 3566 | |
2b56bd01 | 3567 | (void) fd_tracker_util_pipe_close(the_fd_tracker, health_quit_pipe); |
178a0557 | 3568 | exit_health_quit_pipe: |
65931c8b | 3569 | |
178a0557 | 3570 | exit_init_data: |
55706a7d | 3571 | health_app_destroy(health_relayd); |
55706a7d | 3572 | exit_health_app_create: |
178a0557 | 3573 | exit_options: |
2668465a MD |
3574 | /* |
3575 | * Wait for all pending call_rcu work to complete before tearing | |
3576 | * down data structures. call_rcu worker may be trying to | |
3577 | * perform lookups in those structures. | |
3578 | */ | |
3579 | rcu_barrier(); | |
7591bab1 MD |
3580 | relayd_cleanup(); |
3581 | ||
3582 | /* Ensure all prior call_rcu are done. */ | |
3583 | rcu_barrier(); | |
d3e2ba59 | 3584 | |
ba1e629d JG |
3585 | untrack_stdio(); |
3586 | /* | |
3587 | * fd_tracker_destroy() will log the contents of the fd-tracker | |
3588 | * if a leak is detected. | |
3589 | */ | |
bf5e21fd JG |
3590 | fd_tracker_destroy(the_fd_tracker); |
3591 | rcu_unregister_thread(); | |
3592 | ||
178a0557 | 3593 | if (!retval) { |
b8aa1682 | 3594 | exit(EXIT_SUCCESS); |
178a0557 MD |
3595 | } else { |
3596 | exit(EXIT_FAILURE); | |
b8aa1682 | 3597 | } |
b8aa1682 | 3598 | } |