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