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