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