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> | |
72489970 | 37 | #include <sys/resource.h> |
173af62f | 38 | #include <inttypes.h> |
b8aa1682 JD |
39 | #include <urcu/futex.h> |
40 | #include <urcu/uatomic.h> | |
41 | #include <unistd.h> | |
42 | #include <fcntl.h> | |
72489970 | 43 | #include <ctype.h> |
b8aa1682 JD |
44 | |
45 | #include <lttng/lttng.h> | |
46 | #include <common/common.h> | |
47 | #include <common/compat/poll.h> | |
48 | #include <common/compat/socket.h> | |
f263b7fd | 49 | #include <common/compat/endian.h> |
e8fa9fb0 | 50 | #include <common/compat/getenv.h> |
b8aa1682 | 51 | #include <common/defaults.h> |
3fd27398 | 52 | #include <common/daemonize.h> |
b8aa1682 JD |
53 | #include <common/futex.h> |
54 | #include <common/sessiond-comm/sessiond-comm.h> | |
55 | #include <common/sessiond-comm/inet.h> | |
b8aa1682 JD |
56 | #include <common/sessiond-comm/relayd.h> |
57 | #include <common/uri.h> | |
a02de639 | 58 | #include <common/utils.h> |
f40ef1d5 | 59 | #include <common/config/session-config.h> |
a8cad461 JG |
60 | #include <common/dynamic-buffer.h> |
61 | #include <common/buffer-view.h> | |
7591bab1 | 62 | #include <urcu/rculist.h> |
b8aa1682 | 63 | |
cdd566a2 | 64 | #include "version.h" |
0f907de1 | 65 | #include "cmd.h" |
d3e2ba59 | 66 | #include "ctf-trace.h" |
1c20f0e2 | 67 | #include "index.h" |
0f907de1 | 68 | #include "utils.h" |
b8aa1682 | 69 | #include "lttng-relayd.h" |
d3e2ba59 | 70 | #include "live.h" |
55706a7d | 71 | #include "health-relayd.h" |
9b5e0863 | 72 | #include "testpoint.h" |
2f8f53af | 73 | #include "viewer-stream.h" |
2a174661 DG |
74 | #include "session.h" |
75 | #include "stream.h" | |
58eb9381 | 76 | #include "connection.h" |
a44ca2ca | 77 | #include "tracefile-array.h" |
2fc6b1ab | 78 | #include "tcp_keep_alive.h" |
b8aa1682 | 79 | |
a8cad461 JG |
80 | enum relay_connection_status { |
81 | RELAY_CONNECTION_STATUS_OK, | |
82 | /* An error occured while processing an event on the connection. */ | |
83 | RELAY_CONNECTION_STATUS_ERROR, | |
84 | /* Connection closed/shutdown cleanly. */ | |
85 | RELAY_CONNECTION_STATUS_CLOSED, | |
86 | }; | |
87 | ||
b8aa1682 | 88 | /* command line options */ |
a174e6fc | 89 | char *opt_output_path, *opt_working_directory; |
cdd566a2 | 90 | static int opt_daemon, opt_background, opt_print_version; |
4d27e2b4 JR |
91 | int opt_group_output_by_session; |
92 | int opt_group_output_by_host; | |
3fd27398 MD |
93 | |
94 | /* | |
95 | * We need to wait for listener and live listener threads, as well as | |
96 | * health check thread, before being ready to signal readiness. | |
97 | */ | |
98 | #define NR_LTTNG_RELAY_READY 3 | |
99 | static int lttng_relay_ready = NR_LTTNG_RELAY_READY; | |
0848dba7 MD |
100 | |
101 | /* Size of receive buffer. */ | |
102 | #define RECV_DATA_BUFFER_SIZE 65536 | |
103 | ||
3fd27398 MD |
104 | static int recv_child_signal; /* Set to 1 when a SIGUSR1 signal is received. */ |
105 | static pid_t child_ppid; /* Internal parent PID use with daemonize. */ | |
106 | ||
095a4ae5 MD |
107 | static struct lttng_uri *control_uri; |
108 | static struct lttng_uri *data_uri; | |
d3e2ba59 | 109 | static struct lttng_uri *live_uri; |
b8aa1682 JD |
110 | |
111 | const char *progname; | |
b8aa1682 | 112 | |
65931c8b | 113 | const char *tracing_group_name = DEFAULT_TRACING_GROUP; |
cd60b05a JG |
114 | static int tracing_group_name_override; |
115 | ||
116 | const char * const config_section_name = "relayd"; | |
65931c8b | 117 | |
b8aa1682 JD |
118 | /* |
119 | * Quit pipe for all threads. This permits a single cancellation point | |
120 | * for all threads when receiving an event on the pipe. | |
121 | */ | |
0b242f62 | 122 | int thread_quit_pipe[2] = { -1, -1 }; |
b8aa1682 JD |
123 | |
124 | /* | |
125 | * This pipe is used to inform the worker thread that a command is queued and | |
126 | * ready to be processed. | |
127 | */ | |
58eb9381 | 128 | static int relay_conn_pipe[2] = { -1, -1 }; |
b8aa1682 | 129 | |
26c9d55e | 130 | /* Shared between threads */ |
b8aa1682 JD |
131 | static int dispatch_thread_exit; |
132 | ||
133 | static pthread_t listener_thread; | |
134 | static pthread_t dispatcher_thread; | |
135 | static pthread_t worker_thread; | |
65931c8b | 136 | static pthread_t health_thread; |
b8aa1682 | 137 | |
7591bab1 MD |
138 | /* |
139 | * last_relay_stream_id_lock protects last_relay_stream_id increment | |
140 | * atomicity on 32-bit architectures. | |
141 | */ | |
142 | static pthread_mutex_t last_relay_stream_id_lock = PTHREAD_MUTEX_INITIALIZER; | |
095a4ae5 | 143 | static uint64_t last_relay_stream_id; |
b8aa1682 JD |
144 | |
145 | /* | |
146 | * Relay command queue. | |
147 | * | |
148 | * The relay_thread_listener and relay_thread_dispatcher communicate with this | |
149 | * queue. | |
150 | */ | |
58eb9381 | 151 | static struct relay_conn_queue relay_conn_queue; |
b8aa1682 | 152 | |
72489970 JG |
153 | /* Cap of file desriptors to be in simultaneous use by the relay daemon. */ |
154 | static unsigned int lttng_opt_fd_cap; | |
155 | ||
d3e2ba59 JD |
156 | /* Global relay stream hash table. */ |
157 | struct lttng_ht *relay_streams_ht; | |
158 | ||
92c6ca54 DG |
159 | /* Global relay viewer stream hash table. */ |
160 | struct lttng_ht *viewer_streams_ht; | |
161 | ||
7591bab1 MD |
162 | /* Global relay sessions hash table. */ |
163 | struct lttng_ht *sessions_ht; | |
0a6518b0 | 164 | |
55706a7d | 165 | /* Relayd health monitoring */ |
eea7556c | 166 | struct health_app *health_relayd; |
55706a7d | 167 | |
bf5e21fd JG |
168 | /* Global fd tracker. */ |
169 | struct fd_tracker *the_fd_tracker; | |
170 | ||
cd60b05a JG |
171 | static struct option long_options[] = { |
172 | { "control-port", 1, 0, 'C', }, | |
173 | { "data-port", 1, 0, 'D', }, | |
8d5c808e | 174 | { "live-port", 1, 0, 'L', }, |
cd60b05a | 175 | { "daemonize", 0, 0, 'd', }, |
b5218ffb | 176 | { "background", 0, 0, 'b', }, |
cd60b05a | 177 | { "group", 1, 0, 'g', }, |
72489970 | 178 | { "fd-cap", 1, 0, '\0', }, |
cd60b05a JG |
179 | { "help", 0, 0, 'h', }, |
180 | { "output", 1, 0, 'o', }, | |
181 | { "verbose", 0, 0, 'v', }, | |
182 | { "config", 1, 0, 'f' }, | |
297af7ff | 183 | { "version", 0, 0, 'V' }, |
a174e6fc | 184 | { "working-directory", 1, 0, 'w', }, |
4d27e2b4 JR |
185 | { "group-output-by-session", 0, 0, 's', }, |
186 | { "group-output-by-host", 0, 0, 'p', }, | |
cd60b05a JG |
187 | { NULL, 0, 0, 0, }, |
188 | }; | |
189 | ||
297af7ff | 190 | static const char *config_ignore_options[] = { "help", "config", "version" }; |
cd60b05a | 191 | |
cdd566a2 JR |
192 | static void print_version(void) { |
193 | fprintf(stdout, "%s\n", VERSION); | |
194 | } | |
195 | ||
196 | static void relayd_config_log(void) | |
197 | { | |
198 | DBG("LTTng-relayd " VERSION " - " VERSION_NAME "%s%s", | |
199 | GIT_VERSION[0] == '\0' ? "" : " - " GIT_VERSION, | |
200 | EXTRA_VERSION_NAME[0] == '\0' ? "" : " - " EXTRA_VERSION_NAME); | |
201 | if (EXTRA_VERSION_DESCRIPTION[0] != '\0') { | |
202 | DBG("LTTng-relayd extra version description:\n\t" EXTRA_VERSION_DESCRIPTION "\n"); | |
203 | } | |
b8b6fadb JR |
204 | if (EXTRA_VERSION_PATCHES[0] != '\0') { |
205 | DBG("LTTng-relayd extra patches:\n\t" EXTRA_VERSION_PATCHES "\n"); | |
206 | } | |
cdd566a2 JR |
207 | } |
208 | ||
cd60b05a JG |
209 | /* |
210 | * Take an option from the getopt output and set it in the right variable to be | |
211 | * used later. | |
212 | * | |
213 | * Return 0 on success else a negative value. | |
214 | */ | |
7591bab1 | 215 | static int set_option(int opt, const char *arg, const char *optname) |
b8aa1682 | 216 | { |
cd60b05a JG |
217 | int ret; |
218 | ||
219 | switch (opt) { | |
220 | case 0: | |
72489970 JG |
221 | if (!strcmp(optname, "fd-cap")) { |
222 | unsigned long v; | |
223 | ||
224 | errno = 0; | |
225 | v = strtoul(arg, NULL, 0); | |
226 | if (errno != 0 || !isdigit(arg[0])) { | |
227 | ERR("Wrong value in --fd-cap parameter: %s", arg); | |
228 | ret = -1; | |
229 | goto end; | |
230 | } | |
231 | if (v < DEFAULT_RELAYD_MINIMAL_FD_CAP) { | |
232 | ERR("File descriptor cap must be set to at least %d", | |
233 | DEFAULT_RELAYD_MINIMAL_FD_CAP); | |
234 | } | |
235 | if (v >= UINT_MAX) { | |
236 | ERR("File descriptor cap overflow in --fd-cap parameter: %s", arg); | |
237 | ret = -1; | |
238 | goto end; | |
239 | } | |
240 | lttng_opt_fd_cap = (unsigned int) v; | |
241 | DBG3("File descriptor cap set to %u", lttng_opt_fd_cap); | |
242 | ||
243 | } else { | |
244 | fprintf(stderr, "unknown option %s", optname); | |
245 | if (arg) { | |
246 | fprintf(stderr, " with arg %s\n", arg); | |
247 | } | |
cd60b05a JG |
248 | } |
249 | break; | |
250 | case 'C': | |
e8fa9fb0 MD |
251 | if (lttng_is_setuid_setgid()) { |
252 | WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.", | |
253 | "-C, --control-port"); | |
254 | } else { | |
255 | ret = uri_parse(arg, &control_uri); | |
256 | if (ret < 0) { | |
257 | ERR("Invalid control URI specified"); | |
258 | goto end; | |
259 | } | |
260 | if (control_uri->port == 0) { | |
261 | control_uri->port = DEFAULT_NETWORK_CONTROL_PORT; | |
262 | } | |
cd60b05a JG |
263 | } |
264 | break; | |
265 | case 'D': | |
e8fa9fb0 MD |
266 | if (lttng_is_setuid_setgid()) { |
267 | WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.", | |
268 | "-D, -data-port"); | |
269 | } else { | |
270 | ret = uri_parse(arg, &data_uri); | |
271 | if (ret < 0) { | |
272 | ERR("Invalid data URI specified"); | |
273 | goto end; | |
274 | } | |
275 | if (data_uri->port == 0) { | |
276 | data_uri->port = DEFAULT_NETWORK_DATA_PORT; | |
277 | } | |
cd60b05a JG |
278 | } |
279 | break; | |
8d5c808e | 280 | case 'L': |
e8fa9fb0 MD |
281 | if (lttng_is_setuid_setgid()) { |
282 | WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.", | |
283 | "-L, -live-port"); | |
284 | } else { | |
285 | ret = uri_parse(arg, &live_uri); | |
286 | if (ret < 0) { | |
287 | ERR("Invalid live URI specified"); | |
288 | goto end; | |
289 | } | |
290 | if (live_uri->port == 0) { | |
291 | live_uri->port = DEFAULT_NETWORK_VIEWER_PORT; | |
292 | } | |
8d5c808e AM |
293 | } |
294 | break; | |
cd60b05a JG |
295 | case 'd': |
296 | opt_daemon = 1; | |
297 | break; | |
b5218ffb MD |
298 | case 'b': |
299 | opt_background = 1; | |
300 | break; | |
cd60b05a | 301 | case 'g': |
e8fa9fb0 MD |
302 | if (lttng_is_setuid_setgid()) { |
303 | WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.", | |
304 | "-g, --group"); | |
305 | } else { | |
306 | tracing_group_name = strdup(arg); | |
307 | if (tracing_group_name == NULL) { | |
308 | ret = -errno; | |
309 | PERROR("strdup"); | |
310 | goto end; | |
311 | } | |
312 | tracing_group_name_override = 1; | |
330a40bb | 313 | } |
cd60b05a JG |
314 | break; |
315 | case 'h': | |
655b5cc1 PP |
316 | ret = utils_show_man_page(8, "lttng-relayd"); |
317 | if (ret) { | |
318 | ERR("Cannot view man page lttng-relayd(8)"); | |
319 | perror("exec"); | |
320 | } | |
cd60b05a | 321 | exit(EXIT_FAILURE); |
297af7ff | 322 | case 'V': |
cdd566a2 JR |
323 | opt_print_version = 1; |
324 | break; | |
cd60b05a | 325 | case 'o': |
e8fa9fb0 MD |
326 | if (lttng_is_setuid_setgid()) { |
327 | WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.", | |
328 | "-o, --output"); | |
329 | } else { | |
330 | ret = asprintf(&opt_output_path, "%s", arg); | |
331 | if (ret < 0) { | |
332 | ret = -errno; | |
333 | PERROR("asprintf opt_output_path"); | |
334 | goto end; | |
335 | } | |
cd60b05a JG |
336 | } |
337 | break; | |
a174e6fc JR |
338 | case 'w': |
339 | if (lttng_is_setuid_setgid()) { | |
340 | WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.", | |
341 | "-w, --working-directory"); | |
342 | } else { | |
343 | ret = asprintf(&opt_working_directory, "%s", arg); | |
344 | if (ret < 0) { | |
345 | ret = -errno; | |
346 | PERROR("asprintf working_directory"); | |
347 | goto end; | |
348 | } | |
349 | } | |
350 | break; | |
351 | ||
cd60b05a JG |
352 | case 'v': |
353 | /* Verbose level can increase using multiple -v */ | |
354 | if (arg) { | |
355 | lttng_opt_verbose = config_parse_value(arg); | |
356 | } else { | |
849e5b7b DG |
357 | /* Only 3 level of verbosity (-vvv). */ |
358 | if (lttng_opt_verbose < 3) { | |
359 | lttng_opt_verbose += 1; | |
360 | } | |
cd60b05a JG |
361 | } |
362 | break; | |
4d27e2b4 JR |
363 | case 's': |
364 | if (opt_group_output_by_host) { | |
365 | ERR("Cannot set --group-output-by-session, --group-output-by-host already defined"); | |
366 | exit(EXIT_FAILURE); | |
367 | } | |
368 | opt_group_output_by_session = 1; | |
369 | break; | |
370 | case 'p': | |
371 | if (opt_group_output_by_session) { | |
372 | ERR("Cannot set --group-output-by-host, --group-output-by-session already defined"); | |
373 | exit(EXIT_FAILURE); | |
374 | } | |
375 | opt_group_output_by_host = 1; | |
376 | break; | |
cd60b05a JG |
377 | default: |
378 | /* Unknown option or other error. | |
379 | * Error is printed by getopt, just return */ | |
380 | ret = -1; | |
381 | goto end; | |
382 | } | |
383 | ||
384 | /* All good. */ | |
385 | ret = 0; | |
386 | ||
387 | end: | |
388 | return ret; | |
389 | } | |
390 | ||
391 | /* | |
392 | * config_entry_handler_cb used to handle options read from a config file. | |
f40ef1d5 | 393 | * See config_entry_handler_cb comment in common/config/session-config.h for the |
cd60b05a JG |
394 | * return value conventions. |
395 | */ | |
7591bab1 | 396 | static int config_entry_handler(const struct config_entry *entry, void *unused) |
cd60b05a JG |
397 | { |
398 | int ret = 0, i; | |
399 | ||
400 | if (!entry || !entry->name || !entry->value) { | |
401 | ret = -EINVAL; | |
402 | goto end; | |
403 | } | |
404 | ||
405 | /* Check if the option is to be ignored */ | |
406 | for (i = 0; i < sizeof(config_ignore_options) / sizeof(char *); i++) { | |
407 | if (!strcmp(entry->name, config_ignore_options[i])) { | |
408 | goto end; | |
409 | } | |
410 | } | |
411 | ||
412 | for (i = 0; i < (sizeof(long_options) / sizeof(struct option)) - 1; i++) { | |
413 | /* Ignore if entry name is not fully matched. */ | |
414 | if (strcmp(entry->name, long_options[i].name)) { | |
415 | continue; | |
416 | } | |
417 | ||
418 | /* | |
7591bab1 MD |
419 | * If the option takes no argument on the command line, |
420 | * we have to check if the value is "true". We support | |
421 | * non-zero numeric values, true, on and yes. | |
cd60b05a JG |
422 | */ |
423 | if (!long_options[i].has_arg) { | |
424 | ret = config_parse_value(entry->value); | |
425 | if (ret <= 0) { | |
426 | if (ret) { | |
427 | WARN("Invalid configuration value \"%s\" for option %s", | |
428 | entry->value, entry->name); | |
429 | } | |
430 | /* False, skip boolean config option. */ | |
431 | goto end; | |
432 | } | |
433 | } | |
434 | ||
435 | ret = set_option(long_options[i].val, entry->value, entry->name); | |
436 | goto end; | |
437 | } | |
438 | ||
439 | WARN("Unrecognized option \"%s\" in daemon configuration file.", | |
440 | entry->name); | |
441 | ||
442 | end: | |
443 | return ret; | |
444 | } | |
445 | ||
b967f1cc JR |
446 | static void parse_env_options(void) |
447 | { | |
448 | char *value = NULL; | |
449 | ||
450 | value = lttng_secure_getenv(DEFAULT_LTTNG_RELAYD_WORKING_DIRECTORY_ENV); | |
451 | if (value) { | |
452 | opt_working_directory = value; | |
453 | } | |
454 | } | |
455 | ||
7591bab1 | 456 | static int set_options(int argc, char **argv) |
cd60b05a | 457 | { |
178a0557 | 458 | int c, ret = 0, option_index = 0, retval = 0; |
cd60b05a JG |
459 | int orig_optopt = optopt, orig_optind = optind; |
460 | char *default_address, *optstring; | |
461 | const char *config_path = NULL; | |
462 | ||
463 | optstring = utils_generate_optstring(long_options, | |
464 | sizeof(long_options) / sizeof(struct option)); | |
465 | if (!optstring) { | |
178a0557 | 466 | retval = -ENOMEM; |
cd60b05a JG |
467 | goto exit; |
468 | } | |
469 | ||
470 | /* Check for the --config option */ | |
471 | ||
472 | while ((c = getopt_long(argc, argv, optstring, long_options, | |
473 | &option_index)) != -1) { | |
474 | if (c == '?') { | |
178a0557 | 475 | retval = -EINVAL; |
cd60b05a JG |
476 | goto exit; |
477 | } else if (c != 'f') { | |
478 | continue; | |
479 | } | |
480 | ||
e8fa9fb0 MD |
481 | if (lttng_is_setuid_setgid()) { |
482 | WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.", | |
483 | "-f, --config"); | |
484 | } else { | |
485 | config_path = utils_expand_path(optarg); | |
486 | if (!config_path) { | |
487 | ERR("Failed to resolve path: %s", optarg); | |
488 | } | |
cd60b05a JG |
489 | } |
490 | } | |
491 | ||
492 | ret = config_get_section_entries(config_path, config_section_name, | |
493 | config_entry_handler, NULL); | |
494 | if (ret) { | |
495 | if (ret > 0) { | |
496 | ERR("Invalid configuration option at line %i", ret); | |
cd60b05a | 497 | } |
178a0557 | 498 | retval = -1; |
cd60b05a JG |
499 | goto exit; |
500 | } | |
b8aa1682 | 501 | |
cd60b05a JG |
502 | /* Reset getopt's global state */ |
503 | optopt = orig_optopt; | |
504 | optind = orig_optind; | |
b8aa1682 | 505 | while (1) { |
cd60b05a | 506 | c = getopt_long(argc, argv, optstring, long_options, &option_index); |
b8aa1682 JD |
507 | if (c == -1) { |
508 | break; | |
509 | } | |
510 | ||
cd60b05a JG |
511 | ret = set_option(c, optarg, long_options[option_index].name); |
512 | if (ret < 0) { | |
178a0557 | 513 | retval = -1; |
b8aa1682 JD |
514 | goto exit; |
515 | } | |
516 | } | |
517 | ||
518 | /* assign default values */ | |
519 | if (control_uri == NULL) { | |
fa91dc52 MD |
520 | ret = asprintf(&default_address, |
521 | "tcp://" DEFAULT_NETWORK_CONTROL_BIND_ADDRESS ":%d", | |
522 | DEFAULT_NETWORK_CONTROL_PORT); | |
b8aa1682 JD |
523 | if (ret < 0) { |
524 | PERROR("asprintf default data address"); | |
178a0557 | 525 | retval = -1; |
b8aa1682 JD |
526 | goto exit; |
527 | } | |
528 | ||
529 | ret = uri_parse(default_address, &control_uri); | |
530 | free(default_address); | |
531 | if (ret < 0) { | |
532 | ERR("Invalid control URI specified"); | |
178a0557 | 533 | retval = -1; |
b8aa1682 JD |
534 | goto exit; |
535 | } | |
536 | } | |
537 | if (data_uri == NULL) { | |
fa91dc52 MD |
538 | ret = asprintf(&default_address, |
539 | "tcp://" DEFAULT_NETWORK_DATA_BIND_ADDRESS ":%d", | |
540 | DEFAULT_NETWORK_DATA_PORT); | |
b8aa1682 JD |
541 | if (ret < 0) { |
542 | PERROR("asprintf default data address"); | |
178a0557 | 543 | retval = -1; |
b8aa1682 JD |
544 | goto exit; |
545 | } | |
546 | ||
547 | ret = uri_parse(default_address, &data_uri); | |
548 | free(default_address); | |
549 | if (ret < 0) { | |
550 | ERR("Invalid data URI specified"); | |
178a0557 | 551 | retval = -1; |
b8aa1682 JD |
552 | goto exit; |
553 | } | |
554 | } | |
d3e2ba59 | 555 | if (live_uri == NULL) { |
fa91dc52 MD |
556 | ret = asprintf(&default_address, |
557 | "tcp://" DEFAULT_NETWORK_VIEWER_BIND_ADDRESS ":%d", | |
558 | DEFAULT_NETWORK_VIEWER_PORT); | |
d3e2ba59 JD |
559 | if (ret < 0) { |
560 | PERROR("asprintf default viewer control address"); | |
178a0557 | 561 | retval = -1; |
d3e2ba59 JD |
562 | goto exit; |
563 | } | |
564 | ||
565 | ret = uri_parse(default_address, &live_uri); | |
566 | free(default_address); | |
567 | if (ret < 0) { | |
568 | ERR("Invalid viewer control URI specified"); | |
178a0557 | 569 | retval = -1; |
d3e2ba59 JD |
570 | goto exit; |
571 | } | |
572 | } | |
72489970 JG |
573 | if (lttng_opt_fd_cap == 0) { |
574 | int ret; | |
575 | struct rlimit rlimit; | |
576 | ||
577 | ret = getrlimit(RLIMIT_NOFILE, &rlimit); | |
578 | if (ret) { | |
579 | PERROR("Failed to get file descriptor limit"); | |
580 | retval = -1; | |
581 | } | |
582 | ||
583 | lttng_opt_fd_cap = rlimit.rlim_cur; | |
584 | } | |
b8aa1682 | 585 | |
4d27e2b4 JR |
586 | if (!opt_group_output_by_session && !opt_group_output_by_host) { |
587 | /* Group by host by default */ | |
588 | opt_group_output_by_host = 1; | |
589 | } | |
590 | ||
b8aa1682 | 591 | exit: |
cd60b05a | 592 | free(optstring); |
178a0557 | 593 | return retval; |
b8aa1682 JD |
594 | } |
595 | ||
7591bab1 MD |
596 | static void print_global_objects(void) |
597 | { | |
7591bab1 MD |
598 | print_viewer_streams(); |
599 | print_relay_streams(); | |
600 | print_sessions(); | |
7591bab1 MD |
601 | } |
602 | ||
b8aa1682 JD |
603 | /* |
604 | * Cleanup the daemon | |
605 | */ | |
7591bab1 | 606 | static void relayd_cleanup(void) |
b8aa1682 | 607 | { |
7591bab1 MD |
608 | print_global_objects(); |
609 | ||
b8aa1682 JD |
610 | DBG("Cleaning up"); |
611 | ||
178a0557 MD |
612 | if (viewer_streams_ht) |
613 | lttng_ht_destroy(viewer_streams_ht); | |
614 | if (relay_streams_ht) | |
615 | lttng_ht_destroy(relay_streams_ht); | |
7591bab1 MD |
616 | if (sessions_ht) |
617 | lttng_ht_destroy(sessions_ht); | |
178a0557 | 618 | |
095a4ae5 MD |
619 | /* free the dynamically allocated opt_output_path */ |
620 | free(opt_output_path); | |
621 | ||
a02de639 | 622 | /* Close thread quit pipes */ |
c3e83e3d | 623 | (void) fd_tracker_util_pipe_close(the_fd_tracker, thread_quit_pipe); |
a02de639 | 624 | |
710c1f73 DG |
625 | uri_free(control_uri); |
626 | uri_free(data_uri); | |
8d5c808e | 627 | /* Live URI is freed in the live thread. */ |
cd60b05a JG |
628 | |
629 | if (tracing_group_name_override) { | |
630 | free((void *) tracing_group_name); | |
631 | } | |
bf5e21fd | 632 | fd_tracker_log(the_fd_tracker); |
b8aa1682 JD |
633 | } |
634 | ||
635 | /* | |
636 | * Write to writable pipe used to notify a thread. | |
637 | */ | |
7591bab1 | 638 | static int notify_thread_pipe(int wpipe) |
b8aa1682 | 639 | { |
6cd525e8 | 640 | ssize_t ret; |
b8aa1682 | 641 | |
6cd525e8 MD |
642 | ret = lttng_write(wpipe, "!", 1); |
643 | if (ret < 1) { | |
b8aa1682 | 644 | PERROR("write poll pipe"); |
b4aacfdc | 645 | goto end; |
b8aa1682 | 646 | } |
b4aacfdc MD |
647 | ret = 0; |
648 | end: | |
b8aa1682 JD |
649 | return ret; |
650 | } | |
651 | ||
7591bab1 | 652 | static int notify_health_quit_pipe(int *pipe) |
65931c8b | 653 | { |
6cd525e8 | 654 | ssize_t ret; |
65931c8b | 655 | |
6cd525e8 MD |
656 | ret = lttng_write(pipe[1], "4", 1); |
657 | if (ret < 1) { | |
65931c8b | 658 | PERROR("write relay health quit"); |
b4aacfdc | 659 | goto end; |
65931c8b | 660 | } |
b4aacfdc MD |
661 | ret = 0; |
662 | end: | |
663 | return ret; | |
65931c8b MD |
664 | } |
665 | ||
b8aa1682 | 666 | /* |
b4aacfdc | 667 | * Stop all relayd and relayd-live threads. |
b8aa1682 | 668 | */ |
b4aacfdc | 669 | int lttng_relay_stop_threads(void) |
b8aa1682 | 670 | { |
b4aacfdc | 671 | int retval = 0; |
b8aa1682 JD |
672 | |
673 | /* Stopping all threads */ | |
674 | DBG("Terminating all threads"); | |
b4aacfdc | 675 | if (notify_thread_pipe(thread_quit_pipe[1])) { |
b8aa1682 | 676 | ERR("write error on thread quit pipe"); |
b4aacfdc | 677 | retval = -1; |
b8aa1682 JD |
678 | } |
679 | ||
b4aacfdc MD |
680 | if (notify_health_quit_pipe(health_quit_pipe)) { |
681 | ERR("write error on health quit pipe"); | |
682 | } | |
65931c8b | 683 | |
b8aa1682 | 684 | /* Dispatch thread */ |
26c9d55e | 685 | CMM_STORE_SHARED(dispatch_thread_exit, 1); |
58eb9381 | 686 | futex_nto1_wake(&relay_conn_queue.futex); |
178a0557 | 687 | |
b4aacfdc | 688 | if (relayd_live_stop()) { |
178a0557 | 689 | ERR("Error stopping live threads"); |
b4aacfdc | 690 | retval = -1; |
178a0557 | 691 | } |
b4aacfdc | 692 | return retval; |
b8aa1682 JD |
693 | } |
694 | ||
695 | /* | |
696 | * Signal handler for the daemon | |
697 | * | |
698 | * Simply stop all worker threads, leaving main() return gracefully after | |
699 | * joining all threads and calling cleanup(). | |
700 | */ | |
7591bab1 | 701 | static void sighandler(int sig) |
b8aa1682 JD |
702 | { |
703 | switch (sig) { | |
b8aa1682 JD |
704 | case SIGINT: |
705 | DBG("SIGINT caught"); | |
b4aacfdc MD |
706 | if (lttng_relay_stop_threads()) { |
707 | ERR("Error stopping threads"); | |
708 | } | |
b8aa1682 JD |
709 | break; |
710 | case SIGTERM: | |
711 | DBG("SIGTERM caught"); | |
b4aacfdc MD |
712 | if (lttng_relay_stop_threads()) { |
713 | ERR("Error stopping threads"); | |
714 | } | |
b8aa1682 | 715 | break; |
3fd27398 MD |
716 | case SIGUSR1: |
717 | CMM_STORE_SHARED(recv_child_signal, 1); | |
718 | break; | |
b8aa1682 JD |
719 | default: |
720 | break; | |
721 | } | |
722 | } | |
723 | ||
724 | /* | |
725 | * Setup signal handler for : | |
726 | * SIGINT, SIGTERM, SIGPIPE | |
727 | */ | |
7591bab1 | 728 | static int set_signal_handler(void) |
b8aa1682 JD |
729 | { |
730 | int ret = 0; | |
731 | struct sigaction sa; | |
732 | sigset_t sigset; | |
733 | ||
734 | if ((ret = sigemptyset(&sigset)) < 0) { | |
735 | PERROR("sigemptyset"); | |
736 | return ret; | |
737 | } | |
738 | ||
b8aa1682 JD |
739 | sa.sa_mask = sigset; |
740 | sa.sa_flags = 0; | |
0072e5e2 MD |
741 | |
742 | sa.sa_handler = sighandler; | |
b8aa1682 JD |
743 | if ((ret = sigaction(SIGTERM, &sa, NULL)) < 0) { |
744 | PERROR("sigaction"); | |
745 | return ret; | |
746 | } | |
747 | ||
748 | if ((ret = sigaction(SIGINT, &sa, NULL)) < 0) { | |
749 | PERROR("sigaction"); | |
750 | return ret; | |
751 | } | |
752 | ||
0072e5e2 | 753 | if ((ret = sigaction(SIGUSR1, &sa, NULL)) < 0) { |
b8aa1682 JD |
754 | PERROR("sigaction"); |
755 | return ret; | |
756 | } | |
757 | ||
0072e5e2 MD |
758 | sa.sa_handler = SIG_IGN; |
759 | if ((ret = sigaction(SIGPIPE, &sa, NULL)) < 0) { | |
3fd27398 MD |
760 | PERROR("sigaction"); |
761 | return ret; | |
762 | } | |
763 | ||
764 | DBG("Signal handler set for SIGTERM, SIGUSR1, SIGPIPE and SIGINT"); | |
b8aa1682 JD |
765 | |
766 | return ret; | |
767 | } | |
768 | ||
3fd27398 MD |
769 | void lttng_relay_notify_ready(void) |
770 | { | |
771 | /* Notify the parent of the fork() process that we are ready. */ | |
772 | if (opt_daemon || opt_background) { | |
773 | if (uatomic_sub_return(<tng_relay_ready, 1) == 0) { | |
774 | kill(child_ppid, SIGUSR1); | |
775 | } | |
776 | } | |
777 | } | |
778 | ||
b8aa1682 JD |
779 | /* |
780 | * Init thread quit pipe. | |
781 | * | |
782 | * Return -1 on error or 0 if all pipes are created. | |
783 | */ | |
7591bab1 | 784 | static int init_thread_quit_pipe(void) |
b8aa1682 | 785 | { |
c3e83e3d JG |
786 | return fd_tracker_util_pipe_open_cloexec(the_fd_tracker, |
787 | "Quit pipe", thread_quit_pipe); | |
b8aa1682 JD |
788 | } |
789 | ||
2b56bd01 JG |
790 | /* |
791 | * Init health quit pipe. | |
792 | * | |
793 | * Return -1 on error or 0 if all pipes are created. | |
794 | */ | |
795 | static int init_health_quit_pipe(void) | |
796 | { | |
797 | return fd_tracker_util_pipe_open_cloexec(the_fd_tracker, | |
798 | "Health quit pipe", health_quit_pipe); | |
799 | } | |
800 | ||
b8aa1682 JD |
801 | /* |
802 | * Create a poll set with O_CLOEXEC and add the thread quit pipe to the set. | |
803 | */ | |
7591bab1 | 804 | static int create_thread_poll_set(struct lttng_poll_event *events, int size) |
b8aa1682 JD |
805 | { |
806 | int ret; | |
807 | ||
808 | if (events == NULL || size == 0) { | |
809 | ret = -1; | |
810 | goto error; | |
811 | } | |
812 | ||
813 | ret = lttng_poll_create(events, size, LTTNG_CLOEXEC); | |
814 | if (ret < 0) { | |
815 | goto error; | |
816 | } | |
817 | ||
818 | /* Add quit pipe */ | |
c7759e6a | 819 | ret = lttng_poll_add(events, thread_quit_pipe[0], LPOLLIN | LPOLLERR); |
b8aa1682 JD |
820 | if (ret < 0) { |
821 | goto error; | |
822 | } | |
823 | ||
824 | return 0; | |
825 | ||
826 | error: | |
827 | return ret; | |
828 | } | |
829 | ||
830 | /* | |
831 | * Check if the thread quit pipe was triggered. | |
832 | * | |
833 | * Return 1 if it was triggered else 0; | |
834 | */ | |
7591bab1 | 835 | static int check_thread_quit_pipe(int fd, uint32_t events) |
b8aa1682 JD |
836 | { |
837 | if (fd == thread_quit_pipe[0] && (events & LPOLLIN)) { | |
838 | return 1; | |
839 | } | |
840 | ||
841 | return 0; | |
842 | } | |
843 | ||
844 | /* | |
845 | * Create and init socket from uri. | |
846 | */ | |
7591bab1 | 847 | static struct lttcomm_sock *relay_socket_create(struct lttng_uri *uri) |
b8aa1682 JD |
848 | { |
849 | int ret; | |
850 | struct lttcomm_sock *sock = NULL; | |
851 | ||
852 | sock = lttcomm_alloc_sock_from_uri(uri); | |
853 | if (sock == NULL) { | |
854 | ERR("Allocating socket"); | |
855 | goto error; | |
856 | } | |
857 | ||
858 | ret = lttcomm_create_sock(sock); | |
859 | if (ret < 0) { | |
860 | goto error; | |
861 | } | |
862 | DBG("Listening on sock %d", sock->fd); | |
863 | ||
864 | ret = sock->ops->bind(sock); | |
865 | if (ret < 0) { | |
866 | goto error; | |
867 | } | |
868 | ||
869 | ret = sock->ops->listen(sock, -1); | |
870 | if (ret < 0) { | |
871 | goto error; | |
872 | ||
873 | } | |
874 | ||
875 | return sock; | |
876 | ||
877 | error: | |
878 | if (sock) { | |
879 | lttcomm_destroy_sock(sock); | |
880 | } | |
881 | return NULL; | |
882 | } | |
883 | ||
884 | /* | |
885 | * This thread manages the listening for new connections on the network | |
886 | */ | |
7591bab1 | 887 | static void *relay_thread_listener(void *data) |
b8aa1682 | 888 | { |
095a4ae5 | 889 | int i, ret, pollfd, err = -1; |
b8aa1682 JD |
890 | uint32_t revents, nb_fd; |
891 | struct lttng_poll_event events; | |
892 | struct lttcomm_sock *control_sock, *data_sock; | |
893 | ||
b8aa1682 JD |
894 | DBG("[thread] Relay listener started"); |
895 | ||
55706a7d MD |
896 | health_register(health_relayd, HEALTH_RELAYD_TYPE_LISTENER); |
897 | ||
f385ae0a MD |
898 | health_code_update(); |
899 | ||
7591bab1 | 900 | control_sock = relay_socket_create(control_uri); |
b8aa1682 | 901 | if (!control_sock) { |
095a4ae5 | 902 | goto error_sock_control; |
b8aa1682 JD |
903 | } |
904 | ||
7591bab1 | 905 | data_sock = relay_socket_create(data_uri); |
b8aa1682 | 906 | if (!data_sock) { |
095a4ae5 | 907 | goto error_sock_relay; |
b8aa1682 JD |
908 | } |
909 | ||
910 | /* | |
7591bab1 MD |
911 | * Pass 3 as size here for the thread quit pipe, control and |
912 | * data socket. | |
b8aa1682 JD |
913 | */ |
914 | ret = create_thread_poll_set(&events, 3); | |
915 | if (ret < 0) { | |
916 | goto error_create_poll; | |
917 | } | |
918 | ||
919 | /* Add the control socket */ | |
920 | ret = lttng_poll_add(&events, control_sock->fd, LPOLLIN | LPOLLRDHUP); | |
921 | if (ret < 0) { | |
922 | goto error_poll_add; | |
923 | } | |
924 | ||
925 | /* Add the data socket */ | |
926 | ret = lttng_poll_add(&events, data_sock->fd, LPOLLIN | LPOLLRDHUP); | |
927 | if (ret < 0) { | |
928 | goto error_poll_add; | |
929 | } | |
930 | ||
3fd27398 MD |
931 | lttng_relay_notify_ready(); |
932 | ||
9b5e0863 MD |
933 | if (testpoint(relayd_thread_listener)) { |
934 | goto error_testpoint; | |
935 | } | |
936 | ||
b8aa1682 | 937 | while (1) { |
f385ae0a MD |
938 | health_code_update(); |
939 | ||
b8aa1682 JD |
940 | DBG("Listener accepting connections"); |
941 | ||
b8aa1682 | 942 | restart: |
f385ae0a | 943 | health_poll_entry(); |
b8aa1682 | 944 | ret = lttng_poll_wait(&events, -1); |
f385ae0a | 945 | health_poll_exit(); |
b8aa1682 JD |
946 | if (ret < 0) { |
947 | /* | |
948 | * Restart interrupted system call. | |
949 | */ | |
950 | if (errno == EINTR) { | |
951 | goto restart; | |
952 | } | |
953 | goto error; | |
954 | } | |
955 | ||
0d9c5d77 DG |
956 | nb_fd = ret; |
957 | ||
b8aa1682 JD |
958 | DBG("Relay new connection received"); |
959 | for (i = 0; i < nb_fd; i++) { | |
f385ae0a MD |
960 | health_code_update(); |
961 | ||
b8aa1682 JD |
962 | /* Fetch once the poll data */ |
963 | revents = LTTNG_POLL_GETEV(&events, i); | |
964 | pollfd = LTTNG_POLL_GETFD(&events, i); | |
965 | ||
fd20dac9 | 966 | if (!revents) { |
7591bab1 MD |
967 | /* |
968 | * No activity for this FD (poll | |
969 | * implementation). | |
970 | */ | |
fd20dac9 MD |
971 | continue; |
972 | } | |
973 | ||
b8aa1682 JD |
974 | /* Thread quit pipe has been closed. Killing thread. */ |
975 | ret = check_thread_quit_pipe(pollfd, revents); | |
976 | if (ret) { | |
095a4ae5 MD |
977 | err = 0; |
978 | goto exit; | |
b8aa1682 JD |
979 | } |
980 | ||
03e43155 | 981 | if (revents & LPOLLIN) { |
4b7f17b2 | 982 | /* |
7591bab1 MD |
983 | * A new connection is requested, therefore a |
984 | * sessiond/consumerd connection is allocated in | |
985 | * this thread, enqueued to a global queue and | |
986 | * dequeued (and freed) in the worker thread. | |
4b7f17b2 | 987 | */ |
58eb9381 DG |
988 | int val = 1; |
989 | struct relay_connection *new_conn; | |
4b7f17b2 | 990 | struct lttcomm_sock *newsock; |
7591bab1 | 991 | enum connection_type type; |
b8aa1682 JD |
992 | |
993 | if (pollfd == data_sock->fd) { | |
7591bab1 | 994 | type = RELAY_DATA; |
b8aa1682 | 995 | newsock = data_sock->ops->accept(data_sock); |
58eb9381 DG |
996 | DBG("Relay data connection accepted, socket %d", |
997 | newsock->fd); | |
4b7f17b2 MD |
998 | } else { |
999 | assert(pollfd == control_sock->fd); | |
7591bab1 | 1000 | type = RELAY_CONTROL; |
b8aa1682 | 1001 | newsock = control_sock->ops->accept(control_sock); |
58eb9381 DG |
1002 | DBG("Relay control connection accepted, socket %d", |
1003 | newsock->fd); | |
b8aa1682 | 1004 | } |
58eb9381 DG |
1005 | if (!newsock) { |
1006 | PERROR("accepting sock"); | |
58eb9381 DG |
1007 | goto error; |
1008 | } | |
1009 | ||
1010 | ret = setsockopt(newsock->fd, SOL_SOCKET, SO_REUSEADDR, &val, | |
1011 | sizeof(val)); | |
b8aa1682 JD |
1012 | if (ret < 0) { |
1013 | PERROR("setsockopt inet"); | |
4b7f17b2 | 1014 | lttcomm_destroy_sock(newsock); |
b8aa1682 JD |
1015 | goto error; |
1016 | } | |
2fc6b1ab JR |
1017 | |
1018 | ret = socket_apply_keep_alive_config(newsock->fd); | |
1019 | if (ret < 0) { | |
1020 | ERR("Failed to apply TCP keep-alive configuration on socket (%i)", | |
1021 | newsock->fd); | |
1022 | lttcomm_destroy_sock(newsock); | |
1023 | goto error; | |
1024 | } | |
1025 | ||
7591bab1 MD |
1026 | new_conn = connection_create(newsock, type); |
1027 | if (!new_conn) { | |
1028 | lttcomm_destroy_sock(newsock); | |
1029 | goto error; | |
1030 | } | |
58eb9381 DG |
1031 | |
1032 | /* Enqueue request for the dispatcher thread. */ | |
8bdee6e2 SM |
1033 | cds_wfcq_enqueue(&relay_conn_queue.head, &relay_conn_queue.tail, |
1034 | &new_conn->qnode); | |
b8aa1682 JD |
1035 | |
1036 | /* | |
7591bab1 MD |
1037 | * Wake the dispatch queue futex. |
1038 | * Implicit memory barrier with the | |
1039 | * exchange in cds_wfcq_enqueue. | |
b8aa1682 | 1040 | */ |
58eb9381 | 1041 | futex_nto1_wake(&relay_conn_queue.futex); |
03e43155 MD |
1042 | } else if (revents & (LPOLLERR | LPOLLHUP | LPOLLRDHUP)) { |
1043 | ERR("socket poll error"); | |
1044 | goto error; | |
1045 | } else { | |
1046 | ERR("Unexpected poll events %u for sock %d", revents, pollfd); | |
1047 | goto error; | |
b8aa1682 JD |
1048 | } |
1049 | } | |
1050 | } | |
1051 | ||
095a4ae5 | 1052 | exit: |
b8aa1682 JD |
1053 | error: |
1054 | error_poll_add: | |
9b5e0863 | 1055 | error_testpoint: |
b8aa1682 JD |
1056 | lttng_poll_clean(&events); |
1057 | error_create_poll: | |
095a4ae5 MD |
1058 | if (data_sock->fd >= 0) { |
1059 | ret = data_sock->ops->close(data_sock); | |
b8aa1682 JD |
1060 | if (ret) { |
1061 | PERROR("close"); | |
1062 | } | |
b8aa1682 | 1063 | } |
095a4ae5 MD |
1064 | lttcomm_destroy_sock(data_sock); |
1065 | error_sock_relay: | |
1066 | if (control_sock->fd >= 0) { | |
1067 | ret = control_sock->ops->close(control_sock); | |
b8aa1682 JD |
1068 | if (ret) { |
1069 | PERROR("close"); | |
1070 | } | |
b8aa1682 | 1071 | } |
095a4ae5 MD |
1072 | lttcomm_destroy_sock(control_sock); |
1073 | error_sock_control: | |
1074 | if (err) { | |
f385ae0a MD |
1075 | health_error(); |
1076 | ERR("Health error occurred in %s", __func__); | |
095a4ae5 | 1077 | } |
55706a7d | 1078 | health_unregister(health_relayd); |
b8aa1682 | 1079 | DBG("Relay listener thread cleanup complete"); |
b4aacfdc | 1080 | lttng_relay_stop_threads(); |
b8aa1682 JD |
1081 | return NULL; |
1082 | } | |
1083 | ||
1084 | /* | |
1085 | * This thread manages the dispatching of the requests to worker threads | |
1086 | */ | |
7591bab1 | 1087 | static void *relay_thread_dispatcher(void *data) |
b8aa1682 | 1088 | { |
6cd525e8 MD |
1089 | int err = -1; |
1090 | ssize_t ret; | |
8bdee6e2 | 1091 | struct cds_wfcq_node *node; |
58eb9381 | 1092 | struct relay_connection *new_conn = NULL; |
b8aa1682 JD |
1093 | |
1094 | DBG("[thread] Relay dispatcher started"); | |
1095 | ||
55706a7d MD |
1096 | health_register(health_relayd, HEALTH_RELAYD_TYPE_DISPATCHER); |
1097 | ||
9b5e0863 MD |
1098 | if (testpoint(relayd_thread_dispatcher)) { |
1099 | goto error_testpoint; | |
1100 | } | |
1101 | ||
f385ae0a MD |
1102 | health_code_update(); |
1103 | ||
68924dc1 | 1104 | for (;;) { |
f385ae0a MD |
1105 | health_code_update(); |
1106 | ||
b8aa1682 | 1107 | /* Atomically prepare the queue futex */ |
58eb9381 | 1108 | futex_nto1_prepare(&relay_conn_queue.futex); |
b8aa1682 | 1109 | |
68924dc1 MD |
1110 | if (CMM_LOAD_SHARED(dispatch_thread_exit)) { |
1111 | break; | |
1112 | } | |
1113 | ||
b8aa1682 | 1114 | do { |
f385ae0a MD |
1115 | health_code_update(); |
1116 | ||
b8aa1682 | 1117 | /* Dequeue commands */ |
8bdee6e2 SM |
1118 | node = cds_wfcq_dequeue_blocking(&relay_conn_queue.head, |
1119 | &relay_conn_queue.tail); | |
b8aa1682 JD |
1120 | if (node == NULL) { |
1121 | DBG("Woken up but nothing in the relay command queue"); | |
1122 | /* Continue thread execution */ | |
1123 | break; | |
1124 | } | |
58eb9381 | 1125 | new_conn = caa_container_of(node, struct relay_connection, qnode); |
b8aa1682 | 1126 | |
58eb9381 | 1127 | DBG("Dispatching request waiting on sock %d", new_conn->sock->fd); |
b8aa1682 JD |
1128 | |
1129 | /* | |
7591bab1 MD |
1130 | * Inform worker thread of the new request. This |
1131 | * call is blocking so we can be assured that | |
1132 | * the data will be read at some point in time | |
1133 | * or wait to the end of the world :) | |
b8aa1682 | 1134 | */ |
58eb9381 DG |
1135 | ret = lttng_write(relay_conn_pipe[1], &new_conn, sizeof(new_conn)); |
1136 | if (ret < 0) { | |
1137 | PERROR("write connection pipe"); | |
7591bab1 | 1138 | connection_put(new_conn); |
b8aa1682 JD |
1139 | goto error; |
1140 | } | |
1141 | } while (node != NULL); | |
1142 | ||
1143 | /* Futex wait on queue. Blocking call on futex() */ | |
f385ae0a | 1144 | health_poll_entry(); |
58eb9381 | 1145 | futex_nto1_wait(&relay_conn_queue.futex); |
f385ae0a | 1146 | health_poll_exit(); |
b8aa1682 JD |
1147 | } |
1148 | ||
f385ae0a MD |
1149 | /* Normal exit, no error */ |
1150 | err = 0; | |
1151 | ||
b8aa1682 | 1152 | error: |
9b5e0863 | 1153 | error_testpoint: |
f385ae0a MD |
1154 | if (err) { |
1155 | health_error(); | |
1156 | ERR("Health error occurred in %s", __func__); | |
1157 | } | |
55706a7d | 1158 | health_unregister(health_relayd); |
b8aa1682 | 1159 | DBG("Dispatch thread dying"); |
b4aacfdc | 1160 | lttng_relay_stop_threads(); |
b8aa1682 JD |
1161 | return NULL; |
1162 | } | |
1163 | ||
b8aa1682 | 1164 | /* |
7591bab1 | 1165 | * Set index data from the control port to a given index object. |
b8aa1682 | 1166 | */ |
7591bab1 | 1167 | static int set_index_control_data(struct relay_index *index, |
234cd636 JD |
1168 | struct lttcomm_relayd_index *data, |
1169 | struct relay_connection *conn) | |
1c20f0e2 | 1170 | { |
7591bab1 | 1171 | struct ctf_packet_index index_data; |
1c20f0e2 JD |
1172 | |
1173 | /* | |
a8cad461 | 1174 | * The index on disk is encoded in big endian. |
1c20f0e2 | 1175 | */ |
a8cad461 JG |
1176 | index_data.packet_size = htobe64(data->packet_size); |
1177 | index_data.content_size = htobe64(data->content_size); | |
1178 | index_data.timestamp_begin = htobe64(data->timestamp_begin); | |
1179 | index_data.timestamp_end = htobe64(data->timestamp_end); | |
1180 | index_data.events_discarded = htobe64(data->events_discarded); | |
1181 | index_data.stream_id = htobe64(data->stream_id); | |
234cd636 JD |
1182 | |
1183 | if (conn->minor >= 8) { | |
a8cad461 JG |
1184 | index->index_data.stream_instance_id = htobe64(data->stream_instance_id); |
1185 | index->index_data.packet_seq_num = htobe64(data->packet_seq_num); | |
234cd636 JD |
1186 | } |
1187 | ||
7591bab1 | 1188 | return relay_index_set_data(index, &index_data); |
1c20f0e2 JD |
1189 | } |
1190 | ||
c5b6f4f0 DG |
1191 | /* |
1192 | * Handle the RELAYD_CREATE_SESSION command. | |
1193 | * | |
1194 | * On success, send back the session id or else return a negative value. | |
1195 | */ | |
a8cad461 JG |
1196 | static int relay_create_session(const struct lttcomm_relayd_hdr *recv_hdr, |
1197 | struct relay_connection *conn, | |
1198 | const struct lttng_buffer_view *payload) | |
c5b6f4f0 | 1199 | { |
a8cad461 JG |
1200 | int ret = 0; |
1201 | ssize_t send_ret; | |
c5b6f4f0 DG |
1202 | struct relay_session *session; |
1203 | struct lttcomm_relayd_status_session reply; | |
36d2e35d | 1204 | char session_name[LTTNG_NAME_MAX]; |
9cf3eb20 | 1205 | char hostname[LTTNG_HOST_NAME_MAX]; |
7591bab1 MD |
1206 | uint32_t live_timer = 0; |
1207 | bool snapshot = false; | |
c5b6f4f0 | 1208 | |
36d2e35d | 1209 | memset(session_name, 0, LTTNG_NAME_MAX); |
9cf3eb20 | 1210 | memset(hostname, 0, LTTNG_HOST_NAME_MAX); |
c5b6f4f0 DG |
1211 | |
1212 | memset(&reply, 0, sizeof(reply)); | |
1213 | ||
58eb9381 | 1214 | switch (conn->minor) { |
2a174661 DG |
1215 | case 1: |
1216 | case 2: | |
1217 | case 3: | |
1218 | break; | |
1219 | case 4: /* LTTng sessiond 2.4 */ | |
1220 | default: | |
a8cad461 | 1221 | ret = cmd_create_session_2_4(payload, session_name, |
7591bab1 MD |
1222 | hostname, &live_timer, &snapshot); |
1223 | } | |
1224 | if (ret < 0) { | |
1225 | goto send_reply; | |
d3e2ba59 JD |
1226 | } |
1227 | ||
7591bab1 MD |
1228 | session = session_create(session_name, hostname, live_timer, |
1229 | snapshot, conn->major, conn->minor); | |
1230 | if (!session) { | |
1231 | ret = -1; | |
1232 | goto send_reply; | |
1233 | } | |
1234 | assert(!conn->session); | |
1235 | conn->session = session; | |
c5b6f4f0 DG |
1236 | DBG("Created session %" PRIu64, session->id); |
1237 | ||
7591bab1 MD |
1238 | reply.session_id = htobe64(session->id); |
1239 | ||
1240 | send_reply: | |
c5b6f4f0 DG |
1241 | if (ret < 0) { |
1242 | reply.ret_code = htobe32(LTTNG_ERR_FATAL); | |
1243 | } else { | |
1244 | reply.ret_code = htobe32(LTTNG_OK); | |
1245 | } | |
1246 | ||
58eb9381 | 1247 | send_ret = conn->sock->ops->sendmsg(conn->sock, &reply, sizeof(reply), 0); |
a8cad461 JG |
1248 | if (send_ret < (ssize_t) sizeof(reply)) { |
1249 | ERR("Failed to send \"create session\" command reply (ret = %zd)", | |
1250 | send_ret); | |
1251 | ret = -1; | |
c5b6f4f0 DG |
1252 | } |
1253 | ||
1254 | return ret; | |
1255 | } | |
1256 | ||
a4baae1b JD |
1257 | /* |
1258 | * When we have received all the streams and the metadata for a channel, | |
1259 | * we make them visible to the viewer threads. | |
1260 | */ | |
7591bab1 | 1261 | static void publish_connection_local_streams(struct relay_connection *conn) |
a4baae1b | 1262 | { |
7591bab1 MD |
1263 | struct relay_stream *stream; |
1264 | struct relay_session *session = conn->session; | |
a4baae1b | 1265 | |
7591bab1 MD |
1266 | /* |
1267 | * We publish all streams belonging to a session atomically wrt | |
1268 | * session lock. | |
1269 | */ | |
1270 | pthread_mutex_lock(&session->lock); | |
1271 | rcu_read_lock(); | |
1272 | cds_list_for_each_entry_rcu(stream, &session->recv_list, | |
1273 | recv_node) { | |
1274 | stream_publish(stream); | |
a4baae1b | 1275 | } |
7591bab1 | 1276 | rcu_read_unlock(); |
a4baae1b | 1277 | |
7591bab1 MD |
1278 | /* |
1279 | * Inform the viewer that there are new streams in the session. | |
1280 | */ | |
1281 | if (session->viewer_attached) { | |
1282 | uatomic_set(&session->new_streams, 1); | |
1283 | } | |
1284 | pthread_mutex_unlock(&session->lock); | |
a4baae1b JD |
1285 | } |
1286 | ||
b8aa1682 JD |
1287 | /* |
1288 | * relay_add_stream: allocate a new stream for a session | |
1289 | */ | |
a8cad461 JG |
1290 | static int relay_add_stream(const struct lttcomm_relayd_hdr *recv_hdr, |
1291 | struct relay_connection *conn, | |
1292 | const struct lttng_buffer_view *payload) | |
b8aa1682 | 1293 | { |
7591bab1 MD |
1294 | int ret; |
1295 | ssize_t send_ret; | |
58eb9381 | 1296 | struct relay_session *session = conn->session; |
b8aa1682 JD |
1297 | struct relay_stream *stream = NULL; |
1298 | struct lttcomm_relayd_status_stream reply; | |
4030a636 | 1299 | struct ctf_trace *trace = NULL; |
7591bab1 MD |
1300 | uint64_t stream_handle = -1ULL; |
1301 | char *path_name = NULL, *channel_name = NULL; | |
1302 | uint64_t tracefile_size = 0, tracefile_count = 0; | |
b8aa1682 | 1303 | |
a8cad461 | 1304 | if (!session || !conn->version_check_done) { |
b8aa1682 JD |
1305 | ERR("Trying to add a stream before version check"); |
1306 | ret = -1; | |
1307 | goto end_no_session; | |
1308 | } | |
1309 | ||
7591bab1 MD |
1310 | switch (session->minor) { |
1311 | case 1: /* LTTng sessiond 2.1. Allocates path_name and channel_name. */ | |
a8cad461 | 1312 | ret = cmd_recv_stream_2_1(payload, &path_name, |
4d27e2b4 | 1313 | &channel_name, session); |
0f907de1 | 1314 | break; |
7591bab1 | 1315 | case 2: /* LTTng sessiond 2.2. Allocates path_name and channel_name. */ |
0f907de1 | 1316 | default: |
a8cad461 | 1317 | ret = cmd_recv_stream_2_2(payload, &path_name, |
4d27e2b4 JR |
1318 | &channel_name, &tracefile_size, &tracefile_count, |
1319 | session); | |
0f907de1 JD |
1320 | break; |
1321 | } | |
1322 | if (ret < 0) { | |
7591bab1 | 1323 | goto send_reply; |
b8aa1682 JD |
1324 | } |
1325 | ||
7591bab1 | 1326 | trace = ctf_trace_get_by_path_or_create(session, path_name); |
2a174661 | 1327 | if (!trace) { |
7591bab1 | 1328 | goto send_reply; |
2a174661 | 1329 | } |
7591bab1 | 1330 | /* This stream here has one reference on the trace. */ |
2a174661 | 1331 | |
7591bab1 MD |
1332 | pthread_mutex_lock(&last_relay_stream_id_lock); |
1333 | stream_handle = ++last_relay_stream_id; | |
1334 | pthread_mutex_unlock(&last_relay_stream_id_lock); | |
d3e2ba59 | 1335 | |
7591bab1 MD |
1336 | /* We pass ownership of path_name and channel_name. */ |
1337 | stream = stream_create(trace, stream_handle, path_name, | |
1338 | channel_name, tracefile_size, tracefile_count); | |
1339 | path_name = NULL; | |
1340 | channel_name = NULL; | |
a4baae1b | 1341 | |
2a174661 | 1342 | /* |
7591bab1 MD |
1343 | * Streams are the owners of their trace. Reference to trace is |
1344 | * kept within stream_create(). | |
2a174661 | 1345 | */ |
7591bab1 | 1346 | ctf_trace_put(trace); |
d3e2ba59 | 1347 | |
7591bab1 | 1348 | send_reply: |
53efb85a | 1349 | memset(&reply, 0, sizeof(reply)); |
7591bab1 MD |
1350 | reply.handle = htobe64(stream_handle); |
1351 | if (!stream) { | |
f73fabfd | 1352 | reply.ret_code = htobe32(LTTNG_ERR_UNK); |
b8aa1682 | 1353 | } else { |
f73fabfd | 1354 | reply.ret_code = htobe32(LTTNG_OK); |
b8aa1682 | 1355 | } |
5af40280 | 1356 | |
58eb9381 | 1357 | send_ret = conn->sock->ops->sendmsg(conn->sock, &reply, |
b8aa1682 | 1358 | sizeof(struct lttcomm_relayd_status_stream), 0); |
a8cad461 JG |
1359 | if (send_ret < (ssize_t) sizeof(reply)) { |
1360 | ERR("Failed to send \"add stream\" command reply (ret = %zd)", | |
1361 | send_ret); | |
1362 | ret = -1; | |
b8aa1682 JD |
1363 | } |
1364 | ||
1365 | end_no_session: | |
7591bab1 MD |
1366 | free(path_name); |
1367 | free(channel_name); | |
0f907de1 | 1368 | return ret; |
b8aa1682 JD |
1369 | } |
1370 | ||
173af62f DG |
1371 | /* |
1372 | * relay_close_stream: close a specific stream | |
1373 | */ | |
a8cad461 JG |
1374 | static int relay_close_stream(const struct lttcomm_relayd_hdr *recv_hdr, |
1375 | struct relay_connection *conn, | |
1376 | const struct lttng_buffer_view *payload) | |
173af62f | 1377 | { |
a8cad461 JG |
1378 | int ret; |
1379 | ssize_t send_ret; | |
58eb9381 | 1380 | struct relay_session *session = conn->session; |
173af62f DG |
1381 | struct lttcomm_relayd_close_stream stream_info; |
1382 | struct lttcomm_relayd_generic_reply reply; | |
1383 | struct relay_stream *stream; | |
173af62f DG |
1384 | |
1385 | DBG("Close stream received"); | |
1386 | ||
a8cad461 | 1387 | if (!session || !conn->version_check_done) { |
173af62f DG |
1388 | ERR("Trying to close a stream before version check"); |
1389 | ret = -1; | |
1390 | goto end_no_session; | |
1391 | } | |
1392 | ||
a8cad461 JG |
1393 | if (payload->size < sizeof(stream_info)) { |
1394 | ERR("Unexpected payload size in \"relay_close_stream\": expected >= %zu bytes, got %zu bytes", | |
1395 | sizeof(stream_info), payload->size); | |
173af62f DG |
1396 | ret = -1; |
1397 | goto end_no_session; | |
1398 | } | |
a8cad461 JG |
1399 | memcpy(&stream_info, payload->data, sizeof(stream_info)); |
1400 | stream_info.stream_id = be64toh(stream_info.stream_id); | |
1401 | stream_info.last_net_seq_num = be64toh(stream_info.last_net_seq_num); | |
173af62f | 1402 | |
a8cad461 | 1403 | stream = stream_get_by_id(stream_info.stream_id); |
173af62f DG |
1404 | if (!stream) { |
1405 | ret = -1; | |
7591bab1 | 1406 | goto end; |
173af62f | 1407 | } |
77f7bd85 MD |
1408 | |
1409 | /* | |
1410 | * Set last_net_seq_num before the close flag. Required by data | |
1411 | * pending check. | |
1412 | */ | |
7591bab1 | 1413 | pthread_mutex_lock(&stream->lock); |
a8cad461 | 1414 | stream->last_net_seq_num = stream_info.last_net_seq_num; |
77f7bd85 MD |
1415 | pthread_mutex_unlock(&stream->lock); |
1416 | ||
bda7c7b9 JG |
1417 | /* |
1418 | * This is one of the conditions which may trigger a stream close | |
1419 | * with the others being: | |
1420 | * 1) A close command is received for a stream | |
1421 | * 2) The control connection owning the stream is closed | |
1422 | * 3) We have received all of the stream's data _after_ a close | |
1423 | * request. | |
1424 | */ | |
1425 | try_stream_close(stream); | |
7591bab1 MD |
1426 | if (stream->is_metadata) { |
1427 | struct relay_viewer_stream *vstream; | |
173af62f | 1428 | |
7591bab1 MD |
1429 | vstream = viewer_stream_get_by_id(stream->stream_handle); |
1430 | if (vstream) { | |
1431 | if (vstream->metadata_sent == stream->metadata_received) { | |
1432 | /* | |
1433 | * Since all the metadata has been sent to the | |
1434 | * viewer and that we have a request to close | |
1435 | * its stream, we can safely teardown the | |
1436 | * corresponding metadata viewer stream. | |
1437 | */ | |
1438 | viewer_stream_put(vstream); | |
1439 | } | |
1440 | /* Put local reference. */ | |
1441 | viewer_stream_put(vstream); | |
1442 | } | |
1443 | } | |
7591bab1 | 1444 | stream_put(stream); |
a8cad461 | 1445 | ret = 0; |
173af62f | 1446 | |
7591bab1 | 1447 | end: |
53efb85a | 1448 | memset(&reply, 0, sizeof(reply)); |
173af62f | 1449 | if (ret < 0) { |
f73fabfd | 1450 | reply.ret_code = htobe32(LTTNG_ERR_UNK); |
173af62f | 1451 | } else { |
f73fabfd | 1452 | reply.ret_code = htobe32(LTTNG_OK); |
173af62f | 1453 | } |
58eb9381 | 1454 | send_ret = conn->sock->ops->sendmsg(conn->sock, &reply, |
173af62f | 1455 | sizeof(struct lttcomm_relayd_generic_reply), 0); |
a8cad461 JG |
1456 | if (send_ret < (ssize_t) sizeof(reply)) { |
1457 | ERR("Failed to send \"close stream\" command reply (ret = %zd)", | |
1458 | send_ret); | |
1459 | ret = -1; | |
173af62f DG |
1460 | } |
1461 | ||
1462 | end_no_session: | |
1463 | return ret; | |
1464 | } | |
1465 | ||
93ec662e JD |
1466 | /* |
1467 | * relay_reset_metadata: reset a metadata stream | |
1468 | */ | |
1469 | static | |
a8cad461 JG |
1470 | int relay_reset_metadata(const struct lttcomm_relayd_hdr *recv_hdr, |
1471 | struct relay_connection *conn, | |
1472 | const struct lttng_buffer_view *payload) | |
93ec662e | 1473 | { |
a8cad461 JG |
1474 | int ret; |
1475 | ssize_t send_ret; | |
93ec662e JD |
1476 | struct relay_session *session = conn->session; |
1477 | struct lttcomm_relayd_reset_metadata stream_info; | |
1478 | struct lttcomm_relayd_generic_reply reply; | |
1479 | struct relay_stream *stream; | |
1480 | ||
1481 | DBG("Reset metadata received"); | |
1482 | ||
a8cad461 | 1483 | if (!session || !conn->version_check_done) { |
93ec662e JD |
1484 | ERR("Trying to reset a metadata stream before version check"); |
1485 | ret = -1; | |
1486 | goto end_no_session; | |
1487 | } | |
1488 | ||
a8cad461 JG |
1489 | if (payload->size < sizeof(stream_info)) { |
1490 | ERR("Unexpected payload size in \"relay_reset_metadata\": expected >= %zu bytes, got %zu bytes", | |
1491 | sizeof(stream_info), payload->size); | |
93ec662e JD |
1492 | ret = -1; |
1493 | goto end_no_session; | |
1494 | } | |
a8cad461 JG |
1495 | memcpy(&stream_info, payload->data, sizeof(stream_info)); |
1496 | stream_info.stream_id = be64toh(stream_info.stream_id); | |
1497 | stream_info.version = be64toh(stream_info.version); | |
1498 | ||
1499 | DBG("Update metadata to version %" PRIu64, stream_info.version); | |
93ec662e JD |
1500 | |
1501 | /* Unsupported for live sessions for now. */ | |
1502 | if (session->live_timer != 0) { | |
1503 | ret = -1; | |
1504 | goto end; | |
1505 | } | |
1506 | ||
a8cad461 | 1507 | stream = stream_get_by_id(stream_info.stream_id); |
93ec662e JD |
1508 | if (!stream) { |
1509 | ret = -1; | |
1510 | goto end; | |
1511 | } | |
1512 | pthread_mutex_lock(&stream->lock); | |
1513 | if (!stream->is_metadata) { | |
1514 | ret = -1; | |
1515 | goto end_unlock; | |
1516 | } | |
1517 | ||
1518 | ret = utils_rotate_stream_file(stream->path_name, stream->channel_name, | |
1519 | 0, 0, -1, -1, stream->stream_fd->fd, NULL, | |
1520 | &stream->stream_fd->fd); | |
1521 | if (ret < 0) { | |
1522 | ERR("Failed to rotate metadata file %s of channel %s", | |
1523 | stream->path_name, stream->channel_name); | |
1524 | goto end_unlock; | |
1525 | } | |
1526 | ||
1527 | end_unlock: | |
1528 | pthread_mutex_unlock(&stream->lock); | |
1529 | stream_put(stream); | |
1530 | ||
1531 | end: | |
1532 | memset(&reply, 0, sizeof(reply)); | |
1533 | if (ret < 0) { | |
1534 | reply.ret_code = htobe32(LTTNG_ERR_UNK); | |
1535 | } else { | |
1536 | reply.ret_code = htobe32(LTTNG_OK); | |
1537 | } | |
1538 | send_ret = conn->sock->ops->sendmsg(conn->sock, &reply, | |
1539 | sizeof(struct lttcomm_relayd_generic_reply), 0); | |
a8cad461 JG |
1540 | if (send_ret < (ssize_t) sizeof(reply)) { |
1541 | ERR("Failed to send \"reset metadata\" command reply (ret = %zd)", | |
1542 | send_ret); | |
1543 | ret = -1; | |
93ec662e JD |
1544 | } |
1545 | ||
1546 | end_no_session: | |
1547 | return ret; | |
1548 | } | |
1549 | ||
b8aa1682 JD |
1550 | /* |
1551 | * relay_unknown_command: send -1 if received unknown command | |
1552 | */ | |
7591bab1 | 1553 | static void relay_unknown_command(struct relay_connection *conn) |
b8aa1682 JD |
1554 | { |
1555 | struct lttcomm_relayd_generic_reply reply; | |
a8cad461 | 1556 | ssize_t send_ret; |
b8aa1682 | 1557 | |
53efb85a | 1558 | memset(&reply, 0, sizeof(reply)); |
f73fabfd | 1559 | reply.ret_code = htobe32(LTTNG_ERR_UNK); |
a8cad461 JG |
1560 | send_ret = conn->sock->ops->sendmsg(conn->sock, &reply, sizeof(reply), 0); |
1561 | if (send_ret < sizeof(reply)) { | |
1562 | ERR("Failed to send \"unknown command\" command reply (ret = %zd)", send_ret); | |
b8aa1682 JD |
1563 | } |
1564 | } | |
1565 | ||
1566 | /* | |
1567 | * relay_start: send an acknowledgment to the client to tell if we are | |
1568 | * ready to receive data. We are ready if a session is established. | |
1569 | */ | |
a8cad461 JG |
1570 | static int relay_start(const struct lttcomm_relayd_hdr *recv_hdr, |
1571 | struct relay_connection *conn, | |
1572 | const struct lttng_buffer_view *payload) | |
b8aa1682 | 1573 | { |
a8cad461 JG |
1574 | int ret = 0; |
1575 | ssize_t send_ret; | |
b8aa1682 | 1576 | struct lttcomm_relayd_generic_reply reply; |
58eb9381 | 1577 | struct relay_session *session = conn->session; |
b8aa1682 JD |
1578 | |
1579 | if (!session) { | |
1580 | DBG("Trying to start the streaming without a session established"); | |
f73fabfd | 1581 | ret = htobe32(LTTNG_ERR_UNK); |
b8aa1682 JD |
1582 | } |
1583 | ||
53efb85a | 1584 | memset(&reply, 0, sizeof(reply)); |
a8cad461 JG |
1585 | reply.ret_code = htobe32(LTTNG_OK); |
1586 | send_ret = conn->sock->ops->sendmsg(conn->sock, &reply, | |
1587 | sizeof(reply), 0); | |
1588 | if (send_ret < (ssize_t) sizeof(reply)) { | |
1589 | ERR("Failed to send \"relay_start\" command reply (ret = %zd)", | |
1590 | send_ret); | |
1591 | ret = -1; | |
b8aa1682 JD |
1592 | } |
1593 | ||
1594 | return ret; | |
1595 | } | |
1596 | ||
1d4dfdef DG |
1597 | /* |
1598 | * Append padding to the file pointed by the file descriptor fd. | |
1599 | */ | |
1600 | static int write_padding_to_file(int fd, uint32_t size) | |
1601 | { | |
6cd525e8 | 1602 | ssize_t ret = 0; |
1d4dfdef DG |
1603 | char *zeros; |
1604 | ||
1605 | if (size == 0) { | |
1606 | goto end; | |
1607 | } | |
1608 | ||
1609 | zeros = zmalloc(size); | |
1610 | if (zeros == NULL) { | |
1611 | PERROR("zmalloc zeros for padding"); | |
1612 | ret = -1; | |
1613 | goto end; | |
1614 | } | |
1615 | ||
6cd525e8 MD |
1616 | ret = lttng_write(fd, zeros, size); |
1617 | if (ret < size) { | |
1d4dfdef DG |
1618 | PERROR("write padding to file"); |
1619 | } | |
1620 | ||
e986c7a1 DG |
1621 | free(zeros); |
1622 | ||
1d4dfdef DG |
1623 | end: |
1624 | return ret; | |
1625 | } | |
1626 | ||
b8aa1682 | 1627 | /* |
7591bab1 | 1628 | * relay_recv_metadata: receive the metadata for the session. |
b8aa1682 | 1629 | */ |
a8cad461 JG |
1630 | static int relay_recv_metadata(const struct lttcomm_relayd_hdr *recv_hdr, |
1631 | struct relay_connection *conn, | |
1632 | const struct lttng_buffer_view *payload) | |
b8aa1682 | 1633 | { |
32d1569c | 1634 | int ret = 0; |
6cd525e8 | 1635 | ssize_t size_ret; |
58eb9381 | 1636 | struct relay_session *session = conn->session; |
a8cad461 | 1637 | struct lttcomm_relayd_metadata_payload metadata_payload_header; |
b8aa1682 | 1638 | struct relay_stream *metadata_stream; |
a8cad461 | 1639 | uint64_t metadata_payload_size; |
b8aa1682 JD |
1640 | |
1641 | if (!session) { | |
1642 | ERR("Metadata sent before version check"); | |
1643 | ret = -1; | |
1644 | goto end; | |
1645 | } | |
1646 | ||
a8cad461 | 1647 | if (recv_hdr->data_size < sizeof(struct lttcomm_relayd_metadata_payload)) { |
f6416125 MD |
1648 | ERR("Incorrect data size"); |
1649 | ret = -1; | |
1650 | goto end; | |
1651 | } | |
a8cad461 JG |
1652 | metadata_payload_size = recv_hdr->data_size - |
1653 | sizeof(struct lttcomm_relayd_metadata_payload); | |
f6416125 | 1654 | |
a8cad461 JG |
1655 | memcpy(&metadata_payload_header, payload->data, |
1656 | sizeof(metadata_payload_header)); | |
1657 | metadata_payload_header.stream_id = be64toh( | |
1658 | metadata_payload_header.stream_id); | |
1659 | metadata_payload_header.padding_size = be32toh( | |
1660 | metadata_payload_header.padding_size); | |
c617c0c6 | 1661 | |
a8cad461 | 1662 | metadata_stream = stream_get_by_id(metadata_payload_header.stream_id); |
b8aa1682 JD |
1663 | if (!metadata_stream) { |
1664 | ret = -1; | |
7591bab1 | 1665 | goto end; |
b8aa1682 JD |
1666 | } |
1667 | ||
7591bab1 MD |
1668 | pthread_mutex_lock(&metadata_stream->lock); |
1669 | ||
a8cad461 JG |
1670 | size_ret = lttng_write(metadata_stream->stream_fd->fd, |
1671 | payload->data + sizeof(metadata_payload_header), | |
1672 | metadata_payload_size); | |
1673 | if (size_ret < metadata_payload_size) { | |
b8aa1682 JD |
1674 | ERR("Relay error writing metadata on file"); |
1675 | ret = -1; | |
7591bab1 | 1676 | goto end_put; |
b8aa1682 | 1677 | } |
1d4dfdef | 1678 | |
32d1569c | 1679 | size_ret = write_padding_to_file(metadata_stream->stream_fd->fd, |
a8cad461 JG |
1680 | metadata_payload_header.padding_size); |
1681 | if (size_ret < (int64_t) metadata_payload_header.padding_size) { | |
1682 | ret = -1; | |
7591bab1 | 1683 | goto end_put; |
1d4dfdef | 1684 | } |
2a174661 | 1685 | |
7591bab1 | 1686 | metadata_stream->metadata_received += |
a8cad461 | 1687 | metadata_payload_size + metadata_payload_header.padding_size; |
7591bab1 MD |
1688 | DBG2("Relay metadata written. Updated metadata_received %" PRIu64, |
1689 | metadata_stream->metadata_received); | |
1d4dfdef | 1690 | |
7591bab1 MD |
1691 | end_put: |
1692 | pthread_mutex_unlock(&metadata_stream->lock); | |
1693 | stream_put(metadata_stream); | |
b8aa1682 JD |
1694 | end: |
1695 | return ret; | |
1696 | } | |
1697 | ||
1698 | /* | |
1699 | * relay_send_version: send relayd version number | |
1700 | */ | |
a8cad461 JG |
1701 | static int relay_send_version(const struct lttcomm_relayd_hdr *recv_hdr, |
1702 | struct relay_connection *conn, | |
1703 | const struct lttng_buffer_view *payload) | |
b8aa1682 | 1704 | { |
7f51dcba | 1705 | int ret; |
a8cad461 | 1706 | ssize_t send_ret; |
092b6259 | 1707 | struct lttcomm_relayd_version reply, msg; |
40096142 | 1708 | bool compatible = true; |
b8aa1682 | 1709 | |
a8cad461 | 1710 | conn->version_check_done = true; |
b8aa1682 | 1711 | |
092b6259 | 1712 | /* Get version from the other side. */ |
a8cad461 JG |
1713 | if (payload->size < sizeof(msg)) { |
1714 | ERR("Unexpected payload size in \"relay_send_version\": expected >= %zu bytes, got %zu bytes", | |
1715 | sizeof(msg), payload->size); | |
092b6259 | 1716 | ret = -1; |
092b6259 DG |
1717 | goto end; |
1718 | } | |
1719 | ||
a8cad461 JG |
1720 | memcpy(&msg, payload->data, sizeof(msg)); |
1721 | msg.major = be32toh(msg.major); | |
1722 | msg.minor = be32toh(msg.minor); | |
1723 | ||
53efb85a | 1724 | memset(&reply, 0, sizeof(reply)); |
d83a952c MD |
1725 | reply.major = RELAYD_VERSION_COMM_MAJOR; |
1726 | reply.minor = RELAYD_VERSION_COMM_MINOR; | |
d4519fa3 JD |
1727 | |
1728 | /* Major versions must be the same */ | |
a8cad461 | 1729 | if (reply.major != msg.major) { |
6151a90f | 1730 | DBG("Incompatible major versions (%u vs %u), deleting session", |
a8cad461 | 1731 | reply.major, msg.major); |
40096142 | 1732 | compatible = false; |
d4519fa3 JD |
1733 | } |
1734 | ||
58eb9381 | 1735 | conn->major = reply.major; |
0f907de1 | 1736 | /* We adapt to the lowest compatible version */ |
a8cad461 | 1737 | if (reply.minor <= msg.minor) { |
58eb9381 | 1738 | conn->minor = reply.minor; |
0f907de1 | 1739 | } else { |
a8cad461 | 1740 | conn->minor = msg.minor; |
0f907de1 JD |
1741 | } |
1742 | ||
6151a90f JD |
1743 | reply.major = htobe32(reply.major); |
1744 | reply.minor = htobe32(reply.minor); | |
a8cad461 JG |
1745 | send_ret = conn->sock->ops->sendmsg(conn->sock, &reply, |
1746 | sizeof(reply), 0); | |
1747 | if (send_ret < (ssize_t) sizeof(reply)) { | |
1748 | ERR("Failed to send \"send version\" command reply (ret = %zd)", | |
1749 | send_ret); | |
1750 | ret = -1; | |
1751 | goto end; | |
1752 | } else { | |
1753 | ret = 0; | |
6151a90f JD |
1754 | } |
1755 | ||
40096142 JD |
1756 | if (!compatible) { |
1757 | ret = -1; | |
1758 | goto end; | |
1759 | } | |
1760 | ||
58eb9381 DG |
1761 | DBG("Version check done using protocol %u.%u", conn->major, |
1762 | conn->minor); | |
b8aa1682 JD |
1763 | |
1764 | end: | |
1765 | return ret; | |
1766 | } | |
1767 | ||
c8f59ee5 | 1768 | /* |
6d805429 | 1769 | * Check for data pending for a given stream id from the session daemon. |
c8f59ee5 | 1770 | */ |
a8cad461 JG |
1771 | static int relay_data_pending(const struct lttcomm_relayd_hdr *recv_hdr, |
1772 | struct relay_connection *conn, | |
1773 | const struct lttng_buffer_view *payload) | |
c8f59ee5 | 1774 | { |
58eb9381 | 1775 | struct relay_session *session = conn->session; |
6d805429 | 1776 | struct lttcomm_relayd_data_pending msg; |
c8f59ee5 DG |
1777 | struct lttcomm_relayd_generic_reply reply; |
1778 | struct relay_stream *stream; | |
a8cad461 | 1779 | ssize_t send_ret; |
c8f59ee5 | 1780 | int ret; |
c8f59ee5 | 1781 | |
6d805429 | 1782 | DBG("Data pending command received"); |
c8f59ee5 | 1783 | |
a8cad461 | 1784 | if (!session || !conn->version_check_done) { |
c8f59ee5 DG |
1785 | ERR("Trying to check for data before version check"); |
1786 | ret = -1; | |
1787 | goto end_no_session; | |
1788 | } | |
1789 | ||
a8cad461 JG |
1790 | if (payload->size < sizeof(msg)) { |
1791 | ERR("Unexpected payload size in \"relay_data_pending\": expected >= %zu bytes, got %zu bytes", | |
1792 | sizeof(msg), payload->size); | |
c8f59ee5 DG |
1793 | ret = -1; |
1794 | goto end_no_session; | |
1795 | } | |
a8cad461 JG |
1796 | memcpy(&msg, payload->data, sizeof(msg)); |
1797 | msg.stream_id = be64toh(msg.stream_id); | |
1798 | msg.last_net_seq_num = be64toh(msg.last_net_seq_num); | |
c8f59ee5 | 1799 | |
a8cad461 | 1800 | stream = stream_get_by_id(msg.stream_id); |
de91f48a | 1801 | if (stream == NULL) { |
c8f59ee5 | 1802 | ret = -1; |
7591bab1 | 1803 | goto end; |
c8f59ee5 DG |
1804 | } |
1805 | ||
7591bab1 MD |
1806 | pthread_mutex_lock(&stream->lock); |
1807 | ||
6d805429 | 1808 | DBG("Data pending for stream id %" PRIu64 " prev_seq %" PRIu64 |
a8cad461 JG |
1809 | " and last_seq %" PRIu64, msg.stream_id, |
1810 | stream->prev_seq, msg.last_net_seq_num); | |
c8f59ee5 | 1811 | |
33832e64 | 1812 | /* Avoid wrapping issue */ |
a8cad461 | 1813 | if (((int64_t) (stream->prev_seq - msg.last_net_seq_num)) >= 0) { |
6d805429 | 1814 | /* Data has in fact been written and is NOT pending */ |
c8f59ee5 | 1815 | ret = 0; |
6d805429 DG |
1816 | } else { |
1817 | /* Data still being streamed thus pending */ | |
1818 | ret = 1; | |
c8f59ee5 DG |
1819 | } |
1820 | ||
7591bab1 MD |
1821 | stream->data_pending_check_done = true; |
1822 | pthread_mutex_unlock(&stream->lock); | |
f7079f67 | 1823 | |
7591bab1 MD |
1824 | stream_put(stream); |
1825 | end: | |
c8f59ee5 | 1826 | |
53efb85a | 1827 | memset(&reply, 0, sizeof(reply)); |
c8f59ee5 | 1828 | reply.ret_code = htobe32(ret); |
a8cad461 JG |
1829 | send_ret = conn->sock->ops->sendmsg(conn->sock, &reply, sizeof(reply), 0); |
1830 | if (send_ret < (ssize_t) sizeof(reply)) { | |
1831 | ERR("Failed to send \"data pending\" command reply (ret = %zd)", | |
1832 | send_ret); | |
1833 | ret = -1; | |
c8f59ee5 DG |
1834 | } |
1835 | ||
1836 | end_no_session: | |
1837 | return ret; | |
1838 | } | |
1839 | ||
1840 | /* | |
1841 | * Wait for the control socket to reach a quiescent state. | |
1842 | * | |
7591bab1 MD |
1843 | * Note that for now, when receiving this command from the session |
1844 | * daemon, this means that every subsequent commands or data received on | |
1845 | * the control socket has been handled. So, this is why we simply return | |
1846 | * OK here. | |
c8f59ee5 | 1847 | */ |
a8cad461 JG |
1848 | static int relay_quiescent_control(const struct lttcomm_relayd_hdr *recv_hdr, |
1849 | struct relay_connection *conn, | |
1850 | const struct lttng_buffer_view *payload) | |
c8f59ee5 DG |
1851 | { |
1852 | int ret; | |
a8cad461 | 1853 | ssize_t send_ret; |
ad7051c0 | 1854 | struct relay_stream *stream; |
ad7051c0 | 1855 | struct lttcomm_relayd_quiescent_control msg; |
c8f59ee5 DG |
1856 | struct lttcomm_relayd_generic_reply reply; |
1857 | ||
1858 | DBG("Checking quiescent state on control socket"); | |
1859 | ||
a8cad461 | 1860 | if (!conn->session || !conn->version_check_done) { |
ad7051c0 DG |
1861 | ERR("Trying to check for data before version check"); |
1862 | ret = -1; | |
1863 | goto end_no_session; | |
1864 | } | |
1865 | ||
a8cad461 JG |
1866 | if (payload->size < sizeof(msg)) { |
1867 | ERR("Unexpected payload size in \"relay_quiescent_control\": expected >= %zu bytes, got %zu bytes", | |
1868 | sizeof(msg), payload->size); | |
ad7051c0 DG |
1869 | ret = -1; |
1870 | goto end_no_session; | |
1871 | } | |
a8cad461 JG |
1872 | memcpy(&msg, payload->data, sizeof(msg)); |
1873 | msg.stream_id = be64toh(msg.stream_id); | |
ad7051c0 | 1874 | |
a8cad461 | 1875 | stream = stream_get_by_id(msg.stream_id); |
7591bab1 MD |
1876 | if (!stream) { |
1877 | goto reply; | |
1878 | } | |
1879 | pthread_mutex_lock(&stream->lock); | |
1880 | stream->data_pending_check_done = true; | |
1881 | pthread_mutex_unlock(&stream->lock); | |
a8cad461 JG |
1882 | |
1883 | DBG("Relay quiescent control pending flag set to %" PRIu64, msg.stream_id); | |
7591bab1 MD |
1884 | stream_put(stream); |
1885 | reply: | |
53efb85a | 1886 | memset(&reply, 0, sizeof(reply)); |
c8f59ee5 | 1887 | reply.ret_code = htobe32(LTTNG_OK); |
a8cad461 JG |
1888 | send_ret = conn->sock->ops->sendmsg(conn->sock, &reply, sizeof(reply), 0); |
1889 | if (send_ret < (ssize_t) sizeof(reply)) { | |
1890 | ERR("Failed to send \"quiescent control\" command reply (ret = %zd)", | |
1891 | send_ret); | |
1892 | ret = -1; | |
1893 | } else { | |
1894 | ret = 0; | |
c8f59ee5 DG |
1895 | } |
1896 | ||
ad7051c0 | 1897 | end_no_session: |
c8f59ee5 DG |
1898 | return ret; |
1899 | } | |
1900 | ||
f7079f67 | 1901 | /* |
7591bab1 MD |
1902 | * Initialize a data pending command. This means that a consumer is about |
1903 | * to ask for data pending for each stream it holds. Simply iterate over | |
1904 | * all streams of a session and set the data_pending_check_done flag. | |
f7079f67 DG |
1905 | * |
1906 | * This command returns to the client a LTTNG_OK code. | |
1907 | */ | |
a8cad461 JG |
1908 | static int relay_begin_data_pending(const struct lttcomm_relayd_hdr *recv_hdr, |
1909 | struct relay_connection *conn, | |
1910 | const struct lttng_buffer_view *payload) | |
f7079f67 DG |
1911 | { |
1912 | int ret; | |
a8cad461 | 1913 | ssize_t send_ret; |
f7079f67 DG |
1914 | struct lttng_ht_iter iter; |
1915 | struct lttcomm_relayd_begin_data_pending msg; | |
1916 | struct lttcomm_relayd_generic_reply reply; | |
1917 | struct relay_stream *stream; | |
f7079f67 DG |
1918 | |
1919 | assert(recv_hdr); | |
58eb9381 | 1920 | assert(conn); |
f7079f67 DG |
1921 | |
1922 | DBG("Init streams for data pending"); | |
1923 | ||
a8cad461 | 1924 | if (!conn->session || !conn->version_check_done) { |
f7079f67 DG |
1925 | ERR("Trying to check for data before version check"); |
1926 | ret = -1; | |
1927 | goto end_no_session; | |
1928 | } | |
1929 | ||
a8cad461 JG |
1930 | if (payload->size < sizeof(msg)) { |
1931 | ERR("Unexpected payload size in \"relay_begin_data_pending\": expected >= %zu bytes, got %zu bytes", | |
1932 | sizeof(msg), payload->size); | |
f7079f67 DG |
1933 | ret = -1; |
1934 | goto end_no_session; | |
1935 | } | |
a8cad461 JG |
1936 | memcpy(&msg, payload->data, sizeof(msg)); |
1937 | msg.session_id = be64toh(msg.session_id); | |
f7079f67 DG |
1938 | |
1939 | /* | |
7591bab1 MD |
1940 | * Iterate over all streams to set the begin data pending flag. |
1941 | * For now, the streams are indexed by stream handle so we have | |
1942 | * to iterate over all streams to find the one associated with | |
1943 | * the right session_id. | |
f7079f67 DG |
1944 | */ |
1945 | rcu_read_lock(); | |
d3e2ba59 | 1946 | cds_lfht_for_each_entry(relay_streams_ht->ht, &iter.iter, stream, |
2a174661 | 1947 | node.node) { |
7591bab1 MD |
1948 | if (!stream_get(stream)) { |
1949 | continue; | |
1950 | } | |
a8cad461 | 1951 | if (stream->trace->session->id == msg.session_id) { |
7591bab1 MD |
1952 | pthread_mutex_lock(&stream->lock); |
1953 | stream->data_pending_check_done = false; | |
1954 | pthread_mutex_unlock(&stream->lock); | |
f7079f67 DG |
1955 | DBG("Set begin data pending flag to stream %" PRIu64, |
1956 | stream->stream_handle); | |
1957 | } | |
7591bab1 | 1958 | stream_put(stream); |
f7079f67 DG |
1959 | } |
1960 | rcu_read_unlock(); | |
1961 | ||
53efb85a | 1962 | memset(&reply, 0, sizeof(reply)); |
f7079f67 DG |
1963 | /* All good, send back reply. */ |
1964 | reply.ret_code = htobe32(LTTNG_OK); | |
1965 | ||
a8cad461 JG |
1966 | send_ret = conn->sock->ops->sendmsg(conn->sock, &reply, sizeof(reply), 0); |
1967 | if (send_ret < (ssize_t) sizeof(reply)) { | |
1968 | ERR("Failed to send \"begin data pending\" command reply (ret = %zd)", | |
1969 | send_ret); | |
1970 | ret = -1; | |
1971 | } else { | |
1972 | ret = 0; | |
f7079f67 DG |
1973 | } |
1974 | ||
1975 | end_no_session: | |
1976 | return ret; | |
1977 | } | |
1978 | ||
1979 | /* | |
7591bab1 MD |
1980 | * End data pending command. This will check, for a given session id, if |
1981 | * each stream associated with it has its data_pending_check_done flag | |
1982 | * set. If not, this means that the client lost track of the stream but | |
1983 | * the data is still being streamed on our side. In this case, we inform | |
1984 | * the client that data is in flight. | |
f7079f67 DG |
1985 | * |
1986 | * Return to the client if there is data in flight or not with a ret_code. | |
1987 | */ | |
a8cad461 JG |
1988 | static int relay_end_data_pending(const struct lttcomm_relayd_hdr *recv_hdr, |
1989 | struct relay_connection *conn, | |
1990 | const struct lttng_buffer_view *payload) | |
f7079f67 DG |
1991 | { |
1992 | int ret; | |
a8cad461 | 1993 | ssize_t send_ret; |
f7079f67 DG |
1994 | struct lttng_ht_iter iter; |
1995 | struct lttcomm_relayd_end_data_pending msg; | |
1996 | struct lttcomm_relayd_generic_reply reply; | |
1997 | struct relay_stream *stream; | |
f7079f67 DG |
1998 | uint32_t is_data_inflight = 0; |
1999 | ||
f7079f67 DG |
2000 | DBG("End data pending command"); |
2001 | ||
a8cad461 | 2002 | if (!conn->session || !conn->version_check_done) { |
f7079f67 DG |
2003 | ERR("Trying to check for data before version check"); |
2004 | ret = -1; | |
2005 | goto end_no_session; | |
2006 | } | |
2007 | ||
a8cad461 JG |
2008 | if (payload->size < sizeof(msg)) { |
2009 | ERR("Unexpected payload size in \"relay_end_data_pending\": expected >= %zu bytes, got %zu bytes", | |
2010 | sizeof(msg), payload->size); | |
f7079f67 DG |
2011 | ret = -1; |
2012 | goto end_no_session; | |
2013 | } | |
a8cad461 JG |
2014 | memcpy(&msg, payload->data, sizeof(msg)); |
2015 | msg.session_id = be64toh(msg.session_id); | |
f7079f67 | 2016 | |
7591bab1 MD |
2017 | /* |
2018 | * Iterate over all streams to see if the begin data pending | |
2019 | * flag is set. | |
2020 | */ | |
f7079f67 | 2021 | rcu_read_lock(); |
d3e2ba59 | 2022 | cds_lfht_for_each_entry(relay_streams_ht->ht, &iter.iter, stream, |
2a174661 | 2023 | node.node) { |
7591bab1 MD |
2024 | if (!stream_get(stream)) { |
2025 | continue; | |
2026 | } | |
a8cad461 | 2027 | if (stream->trace->session->id != msg.session_id) { |
7591bab1 MD |
2028 | stream_put(stream); |
2029 | continue; | |
2030 | } | |
2031 | pthread_mutex_lock(&stream->lock); | |
2032 | if (!stream->data_pending_check_done) { | |
2033 | if (!stream->closed || !(((int64_t) (stream->prev_seq - stream->last_net_seq_num)) >= 0)) { | |
2034 | is_data_inflight = 1; | |
2035 | DBG("Data is still in flight for stream %" PRIu64, | |
2036 | stream->stream_handle); | |
2037 | pthread_mutex_unlock(&stream->lock); | |
2038 | stream_put(stream); | |
2039 | break; | |
2040 | } | |
f7079f67 | 2041 | } |
7591bab1 MD |
2042 | pthread_mutex_unlock(&stream->lock); |
2043 | stream_put(stream); | |
f7079f67 DG |
2044 | } |
2045 | rcu_read_unlock(); | |
2046 | ||
53efb85a | 2047 | memset(&reply, 0, sizeof(reply)); |
f7079f67 DG |
2048 | /* All good, send back reply. */ |
2049 | reply.ret_code = htobe32(is_data_inflight); | |
2050 | ||
a8cad461 JG |
2051 | send_ret = conn->sock->ops->sendmsg(conn->sock, &reply, sizeof(reply), 0); |
2052 | if (send_ret < (ssize_t) sizeof(reply)) { | |
2053 | ERR("Failed to send \"end data pending\" command reply (ret = %zd)", | |
2054 | send_ret); | |
2055 | ret = -1; | |
2056 | } else { | |
2057 | ret = 0; | |
f7079f67 DG |
2058 | } |
2059 | ||
2060 | end_no_session: | |
2061 | return ret; | |
2062 | } | |
2063 | ||
1c20f0e2 JD |
2064 | /* |
2065 | * Receive an index for a specific stream. | |
2066 | * | |
2067 | * Return 0 on success else a negative value. | |
2068 | */ | |
a8cad461 JG |
2069 | static int relay_recv_index(const struct lttcomm_relayd_hdr *recv_hdr, |
2070 | struct relay_connection *conn, | |
2071 | const struct lttng_buffer_view *payload) | |
1c20f0e2 | 2072 | { |
a8cad461 JG |
2073 | int ret; |
2074 | ssize_t send_ret; | |
58eb9381 | 2075 | struct relay_session *session = conn->session; |
1c20f0e2 | 2076 | struct lttcomm_relayd_index index_info; |
7591bab1 | 2077 | struct relay_index *index; |
1c20f0e2 JD |
2078 | struct lttcomm_relayd_generic_reply reply; |
2079 | struct relay_stream *stream; | |
e0547b83 | 2080 | size_t msg_len; |
1c20f0e2 | 2081 | |
58eb9381 | 2082 | assert(conn); |
1c20f0e2 JD |
2083 | |
2084 | DBG("Relay receiving index"); | |
2085 | ||
a8cad461 | 2086 | if (!session || !conn->version_check_done) { |
1c20f0e2 JD |
2087 | ERR("Trying to close a stream before version check"); |
2088 | ret = -1; | |
2089 | goto end_no_session; | |
2090 | } | |
2091 | ||
e0547b83 MD |
2092 | msg_len = lttcomm_relayd_index_len( |
2093 | lttng_to_index_major(conn->major, conn->minor), | |
2094 | lttng_to_index_minor(conn->major, conn->minor)); | |
a8cad461 JG |
2095 | if (payload->size < msg_len) { |
2096 | ERR("Unexpected payload size in \"relay_recv_index\": expected >= %zu bytes, got %zu bytes", | |
2097 | msg_len, payload->size); | |
1c20f0e2 JD |
2098 | ret = -1; |
2099 | goto end_no_session; | |
2100 | } | |
a8cad461 JG |
2101 | memcpy(&index_info, payload->data, msg_len); |
2102 | index_info.relay_stream_id = be64toh(index_info.relay_stream_id); | |
2103 | index_info.net_seq_num = be64toh(index_info.net_seq_num); | |
2104 | index_info.packet_size = be64toh(index_info.packet_size); | |
2105 | index_info.content_size = be64toh(index_info.content_size); | |
2106 | index_info.timestamp_begin = be64toh(index_info.timestamp_begin); | |
2107 | index_info.timestamp_end = be64toh(index_info.timestamp_end); | |
2108 | index_info.events_discarded = be64toh(index_info.events_discarded); | |
2109 | index_info.stream_id = be64toh(index_info.stream_id); | |
1c20f0e2 | 2110 | |
a8cad461 JG |
2111 | if (conn->minor >= 8) { |
2112 | index_info.stream_instance_id = | |
2113 | be64toh(index_info.stream_instance_id); | |
2114 | index_info.packet_seq_num = be64toh(index_info.packet_seq_num); | |
2115 | } | |
1c20f0e2 | 2116 | |
a8cad461 | 2117 | stream = stream_get_by_id(index_info.relay_stream_id); |
1c20f0e2 | 2118 | if (!stream) { |
7591bab1 | 2119 | ERR("stream_get_by_id not found"); |
1c20f0e2 | 2120 | ret = -1; |
7591bab1 | 2121 | goto end; |
1c20f0e2 | 2122 | } |
7591bab1 | 2123 | pthread_mutex_lock(&stream->lock); |
1c20f0e2 | 2124 | |
d3e2ba59 JD |
2125 | /* Live beacon handling */ |
2126 | if (index_info.packet_size == 0) { | |
7591bab1 MD |
2127 | DBG("Received live beacon for stream %" PRIu64, |
2128 | stream->stream_handle); | |
d3e2ba59 JD |
2129 | |
2130 | /* | |
7591bab1 MD |
2131 | * Only flag a stream inactive when it has already |
2132 | * received data and no indexes are in flight. | |
d3e2ba59 | 2133 | */ |
a44ca2ca | 2134 | if (stream->index_received_seqcount > 0 |
7591bab1 | 2135 | && stream->indexes_in_flight == 0) { |
a8cad461 | 2136 | stream->beacon_ts_end = index_info.timestamp_end; |
d3e2ba59 JD |
2137 | } |
2138 | ret = 0; | |
7591bab1 | 2139 | goto end_stream_put; |
d3e2ba59 JD |
2140 | } else { |
2141 | stream->beacon_ts_end = -1ULL; | |
2142 | } | |
2143 | ||
528f2ffa | 2144 | if (stream->ctf_stream_id == -1ULL) { |
a8cad461 | 2145 | stream->ctf_stream_id = index_info.stream_id; |
528f2ffa | 2146 | } |
a8cad461 | 2147 | index = relay_index_get_by_id_or_create(stream, index_info.net_seq_num); |
7591bab1 MD |
2148 | if (!index) { |
2149 | ret = -1; | |
2150 | ERR("relay_index_get_by_id_or_create index NULL"); | |
2151 | goto end_stream_put; | |
1c20f0e2 | 2152 | } |
234cd636 | 2153 | if (set_index_control_data(index, &index_info, conn)) { |
7591bab1 MD |
2154 | ERR("set_index_control_data error"); |
2155 | relay_index_put(index); | |
2156 | ret = -1; | |
2157 | goto end_stream_put; | |
2158 | } | |
2159 | ret = relay_index_try_flush(index); | |
2160 | if (ret == 0) { | |
a44ca2ca MD |
2161 | tracefile_array_commit_seq(stream->tfa); |
2162 | stream->index_received_seqcount++; | |
7591bab1 MD |
2163 | } else if (ret > 0) { |
2164 | /* no flush. */ | |
2165 | ret = 0; | |
2166 | } else { | |
2167 | ERR("relay_index_try_flush error %d", ret); | |
2168 | relay_index_put(index); | |
2169 | ret = -1; | |
1c20f0e2 JD |
2170 | } |
2171 | ||
7591bab1 MD |
2172 | end_stream_put: |
2173 | pthread_mutex_unlock(&stream->lock); | |
2174 | stream_put(stream); | |
2175 | ||
2176 | end: | |
1c20f0e2 | 2177 | |
53efb85a | 2178 | memset(&reply, 0, sizeof(reply)); |
1c20f0e2 JD |
2179 | if (ret < 0) { |
2180 | reply.ret_code = htobe32(LTTNG_ERR_UNK); | |
2181 | } else { | |
2182 | reply.ret_code = htobe32(LTTNG_OK); | |
2183 | } | |
58eb9381 | 2184 | send_ret = conn->sock->ops->sendmsg(conn->sock, &reply, sizeof(reply), 0); |
a8cad461 JG |
2185 | if (send_ret < (ssize_t) sizeof(reply)) { |
2186 | ERR("Failed to send \"recv index\" command reply (ret = %zd)", send_ret); | |
2187 | ret = -1; | |
1c20f0e2 JD |
2188 | } |
2189 | ||
2190 | end_no_session: | |
2191 | return ret; | |
2192 | } | |
2193 | ||
a4baae1b JD |
2194 | /* |
2195 | * Receive the streams_sent message. | |
2196 | * | |
2197 | * Return 0 on success else a negative value. | |
2198 | */ | |
a8cad461 JG |
2199 | static int relay_streams_sent(const struct lttcomm_relayd_hdr *recv_hdr, |
2200 | struct relay_connection *conn, | |
2201 | const struct lttng_buffer_view *payload) | |
a4baae1b | 2202 | { |
a8cad461 JG |
2203 | int ret; |
2204 | ssize_t send_ret; | |
a4baae1b JD |
2205 | struct lttcomm_relayd_generic_reply reply; |
2206 | ||
58eb9381 | 2207 | assert(conn); |
a4baae1b JD |
2208 | |
2209 | DBG("Relay receiving streams_sent"); | |
2210 | ||
a8cad461 | 2211 | if (!conn->session || !conn->version_check_done) { |
a4baae1b JD |
2212 | ERR("Trying to close a stream before version check"); |
2213 | ret = -1; | |
2214 | goto end_no_session; | |
2215 | } | |
2216 | ||
2217 | /* | |
7591bab1 MD |
2218 | * Publish every pending stream in the connection recv list which are |
2219 | * now ready to be used by the viewer. | |
4a9daf17 | 2220 | */ |
7591bab1 | 2221 | publish_connection_local_streams(conn); |
4a9daf17 | 2222 | |
53efb85a | 2223 | memset(&reply, 0, sizeof(reply)); |
a4baae1b | 2224 | reply.ret_code = htobe32(LTTNG_OK); |
58eb9381 | 2225 | send_ret = conn->sock->ops->sendmsg(conn->sock, &reply, sizeof(reply), 0); |
a8cad461 JG |
2226 | if (send_ret < (ssize_t) sizeof(reply)) { |
2227 | ERR("Failed to send \"streams sent\" command reply (ret = %zd)", | |
2228 | send_ret); | |
2229 | ret = -1; | |
a4baae1b JD |
2230 | } else { |
2231 | /* Success. */ | |
2232 | ret = 0; | |
2233 | } | |
2234 | ||
2235 | end_no_session: | |
2236 | return ret; | |
2237 | } | |
2238 | ||
a8cad461 JG |
2239 | #define DBG_CMD(cmd_name, conn) \ |
2240 | DBG3("Processing \"%s\" command for socket %i", cmd_name, conn->sock->fd); | |
2241 | ||
2242 | static int relay_process_control_command(struct relay_connection *conn, | |
2243 | const struct lttcomm_relayd_hdr *header, | |
2244 | const struct lttng_buffer_view *payload) | |
b8aa1682 JD |
2245 | { |
2246 | int ret = 0; | |
2247 | ||
a8cad461 | 2248 | switch (header->cmd) { |
b8aa1682 | 2249 | case RELAYD_CREATE_SESSION: |
a8cad461 JG |
2250 | DBG_CMD("RELAYD_CREATE_SESSION", conn); |
2251 | ret = relay_create_session(header, conn, payload); | |
b8aa1682 | 2252 | break; |
b8aa1682 | 2253 | case RELAYD_ADD_STREAM: |
a8cad461 JG |
2254 | DBG_CMD("RELAYD_ADD_STREAM", conn); |
2255 | ret = relay_add_stream(header, conn, payload); | |
b8aa1682 JD |
2256 | break; |
2257 | case RELAYD_START_DATA: | |
a8cad461 JG |
2258 | DBG_CMD("RELAYD_START_DATA", conn); |
2259 | ret = relay_start(header, conn, payload); | |
b8aa1682 JD |
2260 | break; |
2261 | case RELAYD_SEND_METADATA: | |
a8cad461 JG |
2262 | DBG_CMD("RELAYD_SEND_METADATA", conn); |
2263 | ret = relay_recv_metadata(header, conn, payload); | |
b8aa1682 JD |
2264 | break; |
2265 | case RELAYD_VERSION: | |
a8cad461 JG |
2266 | DBG_CMD("RELAYD_VERSION", conn); |
2267 | ret = relay_send_version(header, conn, payload); | |
b8aa1682 | 2268 | break; |
173af62f | 2269 | case RELAYD_CLOSE_STREAM: |
a8cad461 JG |
2270 | DBG_CMD("RELAYD_CLOSE_STREAM", conn); |
2271 | ret = relay_close_stream(header, conn, payload); | |
173af62f | 2272 | break; |
6d805429 | 2273 | case RELAYD_DATA_PENDING: |
a8cad461 JG |
2274 | DBG_CMD("RELAYD_DATA_PENDING", conn); |
2275 | ret = relay_data_pending(header, conn, payload); | |
c8f59ee5 DG |
2276 | break; |
2277 | case RELAYD_QUIESCENT_CONTROL: | |
a8cad461 JG |
2278 | DBG_CMD("RELAYD_QUIESCENT_CONTROL", conn); |
2279 | ret = relay_quiescent_control(header, conn, payload); | |
c8f59ee5 | 2280 | break; |
f7079f67 | 2281 | case RELAYD_BEGIN_DATA_PENDING: |
a8cad461 JG |
2282 | DBG_CMD("RELAYD_BEGIN_DATA_PENDING", conn); |
2283 | ret = relay_begin_data_pending(header, conn, payload); | |
f7079f67 DG |
2284 | break; |
2285 | case RELAYD_END_DATA_PENDING: | |
a8cad461 JG |
2286 | DBG_CMD("RELAYD_END_DATA_PENDING", conn); |
2287 | ret = relay_end_data_pending(header, conn, payload); | |
f7079f67 | 2288 | break; |
1c20f0e2 | 2289 | case RELAYD_SEND_INDEX: |
a8cad461 JG |
2290 | DBG_CMD("RELAYD_SEND_INDEX", conn); |
2291 | ret = relay_recv_index(header, conn, payload); | |
1c20f0e2 | 2292 | break; |
a4baae1b | 2293 | case RELAYD_STREAMS_SENT: |
a8cad461 JG |
2294 | DBG_CMD("RELAYD_STREAMS_SENT", conn); |
2295 | ret = relay_streams_sent(header, conn, payload); | |
a4baae1b | 2296 | break; |
93ec662e | 2297 | case RELAYD_RESET_METADATA: |
a8cad461 JG |
2298 | DBG_CMD("RELAYD_RESET_METADATA", conn); |
2299 | ret = relay_reset_metadata(header, conn, payload); | |
93ec662e | 2300 | break; |
b8aa1682 JD |
2301 | case RELAYD_UPDATE_SYNC_INFO: |
2302 | default: | |
a8cad461 | 2303 | ERR("Received unknown command (%u)", header->cmd); |
58eb9381 | 2304 | relay_unknown_command(conn); |
b8aa1682 JD |
2305 | ret = -1; |
2306 | goto end; | |
2307 | } | |
2308 | ||
2309 | end: | |
2310 | return ret; | |
2311 | } | |
2312 | ||
a8cad461 JG |
2313 | static enum relay_connection_status relay_process_control_receive_payload( |
2314 | struct relay_connection *conn) | |
2315 | { | |
2316 | int ret = 0; | |
2317 | enum relay_connection_status status = RELAY_CONNECTION_STATUS_OK; | |
2318 | struct lttng_dynamic_buffer *reception_buffer = | |
2319 | &conn->protocol.ctrl.reception_buffer; | |
2320 | struct ctrl_connection_state_receive_payload *state = | |
2321 | &conn->protocol.ctrl.state.receive_payload; | |
2322 | struct lttng_buffer_view payload_view; | |
2323 | ||
2324 | if (state->left_to_receive == 0) { | |
2325 | /* Short-circuit for payload-less commands. */ | |
2326 | goto reception_complete; | |
2327 | } | |
2328 | ret = conn->sock->ops->recvmsg(conn->sock, | |
2329 | reception_buffer->data + state->received, | |
2330 | state->left_to_receive, MSG_DONTWAIT); | |
2331 | if (ret < 0) { | |
2332 | if (errno != EAGAIN && errno != EWOULDBLOCK) { | |
2333 | PERROR("Unable to receive command payload on sock %d", | |
2334 | conn->sock->fd); | |
2335 | status = RELAY_CONNECTION_STATUS_ERROR; | |
2336 | } | |
2337 | goto end; | |
2338 | } else if (ret == 0) { | |
2339 | DBG("Socket %d performed an orderly shutdown (received EOF)", conn->sock->fd); | |
2340 | status = RELAY_CONNECTION_STATUS_CLOSED; | |
2341 | goto end; | |
2342 | } | |
2343 | ||
2344 | assert(ret > 0); | |
2345 | assert(ret <= state->left_to_receive); | |
2346 | ||
2347 | state->left_to_receive -= ret; | |
2348 | state->received += ret; | |
2349 | ||
2350 | if (state->left_to_receive > 0) { | |
2351 | /* | |
2352 | * Can't transition to the protocol's next state, wait to | |
2353 | * receive the rest of the header. | |
2354 | */ | |
2355 | DBG3("Partial reception of control connection protocol payload (received %" PRIu64 " bytes, %" PRIu64 " bytes left to receive, fd = %i)", | |
2356 | state->received, state->left_to_receive, | |
2357 | conn->sock->fd); | |
2358 | goto end; | |
2359 | } | |
2360 | ||
2361 | reception_complete: | |
2362 | DBG("Done receiving control command payload: fd = %i, payload size = %" PRIu64 " bytes", | |
2363 | conn->sock->fd, state->received); | |
2364 | /* | |
2365 | * The payload required to process the command has been received. | |
2366 | * A view to the reception buffer is forwarded to the various | |
2367 | * commands and the state of the control is reset on success. | |
2368 | * | |
2369 | * Commands are responsible for sending their reply to the peer. | |
2370 | */ | |
2371 | payload_view = lttng_buffer_view_from_dynamic_buffer(reception_buffer, | |
2372 | 0, -1); | |
2373 | ret = relay_process_control_command(conn, | |
2374 | &state->header, &payload_view); | |
2375 | if (ret < 0) { | |
2376 | status = RELAY_CONNECTION_STATUS_ERROR; | |
2377 | goto end; | |
2378 | } | |
2379 | ||
2380 | ret = connection_reset_protocol_state(conn); | |
2381 | if (ret) { | |
2382 | status = RELAY_CONNECTION_STATUS_ERROR; | |
2383 | } | |
2384 | end: | |
2385 | return status; | |
2386 | } | |
2387 | ||
2388 | static enum relay_connection_status relay_process_control_receive_header( | |
2389 | struct relay_connection *conn) | |
2390 | { | |
2391 | int ret = 0; | |
2392 | enum relay_connection_status status = RELAY_CONNECTION_STATUS_OK; | |
2393 | struct lttcomm_relayd_hdr header; | |
2394 | struct lttng_dynamic_buffer *reception_buffer = | |
2395 | &conn->protocol.ctrl.reception_buffer; | |
2396 | struct ctrl_connection_state_receive_header *state = | |
2397 | &conn->protocol.ctrl.state.receive_header; | |
2398 | ||
2399 | assert(state->left_to_receive != 0); | |
2400 | ||
2401 | ret = conn->sock->ops->recvmsg(conn->sock, | |
2402 | reception_buffer->data + state->received, | |
2403 | state->left_to_receive, MSG_DONTWAIT); | |
2404 | if (ret < 0) { | |
2405 | if (errno != EAGAIN && errno != EWOULDBLOCK) { | |
2406 | PERROR("Unable to receive control command header on sock %d", | |
2407 | conn->sock->fd); | |
2408 | status = RELAY_CONNECTION_STATUS_ERROR; | |
2409 | } | |
2410 | goto end; | |
2411 | } else if (ret == 0) { | |
2412 | DBG("Socket %d performed an orderly shutdown (received EOF)", conn->sock->fd); | |
2413 | status = RELAY_CONNECTION_STATUS_CLOSED; | |
2414 | goto end; | |
2415 | } | |
2416 | ||
2417 | assert(ret > 0); | |
2418 | assert(ret <= state->left_to_receive); | |
2419 | ||
2420 | state->left_to_receive -= ret; | |
2421 | state->received += ret; | |
2422 | ||
2423 | if (state->left_to_receive > 0) { | |
2424 | /* | |
2425 | * Can't transition to the protocol's next state, wait to | |
2426 | * receive the rest of the header. | |
2427 | */ | |
2428 | DBG3("Partial reception of control connection protocol header (received %" PRIu64 " bytes, %" PRIu64 " bytes left to receive, fd = %i)", | |
2429 | state->received, state->left_to_receive, | |
2430 | conn->sock->fd); | |
2431 | goto end; | |
2432 | } | |
2433 | ||
2434 | /* Transition to next state: receiving the command's payload. */ | |
2435 | conn->protocol.ctrl.state_id = | |
2436 | CTRL_CONNECTION_STATE_RECEIVE_PAYLOAD; | |
2437 | memcpy(&header, reception_buffer->data, sizeof(header)); | |
2438 | header.circuit_id = be64toh(header.circuit_id); | |
2439 | header.data_size = be64toh(header.data_size); | |
2440 | header.cmd = be32toh(header.cmd); | |
2441 | header.cmd_version = be32toh(header.cmd_version); | |
2442 | memcpy(&conn->protocol.ctrl.state.receive_payload.header, | |
2443 | &header, sizeof(header)); | |
2444 | ||
2445 | DBG("Done receiving control command header: fd = %i, cmd = %" PRIu32 ", cmd_version = %" PRIu32 ", payload size = %" PRIu64 " bytes", | |
2446 | conn->sock->fd, header.cmd, header.cmd_version, | |
2447 | header.data_size); | |
2448 | ||
2449 | if (header.data_size > DEFAULT_NETWORK_RELAYD_CTRL_MAX_PAYLOAD_SIZE) { | |
2450 | ERR("Command header indicates a payload (%" PRIu64 " bytes) that exceeds the maximal payload size allowed on a control connection.", | |
2451 | header.data_size); | |
2452 | status = RELAY_CONNECTION_STATUS_ERROR; | |
2453 | goto end; | |
2454 | } | |
2455 | ||
2456 | conn->protocol.ctrl.state.receive_payload.left_to_receive = | |
2457 | header.data_size; | |
2458 | conn->protocol.ctrl.state.receive_payload.received = 0; | |
2459 | ret = lttng_dynamic_buffer_set_size(reception_buffer, | |
2460 | header.data_size); | |
2461 | if (ret) { | |
2462 | status = RELAY_CONNECTION_STATUS_ERROR; | |
2463 | goto end; | |
2464 | } | |
2465 | ||
2466 | if (header.data_size == 0) { | |
2467 | /* | |
2468 | * Manually invoke the next state as the poll loop | |
2469 | * will not wake-up to allow us to proceed further. | |
2470 | */ | |
2471 | status = relay_process_control_receive_payload(conn); | |
2472 | } | |
2473 | end: | |
2474 | return status; | |
2475 | } | |
2476 | ||
2477 | /* | |
2478 | * Process the commands received on the control socket | |
2479 | */ | |
2480 | static enum relay_connection_status relay_process_control( | |
2481 | struct relay_connection *conn) | |
2482 | { | |
2483 | enum relay_connection_status status; | |
2484 | ||
2485 | switch (conn->protocol.ctrl.state_id) { | |
2486 | case CTRL_CONNECTION_STATE_RECEIVE_HEADER: | |
2487 | status = relay_process_control_receive_header(conn); | |
2488 | break; | |
2489 | case CTRL_CONNECTION_STATE_RECEIVE_PAYLOAD: | |
2490 | status = relay_process_control_receive_payload(conn); | |
2491 | break; | |
2492 | default: | |
2493 | ERR("Unknown control connection protocol state encountered."); | |
2494 | abort(); | |
2495 | } | |
2496 | ||
2497 | return status; | |
2498 | } | |
2499 | ||
7d2f7452 DG |
2500 | /* |
2501 | * Handle index for a data stream. | |
2502 | * | |
7591bab1 | 2503 | * Called with the stream lock held. |
7d2f7452 DG |
2504 | * |
2505 | * Return 0 on success else a negative value. | |
2506 | */ | |
2507 | static int handle_index_data(struct relay_stream *stream, uint64_t net_seq_num, | |
a8cad461 | 2508 | bool rotate_index) |
7d2f7452 | 2509 | { |
7591bab1 MD |
2510 | int ret = 0; |
2511 | uint64_t data_offset; | |
2512 | struct relay_index *index; | |
7d2f7452 | 2513 | |
7d2f7452 DG |
2514 | /* Get data offset because we are about to update the index. */ |
2515 | data_offset = htobe64(stream->tracefile_size_current); | |
2516 | ||
65d66b19 MD |
2517 | DBG("handle_index_data: stream %" PRIu64 " net_seq_num %" PRIu64 " data offset %" PRIu64, |
2518 | stream->stream_handle, net_seq_num, stream->tracefile_size_current); | |
7591bab1 | 2519 | |
7d2f7452 | 2520 | /* |
7591bab1 MD |
2521 | * Lookup for an existing index for that stream id/sequence |
2522 | * number. If it exists, the control thread has already received the | |
2523 | * data for it, thus we need to write it to disk. | |
7d2f7452 | 2524 | */ |
7591bab1 | 2525 | index = relay_index_get_by_id_or_create(stream, net_seq_num); |
7d2f7452 | 2526 | if (!index) { |
7591bab1 MD |
2527 | ret = -1; |
2528 | goto end; | |
7d2f7452 DG |
2529 | } |
2530 | ||
e0547b83 MD |
2531 | if (rotate_index || !stream->index_file) { |
2532 | uint32_t major, minor; | |
7591bab1 | 2533 | |
e0547b83 MD |
2534 | /* Put ref on previous index_file. */ |
2535 | if (stream->index_file) { | |
2536 | lttng_index_file_put(stream->index_file); | |
2537 | stream->index_file = NULL; | |
7d2f7452 | 2538 | } |
e0547b83 MD |
2539 | major = stream->trace->session->major; |
2540 | minor = stream->trace->session->minor; | |
2541 | stream->index_file = lttng_index_file_create(stream->path_name, | |
2542 | stream->channel_name, | |
7591bab1 | 2543 | -1, -1, stream->tracefile_size, |
e0547b83 MD |
2544 | tracefile_array_get_file_index_head(stream->tfa), |
2545 | lttng_to_index_major(major, minor), | |
2546 | lttng_to_index_minor(major, minor)); | |
2547 | if (!stream->index_file) { | |
7591bab1 MD |
2548 | ret = -1; |
2549 | /* Put self-ref for this index due to error. */ | |
2550 | relay_index_put(index); | |
e0547b83 | 2551 | index = NULL; |
7591bab1 | 2552 | goto end; |
7d2f7452 | 2553 | } |
7d2f7452 DG |
2554 | } |
2555 | ||
e0547b83 | 2556 | if (relay_index_set_file(index, stream->index_file, data_offset)) { |
7591bab1 MD |
2557 | ret = -1; |
2558 | /* Put self-ref for this index due to error. */ | |
2559 | relay_index_put(index); | |
e0547b83 | 2560 | index = NULL; |
7591bab1 | 2561 | goto end; |
7d2f7452 DG |
2562 | } |
2563 | ||
7591bab1 MD |
2564 | ret = relay_index_try_flush(index); |
2565 | if (ret == 0) { | |
a44ca2ca MD |
2566 | tracefile_array_commit_seq(stream->tfa); |
2567 | stream->index_received_seqcount++; | |
7591bab1 MD |
2568 | } else if (ret > 0) { |
2569 | /* No flush. */ | |
2570 | ret = 0; | |
2571 | } else { | |
2572 | /* Put self-ref for this index due to error. */ | |
2573 | relay_index_put(index); | |
e0547b83 | 2574 | index = NULL; |
7591bab1 MD |
2575 | ret = -1; |
2576 | } | |
2577 | end: | |
7d2f7452 DG |
2578 | return ret; |
2579 | } | |
2580 | ||
a8cad461 JG |
2581 | static enum relay_connection_status relay_process_data_receive_header( |
2582 | struct relay_connection *conn) | |
b8aa1682 | 2583 | { |
a8cad461 JG |
2584 | int ret; |
2585 | enum relay_connection_status status = RELAY_CONNECTION_STATUS_OK; | |
2586 | struct data_connection_state_receive_header *state = | |
2587 | &conn->protocol.data.state.receive_header; | |
2588 | struct lttcomm_relayd_data_hdr header; | |
b8aa1682 | 2589 | struct relay_stream *stream; |
b8aa1682 | 2590 | |
a8cad461 JG |
2591 | assert(state->left_to_receive != 0); |
2592 | ||
2593 | ret = conn->sock->ops->recvmsg(conn->sock, | |
2594 | state->header_reception_buffer + state->received, | |
2595 | state->left_to_receive, MSG_DONTWAIT); | |
2596 | if (ret < 0) { | |
2597 | if (errno != EAGAIN && errno != EWOULDBLOCK) { | |
2598 | PERROR("Unable to receive data header on sock %d", conn->sock->fd); | |
2599 | status = RELAY_CONNECTION_STATUS_ERROR; | |
a6cd2b97 | 2600 | } |
a8cad461 JG |
2601 | goto end; |
2602 | } else if (ret == 0) { | |
2603 | /* Orderly shutdown. Not necessary to print an error. */ | |
2604 | DBG("Socket %d performed an orderly shutdown (received EOF)", conn->sock->fd); | |
2605 | status = RELAY_CONNECTION_STATUS_CLOSED; | |
b8aa1682 JD |
2606 | goto end; |
2607 | } | |
2608 | ||
a8cad461 JG |
2609 | assert(ret > 0); |
2610 | assert(ret <= state->left_to_receive); | |
2611 | ||
2612 | state->left_to_receive -= ret; | |
2613 | state->received += ret; | |
2614 | ||
2615 | if (state->left_to_receive > 0) { | |
2616 | /* | |
2617 | * Can't transition to the protocol's next state, wait to | |
2618 | * receive the rest of the header. | |
2619 | */ | |
2620 | DBG3("Partial reception of data connection header (received %" PRIu64 " bytes, %" PRIu64 " bytes left to receive, fd = %i)", | |
2621 | state->received, state->left_to_receive, | |
2622 | conn->sock->fd); | |
2623 | ret = 0; | |
7591bab1 | 2624 | goto end; |
b8aa1682 | 2625 | } |
b8aa1682 | 2626 | |
a8cad461 JG |
2627 | /* Transition to next state: receiving the payload. */ |
2628 | conn->protocol.data.state_id = DATA_CONNECTION_STATE_RECEIVE_PAYLOAD; | |
173af62f | 2629 | |
a8cad461 JG |
2630 | memcpy(&header, state->header_reception_buffer, sizeof(header)); |
2631 | header.circuit_id = be64toh(header.circuit_id); | |
2632 | header.stream_id = be64toh(header.stream_id); | |
2633 | header.data_size = be32toh(header.data_size); | |
2634 | header.net_seq_num = be64toh(header.net_seq_num); | |
2635 | header.padding_size = be32toh(header.padding_size); | |
2636 | memcpy(&conn->protocol.data.state.receive_payload.header, &header, sizeof(header)); | |
2637 | ||
2638 | conn->protocol.data.state.receive_payload.left_to_receive = | |
2639 | header.data_size; | |
2640 | conn->protocol.data.state.receive_payload.received = 0; | |
2641 | conn->protocol.data.state.receive_payload.rotate_index = false; | |
2642 | ||
2643 | DBG("Received data connection header on fd %i: circuit_id = %" PRIu64 ", stream_id = %" PRIu64 ", data_size = %" PRIu32 ", net_seq_num = %" PRIu64 ", padding_size = %" PRIu32, | |
2644 | conn->sock->fd, header.circuit_id, | |
2645 | header.stream_id, header.data_size, | |
2646 | header.net_seq_num, header.padding_size); | |
2647 | ||
2648 | stream = stream_get_by_id(header.stream_id); | |
2649 | if (!stream) { | |
2650 | DBG("relay_process_data_receive_payload: Cannot find stream %" PRIu64, | |
2651 | header.stream_id); | |
2652 | /* Protocol error. */ | |
2653 | status = RELAY_CONNECTION_STATUS_ERROR; | |
2654 | goto end; | |
2655 | } | |
b8aa1682 | 2656 | |
7591bab1 MD |
2657 | pthread_mutex_lock(&stream->lock); |
2658 | ||
1c20f0e2 | 2659 | /* Check if a rotation is needed. */ |
0f907de1 | 2660 | if (stream->tracefile_size > 0 && |
a8cad461 | 2661 | (stream->tracefile_size_current + header.data_size) > |
0f907de1 | 2662 | stream->tracefile_size) { |
a44ca2ca MD |
2663 | uint64_t old_id, new_id; |
2664 | ||
2665 | old_id = tracefile_array_get_file_index_head(stream->tfa); | |
2666 | tracefile_array_file_rotate(stream->tfa); | |
2667 | ||
2668 | /* new_id is updated by utils_rotate_stream_file. */ | |
2669 | new_id = old_id; | |
6b6b9a5a | 2670 | |
7591bab1 MD |
2671 | ret = utils_rotate_stream_file(stream->path_name, |
2672 | stream->channel_name, stream->tracefile_size, | |
2673 | stream->tracefile_count, -1, | |
2674 | -1, stream->stream_fd->fd, | |
a44ca2ca | 2675 | &new_id, &stream->stream_fd->fd); |
0f907de1 | 2676 | if (ret < 0) { |
a8cad461 JG |
2677 | ERR("Failed to rotate stream output file"); |
2678 | status = RELAY_CONNECTION_STATUS_ERROR; | |
7591bab1 MD |
2679 | goto end_stream_unlock; |
2680 | } | |
a8cad461 | 2681 | |
7591bab1 MD |
2682 | /* |
2683 | * Reset current size because we just performed a stream | |
2684 | * rotation. | |
2685 | */ | |
a6976990 | 2686 | stream->tracefile_size_current = 0; |
a8cad461 | 2687 | conn->protocol.data.state.receive_payload.rotate_index = true; |
1c20f0e2 JD |
2688 | } |
2689 | ||
a8cad461 JG |
2690 | ret = 0; |
2691 | end_stream_unlock: | |
2692 | pthread_mutex_unlock(&stream->lock); | |
2693 | stream_put(stream); | |
2694 | end: | |
2695 | return status; | |
2696 | } | |
2697 | ||
2698 | static enum relay_connection_status relay_process_data_receive_payload( | |
2699 | struct relay_connection *conn) | |
2700 | { | |
2701 | int ret; | |
2702 | enum relay_connection_status status = RELAY_CONNECTION_STATUS_OK; | |
2703 | struct relay_stream *stream; | |
2704 | struct data_connection_state_receive_payload *state = | |
2705 | &conn->protocol.data.state.receive_payload; | |
2706 | const size_t chunk_size = RECV_DATA_BUFFER_SIZE; | |
2707 | char data_buffer[chunk_size]; | |
2708 | bool partial_recv = false; | |
2709 | bool new_stream = false, close_requested = false; | |
2710 | uint64_t left_to_receive = state->left_to_receive; | |
2711 | struct relay_session *session; | |
2712 | ||
2713 | DBG3("Receiving data for stream id %" PRIu64 " seqnum %" PRIu64 ", %" PRIu64" bytes received, %" PRIu64 " bytes left to receive", | |
2714 | state->header.stream_id, state->header.net_seq_num, | |
2715 | state->received, left_to_receive); | |
2716 | ||
2717 | stream = stream_get_by_id(state->header.stream_id); | |
2718 | if (!stream) { | |
2719 | /* Protocol error. */ | |
2720 | ERR("relay_process_data_receive_payload: cannot find stream %" PRIu64, | |
2721 | state->header.stream_id); | |
2722 | status = RELAY_CONNECTION_STATUS_ERROR; | |
2723 | goto end; | |
2724 | } | |
2725 | ||
2726 | pthread_mutex_lock(&stream->lock); | |
2727 | session = stream->trace->session; | |
2728 | if (!conn->session) { | |
2729 | ret = connection_set_session(conn, session); | |
2730 | if (ret) { | |
2731 | status = RELAY_CONNECTION_STATUS_ERROR; | |
7591bab1 | 2732 | goto end_stream_unlock; |
1c20f0e2 | 2733 | } |
1c20f0e2 JD |
2734 | } |
2735 | ||
a8cad461 JG |
2736 | /* |
2737 | * The size of the "chunk" received on any iteration is bounded by: | |
2738 | * - the data left to receive, | |
2739 | * - the data immediately available on the socket, | |
2740 | * - the on-stack data buffer | |
2741 | */ | |
2742 | while (left_to_receive > 0 && !partial_recv) { | |
2743 | ssize_t write_ret; | |
2744 | size_t recv_size = min(left_to_receive, chunk_size); | |
1d4dfdef | 2745 | |
a8cad461 JG |
2746 | ret = conn->sock->ops->recvmsg(conn->sock, data_buffer, |
2747 | recv_size, MSG_DONTWAIT); | |
2748 | if (ret < 0) { | |
2749 | if (errno != EAGAIN && errno != EWOULDBLOCK) { | |
2750 | PERROR("Socket %d error", conn->sock->fd); | |
2751 | status = RELAY_CONNECTION_STATUS_ERROR; | |
0848dba7 | 2752 | } |
0848dba7 | 2753 | goto end_stream_unlock; |
a8cad461 JG |
2754 | } else if (ret == 0) { |
2755 | /* No more data ready to be consumed on socket. */ | |
2756 | DBG3("No more data ready for consumption on data socket of stream id %" PRIu64, | |
2757 | state->header.stream_id); | |
2758 | status = RELAY_CONNECTION_STATUS_CLOSED; | |
2759 | break; | |
2760 | } else if (ret < (int) recv_size) { | |
2761 | /* | |
2762 | * All the data available on the socket has been | |
2763 | * consumed. | |
2764 | */ | |
2765 | partial_recv = true; | |
0848dba7 MD |
2766 | } |
2767 | ||
a8cad461 JG |
2768 | recv_size = ret; |
2769 | ||
0848dba7 | 2770 | /* Write data to stream output fd. */ |
a8cad461 | 2771 | write_ret = lttng_write(stream->stream_fd->fd, data_buffer, |
0848dba7 | 2772 | recv_size); |
a8cad461 | 2773 | if (write_ret < (ssize_t) recv_size) { |
0848dba7 | 2774 | ERR("Relay error writing data to file"); |
a8cad461 | 2775 | status = RELAY_CONNECTION_STATUS_ERROR; |
0848dba7 MD |
2776 | goto end_stream_unlock; |
2777 | } | |
2778 | ||
a8cad461 JG |
2779 | left_to_receive -= recv_size; |
2780 | state->received += recv_size; | |
2781 | state->left_to_receive = left_to_receive; | |
2782 | ||
0848dba7 | 2783 | DBG2("Relay wrote %zd bytes to tracefile for stream id %" PRIu64, |
a8cad461 JG |
2784 | write_ret, stream->stream_handle); |
2785 | } | |
2786 | ||
2787 | if (state->left_to_receive > 0) { | |
2788 | /* | |
2789 | * Did not receive all the data expected, wait for more data to | |
2790 | * become available on the socket. | |
2791 | */ | |
2792 | DBG3("Partial receive on data connection of stream id %" PRIu64 ", %" PRIu64 " bytes received, %" PRIu64 " bytes left to receive", | |
2793 | state->header.stream_id, state->received, | |
2794 | state->left_to_receive); | |
2795 | goto end_stream_unlock; | |
0848dba7 | 2796 | } |
5ab7344e | 2797 | |
7591bab1 | 2798 | ret = write_padding_to_file(stream->stream_fd->fd, |
a8cad461 JG |
2799 | state->header.padding_size); |
2800 | if ((int64_t) ret < (int64_t) state->header.padding_size) { | |
65d66b19 | 2801 | ERR("write_padding_to_file: fail stream %" PRIu64 " net_seq_num %" PRIu64 " ret %d", |
a8cad461 JG |
2802 | stream->stream_handle, |
2803 | state->header.net_seq_num, ret); | |
2804 | status = RELAY_CONNECTION_STATUS_ERROR; | |
7591bab1 | 2805 | goto end_stream_unlock; |
1d4dfdef | 2806 | } |
a8cad461 JG |
2807 | |
2808 | ||
2809 | if (session->minor >= 4 && !session->snapshot) { | |
2810 | ret = handle_index_data(stream, state->header.net_seq_num, | |
2811 | state->rotate_index); | |
2812 | if (ret < 0) { | |
2813 | ERR("handle_index_data: fail stream %" PRIu64 " net_seq_num %" PRIu64 " ret %d", | |
2814 | stream->stream_handle, | |
2815 | state->header.net_seq_num, ret); | |
2816 | status = RELAY_CONNECTION_STATUS_ERROR; | |
2817 | goto end_stream_unlock; | |
2818 | } | |
2819 | } | |
2820 | ||
2821 | stream->tracefile_size_current += state->header.data_size + | |
2822 | state->header.padding_size; | |
2823 | ||
c0bae11d MD |
2824 | if (stream->prev_seq == -1ULL) { |
2825 | new_stream = true; | |
2826 | } | |
2827 | ||
a8cad461 JG |
2828 | stream->prev_seq = state->header.net_seq_num; |
2829 | ||
2830 | /* | |
2831 | * Resetting the protocol state (to RECEIVE_HEADER) will trash the | |
2832 | * contents of *state which are aliased (union) to the same location as | |
2833 | * the new state. Don't use it beyond this point. | |
2834 | */ | |
2835 | connection_reset_protocol_state(conn); | |
2836 | state = NULL; | |
173af62f | 2837 | |
7591bab1 | 2838 | end_stream_unlock: |
bda7c7b9 | 2839 | close_requested = stream->close_requested; |
7591bab1 | 2840 | pthread_mutex_unlock(&stream->lock); |
a8cad461 | 2841 | if (close_requested && left_to_receive == 0) { |
bda7c7b9 JG |
2842 | try_stream_close(stream); |
2843 | } | |
2844 | ||
c0bae11d MD |
2845 | if (new_stream) { |
2846 | pthread_mutex_lock(&session->lock); | |
2847 | uatomic_set(&session->new_streams, 1); | |
2848 | pthread_mutex_unlock(&session->lock); | |
2849 | } | |
a8cad461 | 2850 | |
7591bab1 | 2851 | stream_put(stream); |
b8aa1682 | 2852 | end: |
a8cad461 JG |
2853 | return status; |
2854 | } | |
2855 | ||
2856 | /* | |
2857 | * relay_process_data: Process the data received on the data socket | |
2858 | */ | |
2859 | static enum relay_connection_status relay_process_data( | |
2860 | struct relay_connection *conn) | |
2861 | { | |
2862 | enum relay_connection_status status; | |
2863 | ||
2864 | switch (conn->protocol.data.state_id) { | |
2865 | case DATA_CONNECTION_STATE_RECEIVE_HEADER: | |
2866 | status = relay_process_data_receive_header(conn); | |
2867 | break; | |
2868 | case DATA_CONNECTION_STATE_RECEIVE_PAYLOAD: | |
2869 | status = relay_process_data_receive_payload(conn); | |
2870 | break; | |
2871 | default: | |
2872 | ERR("Unexpected data connection communication state."); | |
2873 | abort(); | |
2874 | } | |
2875 | ||
2876 | return status; | |
b8aa1682 JD |
2877 | } |
2878 | ||
7591bab1 | 2879 | static void cleanup_connection_pollfd(struct lttng_poll_event *events, int pollfd) |
b8aa1682 JD |
2880 | { |
2881 | int ret; | |
2882 | ||
58eb9381 | 2883 | (void) lttng_poll_del(events, pollfd); |
b8aa1682 JD |
2884 | |
2885 | ret = close(pollfd); | |
2886 | if (ret < 0) { | |
2887 | ERR("Closing pollfd %d", pollfd); | |
2888 | } | |
2889 | } | |
2890 | ||
7591bab1 MD |
2891 | static void relay_thread_close_connection(struct lttng_poll_event *events, |
2892 | int pollfd, struct relay_connection *conn) | |
9d1bbf21 | 2893 | { |
7591bab1 | 2894 | const char *type_str; |
2a174661 | 2895 | |
7591bab1 MD |
2896 | switch (conn->type) { |
2897 | case RELAY_DATA: | |
2898 | type_str = "Data"; | |
2899 | break; | |
2900 | case RELAY_CONTROL: | |
2901 | type_str = "Control"; | |
2902 | break; | |
2903 | case RELAY_VIEWER_COMMAND: | |
2904 | type_str = "Viewer Command"; | |
2905 | break; | |
2906 | case RELAY_VIEWER_NOTIFICATION: | |
2907 | type_str = "Viewer Notification"; | |
2908 | break; | |
2909 | default: | |
2910 | type_str = "Unknown"; | |
9d1bbf21 | 2911 | } |
7591bab1 MD |
2912 | cleanup_connection_pollfd(events, pollfd); |
2913 | connection_put(conn); | |
2914 | DBG("%s connection closed with %d", type_str, pollfd); | |
b8aa1682 JD |
2915 | } |
2916 | ||
2917 | /* | |
2918 | * This thread does the actual work | |
2919 | */ | |
7591bab1 | 2920 | static void *relay_thread_worker(void *data) |
b8aa1682 | 2921 | { |
beaad64c DG |
2922 | int ret, err = -1, last_seen_data_fd = -1; |
2923 | uint32_t nb_fd; | |
b8aa1682 JD |
2924 | struct lttng_poll_event events; |
2925 | struct lttng_ht *relay_connections_ht; | |
b8aa1682 | 2926 | struct lttng_ht_iter iter; |
90e7d72f | 2927 | struct relay_connection *destroy_conn = NULL; |
b8aa1682 JD |
2928 | |
2929 | DBG("[thread] Relay worker started"); | |
2930 | ||
9d1bbf21 MD |
2931 | rcu_register_thread(); |
2932 | ||
55706a7d MD |
2933 | health_register(health_relayd, HEALTH_RELAYD_TYPE_WORKER); |
2934 | ||
9b5e0863 MD |
2935 | if (testpoint(relayd_thread_worker)) { |
2936 | goto error_testpoint; | |
2937 | } | |
2938 | ||
f385ae0a MD |
2939 | health_code_update(); |
2940 | ||
b8aa1682 JD |
2941 | /* table of connections indexed on socket */ |
2942 | relay_connections_ht = lttng_ht_new(0, LTTNG_HT_TYPE_ULONG); | |
095a4ae5 MD |
2943 | if (!relay_connections_ht) { |
2944 | goto relay_connections_ht_error; | |
2945 | } | |
b8aa1682 | 2946 | |
b8aa1682 JD |
2947 | ret = create_thread_poll_set(&events, 2); |
2948 | if (ret < 0) { | |
2949 | goto error_poll_create; | |
2950 | } | |
2951 | ||
58eb9381 | 2952 | ret = lttng_poll_add(&events, relay_conn_pipe[0], LPOLLIN | LPOLLRDHUP); |
b8aa1682 JD |
2953 | if (ret < 0) { |
2954 | goto error; | |
2955 | } | |
2956 | ||
beaad64c | 2957 | restart: |
b8aa1682 | 2958 | while (1) { |
beaad64c DG |
2959 | int idx = -1, i, seen_control = 0, last_notdel_data_fd = -1; |
2960 | ||
f385ae0a MD |
2961 | health_code_update(); |
2962 | ||
b8aa1682 | 2963 | /* Infinite blocking call, waiting for transmission */ |
87c1611d | 2964 | DBG3("Relayd worker thread polling..."); |
f385ae0a | 2965 | health_poll_entry(); |
b8aa1682 | 2966 | ret = lttng_poll_wait(&events, -1); |
f385ae0a | 2967 | health_poll_exit(); |
b8aa1682 JD |
2968 | if (ret < 0) { |
2969 | /* | |
2970 | * Restart interrupted system call. | |
2971 | */ | |
2972 | if (errno == EINTR) { | |
2973 | goto restart; | |
2974 | } | |
2975 | goto error; | |
2976 | } | |
2977 | ||
0d9c5d77 DG |
2978 | nb_fd = ret; |
2979 | ||
beaad64c | 2980 | /* |
7591bab1 MD |
2981 | * Process control. The control connection is |
2982 | * prioritized so we don't starve it with high | |
2983 | * throughput tracing data on the data connection. | |
beaad64c | 2984 | */ |
b8aa1682 JD |
2985 | for (i = 0; i < nb_fd; i++) { |
2986 | /* Fetch once the poll data */ | |
beaad64c DG |
2987 | uint32_t revents = LTTNG_POLL_GETEV(&events, i); |
2988 | int pollfd = LTTNG_POLL_GETFD(&events, i); | |
b8aa1682 | 2989 | |
f385ae0a MD |
2990 | health_code_update(); |
2991 | ||
fd20dac9 | 2992 | if (!revents) { |
7591bab1 MD |
2993 | /* |
2994 | * No activity for this FD (poll | |
2995 | * implementation). | |
2996 | */ | |
fd20dac9 MD |
2997 | continue; |
2998 | } | |
2999 | ||
b8aa1682 JD |
3000 | /* Thread quit pipe has been closed. Killing thread. */ |
3001 | ret = check_thread_quit_pipe(pollfd, revents); | |
3002 | if (ret) { | |
095a4ae5 MD |
3003 | err = 0; |
3004 | goto exit; | |
b8aa1682 JD |
3005 | } |
3006 | ||
58eb9381 DG |
3007 | /* Inspect the relay conn pipe for new connection */ |
3008 | if (pollfd == relay_conn_pipe[0]) { | |
03e43155 | 3009 | if (revents & LPOLLIN) { |
90e7d72f JG |
3010 | struct relay_connection *conn; |
3011 | ||
58eb9381 | 3012 | ret = lttng_read(relay_conn_pipe[0], &conn, sizeof(conn)); |
b8aa1682 JD |
3013 | if (ret < 0) { |
3014 | goto error; | |
3015 | } | |
58eb9381 DG |
3016 | lttng_poll_add(&events, conn->sock->fd, |
3017 | LPOLLIN | LPOLLRDHUP); | |
7591bab1 | 3018 | connection_ht_add(relay_connections_ht, conn); |
58eb9381 | 3019 | DBG("Connection socket %d added", conn->sock->fd); |
03e43155 MD |
3020 | } else if (revents & (LPOLLERR | LPOLLHUP | LPOLLRDHUP)) { |
3021 | ERR("Relay connection pipe error"); | |
3022 | goto error; | |
3023 | } else { | |
3024 | ERR("Unexpected poll events %u for sock %d", revents, pollfd); | |
3025 | goto error; | |
b8aa1682 | 3026 | } |
58eb9381 | 3027 | } else { |
90e7d72f JG |
3028 | struct relay_connection *ctrl_conn; |
3029 | ||
7591bab1 | 3030 | ctrl_conn = connection_get_by_sock(relay_connections_ht, pollfd); |
58eb9381 | 3031 | /* If not found, there is a synchronization issue. */ |
90e7d72f | 3032 | assert(ctrl_conn); |
58eb9381 | 3033 | |
03e43155 MD |
3034 | if (ctrl_conn->type == RELAY_DATA) { |
3035 | if (revents & LPOLLIN) { | |
beaad64c DG |
3036 | /* |
3037 | * Flag the last seen data fd not deleted. It will be | |
3038 | * used as the last seen fd if any fd gets deleted in | |
3039 | * this first loop. | |
3040 | */ | |
3041 | last_notdel_data_fd = pollfd; | |
3042 | } | |
03e43155 MD |
3043 | goto put_ctrl_connection; |
3044 | } | |
3045 | assert(ctrl_conn->type == RELAY_CONTROL); | |
3046 | ||
3047 | if (revents & LPOLLIN) { | |
a8cad461 JG |
3048 | enum relay_connection_status status; |
3049 | ||
3050 | status = relay_process_control(ctrl_conn); | |
3051 | if (status != RELAY_CONNECTION_STATUS_OK) { | |
3052 | /* | |
3053 | * On socket error flag the session as aborted to force | |
3054 | * the cleanup of its stream otherwise it can leak | |
3055 | * during the lifetime of the relayd. | |
3056 | * | |
3057 | * This prevents situations in which streams can be | |
3058 | * left opened because an index was received, the | |
3059 | * control connection is closed, and the data | |
3060 | * connection is closed (uncleanly) before the packet's | |
3061 | * data provided. | |
3062 | * | |
3063 | * Since the control connection encountered an error, | |
3064 | * it is okay to be conservative and close the | |
3065 | * session right now as we can't rely on the protocol | |
3066 | * being respected anymore. | |
3067 | */ | |
3068 | if (status == RELAY_CONNECTION_STATUS_ERROR) { | |
3069 | session_abort(ctrl_conn->session); | |
03e43155 | 3070 | } |
a8cad461 JG |
3071 | |
3072 | /* Clear the connection on error or close. */ | |
3073 | relay_thread_close_connection(&events, | |
3074 | pollfd, | |
3075 | ctrl_conn); | |
03e43155 | 3076 | } |
a8cad461 | 3077 | seen_control = 1; |
03e43155 MD |
3078 | } else if (revents & (LPOLLERR | LPOLLHUP | LPOLLRDHUP)) { |
3079 | relay_thread_close_connection(&events, | |
3080 | pollfd, ctrl_conn); | |
3081 | if (last_seen_data_fd == pollfd) { | |
3082 | last_seen_data_fd = last_notdel_data_fd; | |
3083 | } | |
58eb9381 | 3084 | } else { |
03e43155 MD |
3085 | ERR("Unexpected poll events %u for control sock %d", |
3086 | revents, pollfd); | |
3087 | connection_put(ctrl_conn); | |
3088 | goto error; | |
beaad64c | 3089 | } |
03e43155 | 3090 | put_ctrl_connection: |
7591bab1 | 3091 | connection_put(ctrl_conn); |
beaad64c DG |
3092 | } |
3093 | } | |
3094 | ||
3095 | /* | |
3096 | * The last loop handled a control request, go back to poll to make | |
3097 | * sure we prioritise the control socket. | |
3098 | */ | |
3099 | if (seen_control) { | |
3100 | continue; | |
3101 | } | |
3102 | ||
3103 | if (last_seen_data_fd >= 0) { | |
3104 | for (i = 0; i < nb_fd; i++) { | |
3105 | int pollfd = LTTNG_POLL_GETFD(&events, i); | |
f385ae0a MD |
3106 | |
3107 | health_code_update(); | |
3108 | ||
beaad64c DG |
3109 | if (last_seen_data_fd == pollfd) { |
3110 | idx = i; | |
3111 | break; | |
3112 | } | |
3113 | } | |
3114 | } | |
3115 | ||
3116 | /* Process data connection. */ | |
3117 | for (i = idx + 1; i < nb_fd; i++) { | |
3118 | /* Fetch the poll data. */ | |
3119 | uint32_t revents = LTTNG_POLL_GETEV(&events, i); | |
3120 | int pollfd = LTTNG_POLL_GETFD(&events, i); | |
90e7d72f | 3121 | struct relay_connection *data_conn; |
beaad64c | 3122 | |
f385ae0a MD |
3123 | health_code_update(); |
3124 | ||
fd20dac9 MD |
3125 | if (!revents) { |
3126 | /* No activity for this FD (poll implementation). */ | |
3127 | continue; | |
3128 | } | |
3129 | ||
beaad64c | 3130 | /* Skip the command pipe. It's handled in the first loop. */ |
58eb9381 | 3131 | if (pollfd == relay_conn_pipe[0]) { |
beaad64c DG |
3132 | continue; |
3133 | } | |
3134 | ||
7591bab1 | 3135 | data_conn = connection_get_by_sock(relay_connections_ht, pollfd); |
90e7d72f | 3136 | if (!data_conn) { |
fd20dac9 | 3137 | /* Skip it. Might be removed before. */ |
fd20dac9 MD |
3138 | continue; |
3139 | } | |
03e43155 MD |
3140 | if (data_conn->type == RELAY_CONTROL) { |
3141 | goto put_data_connection; | |
3142 | } | |
3143 | assert(data_conn->type == RELAY_DATA); | |
fd20dac9 MD |
3144 | |
3145 | if (revents & LPOLLIN) { | |
a8cad461 JG |
3146 | enum relay_connection_status status; |
3147 | ||
3148 | status = relay_process_data(data_conn); | |
3149 | /* Connection closed or error. */ | |
3150 | if (status != RELAY_CONNECTION_STATUS_OK) { | |
3151 | /* | |
3152 | * On socket error flag the session as aborted to force | |
3153 | * the cleanup of its stream otherwise it can leak | |
3154 | * during the lifetime of the relayd. | |
3155 | * | |
3156 | * This prevents situations in which streams can be | |
3157 | * left opened because an index was received, the | |
3158 | * control connection is closed, and the data | |
3159 | * connection is closed (uncleanly) before the packet's | |
3160 | * data provided. | |
3161 | * | |
3162 | * Since the data connection encountered an error, | |
3163 | * it is okay to be conservative and close the | |
3164 | * session right now as we can't rely on the protocol | |
3165 | * being respected anymore. | |
3166 | */ | |
3167 | if (status == RELAY_CONNECTION_STATUS_ERROR) { | |
3168 | session_abort(data_conn->session); | |
3169 | } | |
7591bab1 | 3170 | relay_thread_close_connection(&events, pollfd, |
03e43155 | 3171 | data_conn); |
fd20dac9 MD |
3172 | /* |
3173 | * Every goto restart call sets the last seen fd where | |
3174 | * here we don't really care since we gracefully | |
3175 | * continue the loop after the connection is deleted. | |
3176 | */ | |
3177 | } else { | |
3178 | /* Keep last seen port. */ | |
3179 | last_seen_data_fd = pollfd; | |
7591bab1 | 3180 | connection_put(data_conn); |
fd20dac9 | 3181 | goto restart; |
b8aa1682 | 3182 | } |
03e43155 MD |
3183 | } else if (revents & (LPOLLERR | LPOLLHUP | LPOLLRDHUP)) { |
3184 | relay_thread_close_connection(&events, pollfd, | |
3185 | data_conn); | |
3186 | } else { | |
3187 | ERR("Unknown poll events %u for data sock %d", | |
3188 | revents, pollfd); | |
b8aa1682 | 3189 | } |
03e43155 | 3190 | put_data_connection: |
7591bab1 | 3191 | connection_put(data_conn); |
b8aa1682 | 3192 | } |
beaad64c | 3193 | last_seen_data_fd = -1; |
b8aa1682 JD |
3194 | } |
3195 | ||
f385ae0a MD |
3196 | /* Normal exit, no error */ |
3197 | ret = 0; | |
3198 | ||
095a4ae5 | 3199 | exit: |
b8aa1682 | 3200 | error: |
58eb9381 | 3201 | /* Cleanup reamaining connection object. */ |
9d1bbf21 | 3202 | rcu_read_lock(); |
90e7d72f JG |
3203 | cds_lfht_for_each_entry(relay_connections_ht->ht, &iter.iter, |
3204 | destroy_conn, | |
58eb9381 | 3205 | sock_n.node) { |
f385ae0a | 3206 | health_code_update(); |
06a68401 | 3207 | |
a8cad461 | 3208 | session_abort(destroy_conn->session); |
06a68401 | 3209 | |
7591bab1 MD |
3210 | /* |
3211 | * No need to grab another ref, because we own | |
3212 | * destroy_conn. | |
3213 | */ | |
3214 | relay_thread_close_connection(&events, destroy_conn->sock->fd, | |
3215 | destroy_conn); | |
b8aa1682 | 3216 | } |
94d49140 | 3217 | rcu_read_unlock(); |
7591bab1 MD |
3218 | |
3219 | lttng_poll_clean(&events); | |
7d2f7452 | 3220 | error_poll_create: |
b8aa1682 | 3221 | lttng_ht_destroy(relay_connections_ht); |
095a4ae5 | 3222 | relay_connections_ht_error: |
58eb9381 | 3223 | /* Close relay conn pipes */ |
33f8df9f JG |
3224 | (void) fd_tracker_util_pipe_close(the_fd_tracker, |
3225 | relay_conn_pipe); | |
095a4ae5 MD |
3226 | if (err) { |
3227 | DBG("Thread exited with error"); | |
3228 | } | |
b8aa1682 | 3229 | DBG("Worker thread cleanup complete"); |
9b5e0863 | 3230 | error_testpoint: |
f385ae0a MD |
3231 | if (err) { |
3232 | health_error(); | |
3233 | ERR("Health error occurred in %s", __func__); | |
3234 | } | |
3235 | health_unregister(health_relayd); | |
9d1bbf21 | 3236 | rcu_unregister_thread(); |
b4aacfdc | 3237 | lttng_relay_stop_threads(); |
b8aa1682 JD |
3238 | return NULL; |
3239 | } | |
3240 | ||
3241 | /* | |
3242 | * Create the relay command pipe to wake thread_manage_apps. | |
3243 | * Closed in cleanup(). | |
3244 | */ | |
58eb9381 | 3245 | static int create_relay_conn_pipe(void) |
b8aa1682 | 3246 | { |
33f8df9f JG |
3247 | return fd_tracker_util_pipe_open_cloexec(the_fd_tracker, |
3248 | "Relayd connection pipe", relay_conn_pipe); | |
b8aa1682 JD |
3249 | } |
3250 | ||
3251 | /* | |
3252 | * main | |
3253 | */ | |
3254 | int main(int argc, char **argv) | |
3255 | { | |
178a0557 | 3256 | int ret = 0, retval = 0; |
b8aa1682 JD |
3257 | void *status; |
3258 | ||
b967f1cc JR |
3259 | /* Parse environment variables */ |
3260 | parse_env_options(); | |
3261 | ||
3262 | /* | |
3263 | * Parse arguments. | |
3264 | * Command line arguments overwrite environment. | |
3265 | */ | |
b8aa1682 | 3266 | progname = argv[0]; |
178a0557 MD |
3267 | if (set_options(argc, argv)) { |
3268 | retval = -1; | |
3269 | goto exit_options; | |
b8aa1682 JD |
3270 | } |
3271 | ||
178a0557 MD |
3272 | if (set_signal_handler()) { |
3273 | retval = -1; | |
3274 | goto exit_options; | |
b8aa1682 JD |
3275 | } |
3276 | ||
cdd566a2 JR |
3277 | relayd_config_log(); |
3278 | ||
3279 | if (opt_print_version) { | |
3280 | print_version(); | |
3281 | retval = 0; | |
3282 | goto exit_options; | |
3283 | } | |
3284 | ||
23c5429a JG |
3285 | ret = fclose(stdin); |
3286 | if (ret) { | |
3287 | PERROR("Failed to close stdin"); | |
3288 | goto exit_options; | |
3289 | } | |
4d513a50 DG |
3290 | /* Try to create directory if -o, --output is specified. */ |
3291 | if (opt_output_path) { | |
994fa64f DG |
3292 | if (*opt_output_path != '/') { |
3293 | ERR("Please specify an absolute path for -o, --output PATH"); | |
178a0557 MD |
3294 | retval = -1; |
3295 | goto exit_options; | |
994fa64f DG |
3296 | } |
3297 | ||
d77dded2 JG |
3298 | ret = utils_mkdir_recursive(opt_output_path, S_IRWXU | S_IRWXG, |
3299 | -1, -1); | |
4d513a50 DG |
3300 | if (ret < 0) { |
3301 | ERR("Unable to create %s", opt_output_path); | |
178a0557 MD |
3302 | retval = -1; |
3303 | goto exit_options; | |
4d513a50 DG |
3304 | } |
3305 | } | |
3306 | ||
b8aa1682 | 3307 | /* Daemonize */ |
b5218ffb | 3308 | if (opt_daemon || opt_background) { |
3fd27398 MD |
3309 | ret = lttng_daemonize(&child_ppid, &recv_child_signal, |
3310 | !opt_background); | |
b8aa1682 | 3311 | if (ret < 0) { |
178a0557 MD |
3312 | retval = -1; |
3313 | goto exit_options; | |
b8aa1682 | 3314 | } |
3fd27398 MD |
3315 | } |
3316 | ||
a174e6fc JR |
3317 | if (opt_working_directory) { |
3318 | ret = utils_change_working_dir(opt_working_directory); | |
3319 | if (ret) { | |
3320 | ERR("Changing working directory"); | |
3321 | goto exit_options; | |
3322 | } | |
3323 | } | |
bf5e21fd JG |
3324 | /* |
3325 | * The RCU thread registration (and use, through the fd-tracker's | |
3326 | * creation) is done after the daemonization to allow us to not | |
3327 | * deal with liburcu's fork() management as the call RCU needs to | |
3328 | * be restored. | |
3329 | */ | |
3330 | rcu_register_thread(); | |
3331 | ||
3332 | the_fd_tracker = fd_tracker_create(lttng_opt_fd_cap); | |
3333 | if (!the_fd_tracker) { | |
3334 | retval = -1; | |
3335 | goto exit_options; | |
3336 | } | |
a174e6fc | 3337 | |
178a0557 MD |
3338 | /* Initialize thread health monitoring */ |
3339 | health_relayd = health_app_create(NR_HEALTH_RELAYD_TYPES); | |
3340 | if (!health_relayd) { | |
3341 | PERROR("health_app_create error"); | |
3342 | retval = -1; | |
3343 | goto exit_health_app_create; | |
3344 | } | |
3345 | ||
3fd27398 | 3346 | /* Create thread quit pipe */ |
178a0557 MD |
3347 | if (init_thread_quit_pipe()) { |
3348 | retval = -1; | |
3349 | goto exit_init_data; | |
b8aa1682 JD |
3350 | } |
3351 | ||
b8aa1682 | 3352 | /* Setup the thread apps communication pipe. */ |
178a0557 MD |
3353 | if (create_relay_conn_pipe()) { |
3354 | retval = -1; | |
3355 | goto exit_init_data; | |
b8aa1682 JD |
3356 | } |
3357 | ||
3358 | /* Init relay command queue. */ | |
8bdee6e2 | 3359 | cds_wfcq_init(&relay_conn_queue.head, &relay_conn_queue.tail); |
b8aa1682 | 3360 | |
554831e7 MD |
3361 | /* Initialize communication library */ |
3362 | lttcomm_init(); | |
87e45c13 | 3363 | lttcomm_inet_init(); |
554831e7 | 3364 | |
d3e2ba59 | 3365 | /* tables of sessions indexed by session ID */ |
7591bab1 MD |
3366 | sessions_ht = lttng_ht_new(0, LTTNG_HT_TYPE_U64); |
3367 | if (!sessions_ht) { | |
178a0557 MD |
3368 | retval = -1; |
3369 | goto exit_init_data; | |
d3e2ba59 JD |
3370 | } |
3371 | ||
3372 | /* tables of streams indexed by stream ID */ | |
2a174661 | 3373 | relay_streams_ht = lttng_ht_new(0, LTTNG_HT_TYPE_U64); |
d3e2ba59 | 3374 | if (!relay_streams_ht) { |
178a0557 MD |
3375 | retval = -1; |
3376 | goto exit_init_data; | |
d3e2ba59 JD |
3377 | } |
3378 | ||
3379 | /* tables of streams indexed by stream ID */ | |
92c6ca54 DG |
3380 | viewer_streams_ht = lttng_ht_new(0, LTTNG_HT_TYPE_U64); |
3381 | if (!viewer_streams_ht) { | |
178a0557 MD |
3382 | retval = -1; |
3383 | goto exit_init_data; | |
55706a7d MD |
3384 | } |
3385 | ||
2b56bd01 | 3386 | ret = init_health_quit_pipe(); |
178a0557 MD |
3387 | if (ret) { |
3388 | retval = -1; | |
3389 | goto exit_health_quit_pipe; | |
65931c8b MD |
3390 | } |
3391 | ||
3392 | /* Create thread to manage the client socket */ | |
1a1a34b4 | 3393 | ret = pthread_create(&health_thread, default_pthread_attr(), |
65931c8b | 3394 | thread_manage_health, (void *) NULL); |
178a0557 MD |
3395 | if (ret) { |
3396 | errno = ret; | |
65931c8b | 3397 | PERROR("pthread_create health"); |
178a0557 MD |
3398 | retval = -1; |
3399 | goto exit_health_thread; | |
65931c8b MD |
3400 | } |
3401 | ||
b8aa1682 | 3402 | /* Setup the dispatcher thread */ |
1a1a34b4 | 3403 | ret = pthread_create(&dispatcher_thread, default_pthread_attr(), |
b8aa1682 | 3404 | relay_thread_dispatcher, (void *) NULL); |
178a0557 MD |
3405 | if (ret) { |
3406 | errno = ret; | |
b8aa1682 | 3407 | PERROR("pthread_create dispatcher"); |
178a0557 MD |
3408 | retval = -1; |
3409 | goto exit_dispatcher_thread; | |
b8aa1682 JD |
3410 | } |
3411 | ||
3412 | /* Setup the worker thread */ | |
1a1a34b4 | 3413 | ret = pthread_create(&worker_thread, default_pthread_attr(), |
7591bab1 | 3414 | relay_thread_worker, NULL); |
178a0557 MD |
3415 | if (ret) { |
3416 | errno = ret; | |
b8aa1682 | 3417 | PERROR("pthread_create worker"); |
178a0557 MD |
3418 | retval = -1; |
3419 | goto exit_worker_thread; | |
b8aa1682 JD |
3420 | } |
3421 | ||
3422 | /* Setup the listener thread */ | |
1a1a34b4 | 3423 | ret = pthread_create(&listener_thread, default_pthread_attr(), |
b8aa1682 | 3424 | relay_thread_listener, (void *) NULL); |
178a0557 MD |
3425 | if (ret) { |
3426 | errno = ret; | |
b8aa1682 | 3427 | PERROR("pthread_create listener"); |
178a0557 MD |
3428 | retval = -1; |
3429 | goto exit_listener_thread; | |
b8aa1682 JD |
3430 | } |
3431 | ||
7591bab1 | 3432 | ret = relayd_live_create(live_uri); |
178a0557 | 3433 | if (ret) { |
d3e2ba59 | 3434 | ERR("Starting live viewer threads"); |
178a0557 | 3435 | retval = -1; |
50138f51 | 3436 | goto exit_live; |
d3e2ba59 JD |
3437 | } |
3438 | ||
178a0557 MD |
3439 | /* |
3440 | * This is where we start awaiting program completion (e.g. through | |
3441 | * signal that asks threads to teardown). | |
3442 | */ | |
3443 | ||
3444 | ret = relayd_live_join(); | |
3445 | if (ret) { | |
3446 | retval = -1; | |
3447 | } | |
50138f51 | 3448 | exit_live: |
178a0557 | 3449 | |
b8aa1682 | 3450 | ret = pthread_join(listener_thread, &status); |
178a0557 MD |
3451 | if (ret) { |
3452 | errno = ret; | |
3453 | PERROR("pthread_join listener_thread"); | |
3454 | retval = -1; | |
b8aa1682 JD |
3455 | } |
3456 | ||
178a0557 | 3457 | exit_listener_thread: |
b8aa1682 | 3458 | ret = pthread_join(worker_thread, &status); |
178a0557 MD |
3459 | if (ret) { |
3460 | errno = ret; | |
3461 | PERROR("pthread_join worker_thread"); | |
3462 | retval = -1; | |
b8aa1682 JD |
3463 | } |
3464 | ||
178a0557 | 3465 | exit_worker_thread: |
b8aa1682 | 3466 | ret = pthread_join(dispatcher_thread, &status); |
178a0557 MD |
3467 | if (ret) { |
3468 | errno = ret; | |
3469 | PERROR("pthread_join dispatcher_thread"); | |
3470 | retval = -1; | |
b8aa1682 | 3471 | } |
178a0557 | 3472 | exit_dispatcher_thread: |
42415026 | 3473 | |
65931c8b | 3474 | ret = pthread_join(health_thread, &status); |
178a0557 MD |
3475 | if (ret) { |
3476 | errno = ret; | |
3477 | PERROR("pthread_join health_thread"); | |
3478 | retval = -1; | |
65931c8b | 3479 | } |
178a0557 | 3480 | exit_health_thread: |
65931c8b | 3481 | |
2b56bd01 | 3482 | (void) fd_tracker_util_pipe_close(the_fd_tracker, health_quit_pipe); |
178a0557 | 3483 | exit_health_quit_pipe: |
65931c8b | 3484 | |
178a0557 | 3485 | exit_init_data: |
55706a7d | 3486 | health_app_destroy(health_relayd); |
55706a7d | 3487 | exit_health_app_create: |
178a0557 | 3488 | exit_options: |
2668465a MD |
3489 | /* |
3490 | * Wait for all pending call_rcu work to complete before tearing | |
3491 | * down data structures. call_rcu worker may be trying to | |
3492 | * perform lookups in those structures. | |
3493 | */ | |
3494 | rcu_barrier(); | |
7591bab1 MD |
3495 | relayd_cleanup(); |
3496 | ||
3497 | /* Ensure all prior call_rcu are done. */ | |
3498 | rcu_barrier(); | |
d3e2ba59 | 3499 | |
bf5e21fd JG |
3500 | fd_tracker_destroy(the_fd_tracker); |
3501 | rcu_unregister_thread(); | |
3502 | ||
178a0557 | 3503 | if (!retval) { |
b8aa1682 | 3504 | exit(EXIT_SUCCESS); |
178a0557 MD |
3505 | } else { |
3506 | exit(EXIT_FAILURE); | |
b8aa1682 | 3507 | } |
b8aa1682 | 3508 | } |