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