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