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