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