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