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