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