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