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