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