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 | } | |
5d2369e3 | 1529 | |
7591bab1 | 1530 | pthread_mutex_unlock(&session->lock); |
a4baae1b JD |
1531 | } |
1532 | ||
348a81dc JG |
1533 | static int conform_channel_path(char *channel_path) |
1534 | { | |
1535 | int ret = 0; | |
1536 | ||
1537 | if (strstr("../", channel_path)) { | |
1538 | ERR("Refusing channel path as it walks up the path hierarchy: \"%s\"", | |
1539 | channel_path); | |
1540 | ret = -1; | |
1541 | goto end; | |
1542 | } | |
1543 | ||
1544 | if (*channel_path == '/') { | |
1545 | const size_t len = strlen(channel_path); | |
1546 | ||
1547 | /* | |
1548 | * Channel paths from peers prior to 2.11 are expressed as an | |
1549 | * absolute path that is, in reality, relative to the relay | |
1550 | * daemon's output directory. Remove the leading slash so it | |
1551 | * is correctly interpreted as a relative path later on. | |
1552 | * | |
1553 | * len (and not len - 1) is used to copy the trailing NULL. | |
1554 | */ | |
1555 | bcopy(channel_path + 1, channel_path, len); | |
1556 | } | |
1557 | end: | |
1558 | return ret; | |
1559 | } | |
1560 | ||
b8aa1682 JD |
1561 | /* |
1562 | * relay_add_stream: allocate a new stream for a session | |
1563 | */ | |
5312a3ed JG |
1564 | static int relay_add_stream(const struct lttcomm_relayd_hdr *recv_hdr, |
1565 | struct relay_connection *conn, | |
1566 | const struct lttng_buffer_view *payload) | |
b8aa1682 | 1567 | { |
7591bab1 MD |
1568 | int ret; |
1569 | ssize_t send_ret; | |
58eb9381 | 1570 | struct relay_session *session = conn->session; |
b8aa1682 JD |
1571 | struct relay_stream *stream = NULL; |
1572 | struct lttcomm_relayd_status_stream reply; | |
4030a636 | 1573 | struct ctf_trace *trace = NULL; |
7591bab1 MD |
1574 | uint64_t stream_handle = -1ULL; |
1575 | char *path_name = NULL, *channel_name = NULL; | |
1576 | uint64_t tracefile_size = 0, tracefile_count = 0; | |
348a81dc | 1577 | LTTNG_OPTIONAL(uint64_t) stream_chunk_id = {}; |
b8aa1682 | 1578 | |
5312a3ed | 1579 | if (!session || !conn->version_check_done) { |
b8aa1682 JD |
1580 | ERR("Trying to add a stream before version check"); |
1581 | ret = -1; | |
1582 | goto end_no_session; | |
1583 | } | |
1584 | ||
2f21a469 JR |
1585 | if (session->minor == 1) { |
1586 | /* For 2.1 */ | |
5312a3ed | 1587 | ret = cmd_recv_stream_2_1(payload, &path_name, |
7591bab1 | 1588 | &channel_name); |
2f21a469 JR |
1589 | } else if (session->minor > 1 && session->minor < 11) { |
1590 | /* From 2.2 to 2.10 */ | |
5312a3ed | 1591 | ret = cmd_recv_stream_2_2(payload, &path_name, |
7591bab1 | 1592 | &channel_name, &tracefile_size, &tracefile_count); |
2f21a469 JR |
1593 | } else { |
1594 | /* From 2.11 to ... */ | |
1595 | ret = cmd_recv_stream_2_11(payload, &path_name, | |
0b50e4b3 JG |
1596 | &channel_name, &tracefile_size, &tracefile_count, |
1597 | &stream_chunk_id.value); | |
1598 | stream_chunk_id.is_set = true; | |
0f907de1 | 1599 | } |
2f21a469 | 1600 | |
0f907de1 | 1601 | if (ret < 0) { |
7591bab1 | 1602 | goto send_reply; |
b8aa1682 JD |
1603 | } |
1604 | ||
348a81dc JG |
1605 | if (conform_channel_path(path_name)) { |
1606 | goto send_reply; | |
1607 | } | |
1608 | ||
2a635488 JR |
1609 | /* |
1610 | * Backward compatibility for --group-output-by-session. | |
1611 | * Prior to lttng 2.11, the complete path is passed by the stream. | |
1612 | * Starting at 2.11, lttng-relayd uses chunk. When dealing with producer | |
1613 | * >=2.11 the chunk is responsible for the output path. When dealing | |
1614 | * with producer < 2.11 the chunk output_path is the root output path | |
1615 | * and the stream carries the complete path (path_name). | |
1616 | * To support --group-output-by-session with older producer (<2.11), we | |
1617 | * need to craft the path based on the stream path. | |
1618 | */ | |
1619 | if (opt_group_output_by == RELAYD_GROUP_OUTPUT_BY_SESSION) { | |
1620 | if (conn->minor < 4) { | |
1621 | /* | |
1622 | * From 2.1 to 2.3, the session_name is not passed on | |
1623 | * the RELAYD_CREATE_SESSION command. The session name | |
1624 | * is necessary to detect the presence of a base_path | |
1625 | * inside the stream path. Without it we cannot perform | |
1626 | * a valid group-output-by-session transformation. | |
1627 | */ | |
1628 | WARN("Unable to perform a --group-by-session transformation for session %" PRIu64 | |
1629 | " for stream with path \"%s\" as it is produced by a peer using a protocol older than v2.4", | |
1630 | session->id, path_name); | |
1631 | } else if (conn->minor >= 4 && conn->minor < 11) { | |
1632 | char *group_by_session_path_name; | |
1633 | ||
1634 | assert(session->session_name[0] != '\0'); | |
1635 | ||
1636 | group_by_session_path_name = | |
1637 | backward_compat_group_by_session( | |
1638 | path_name, | |
d2cb4a90 JG |
1639 | session->session_name, |
1640 | session->creation_time.value); | |
2a635488 JR |
1641 | if (!group_by_session_path_name) { |
1642 | ERR("Failed to apply group by session to stream of session %" PRIu64, | |
1643 | session->id); | |
1644 | goto send_reply; | |
1645 | } | |
1646 | ||
1647 | DBG("Transformed session path from \"%s\" to \"%s\" to honor per-session name grouping", | |
1648 | path_name, group_by_session_path_name); | |
1649 | ||
1650 | free(path_name); | |
1651 | path_name = group_by_session_path_name; | |
1652 | } | |
1653 | } | |
1654 | ||
7591bab1 | 1655 | trace = ctf_trace_get_by_path_or_create(session, path_name); |
2a174661 | 1656 | if (!trace) { |
7591bab1 | 1657 | goto send_reply; |
2a174661 | 1658 | } |
2a174661 | 1659 | |
2a635488 | 1660 | /* This stream here has one reference on the trace. */ |
7591bab1 MD |
1661 | pthread_mutex_lock(&last_relay_stream_id_lock); |
1662 | stream_handle = ++last_relay_stream_id; | |
1663 | pthread_mutex_unlock(&last_relay_stream_id_lock); | |
d3e2ba59 | 1664 | |
7591bab1 MD |
1665 | /* We pass ownership of path_name and channel_name. */ |
1666 | stream = stream_create(trace, stream_handle, path_name, | |
348a81dc | 1667 | channel_name, tracefile_size, tracefile_count); |
7591bab1 MD |
1668 | path_name = NULL; |
1669 | channel_name = NULL; | |
a4baae1b | 1670 | |
2a174661 | 1671 | /* |
7591bab1 MD |
1672 | * Streams are the owners of their trace. Reference to trace is |
1673 | * kept within stream_create(). | |
2a174661 | 1674 | */ |
7591bab1 | 1675 | ctf_trace_put(trace); |
d3e2ba59 | 1676 | |
7591bab1 | 1677 | send_reply: |
53efb85a | 1678 | memset(&reply, 0, sizeof(reply)); |
7591bab1 MD |
1679 | reply.handle = htobe64(stream_handle); |
1680 | if (!stream) { | |
f73fabfd | 1681 | reply.ret_code = htobe32(LTTNG_ERR_UNK); |
b8aa1682 | 1682 | } else { |
f73fabfd | 1683 | reply.ret_code = htobe32(LTTNG_OK); |
b8aa1682 | 1684 | } |
5af40280 | 1685 | |
58eb9381 | 1686 | send_ret = conn->sock->ops->sendmsg(conn->sock, &reply, |
b8aa1682 | 1687 | sizeof(struct lttcomm_relayd_status_stream), 0); |
5312a3ed JG |
1688 | if (send_ret < (ssize_t) sizeof(reply)) { |
1689 | ERR("Failed to send \"add stream\" command reply (ret = %zd)", | |
1690 | send_ret); | |
1691 | ret = -1; | |
b8aa1682 JD |
1692 | } |
1693 | ||
1694 | end_no_session: | |
7591bab1 MD |
1695 | free(path_name); |
1696 | free(channel_name); | |
0f907de1 | 1697 | return ret; |
b8aa1682 JD |
1698 | } |
1699 | ||
173af62f DG |
1700 | /* |
1701 | * relay_close_stream: close a specific stream | |
1702 | */ | |
5312a3ed JG |
1703 | static int relay_close_stream(const struct lttcomm_relayd_hdr *recv_hdr, |
1704 | struct relay_connection *conn, | |
1705 | const struct lttng_buffer_view *payload) | |
173af62f | 1706 | { |
5312a3ed JG |
1707 | int ret; |
1708 | ssize_t send_ret; | |
58eb9381 | 1709 | struct relay_session *session = conn->session; |
173af62f DG |
1710 | struct lttcomm_relayd_close_stream stream_info; |
1711 | struct lttcomm_relayd_generic_reply reply; | |
1712 | struct relay_stream *stream; | |
173af62f DG |
1713 | |
1714 | DBG("Close stream received"); | |
1715 | ||
5312a3ed | 1716 | if (!session || !conn->version_check_done) { |
173af62f DG |
1717 | ERR("Trying to close a stream before version check"); |
1718 | ret = -1; | |
1719 | goto end_no_session; | |
1720 | } | |
1721 | ||
5312a3ed JG |
1722 | if (payload->size < sizeof(stream_info)) { |
1723 | ERR("Unexpected payload size in \"relay_close_stream\": expected >= %zu bytes, got %zu bytes", | |
1724 | sizeof(stream_info), payload->size); | |
173af62f DG |
1725 | ret = -1; |
1726 | goto end_no_session; | |
1727 | } | |
5312a3ed JG |
1728 | memcpy(&stream_info, payload->data, sizeof(stream_info)); |
1729 | stream_info.stream_id = be64toh(stream_info.stream_id); | |
1730 | stream_info.last_net_seq_num = be64toh(stream_info.last_net_seq_num); | |
173af62f | 1731 | |
5312a3ed | 1732 | stream = stream_get_by_id(stream_info.stream_id); |
173af62f DG |
1733 | if (!stream) { |
1734 | ret = -1; | |
7591bab1 | 1735 | goto end; |
173af62f | 1736 | } |
77f7bd85 MD |
1737 | |
1738 | /* | |
1739 | * Set last_net_seq_num before the close flag. Required by data | |
1740 | * pending check. | |
1741 | */ | |
7591bab1 | 1742 | pthread_mutex_lock(&stream->lock); |
5312a3ed | 1743 | stream->last_net_seq_num = stream_info.last_net_seq_num; |
77f7bd85 MD |
1744 | pthread_mutex_unlock(&stream->lock); |
1745 | ||
bda7c7b9 JG |
1746 | /* |
1747 | * This is one of the conditions which may trigger a stream close | |
1748 | * with the others being: | |
1749 | * 1) A close command is received for a stream | |
1750 | * 2) The control connection owning the stream is closed | |
1751 | * 3) We have received all of the stream's data _after_ a close | |
1752 | * request. | |
1753 | */ | |
1754 | try_stream_close(stream); | |
7591bab1 | 1755 | stream_put(stream); |
5312a3ed | 1756 | ret = 0; |
173af62f | 1757 | |
7591bab1 | 1758 | end: |
53efb85a | 1759 | memset(&reply, 0, sizeof(reply)); |
173af62f | 1760 | if (ret < 0) { |
f73fabfd | 1761 | reply.ret_code = htobe32(LTTNG_ERR_UNK); |
173af62f | 1762 | } else { |
f73fabfd | 1763 | reply.ret_code = htobe32(LTTNG_OK); |
173af62f | 1764 | } |
58eb9381 | 1765 | send_ret = conn->sock->ops->sendmsg(conn->sock, &reply, |
173af62f | 1766 | sizeof(struct lttcomm_relayd_generic_reply), 0); |
5312a3ed JG |
1767 | if (send_ret < (ssize_t) sizeof(reply)) { |
1768 | ERR("Failed to send \"close stream\" command reply (ret = %zd)", | |
1769 | send_ret); | |
1770 | ret = -1; | |
173af62f DG |
1771 | } |
1772 | ||
1773 | end_no_session: | |
1774 | return ret; | |
1775 | } | |
1776 | ||
93ec662e JD |
1777 | /* |
1778 | * relay_reset_metadata: reset a metadata stream | |
1779 | */ | |
1780 | static | |
5312a3ed JG |
1781 | int relay_reset_metadata(const struct lttcomm_relayd_hdr *recv_hdr, |
1782 | struct relay_connection *conn, | |
1783 | const struct lttng_buffer_view *payload) | |
93ec662e | 1784 | { |
5312a3ed JG |
1785 | int ret; |
1786 | ssize_t send_ret; | |
93ec662e JD |
1787 | struct relay_session *session = conn->session; |
1788 | struct lttcomm_relayd_reset_metadata stream_info; | |
1789 | struct lttcomm_relayd_generic_reply reply; | |
1790 | struct relay_stream *stream; | |
1791 | ||
1792 | DBG("Reset metadata received"); | |
1793 | ||
5312a3ed | 1794 | if (!session || !conn->version_check_done) { |
93ec662e JD |
1795 | ERR("Trying to reset a metadata stream before version check"); |
1796 | ret = -1; | |
1797 | goto end_no_session; | |
1798 | } | |
1799 | ||
5312a3ed JG |
1800 | if (payload->size < sizeof(stream_info)) { |
1801 | ERR("Unexpected payload size in \"relay_reset_metadata\": expected >= %zu bytes, got %zu bytes", | |
1802 | sizeof(stream_info), payload->size); | |
93ec662e JD |
1803 | ret = -1; |
1804 | goto end_no_session; | |
1805 | } | |
5312a3ed JG |
1806 | memcpy(&stream_info, payload->data, sizeof(stream_info)); |
1807 | stream_info.stream_id = be64toh(stream_info.stream_id); | |
1808 | stream_info.version = be64toh(stream_info.version); | |
1809 | ||
1810 | DBG("Update metadata to version %" PRIu64, stream_info.version); | |
93ec662e JD |
1811 | |
1812 | /* Unsupported for live sessions for now. */ | |
1813 | if (session->live_timer != 0) { | |
1814 | ret = -1; | |
1815 | goto end; | |
1816 | } | |
1817 | ||
5312a3ed | 1818 | stream = stream_get_by_id(stream_info.stream_id); |
93ec662e JD |
1819 | if (!stream) { |
1820 | ret = -1; | |
1821 | goto end; | |
1822 | } | |
1823 | pthread_mutex_lock(&stream->lock); | |
1824 | if (!stream->is_metadata) { | |
1825 | ret = -1; | |
1826 | goto end_unlock; | |
1827 | } | |
1828 | ||
c35f9726 | 1829 | ret = stream_reset_file(stream); |
93ec662e | 1830 | if (ret < 0) { |
c35f9726 JG |
1831 | ERR("Failed to reset metadata stream %" PRIu64 |
1832 | ": stream_path = %s, channel = %s", | |
1833 | stream->stream_handle, stream->path_name, | |
1834 | stream->channel_name); | |
93ec662e JD |
1835 | goto end_unlock; |
1836 | } | |
93ec662e JD |
1837 | end_unlock: |
1838 | pthread_mutex_unlock(&stream->lock); | |
1839 | stream_put(stream); | |
1840 | ||
1841 | end: | |
1842 | memset(&reply, 0, sizeof(reply)); | |
1843 | if (ret < 0) { | |
1844 | reply.ret_code = htobe32(LTTNG_ERR_UNK); | |
1845 | } else { | |
1846 | reply.ret_code = htobe32(LTTNG_OK); | |
1847 | } | |
1848 | send_ret = conn->sock->ops->sendmsg(conn->sock, &reply, | |
1849 | sizeof(struct lttcomm_relayd_generic_reply), 0); | |
5312a3ed JG |
1850 | if (send_ret < (ssize_t) sizeof(reply)) { |
1851 | ERR("Failed to send \"reset metadata\" command reply (ret = %zd)", | |
1852 | send_ret); | |
1853 | ret = -1; | |
93ec662e JD |
1854 | } |
1855 | ||
1856 | end_no_session: | |
1857 | return ret; | |
1858 | } | |
1859 | ||
b8aa1682 JD |
1860 | /* |
1861 | * relay_unknown_command: send -1 if received unknown command | |
1862 | */ | |
7591bab1 | 1863 | static void relay_unknown_command(struct relay_connection *conn) |
b8aa1682 JD |
1864 | { |
1865 | struct lttcomm_relayd_generic_reply reply; | |
5312a3ed | 1866 | ssize_t send_ret; |
b8aa1682 | 1867 | |
53efb85a | 1868 | memset(&reply, 0, sizeof(reply)); |
f73fabfd | 1869 | reply.ret_code = htobe32(LTTNG_ERR_UNK); |
5312a3ed JG |
1870 | send_ret = conn->sock->ops->sendmsg(conn->sock, &reply, sizeof(reply), 0); |
1871 | if (send_ret < sizeof(reply)) { | |
1872 | ERR("Failed to send \"unknown command\" command reply (ret = %zd)", send_ret); | |
b8aa1682 JD |
1873 | } |
1874 | } | |
1875 | ||
1876 | /* | |
1877 | * relay_start: send an acknowledgment to the client to tell if we are | |
1878 | * ready to receive data. We are ready if a session is established. | |
1879 | */ | |
5312a3ed JG |
1880 | static int relay_start(const struct lttcomm_relayd_hdr *recv_hdr, |
1881 | struct relay_connection *conn, | |
1882 | const struct lttng_buffer_view *payload) | |
b8aa1682 | 1883 | { |
5312a3ed JG |
1884 | int ret = 0; |
1885 | ssize_t send_ret; | |
b8aa1682 | 1886 | struct lttcomm_relayd_generic_reply reply; |
58eb9381 | 1887 | struct relay_session *session = conn->session; |
b8aa1682 JD |
1888 | |
1889 | if (!session) { | |
1890 | DBG("Trying to start the streaming without a session established"); | |
f73fabfd | 1891 | ret = htobe32(LTTNG_ERR_UNK); |
b8aa1682 JD |
1892 | } |
1893 | ||
53efb85a | 1894 | memset(&reply, 0, sizeof(reply)); |
5312a3ed JG |
1895 | reply.ret_code = htobe32(LTTNG_OK); |
1896 | send_ret = conn->sock->ops->sendmsg(conn->sock, &reply, | |
1897 | sizeof(reply), 0); | |
1898 | if (send_ret < (ssize_t) sizeof(reply)) { | |
1899 | ERR("Failed to send \"relay_start\" command reply (ret = %zd)", | |
1900 | send_ret); | |
1901 | ret = -1; | |
b8aa1682 JD |
1902 | } |
1903 | ||
1904 | return ret; | |
1905 | } | |
1906 | ||
b8aa1682 | 1907 | /* |
7591bab1 | 1908 | * relay_recv_metadata: receive the metadata for the session. |
b8aa1682 | 1909 | */ |
5312a3ed JG |
1910 | static int relay_recv_metadata(const struct lttcomm_relayd_hdr *recv_hdr, |
1911 | struct relay_connection *conn, | |
1912 | const struct lttng_buffer_view *payload) | |
b8aa1682 | 1913 | { |
32d1569c | 1914 | int ret = 0; |
58eb9381 | 1915 | struct relay_session *session = conn->session; |
5312a3ed | 1916 | struct lttcomm_relayd_metadata_payload metadata_payload_header; |
b8aa1682 | 1917 | struct relay_stream *metadata_stream; |
5312a3ed | 1918 | uint64_t metadata_payload_size; |
c35f9726 | 1919 | struct lttng_buffer_view packet_view; |
b8aa1682 JD |
1920 | |
1921 | if (!session) { | |
1922 | ERR("Metadata sent before version check"); | |
1923 | ret = -1; | |
1924 | goto end; | |
1925 | } | |
1926 | ||
5312a3ed | 1927 | if (recv_hdr->data_size < sizeof(struct lttcomm_relayd_metadata_payload)) { |
f6416125 MD |
1928 | ERR("Incorrect data size"); |
1929 | ret = -1; | |
1930 | goto end; | |
1931 | } | |
5312a3ed JG |
1932 | metadata_payload_size = recv_hdr->data_size - |
1933 | sizeof(struct lttcomm_relayd_metadata_payload); | |
f6416125 | 1934 | |
5312a3ed JG |
1935 | memcpy(&metadata_payload_header, payload->data, |
1936 | sizeof(metadata_payload_header)); | |
1937 | metadata_payload_header.stream_id = be64toh( | |
1938 | metadata_payload_header.stream_id); | |
1939 | metadata_payload_header.padding_size = be32toh( | |
1940 | metadata_payload_header.padding_size); | |
9d1bbf21 | 1941 | |
5312a3ed | 1942 | metadata_stream = stream_get_by_id(metadata_payload_header.stream_id); |
b8aa1682 JD |
1943 | if (!metadata_stream) { |
1944 | ret = -1; | |
7591bab1 | 1945 | goto end; |
b8aa1682 JD |
1946 | } |
1947 | ||
c35f9726 JG |
1948 | packet_view = lttng_buffer_view_from_view(payload, |
1949 | sizeof(metadata_payload_header), metadata_payload_size); | |
3e6e0df2 | 1950 | if (!lttng_buffer_view_is_valid(&packet_view)) { |
c35f9726 | 1951 | ERR("Invalid metadata packet length announced by header"); |
b8aa1682 | 1952 | ret = -1; |
7591bab1 | 1953 | goto end_put; |
b8aa1682 | 1954 | } |
1d4dfdef | 1955 | |
c35f9726 JG |
1956 | pthread_mutex_lock(&metadata_stream->lock); |
1957 | ret = stream_write(metadata_stream, &packet_view, | |
5312a3ed | 1958 | metadata_payload_header.padding_size); |
c35f9726 JG |
1959 | pthread_mutex_unlock(&metadata_stream->lock); |
1960 | if (ret){ | |
5312a3ed | 1961 | ret = -1; |
7591bab1 | 1962 | goto end_put; |
1d4dfdef | 1963 | } |
7591bab1 | 1964 | end_put: |
7591bab1 | 1965 | stream_put(metadata_stream); |
b8aa1682 JD |
1966 | end: |
1967 | return ret; | |
1968 | } | |
1969 | ||
1970 | /* | |
1971 | * relay_send_version: send relayd version number | |
1972 | */ | |
5312a3ed JG |
1973 | static int relay_send_version(const struct lttcomm_relayd_hdr *recv_hdr, |
1974 | struct relay_connection *conn, | |
1975 | const struct lttng_buffer_view *payload) | |
b8aa1682 | 1976 | { |
7f51dcba | 1977 | int ret; |
5312a3ed | 1978 | ssize_t send_ret; |
092b6259 | 1979 | struct lttcomm_relayd_version reply, msg; |
87cb6359 | 1980 | bool compatible = true; |
b8aa1682 | 1981 | |
5312a3ed | 1982 | conn->version_check_done = true; |
b8aa1682 | 1983 | |
092b6259 | 1984 | /* Get version from the other side. */ |
5312a3ed JG |
1985 | if (payload->size < sizeof(msg)) { |
1986 | ERR("Unexpected payload size in \"relay_send_version\": expected >= %zu bytes, got %zu bytes", | |
1987 | sizeof(msg), payload->size); | |
092b6259 | 1988 | ret = -1; |
092b6259 DG |
1989 | goto end; |
1990 | } | |
1991 | ||
5312a3ed JG |
1992 | memcpy(&msg, payload->data, sizeof(msg)); |
1993 | msg.major = be32toh(msg.major); | |
1994 | msg.minor = be32toh(msg.minor); | |
1995 | ||
53efb85a | 1996 | memset(&reply, 0, sizeof(reply)); |
d83a952c MD |
1997 | reply.major = RELAYD_VERSION_COMM_MAJOR; |
1998 | reply.minor = RELAYD_VERSION_COMM_MINOR; | |
d4519fa3 JD |
1999 | |
2000 | /* Major versions must be the same */ | |
5312a3ed | 2001 | if (reply.major != msg.major) { |
6151a90f | 2002 | DBG("Incompatible major versions (%u vs %u), deleting session", |
5312a3ed | 2003 | reply.major, msg.major); |
87cb6359 | 2004 | compatible = false; |
d4519fa3 JD |
2005 | } |
2006 | ||
58eb9381 | 2007 | conn->major = reply.major; |
0f907de1 | 2008 | /* We adapt to the lowest compatible version */ |
5312a3ed | 2009 | if (reply.minor <= msg.minor) { |
58eb9381 | 2010 | conn->minor = reply.minor; |
0f907de1 | 2011 | } else { |
5312a3ed | 2012 | conn->minor = msg.minor; |
0f907de1 JD |
2013 | } |
2014 | ||
6151a90f JD |
2015 | reply.major = htobe32(reply.major); |
2016 | reply.minor = htobe32(reply.minor); | |
5312a3ed JG |
2017 | send_ret = conn->sock->ops->sendmsg(conn->sock, &reply, |
2018 | sizeof(reply), 0); | |
2019 | if (send_ret < (ssize_t) sizeof(reply)) { | |
2020 | ERR("Failed to send \"send version\" command reply (ret = %zd)", | |
2021 | send_ret); | |
2022 | ret = -1; | |
2023 | goto end; | |
2024 | } else { | |
2025 | ret = 0; | |
6151a90f JD |
2026 | } |
2027 | ||
87cb6359 JD |
2028 | if (!compatible) { |
2029 | ret = -1; | |
2030 | goto end; | |
2031 | } | |
2032 | ||
58eb9381 DG |
2033 | DBG("Version check done using protocol %u.%u", conn->major, |
2034 | conn->minor); | |
b8aa1682 JD |
2035 | |
2036 | end: | |
2037 | return ret; | |
2038 | } | |
2039 | ||
c8f59ee5 | 2040 | /* |
6d805429 | 2041 | * Check for data pending for a given stream id from the session daemon. |
c8f59ee5 | 2042 | */ |
5312a3ed JG |
2043 | static int relay_data_pending(const struct lttcomm_relayd_hdr *recv_hdr, |
2044 | struct relay_connection *conn, | |
2045 | const struct lttng_buffer_view *payload) | |
c8f59ee5 | 2046 | { |
58eb9381 | 2047 | struct relay_session *session = conn->session; |
6d805429 | 2048 | struct lttcomm_relayd_data_pending msg; |
c8f59ee5 DG |
2049 | struct lttcomm_relayd_generic_reply reply; |
2050 | struct relay_stream *stream; | |
5312a3ed | 2051 | ssize_t send_ret; |
c8f59ee5 | 2052 | int ret; |
298a25ca | 2053 | uint64_t stream_seq; |
c8f59ee5 | 2054 | |
6d805429 | 2055 | DBG("Data pending command received"); |
c8f59ee5 | 2056 | |
5312a3ed | 2057 | if (!session || !conn->version_check_done) { |
c8f59ee5 DG |
2058 | ERR("Trying to check for data before version check"); |
2059 | ret = -1; | |
2060 | goto end_no_session; | |
2061 | } | |
2062 | ||
5312a3ed JG |
2063 | if (payload->size < sizeof(msg)) { |
2064 | ERR("Unexpected payload size in \"relay_data_pending\": expected >= %zu bytes, got %zu bytes", | |
2065 | sizeof(msg), payload->size); | |
c8f59ee5 DG |
2066 | ret = -1; |
2067 | goto end_no_session; | |
2068 | } | |
5312a3ed JG |
2069 | memcpy(&msg, payload->data, sizeof(msg)); |
2070 | msg.stream_id = be64toh(msg.stream_id); | |
2071 | msg.last_net_seq_num = be64toh(msg.last_net_seq_num); | |
c8f59ee5 | 2072 | |
5312a3ed | 2073 | stream = stream_get_by_id(msg.stream_id); |
de91f48a | 2074 | if (stream == NULL) { |
c8f59ee5 | 2075 | ret = -1; |
7591bab1 | 2076 | goto end; |
c8f59ee5 DG |
2077 | } |
2078 | ||
7591bab1 MD |
2079 | pthread_mutex_lock(&stream->lock); |
2080 | ||
298a25ca JG |
2081 | if (session_streams_have_index(session)) { |
2082 | /* | |
2083 | * Ensure that both the index and stream data have been | |
2084 | * flushed up to the requested point. | |
2085 | */ | |
a8f9f353 | 2086 | stream_seq = min(stream->prev_data_seq, stream->prev_index_seq); |
298a25ca | 2087 | } else { |
a8f9f353 | 2088 | stream_seq = stream->prev_data_seq; |
298a25ca | 2089 | } |
a8f9f353 | 2090 | DBG("Data pending for stream id %" PRIu64 ": prev_data_seq %" PRIu64 |
298a25ca JG |
2091 | ", prev_index_seq %" PRIu64 |
2092 | ", and last_seq %" PRIu64, msg.stream_id, | |
a8f9f353 | 2093 | stream->prev_data_seq, stream->prev_index_seq, |
298a25ca | 2094 | msg.last_net_seq_num); |
c8f59ee5 | 2095 | |
33832e64 | 2096 | /* Avoid wrapping issue */ |
298a25ca | 2097 | if (((int64_t) (stream_seq - msg.last_net_seq_num)) >= 0) { |
6d805429 | 2098 | /* Data has in fact been written and is NOT pending */ |
c8f59ee5 | 2099 | ret = 0; |
6d805429 DG |
2100 | } else { |
2101 | /* Data still being streamed thus pending */ | |
2102 | ret = 1; | |
c8f59ee5 DG |
2103 | } |
2104 | ||
7591bab1 MD |
2105 | stream->data_pending_check_done = true; |
2106 | pthread_mutex_unlock(&stream->lock); | |
f7079f67 | 2107 | |
7591bab1 MD |
2108 | stream_put(stream); |
2109 | end: | |
c8f59ee5 | 2110 | |
53efb85a | 2111 | memset(&reply, 0, sizeof(reply)); |
c8f59ee5 | 2112 | reply.ret_code = htobe32(ret); |
5312a3ed JG |
2113 | send_ret = conn->sock->ops->sendmsg(conn->sock, &reply, sizeof(reply), 0); |
2114 | if (send_ret < (ssize_t) sizeof(reply)) { | |
2115 | ERR("Failed to send \"data pending\" command reply (ret = %zd)", | |
2116 | send_ret); | |
2117 | ret = -1; | |
c8f59ee5 DG |
2118 | } |
2119 | ||
2120 | end_no_session: | |
2121 | return ret; | |
2122 | } | |
2123 | ||
2124 | /* | |
2125 | * Wait for the control socket to reach a quiescent state. | |
2126 | * | |
7591bab1 MD |
2127 | * Note that for now, when receiving this command from the session |
2128 | * daemon, this means that every subsequent commands or data received on | |
2129 | * the control socket has been handled. So, this is why we simply return | |
2130 | * OK here. | |
c8f59ee5 | 2131 | */ |
5312a3ed JG |
2132 | static int relay_quiescent_control(const struct lttcomm_relayd_hdr *recv_hdr, |
2133 | struct relay_connection *conn, | |
2134 | const struct lttng_buffer_view *payload) | |
c8f59ee5 DG |
2135 | { |
2136 | int ret; | |
5312a3ed | 2137 | ssize_t send_ret; |
ad7051c0 | 2138 | struct relay_stream *stream; |
ad7051c0 | 2139 | struct lttcomm_relayd_quiescent_control msg; |
c8f59ee5 DG |
2140 | struct lttcomm_relayd_generic_reply reply; |
2141 | ||
2142 | DBG("Checking quiescent state on control socket"); | |
2143 | ||
5312a3ed | 2144 | if (!conn->session || !conn->version_check_done) { |
ad7051c0 DG |
2145 | ERR("Trying to check for data before version check"); |
2146 | ret = -1; | |
2147 | goto end_no_session; | |
2148 | } | |
2149 | ||
5312a3ed JG |
2150 | if (payload->size < sizeof(msg)) { |
2151 | ERR("Unexpected payload size in \"relay_quiescent_control\": expected >= %zu bytes, got %zu bytes", | |
2152 | sizeof(msg), payload->size); | |
ad7051c0 DG |
2153 | ret = -1; |
2154 | goto end_no_session; | |
2155 | } | |
5312a3ed JG |
2156 | memcpy(&msg, payload->data, sizeof(msg)); |
2157 | msg.stream_id = be64toh(msg.stream_id); | |
ad7051c0 | 2158 | |
5312a3ed | 2159 | stream = stream_get_by_id(msg.stream_id); |
7591bab1 MD |
2160 | if (!stream) { |
2161 | goto reply; | |
2162 | } | |
2163 | pthread_mutex_lock(&stream->lock); | |
2164 | stream->data_pending_check_done = true; | |
2165 | pthread_mutex_unlock(&stream->lock); | |
5312a3ed JG |
2166 | |
2167 | DBG("Relay quiescent control pending flag set to %" PRIu64, msg.stream_id); | |
7591bab1 MD |
2168 | stream_put(stream); |
2169 | reply: | |
53efb85a | 2170 | memset(&reply, 0, sizeof(reply)); |
c8f59ee5 | 2171 | reply.ret_code = htobe32(LTTNG_OK); |
5312a3ed JG |
2172 | send_ret = conn->sock->ops->sendmsg(conn->sock, &reply, sizeof(reply), 0); |
2173 | if (send_ret < (ssize_t) sizeof(reply)) { | |
2174 | ERR("Failed to send \"quiescent control\" command reply (ret = %zd)", | |
2175 | send_ret); | |
2176 | ret = -1; | |
2177 | } else { | |
2178 | ret = 0; | |
c8f59ee5 DG |
2179 | } |
2180 | ||
ad7051c0 | 2181 | end_no_session: |
c8f59ee5 DG |
2182 | return ret; |
2183 | } | |
2184 | ||
f7079f67 | 2185 | /* |
7591bab1 MD |
2186 | * Initialize a data pending command. This means that a consumer is about |
2187 | * to ask for data pending for each stream it holds. Simply iterate over | |
2188 | * all streams of a session and set the data_pending_check_done flag. | |
f7079f67 DG |
2189 | * |
2190 | * This command returns to the client a LTTNG_OK code. | |
2191 | */ | |
5312a3ed JG |
2192 | static int relay_begin_data_pending(const struct lttcomm_relayd_hdr *recv_hdr, |
2193 | struct relay_connection *conn, | |
2194 | const struct lttng_buffer_view *payload) | |
f7079f67 DG |
2195 | { |
2196 | int ret; | |
5312a3ed | 2197 | ssize_t send_ret; |
f7079f67 DG |
2198 | struct lttng_ht_iter iter; |
2199 | struct lttcomm_relayd_begin_data_pending msg; | |
2200 | struct lttcomm_relayd_generic_reply reply; | |
2201 | struct relay_stream *stream; | |
f7079f67 DG |
2202 | |
2203 | assert(recv_hdr); | |
58eb9381 | 2204 | assert(conn); |
f7079f67 DG |
2205 | |
2206 | DBG("Init streams for data pending"); | |
2207 | ||
5312a3ed | 2208 | if (!conn->session || !conn->version_check_done) { |
f7079f67 DG |
2209 | ERR("Trying to check for data before version check"); |
2210 | ret = -1; | |
2211 | goto end_no_session; | |
2212 | } | |
2213 | ||
5312a3ed JG |
2214 | if (payload->size < sizeof(msg)) { |
2215 | ERR("Unexpected payload size in \"relay_begin_data_pending\": expected >= %zu bytes, got %zu bytes", | |
2216 | sizeof(msg), payload->size); | |
f7079f67 DG |
2217 | ret = -1; |
2218 | goto end_no_session; | |
2219 | } | |
5312a3ed JG |
2220 | memcpy(&msg, payload->data, sizeof(msg)); |
2221 | msg.session_id = be64toh(msg.session_id); | |
f7079f67 DG |
2222 | |
2223 | /* | |
7591bab1 MD |
2224 | * Iterate over all streams to set the begin data pending flag. |
2225 | * For now, the streams are indexed by stream handle so we have | |
2226 | * to iterate over all streams to find the one associated with | |
2227 | * the right session_id. | |
f7079f67 DG |
2228 | */ |
2229 | rcu_read_lock(); | |
d3e2ba59 | 2230 | cds_lfht_for_each_entry(relay_streams_ht->ht, &iter.iter, stream, |
2a174661 | 2231 | node.node) { |
7591bab1 MD |
2232 | if (!stream_get(stream)) { |
2233 | continue; | |
2234 | } | |
5312a3ed | 2235 | if (stream->trace->session->id == msg.session_id) { |
7591bab1 MD |
2236 | pthread_mutex_lock(&stream->lock); |
2237 | stream->data_pending_check_done = false; | |
2238 | pthread_mutex_unlock(&stream->lock); | |
f7079f67 DG |
2239 | DBG("Set begin data pending flag to stream %" PRIu64, |
2240 | stream->stream_handle); | |
2241 | } | |
7591bab1 | 2242 | stream_put(stream); |
f7079f67 DG |
2243 | } |
2244 | rcu_read_unlock(); | |
2245 | ||
53efb85a | 2246 | memset(&reply, 0, sizeof(reply)); |
f7079f67 DG |
2247 | /* All good, send back reply. */ |
2248 | reply.ret_code = htobe32(LTTNG_OK); | |
2249 | ||
5312a3ed JG |
2250 | send_ret = conn->sock->ops->sendmsg(conn->sock, &reply, sizeof(reply), 0); |
2251 | if (send_ret < (ssize_t) sizeof(reply)) { | |
2252 | ERR("Failed to send \"begin data pending\" command reply (ret = %zd)", | |
2253 | send_ret); | |
2254 | ret = -1; | |
2255 | } else { | |
2256 | ret = 0; | |
f7079f67 DG |
2257 | } |
2258 | ||
2259 | end_no_session: | |
2260 | return ret; | |
2261 | } | |
2262 | ||
2263 | /* | |
7591bab1 MD |
2264 | * End data pending command. This will check, for a given session id, if |
2265 | * each stream associated with it has its data_pending_check_done flag | |
2266 | * set. If not, this means that the client lost track of the stream but | |
2267 | * the data is still being streamed on our side. In this case, we inform | |
2268 | * the client that data is in flight. | |
f7079f67 DG |
2269 | * |
2270 | * Return to the client if there is data in flight or not with a ret_code. | |
2271 | */ | |
5312a3ed JG |
2272 | static int relay_end_data_pending(const struct lttcomm_relayd_hdr *recv_hdr, |
2273 | struct relay_connection *conn, | |
2274 | const struct lttng_buffer_view *payload) | |
f7079f67 DG |
2275 | { |
2276 | int ret; | |
5312a3ed | 2277 | ssize_t send_ret; |
f7079f67 DG |
2278 | struct lttng_ht_iter iter; |
2279 | struct lttcomm_relayd_end_data_pending msg; | |
2280 | struct lttcomm_relayd_generic_reply reply; | |
2281 | struct relay_stream *stream; | |
f7079f67 DG |
2282 | uint32_t is_data_inflight = 0; |
2283 | ||
f7079f67 DG |
2284 | DBG("End data pending command"); |
2285 | ||
5312a3ed | 2286 | if (!conn->session || !conn->version_check_done) { |
f7079f67 DG |
2287 | ERR("Trying to check for data before version check"); |
2288 | ret = -1; | |
2289 | goto end_no_session; | |
2290 | } | |
2291 | ||
5312a3ed JG |
2292 | if (payload->size < sizeof(msg)) { |
2293 | ERR("Unexpected payload size in \"relay_end_data_pending\": expected >= %zu bytes, got %zu bytes", | |
2294 | sizeof(msg), payload->size); | |
f7079f67 DG |
2295 | ret = -1; |
2296 | goto end_no_session; | |
2297 | } | |
5312a3ed JG |
2298 | memcpy(&msg, payload->data, sizeof(msg)); |
2299 | msg.session_id = be64toh(msg.session_id); | |
f7079f67 | 2300 | |
7591bab1 MD |
2301 | /* |
2302 | * Iterate over all streams to see if the begin data pending | |
2303 | * flag is set. | |
2304 | */ | |
f7079f67 | 2305 | rcu_read_lock(); |
d3e2ba59 | 2306 | cds_lfht_for_each_entry(relay_streams_ht->ht, &iter.iter, stream, |
2a174661 | 2307 | node.node) { |
7591bab1 MD |
2308 | if (!stream_get(stream)) { |
2309 | continue; | |
2310 | } | |
5312a3ed | 2311 | if (stream->trace->session->id != msg.session_id) { |
7591bab1 MD |
2312 | stream_put(stream); |
2313 | continue; | |
2314 | } | |
2315 | pthread_mutex_lock(&stream->lock); | |
2316 | if (!stream->data_pending_check_done) { | |
298a25ca JG |
2317 | uint64_t stream_seq; |
2318 | ||
2319 | if (session_streams_have_index(conn->session)) { | |
2320 | /* | |
2321 | * Ensure that both the index and stream data have been | |
2322 | * flushed up to the requested point. | |
2323 | */ | |
a8f9f353 | 2324 | stream_seq = min(stream->prev_data_seq, stream->prev_index_seq); |
298a25ca | 2325 | } else { |
a8f9f353 | 2326 | stream_seq = stream->prev_data_seq; |
298a25ca JG |
2327 | } |
2328 | if (!stream->closed || !(((int64_t) (stream_seq - stream->last_net_seq_num)) >= 0)) { | |
7591bab1 MD |
2329 | is_data_inflight = 1; |
2330 | DBG("Data is still in flight for stream %" PRIu64, | |
2331 | stream->stream_handle); | |
2332 | pthread_mutex_unlock(&stream->lock); | |
2333 | stream_put(stream); | |
2334 | break; | |
2335 | } | |
f7079f67 | 2336 | } |
7591bab1 MD |
2337 | pthread_mutex_unlock(&stream->lock); |
2338 | stream_put(stream); | |
f7079f67 DG |
2339 | } |
2340 | rcu_read_unlock(); | |
2341 | ||
53efb85a | 2342 | memset(&reply, 0, sizeof(reply)); |
f7079f67 DG |
2343 | /* All good, send back reply. */ |
2344 | reply.ret_code = htobe32(is_data_inflight); | |
2345 | ||
5312a3ed JG |
2346 | send_ret = conn->sock->ops->sendmsg(conn->sock, &reply, sizeof(reply), 0); |
2347 | if (send_ret < (ssize_t) sizeof(reply)) { | |
2348 | ERR("Failed to send \"end data pending\" command reply (ret = %zd)", | |
2349 | send_ret); | |
2350 | ret = -1; | |
2351 | } else { | |
2352 | ret = 0; | |
f7079f67 DG |
2353 | } |
2354 | ||
2355 | end_no_session: | |
2356 | return ret; | |
2357 | } | |
2358 | ||
1c20f0e2 JD |
2359 | /* |
2360 | * Receive an index for a specific stream. | |
2361 | * | |
2362 | * Return 0 on success else a negative value. | |
2363 | */ | |
5312a3ed JG |
2364 | static int relay_recv_index(const struct lttcomm_relayd_hdr *recv_hdr, |
2365 | struct relay_connection *conn, | |
2366 | const struct lttng_buffer_view *payload) | |
1c20f0e2 | 2367 | { |
5312a3ed JG |
2368 | int ret; |
2369 | ssize_t send_ret; | |
58eb9381 | 2370 | struct relay_session *session = conn->session; |
1c20f0e2 | 2371 | struct lttcomm_relayd_index index_info; |
1c20f0e2 JD |
2372 | struct lttcomm_relayd_generic_reply reply; |
2373 | struct relay_stream *stream; | |
f8f3885c | 2374 | size_t msg_len; |
1c20f0e2 | 2375 | |
58eb9381 | 2376 | assert(conn); |
1c20f0e2 JD |
2377 | |
2378 | DBG("Relay receiving index"); | |
2379 | ||
5312a3ed | 2380 | if (!session || !conn->version_check_done) { |
1c20f0e2 JD |
2381 | ERR("Trying to close a stream before version check"); |
2382 | ret = -1; | |
2383 | goto end_no_session; | |
2384 | } | |
2385 | ||
f8f3885c MD |
2386 | msg_len = lttcomm_relayd_index_len( |
2387 | lttng_to_index_major(conn->major, conn->minor), | |
2388 | lttng_to_index_minor(conn->major, conn->minor)); | |
5312a3ed JG |
2389 | if (payload->size < msg_len) { |
2390 | ERR("Unexpected payload size in \"relay_recv_index\": expected >= %zu bytes, got %zu bytes", | |
2391 | msg_len, payload->size); | |
1c20f0e2 JD |
2392 | ret = -1; |
2393 | goto end_no_session; | |
2394 | } | |
5312a3ed JG |
2395 | memcpy(&index_info, payload->data, msg_len); |
2396 | index_info.relay_stream_id = be64toh(index_info.relay_stream_id); | |
2397 | index_info.net_seq_num = be64toh(index_info.net_seq_num); | |
2398 | index_info.packet_size = be64toh(index_info.packet_size); | |
2399 | index_info.content_size = be64toh(index_info.content_size); | |
2400 | index_info.timestamp_begin = be64toh(index_info.timestamp_begin); | |
2401 | index_info.timestamp_end = be64toh(index_info.timestamp_end); | |
2402 | index_info.events_discarded = be64toh(index_info.events_discarded); | |
2403 | index_info.stream_id = be64toh(index_info.stream_id); | |
81df238b JR |
2404 | |
2405 | if (conn->minor >= 8) { | |
2406 | index_info.stream_instance_id = | |
2407 | be64toh(index_info.stream_instance_id); | |
2408 | index_info.packet_seq_num = be64toh(index_info.packet_seq_num); | |
0f83d1cc MD |
2409 | } else { |
2410 | index_info.stream_instance_id = -1ULL; | |
2411 | index_info.packet_seq_num = -1ULL; | |
81df238b | 2412 | } |
5312a3ed JG |
2413 | |
2414 | stream = stream_get_by_id(index_info.relay_stream_id); | |
1c20f0e2 | 2415 | if (!stream) { |
7591bab1 | 2416 | ERR("stream_get_by_id not found"); |
1c20f0e2 | 2417 | ret = -1; |
7591bab1 | 2418 | goto end; |
1c20f0e2 | 2419 | } |
d3e2ba59 | 2420 | |
c35f9726 JG |
2421 | pthread_mutex_lock(&stream->lock); |
2422 | ret = stream_add_index(stream, &index_info); | |
2423 | pthread_mutex_unlock(&stream->lock); | |
2424 | if (ret) { | |
7591bab1 MD |
2425 | goto end_stream_put; |
2426 | } | |
1c20f0e2 | 2427 | |
7591bab1 | 2428 | end_stream_put: |
7591bab1 | 2429 | stream_put(stream); |
7591bab1 | 2430 | end: |
53efb85a | 2431 | memset(&reply, 0, sizeof(reply)); |
1c20f0e2 JD |
2432 | if (ret < 0) { |
2433 | reply.ret_code = htobe32(LTTNG_ERR_UNK); | |
2434 | } else { | |
2435 | reply.ret_code = htobe32(LTTNG_OK); | |
2436 | } | |
58eb9381 | 2437 | send_ret = conn->sock->ops->sendmsg(conn->sock, &reply, sizeof(reply), 0); |
5312a3ed JG |
2438 | if (send_ret < (ssize_t) sizeof(reply)) { |
2439 | ERR("Failed to send \"recv index\" command reply (ret = %zd)", send_ret); | |
2440 | ret = -1; | |
1c20f0e2 JD |
2441 | } |
2442 | ||
2443 | end_no_session: | |
2444 | return ret; | |
2445 | } | |
2446 | ||
a4baae1b JD |
2447 | /* |
2448 | * Receive the streams_sent message. | |
2449 | * | |
2450 | * Return 0 on success else a negative value. | |
2451 | */ | |
5312a3ed JG |
2452 | static int relay_streams_sent(const struct lttcomm_relayd_hdr *recv_hdr, |
2453 | struct relay_connection *conn, | |
2454 | const struct lttng_buffer_view *payload) | |
a4baae1b | 2455 | { |
5312a3ed JG |
2456 | int ret; |
2457 | ssize_t send_ret; | |
a4baae1b JD |
2458 | struct lttcomm_relayd_generic_reply reply; |
2459 | ||
58eb9381 | 2460 | assert(conn); |
a4baae1b JD |
2461 | |
2462 | DBG("Relay receiving streams_sent"); | |
2463 | ||
5312a3ed | 2464 | if (!conn->session || !conn->version_check_done) { |
a4baae1b JD |
2465 | ERR("Trying to close a stream before version check"); |
2466 | ret = -1; | |
2467 | goto end_no_session; | |
2468 | } | |
2469 | ||
2470 | /* | |
7591bab1 MD |
2471 | * Publish every pending stream in the connection recv list which are |
2472 | * now ready to be used by the viewer. | |
4a9daf17 | 2473 | */ |
7591bab1 | 2474 | publish_connection_local_streams(conn); |
4a9daf17 | 2475 | |
53efb85a | 2476 | memset(&reply, 0, sizeof(reply)); |
a4baae1b | 2477 | reply.ret_code = htobe32(LTTNG_OK); |
58eb9381 | 2478 | send_ret = conn->sock->ops->sendmsg(conn->sock, &reply, sizeof(reply), 0); |
5312a3ed JG |
2479 | if (send_ret < (ssize_t) sizeof(reply)) { |
2480 | ERR("Failed to send \"streams sent\" command reply (ret = %zd)", | |
2481 | send_ret); | |
2482 | ret = -1; | |
a4baae1b JD |
2483 | } else { |
2484 | /* Success. */ | |
2485 | ret = 0; | |
2486 | } | |
2487 | ||
2488 | end_no_session: | |
2489 | return ret; | |
2490 | } | |
2491 | ||
0710c89e JG |
2492 | static ssize_t relay_unpack_rotate_streams_header( |
2493 | const struct lttng_buffer_view *payload, | |
2494 | struct lttcomm_relayd_rotate_streams *_rotate_streams) | |
2495 | { | |
2496 | struct lttcomm_relayd_rotate_streams rotate_streams; | |
2497 | /* | |
2498 | * Set to the smallest version (packed) of `lttcomm_relayd_rotate_streams`. | |
2499 | * This is the smallest version of this structure, but it can be larger; | |
2500 | * this variable is updated once the proper size of the structure is known. | |
2501 | * | |
2502 | * See comment at the declaration of this structure for more information. | |
2503 | */ | |
2504 | ssize_t header_len = sizeof(struct lttcomm_relayd_rotate_streams_packed); | |
2505 | size_t expected_payload_size_no_padding, | |
2506 | expected_payload_size_3_bytes_padding, | |
2507 | expected_payload_size_7_bytes_padding; | |
2508 | ||
2509 | if (payload->size < header_len) { | |
2510 | ERR("Unexpected payload size in \"relay_rotate_session_stream\": expected >= %zu bytes, got %zu bytes", | |
2511 | header_len, payload->size); | |
2512 | goto error; | |
2513 | } | |
2514 | ||
2515 | /* | |
2516 | * Some versions incorrectly omitted the LTTNG_PACKED annotation on the | |
2517 | * `new_chunk_id` optional field of struct lttcomm_relayd_rotate_streams. | |
2518 | * | |
2519 | * We start by "unpacking" `stream_count` to figure out the padding length | |
2520 | * emited by our peer. | |
2521 | */ | |
2522 | memcpy(&rotate_streams.stream_count, payload->data, | |
2523 | sizeof(rotate_streams.stream_count)); | |
2524 | rotate_streams = (typeof(rotate_streams)) { | |
2525 | .stream_count = be32toh(rotate_streams.stream_count), | |
2526 | }; | |
2527 | ||
2528 | /* | |
2529 | * Payload size expected given the possible padding lengths in | |
2530 | * `struct lttcomm_relayd_rotate_streams`. | |
2531 | */ | |
2532 | expected_payload_size_no_padding = (rotate_streams.stream_count * | |
2533 | sizeof(*rotate_streams.rotation_positions)) + | |
2534 | sizeof(struct lttcomm_relayd_rotate_streams_packed); | |
2535 | expected_payload_size_3_bytes_padding = (rotate_streams.stream_count * | |
2536 | sizeof(*rotate_streams.rotation_positions)) + | |
2537 | sizeof(struct lttcomm_relayd_rotate_streams_3_bytes_padding); | |
2538 | expected_payload_size_7_bytes_padding = (rotate_streams.stream_count * | |
2539 | sizeof(*rotate_streams.rotation_positions)) + | |
2540 | sizeof(struct lttcomm_relayd_rotate_streams_7_bytes_padding); | |
2541 | ||
2542 | if (payload->size == expected_payload_size_no_padding) { | |
2543 | struct lttcomm_relayd_rotate_streams_packed packed_rotate_streams; | |
2544 | ||
2545 | /* | |
2546 | * This handles cases where someone might build with | |
2547 | * -fpack-struct or any other toolchain that wouldn't produce | |
2548 | * padding to align `value`. | |
2549 | */ | |
2550 | DBG("Received `struct lttcomm_relayd_rotate_streams` with no padding"); | |
2551 | ||
2552 | header_len = sizeof(packed_rotate_streams); | |
2553 | memcpy(&packed_rotate_streams, payload->data, header_len); | |
2554 | ||
2555 | /* Unpack the packed structure to the natively-packed version. */ | |
2556 | *_rotate_streams = (typeof(*_rotate_streams)) { | |
2557 | .stream_count = be32toh(packed_rotate_streams.stream_count), | |
2558 | .new_chunk_id = (typeof(_rotate_streams->new_chunk_id)) { | |
2559 | .is_set = !!packed_rotate_streams.new_chunk_id.is_set, | |
2560 | .value = be64toh(packed_rotate_streams.new_chunk_id.value), | |
2561 | } | |
2562 | }; | |
2563 | } else if (payload->size == expected_payload_size_3_bytes_padding) { | |
2564 | struct lttcomm_relayd_rotate_streams_3_bytes_padding padded_rotate_streams; | |
2565 | ||
2566 | DBG("Received `struct lttcomm_relayd_rotate_streams` with 3 bytes of padding (4-byte aligned peer)"); | |
2567 | ||
2568 | header_len = sizeof(padded_rotate_streams); | |
2569 | memcpy(&padded_rotate_streams, payload->data, header_len); | |
2570 | ||
2571 | /* Unpack the 3-byte padded structure to the natively-packed version. */ | |
2572 | *_rotate_streams = (typeof(*_rotate_streams)) { | |
2573 | .stream_count = be32toh(padded_rotate_streams.stream_count), | |
2574 | .new_chunk_id = (typeof(_rotate_streams->new_chunk_id)) { | |
2575 | .is_set = !!padded_rotate_streams.new_chunk_id.is_set, | |
2576 | .value = be64toh(padded_rotate_streams.new_chunk_id.value), | |
2577 | } | |
2578 | }; | |
2579 | } else if (payload->size == expected_payload_size_7_bytes_padding) { | |
2580 | struct lttcomm_relayd_rotate_streams_7_bytes_padding padded_rotate_streams; | |
2581 | ||
2582 | DBG("Received `struct lttcomm_relayd_rotate_streams` with 7 bytes of padding (8-byte aligned peer)"); | |
2583 | ||
2584 | header_len = sizeof(padded_rotate_streams); | |
2585 | memcpy(&padded_rotate_streams, payload->data, header_len); | |
2586 | ||
2587 | /* Unpack the 7-byte padded structure to the natively-packed version. */ | |
2588 | *_rotate_streams = (typeof(*_rotate_streams)) { | |
2589 | .stream_count = be32toh(padded_rotate_streams.stream_count), | |
2590 | .new_chunk_id = (typeof(_rotate_streams->new_chunk_id)) { | |
2591 | .is_set = !!padded_rotate_streams.new_chunk_id.is_set, | |
2592 | .value = be64toh(padded_rotate_streams.new_chunk_id.value), | |
2593 | } | |
2594 | }; | |
2595 | ||
2596 | header_len = sizeof(padded_rotate_streams); | |
2597 | } else { | |
2598 | ERR("Unexpected payload size in \"relay_rotate_session_stream\": expected %zu, %zu or %zu bytes, got %zu bytes", | |
2599 | expected_payload_size_no_padding, | |
2600 | expected_payload_size_3_bytes_padding, | |
2601 | expected_payload_size_7_bytes_padding, | |
2602 | payload->size); | |
2603 | goto error; | |
2604 | } | |
2605 | ||
2606 | return header_len; | |
2607 | error: | |
2608 | return -1; | |
2609 | } | |
2610 | ||
d3ecc550 | 2611 | /* |
c35f9726 JG |
2612 | * relay_rotate_session_stream: rotate a stream to a new tracefile for the |
2613 | * session rotation feature (not the tracefile rotation feature). | |
d3ecc550 | 2614 | */ |
c35f9726 JG |
2615 | static int relay_rotate_session_streams( |
2616 | const struct lttcomm_relayd_hdr *recv_hdr, | |
5312a3ed JG |
2617 | struct relay_connection *conn, |
2618 | const struct lttng_buffer_view *payload) | |
d3ecc550 | 2619 | { |
30b9d5ab | 2620 | int ret = 0; |
c35f9726 | 2621 | uint32_t i; |
5312a3ed | 2622 | ssize_t send_ret; |
c35f9726 | 2623 | enum lttng_error_code reply_code = LTTNG_ERR_UNK; |
d3ecc550 | 2624 | struct relay_session *session = conn->session; |
c35f9726 JG |
2625 | struct lttcomm_relayd_rotate_streams rotate_streams; |
2626 | struct lttcomm_relayd_generic_reply reply = {}; | |
2627 | struct relay_stream *stream = NULL; | |
c35f9726 JG |
2628 | struct lttng_trace_chunk *next_trace_chunk = NULL; |
2629 | struct lttng_buffer_view stream_positions; | |
70626904 JG |
2630 | char chunk_id_buf[MAX_INT_DEC_LEN(uint64_t)]; |
2631 | const char *chunk_id_str = "none"; | |
0710c89e | 2632 | ssize_t header_len; |
d3ecc550 | 2633 | |
d3ecc550 JD |
2634 | if (!session || !conn->version_check_done) { |
2635 | ERR("Trying to rotate a stream before version check"); | |
2636 | ret = -1; | |
2637 | goto end_no_reply; | |
2638 | } | |
2639 | ||
2640 | if (session->major == 2 && session->minor < 11) { | |
2641 | ERR("Unsupported feature before 2.11"); | |
2642 | ret = -1; | |
2643 | goto end_no_reply; | |
2644 | } | |
2645 | ||
0710c89e JG |
2646 | header_len = relay_unpack_rotate_streams_header(payload, &rotate_streams); |
2647 | if (header_len < 0) { | |
d3ecc550 JD |
2648 | ret = -1; |
2649 | goto end_no_reply; | |
2650 | } | |
2651 | ||
c35f9726 JG |
2652 | if (rotate_streams.new_chunk_id.is_set) { |
2653 | /* | |
2654 | * Retrieve the trace chunk the stream must transition to. As | |
2655 | * per the protocol, this chunk should have been created | |
2656 | * before this command is received. | |
2657 | */ | |
2658 | next_trace_chunk = sessiond_trace_chunk_registry_get_chunk( | |
2659 | sessiond_trace_chunk_registry, | |
2c93dc32 JG |
2660 | session->sessiond_uuid, |
2661 | conn->session->id_sessiond.is_set ? | |
2662 | conn->session->id_sessiond.value : | |
2663 | conn->session->id, | |
c35f9726 JG |
2664 | rotate_streams.new_chunk_id.value); |
2665 | if (!next_trace_chunk) { | |
c70636a7 | 2666 | char uuid_str[LTTNG_UUID_STR_LEN]; |
c35f9726 JG |
2667 | |
2668 | lttng_uuid_to_str(session->sessiond_uuid, uuid_str); | |
2669 | ERR("Unknown next trace chunk in ROTATE_STREAMS command: sessiond_uuid = {%s}, session_id = %" PRIu64 | |
2670 | ", trace_chunk_id = %" PRIu64, | |
2671 | uuid_str, session->id, | |
2672 | rotate_streams.new_chunk_id.value); | |
2673 | reply_code = LTTNG_ERR_INVALID_PROTOCOL; | |
2674 | ret = -1; | |
2675 | goto end; | |
2676 | } | |
70626904 JG |
2677 | |
2678 | ret = snprintf(chunk_id_buf, sizeof(chunk_id_buf), "%" PRIu64, | |
2679 | rotate_streams.new_chunk_id.value); | |
2680 | if (ret < 0 || ret >= sizeof(chunk_id_buf)) { | |
2681 | chunk_id_str = "formatting error"; | |
2682 | } else { | |
2683 | chunk_id_str = chunk_id_buf; | |
2684 | } | |
d3ecc550 JD |
2685 | } |
2686 | ||
70626904 JG |
2687 | DBG("Rotate %" PRIu32 " streams of session \"%s\" to chunk \"%s\"", |
2688 | rotate_streams.stream_count, session->session_name, | |
2689 | chunk_id_str); | |
2690 | ||
c35f9726 | 2691 | stream_positions = lttng_buffer_view_from_view(payload, |
0710c89e | 2692 | header_len, -1); |
c35f9726 JG |
2693 | if (!stream_positions.data || |
2694 | stream_positions.size < | |
2695 | (rotate_streams.stream_count * | |
2696 | sizeof(struct lttcomm_relayd_stream_rotation_position))) { | |
2697 | reply_code = LTTNG_ERR_INVALID_PROTOCOL; | |
d3ecc550 | 2698 | ret = -1; |
5312a3ed | 2699 | goto end; |
d3ecc550 JD |
2700 | } |
2701 | ||
c35f9726 JG |
2702 | for (i = 0; i < rotate_streams.stream_count; i++) { |
2703 | struct lttcomm_relayd_stream_rotation_position *position_comm = | |
2704 | &((typeof(position_comm)) stream_positions.data)[i]; | |
2705 | const struct lttcomm_relayd_stream_rotation_position pos = { | |
2706 | .stream_id = be64toh(position_comm->stream_id), | |
2707 | .rotate_at_seq_num = be64toh( | |
2708 | position_comm->rotate_at_seq_num), | |
2709 | }; | |
5312a3ed | 2710 | |
c35f9726 JG |
2711 | stream = stream_get_by_id(pos.stream_id); |
2712 | if (!stream) { | |
2713 | reply_code = LTTNG_ERR_INVALID; | |
2714 | ret = -1; | |
2715 | goto end; | |
c6db3843 JG |
2716 | } |
2717 | ||
c35f9726 JG |
2718 | pthread_mutex_lock(&stream->lock); |
2719 | ret = stream_set_pending_rotation(stream, next_trace_chunk, | |
2720 | pos.rotate_at_seq_num); | |
2721 | pthread_mutex_unlock(&stream->lock); | |
2722 | if (ret) { | |
2723 | reply_code = LTTNG_ERR_FILE_CREATION_ERROR; | |
2724 | goto end; | |
c6db3843 | 2725 | } |
c35f9726 JG |
2726 | |
2727 | stream_put(stream); | |
2728 | stream = NULL; | |
d3ecc550 JD |
2729 | } |
2730 | ||
c35f9726 | 2731 | reply_code = LTTNG_OK; |
eaeb64a9 | 2732 | ret = 0; |
d3ecc550 | 2733 | end: |
c35f9726 JG |
2734 | if (stream) { |
2735 | stream_put(stream); | |
d3ecc550 | 2736 | } |
c35f9726 JG |
2737 | |
2738 | reply.ret_code = htobe32((uint32_t) reply_code); | |
d3ecc550 JD |
2739 | send_ret = conn->sock->ops->sendmsg(conn->sock, &reply, |
2740 | sizeof(struct lttcomm_relayd_generic_reply), 0); | |
5312a3ed JG |
2741 | if (send_ret < (ssize_t) sizeof(reply)) { |
2742 | ERR("Failed to send \"rotate session stream\" command reply (ret = %zd)", | |
2743 | send_ret); | |
2744 | ret = -1; | |
d3ecc550 | 2745 | } |
d3ecc550 | 2746 | end_no_reply: |
c35f9726 | 2747 | lttng_trace_chunk_put(next_trace_chunk); |
d3ecc550 JD |
2748 | return ret; |
2749 | } | |
2750 | ||
e5add6d0 JG |
2751 | /* |
2752 | * relay_create_trace_chunk: create a new trace chunk | |
2753 | */ | |
2754 | static int relay_create_trace_chunk(const struct lttcomm_relayd_hdr *recv_hdr, | |
2755 | struct relay_connection *conn, | |
2756 | const struct lttng_buffer_view *payload) | |
2757 | { | |
2758 | int ret = 0; | |
2759 | ssize_t send_ret; | |
2760 | struct relay_session *session = conn->session; | |
2761 | struct lttcomm_relayd_create_trace_chunk *msg; | |
2762 | struct lttcomm_relayd_generic_reply reply = {}; | |
2763 | struct lttng_buffer_view header_view; | |
e5add6d0 JG |
2764 | struct lttng_trace_chunk *chunk = NULL, *published_chunk = NULL; |
2765 | enum lttng_error_code reply_code = LTTNG_OK; | |
2766 | enum lttng_trace_chunk_status chunk_status; | |
a7ceb342 | 2767 | const char *new_path; |
e5add6d0 JG |
2768 | |
2769 | if (!session || !conn->version_check_done) { | |
2770 | ERR("Trying to create a trace chunk before version check"); | |
2771 | ret = -1; | |
2772 | goto end_no_reply; | |
2773 | } | |
2774 | ||
2775 | if (session->major == 2 && session->minor < 11) { | |
2776 | ERR("Chunk creation command is unsupported before 2.11"); | |
2777 | ret = -1; | |
2778 | goto end_no_reply; | |
2779 | } | |
2780 | ||
2781 | header_view = lttng_buffer_view_from_view(payload, 0, sizeof(*msg)); | |
3e6e0df2 | 2782 | if (!lttng_buffer_view_is_valid(&header_view)) { |
e5add6d0 JG |
2783 | ERR("Failed to receive payload of chunk creation command"); |
2784 | ret = -1; | |
2785 | goto end_no_reply; | |
2786 | } | |
2787 | ||
2788 | /* Convert to host endianness. */ | |
2789 | msg = (typeof(msg)) header_view.data; | |
2790 | msg->chunk_id = be64toh(msg->chunk_id); | |
2791 | msg->creation_timestamp = be64toh(msg->creation_timestamp); | |
2792 | msg->override_name_length = be32toh(msg->override_name_length); | |
2793 | ||
8433d916 MD |
2794 | pthread_mutex_lock(&conn->session->lock); |
2795 | session->ongoing_rotation = true; | |
a7ceb342 MD |
2796 | if (session->current_trace_chunk && |
2797 | !lttng_trace_chunk_get_name_overridden(session->current_trace_chunk)) { | |
2798 | chunk_status = lttng_trace_chunk_rename_path(session->current_trace_chunk, | |
2799 | DEFAULT_CHUNK_TMP_OLD_DIRECTORY); | |
2800 | if (chunk_status != LTTNG_TRACE_CHUNK_STATUS_OK) { | |
2801 | ERR("Failed to rename old chunk"); | |
2802 | ret = -1; | |
2803 | reply_code = LTTNG_ERR_UNK; | |
2804 | goto end; | |
2805 | } | |
2806 | } | |
a7ceb342 MD |
2807 | if (!session->current_trace_chunk) { |
2808 | if (!session->has_rotated) { | |
2809 | new_path = ""; | |
2810 | } else { | |
2811 | new_path = NULL; | |
2812 | } | |
2813 | } else { | |
2814 | new_path = DEFAULT_CHUNK_TMP_NEW_DIRECTORY; | |
2815 | } | |
e5add6d0 | 2816 | chunk = lttng_trace_chunk_create( |
a7ceb342 | 2817 | msg->chunk_id, msg->creation_timestamp, new_path); |
e5add6d0 JG |
2818 | if (!chunk) { |
2819 | ERR("Failed to create trace chunk in trace chunk creation command"); | |
2820 | ret = -1; | |
2821 | reply_code = LTTNG_ERR_NOMEM; | |
2822 | goto end; | |
2823 | } | |
7145f5e9 | 2824 | lttng_trace_chunk_set_fd_tracker(chunk, the_fd_tracker); |
e5add6d0 JG |
2825 | |
2826 | if (msg->override_name_length) { | |
2827 | const char *name; | |
3e6e0df2 JG |
2828 | const struct lttng_buffer_view chunk_name_view = |
2829 | lttng_buffer_view_from_view(payload, | |
2830 | sizeof(*msg), | |
2831 | msg->override_name_length); | |
2832 | ||
2833 | if (!lttng_buffer_view_is_valid(&chunk_name_view)) { | |
2834 | ERR("Invalid payload of chunk creation command (protocol error): buffer too short for expected name length"); | |
2835 | ret = -1; | |
2836 | reply_code = LTTNG_ERR_INVALID; | |
2837 | goto end; | |
2838 | } | |
e5add6d0 | 2839 | |
e5add6d0 | 2840 | name = chunk_name_view.data; |
3e6e0df2 JG |
2841 | if (name[msg->override_name_length - 1]) { |
2842 | ERR("Invalid payload of chunk creation command (protocol error): name is not null-terminated"); | |
e5add6d0 JG |
2843 | ret = -1; |
2844 | reply_code = LTTNG_ERR_INVALID; | |
2845 | goto end; | |
2846 | } | |
2847 | ||
2848 | chunk_status = lttng_trace_chunk_override_name( | |
2849 | chunk, chunk_name_view.data); | |
2850 | switch (chunk_status) { | |
2851 | case LTTNG_TRACE_CHUNK_STATUS_OK: | |
2852 | break; | |
2853 | case LTTNG_TRACE_CHUNK_STATUS_INVALID_ARGUMENT: | |
2854 | ERR("Failed to set the name of new trace chunk in trace chunk creation command (invalid name)"); | |
2855 | reply_code = LTTNG_ERR_INVALID; | |
2856 | ret = -1; | |
2857 | goto end; | |
2858 | default: | |
2859 | ERR("Failed to set the name of new trace chunk in trace chunk creation command (unknown error)"); | |
2860 | reply_code = LTTNG_ERR_UNK; | |
2861 | ret = -1; | |
2862 | goto end; | |
2863 | } | |
2864 | } | |
2865 | ||
e5add6d0 JG |
2866 | chunk_status = lttng_trace_chunk_set_credentials_current_user(chunk); |
2867 | if (chunk_status != LTTNG_TRACE_CHUNK_STATUS_OK) { | |
2868 | reply_code = LTTNG_ERR_UNK; | |
2869 | ret = -1; | |
2870 | goto end; | |
2871 | } | |
2872 | ||
7ceefac4 JG |
2873 | assert(conn->session->output_directory); |
2874 | chunk_status = lttng_trace_chunk_set_as_owner(chunk, | |
2875 | conn->session->output_directory); | |
e5add6d0 JG |
2876 | if (chunk_status != LTTNG_TRACE_CHUNK_STATUS_OK) { |
2877 | reply_code = LTTNG_ERR_UNK; | |
2878 | ret = -1; | |
2879 | goto end; | |
2880 | } | |
2881 | ||
2882 | published_chunk = sessiond_trace_chunk_registry_publish_chunk( | |
2883 | sessiond_trace_chunk_registry, | |
2884 | conn->session->sessiond_uuid, | |
2c93dc32 JG |
2885 | conn->session->id_sessiond.is_set ? |
2886 | conn->session->id_sessiond.value : | |
2887 | conn->session->id, | |
e5add6d0 JG |
2888 | chunk); |
2889 | if (!published_chunk) { | |
c70636a7 | 2890 | char uuid_str[LTTNG_UUID_STR_LEN]; |
e5add6d0 JG |
2891 | |
2892 | lttng_uuid_to_str(conn->session->sessiond_uuid, uuid_str); | |
2893 | ERR("Failed to publish chunk: sessiond_uuid = %s, session_id = %" PRIu64 ", chunk_id = %" PRIu64, | |
2894 | uuid_str, | |
2895 | conn->session->id, | |
2896 | msg->chunk_id); | |
2897 | ret = -1; | |
2898 | reply_code = LTTNG_ERR_NOMEM; | |
2899 | goto end; | |
2900 | } | |
2901 | ||
62bad3bf JG |
2902 | if (conn->session->pending_closure_trace_chunk) { |
2903 | /* | |
2904 | * Invalid; this means a second create_trace_chunk command was | |
2905 | * received before a close_trace_chunk. | |
2906 | */ | |
2907 | ERR("Invalid trace chunk close command received; a trace chunk is already waiting for a trace chunk close command"); | |
2908 | reply_code = LTTNG_ERR_INVALID_PROTOCOL; | |
2909 | ret = -1; | |
8433d916 | 2910 | goto end; |
62bad3bf JG |
2911 | } |
2912 | conn->session->pending_closure_trace_chunk = | |
2913 | conn->session->current_trace_chunk; | |
e5add6d0 | 2914 | conn->session->current_trace_chunk = published_chunk; |
e5add6d0 | 2915 | published_chunk = NULL; |
a7ceb342 MD |
2916 | if (!conn->session->pending_closure_trace_chunk) { |
2917 | session->ongoing_rotation = false; | |
2918 | } | |
e5add6d0 | 2919 | end: |
8433d916 | 2920 | pthread_mutex_unlock(&conn->session->lock); |
e5add6d0 JG |
2921 | reply.ret_code = htobe32((uint32_t) reply_code); |
2922 | send_ret = conn->sock->ops->sendmsg(conn->sock, | |
2923 | &reply, | |
2924 | sizeof(struct lttcomm_relayd_generic_reply), | |
2925 | 0); | |
2926 | if (send_ret < (ssize_t) sizeof(reply)) { | |
2927 | ERR("Failed to send \"create trace chunk\" command reply (ret = %zd)", | |
2928 | send_ret); | |
2929 | ret = -1; | |
2930 | } | |
2931 | end_no_reply: | |
2932 | lttng_trace_chunk_put(chunk); | |
2933 | lttng_trace_chunk_put(published_chunk); | |
e5add6d0 JG |
2934 | return ret; |
2935 | } | |
2936 | ||
bbc4768c JG |
2937 | /* |
2938 | * relay_close_trace_chunk: close a trace chunk | |
2939 | */ | |
2940 | static int relay_close_trace_chunk(const struct lttcomm_relayd_hdr *recv_hdr, | |
2941 | struct relay_connection *conn, | |
2942 | const struct lttng_buffer_view *payload) | |
2943 | { | |
9898f786 | 2944 | int ret = 0, buf_ret; |
bbc4768c JG |
2945 | ssize_t send_ret; |
2946 | struct relay_session *session = conn->session; | |
2947 | struct lttcomm_relayd_close_trace_chunk *msg; | |
ecd1a12f | 2948 | struct lttcomm_relayd_close_trace_chunk_reply reply = {}; |
bbc4768c JG |
2949 | struct lttng_buffer_view header_view; |
2950 | struct lttng_trace_chunk *chunk = NULL; | |
2951 | enum lttng_error_code reply_code = LTTNG_OK; | |
2952 | enum lttng_trace_chunk_status chunk_status; | |
2953 | uint64_t chunk_id; | |
c35f9726 | 2954 | LTTNG_OPTIONAL(enum lttng_trace_chunk_command_type) close_command = {}; |
bbc4768c | 2955 | time_t close_timestamp; |
ecd1a12f MD |
2956 | char closed_trace_chunk_path[LTTNG_PATH_MAX]; |
2957 | size_t path_length = 0; | |
2958 | const char *chunk_name = NULL; | |
2959 | struct lttng_dynamic_buffer reply_payload; | |
a7ceb342 | 2960 | const char *new_path; |
ecd1a12f MD |
2961 | |
2962 | lttng_dynamic_buffer_init(&reply_payload); | |
bbc4768c JG |
2963 | |
2964 | if (!session || !conn->version_check_done) { | |
2965 | ERR("Trying to close a trace chunk before version check"); | |
2966 | ret = -1; | |
2967 | goto end_no_reply; | |
2968 | } | |
2969 | ||
2970 | if (session->major == 2 && session->minor < 11) { | |
2971 | ERR("Chunk close command is unsupported before 2.11"); | |
2972 | ret = -1; | |
2973 | goto end_no_reply; | |
2974 | } | |
2975 | ||
2976 | header_view = lttng_buffer_view_from_view(payload, 0, sizeof(*msg)); | |
3e6e0df2 | 2977 | if (!lttng_buffer_view_is_valid(&header_view)) { |
bbc4768c JG |
2978 | ERR("Failed to receive payload of chunk close command"); |
2979 | ret = -1; | |
2980 | goto end_no_reply; | |
2981 | } | |
2982 | ||
2983 | /* Convert to host endianness. */ | |
2984 | msg = (typeof(msg)) header_view.data; | |
2985 | chunk_id = be64toh(msg->chunk_id); | |
2986 | close_timestamp = (time_t) be64toh(msg->close_timestamp); | |
2987 | close_command = (typeof(close_command)){ | |
2988 | .value = be32toh(msg->close_command.value), | |
2989 | .is_set = msg->close_command.is_set, | |
2990 | }; | |
2991 | ||
2992 | chunk = sessiond_trace_chunk_registry_get_chunk( | |
2993 | sessiond_trace_chunk_registry, | |
2994 | conn->session->sessiond_uuid, | |
2c93dc32 JG |
2995 | conn->session->id_sessiond.is_set ? |
2996 | conn->session->id_sessiond.value : | |
2997 | conn->session->id, | |
bbc4768c JG |
2998 | chunk_id); |
2999 | if (!chunk) { | |
c70636a7 | 3000 | char uuid_str[LTTNG_UUID_STR_LEN]; |
bbc4768c JG |
3001 | |
3002 | lttng_uuid_to_str(conn->session->sessiond_uuid, uuid_str); | |
3003 | ERR("Failed to find chunk to close: sessiond_uuid = %s, session_id = %" PRIu64 ", chunk_id = %" PRIu64, | |
3004 | uuid_str, | |
3005 | conn->session->id, | |
3006 | msg->chunk_id); | |
3007 | ret = -1; | |
3008 | reply_code = LTTNG_ERR_NOMEM; | |
3009 | goto end; | |
3010 | } | |
3011 | ||
62bad3bf | 3012 | pthread_mutex_lock(&session->lock); |
f77a6ec2 MD |
3013 | if (close_command.is_set && |
3014 | close_command.value == LTTNG_TRACE_CHUNK_COMMAND_TYPE_DELETE) { | |
3015 | /* | |
3016 | * Clear command. It is a protocol error to ask for a | |
3017 | * clear on a relay which does not allow it. Querying | |
3018 | * the configuration allows figuring out whether | |
3019 | * clearing is allowed before doing the clear. | |
3020 | */ | |
3021 | if (!opt_allow_clear) { | |
3022 | ret = -1; | |
3023 | reply_code = LTTNG_ERR_INVALID_PROTOCOL; | |
3024 | goto end_unlock_session; | |
3025 | } | |
3026 | } | |
62bad3bf JG |
3027 | if (session->pending_closure_trace_chunk && |
3028 | session->pending_closure_trace_chunk != chunk) { | |
3029 | ERR("Trace chunk close command for session \"%s\" does not target the trace chunk pending closure", | |
3030 | session->session_name); | |
3031 | reply_code = LTTNG_ERR_INVALID_PROTOCOL; | |
3032 | ret = -1; | |
3033 | goto end_unlock_session; | |
3034 | } | |
3035 | ||
a7ceb342 MD |
3036 | if (session->current_trace_chunk && session->current_trace_chunk != chunk && |
3037 | !lttng_trace_chunk_get_name_overridden(session->current_trace_chunk)) { | |
3038 | if (close_command.is_set && | |
3039 | close_command.value == LTTNG_TRACE_CHUNK_COMMAND_TYPE_DELETE && | |
3040 | !session->has_rotated) { | |
3041 | /* New chunk stays in session output directory. */ | |
3042 | new_path = ""; | |
3043 | } else { | |
3044 | /* Use chunk name for new chunk. */ | |
3045 | new_path = NULL; | |
3046 | } | |
3047 | /* Rename new chunk path. */ | |
3048 | chunk_status = lttng_trace_chunk_rename_path(session->current_trace_chunk, | |
3049 | new_path); | |
3050 | if (chunk_status != LTTNG_TRACE_CHUNK_STATUS_OK) { | |
3051 | ret = -1; | |
88188199 | 3052 | goto end_unlock_session; |
a7ceb342 MD |
3053 | } |
3054 | session->ongoing_rotation = false; | |
3055 | } | |
3056 | if ((!close_command.is_set || | |
3057 | close_command.value == LTTNG_TRACE_CHUNK_COMMAND_TYPE_NO_OPERATION) && | |
3058 | !lttng_trace_chunk_get_name_overridden(chunk)) { | |
3059 | const char *old_path; | |
3060 | ||
3061 | if (!session->has_rotated) { | |
3062 | old_path = ""; | |
3063 | } else { | |
3064 | old_path = NULL; | |
3065 | } | |
3066 | /* We need to move back the .tmp_old_chunk to its rightful place. */ | |
3067 | chunk_status = lttng_trace_chunk_rename_path(chunk, old_path); | |
3068 | if (chunk_status != LTTNG_TRACE_CHUNK_STATUS_OK) { | |
3069 | ret = -1; | |
88188199 | 3070 | goto end_unlock_session; |
a7ceb342 MD |
3071 | } |
3072 | } | |
bbc4768c JG |
3073 | chunk_status = lttng_trace_chunk_set_close_timestamp( |
3074 | chunk, close_timestamp); | |
3075 | if (chunk_status != LTTNG_TRACE_CHUNK_STATUS_OK) { | |
3076 | ERR("Failed to set trace chunk close timestamp"); | |
3077 | ret = -1; | |
3078 | reply_code = LTTNG_ERR_UNK; | |
62bad3bf | 3079 | goto end_unlock_session; |
bbc4768c JG |
3080 | } |
3081 | ||
3082 | if (close_command.is_set) { | |
3083 | chunk_status = lttng_trace_chunk_set_close_command( | |
3084 | chunk, close_command.value); | |
3085 | if (chunk_status != LTTNG_TRACE_CHUNK_STATUS_OK) { | |
3086 | ret = -1; | |
3087 | reply_code = LTTNG_ERR_INVALID; | |
62bad3bf | 3088 | goto end_unlock_session; |
bbc4768c JG |
3089 | } |
3090 | } | |
ecd1a12f MD |
3091 | chunk_status = lttng_trace_chunk_get_name(chunk, &chunk_name, NULL); |
3092 | if (chunk_status != LTTNG_TRACE_CHUNK_STATUS_OK) { | |
3093 | ERR("Failed to get chunk name"); | |
3094 | ret = -1; | |
3095 | reply_code = LTTNG_ERR_UNK; | |
3096 | goto end_unlock_session; | |
3097 | } | |
3098 | if (!session->has_rotated && !session->snapshot) { | |
3099 | ret = lttng_strncpy(closed_trace_chunk_path, | |
3100 | session->output_path, | |
3101 | sizeof(closed_trace_chunk_path)); | |
3102 | if (ret) { | |
3103 | ERR("Failed to send trace chunk path: path length of %zu bytes exceeds the maximal allowed length of %zu bytes", | |
3104 | strlen(session->output_path), | |
3105 | sizeof(closed_trace_chunk_path)); | |
3106 | reply_code = LTTNG_ERR_NOMEM; | |
3107 | ret = -1; | |
3108 | goto end_unlock_session; | |
3109 | } | |
3110 | } else { | |
3111 | if (session->snapshot) { | |
3112 | ret = snprintf(closed_trace_chunk_path, | |
3113 | sizeof(closed_trace_chunk_path), | |
3114 | "%s/%s", session->output_path, | |
3115 | chunk_name); | |
3116 | } else { | |
3117 | ret = snprintf(closed_trace_chunk_path, | |
3118 | sizeof(closed_trace_chunk_path), | |
3119 | "%s/" DEFAULT_ARCHIVED_TRACE_CHUNKS_DIRECTORY | |
3120 | "/%s", | |
3121 | session->output_path, chunk_name); | |
3122 | } | |
3123 | if (ret < 0 || ret == sizeof(closed_trace_chunk_path)) { | |
3124 | ERR("Failed to format closed trace chunk resulting path"); | |
3125 | reply_code = ret < 0 ? LTTNG_ERR_UNK : LTTNG_ERR_NOMEM; | |
3126 | ret = -1; | |
3127 | goto end_unlock_session; | |
3128 | } | |
3129 | } | |
489ea154 MD |
3130 | if (close_command.is_set && |
3131 | close_command.value == LTTNG_TRACE_CHUNK_COMMAND_TYPE_MOVE_TO_COMPLETED) { | |
3132 | session->has_rotated = true; | |
3133 | } | |
ecd1a12f MD |
3134 | DBG("Reply chunk path on close: %s", closed_trace_chunk_path); |
3135 | path_length = strlen(closed_trace_chunk_path) + 1; | |
3136 | if (path_length > UINT32_MAX) { | |
3137 | ERR("Closed trace chunk path exceeds the maximal length allowed by the protocol"); | |
3138 | ret = -1; | |
3139 | reply_code = LTTNG_ERR_INVALID_PROTOCOL; | |
3140 | goto end_unlock_session; | |
3141 | } | |
bbc4768c | 3142 | |
c35f9726 JG |
3143 | if (session->current_trace_chunk == chunk) { |
3144 | /* | |
3145 | * After a trace chunk close command, no new streams | |
3146 | * referencing the chunk may be created. Hence, on the | |
3147 | * event that no new trace chunk have been created for | |
3148 | * the session, the reference to the current trace chunk | |
3149 | * is released in order to allow it to be reclaimed when | |
3150 | * the last stream releases its reference to it. | |
3151 | */ | |
3152 | lttng_trace_chunk_put(session->current_trace_chunk); | |
3153 | session->current_trace_chunk = NULL; | |
3154 | } | |
62bad3bf JG |
3155 | lttng_trace_chunk_put(session->pending_closure_trace_chunk); |
3156 | session->pending_closure_trace_chunk = NULL; | |
3157 | end_unlock_session: | |
c35f9726 JG |
3158 | pthread_mutex_unlock(&session->lock); |
3159 | ||
bbc4768c | 3160 | end: |
ecd1a12f MD |
3161 | reply.generic.ret_code = htobe32((uint32_t) reply_code); |
3162 | reply.path_length = htobe32((uint32_t) path_length); | |
9898f786 | 3163 | buf_ret = lttng_dynamic_buffer_append( |
ecd1a12f | 3164 | &reply_payload, &reply, sizeof(reply)); |
9898f786 | 3165 | if (buf_ret) { |
ecd1a12f MD |
3166 | ERR("Failed to append \"close trace chunk\" command reply header to payload buffer"); |
3167 | goto end_no_reply; | |
3168 | } | |
3169 | ||
3170 | if (reply_code == LTTNG_OK) { | |
9898f786 | 3171 | buf_ret = lttng_dynamic_buffer_append(&reply_payload, |
ecd1a12f | 3172 | closed_trace_chunk_path, path_length); |
9898f786 | 3173 | if (buf_ret) { |
ecd1a12f MD |
3174 | ERR("Failed to append \"close trace chunk\" command reply path to payload buffer"); |
3175 | goto end_no_reply; | |
3176 | } | |
3177 | } | |
3178 | ||
bbc4768c | 3179 | send_ret = conn->sock->ops->sendmsg(conn->sock, |
ecd1a12f MD |
3180 | reply_payload.data, |
3181 | reply_payload.size, | |
bbc4768c | 3182 | 0); |
ecd1a12f MD |
3183 | if (send_ret < reply_payload.size) { |
3184 | ERR("Failed to send \"close trace chunk\" command reply of %zu bytes (ret = %zd)", | |
3185 | reply_payload.size, send_ret); | |
bbc4768c | 3186 | ret = -1; |
ecd1a12f | 3187 | goto end_no_reply; |
bbc4768c JG |
3188 | } |
3189 | end_no_reply: | |
3190 | lttng_trace_chunk_put(chunk); | |
ecd1a12f | 3191 | lttng_dynamic_buffer_reset(&reply_payload); |
bbc4768c JG |
3192 | return ret; |
3193 | } | |
3194 | ||
c35f9726 JG |
3195 | /* |
3196 | * relay_trace_chunk_exists: check if a trace chunk exists | |
3197 | */ | |
3198 | static int relay_trace_chunk_exists(const struct lttcomm_relayd_hdr *recv_hdr, | |
3199 | struct relay_connection *conn, | |
3200 | const struct lttng_buffer_view *payload) | |
3201 | { | |
3202 | int ret = 0; | |
3203 | ssize_t send_ret; | |
3204 | struct relay_session *session = conn->session; | |
3205 | struct lttcomm_relayd_trace_chunk_exists *msg; | |
3206 | struct lttcomm_relayd_trace_chunk_exists_reply reply = {}; | |
3207 | struct lttng_buffer_view header_view; | |
c35f9726 | 3208 | uint64_t chunk_id; |
6b584c2e | 3209 | bool chunk_exists; |
c35f9726 JG |
3210 | |
3211 | if (!session || !conn->version_check_done) { | |
8a82be4c | 3212 | ERR("Trying to check for the existance of a trace chunk before version check"); |
c35f9726 JG |
3213 | ret = -1; |
3214 | goto end_no_reply; | |
3215 | } | |
3216 | ||
3217 | if (session->major == 2 && session->minor < 11) { | |
8a82be4c | 3218 | ERR("Chunk exists command is unsupported before 2.11"); |
c35f9726 JG |
3219 | ret = -1; |
3220 | goto end_no_reply; | |
3221 | } | |
3222 | ||
3223 | header_view = lttng_buffer_view_from_view(payload, 0, sizeof(*msg)); | |
3e6e0df2 | 3224 | if (!lttng_buffer_view_is_valid(&header_view)) { |
8a82be4c | 3225 | ERR("Failed to receive payload of chunk exists command"); |
c35f9726 JG |
3226 | ret = -1; |
3227 | goto end_no_reply; | |
3228 | } | |
3229 | ||
3230 | /* Convert to host endianness. */ | |
3231 | msg = (typeof(msg)) header_view.data; | |
3232 | chunk_id = be64toh(msg->chunk_id); | |
3233 | ||
6b584c2e | 3234 | ret = sessiond_trace_chunk_registry_chunk_exists( |
c35f9726 JG |
3235 | sessiond_trace_chunk_registry, |
3236 | conn->session->sessiond_uuid, | |
3237 | conn->session->id, | |
6b584c2e JG |
3238 | chunk_id, &chunk_exists); |
3239 | /* | |
3240 | * If ret is not 0, send the reply and report the error to the caller. | |
3241 | * It is a protocol (or internal) error and the session/connection | |
3242 | * should be torn down. | |
3243 | */ | |
3244 | reply = (typeof(reply)){ | |
3245 | .generic.ret_code = htobe32((uint32_t) | |
3246 | (ret == 0 ? LTTNG_OK : LTTNG_ERR_INVALID_PROTOCOL)), | |
3247 | .trace_chunk_exists = ret == 0 ? chunk_exists : 0, | |
c35f9726 | 3248 | }; |
6b584c2e JG |
3249 | send_ret = conn->sock->ops->sendmsg( |
3250 | conn->sock, &reply, sizeof(reply), 0); | |
c35f9726 JG |
3251 | if (send_ret < (ssize_t) sizeof(reply)) { |
3252 | ERR("Failed to send \"create trace chunk\" command reply (ret = %zd)", | |
3253 | send_ret); | |
3254 | ret = -1; | |
3255 | } | |
3256 | end_no_reply: | |
c35f9726 JG |
3257 | return ret; |
3258 | } | |
3259 | ||
8614e600 MD |
3260 | /* |
3261 | * relay_get_configuration: query whether feature is available | |
3262 | */ | |
3263 | static int relay_get_configuration(const struct lttcomm_relayd_hdr *recv_hdr, | |
3264 | struct relay_connection *conn, | |
3265 | const struct lttng_buffer_view *payload) | |
3266 | { | |
3267 | int ret = 0; | |
3268 | ssize_t send_ret; | |
3269 | struct lttcomm_relayd_get_configuration *msg; | |
3270 | struct lttcomm_relayd_get_configuration_reply reply = {}; | |
3271 | struct lttng_buffer_view header_view; | |
3272 | uint64_t query_flags = 0; | |
3273 | uint64_t result_flags = 0; | |
3274 | ||
3275 | header_view = lttng_buffer_view_from_view(payload, 0, sizeof(*msg)); | |
3e6e0df2 | 3276 | if (!lttng_buffer_view_is_valid(&header_view)) { |
8614e600 MD |
3277 | ERR("Failed to receive payload of chunk close command"); |
3278 | ret = -1; | |
3279 | goto end_no_reply; | |
3280 | } | |
3281 | ||
3282 | /* Convert to host endianness. */ | |
3283 | msg = (typeof(msg)) header_view.data; | |
3284 | query_flags = be64toh(msg->query_flags); | |
3285 | ||
3286 | if (query_flags) { | |
3287 | ret = LTTNG_ERR_INVALID_PROTOCOL; | |
3288 | goto reply; | |
3289 | } | |
3290 | if (opt_allow_clear) { | |
3291 | result_flags |= LTTCOMM_RELAYD_CONFIGURATION_FLAG_CLEAR_ALLOWED; | |
3292 | } | |
3293 | ret = 0; | |
3294 | reply: | |
3295 | reply = (typeof(reply)){ | |
3296 | .generic.ret_code = htobe32((uint32_t) | |
3297 | (ret == 0 ? LTTNG_OK : LTTNG_ERR_INVALID_PROTOCOL)), | |
3298 | .relayd_configuration_flags = htobe64(result_flags), | |
3299 | }; | |
3300 | send_ret = conn->sock->ops->sendmsg( | |
3301 | conn->sock, &reply, sizeof(reply), 0); | |
3302 | if (send_ret < (ssize_t) sizeof(reply)) { | |
3303 | ERR("Failed to send \"get configuration\" command reply (ret = %zd)", | |
3304 | send_ret); | |
3305 | ret = -1; | |
3306 | } | |
3307 | end_no_reply: | |
3308 | return ret; | |
3309 | } | |
3310 | ||
5312a3ed JG |
3311 | #define DBG_CMD(cmd_name, conn) \ |
3312 | DBG3("Processing \"%s\" command for socket %i", cmd_name, conn->sock->fd); | |
3313 | ||
3314 | static int relay_process_control_command(struct relay_connection *conn, | |
3315 | const struct lttcomm_relayd_hdr *header, | |
3316 | const struct lttng_buffer_view *payload) | |
b8aa1682 JD |
3317 | { |
3318 | int ret = 0; | |
3319 | ||
5312a3ed | 3320 | switch (header->cmd) { |
b8aa1682 | 3321 | case RELAYD_CREATE_SESSION: |
5312a3ed JG |
3322 | DBG_CMD("RELAYD_CREATE_SESSION", conn); |
3323 | ret = relay_create_session(header, conn, payload); | |
b8aa1682 | 3324 | break; |
b8aa1682 | 3325 | case RELAYD_ADD_STREAM: |
5312a3ed JG |
3326 | DBG_CMD("RELAYD_ADD_STREAM", conn); |
3327 | ret = relay_add_stream(header, conn, payload); | |
b8aa1682 JD |
3328 | break; |
3329 | case RELAYD_START_DATA: | |
5312a3ed JG |
3330 | DBG_CMD("RELAYD_START_DATA", conn); |
3331 | ret = relay_start(header, conn, payload); | |
b8aa1682 JD |
3332 | break; |
3333 | case RELAYD_SEND_METADATA: | |
5312a3ed JG |
3334 | DBG_CMD("RELAYD_SEND_METADATA", conn); |
3335 | ret = relay_recv_metadata(header, conn, payload); | |
b8aa1682 JD |
3336 | break; |
3337 | case RELAYD_VERSION: | |
5312a3ed JG |
3338 | DBG_CMD("RELAYD_VERSION", conn); |
3339 | ret = relay_send_version(header, conn, payload); | |
b8aa1682 | 3340 | break; |
173af62f | 3341 | case RELAYD_CLOSE_STREAM: |
5312a3ed JG |
3342 | DBG_CMD("RELAYD_CLOSE_STREAM", conn); |
3343 | ret = relay_close_stream(header, conn, payload); | |
173af62f | 3344 | break; |
6d805429 | 3345 | case RELAYD_DATA_PENDING: |
5312a3ed JG |
3346 | DBG_CMD("RELAYD_DATA_PENDING", conn); |
3347 | ret = relay_data_pending(header, conn, payload); | |
c8f59ee5 DG |
3348 | break; |
3349 | case RELAYD_QUIESCENT_CONTROL: | |
5312a3ed JG |
3350 | DBG_CMD("RELAYD_QUIESCENT_CONTROL", conn); |
3351 | ret = relay_quiescent_control(header, conn, payload); | |
c8f59ee5 | 3352 | break; |
f7079f67 | 3353 | case RELAYD_BEGIN_DATA_PENDING: |
5312a3ed JG |
3354 | DBG_CMD("RELAYD_BEGIN_DATA_PENDING", conn); |
3355 | ret = relay_begin_data_pending(header, conn, payload); | |
f7079f67 DG |
3356 | break; |
3357 | case RELAYD_END_DATA_PENDING: | |
5312a3ed JG |
3358 | DBG_CMD("RELAYD_END_DATA_PENDING", conn); |
3359 | ret = relay_end_data_pending(header, conn, payload); | |
f7079f67 | 3360 | break; |
1c20f0e2 | 3361 | case RELAYD_SEND_INDEX: |
5312a3ed JG |
3362 | DBG_CMD("RELAYD_SEND_INDEX", conn); |
3363 | ret = relay_recv_index(header, conn, payload); | |
1c20f0e2 | 3364 | break; |
a4baae1b | 3365 | case RELAYD_STREAMS_SENT: |
5312a3ed JG |
3366 | DBG_CMD("RELAYD_STREAMS_SENT", conn); |
3367 | ret = relay_streams_sent(header, conn, payload); | |
a4baae1b | 3368 | break; |
93ec662e | 3369 | case RELAYD_RESET_METADATA: |
5312a3ed JG |
3370 | DBG_CMD("RELAYD_RESET_METADATA", conn); |
3371 | ret = relay_reset_metadata(header, conn, payload); | |
93ec662e | 3372 | break; |
c35f9726 JG |
3373 | case RELAYD_ROTATE_STREAMS: |
3374 | DBG_CMD("RELAYD_ROTATE_STREAMS", conn); | |
3375 | ret = relay_rotate_session_streams(header, conn, payload); | |
d3ecc550 | 3376 | break; |
e5add6d0 JG |
3377 | case RELAYD_CREATE_TRACE_CHUNK: |
3378 | DBG_CMD("RELAYD_CREATE_TRACE_CHUNK", conn); | |
3379 | ret = relay_create_trace_chunk(header, conn, payload); | |
3380 | break; | |
bbc4768c JG |
3381 | case RELAYD_CLOSE_TRACE_CHUNK: |
3382 | DBG_CMD("RELAYD_CLOSE_TRACE_CHUNK", conn); | |
3383 | ret = relay_close_trace_chunk(header, conn, payload); | |
3384 | break; | |
c35f9726 JG |
3385 | case RELAYD_TRACE_CHUNK_EXISTS: |
3386 | DBG_CMD("RELAYD_TRACE_CHUNK_EXISTS", conn); | |
3387 | ret = relay_trace_chunk_exists(header, conn, payload); | |
3388 | break; | |
8614e600 MD |
3389 | case RELAYD_GET_CONFIGURATION: |
3390 | DBG_CMD("RELAYD_GET_CONFIGURATION", conn); | |
3391 | ret = relay_get_configuration(header, conn, payload); | |
3392 | break; | |
b8aa1682 JD |
3393 | case RELAYD_UPDATE_SYNC_INFO: |
3394 | default: | |
5312a3ed | 3395 | ERR("Received unknown command (%u)", header->cmd); |
58eb9381 | 3396 | relay_unknown_command(conn); |
b8aa1682 JD |
3397 | ret = -1; |
3398 | goto end; | |
3399 | } | |
3400 | ||
3401 | end: | |
3402 | return ret; | |
3403 | } | |
3404 | ||
5569b118 JG |
3405 | static enum relay_connection_status relay_process_control_receive_payload( |
3406 | struct relay_connection *conn) | |
5312a3ed JG |
3407 | { |
3408 | int ret = 0; | |
5569b118 | 3409 | enum relay_connection_status status = RELAY_CONNECTION_STATUS_OK; |
5312a3ed JG |
3410 | struct lttng_dynamic_buffer *reception_buffer = |
3411 | &conn->protocol.ctrl.reception_buffer; | |
3412 | struct ctrl_connection_state_receive_payload *state = | |
3413 | &conn->protocol.ctrl.state.receive_payload; | |
3414 | struct lttng_buffer_view payload_view; | |
3415 | ||
3416 | if (state->left_to_receive == 0) { | |
3417 | /* Short-circuit for payload-less commands. */ | |
3418 | goto reception_complete; | |
3419 | } | |
3420 | ||
3421 | ret = conn->sock->ops->recvmsg(conn->sock, | |
3422 | reception_buffer->data + state->received, | |
3423 | state->left_to_receive, MSG_DONTWAIT); | |
3424 | if (ret < 0) { | |
5569b118 JG |
3425 | if (errno != EAGAIN && errno != EWOULDBLOCK) { |
3426 | PERROR("Unable to receive command payload on sock %d", | |
3427 | conn->sock->fd); | |
3428 | status = RELAY_CONNECTION_STATUS_ERROR; | |
3429 | } | |
5312a3ed JG |
3430 | goto end; |
3431 | } else if (ret == 0) { | |
3432 | DBG("Socket %d performed an orderly shutdown (received EOF)", conn->sock->fd); | |
5569b118 | 3433 | status = RELAY_CONNECTION_STATUS_CLOSED; |
5312a3ed JG |
3434 | goto end; |
3435 | } | |
3436 | ||
3437 | assert(ret > 0); | |
3438 | assert(ret <= state->left_to_receive); | |
3439 | ||
3440 | state->left_to_receive -= ret; | |
3441 | state->received += ret; | |
3442 | ||
3443 | if (state->left_to_receive > 0) { | |
3444 | /* | |
3445 | * Can't transition to the protocol's next state, wait to | |
3446 | * receive the rest of the header. | |
3447 | */ | |
3448 | DBG3("Partial reception of control connection protocol payload (received %" PRIu64 " bytes, %" PRIu64 " bytes left to receive, fd = %i)", | |
3449 | state->received, state->left_to_receive, | |
3450 | conn->sock->fd); | |
5312a3ed JG |
3451 | goto end; |
3452 | } | |
3453 | ||
3454 | reception_complete: | |
3455 | DBG("Done receiving control command payload: fd = %i, payload size = %" PRIu64 " bytes", | |
3456 | conn->sock->fd, state->received); | |
3457 | /* | |
3458 | * The payload required to process the command has been received. | |
3459 | * A view to the reception buffer is forwarded to the various | |
3460 | * commands and the state of the control is reset on success. | |
3461 | * | |
3462 | * Commands are responsible for sending their reply to the peer. | |
3463 | */ | |
3464 | payload_view = lttng_buffer_view_from_dynamic_buffer(reception_buffer, | |
3465 | 0, -1); | |
3466 | ret = relay_process_control_command(conn, | |
3467 | &state->header, &payload_view); | |
3468 | if (ret < 0) { | |
5569b118 | 3469 | status = RELAY_CONNECTION_STATUS_ERROR; |
5312a3ed JG |
3470 | goto end; |
3471 | } | |
3472 | ||
3473 | ret = connection_reset_protocol_state(conn); | |
5569b118 JG |
3474 | if (ret) { |
3475 | status = RELAY_CONNECTION_STATUS_ERROR; | |
3476 | } | |
5312a3ed | 3477 | end: |
5569b118 | 3478 | return status; |
5312a3ed JG |
3479 | } |
3480 | ||
5569b118 JG |
3481 | static enum relay_connection_status relay_process_control_receive_header( |
3482 | struct relay_connection *conn) | |
5312a3ed JG |
3483 | { |
3484 | int ret = 0; | |
5569b118 | 3485 | enum relay_connection_status status = RELAY_CONNECTION_STATUS_OK; |
5312a3ed JG |
3486 | struct lttcomm_relayd_hdr header; |
3487 | struct lttng_dynamic_buffer *reception_buffer = | |
3488 | &conn->protocol.ctrl.reception_buffer; | |
3489 | struct ctrl_connection_state_receive_header *state = | |
3490 | &conn->protocol.ctrl.state.receive_header; | |
3491 | ||
3492 | assert(state->left_to_receive != 0); | |
3493 | ||
3494 | ret = conn->sock->ops->recvmsg(conn->sock, | |
3495 | reception_buffer->data + state->received, | |
3496 | state->left_to_receive, MSG_DONTWAIT); | |
3497 | if (ret < 0) { | |
5569b118 JG |
3498 | if (errno != EAGAIN && errno != EWOULDBLOCK) { |
3499 | PERROR("Unable to receive control command header on sock %d", | |
3500 | conn->sock->fd); | |
3501 | status = RELAY_CONNECTION_STATUS_ERROR; | |
3502 | } | |
5312a3ed JG |
3503 | goto end; |
3504 | } else if (ret == 0) { | |
3505 | DBG("Socket %d performed an orderly shutdown (received EOF)", conn->sock->fd); | |
5569b118 | 3506 | status = RELAY_CONNECTION_STATUS_CLOSED; |
5312a3ed JG |
3507 | goto end; |
3508 | } | |
3509 | ||
3510 | assert(ret > 0); | |
3511 | assert(ret <= state->left_to_receive); | |
3512 | ||
3513 | state->left_to_receive -= ret; | |
3514 | state->received += ret; | |
3515 | ||
3516 | if (state->left_to_receive > 0) { | |
3517 | /* | |
3518 | * Can't transition to the protocol's next state, wait to | |
3519 | * receive the rest of the header. | |
3520 | */ | |
3521 | DBG3("Partial reception of control connection protocol header (received %" PRIu64 " bytes, %" PRIu64 " bytes left to receive, fd = %i)", | |
3522 | state->received, state->left_to_receive, | |
3523 | conn->sock->fd); | |
5312a3ed JG |
3524 | goto end; |
3525 | } | |
3526 | ||
3527 | /* Transition to next state: receiving the command's payload. */ | |
3528 | conn->protocol.ctrl.state_id = | |
3529 | CTRL_CONNECTION_STATE_RECEIVE_PAYLOAD; | |
3530 | memcpy(&header, reception_buffer->data, sizeof(header)); | |
3531 | header.circuit_id = be64toh(header.circuit_id); | |
3532 | header.data_size = be64toh(header.data_size); | |
3533 | header.cmd = be32toh(header.cmd); | |
3534 | header.cmd_version = be32toh(header.cmd_version); | |
3535 | memcpy(&conn->protocol.ctrl.state.receive_payload.header, | |
3536 | &header, sizeof(header)); | |
3537 | ||
3538 | DBG("Done receiving control command header: fd = %i, cmd = %" PRIu32 ", cmd_version = %" PRIu32 ", payload size = %" PRIu64 " bytes", | |
3539 | conn->sock->fd, header.cmd, header.cmd_version, | |
3540 | header.data_size); | |
3541 | ||
715e6fb1 | 3542 | if (header.data_size > DEFAULT_NETWORK_RELAYD_CTRL_MAX_PAYLOAD_SIZE) { |
5312a3ed JG |
3543 | ERR("Command header indicates a payload (%" PRIu64 " bytes) that exceeds the maximal payload size allowed on a control connection.", |
3544 | header.data_size); | |
5569b118 | 3545 | status = RELAY_CONNECTION_STATUS_ERROR; |
5312a3ed JG |
3546 | goto end; |
3547 | } | |
3548 | ||
3549 | conn->protocol.ctrl.state.receive_payload.left_to_receive = | |
3550 | header.data_size; | |
3551 | conn->protocol.ctrl.state.receive_payload.received = 0; | |
3552 | ret = lttng_dynamic_buffer_set_size(reception_buffer, | |
3553 | header.data_size); | |
3554 | if (ret) { | |
5569b118 | 3555 | status = RELAY_CONNECTION_STATUS_ERROR; |
5312a3ed JG |
3556 | goto end; |
3557 | } | |
3558 | ||
3559 | if (header.data_size == 0) { | |
3560 | /* | |
3561 | * Manually invoke the next state as the poll loop | |
3562 | * will not wake-up to allow us to proceed further. | |
3563 | */ | |
5569b118 | 3564 | status = relay_process_control_receive_payload(conn); |
5312a3ed JG |
3565 | } |
3566 | end: | |
5569b118 | 3567 | return status; |
5312a3ed JG |
3568 | } |
3569 | ||
3570 | /* | |
3571 | * Process the commands received on the control socket | |
3572 | */ | |
5569b118 JG |
3573 | static enum relay_connection_status relay_process_control( |
3574 | struct relay_connection *conn) | |
5312a3ed | 3575 | { |
5569b118 | 3576 | enum relay_connection_status status; |
5312a3ed JG |
3577 | |
3578 | switch (conn->protocol.ctrl.state_id) { | |
3579 | case CTRL_CONNECTION_STATE_RECEIVE_HEADER: | |
5569b118 | 3580 | status = relay_process_control_receive_header(conn); |
5312a3ed JG |
3581 | break; |
3582 | case CTRL_CONNECTION_STATE_RECEIVE_PAYLOAD: | |
5569b118 | 3583 | status = relay_process_control_receive_payload(conn); |
5312a3ed JG |
3584 | break; |
3585 | default: | |
3586 | ERR("Unknown control connection protocol state encountered."); | |
3587 | abort(); | |
3588 | } | |
3589 | ||
5569b118 | 3590 | return status; |
5312a3ed JG |
3591 | } |
3592 | ||
5569b118 JG |
3593 | static enum relay_connection_status relay_process_data_receive_header( |
3594 | struct relay_connection *conn) | |
b8aa1682 | 3595 | { |
5312a3ed | 3596 | int ret; |
5569b118 | 3597 | enum relay_connection_status status = RELAY_CONNECTION_STATUS_OK; |
5312a3ed JG |
3598 | struct data_connection_state_receive_header *state = |
3599 | &conn->protocol.data.state.receive_header; | |
3600 | struct lttcomm_relayd_data_hdr header; | |
b8aa1682 | 3601 | struct relay_stream *stream; |
5312a3ed JG |
3602 | |
3603 | assert(state->left_to_receive != 0); | |
3604 | ||
3605 | ret = conn->sock->ops->recvmsg(conn->sock, | |
3606 | state->header_reception_buffer + state->received, | |
3607 | state->left_to_receive, MSG_DONTWAIT); | |
3608 | if (ret < 0) { | |
5569b118 JG |
3609 | if (errno != EAGAIN && errno != EWOULDBLOCK) { |
3610 | PERROR("Unable to receive data header on sock %d", conn->sock->fd); | |
3611 | status = RELAY_CONNECTION_STATUS_ERROR; | |
3612 | } | |
5312a3ed JG |
3613 | goto end; |
3614 | } else if (ret == 0) { | |
3615 | /* Orderly shutdown. Not necessary to print an error. */ | |
3616 | DBG("Socket %d performed an orderly shutdown (received EOF)", conn->sock->fd); | |
5569b118 | 3617 | status = RELAY_CONNECTION_STATUS_CLOSED; |
b8aa1682 JD |
3618 | goto end; |
3619 | } | |
3620 | ||
5312a3ed JG |
3621 | assert(ret > 0); |
3622 | assert(ret <= state->left_to_receive); | |
3623 | ||
3624 | state->left_to_receive -= ret; | |
3625 | state->received += ret; | |
3626 | ||
3627 | if (state->left_to_receive > 0) { | |
3628 | /* | |
3629 | * Can't transition to the protocol's next state, wait to | |
3630 | * receive the rest of the header. | |
3631 | */ | |
3632 | DBG3("Partial reception of data connection header (received %" PRIu64 " bytes, %" PRIu64 " bytes left to receive, fd = %i)", | |
3633 | state->received, state->left_to_receive, | |
3634 | conn->sock->fd); | |
7591bab1 | 3635 | goto end; |
b8aa1682 | 3636 | } |
b8aa1682 | 3637 | |
5312a3ed JG |
3638 | /* Transition to next state: receiving the payload. */ |
3639 | conn->protocol.data.state_id = DATA_CONNECTION_STATE_RECEIVE_PAYLOAD; | |
173af62f | 3640 | |
5312a3ed JG |
3641 | memcpy(&header, state->header_reception_buffer, sizeof(header)); |
3642 | header.circuit_id = be64toh(header.circuit_id); | |
3643 | header.stream_id = be64toh(header.stream_id); | |
3644 | header.data_size = be32toh(header.data_size); | |
3645 | header.net_seq_num = be64toh(header.net_seq_num); | |
3646 | header.padding_size = be32toh(header.padding_size); | |
3647 | memcpy(&conn->protocol.data.state.receive_payload.header, &header, sizeof(header)); | |
3648 | ||
3649 | conn->protocol.data.state.receive_payload.left_to_receive = | |
3650 | header.data_size; | |
3651 | conn->protocol.data.state.receive_payload.received = 0; | |
3652 | conn->protocol.data.state.receive_payload.rotate_index = false; | |
3653 | ||
3654 | DBG("Received data connection header on fd %i: circuit_id = %" PRIu64 ", stream_id = %" PRIu64 ", data_size = %" PRIu32 ", net_seq_num = %" PRIu64 ", padding_size = %" PRIu32, | |
3655 | conn->sock->fd, header.circuit_id, | |
3656 | header.stream_id, header.data_size, | |
3657 | header.net_seq_num, header.padding_size); | |
3658 | ||
3659 | stream = stream_get_by_id(header.stream_id); | |
3660 | if (!stream) { | |
3661 | DBG("relay_process_data_receive_payload: Cannot find stream %" PRIu64, | |
3662 | header.stream_id); | |
5569b118 JG |
3663 | /* Protocol error. */ |
3664 | status = RELAY_CONNECTION_STATUS_ERROR; | |
5312a3ed JG |
3665 | goto end; |
3666 | } | |
b8aa1682 | 3667 | |
7591bab1 | 3668 | pthread_mutex_lock(&stream->lock); |
c35f9726 JG |
3669 | /* Prepare stream for the reception of a new packet. */ |
3670 | ret = stream_init_packet(stream, header.data_size, | |
3671 | &conn->protocol.data.state.receive_payload.rotate_index); | |
3672 | pthread_mutex_unlock(&stream->lock); | |
3673 | if (ret) { | |
3674 | ERR("Failed to rotate stream output file"); | |
3675 | status = RELAY_CONNECTION_STATUS_ERROR; | |
3676 | goto end_stream_unlock; | |
1c20f0e2 JD |
3677 | } |
3678 | ||
5312a3ed | 3679 | end_stream_unlock: |
5312a3ed JG |
3680 | stream_put(stream); |
3681 | end: | |
5569b118 | 3682 | return status; |
5312a3ed JG |
3683 | } |
3684 | ||
5569b118 JG |
3685 | static enum relay_connection_status relay_process_data_receive_payload( |
3686 | struct relay_connection *conn) | |
5312a3ed JG |
3687 | { |
3688 | int ret; | |
5569b118 | 3689 | enum relay_connection_status status = RELAY_CONNECTION_STATUS_OK; |
5312a3ed JG |
3690 | struct relay_stream *stream; |
3691 | struct data_connection_state_receive_payload *state = | |
3692 | &conn->protocol.data.state.receive_payload; | |
3693 | const size_t chunk_size = RECV_DATA_BUFFER_SIZE; | |
3694 | char data_buffer[chunk_size]; | |
3695 | bool partial_recv = false; | |
3696 | bool new_stream = false, close_requested = false, index_flushed = false; | |
3697 | uint64_t left_to_receive = state->left_to_receive; | |
3698 | struct relay_session *session; | |
3699 | ||
fd0f1e3e JR |
3700 | DBG3("Receiving data for stream id %" PRIu64 " seqnum %" PRIu64 ", %" PRIu64" bytes received, %" PRIu64 " bytes left to receive", |
3701 | state->header.stream_id, state->header.net_seq_num, | |
3702 | state->received, left_to_receive); | |
3703 | ||
5312a3ed JG |
3704 | stream = stream_get_by_id(state->header.stream_id); |
3705 | if (!stream) { | |
5569b118 | 3706 | /* Protocol error. */ |
fd0f1e3e | 3707 | ERR("relay_process_data_receive_payload: cannot find stream %" PRIu64, |
5312a3ed | 3708 | state->header.stream_id); |
5569b118 | 3709 | status = RELAY_CONNECTION_STATUS_ERROR; |
5312a3ed | 3710 | goto end; |
1c20f0e2 JD |
3711 | } |
3712 | ||
5312a3ed JG |
3713 | pthread_mutex_lock(&stream->lock); |
3714 | session = stream->trace->session; | |
fd0f1e3e JR |
3715 | if (!conn->session) { |
3716 | ret = connection_set_session(conn, session); | |
3717 | if (ret) { | |
3718 | status = RELAY_CONNECTION_STATUS_ERROR; | |
3719 | goto end_stream_unlock; | |
3720 | } | |
3721 | } | |
5312a3ed JG |
3722 | |
3723 | /* | |
3724 | * The size of the "chunk" received on any iteration is bounded by: | |
3725 | * - the data left to receive, | |
3726 | * - the data immediately available on the socket, | |
3727 | * - the on-stack data buffer | |
3728 | */ | |
3729 | while (left_to_receive > 0 && !partial_recv) { | |
5312a3ed | 3730 | size_t recv_size = min(left_to_receive, chunk_size); |
c35f9726 | 3731 | struct lttng_buffer_view packet_chunk; |
5312a3ed JG |
3732 | |
3733 | ret = conn->sock->ops->recvmsg(conn->sock, data_buffer, | |
3734 | recv_size, MSG_DONTWAIT); | |
3735 | if (ret < 0) { | |
5569b118 JG |
3736 | if (errno != EAGAIN && errno != EWOULDBLOCK) { |
3737 | PERROR("Socket %d error", conn->sock->fd); | |
3738 | status = RELAY_CONNECTION_STATUS_ERROR; | |
3739 | } | |
0848dba7 | 3740 | goto end_stream_unlock; |
5312a3ed JG |
3741 | } else if (ret == 0) { |
3742 | /* No more data ready to be consumed on socket. */ | |
3743 | DBG3("No more data ready for consumption on data socket of stream id %" PRIu64, | |
3744 | state->header.stream_id); | |
5569b118 | 3745 | status = RELAY_CONNECTION_STATUS_CLOSED; |
5312a3ed JG |
3746 | break; |
3747 | } else if (ret < (int) recv_size) { | |
3748 | /* | |
3749 | * All the data available on the socket has been | |
3750 | * consumed. | |
3751 | */ | |
3752 | partial_recv = true; | |
c35f9726 | 3753 | recv_size = ret; |
0848dba7 MD |
3754 | } |
3755 | ||
c35f9726 JG |
3756 | packet_chunk = lttng_buffer_view_init(data_buffer, |
3757 | 0, recv_size); | |
3758 | assert(packet_chunk.data); | |
5312a3ed | 3759 | |
c35f9726 JG |
3760 | ret = stream_write(stream, &packet_chunk, 0); |
3761 | if (ret) { | |
0848dba7 | 3762 | ERR("Relay error writing data to file"); |
5569b118 | 3763 | status = RELAY_CONNECTION_STATUS_ERROR; |
0848dba7 MD |
3764 | goto end_stream_unlock; |
3765 | } | |
3766 | ||
5312a3ed JG |
3767 | left_to_receive -= recv_size; |
3768 | state->received += recv_size; | |
3769 | state->left_to_receive = left_to_receive; | |
5312a3ed JG |
3770 | } |
3771 | ||
3772 | if (state->left_to_receive > 0) { | |
3773 | /* | |
3774 | * Did not receive all the data expected, wait for more data to | |
3775 | * become available on the socket. | |
3776 | */ | |
3777 | DBG3("Partial receive on data connection of stream id %" PRIu64 ", %" PRIu64 " bytes received, %" PRIu64 " bytes left to receive", | |
3778 | state->header.stream_id, state->received, | |
3779 | state->left_to_receive); | |
5312a3ed | 3780 | goto end_stream_unlock; |
0848dba7 | 3781 | } |
5ab7344e | 3782 | |
c35f9726 JG |
3783 | ret = stream_write(stream, NULL, state->header.padding_size); |
3784 | if (ret) { | |
5569b118 | 3785 | status = RELAY_CONNECTION_STATUS_ERROR; |
7591bab1 | 3786 | goto end_stream_unlock; |
1d4dfdef | 3787 | } |
5312a3ed | 3788 | |
298a25ca | 3789 | if (session_streams_have_index(session)) { |
c35f9726 JG |
3790 | ret = stream_update_index(stream, state->header.net_seq_num, |
3791 | state->rotate_index, &index_flushed, | |
3792 | state->header.data_size + state->header.padding_size); | |
5312a3ed | 3793 | if (ret < 0) { |
c35f9726 | 3794 | ERR("Failed to update index: stream %" PRIu64 " net_seq_num %" PRIu64 " ret %d", |
5312a3ed JG |
3795 | stream->stream_handle, |
3796 | state->header.net_seq_num, ret); | |
5569b118 | 3797 | status = RELAY_CONNECTION_STATUS_ERROR; |
5312a3ed JG |
3798 | goto end_stream_unlock; |
3799 | } | |
3800 | } | |
3801 | ||
a8f9f353 | 3802 | if (stream->prev_data_seq == -1ULL) { |
c0bae11d MD |
3803 | new_stream = true; |
3804 | } | |
3805 | ||
c35f9726 JG |
3806 | ret = stream_complete_packet(stream, state->header.data_size + |
3807 | state->header.padding_size, state->header.net_seq_num, | |
3808 | index_flushed); | |
3809 | if (ret) { | |
3810 | status = RELAY_CONNECTION_STATUS_ERROR; | |
3811 | goto end_stream_unlock; | |
3812 | } | |
5312a3ed JG |
3813 | |
3814 | /* | |
3815 | * Resetting the protocol state (to RECEIVE_HEADER) will trash the | |
3816 | * contents of *state which are aliased (union) to the same location as | |
3817 | * the new state. Don't use it beyond this point. | |
3818 | */ | |
3819 | connection_reset_protocol_state(conn); | |
3820 | state = NULL; | |
173af62f | 3821 | |
7591bab1 | 3822 | end_stream_unlock: |
bda7c7b9 | 3823 | close_requested = stream->close_requested; |
7591bab1 | 3824 | pthread_mutex_unlock(&stream->lock); |
5312a3ed | 3825 | if (close_requested && left_to_receive == 0) { |
bda7c7b9 JG |
3826 | try_stream_close(stream); |
3827 | } | |
3828 | ||
c0bae11d MD |
3829 | if (new_stream) { |
3830 | pthread_mutex_lock(&session->lock); | |
3831 | uatomic_set(&session->new_streams, 1); | |
3832 | pthread_mutex_unlock(&session->lock); | |
3833 | } | |
5312a3ed | 3834 | |
7591bab1 | 3835 | stream_put(stream); |
b8aa1682 | 3836 | end: |
5569b118 | 3837 | return status; |
b8aa1682 JD |
3838 | } |
3839 | ||
5312a3ed JG |
3840 | /* |
3841 | * relay_process_data: Process the data received on the data socket | |
3842 | */ | |
5569b118 JG |
3843 | static enum relay_connection_status relay_process_data( |
3844 | struct relay_connection *conn) | |
5312a3ed | 3845 | { |
5569b118 | 3846 | enum relay_connection_status status; |
5312a3ed JG |
3847 | |
3848 | switch (conn->protocol.data.state_id) { | |
3849 | case DATA_CONNECTION_STATE_RECEIVE_HEADER: | |
5569b118 | 3850 | status = relay_process_data_receive_header(conn); |
5312a3ed JG |
3851 | break; |
3852 | case DATA_CONNECTION_STATE_RECEIVE_PAYLOAD: | |
5569b118 | 3853 | status = relay_process_data_receive_payload(conn); |
5312a3ed JG |
3854 | break; |
3855 | default: | |
3856 | ERR("Unexpected data connection communication state."); | |
3857 | abort(); | |
3858 | } | |
3859 | ||
5569b118 | 3860 | return status; |
5312a3ed JG |
3861 | } |
3862 | ||
7591bab1 | 3863 | static void cleanup_connection_pollfd(struct lttng_poll_event *events, int pollfd) |
b8aa1682 JD |
3864 | { |
3865 | int ret; | |
3866 | ||
58eb9381 | 3867 | (void) lttng_poll_del(events, pollfd); |
b8aa1682 | 3868 | |
f355467e JG |
3869 | ret = fd_tracker_close_unsuspendable_fd(the_fd_tracker, &pollfd, 1, |
3870 | fd_tracker_util_close_fd, NULL); | |
b8aa1682 JD |
3871 | if (ret < 0) { |
3872 | ERR("Closing pollfd %d", pollfd); | |
3873 | } | |
3874 | } | |
3875 | ||
7591bab1 MD |
3876 | static void relay_thread_close_connection(struct lttng_poll_event *events, |
3877 | int pollfd, struct relay_connection *conn) | |
9d1bbf21 | 3878 | { |
7591bab1 | 3879 | const char *type_str; |
2a174661 | 3880 | |
7591bab1 MD |
3881 | switch (conn->type) { |
3882 | case RELAY_DATA: | |
3883 | type_str = "Data"; | |
3884 | break; | |
3885 | case RELAY_CONTROL: | |
3886 | type_str = "Control"; | |
3887 | break; | |
3888 | case RELAY_VIEWER_COMMAND: | |
3889 | type_str = "Viewer Command"; | |
3890 | break; | |
3891 | case RELAY_VIEWER_NOTIFICATION: | |
3892 | type_str = "Viewer Notification"; | |
3893 | break; | |
3894 | default: | |
3895 | type_str = "Unknown"; | |
9d1bbf21 | 3896 | } |
7591bab1 MD |
3897 | cleanup_connection_pollfd(events, pollfd); |
3898 | connection_put(conn); | |
3899 | DBG("%s connection closed with %d", type_str, pollfd); | |
b8aa1682 JD |
3900 | } |
3901 | ||
3902 | /* | |
3903 | * This thread does the actual work | |
3904 | */ | |
7591bab1 | 3905 | static void *relay_thread_worker(void *data) |
b8aa1682 | 3906 | { |
beaad64c DG |
3907 | int ret, err = -1, last_seen_data_fd = -1; |
3908 | uint32_t nb_fd; | |
b8aa1682 JD |
3909 | struct lttng_poll_event events; |
3910 | struct lttng_ht *relay_connections_ht; | |
b8aa1682 | 3911 | struct lttng_ht_iter iter; |
90e7d72f | 3912 | struct relay_connection *destroy_conn = NULL; |
b8aa1682 JD |
3913 | |
3914 | DBG("[thread] Relay worker started"); | |
3915 | ||
9d1bbf21 MD |
3916 | rcu_register_thread(); |
3917 | ||
55706a7d MD |
3918 | health_register(health_relayd, HEALTH_RELAYD_TYPE_WORKER); |
3919 | ||
9b5e0863 MD |
3920 | if (testpoint(relayd_thread_worker)) { |
3921 | goto error_testpoint; | |
3922 | } | |
3923 | ||
f385ae0a MD |
3924 | health_code_update(); |
3925 | ||
b8aa1682 JD |
3926 | /* table of connections indexed on socket */ |
3927 | relay_connections_ht = lttng_ht_new(0, LTTNG_HT_TYPE_ULONG); | |
095a4ae5 MD |
3928 | if (!relay_connections_ht) { |
3929 | goto relay_connections_ht_error; | |
3930 | } | |
b8aa1682 | 3931 | |
e32a0864 | 3932 | ret = create_named_thread_poll_set(&events, 2, "Worker thread epoll"); |
b8aa1682 JD |
3933 | if (ret < 0) { |
3934 | goto error_poll_create; | |
3935 | } | |
3936 | ||
58eb9381 | 3937 | ret = lttng_poll_add(&events, relay_conn_pipe[0], LPOLLIN | LPOLLRDHUP); |
b8aa1682 JD |
3938 | if (ret < 0) { |
3939 | goto error; | |
3940 | } | |
3941 | ||
beaad64c | 3942 | restart: |
b8aa1682 | 3943 | while (1) { |
beaad64c DG |
3944 | int idx = -1, i, seen_control = 0, last_notdel_data_fd = -1; |
3945 | ||
f385ae0a MD |
3946 | health_code_update(); |
3947 | ||
b8aa1682 | 3948 | /* Infinite blocking call, waiting for transmission */ |
87c1611d | 3949 | DBG3("Relayd worker thread polling..."); |
f385ae0a | 3950 | health_poll_entry(); |
b8aa1682 | 3951 | ret = lttng_poll_wait(&events, -1); |
f385ae0a | 3952 | health_poll_exit(); |
b8aa1682 JD |
3953 | if (ret < 0) { |
3954 | /* | |
3955 | * Restart interrupted system call. | |
3956 | */ | |
3957 | if (errno == EINTR) { | |
3958 | goto restart; | |
3959 | } | |
3960 | goto error; | |
3961 | } | |
3962 | ||
0d9c5d77 DG |
3963 | nb_fd = ret; |
3964 | ||
beaad64c | 3965 | /* |
7591bab1 MD |
3966 | * Process control. The control connection is |
3967 | * prioritized so we don't starve it with high | |
3968 | * throughput tracing data on the data connection. | |
beaad64c | 3969 | */ |
b8aa1682 JD |
3970 | for (i = 0; i < nb_fd; i++) { |
3971 | /* Fetch once the poll data */ | |
beaad64c DG |
3972 | uint32_t revents = LTTNG_POLL_GETEV(&events, i); |
3973 | int pollfd = LTTNG_POLL_GETFD(&events, i); | |
b8aa1682 | 3974 | |
f385ae0a MD |
3975 | health_code_update(); |
3976 | ||
b8aa1682 JD |
3977 | /* Thread quit pipe has been closed. Killing thread. */ |
3978 | ret = check_thread_quit_pipe(pollfd, revents); | |
3979 | if (ret) { | |
095a4ae5 MD |
3980 | err = 0; |
3981 | goto exit; | |
b8aa1682 JD |
3982 | } |
3983 | ||
58eb9381 DG |
3984 | /* Inspect the relay conn pipe for new connection */ |
3985 | if (pollfd == relay_conn_pipe[0]) { | |
03e43155 | 3986 | if (revents & LPOLLIN) { |
90e7d72f JG |
3987 | struct relay_connection *conn; |
3988 | ||
58eb9381 | 3989 | ret = lttng_read(relay_conn_pipe[0], &conn, sizeof(conn)); |
b8aa1682 JD |
3990 | if (ret < 0) { |
3991 | goto error; | |
3992 | } | |
73039936 FD |
3993 | ret = lttng_poll_add(&events, |
3994 | conn->sock->fd, | |
58eb9381 | 3995 | LPOLLIN | LPOLLRDHUP); |
73039936 FD |
3996 | if (ret) { |
3997 | ERR("Failed to add new connection file descriptor to poll set"); | |
3998 | goto error; | |
3999 | } | |
7591bab1 | 4000 | connection_ht_add(relay_connections_ht, conn); |
58eb9381 | 4001 | DBG("Connection socket %d added", conn->sock->fd); |
03e43155 MD |
4002 | } else if (revents & (LPOLLERR | LPOLLHUP | LPOLLRDHUP)) { |
4003 | ERR("Relay connection pipe error"); | |
4004 | goto error; | |
4005 | } else { | |
4006 | ERR("Unexpected poll events %u for sock %d", revents, pollfd); | |
4007 | goto error; | |
b8aa1682 | 4008 | } |
58eb9381 | 4009 | } else { |
90e7d72f JG |
4010 | struct relay_connection *ctrl_conn; |
4011 | ||
7591bab1 | 4012 | ctrl_conn = connection_get_by_sock(relay_connections_ht, pollfd); |
58eb9381 | 4013 | /* If not found, there is a synchronization issue. */ |
90e7d72f | 4014 | assert(ctrl_conn); |
58eb9381 | 4015 | |
03e43155 MD |
4016 | if (ctrl_conn->type == RELAY_DATA) { |
4017 | if (revents & LPOLLIN) { | |
beaad64c DG |
4018 | /* |
4019 | * Flag the last seen data fd not deleted. It will be | |
4020 | * used as the last seen fd if any fd gets deleted in | |
4021 | * this first loop. | |
4022 | */ | |
4023 | last_notdel_data_fd = pollfd; | |
4024 | } | |
03e43155 MD |
4025 | goto put_ctrl_connection; |
4026 | } | |
4027 | assert(ctrl_conn->type == RELAY_CONTROL); | |
4028 | ||
4029 | if (revents & LPOLLIN) { | |
5569b118 JG |
4030 | enum relay_connection_status status; |
4031 | ||
4032 | status = relay_process_control(ctrl_conn); | |
4033 | if (status != RELAY_CONNECTION_STATUS_OK) { | |
fd0f1e3e JR |
4034 | /* |
4035 | * On socket error flag the session as aborted to force | |
4036 | * the cleanup of its stream otherwise it can leak | |
4037 | * during the lifetime of the relayd. | |
4038 | * | |
4039 | * This prevents situations in which streams can be | |
4040 | * left opened because an index was received, the | |
4041 | * control connection is closed, and the data | |
4042 | * connection is closed (uncleanly) before the packet's | |
4043 | * data provided. | |
4044 | * | |
4045 | * Since the control connection encountered an error, | |
4046 | * it is okay to be conservative and close the | |
4047 | * session right now as we can't rely on the protocol | |
4048 | * being respected anymore. | |
4049 | */ | |
4050 | if (status == RELAY_CONNECTION_STATUS_ERROR) { | |
4051 | session_abort(ctrl_conn->session); | |
4052 | } | |
4053 | ||
5569b118 | 4054 | /* Clear the connection on error or close. */ |
5312a3ed JG |
4055 | relay_thread_close_connection(&events, |
4056 | pollfd, | |
03e43155 | 4057 | ctrl_conn); |
03e43155 | 4058 | } |
5312a3ed | 4059 | seen_control = 1; |
03e43155 MD |
4060 | } else if (revents & (LPOLLERR | LPOLLHUP | LPOLLRDHUP)) { |
4061 | relay_thread_close_connection(&events, | |
4062 | pollfd, ctrl_conn); | |
4063 | if (last_seen_data_fd == pollfd) { | |
4064 | last_seen_data_fd = last_notdel_data_fd; | |
4065 | } | |
58eb9381 | 4066 | } else { |
03e43155 MD |
4067 | ERR("Unexpected poll events %u for control sock %d", |
4068 | revents, pollfd); | |
4069 | connection_put(ctrl_conn); | |
4070 | goto error; | |
beaad64c | 4071 | } |
03e43155 | 4072 | put_ctrl_connection: |
7591bab1 | 4073 | connection_put(ctrl_conn); |
beaad64c DG |
4074 | } |
4075 | } | |
4076 | ||
4077 | /* | |
4078 | * The last loop handled a control request, go back to poll to make | |
4079 | * sure we prioritise the control socket. | |
4080 | */ | |
4081 | if (seen_control) { | |
4082 | continue; | |
4083 | } | |
4084 | ||
4085 | if (last_seen_data_fd >= 0) { | |
4086 | for (i = 0; i < nb_fd; i++) { | |
4087 | int pollfd = LTTNG_POLL_GETFD(&events, i); | |
f385ae0a MD |
4088 | |
4089 | health_code_update(); | |
4090 | ||
beaad64c DG |
4091 | if (last_seen_data_fd == pollfd) { |
4092 | idx = i; | |
4093 | break; | |
4094 | } | |
4095 | } | |
4096 | } | |
4097 | ||
4098 | /* Process data connection. */ | |
4099 | for (i = idx + 1; i < nb_fd; i++) { | |
4100 | /* Fetch the poll data. */ | |
4101 | uint32_t revents = LTTNG_POLL_GETEV(&events, i); | |
4102 | int pollfd = LTTNG_POLL_GETFD(&events, i); | |
90e7d72f | 4103 | struct relay_connection *data_conn; |
beaad64c | 4104 | |
f385ae0a MD |
4105 | health_code_update(); |
4106 | ||
fd20dac9 MD |
4107 | if (!revents) { |
4108 | /* No activity for this FD (poll implementation). */ | |
4109 | continue; | |
4110 | } | |
4111 | ||
beaad64c | 4112 | /* Skip the command pipe. It's handled in the first loop. */ |
58eb9381 | 4113 | if (pollfd == relay_conn_pipe[0]) { |
beaad64c DG |
4114 | continue; |
4115 | } | |
4116 | ||
7591bab1 | 4117 | data_conn = connection_get_by_sock(relay_connections_ht, pollfd); |
90e7d72f | 4118 | if (!data_conn) { |
fd20dac9 | 4119 | /* Skip it. Might be removed before. */ |
fd20dac9 MD |
4120 | continue; |
4121 | } | |
03e43155 MD |
4122 | if (data_conn->type == RELAY_CONTROL) { |
4123 | goto put_data_connection; | |
4124 | } | |
4125 | assert(data_conn->type == RELAY_DATA); | |
fd20dac9 MD |
4126 | |
4127 | if (revents & LPOLLIN) { | |
5569b118 JG |
4128 | enum relay_connection_status status; |
4129 | ||
4130 | status = relay_process_data(data_conn); | |
4131 | /* Connection closed or error. */ | |
4132 | if (status != RELAY_CONNECTION_STATUS_OK) { | |
fd0f1e3e JR |
4133 | /* |
4134 | * On socket error flag the session as aborted to force | |
4135 | * the cleanup of its stream otherwise it can leak | |
4136 | * during the lifetime of the relayd. | |
4137 | * | |
4138 | * This prevents situations in which streams can be | |
4139 | * left opened because an index was received, the | |
4140 | * control connection is closed, and the data | |
4141 | * connection is closed (uncleanly) before the packet's | |
4142 | * data provided. | |
4143 | * | |
4144 | * Since the data connection encountered an error, | |
4145 | * it is okay to be conservative and close the | |
4146 | * session right now as we can't rely on the protocol | |
4147 | * being respected anymore. | |
4148 | */ | |
4149 | if (status == RELAY_CONNECTION_STATUS_ERROR) { | |
4150 | session_abort(data_conn->session); | |
4151 | } | |
7591bab1 | 4152 | relay_thread_close_connection(&events, pollfd, |
03e43155 | 4153 | data_conn); |
fd20dac9 MD |
4154 | /* |
4155 | * Every goto restart call sets the last seen fd where | |
4156 | * here we don't really care since we gracefully | |
4157 | * continue the loop after the connection is deleted. | |
4158 | */ | |
4159 | } else { | |
4160 | /* Keep last seen port. */ | |
4161 | last_seen_data_fd = pollfd; | |
7591bab1 | 4162 | connection_put(data_conn); |
fd20dac9 | 4163 | goto restart; |
b8aa1682 | 4164 | } |
03e43155 MD |
4165 | } else if (revents & (LPOLLERR | LPOLLHUP | LPOLLRDHUP)) { |
4166 | relay_thread_close_connection(&events, pollfd, | |
4167 | data_conn); | |
4168 | } else { | |
4169 | ERR("Unknown poll events %u for data sock %d", | |
4170 | revents, pollfd); | |
b8aa1682 | 4171 | } |
03e43155 | 4172 | put_data_connection: |
7591bab1 | 4173 | connection_put(data_conn); |
b8aa1682 | 4174 | } |
beaad64c | 4175 | last_seen_data_fd = -1; |
b8aa1682 JD |
4176 | } |
4177 | ||
f385ae0a MD |
4178 | /* Normal exit, no error */ |
4179 | ret = 0; | |
4180 | ||
095a4ae5 | 4181 | exit: |
b8aa1682 | 4182 | error: |
71efa8ef | 4183 | /* Cleanup remaining connection object. */ |
9d1bbf21 | 4184 | rcu_read_lock(); |
90e7d72f JG |
4185 | cds_lfht_for_each_entry(relay_connections_ht->ht, &iter.iter, |
4186 | destroy_conn, | |
58eb9381 | 4187 | sock_n.node) { |
f385ae0a | 4188 | health_code_update(); |
98ba050e | 4189 | |
fd0f1e3e | 4190 | session_abort(destroy_conn->session); |
98ba050e | 4191 | |
7591bab1 MD |
4192 | /* |
4193 | * No need to grab another ref, because we own | |
4194 | * destroy_conn. | |
4195 | */ | |
4196 | relay_thread_close_connection(&events, destroy_conn->sock->fd, | |
4197 | destroy_conn); | |
b8aa1682 | 4198 | } |
94d49140 | 4199 | rcu_read_unlock(); |
7591bab1 | 4200 | |
e32a0864 | 4201 | (void) fd_tracker_util_poll_clean(the_fd_tracker, &events); |
7d2f7452 | 4202 | error_poll_create: |
b8aa1682 | 4203 | lttng_ht_destroy(relay_connections_ht); |
095a4ae5 | 4204 | relay_connections_ht_error: |
58eb9381 | 4205 | /* Close relay conn pipes */ |
726b2396 JG |
4206 | (void) fd_tracker_util_pipe_close(the_fd_tracker, |
4207 | relay_conn_pipe); | |
095a4ae5 MD |
4208 | if (err) { |
4209 | DBG("Thread exited with error"); | |
4210 | } | |
b8aa1682 | 4211 | DBG("Worker thread cleanup complete"); |
9b5e0863 | 4212 | error_testpoint: |
f385ae0a MD |
4213 | if (err) { |
4214 | health_error(); | |
4215 | ERR("Health error occurred in %s", __func__); | |
4216 | } | |
4217 | health_unregister(health_relayd); | |
9d1bbf21 | 4218 | rcu_unregister_thread(); |
b4aacfdc | 4219 | lttng_relay_stop_threads(); |
b8aa1682 JD |
4220 | return NULL; |
4221 | } | |
4222 | ||
4223 | /* | |
4224 | * Create the relay command pipe to wake thread_manage_apps. | |
4225 | * Closed in cleanup(). | |
4226 | */ | |
58eb9381 | 4227 | static int create_relay_conn_pipe(void) |
b8aa1682 | 4228 | { |
726b2396 JG |
4229 | return fd_tracker_util_pipe_open_cloexec(the_fd_tracker, |
4230 | "Relayd connection pipe", relay_conn_pipe); | |
b8aa1682 JD |
4231 | } |
4232 | ||
9c256b01 JG |
4233 | static int stdio_open(void *data, int *fds) |
4234 | { | |
4235 | fds[0] = fileno(stdout); | |
4236 | fds[1] = fileno(stderr); | |
4237 | return 0; | |
4238 | } | |
4239 | ||
9c256b01 JG |
4240 | static int track_stdio(void) |
4241 | { | |
4242 | int fds[2]; | |
4243 | const char *names[] = { "stdout", "stderr" }; | |
4244 | ||
4245 | return fd_tracker_open_unsuspendable_fd(the_fd_tracker, fds, | |
4246 | names, 2, stdio_open, NULL); | |
4247 | } | |
4248 | ||
b8aa1682 JD |
4249 | /* |
4250 | * main | |
4251 | */ | |
4252 | int main(int argc, char **argv) | |
4253 | { | |
00e3b7f1 | 4254 | bool thread_is_rcu_registered = false; |
178a0557 | 4255 | int ret = 0, retval = 0; |
b8aa1682 | 4256 | void *status; |
f7c3ffd7 | 4257 | char *unlinked_file_directory_path = NULL, *output_path = NULL; |
b8aa1682 | 4258 | |
2a10de3b JR |
4259 | /* Parse environment variables */ |
4260 | ret = parse_env_options(); | |
4261 | if (ret) { | |
4262 | retval = -1; | |
4263 | goto exit_options; | |
4264 | } | |
4265 | ||
4266 | /* | |
4267 | * Parse arguments. | |
4268 | * Command line arguments overwrite environment. | |
4269 | */ | |
b8aa1682 | 4270 | progname = argv[0]; |
178a0557 MD |
4271 | if (set_options(argc, argv)) { |
4272 | retval = -1; | |
4273 | goto exit_options; | |
b8aa1682 JD |
4274 | } |
4275 | ||
178a0557 MD |
4276 | if (set_signal_handler()) { |
4277 | retval = -1; | |
4278 | goto exit_options; | |
b8aa1682 JD |
4279 | } |
4280 | ||
a3bc3918 JR |
4281 | relayd_config_log(); |
4282 | ||
4283 | if (opt_print_version) { | |
4284 | print_version(); | |
4285 | retval = 0; | |
4286 | goto exit_options; | |
4287 | } | |
4288 | ||
c0407718 JG |
4289 | ret = fclose(stdin); |
4290 | if (ret) { | |
4291 | PERROR("Failed to close stdin"); | |
4292 | goto exit_options; | |
4293 | } | |
4294 | ||
35ab25e5 MD |
4295 | DBG("Clear command %s", opt_allow_clear ? "allowed" : "disallowed"); |
4296 | ||
4d513a50 DG |
4297 | /* Try to create directory if -o, --output is specified. */ |
4298 | if (opt_output_path) { | |
994fa64f DG |
4299 | if (*opt_output_path != '/') { |
4300 | ERR("Please specify an absolute path for -o, --output PATH"); | |
178a0557 MD |
4301 | retval = -1; |
4302 | goto exit_options; | |
994fa64f DG |
4303 | } |
4304 | ||
d77dded2 JG |
4305 | ret = utils_mkdir_recursive(opt_output_path, S_IRWXU | S_IRWXG, |
4306 | -1, -1); | |
4d513a50 DG |
4307 | if (ret < 0) { |
4308 | ERR("Unable to create %s", opt_output_path); | |
178a0557 MD |
4309 | retval = -1; |
4310 | goto exit_options; | |
4d513a50 DG |
4311 | } |
4312 | } | |
4313 | ||
b8aa1682 | 4314 | /* Daemonize */ |
b5218ffb | 4315 | if (opt_daemon || opt_background) { |
3fd27398 MD |
4316 | ret = lttng_daemonize(&child_ppid, &recv_child_signal, |
4317 | !opt_background); | |
b8aa1682 | 4318 | if (ret < 0) { |
178a0557 MD |
4319 | retval = -1; |
4320 | goto exit_options; | |
b8aa1682 | 4321 | } |
3fd27398 MD |
4322 | } |
4323 | ||
ce9ee1fb JR |
4324 | if (opt_working_directory) { |
4325 | ret = utils_change_working_directory(opt_working_directory); | |
4326 | if (ret) { | |
4327 | /* All errors are already logged. */ | |
4328 | goto exit_options; | |
4329 | } | |
4330 | } | |
4331 | ||
23c8ff50 JG |
4332 | sessiond_trace_chunk_registry = sessiond_trace_chunk_registry_create(); |
4333 | if (!sessiond_trace_chunk_registry) { | |
4334 | ERR("Failed to initialize session daemon trace chunk registry"); | |
4335 | retval = -1; | |
794e2e5f | 4336 | goto exit_options; |
23c8ff50 JG |
4337 | } |
4338 | ||
00e3b7f1 JG |
4339 | /* |
4340 | * The RCU thread registration (and use, through the fd-tracker's | |
4341 | * creation) is done after the daemonization to allow us to not | |
4342 | * deal with liburcu's fork() management as the call RCU needs to | |
4343 | * be restored. | |
4344 | */ | |
4345 | rcu_register_thread(); | |
4346 | thread_is_rcu_registered = true; | |
4347 | ||
f7c3ffd7 JG |
4348 | output_path = create_output_path(""); |
4349 | if (!output_path) { | |
4350 | ERR("Failed to get output path"); | |
4351 | retval = -1; | |
4352 | goto exit_options; | |
4353 | } | |
4354 | ret = asprintf(&unlinked_file_directory_path, "%s/%s", output_path, | |
4355 | DEFAULT_UNLINKED_FILES_DIRECTORY); | |
4356 | free(output_path); | |
4357 | if (ret < 0) { | |
4358 | ERR("Failed to format unlinked file directory path"); | |
4359 | retval = -1; | |
4360 | goto exit_options; | |
4361 | } | |
4362 | the_fd_tracker = fd_tracker_create( | |
4363 | unlinked_file_directory_path, lttng_opt_fd_pool_size); | |
4364 | free(unlinked_file_directory_path); | |
00e3b7f1 JG |
4365 | if (!the_fd_tracker) { |
4366 | retval = -1; | |
4367 | goto exit_options; | |
4368 | } | |
4369 | ||
9c256b01 JG |
4370 | ret = track_stdio(); |
4371 | if (ret) { | |
4372 | retval = -1; | |
4373 | goto exit_options; | |
4374 | } | |
4375 | ||
178a0557 MD |
4376 | /* Initialize thread health monitoring */ |
4377 | health_relayd = health_app_create(NR_HEALTH_RELAYD_TYPES); | |
4378 | if (!health_relayd) { | |
4379 | PERROR("health_app_create error"); | |
4380 | retval = -1; | |
794e2e5f | 4381 | goto exit_options; |
178a0557 MD |
4382 | } |
4383 | ||
3fd27398 | 4384 | /* Create thread quit pipe */ |
178a0557 MD |
4385 | if (init_thread_quit_pipe()) { |
4386 | retval = -1; | |
794e2e5f | 4387 | goto exit_options; |
b8aa1682 JD |
4388 | } |
4389 | ||
b8aa1682 | 4390 | /* Setup the thread apps communication pipe. */ |
178a0557 MD |
4391 | if (create_relay_conn_pipe()) { |
4392 | retval = -1; | |
794e2e5f | 4393 | goto exit_options; |
b8aa1682 JD |
4394 | } |
4395 | ||
4396 | /* Init relay command queue. */ | |
8bdee6e2 | 4397 | cds_wfcq_init(&relay_conn_queue.head, &relay_conn_queue.tail); |
b8aa1682 | 4398 | |
554831e7 MD |
4399 | /* Initialize communication library */ |
4400 | lttcomm_init(); | |
87e45c13 | 4401 | lttcomm_inet_init(); |
554831e7 | 4402 | |
d3e2ba59 | 4403 | /* tables of sessions indexed by session ID */ |
7591bab1 MD |
4404 | sessions_ht = lttng_ht_new(0, LTTNG_HT_TYPE_U64); |
4405 | if (!sessions_ht) { | |
178a0557 | 4406 | retval = -1; |
794e2e5f | 4407 | goto exit_options; |
d3e2ba59 JD |
4408 | } |
4409 | ||
4410 | /* tables of streams indexed by stream ID */ | |
2a174661 | 4411 | relay_streams_ht = lttng_ht_new(0, LTTNG_HT_TYPE_U64); |
d3e2ba59 | 4412 | if (!relay_streams_ht) { |
178a0557 | 4413 | retval = -1; |
794e2e5f | 4414 | goto exit_options; |
d3e2ba59 JD |
4415 | } |
4416 | ||
4417 | /* tables of streams indexed by stream ID */ | |
92c6ca54 DG |
4418 | viewer_streams_ht = lttng_ht_new(0, LTTNG_HT_TYPE_U64); |
4419 | if (!viewer_streams_ht) { | |
178a0557 | 4420 | retval = -1; |
794e2e5f | 4421 | goto exit_options; |
55706a7d MD |
4422 | } |
4423 | ||
bcee2b96 | 4424 | ret = init_health_quit_pipe(); |
178a0557 MD |
4425 | if (ret) { |
4426 | retval = -1; | |
794e2e5f | 4427 | goto exit_options; |
65931c8b MD |
4428 | } |
4429 | ||
4430 | /* Create thread to manage the client socket */ | |
1a1a34b4 | 4431 | ret = pthread_create(&health_thread, default_pthread_attr(), |
65931c8b | 4432 | thread_manage_health, (void *) NULL); |
178a0557 MD |
4433 | if (ret) { |
4434 | errno = ret; | |
65931c8b | 4435 | PERROR("pthread_create health"); |
178a0557 | 4436 | retval = -1; |
794e2e5f | 4437 | goto exit_options; |
65931c8b MD |
4438 | } |
4439 | ||
b8aa1682 | 4440 | /* Setup the dispatcher thread */ |
1a1a34b4 | 4441 | ret = pthread_create(&dispatcher_thread, default_pthread_attr(), |
b8aa1682 | 4442 | relay_thread_dispatcher, (void *) NULL); |
178a0557 MD |
4443 | if (ret) { |
4444 | errno = ret; | |
b8aa1682 | 4445 | PERROR("pthread_create dispatcher"); |
178a0557 MD |
4446 | retval = -1; |
4447 | goto exit_dispatcher_thread; | |
b8aa1682 JD |
4448 | } |
4449 | ||
4450 | /* Setup the worker thread */ | |
1a1a34b4 | 4451 | ret = pthread_create(&worker_thread, default_pthread_attr(), |
7591bab1 | 4452 | relay_thread_worker, NULL); |
178a0557 MD |
4453 | if (ret) { |
4454 | errno = ret; | |
b8aa1682 | 4455 | PERROR("pthread_create worker"); |
178a0557 MD |
4456 | retval = -1; |
4457 | goto exit_worker_thread; | |
b8aa1682 JD |
4458 | } |
4459 | ||
4460 | /* Setup the listener thread */ | |
1a1a34b4 | 4461 | ret = pthread_create(&listener_thread, default_pthread_attr(), |
b8aa1682 | 4462 | relay_thread_listener, (void *) NULL); |
178a0557 MD |
4463 | if (ret) { |
4464 | errno = ret; | |
b8aa1682 | 4465 | PERROR("pthread_create listener"); |
178a0557 MD |
4466 | retval = -1; |
4467 | goto exit_listener_thread; | |
b8aa1682 JD |
4468 | } |
4469 | ||
7591bab1 | 4470 | ret = relayd_live_create(live_uri); |
178a0557 | 4471 | if (ret) { |
d3e2ba59 | 4472 | ERR("Starting live viewer threads"); |
178a0557 | 4473 | retval = -1; |
50138f51 | 4474 | goto exit_live; |
d3e2ba59 JD |
4475 | } |
4476 | ||
178a0557 MD |
4477 | /* |
4478 | * This is where we start awaiting program completion (e.g. through | |
4479 | * signal that asks threads to teardown). | |
4480 | */ | |
4481 | ||
4482 | ret = relayd_live_join(); | |
4483 | if (ret) { | |
4484 | retval = -1; | |
4485 | } | |
50138f51 | 4486 | exit_live: |
178a0557 | 4487 | |
b8aa1682 | 4488 | ret = pthread_join(listener_thread, &status); |
178a0557 MD |
4489 | if (ret) { |
4490 | errno = ret; | |
4491 | PERROR("pthread_join listener_thread"); | |
4492 | retval = -1; | |
b8aa1682 JD |
4493 | } |
4494 | ||
178a0557 | 4495 | exit_listener_thread: |
b8aa1682 | 4496 | ret = pthread_join(worker_thread, &status); |
178a0557 MD |
4497 | if (ret) { |
4498 | errno = ret; | |
4499 | PERROR("pthread_join worker_thread"); | |
4500 | retval = -1; | |
b8aa1682 JD |
4501 | } |
4502 | ||
178a0557 | 4503 | exit_worker_thread: |
b8aa1682 | 4504 | ret = pthread_join(dispatcher_thread, &status); |
178a0557 MD |
4505 | if (ret) { |
4506 | errno = ret; | |
4507 | PERROR("pthread_join dispatcher_thread"); | |
4508 | retval = -1; | |
b8aa1682 | 4509 | } |
178a0557 | 4510 | exit_dispatcher_thread: |
42415026 | 4511 | |
65931c8b | 4512 | ret = pthread_join(health_thread, &status); |
178a0557 MD |
4513 | if (ret) { |
4514 | errno = ret; | |
4515 | PERROR("pthread_join health_thread"); | |
4516 | retval = -1; | |
65931c8b | 4517 | } |
178a0557 | 4518 | exit_options: |
4d62fbf8 MD |
4519 | /* |
4520 | * Wait for all pending call_rcu work to complete before tearing | |
4521 | * down data structures. call_rcu worker may be trying to | |
4522 | * perform lookups in those structures. | |
4523 | */ | |
4524 | rcu_barrier(); | |
7591bab1 MD |
4525 | relayd_cleanup(); |
4526 | ||
4527 | /* Ensure all prior call_rcu are done. */ | |
4528 | rcu_barrier(); | |
d3e2ba59 | 4529 | |
00e3b7f1 JG |
4530 | if (thread_is_rcu_registered) { |
4531 | rcu_unregister_thread(); | |
4532 | } | |
4533 | ||
178a0557 | 4534 | if (!retval) { |
b8aa1682 | 4535 | exit(EXIT_SUCCESS); |
178a0557 MD |
4536 | } else { |
4537 | exit(EXIT_FAILURE); | |
b8aa1682 | 4538 | } |
b8aa1682 | 4539 | } |