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