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