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