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