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