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