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 CB |
622 | /* Close thread quit pipes */ |
623 | utils_close_pipe(thread_quit_pipe); | |
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 | { |
a02de639 | 786 | int ret; |
b8aa1682 | 787 | |
a02de639 | 788 | ret = utils_create_pipe_cloexec(thread_quit_pipe); |
b8aa1682 | 789 | |
b8aa1682 JD |
790 | return ret; |
791 | } | |
792 | ||
793 | /* | |
794 | * Create a poll set with O_CLOEXEC and add the thread quit pipe to the set. | |
795 | */ | |
7591bab1 | 796 | static int create_thread_poll_set(struct lttng_poll_event *events, int size) |
b8aa1682 JD |
797 | { |
798 | int ret; | |
799 | ||
800 | if (events == NULL || size == 0) { | |
801 | ret = -1; | |
802 | goto error; | |
803 | } | |
804 | ||
805 | ret = lttng_poll_create(events, size, LTTNG_CLOEXEC); | |
806 | if (ret < 0) { | |
807 | goto error; | |
808 | } | |
809 | ||
810 | /* Add quit pipe */ | |
c7759e6a | 811 | ret = lttng_poll_add(events, thread_quit_pipe[0], LPOLLIN | LPOLLERR); |
b8aa1682 JD |
812 | if (ret < 0) { |
813 | goto error; | |
814 | } | |
815 | ||
816 | return 0; | |
817 | ||
818 | error: | |
819 | return ret; | |
820 | } | |
821 | ||
822 | /* | |
823 | * Check if the thread quit pipe was triggered. | |
824 | * | |
825 | * Return 1 if it was triggered else 0; | |
826 | */ | |
7591bab1 | 827 | static int check_thread_quit_pipe(int fd, uint32_t events) |
b8aa1682 JD |
828 | { |
829 | if (fd == thread_quit_pipe[0] && (events & LPOLLIN)) { | |
830 | return 1; | |
831 | } | |
832 | ||
833 | return 0; | |
834 | } | |
835 | ||
836 | /* | |
837 | * Create and init socket from uri. | |
838 | */ | |
7591bab1 | 839 | static struct lttcomm_sock *relay_socket_create(struct lttng_uri *uri) |
b8aa1682 JD |
840 | { |
841 | int ret; | |
842 | struct lttcomm_sock *sock = NULL; | |
843 | ||
844 | sock = lttcomm_alloc_sock_from_uri(uri); | |
845 | if (sock == NULL) { | |
846 | ERR("Allocating socket"); | |
847 | goto error; | |
848 | } | |
849 | ||
850 | ret = lttcomm_create_sock(sock); | |
851 | if (ret < 0) { | |
852 | goto error; | |
853 | } | |
854 | DBG("Listening on sock %d", sock->fd); | |
855 | ||
856 | ret = sock->ops->bind(sock); | |
857 | if (ret < 0) { | |
858 | goto error; | |
859 | } | |
860 | ||
861 | ret = sock->ops->listen(sock, -1); | |
862 | if (ret < 0) { | |
863 | goto error; | |
864 | ||
865 | } | |
866 | ||
867 | return sock; | |
868 | ||
869 | error: | |
870 | if (sock) { | |
871 | lttcomm_destroy_sock(sock); | |
872 | } | |
873 | return NULL; | |
874 | } | |
875 | ||
876 | /* | |
877 | * This thread manages the listening for new connections on the network | |
878 | */ | |
7591bab1 | 879 | static void *relay_thread_listener(void *data) |
b8aa1682 | 880 | { |
095a4ae5 | 881 | int i, ret, pollfd, err = -1; |
b8aa1682 JD |
882 | uint32_t revents, nb_fd; |
883 | struct lttng_poll_event events; | |
884 | struct lttcomm_sock *control_sock, *data_sock; | |
885 | ||
b8aa1682 JD |
886 | DBG("[thread] Relay listener started"); |
887 | ||
55706a7d MD |
888 | health_register(health_relayd, HEALTH_RELAYD_TYPE_LISTENER); |
889 | ||
f385ae0a MD |
890 | health_code_update(); |
891 | ||
7591bab1 | 892 | control_sock = relay_socket_create(control_uri); |
b8aa1682 | 893 | if (!control_sock) { |
095a4ae5 | 894 | goto error_sock_control; |
b8aa1682 JD |
895 | } |
896 | ||
7591bab1 | 897 | data_sock = relay_socket_create(data_uri); |
b8aa1682 | 898 | if (!data_sock) { |
095a4ae5 | 899 | goto error_sock_relay; |
b8aa1682 JD |
900 | } |
901 | ||
902 | /* | |
7591bab1 MD |
903 | * Pass 3 as size here for the thread quit pipe, control and |
904 | * data socket. | |
b8aa1682 JD |
905 | */ |
906 | ret = create_thread_poll_set(&events, 3); | |
907 | if (ret < 0) { | |
908 | goto error_create_poll; | |
909 | } | |
910 | ||
911 | /* Add the control socket */ | |
912 | ret = lttng_poll_add(&events, control_sock->fd, LPOLLIN | LPOLLRDHUP); | |
913 | if (ret < 0) { | |
914 | goto error_poll_add; | |
915 | } | |
916 | ||
917 | /* Add the data socket */ | |
918 | ret = lttng_poll_add(&events, data_sock->fd, LPOLLIN | LPOLLRDHUP); | |
919 | if (ret < 0) { | |
920 | goto error_poll_add; | |
921 | } | |
922 | ||
3fd27398 MD |
923 | lttng_relay_notify_ready(); |
924 | ||
9b5e0863 MD |
925 | if (testpoint(relayd_thread_listener)) { |
926 | goto error_testpoint; | |
927 | } | |
928 | ||
b8aa1682 | 929 | while (1) { |
f385ae0a MD |
930 | health_code_update(); |
931 | ||
b8aa1682 JD |
932 | DBG("Listener accepting connections"); |
933 | ||
b8aa1682 | 934 | restart: |
f385ae0a | 935 | health_poll_entry(); |
b8aa1682 | 936 | ret = lttng_poll_wait(&events, -1); |
f385ae0a | 937 | health_poll_exit(); |
b8aa1682 JD |
938 | if (ret < 0) { |
939 | /* | |
940 | * Restart interrupted system call. | |
941 | */ | |
942 | if (errno == EINTR) { | |
943 | goto restart; | |
944 | } | |
945 | goto error; | |
946 | } | |
947 | ||
0d9c5d77 DG |
948 | nb_fd = ret; |
949 | ||
b8aa1682 JD |
950 | DBG("Relay new connection received"); |
951 | for (i = 0; i < nb_fd; i++) { | |
f385ae0a MD |
952 | health_code_update(); |
953 | ||
b8aa1682 JD |
954 | /* Fetch once the poll data */ |
955 | revents = LTTNG_POLL_GETEV(&events, i); | |
956 | pollfd = LTTNG_POLL_GETFD(&events, i); | |
957 | ||
fd20dac9 | 958 | if (!revents) { |
7591bab1 MD |
959 | /* |
960 | * No activity for this FD (poll | |
961 | * implementation). | |
962 | */ | |
fd20dac9 MD |
963 | continue; |
964 | } | |
965 | ||
b8aa1682 JD |
966 | /* Thread quit pipe has been closed. Killing thread. */ |
967 | ret = check_thread_quit_pipe(pollfd, revents); | |
968 | if (ret) { | |
095a4ae5 MD |
969 | err = 0; |
970 | goto exit; | |
b8aa1682 JD |
971 | } |
972 | ||
03e43155 | 973 | if (revents & LPOLLIN) { |
4b7f17b2 | 974 | /* |
7591bab1 MD |
975 | * A new connection is requested, therefore a |
976 | * sessiond/consumerd connection is allocated in | |
977 | * this thread, enqueued to a global queue and | |
978 | * dequeued (and freed) in the worker thread. | |
4b7f17b2 | 979 | */ |
58eb9381 DG |
980 | int val = 1; |
981 | struct relay_connection *new_conn; | |
4b7f17b2 | 982 | struct lttcomm_sock *newsock; |
7591bab1 | 983 | enum connection_type type; |
b8aa1682 JD |
984 | |
985 | if (pollfd == data_sock->fd) { | |
7591bab1 | 986 | type = RELAY_DATA; |
b8aa1682 | 987 | newsock = data_sock->ops->accept(data_sock); |
58eb9381 DG |
988 | DBG("Relay data connection accepted, socket %d", |
989 | newsock->fd); | |
4b7f17b2 MD |
990 | } else { |
991 | assert(pollfd == control_sock->fd); | |
7591bab1 | 992 | type = RELAY_CONTROL; |
b8aa1682 | 993 | newsock = control_sock->ops->accept(control_sock); |
58eb9381 DG |
994 | DBG("Relay control connection accepted, socket %d", |
995 | newsock->fd); | |
b8aa1682 | 996 | } |
58eb9381 DG |
997 | if (!newsock) { |
998 | PERROR("accepting sock"); | |
58eb9381 DG |
999 | goto error; |
1000 | } | |
1001 | ||
1002 | ret = setsockopt(newsock->fd, SOL_SOCKET, SO_REUSEADDR, &val, | |
1003 | sizeof(val)); | |
b8aa1682 JD |
1004 | if (ret < 0) { |
1005 | PERROR("setsockopt inet"); | |
4b7f17b2 | 1006 | lttcomm_destroy_sock(newsock); |
b8aa1682 JD |
1007 | goto error; |
1008 | } | |
2fc6b1ab JR |
1009 | |
1010 | ret = socket_apply_keep_alive_config(newsock->fd); | |
1011 | if (ret < 0) { | |
1012 | ERR("Failed to apply TCP keep-alive configuration on socket (%i)", | |
1013 | newsock->fd); | |
1014 | lttcomm_destroy_sock(newsock); | |
1015 | goto error; | |
1016 | } | |
1017 | ||
7591bab1 MD |
1018 | new_conn = connection_create(newsock, type); |
1019 | if (!new_conn) { | |
1020 | lttcomm_destroy_sock(newsock); | |
1021 | goto error; | |
1022 | } | |
58eb9381 DG |
1023 | |
1024 | /* Enqueue request for the dispatcher thread. */ | |
8bdee6e2 SM |
1025 | cds_wfcq_enqueue(&relay_conn_queue.head, &relay_conn_queue.tail, |
1026 | &new_conn->qnode); | |
b8aa1682 JD |
1027 | |
1028 | /* | |
7591bab1 MD |
1029 | * Wake the dispatch queue futex. |
1030 | * Implicit memory barrier with the | |
1031 | * exchange in cds_wfcq_enqueue. | |
b8aa1682 | 1032 | */ |
58eb9381 | 1033 | futex_nto1_wake(&relay_conn_queue.futex); |
03e43155 MD |
1034 | } else if (revents & (LPOLLERR | LPOLLHUP | LPOLLRDHUP)) { |
1035 | ERR("socket poll error"); | |
1036 | goto error; | |
1037 | } else { | |
1038 | ERR("Unexpected poll events %u for sock %d", revents, pollfd); | |
1039 | goto error; | |
b8aa1682 JD |
1040 | } |
1041 | } | |
1042 | } | |
1043 | ||
095a4ae5 | 1044 | exit: |
b8aa1682 JD |
1045 | error: |
1046 | error_poll_add: | |
9b5e0863 | 1047 | error_testpoint: |
b8aa1682 JD |
1048 | lttng_poll_clean(&events); |
1049 | error_create_poll: | |
095a4ae5 MD |
1050 | if (data_sock->fd >= 0) { |
1051 | ret = data_sock->ops->close(data_sock); | |
b8aa1682 JD |
1052 | if (ret) { |
1053 | PERROR("close"); | |
1054 | } | |
b8aa1682 | 1055 | } |
095a4ae5 MD |
1056 | lttcomm_destroy_sock(data_sock); |
1057 | error_sock_relay: | |
1058 | if (control_sock->fd >= 0) { | |
1059 | ret = control_sock->ops->close(control_sock); | |
b8aa1682 JD |
1060 | if (ret) { |
1061 | PERROR("close"); | |
1062 | } | |
b8aa1682 | 1063 | } |
095a4ae5 MD |
1064 | lttcomm_destroy_sock(control_sock); |
1065 | error_sock_control: | |
1066 | if (err) { | |
f385ae0a MD |
1067 | health_error(); |
1068 | ERR("Health error occurred in %s", __func__); | |
095a4ae5 | 1069 | } |
55706a7d | 1070 | health_unregister(health_relayd); |
b8aa1682 | 1071 | DBG("Relay listener thread cleanup complete"); |
b4aacfdc | 1072 | lttng_relay_stop_threads(); |
b8aa1682 JD |
1073 | return NULL; |
1074 | } | |
1075 | ||
1076 | /* | |
1077 | * This thread manages the dispatching of the requests to worker threads | |
1078 | */ | |
7591bab1 | 1079 | static void *relay_thread_dispatcher(void *data) |
b8aa1682 | 1080 | { |
6cd525e8 MD |
1081 | int err = -1; |
1082 | ssize_t ret; | |
8bdee6e2 | 1083 | struct cds_wfcq_node *node; |
58eb9381 | 1084 | struct relay_connection *new_conn = NULL; |
b8aa1682 JD |
1085 | |
1086 | DBG("[thread] Relay dispatcher started"); | |
1087 | ||
55706a7d MD |
1088 | health_register(health_relayd, HEALTH_RELAYD_TYPE_DISPATCHER); |
1089 | ||
9b5e0863 MD |
1090 | if (testpoint(relayd_thread_dispatcher)) { |
1091 | goto error_testpoint; | |
1092 | } | |
1093 | ||
f385ae0a MD |
1094 | health_code_update(); |
1095 | ||
68924dc1 | 1096 | for (;;) { |
f385ae0a MD |
1097 | health_code_update(); |
1098 | ||
b8aa1682 | 1099 | /* Atomically prepare the queue futex */ |
58eb9381 | 1100 | futex_nto1_prepare(&relay_conn_queue.futex); |
b8aa1682 | 1101 | |
68924dc1 MD |
1102 | if (CMM_LOAD_SHARED(dispatch_thread_exit)) { |
1103 | break; | |
1104 | } | |
1105 | ||
b8aa1682 | 1106 | do { |
f385ae0a MD |
1107 | health_code_update(); |
1108 | ||
b8aa1682 | 1109 | /* Dequeue commands */ |
8bdee6e2 SM |
1110 | node = cds_wfcq_dequeue_blocking(&relay_conn_queue.head, |
1111 | &relay_conn_queue.tail); | |
b8aa1682 JD |
1112 | if (node == NULL) { |
1113 | DBG("Woken up but nothing in the relay command queue"); | |
1114 | /* Continue thread execution */ | |
1115 | break; | |
1116 | } | |
58eb9381 | 1117 | new_conn = caa_container_of(node, struct relay_connection, qnode); |
b8aa1682 | 1118 | |
58eb9381 | 1119 | DBG("Dispatching request waiting on sock %d", new_conn->sock->fd); |
b8aa1682 JD |
1120 | |
1121 | /* | |
7591bab1 MD |
1122 | * Inform worker thread of the new request. This |
1123 | * call is blocking so we can be assured that | |
1124 | * the data will be read at some point in time | |
1125 | * or wait to the end of the world :) | |
b8aa1682 | 1126 | */ |
58eb9381 DG |
1127 | ret = lttng_write(relay_conn_pipe[1], &new_conn, sizeof(new_conn)); |
1128 | if (ret < 0) { | |
1129 | PERROR("write connection pipe"); | |
7591bab1 | 1130 | connection_put(new_conn); |
b8aa1682 JD |
1131 | goto error; |
1132 | } | |
1133 | } while (node != NULL); | |
1134 | ||
1135 | /* Futex wait on queue. Blocking call on futex() */ | |
f385ae0a | 1136 | health_poll_entry(); |
58eb9381 | 1137 | futex_nto1_wait(&relay_conn_queue.futex); |
f385ae0a | 1138 | health_poll_exit(); |
b8aa1682 JD |
1139 | } |
1140 | ||
f385ae0a MD |
1141 | /* Normal exit, no error */ |
1142 | err = 0; | |
1143 | ||
b8aa1682 | 1144 | error: |
9b5e0863 | 1145 | error_testpoint: |
f385ae0a MD |
1146 | if (err) { |
1147 | health_error(); | |
1148 | ERR("Health error occurred in %s", __func__); | |
1149 | } | |
55706a7d | 1150 | health_unregister(health_relayd); |
b8aa1682 | 1151 | DBG("Dispatch thread dying"); |
b4aacfdc | 1152 | lttng_relay_stop_threads(); |
b8aa1682 JD |
1153 | return NULL; |
1154 | } | |
1155 | ||
b8aa1682 | 1156 | /* |
7591bab1 | 1157 | * Set index data from the control port to a given index object. |
b8aa1682 | 1158 | */ |
7591bab1 | 1159 | static int set_index_control_data(struct relay_index *index, |
234cd636 JD |
1160 | struct lttcomm_relayd_index *data, |
1161 | struct relay_connection *conn) | |
1c20f0e2 | 1162 | { |
7591bab1 | 1163 | struct ctf_packet_index index_data; |
1c20f0e2 JD |
1164 | |
1165 | /* | |
a8cad461 | 1166 | * The index on disk is encoded in big endian. |
1c20f0e2 | 1167 | */ |
a8cad461 JG |
1168 | index_data.packet_size = htobe64(data->packet_size); |
1169 | index_data.content_size = htobe64(data->content_size); | |
1170 | index_data.timestamp_begin = htobe64(data->timestamp_begin); | |
1171 | index_data.timestamp_end = htobe64(data->timestamp_end); | |
1172 | index_data.events_discarded = htobe64(data->events_discarded); | |
1173 | index_data.stream_id = htobe64(data->stream_id); | |
234cd636 JD |
1174 | |
1175 | if (conn->minor >= 8) { | |
a8cad461 JG |
1176 | index->index_data.stream_instance_id = htobe64(data->stream_instance_id); |
1177 | index->index_data.packet_seq_num = htobe64(data->packet_seq_num); | |
234cd636 JD |
1178 | } |
1179 | ||
7591bab1 | 1180 | return relay_index_set_data(index, &index_data); |
1c20f0e2 JD |
1181 | } |
1182 | ||
c5b6f4f0 DG |
1183 | /* |
1184 | * Handle the RELAYD_CREATE_SESSION command. | |
1185 | * | |
1186 | * On success, send back the session id or else return a negative value. | |
1187 | */ | |
a8cad461 JG |
1188 | static int relay_create_session(const struct lttcomm_relayd_hdr *recv_hdr, |
1189 | struct relay_connection *conn, | |
1190 | const struct lttng_buffer_view *payload) | |
c5b6f4f0 | 1191 | { |
a8cad461 JG |
1192 | int ret = 0; |
1193 | ssize_t send_ret; | |
c5b6f4f0 DG |
1194 | struct relay_session *session; |
1195 | struct lttcomm_relayd_status_session reply; | |
36d2e35d | 1196 | char session_name[LTTNG_NAME_MAX]; |
9cf3eb20 | 1197 | char hostname[LTTNG_HOST_NAME_MAX]; |
7591bab1 MD |
1198 | uint32_t live_timer = 0; |
1199 | bool snapshot = false; | |
c5b6f4f0 | 1200 | |
36d2e35d | 1201 | memset(session_name, 0, LTTNG_NAME_MAX); |
9cf3eb20 | 1202 | memset(hostname, 0, LTTNG_HOST_NAME_MAX); |
c5b6f4f0 DG |
1203 | |
1204 | memset(&reply, 0, sizeof(reply)); | |
1205 | ||
58eb9381 | 1206 | switch (conn->minor) { |
2a174661 DG |
1207 | case 1: |
1208 | case 2: | |
1209 | case 3: | |
1210 | break; | |
1211 | case 4: /* LTTng sessiond 2.4 */ | |
1212 | default: | |
a8cad461 | 1213 | ret = cmd_create_session_2_4(payload, session_name, |
7591bab1 MD |
1214 | hostname, &live_timer, &snapshot); |
1215 | } | |
1216 | if (ret < 0) { | |
1217 | goto send_reply; | |
d3e2ba59 JD |
1218 | } |
1219 | ||
7591bab1 MD |
1220 | session = session_create(session_name, hostname, live_timer, |
1221 | snapshot, conn->major, conn->minor); | |
1222 | if (!session) { | |
1223 | ret = -1; | |
1224 | goto send_reply; | |
1225 | } | |
1226 | assert(!conn->session); | |
1227 | conn->session = session; | |
c5b6f4f0 DG |
1228 | DBG("Created session %" PRIu64, session->id); |
1229 | ||
7591bab1 MD |
1230 | reply.session_id = htobe64(session->id); |
1231 | ||
1232 | send_reply: | |
c5b6f4f0 DG |
1233 | if (ret < 0) { |
1234 | reply.ret_code = htobe32(LTTNG_ERR_FATAL); | |
1235 | } else { | |
1236 | reply.ret_code = htobe32(LTTNG_OK); | |
1237 | } | |
1238 | ||
58eb9381 | 1239 | send_ret = conn->sock->ops->sendmsg(conn->sock, &reply, sizeof(reply), 0); |
a8cad461 JG |
1240 | if (send_ret < (ssize_t) sizeof(reply)) { |
1241 | ERR("Failed to send \"create session\" command reply (ret = %zd)", | |
1242 | send_ret); | |
1243 | ret = -1; | |
c5b6f4f0 DG |
1244 | } |
1245 | ||
1246 | return ret; | |
1247 | } | |
1248 | ||
a4baae1b JD |
1249 | /* |
1250 | * When we have received all the streams and the metadata for a channel, | |
1251 | * we make them visible to the viewer threads. | |
1252 | */ | |
7591bab1 | 1253 | static void publish_connection_local_streams(struct relay_connection *conn) |
a4baae1b | 1254 | { |
7591bab1 MD |
1255 | struct relay_stream *stream; |
1256 | struct relay_session *session = conn->session; | |
a4baae1b | 1257 | |
7591bab1 MD |
1258 | /* |
1259 | * We publish all streams belonging to a session atomically wrt | |
1260 | * session lock. | |
1261 | */ | |
1262 | pthread_mutex_lock(&session->lock); | |
1263 | rcu_read_lock(); | |
1264 | cds_list_for_each_entry_rcu(stream, &session->recv_list, | |
1265 | recv_node) { | |
1266 | stream_publish(stream); | |
a4baae1b | 1267 | } |
7591bab1 | 1268 | rcu_read_unlock(); |
a4baae1b | 1269 | |
7591bab1 MD |
1270 | /* |
1271 | * Inform the viewer that there are new streams in the session. | |
1272 | */ | |
1273 | if (session->viewer_attached) { | |
1274 | uatomic_set(&session->new_streams, 1); | |
1275 | } | |
1276 | pthread_mutex_unlock(&session->lock); | |
a4baae1b JD |
1277 | } |
1278 | ||
b8aa1682 JD |
1279 | /* |
1280 | * relay_add_stream: allocate a new stream for a session | |
1281 | */ | |
a8cad461 JG |
1282 | static int relay_add_stream(const struct lttcomm_relayd_hdr *recv_hdr, |
1283 | struct relay_connection *conn, | |
1284 | const struct lttng_buffer_view *payload) | |
b8aa1682 | 1285 | { |
7591bab1 MD |
1286 | int ret; |
1287 | ssize_t send_ret; | |
58eb9381 | 1288 | struct relay_session *session = conn->session; |
b8aa1682 JD |
1289 | struct relay_stream *stream = NULL; |
1290 | struct lttcomm_relayd_status_stream reply; | |
4030a636 | 1291 | struct ctf_trace *trace = NULL; |
7591bab1 MD |
1292 | uint64_t stream_handle = -1ULL; |
1293 | char *path_name = NULL, *channel_name = NULL; | |
1294 | uint64_t tracefile_size = 0, tracefile_count = 0; | |
b8aa1682 | 1295 | |
a8cad461 | 1296 | if (!session || !conn->version_check_done) { |
b8aa1682 JD |
1297 | ERR("Trying to add a stream before version check"); |
1298 | ret = -1; | |
1299 | goto end_no_session; | |
1300 | } | |
1301 | ||
7591bab1 MD |
1302 | switch (session->minor) { |
1303 | case 1: /* LTTng sessiond 2.1. Allocates path_name and channel_name. */ | |
a8cad461 | 1304 | ret = cmd_recv_stream_2_1(payload, &path_name, |
4d27e2b4 | 1305 | &channel_name, session); |
0f907de1 | 1306 | break; |
7591bab1 | 1307 | case 2: /* LTTng sessiond 2.2. Allocates path_name and channel_name. */ |
0f907de1 | 1308 | default: |
a8cad461 | 1309 | ret = cmd_recv_stream_2_2(payload, &path_name, |
4d27e2b4 JR |
1310 | &channel_name, &tracefile_size, &tracefile_count, |
1311 | session); | |
0f907de1 JD |
1312 | break; |
1313 | } | |
1314 | if (ret < 0) { | |
7591bab1 | 1315 | goto send_reply; |
b8aa1682 JD |
1316 | } |
1317 | ||
7591bab1 | 1318 | trace = ctf_trace_get_by_path_or_create(session, path_name); |
2a174661 | 1319 | if (!trace) { |
7591bab1 | 1320 | goto send_reply; |
2a174661 | 1321 | } |
7591bab1 | 1322 | /* This stream here has one reference on the trace. */ |
2a174661 | 1323 | |
7591bab1 MD |
1324 | pthread_mutex_lock(&last_relay_stream_id_lock); |
1325 | stream_handle = ++last_relay_stream_id; | |
1326 | pthread_mutex_unlock(&last_relay_stream_id_lock); | |
d3e2ba59 | 1327 | |
7591bab1 MD |
1328 | /* We pass ownership of path_name and channel_name. */ |
1329 | stream = stream_create(trace, stream_handle, path_name, | |
1330 | channel_name, tracefile_size, tracefile_count); | |
1331 | path_name = NULL; | |
1332 | channel_name = NULL; | |
a4baae1b | 1333 | |
2a174661 | 1334 | /* |
7591bab1 MD |
1335 | * Streams are the owners of their trace. Reference to trace is |
1336 | * kept within stream_create(). | |
2a174661 | 1337 | */ |
7591bab1 | 1338 | ctf_trace_put(trace); |
d3e2ba59 | 1339 | |
7591bab1 | 1340 | send_reply: |
53efb85a | 1341 | memset(&reply, 0, sizeof(reply)); |
7591bab1 MD |
1342 | reply.handle = htobe64(stream_handle); |
1343 | if (!stream) { | |
f73fabfd | 1344 | reply.ret_code = htobe32(LTTNG_ERR_UNK); |
b8aa1682 | 1345 | } else { |
f73fabfd | 1346 | reply.ret_code = htobe32(LTTNG_OK); |
b8aa1682 | 1347 | } |
5af40280 | 1348 | |
58eb9381 | 1349 | send_ret = conn->sock->ops->sendmsg(conn->sock, &reply, |
b8aa1682 | 1350 | sizeof(struct lttcomm_relayd_status_stream), 0); |
a8cad461 JG |
1351 | if (send_ret < (ssize_t) sizeof(reply)) { |
1352 | ERR("Failed to send \"add stream\" command reply (ret = %zd)", | |
1353 | send_ret); | |
1354 | ret = -1; | |
b8aa1682 JD |
1355 | } |
1356 | ||
1357 | end_no_session: | |
7591bab1 MD |
1358 | free(path_name); |
1359 | free(channel_name); | |
0f907de1 | 1360 | return ret; |
b8aa1682 JD |
1361 | } |
1362 | ||
173af62f DG |
1363 | /* |
1364 | * relay_close_stream: close a specific stream | |
1365 | */ | |
a8cad461 JG |
1366 | static int relay_close_stream(const struct lttcomm_relayd_hdr *recv_hdr, |
1367 | struct relay_connection *conn, | |
1368 | const struct lttng_buffer_view *payload) | |
173af62f | 1369 | { |
a8cad461 JG |
1370 | int ret; |
1371 | ssize_t send_ret; | |
58eb9381 | 1372 | struct relay_session *session = conn->session; |
173af62f DG |
1373 | struct lttcomm_relayd_close_stream stream_info; |
1374 | struct lttcomm_relayd_generic_reply reply; | |
1375 | struct relay_stream *stream; | |
173af62f DG |
1376 | |
1377 | DBG("Close stream received"); | |
1378 | ||
a8cad461 | 1379 | if (!session || !conn->version_check_done) { |
173af62f DG |
1380 | ERR("Trying to close a stream before version check"); |
1381 | ret = -1; | |
1382 | goto end_no_session; | |
1383 | } | |
1384 | ||
a8cad461 JG |
1385 | if (payload->size < sizeof(stream_info)) { |
1386 | ERR("Unexpected payload size in \"relay_close_stream\": expected >= %zu bytes, got %zu bytes", | |
1387 | sizeof(stream_info), payload->size); | |
173af62f DG |
1388 | ret = -1; |
1389 | goto end_no_session; | |
1390 | } | |
a8cad461 JG |
1391 | memcpy(&stream_info, payload->data, sizeof(stream_info)); |
1392 | stream_info.stream_id = be64toh(stream_info.stream_id); | |
1393 | stream_info.last_net_seq_num = be64toh(stream_info.last_net_seq_num); | |
173af62f | 1394 | |
a8cad461 | 1395 | stream = stream_get_by_id(stream_info.stream_id); |
173af62f DG |
1396 | if (!stream) { |
1397 | ret = -1; | |
7591bab1 | 1398 | goto end; |
173af62f | 1399 | } |
77f7bd85 MD |
1400 | |
1401 | /* | |
1402 | * Set last_net_seq_num before the close flag. Required by data | |
1403 | * pending check. | |
1404 | */ | |
7591bab1 | 1405 | pthread_mutex_lock(&stream->lock); |
a8cad461 | 1406 | stream->last_net_seq_num = stream_info.last_net_seq_num; |
77f7bd85 MD |
1407 | pthread_mutex_unlock(&stream->lock); |
1408 | ||
bda7c7b9 JG |
1409 | /* |
1410 | * This is one of the conditions which may trigger a stream close | |
1411 | * with the others being: | |
1412 | * 1) A close command is received for a stream | |
1413 | * 2) The control connection owning the stream is closed | |
1414 | * 3) We have received all of the stream's data _after_ a close | |
1415 | * request. | |
1416 | */ | |
1417 | try_stream_close(stream); | |
7591bab1 MD |
1418 | if (stream->is_metadata) { |
1419 | struct relay_viewer_stream *vstream; | |
173af62f | 1420 | |
7591bab1 MD |
1421 | vstream = viewer_stream_get_by_id(stream->stream_handle); |
1422 | if (vstream) { | |
1423 | if (vstream->metadata_sent == stream->metadata_received) { | |
1424 | /* | |
1425 | * Since all the metadata has been sent to the | |
1426 | * viewer and that we have a request to close | |
1427 | * its stream, we can safely teardown the | |
1428 | * corresponding metadata viewer stream. | |
1429 | */ | |
1430 | viewer_stream_put(vstream); | |
1431 | } | |
1432 | /* Put local reference. */ | |
1433 | viewer_stream_put(vstream); | |
1434 | } | |
1435 | } | |
7591bab1 | 1436 | stream_put(stream); |
a8cad461 | 1437 | ret = 0; |
173af62f | 1438 | |
7591bab1 | 1439 | end: |
53efb85a | 1440 | memset(&reply, 0, sizeof(reply)); |
173af62f | 1441 | if (ret < 0) { |
f73fabfd | 1442 | reply.ret_code = htobe32(LTTNG_ERR_UNK); |
173af62f | 1443 | } else { |
f73fabfd | 1444 | reply.ret_code = htobe32(LTTNG_OK); |
173af62f | 1445 | } |
58eb9381 | 1446 | send_ret = conn->sock->ops->sendmsg(conn->sock, &reply, |
173af62f | 1447 | sizeof(struct lttcomm_relayd_generic_reply), 0); |
a8cad461 JG |
1448 | if (send_ret < (ssize_t) sizeof(reply)) { |
1449 | ERR("Failed to send \"close stream\" command reply (ret = %zd)", | |
1450 | send_ret); | |
1451 | ret = -1; | |
173af62f DG |
1452 | } |
1453 | ||
1454 | end_no_session: | |
1455 | return ret; | |
1456 | } | |
1457 | ||
93ec662e JD |
1458 | /* |
1459 | * relay_reset_metadata: reset a metadata stream | |
1460 | */ | |
1461 | static | |
a8cad461 JG |
1462 | int relay_reset_metadata(const struct lttcomm_relayd_hdr *recv_hdr, |
1463 | struct relay_connection *conn, | |
1464 | const struct lttng_buffer_view *payload) | |
93ec662e | 1465 | { |
a8cad461 JG |
1466 | int ret; |
1467 | ssize_t send_ret; | |
93ec662e JD |
1468 | struct relay_session *session = conn->session; |
1469 | struct lttcomm_relayd_reset_metadata stream_info; | |
1470 | struct lttcomm_relayd_generic_reply reply; | |
1471 | struct relay_stream *stream; | |
1472 | ||
1473 | DBG("Reset metadata received"); | |
1474 | ||
a8cad461 | 1475 | if (!session || !conn->version_check_done) { |
93ec662e JD |
1476 | ERR("Trying to reset a metadata stream before version check"); |
1477 | ret = -1; | |
1478 | goto end_no_session; | |
1479 | } | |
1480 | ||
a8cad461 JG |
1481 | if (payload->size < sizeof(stream_info)) { |
1482 | ERR("Unexpected payload size in \"relay_reset_metadata\": expected >= %zu bytes, got %zu bytes", | |
1483 | sizeof(stream_info), payload->size); | |
93ec662e JD |
1484 | ret = -1; |
1485 | goto end_no_session; | |
1486 | } | |
a8cad461 JG |
1487 | memcpy(&stream_info, payload->data, sizeof(stream_info)); |
1488 | stream_info.stream_id = be64toh(stream_info.stream_id); | |
1489 | stream_info.version = be64toh(stream_info.version); | |
1490 | ||
1491 | DBG("Update metadata to version %" PRIu64, stream_info.version); | |
93ec662e JD |
1492 | |
1493 | /* Unsupported for live sessions for now. */ | |
1494 | if (session->live_timer != 0) { | |
1495 | ret = -1; | |
1496 | goto end; | |
1497 | } | |
1498 | ||
a8cad461 | 1499 | stream = stream_get_by_id(stream_info.stream_id); |
93ec662e JD |
1500 | if (!stream) { |
1501 | ret = -1; | |
1502 | goto end; | |
1503 | } | |
1504 | pthread_mutex_lock(&stream->lock); | |
1505 | if (!stream->is_metadata) { | |
1506 | ret = -1; | |
1507 | goto end_unlock; | |
1508 | } | |
1509 | ||
1510 | ret = utils_rotate_stream_file(stream->path_name, stream->channel_name, | |
1511 | 0, 0, -1, -1, stream->stream_fd->fd, NULL, | |
1512 | &stream->stream_fd->fd); | |
1513 | if (ret < 0) { | |
1514 | ERR("Failed to rotate metadata file %s of channel %s", | |
1515 | stream->path_name, stream->channel_name); | |
1516 | goto end_unlock; | |
1517 | } | |
1518 | ||
1519 | end_unlock: | |
1520 | pthread_mutex_unlock(&stream->lock); | |
1521 | stream_put(stream); | |
1522 | ||
1523 | end: | |
1524 | memset(&reply, 0, sizeof(reply)); | |
1525 | if (ret < 0) { | |
1526 | reply.ret_code = htobe32(LTTNG_ERR_UNK); | |
1527 | } else { | |
1528 | reply.ret_code = htobe32(LTTNG_OK); | |
1529 | } | |
1530 | send_ret = conn->sock->ops->sendmsg(conn->sock, &reply, | |
1531 | sizeof(struct lttcomm_relayd_generic_reply), 0); | |
a8cad461 JG |
1532 | if (send_ret < (ssize_t) sizeof(reply)) { |
1533 | ERR("Failed to send \"reset metadata\" command reply (ret = %zd)", | |
1534 | send_ret); | |
1535 | ret = -1; | |
93ec662e JD |
1536 | } |
1537 | ||
1538 | end_no_session: | |
1539 | return ret; | |
1540 | } | |
1541 | ||
b8aa1682 JD |
1542 | /* |
1543 | * relay_unknown_command: send -1 if received unknown command | |
1544 | */ | |
7591bab1 | 1545 | static void relay_unknown_command(struct relay_connection *conn) |
b8aa1682 JD |
1546 | { |
1547 | struct lttcomm_relayd_generic_reply reply; | |
a8cad461 | 1548 | ssize_t send_ret; |
b8aa1682 | 1549 | |
53efb85a | 1550 | memset(&reply, 0, sizeof(reply)); |
f73fabfd | 1551 | reply.ret_code = htobe32(LTTNG_ERR_UNK); |
a8cad461 JG |
1552 | send_ret = conn->sock->ops->sendmsg(conn->sock, &reply, sizeof(reply), 0); |
1553 | if (send_ret < sizeof(reply)) { | |
1554 | ERR("Failed to send \"unknown command\" command reply (ret = %zd)", send_ret); | |
b8aa1682 JD |
1555 | } |
1556 | } | |
1557 | ||
1558 | /* | |
1559 | * relay_start: send an acknowledgment to the client to tell if we are | |
1560 | * ready to receive data. We are ready if a session is established. | |
1561 | */ | |
a8cad461 JG |
1562 | static int relay_start(const struct lttcomm_relayd_hdr *recv_hdr, |
1563 | struct relay_connection *conn, | |
1564 | const struct lttng_buffer_view *payload) | |
b8aa1682 | 1565 | { |
a8cad461 JG |
1566 | int ret = 0; |
1567 | ssize_t send_ret; | |
b8aa1682 | 1568 | struct lttcomm_relayd_generic_reply reply; |
58eb9381 | 1569 | struct relay_session *session = conn->session; |
b8aa1682 JD |
1570 | |
1571 | if (!session) { | |
1572 | DBG("Trying to start the streaming without a session established"); | |
f73fabfd | 1573 | ret = htobe32(LTTNG_ERR_UNK); |
b8aa1682 JD |
1574 | } |
1575 | ||
53efb85a | 1576 | memset(&reply, 0, sizeof(reply)); |
a8cad461 JG |
1577 | reply.ret_code = htobe32(LTTNG_OK); |
1578 | send_ret = conn->sock->ops->sendmsg(conn->sock, &reply, | |
1579 | sizeof(reply), 0); | |
1580 | if (send_ret < (ssize_t) sizeof(reply)) { | |
1581 | ERR("Failed to send \"relay_start\" command reply (ret = %zd)", | |
1582 | send_ret); | |
1583 | ret = -1; | |
b8aa1682 JD |
1584 | } |
1585 | ||
1586 | return ret; | |
1587 | } | |
1588 | ||
1d4dfdef DG |
1589 | /* |
1590 | * Append padding to the file pointed by the file descriptor fd. | |
1591 | */ | |
1592 | static int write_padding_to_file(int fd, uint32_t size) | |
1593 | { | |
6cd525e8 | 1594 | ssize_t ret = 0; |
1d4dfdef DG |
1595 | char *zeros; |
1596 | ||
1597 | if (size == 0) { | |
1598 | goto end; | |
1599 | } | |
1600 | ||
1601 | zeros = zmalloc(size); | |
1602 | if (zeros == NULL) { | |
1603 | PERROR("zmalloc zeros for padding"); | |
1604 | ret = -1; | |
1605 | goto end; | |
1606 | } | |
1607 | ||
6cd525e8 MD |
1608 | ret = lttng_write(fd, zeros, size); |
1609 | if (ret < size) { | |
1d4dfdef DG |
1610 | PERROR("write padding to file"); |
1611 | } | |
1612 | ||
e986c7a1 DG |
1613 | free(zeros); |
1614 | ||
1d4dfdef DG |
1615 | end: |
1616 | return ret; | |
1617 | } | |
1618 | ||
b8aa1682 | 1619 | /* |
7591bab1 | 1620 | * relay_recv_metadata: receive the metadata for the session. |
b8aa1682 | 1621 | */ |
a8cad461 JG |
1622 | static int relay_recv_metadata(const struct lttcomm_relayd_hdr *recv_hdr, |
1623 | struct relay_connection *conn, | |
1624 | const struct lttng_buffer_view *payload) | |
b8aa1682 | 1625 | { |
32d1569c | 1626 | int ret = 0; |
6cd525e8 | 1627 | ssize_t size_ret; |
58eb9381 | 1628 | struct relay_session *session = conn->session; |
a8cad461 | 1629 | struct lttcomm_relayd_metadata_payload metadata_payload_header; |
b8aa1682 | 1630 | struct relay_stream *metadata_stream; |
a8cad461 | 1631 | uint64_t metadata_payload_size; |
b8aa1682 JD |
1632 | |
1633 | if (!session) { | |
1634 | ERR("Metadata sent before version check"); | |
1635 | ret = -1; | |
1636 | goto end; | |
1637 | } | |
1638 | ||
a8cad461 | 1639 | if (recv_hdr->data_size < sizeof(struct lttcomm_relayd_metadata_payload)) { |
f6416125 MD |
1640 | ERR("Incorrect data size"); |
1641 | ret = -1; | |
1642 | goto end; | |
1643 | } | |
a8cad461 JG |
1644 | metadata_payload_size = recv_hdr->data_size - |
1645 | sizeof(struct lttcomm_relayd_metadata_payload); | |
f6416125 | 1646 | |
a8cad461 JG |
1647 | memcpy(&metadata_payload_header, payload->data, |
1648 | sizeof(metadata_payload_header)); | |
1649 | metadata_payload_header.stream_id = be64toh( | |
1650 | metadata_payload_header.stream_id); | |
1651 | metadata_payload_header.padding_size = be32toh( | |
1652 | metadata_payload_header.padding_size); | |
c617c0c6 | 1653 | |
a8cad461 | 1654 | metadata_stream = stream_get_by_id(metadata_payload_header.stream_id); |
b8aa1682 JD |
1655 | if (!metadata_stream) { |
1656 | ret = -1; | |
7591bab1 | 1657 | goto end; |
b8aa1682 JD |
1658 | } |
1659 | ||
7591bab1 MD |
1660 | pthread_mutex_lock(&metadata_stream->lock); |
1661 | ||
a8cad461 JG |
1662 | size_ret = lttng_write(metadata_stream->stream_fd->fd, |
1663 | payload->data + sizeof(metadata_payload_header), | |
1664 | metadata_payload_size); | |
1665 | if (size_ret < metadata_payload_size) { | |
b8aa1682 JD |
1666 | ERR("Relay error writing metadata on file"); |
1667 | ret = -1; | |
7591bab1 | 1668 | goto end_put; |
b8aa1682 | 1669 | } |
1d4dfdef | 1670 | |
32d1569c | 1671 | size_ret = write_padding_to_file(metadata_stream->stream_fd->fd, |
a8cad461 JG |
1672 | metadata_payload_header.padding_size); |
1673 | if (size_ret < (int64_t) metadata_payload_header.padding_size) { | |
1674 | ret = -1; | |
7591bab1 | 1675 | goto end_put; |
1d4dfdef | 1676 | } |
2a174661 | 1677 | |
7591bab1 | 1678 | metadata_stream->metadata_received += |
a8cad461 | 1679 | metadata_payload_size + metadata_payload_header.padding_size; |
7591bab1 MD |
1680 | DBG2("Relay metadata written. Updated metadata_received %" PRIu64, |
1681 | metadata_stream->metadata_received); | |
1d4dfdef | 1682 | |
7591bab1 MD |
1683 | end_put: |
1684 | pthread_mutex_unlock(&metadata_stream->lock); | |
1685 | stream_put(metadata_stream); | |
b8aa1682 JD |
1686 | end: |
1687 | return ret; | |
1688 | } | |
1689 | ||
1690 | /* | |
1691 | * relay_send_version: send relayd version number | |
1692 | */ | |
a8cad461 JG |
1693 | static int relay_send_version(const struct lttcomm_relayd_hdr *recv_hdr, |
1694 | struct relay_connection *conn, | |
1695 | const struct lttng_buffer_view *payload) | |
b8aa1682 | 1696 | { |
7f51dcba | 1697 | int ret; |
a8cad461 | 1698 | ssize_t send_ret; |
092b6259 | 1699 | struct lttcomm_relayd_version reply, msg; |
40096142 | 1700 | bool compatible = true; |
b8aa1682 | 1701 | |
a8cad461 | 1702 | conn->version_check_done = true; |
b8aa1682 | 1703 | |
092b6259 | 1704 | /* Get version from the other side. */ |
a8cad461 JG |
1705 | if (payload->size < sizeof(msg)) { |
1706 | ERR("Unexpected payload size in \"relay_send_version\": expected >= %zu bytes, got %zu bytes", | |
1707 | sizeof(msg), payload->size); | |
092b6259 | 1708 | ret = -1; |
092b6259 DG |
1709 | goto end; |
1710 | } | |
1711 | ||
a8cad461 JG |
1712 | memcpy(&msg, payload->data, sizeof(msg)); |
1713 | msg.major = be32toh(msg.major); | |
1714 | msg.minor = be32toh(msg.minor); | |
1715 | ||
53efb85a | 1716 | memset(&reply, 0, sizeof(reply)); |
d83a952c MD |
1717 | reply.major = RELAYD_VERSION_COMM_MAJOR; |
1718 | reply.minor = RELAYD_VERSION_COMM_MINOR; | |
d4519fa3 JD |
1719 | |
1720 | /* Major versions must be the same */ | |
a8cad461 | 1721 | if (reply.major != msg.major) { |
6151a90f | 1722 | DBG("Incompatible major versions (%u vs %u), deleting session", |
a8cad461 | 1723 | reply.major, msg.major); |
40096142 | 1724 | compatible = false; |
d4519fa3 JD |
1725 | } |
1726 | ||
58eb9381 | 1727 | conn->major = reply.major; |
0f907de1 | 1728 | /* We adapt to the lowest compatible version */ |
a8cad461 | 1729 | if (reply.minor <= msg.minor) { |
58eb9381 | 1730 | conn->minor = reply.minor; |
0f907de1 | 1731 | } else { |
a8cad461 | 1732 | conn->minor = msg.minor; |
0f907de1 JD |
1733 | } |
1734 | ||
6151a90f JD |
1735 | reply.major = htobe32(reply.major); |
1736 | reply.minor = htobe32(reply.minor); | |
a8cad461 JG |
1737 | send_ret = conn->sock->ops->sendmsg(conn->sock, &reply, |
1738 | sizeof(reply), 0); | |
1739 | if (send_ret < (ssize_t) sizeof(reply)) { | |
1740 | ERR("Failed to send \"send version\" command reply (ret = %zd)", | |
1741 | send_ret); | |
1742 | ret = -1; | |
1743 | goto end; | |
1744 | } else { | |
1745 | ret = 0; | |
6151a90f JD |
1746 | } |
1747 | ||
40096142 JD |
1748 | if (!compatible) { |
1749 | ret = -1; | |
1750 | goto end; | |
1751 | } | |
1752 | ||
58eb9381 DG |
1753 | DBG("Version check done using protocol %u.%u", conn->major, |
1754 | conn->minor); | |
b8aa1682 JD |
1755 | |
1756 | end: | |
1757 | return ret; | |
1758 | } | |
1759 | ||
c8f59ee5 | 1760 | /* |
6d805429 | 1761 | * Check for data pending for a given stream id from the session daemon. |
c8f59ee5 | 1762 | */ |
a8cad461 JG |
1763 | static int relay_data_pending(const struct lttcomm_relayd_hdr *recv_hdr, |
1764 | struct relay_connection *conn, | |
1765 | const struct lttng_buffer_view *payload) | |
c8f59ee5 | 1766 | { |
58eb9381 | 1767 | struct relay_session *session = conn->session; |
6d805429 | 1768 | struct lttcomm_relayd_data_pending msg; |
c8f59ee5 DG |
1769 | struct lttcomm_relayd_generic_reply reply; |
1770 | struct relay_stream *stream; | |
a8cad461 | 1771 | ssize_t send_ret; |
c8f59ee5 | 1772 | int ret; |
c8f59ee5 | 1773 | |
6d805429 | 1774 | DBG("Data pending command received"); |
c8f59ee5 | 1775 | |
a8cad461 | 1776 | if (!session || !conn->version_check_done) { |
c8f59ee5 DG |
1777 | ERR("Trying to check for data before version check"); |
1778 | ret = -1; | |
1779 | goto end_no_session; | |
1780 | } | |
1781 | ||
a8cad461 JG |
1782 | if (payload->size < sizeof(msg)) { |
1783 | ERR("Unexpected payload size in \"relay_data_pending\": expected >= %zu bytes, got %zu bytes", | |
1784 | sizeof(msg), payload->size); | |
c8f59ee5 DG |
1785 | ret = -1; |
1786 | goto end_no_session; | |
1787 | } | |
a8cad461 JG |
1788 | memcpy(&msg, payload->data, sizeof(msg)); |
1789 | msg.stream_id = be64toh(msg.stream_id); | |
1790 | msg.last_net_seq_num = be64toh(msg.last_net_seq_num); | |
c8f59ee5 | 1791 | |
a8cad461 | 1792 | stream = stream_get_by_id(msg.stream_id); |
de91f48a | 1793 | if (stream == NULL) { |
c8f59ee5 | 1794 | ret = -1; |
7591bab1 | 1795 | goto end; |
c8f59ee5 DG |
1796 | } |
1797 | ||
7591bab1 MD |
1798 | pthread_mutex_lock(&stream->lock); |
1799 | ||
6d805429 | 1800 | DBG("Data pending for stream id %" PRIu64 " prev_seq %" PRIu64 |
a8cad461 JG |
1801 | " and last_seq %" PRIu64, msg.stream_id, |
1802 | stream->prev_seq, msg.last_net_seq_num); | |
c8f59ee5 | 1803 | |
33832e64 | 1804 | /* Avoid wrapping issue */ |
a8cad461 | 1805 | if (((int64_t) (stream->prev_seq - msg.last_net_seq_num)) >= 0) { |
6d805429 | 1806 | /* Data has in fact been written and is NOT pending */ |
c8f59ee5 | 1807 | ret = 0; |
6d805429 DG |
1808 | } else { |
1809 | /* Data still being streamed thus pending */ | |
1810 | ret = 1; | |
c8f59ee5 DG |
1811 | } |
1812 | ||
7591bab1 MD |
1813 | stream->data_pending_check_done = true; |
1814 | pthread_mutex_unlock(&stream->lock); | |
f7079f67 | 1815 | |
7591bab1 MD |
1816 | stream_put(stream); |
1817 | end: | |
c8f59ee5 | 1818 | |
53efb85a | 1819 | memset(&reply, 0, sizeof(reply)); |
c8f59ee5 | 1820 | reply.ret_code = htobe32(ret); |
a8cad461 JG |
1821 | send_ret = conn->sock->ops->sendmsg(conn->sock, &reply, sizeof(reply), 0); |
1822 | if (send_ret < (ssize_t) sizeof(reply)) { | |
1823 | ERR("Failed to send \"data pending\" command reply (ret = %zd)", | |
1824 | send_ret); | |
1825 | ret = -1; | |
c8f59ee5 DG |
1826 | } |
1827 | ||
1828 | end_no_session: | |
1829 | return ret; | |
1830 | } | |
1831 | ||
1832 | /* | |
1833 | * Wait for the control socket to reach a quiescent state. | |
1834 | * | |
7591bab1 MD |
1835 | * Note that for now, when receiving this command from the session |
1836 | * daemon, this means that every subsequent commands or data received on | |
1837 | * the control socket has been handled. So, this is why we simply return | |
1838 | * OK here. | |
c8f59ee5 | 1839 | */ |
a8cad461 JG |
1840 | static int relay_quiescent_control(const struct lttcomm_relayd_hdr *recv_hdr, |
1841 | struct relay_connection *conn, | |
1842 | const struct lttng_buffer_view *payload) | |
c8f59ee5 DG |
1843 | { |
1844 | int ret; | |
a8cad461 | 1845 | ssize_t send_ret; |
ad7051c0 | 1846 | struct relay_stream *stream; |
ad7051c0 | 1847 | struct lttcomm_relayd_quiescent_control msg; |
c8f59ee5 DG |
1848 | struct lttcomm_relayd_generic_reply reply; |
1849 | ||
1850 | DBG("Checking quiescent state on control socket"); | |
1851 | ||
a8cad461 | 1852 | if (!conn->session || !conn->version_check_done) { |
ad7051c0 DG |
1853 | ERR("Trying to check for data before version check"); |
1854 | ret = -1; | |
1855 | goto end_no_session; | |
1856 | } | |
1857 | ||
a8cad461 JG |
1858 | if (payload->size < sizeof(msg)) { |
1859 | ERR("Unexpected payload size in \"relay_quiescent_control\": expected >= %zu bytes, got %zu bytes", | |
1860 | sizeof(msg), payload->size); | |
ad7051c0 DG |
1861 | ret = -1; |
1862 | goto end_no_session; | |
1863 | } | |
a8cad461 JG |
1864 | memcpy(&msg, payload->data, sizeof(msg)); |
1865 | msg.stream_id = be64toh(msg.stream_id); | |
ad7051c0 | 1866 | |
a8cad461 | 1867 | stream = stream_get_by_id(msg.stream_id); |
7591bab1 MD |
1868 | if (!stream) { |
1869 | goto reply; | |
1870 | } | |
1871 | pthread_mutex_lock(&stream->lock); | |
1872 | stream->data_pending_check_done = true; | |
1873 | pthread_mutex_unlock(&stream->lock); | |
a8cad461 JG |
1874 | |
1875 | DBG("Relay quiescent control pending flag set to %" PRIu64, msg.stream_id); | |
7591bab1 MD |
1876 | stream_put(stream); |
1877 | reply: | |
53efb85a | 1878 | memset(&reply, 0, sizeof(reply)); |
c8f59ee5 | 1879 | reply.ret_code = htobe32(LTTNG_OK); |
a8cad461 JG |
1880 | send_ret = conn->sock->ops->sendmsg(conn->sock, &reply, sizeof(reply), 0); |
1881 | if (send_ret < (ssize_t) sizeof(reply)) { | |
1882 | ERR("Failed to send \"quiescent control\" command reply (ret = %zd)", | |
1883 | send_ret); | |
1884 | ret = -1; | |
1885 | } else { | |
1886 | ret = 0; | |
c8f59ee5 DG |
1887 | } |
1888 | ||
ad7051c0 | 1889 | end_no_session: |
c8f59ee5 DG |
1890 | return ret; |
1891 | } | |
1892 | ||
f7079f67 | 1893 | /* |
7591bab1 MD |
1894 | * Initialize a data pending command. This means that a consumer is about |
1895 | * to ask for data pending for each stream it holds. Simply iterate over | |
1896 | * all streams of a session and set the data_pending_check_done flag. | |
f7079f67 DG |
1897 | * |
1898 | * This command returns to the client a LTTNG_OK code. | |
1899 | */ | |
a8cad461 JG |
1900 | static int relay_begin_data_pending(const struct lttcomm_relayd_hdr *recv_hdr, |
1901 | struct relay_connection *conn, | |
1902 | const struct lttng_buffer_view *payload) | |
f7079f67 DG |
1903 | { |
1904 | int ret; | |
a8cad461 | 1905 | ssize_t send_ret; |
f7079f67 DG |
1906 | struct lttng_ht_iter iter; |
1907 | struct lttcomm_relayd_begin_data_pending msg; | |
1908 | struct lttcomm_relayd_generic_reply reply; | |
1909 | struct relay_stream *stream; | |
f7079f67 DG |
1910 | |
1911 | assert(recv_hdr); | |
58eb9381 | 1912 | assert(conn); |
f7079f67 DG |
1913 | |
1914 | DBG("Init streams for data pending"); | |
1915 | ||
a8cad461 | 1916 | if (!conn->session || !conn->version_check_done) { |
f7079f67 DG |
1917 | ERR("Trying to check for data before version check"); |
1918 | ret = -1; | |
1919 | goto end_no_session; | |
1920 | } | |
1921 | ||
a8cad461 JG |
1922 | if (payload->size < sizeof(msg)) { |
1923 | ERR("Unexpected payload size in \"relay_begin_data_pending\": expected >= %zu bytes, got %zu bytes", | |
1924 | sizeof(msg), payload->size); | |
f7079f67 DG |
1925 | ret = -1; |
1926 | goto end_no_session; | |
1927 | } | |
a8cad461 JG |
1928 | memcpy(&msg, payload->data, sizeof(msg)); |
1929 | msg.session_id = be64toh(msg.session_id); | |
f7079f67 DG |
1930 | |
1931 | /* | |
7591bab1 MD |
1932 | * Iterate over all streams to set the begin data pending flag. |
1933 | * For now, the streams are indexed by stream handle so we have | |
1934 | * to iterate over all streams to find the one associated with | |
1935 | * the right session_id. | |
f7079f67 DG |
1936 | */ |
1937 | rcu_read_lock(); | |
d3e2ba59 | 1938 | cds_lfht_for_each_entry(relay_streams_ht->ht, &iter.iter, stream, |
2a174661 | 1939 | node.node) { |
7591bab1 MD |
1940 | if (!stream_get(stream)) { |
1941 | continue; | |
1942 | } | |
a8cad461 | 1943 | if (stream->trace->session->id == msg.session_id) { |
7591bab1 MD |
1944 | pthread_mutex_lock(&stream->lock); |
1945 | stream->data_pending_check_done = false; | |
1946 | pthread_mutex_unlock(&stream->lock); | |
f7079f67 DG |
1947 | DBG("Set begin data pending flag to stream %" PRIu64, |
1948 | stream->stream_handle); | |
1949 | } | |
7591bab1 | 1950 | stream_put(stream); |
f7079f67 DG |
1951 | } |
1952 | rcu_read_unlock(); | |
1953 | ||
53efb85a | 1954 | memset(&reply, 0, sizeof(reply)); |
f7079f67 DG |
1955 | /* All good, send back reply. */ |
1956 | reply.ret_code = htobe32(LTTNG_OK); | |
1957 | ||
a8cad461 JG |
1958 | send_ret = conn->sock->ops->sendmsg(conn->sock, &reply, sizeof(reply), 0); |
1959 | if (send_ret < (ssize_t) sizeof(reply)) { | |
1960 | ERR("Failed to send \"begin data pending\" command reply (ret = %zd)", | |
1961 | send_ret); | |
1962 | ret = -1; | |
1963 | } else { | |
1964 | ret = 0; | |
f7079f67 DG |
1965 | } |
1966 | ||
1967 | end_no_session: | |
1968 | return ret; | |
1969 | } | |
1970 | ||
1971 | /* | |
7591bab1 MD |
1972 | * End data pending command. This will check, for a given session id, if |
1973 | * each stream associated with it has its data_pending_check_done flag | |
1974 | * set. If not, this means that the client lost track of the stream but | |
1975 | * the data is still being streamed on our side. In this case, we inform | |
1976 | * the client that data is in flight. | |
f7079f67 DG |
1977 | * |
1978 | * Return to the client if there is data in flight or not with a ret_code. | |
1979 | */ | |
a8cad461 JG |
1980 | static int relay_end_data_pending(const struct lttcomm_relayd_hdr *recv_hdr, |
1981 | struct relay_connection *conn, | |
1982 | const struct lttng_buffer_view *payload) | |
f7079f67 DG |
1983 | { |
1984 | int ret; | |
a8cad461 | 1985 | ssize_t send_ret; |
f7079f67 DG |
1986 | struct lttng_ht_iter iter; |
1987 | struct lttcomm_relayd_end_data_pending msg; | |
1988 | struct lttcomm_relayd_generic_reply reply; | |
1989 | struct relay_stream *stream; | |
f7079f67 DG |
1990 | uint32_t is_data_inflight = 0; |
1991 | ||
f7079f67 DG |
1992 | DBG("End data pending command"); |
1993 | ||
a8cad461 | 1994 | if (!conn->session || !conn->version_check_done) { |
f7079f67 DG |
1995 | ERR("Trying to check for data before version check"); |
1996 | ret = -1; | |
1997 | goto end_no_session; | |
1998 | } | |
1999 | ||
a8cad461 JG |
2000 | if (payload->size < sizeof(msg)) { |
2001 | ERR("Unexpected payload size in \"relay_end_data_pending\": expected >= %zu bytes, got %zu bytes", | |
2002 | sizeof(msg), payload->size); | |
f7079f67 DG |
2003 | ret = -1; |
2004 | goto end_no_session; | |
2005 | } | |
a8cad461 JG |
2006 | memcpy(&msg, payload->data, sizeof(msg)); |
2007 | msg.session_id = be64toh(msg.session_id); | |
f7079f67 | 2008 | |
7591bab1 MD |
2009 | /* |
2010 | * Iterate over all streams to see if the begin data pending | |
2011 | * flag is set. | |
2012 | */ | |
f7079f67 | 2013 | rcu_read_lock(); |
d3e2ba59 | 2014 | cds_lfht_for_each_entry(relay_streams_ht->ht, &iter.iter, stream, |
2a174661 | 2015 | node.node) { |
7591bab1 MD |
2016 | if (!stream_get(stream)) { |
2017 | continue; | |
2018 | } | |
a8cad461 | 2019 | if (stream->trace->session->id != msg.session_id) { |
7591bab1 MD |
2020 | stream_put(stream); |
2021 | continue; | |
2022 | } | |
2023 | pthread_mutex_lock(&stream->lock); | |
2024 | if (!stream->data_pending_check_done) { | |
2025 | if (!stream->closed || !(((int64_t) (stream->prev_seq - stream->last_net_seq_num)) >= 0)) { | |
2026 | is_data_inflight = 1; | |
2027 | DBG("Data is still in flight for stream %" PRIu64, | |
2028 | stream->stream_handle); | |
2029 | pthread_mutex_unlock(&stream->lock); | |
2030 | stream_put(stream); | |
2031 | break; | |
2032 | } | |
f7079f67 | 2033 | } |
7591bab1 MD |
2034 | pthread_mutex_unlock(&stream->lock); |
2035 | stream_put(stream); | |
f7079f67 DG |
2036 | } |
2037 | rcu_read_unlock(); | |
2038 | ||
53efb85a | 2039 | memset(&reply, 0, sizeof(reply)); |
f7079f67 DG |
2040 | /* All good, send back reply. */ |
2041 | reply.ret_code = htobe32(is_data_inflight); | |
2042 | ||
a8cad461 JG |
2043 | send_ret = conn->sock->ops->sendmsg(conn->sock, &reply, sizeof(reply), 0); |
2044 | if (send_ret < (ssize_t) sizeof(reply)) { | |
2045 | ERR("Failed to send \"end data pending\" command reply (ret = %zd)", | |
2046 | send_ret); | |
2047 | ret = -1; | |
2048 | } else { | |
2049 | ret = 0; | |
f7079f67 DG |
2050 | } |
2051 | ||
2052 | end_no_session: | |
2053 | return ret; | |
2054 | } | |
2055 | ||
1c20f0e2 JD |
2056 | /* |
2057 | * Receive an index for a specific stream. | |
2058 | * | |
2059 | * Return 0 on success else a negative value. | |
2060 | */ | |
a8cad461 JG |
2061 | static int relay_recv_index(const struct lttcomm_relayd_hdr *recv_hdr, |
2062 | struct relay_connection *conn, | |
2063 | const struct lttng_buffer_view *payload) | |
1c20f0e2 | 2064 | { |
a8cad461 JG |
2065 | int ret; |
2066 | ssize_t send_ret; | |
58eb9381 | 2067 | struct relay_session *session = conn->session; |
1c20f0e2 | 2068 | struct lttcomm_relayd_index index_info; |
7591bab1 | 2069 | struct relay_index *index; |
1c20f0e2 JD |
2070 | struct lttcomm_relayd_generic_reply reply; |
2071 | struct relay_stream *stream; | |
e0547b83 | 2072 | size_t msg_len; |
1c20f0e2 | 2073 | |
58eb9381 | 2074 | assert(conn); |
1c20f0e2 JD |
2075 | |
2076 | DBG("Relay receiving index"); | |
2077 | ||
a8cad461 | 2078 | if (!session || !conn->version_check_done) { |
1c20f0e2 JD |
2079 | ERR("Trying to close a stream before version check"); |
2080 | ret = -1; | |
2081 | goto end_no_session; | |
2082 | } | |
2083 | ||
e0547b83 MD |
2084 | msg_len = lttcomm_relayd_index_len( |
2085 | lttng_to_index_major(conn->major, conn->minor), | |
2086 | lttng_to_index_minor(conn->major, conn->minor)); | |
a8cad461 JG |
2087 | if (payload->size < msg_len) { |
2088 | ERR("Unexpected payload size in \"relay_recv_index\": expected >= %zu bytes, got %zu bytes", | |
2089 | msg_len, payload->size); | |
1c20f0e2 JD |
2090 | ret = -1; |
2091 | goto end_no_session; | |
2092 | } | |
a8cad461 JG |
2093 | memcpy(&index_info, payload->data, msg_len); |
2094 | index_info.relay_stream_id = be64toh(index_info.relay_stream_id); | |
2095 | index_info.net_seq_num = be64toh(index_info.net_seq_num); | |
2096 | index_info.packet_size = be64toh(index_info.packet_size); | |
2097 | index_info.content_size = be64toh(index_info.content_size); | |
2098 | index_info.timestamp_begin = be64toh(index_info.timestamp_begin); | |
2099 | index_info.timestamp_end = be64toh(index_info.timestamp_end); | |
2100 | index_info.events_discarded = be64toh(index_info.events_discarded); | |
2101 | index_info.stream_id = be64toh(index_info.stream_id); | |
1c20f0e2 | 2102 | |
a8cad461 JG |
2103 | if (conn->minor >= 8) { |
2104 | index_info.stream_instance_id = | |
2105 | be64toh(index_info.stream_instance_id); | |
2106 | index_info.packet_seq_num = be64toh(index_info.packet_seq_num); | |
2107 | } | |
1c20f0e2 | 2108 | |
a8cad461 | 2109 | stream = stream_get_by_id(index_info.relay_stream_id); |
1c20f0e2 | 2110 | if (!stream) { |
7591bab1 | 2111 | ERR("stream_get_by_id not found"); |
1c20f0e2 | 2112 | ret = -1; |
7591bab1 | 2113 | goto end; |
1c20f0e2 | 2114 | } |
7591bab1 | 2115 | pthread_mutex_lock(&stream->lock); |
1c20f0e2 | 2116 | |
d3e2ba59 JD |
2117 | /* Live beacon handling */ |
2118 | if (index_info.packet_size == 0) { | |
7591bab1 MD |
2119 | DBG("Received live beacon for stream %" PRIu64, |
2120 | stream->stream_handle); | |
d3e2ba59 JD |
2121 | |
2122 | /* | |
7591bab1 MD |
2123 | * Only flag a stream inactive when it has already |
2124 | * received data and no indexes are in flight. | |
d3e2ba59 | 2125 | */ |
a44ca2ca | 2126 | if (stream->index_received_seqcount > 0 |
7591bab1 | 2127 | && stream->indexes_in_flight == 0) { |
a8cad461 | 2128 | stream->beacon_ts_end = index_info.timestamp_end; |
d3e2ba59 JD |
2129 | } |
2130 | ret = 0; | |
7591bab1 | 2131 | goto end_stream_put; |
d3e2ba59 JD |
2132 | } else { |
2133 | stream->beacon_ts_end = -1ULL; | |
2134 | } | |
2135 | ||
528f2ffa | 2136 | if (stream->ctf_stream_id == -1ULL) { |
a8cad461 | 2137 | stream->ctf_stream_id = index_info.stream_id; |
528f2ffa | 2138 | } |
a8cad461 | 2139 | index = relay_index_get_by_id_or_create(stream, index_info.net_seq_num); |
7591bab1 MD |
2140 | if (!index) { |
2141 | ret = -1; | |
2142 | ERR("relay_index_get_by_id_or_create index NULL"); | |
2143 | goto end_stream_put; | |
1c20f0e2 | 2144 | } |
234cd636 | 2145 | if (set_index_control_data(index, &index_info, conn)) { |
7591bab1 MD |
2146 | ERR("set_index_control_data error"); |
2147 | relay_index_put(index); | |
2148 | ret = -1; | |
2149 | goto end_stream_put; | |
2150 | } | |
2151 | ret = relay_index_try_flush(index); | |
2152 | if (ret == 0) { | |
a44ca2ca MD |
2153 | tracefile_array_commit_seq(stream->tfa); |
2154 | stream->index_received_seqcount++; | |
7591bab1 MD |
2155 | } else if (ret > 0) { |
2156 | /* no flush. */ | |
2157 | ret = 0; | |
2158 | } else { | |
2159 | ERR("relay_index_try_flush error %d", ret); | |
2160 | relay_index_put(index); | |
2161 | ret = -1; | |
1c20f0e2 JD |
2162 | } |
2163 | ||
7591bab1 MD |
2164 | end_stream_put: |
2165 | pthread_mutex_unlock(&stream->lock); | |
2166 | stream_put(stream); | |
2167 | ||
2168 | end: | |
1c20f0e2 | 2169 | |
53efb85a | 2170 | memset(&reply, 0, sizeof(reply)); |
1c20f0e2 JD |
2171 | if (ret < 0) { |
2172 | reply.ret_code = htobe32(LTTNG_ERR_UNK); | |
2173 | } else { | |
2174 | reply.ret_code = htobe32(LTTNG_OK); | |
2175 | } | |
58eb9381 | 2176 | send_ret = conn->sock->ops->sendmsg(conn->sock, &reply, sizeof(reply), 0); |
a8cad461 JG |
2177 | if (send_ret < (ssize_t) sizeof(reply)) { |
2178 | ERR("Failed to send \"recv index\" command reply (ret = %zd)", send_ret); | |
2179 | ret = -1; | |
1c20f0e2 JD |
2180 | } |
2181 | ||
2182 | end_no_session: | |
2183 | return ret; | |
2184 | } | |
2185 | ||
a4baae1b JD |
2186 | /* |
2187 | * Receive the streams_sent message. | |
2188 | * | |
2189 | * Return 0 on success else a negative value. | |
2190 | */ | |
a8cad461 JG |
2191 | static int relay_streams_sent(const struct lttcomm_relayd_hdr *recv_hdr, |
2192 | struct relay_connection *conn, | |
2193 | const struct lttng_buffer_view *payload) | |
a4baae1b | 2194 | { |
a8cad461 JG |
2195 | int ret; |
2196 | ssize_t send_ret; | |
a4baae1b JD |
2197 | struct lttcomm_relayd_generic_reply reply; |
2198 | ||
58eb9381 | 2199 | assert(conn); |
a4baae1b JD |
2200 | |
2201 | DBG("Relay receiving streams_sent"); | |
2202 | ||
a8cad461 | 2203 | if (!conn->session || !conn->version_check_done) { |
a4baae1b JD |
2204 | ERR("Trying to close a stream before version check"); |
2205 | ret = -1; | |
2206 | goto end_no_session; | |
2207 | } | |
2208 | ||
2209 | /* | |
7591bab1 MD |
2210 | * Publish every pending stream in the connection recv list which are |
2211 | * now ready to be used by the viewer. | |
4a9daf17 | 2212 | */ |
7591bab1 | 2213 | publish_connection_local_streams(conn); |
4a9daf17 | 2214 | |
53efb85a | 2215 | memset(&reply, 0, sizeof(reply)); |
a4baae1b | 2216 | reply.ret_code = htobe32(LTTNG_OK); |
58eb9381 | 2217 | send_ret = conn->sock->ops->sendmsg(conn->sock, &reply, sizeof(reply), 0); |
a8cad461 JG |
2218 | if (send_ret < (ssize_t) sizeof(reply)) { |
2219 | ERR("Failed to send \"streams sent\" command reply (ret = %zd)", | |
2220 | send_ret); | |
2221 | ret = -1; | |
a4baae1b JD |
2222 | } else { |
2223 | /* Success. */ | |
2224 | ret = 0; | |
2225 | } | |
2226 | ||
2227 | end_no_session: | |
2228 | return ret; | |
2229 | } | |
2230 | ||
a8cad461 JG |
2231 | #define DBG_CMD(cmd_name, conn) \ |
2232 | DBG3("Processing \"%s\" command for socket %i", cmd_name, conn->sock->fd); | |
2233 | ||
2234 | static int relay_process_control_command(struct relay_connection *conn, | |
2235 | const struct lttcomm_relayd_hdr *header, | |
2236 | const struct lttng_buffer_view *payload) | |
b8aa1682 JD |
2237 | { |
2238 | int ret = 0; | |
2239 | ||
a8cad461 | 2240 | switch (header->cmd) { |
b8aa1682 | 2241 | case RELAYD_CREATE_SESSION: |
a8cad461 JG |
2242 | DBG_CMD("RELAYD_CREATE_SESSION", conn); |
2243 | ret = relay_create_session(header, conn, payload); | |
b8aa1682 | 2244 | break; |
b8aa1682 | 2245 | case RELAYD_ADD_STREAM: |
a8cad461 JG |
2246 | DBG_CMD("RELAYD_ADD_STREAM", conn); |
2247 | ret = relay_add_stream(header, conn, payload); | |
b8aa1682 JD |
2248 | break; |
2249 | case RELAYD_START_DATA: | |
a8cad461 JG |
2250 | DBG_CMD("RELAYD_START_DATA", conn); |
2251 | ret = relay_start(header, conn, payload); | |
b8aa1682 JD |
2252 | break; |
2253 | case RELAYD_SEND_METADATA: | |
a8cad461 JG |
2254 | DBG_CMD("RELAYD_SEND_METADATA", conn); |
2255 | ret = relay_recv_metadata(header, conn, payload); | |
b8aa1682 JD |
2256 | break; |
2257 | case RELAYD_VERSION: | |
a8cad461 JG |
2258 | DBG_CMD("RELAYD_VERSION", conn); |
2259 | ret = relay_send_version(header, conn, payload); | |
b8aa1682 | 2260 | break; |
173af62f | 2261 | case RELAYD_CLOSE_STREAM: |
a8cad461 JG |
2262 | DBG_CMD("RELAYD_CLOSE_STREAM", conn); |
2263 | ret = relay_close_stream(header, conn, payload); | |
173af62f | 2264 | break; |
6d805429 | 2265 | case RELAYD_DATA_PENDING: |
a8cad461 JG |
2266 | DBG_CMD("RELAYD_DATA_PENDING", conn); |
2267 | ret = relay_data_pending(header, conn, payload); | |
c8f59ee5 DG |
2268 | break; |
2269 | case RELAYD_QUIESCENT_CONTROL: | |
a8cad461 JG |
2270 | DBG_CMD("RELAYD_QUIESCENT_CONTROL", conn); |
2271 | ret = relay_quiescent_control(header, conn, payload); | |
c8f59ee5 | 2272 | break; |
f7079f67 | 2273 | case RELAYD_BEGIN_DATA_PENDING: |
a8cad461 JG |
2274 | DBG_CMD("RELAYD_BEGIN_DATA_PENDING", conn); |
2275 | ret = relay_begin_data_pending(header, conn, payload); | |
f7079f67 DG |
2276 | break; |
2277 | case RELAYD_END_DATA_PENDING: | |
a8cad461 JG |
2278 | DBG_CMD("RELAYD_END_DATA_PENDING", conn); |
2279 | ret = relay_end_data_pending(header, conn, payload); | |
f7079f67 | 2280 | break; |
1c20f0e2 | 2281 | case RELAYD_SEND_INDEX: |
a8cad461 JG |
2282 | DBG_CMD("RELAYD_SEND_INDEX", conn); |
2283 | ret = relay_recv_index(header, conn, payload); | |
1c20f0e2 | 2284 | break; |
a4baae1b | 2285 | case RELAYD_STREAMS_SENT: |
a8cad461 JG |
2286 | DBG_CMD("RELAYD_STREAMS_SENT", conn); |
2287 | ret = relay_streams_sent(header, conn, payload); | |
a4baae1b | 2288 | break; |
93ec662e | 2289 | case RELAYD_RESET_METADATA: |
a8cad461 JG |
2290 | DBG_CMD("RELAYD_RESET_METADATA", conn); |
2291 | ret = relay_reset_metadata(header, conn, payload); | |
93ec662e | 2292 | break; |
b8aa1682 JD |
2293 | case RELAYD_UPDATE_SYNC_INFO: |
2294 | default: | |
a8cad461 | 2295 | ERR("Received unknown command (%u)", header->cmd); |
58eb9381 | 2296 | relay_unknown_command(conn); |
b8aa1682 JD |
2297 | ret = -1; |
2298 | goto end; | |
2299 | } | |
2300 | ||
2301 | end: | |
2302 | return ret; | |
2303 | } | |
2304 | ||
a8cad461 JG |
2305 | static enum relay_connection_status relay_process_control_receive_payload( |
2306 | struct relay_connection *conn) | |
2307 | { | |
2308 | int ret = 0; | |
2309 | enum relay_connection_status status = RELAY_CONNECTION_STATUS_OK; | |
2310 | struct lttng_dynamic_buffer *reception_buffer = | |
2311 | &conn->protocol.ctrl.reception_buffer; | |
2312 | struct ctrl_connection_state_receive_payload *state = | |
2313 | &conn->protocol.ctrl.state.receive_payload; | |
2314 | struct lttng_buffer_view payload_view; | |
2315 | ||
2316 | if (state->left_to_receive == 0) { | |
2317 | /* Short-circuit for payload-less commands. */ | |
2318 | goto reception_complete; | |
2319 | } | |
2320 | ret = conn->sock->ops->recvmsg(conn->sock, | |
2321 | reception_buffer->data + state->received, | |
2322 | state->left_to_receive, MSG_DONTWAIT); | |
2323 | if (ret < 0) { | |
2324 | if (errno != EAGAIN && errno != EWOULDBLOCK) { | |
2325 | PERROR("Unable to receive command payload on sock %d", | |
2326 | conn->sock->fd); | |
2327 | status = RELAY_CONNECTION_STATUS_ERROR; | |
2328 | } | |
2329 | goto end; | |
2330 | } else if (ret == 0) { | |
2331 | DBG("Socket %d performed an orderly shutdown (received EOF)", conn->sock->fd); | |
2332 | status = RELAY_CONNECTION_STATUS_CLOSED; | |
2333 | goto end; | |
2334 | } | |
2335 | ||
2336 | assert(ret > 0); | |
2337 | assert(ret <= state->left_to_receive); | |
2338 | ||
2339 | state->left_to_receive -= ret; | |
2340 | state->received += ret; | |
2341 | ||
2342 | if (state->left_to_receive > 0) { | |
2343 | /* | |
2344 | * Can't transition to the protocol's next state, wait to | |
2345 | * receive the rest of the header. | |
2346 | */ | |
2347 | DBG3("Partial reception of control connection protocol payload (received %" PRIu64 " bytes, %" PRIu64 " bytes left to receive, fd = %i)", | |
2348 | state->received, state->left_to_receive, | |
2349 | conn->sock->fd); | |
2350 | goto end; | |
2351 | } | |
2352 | ||
2353 | reception_complete: | |
2354 | DBG("Done receiving control command payload: fd = %i, payload size = %" PRIu64 " bytes", | |
2355 | conn->sock->fd, state->received); | |
2356 | /* | |
2357 | * The payload required to process the command has been received. | |
2358 | * A view to the reception buffer is forwarded to the various | |
2359 | * commands and the state of the control is reset on success. | |
2360 | * | |
2361 | * Commands are responsible for sending their reply to the peer. | |
2362 | */ | |
2363 | payload_view = lttng_buffer_view_from_dynamic_buffer(reception_buffer, | |
2364 | 0, -1); | |
2365 | ret = relay_process_control_command(conn, | |
2366 | &state->header, &payload_view); | |
2367 | if (ret < 0) { | |
2368 | status = RELAY_CONNECTION_STATUS_ERROR; | |
2369 | goto end; | |
2370 | } | |
2371 | ||
2372 | ret = connection_reset_protocol_state(conn); | |
2373 | if (ret) { | |
2374 | status = RELAY_CONNECTION_STATUS_ERROR; | |
2375 | } | |
2376 | end: | |
2377 | return status; | |
2378 | } | |
2379 | ||
2380 | static enum relay_connection_status relay_process_control_receive_header( | |
2381 | struct relay_connection *conn) | |
2382 | { | |
2383 | int ret = 0; | |
2384 | enum relay_connection_status status = RELAY_CONNECTION_STATUS_OK; | |
2385 | struct lttcomm_relayd_hdr header; | |
2386 | struct lttng_dynamic_buffer *reception_buffer = | |
2387 | &conn->protocol.ctrl.reception_buffer; | |
2388 | struct ctrl_connection_state_receive_header *state = | |
2389 | &conn->protocol.ctrl.state.receive_header; | |
2390 | ||
2391 | assert(state->left_to_receive != 0); | |
2392 | ||
2393 | ret = conn->sock->ops->recvmsg(conn->sock, | |
2394 | reception_buffer->data + state->received, | |
2395 | state->left_to_receive, MSG_DONTWAIT); | |
2396 | if (ret < 0) { | |
2397 | if (errno != EAGAIN && errno != EWOULDBLOCK) { | |
2398 | PERROR("Unable to receive control command header on sock %d", | |
2399 | conn->sock->fd); | |
2400 | status = RELAY_CONNECTION_STATUS_ERROR; | |
2401 | } | |
2402 | goto end; | |
2403 | } else if (ret == 0) { | |
2404 | DBG("Socket %d performed an orderly shutdown (received EOF)", conn->sock->fd); | |
2405 | status = RELAY_CONNECTION_STATUS_CLOSED; | |
2406 | goto end; | |
2407 | } | |
2408 | ||
2409 | assert(ret > 0); | |
2410 | assert(ret <= state->left_to_receive); | |
2411 | ||
2412 | state->left_to_receive -= ret; | |
2413 | state->received += ret; | |
2414 | ||
2415 | if (state->left_to_receive > 0) { | |
2416 | /* | |
2417 | * Can't transition to the protocol's next state, wait to | |
2418 | * receive the rest of the header. | |
2419 | */ | |
2420 | DBG3("Partial reception of control connection protocol header (received %" PRIu64 " bytes, %" PRIu64 " bytes left to receive, fd = %i)", | |
2421 | state->received, state->left_to_receive, | |
2422 | conn->sock->fd); | |
2423 | goto end; | |
2424 | } | |
2425 | ||
2426 | /* Transition to next state: receiving the command's payload. */ | |
2427 | conn->protocol.ctrl.state_id = | |
2428 | CTRL_CONNECTION_STATE_RECEIVE_PAYLOAD; | |
2429 | memcpy(&header, reception_buffer->data, sizeof(header)); | |
2430 | header.circuit_id = be64toh(header.circuit_id); | |
2431 | header.data_size = be64toh(header.data_size); | |
2432 | header.cmd = be32toh(header.cmd); | |
2433 | header.cmd_version = be32toh(header.cmd_version); | |
2434 | memcpy(&conn->protocol.ctrl.state.receive_payload.header, | |
2435 | &header, sizeof(header)); | |
2436 | ||
2437 | DBG("Done receiving control command header: fd = %i, cmd = %" PRIu32 ", cmd_version = %" PRIu32 ", payload size = %" PRIu64 " bytes", | |
2438 | conn->sock->fd, header.cmd, header.cmd_version, | |
2439 | header.data_size); | |
2440 | ||
2441 | if (header.data_size > DEFAULT_NETWORK_RELAYD_CTRL_MAX_PAYLOAD_SIZE) { | |
2442 | ERR("Command header indicates a payload (%" PRIu64 " bytes) that exceeds the maximal payload size allowed on a control connection.", | |
2443 | header.data_size); | |
2444 | status = RELAY_CONNECTION_STATUS_ERROR; | |
2445 | goto end; | |
2446 | } | |
2447 | ||
2448 | conn->protocol.ctrl.state.receive_payload.left_to_receive = | |
2449 | header.data_size; | |
2450 | conn->protocol.ctrl.state.receive_payload.received = 0; | |
2451 | ret = lttng_dynamic_buffer_set_size(reception_buffer, | |
2452 | header.data_size); | |
2453 | if (ret) { | |
2454 | status = RELAY_CONNECTION_STATUS_ERROR; | |
2455 | goto end; | |
2456 | } | |
2457 | ||
2458 | if (header.data_size == 0) { | |
2459 | /* | |
2460 | * Manually invoke the next state as the poll loop | |
2461 | * will not wake-up to allow us to proceed further. | |
2462 | */ | |
2463 | status = relay_process_control_receive_payload(conn); | |
2464 | } | |
2465 | end: | |
2466 | return status; | |
2467 | } | |
2468 | ||
2469 | /* | |
2470 | * Process the commands received on the control socket | |
2471 | */ | |
2472 | static enum relay_connection_status relay_process_control( | |
2473 | struct relay_connection *conn) | |
2474 | { | |
2475 | enum relay_connection_status status; | |
2476 | ||
2477 | switch (conn->protocol.ctrl.state_id) { | |
2478 | case CTRL_CONNECTION_STATE_RECEIVE_HEADER: | |
2479 | status = relay_process_control_receive_header(conn); | |
2480 | break; | |
2481 | case CTRL_CONNECTION_STATE_RECEIVE_PAYLOAD: | |
2482 | status = relay_process_control_receive_payload(conn); | |
2483 | break; | |
2484 | default: | |
2485 | ERR("Unknown control connection protocol state encountered."); | |
2486 | abort(); | |
2487 | } | |
2488 | ||
2489 | return status; | |
2490 | } | |
2491 | ||
7d2f7452 DG |
2492 | /* |
2493 | * Handle index for a data stream. | |
2494 | * | |
7591bab1 | 2495 | * Called with the stream lock held. |
7d2f7452 DG |
2496 | * |
2497 | * Return 0 on success else a negative value. | |
2498 | */ | |
2499 | static int handle_index_data(struct relay_stream *stream, uint64_t net_seq_num, | |
a8cad461 | 2500 | bool rotate_index) |
7d2f7452 | 2501 | { |
7591bab1 MD |
2502 | int ret = 0; |
2503 | uint64_t data_offset; | |
2504 | struct relay_index *index; | |
7d2f7452 | 2505 | |
7d2f7452 DG |
2506 | /* Get data offset because we are about to update the index. */ |
2507 | data_offset = htobe64(stream->tracefile_size_current); | |
2508 | ||
65d66b19 MD |
2509 | DBG("handle_index_data: stream %" PRIu64 " net_seq_num %" PRIu64 " data offset %" PRIu64, |
2510 | stream->stream_handle, net_seq_num, stream->tracefile_size_current); | |
7591bab1 | 2511 | |
7d2f7452 | 2512 | /* |
7591bab1 MD |
2513 | * Lookup for an existing index for that stream id/sequence |
2514 | * number. If it exists, the control thread has already received the | |
2515 | * data for it, thus we need to write it to disk. | |
7d2f7452 | 2516 | */ |
7591bab1 | 2517 | index = relay_index_get_by_id_or_create(stream, net_seq_num); |
7d2f7452 | 2518 | if (!index) { |
7591bab1 MD |
2519 | ret = -1; |
2520 | goto end; | |
7d2f7452 DG |
2521 | } |
2522 | ||
e0547b83 MD |
2523 | if (rotate_index || !stream->index_file) { |
2524 | uint32_t major, minor; | |
7591bab1 | 2525 | |
e0547b83 MD |
2526 | /* Put ref on previous index_file. */ |
2527 | if (stream->index_file) { | |
2528 | lttng_index_file_put(stream->index_file); | |
2529 | stream->index_file = NULL; | |
7d2f7452 | 2530 | } |
e0547b83 MD |
2531 | major = stream->trace->session->major; |
2532 | minor = stream->trace->session->minor; | |
2533 | stream->index_file = lttng_index_file_create(stream->path_name, | |
2534 | stream->channel_name, | |
7591bab1 | 2535 | -1, -1, stream->tracefile_size, |
e0547b83 MD |
2536 | tracefile_array_get_file_index_head(stream->tfa), |
2537 | lttng_to_index_major(major, minor), | |
2538 | lttng_to_index_minor(major, minor)); | |
2539 | if (!stream->index_file) { | |
7591bab1 MD |
2540 | ret = -1; |
2541 | /* Put self-ref for this index due to error. */ | |
2542 | relay_index_put(index); | |
e0547b83 | 2543 | index = NULL; |
7591bab1 | 2544 | goto end; |
7d2f7452 | 2545 | } |
7d2f7452 DG |
2546 | } |
2547 | ||
e0547b83 | 2548 | if (relay_index_set_file(index, stream->index_file, data_offset)) { |
7591bab1 MD |
2549 | ret = -1; |
2550 | /* Put self-ref for this index due to error. */ | |
2551 | relay_index_put(index); | |
e0547b83 | 2552 | index = NULL; |
7591bab1 | 2553 | goto end; |
7d2f7452 DG |
2554 | } |
2555 | ||
7591bab1 MD |
2556 | ret = relay_index_try_flush(index); |
2557 | if (ret == 0) { | |
a44ca2ca MD |
2558 | tracefile_array_commit_seq(stream->tfa); |
2559 | stream->index_received_seqcount++; | |
7591bab1 MD |
2560 | } else if (ret > 0) { |
2561 | /* No flush. */ | |
2562 | ret = 0; | |
2563 | } else { | |
2564 | /* Put self-ref for this index due to error. */ | |
2565 | relay_index_put(index); | |
e0547b83 | 2566 | index = NULL; |
7591bab1 MD |
2567 | ret = -1; |
2568 | } | |
2569 | end: | |
7d2f7452 DG |
2570 | return ret; |
2571 | } | |
2572 | ||
a8cad461 JG |
2573 | static enum relay_connection_status relay_process_data_receive_header( |
2574 | struct relay_connection *conn) | |
b8aa1682 | 2575 | { |
a8cad461 JG |
2576 | int ret; |
2577 | enum relay_connection_status status = RELAY_CONNECTION_STATUS_OK; | |
2578 | struct data_connection_state_receive_header *state = | |
2579 | &conn->protocol.data.state.receive_header; | |
2580 | struct lttcomm_relayd_data_hdr header; | |
b8aa1682 | 2581 | struct relay_stream *stream; |
b8aa1682 | 2582 | |
a8cad461 JG |
2583 | assert(state->left_to_receive != 0); |
2584 | ||
2585 | ret = conn->sock->ops->recvmsg(conn->sock, | |
2586 | state->header_reception_buffer + state->received, | |
2587 | state->left_to_receive, MSG_DONTWAIT); | |
2588 | if (ret < 0) { | |
2589 | if (errno != EAGAIN && errno != EWOULDBLOCK) { | |
2590 | PERROR("Unable to receive data header on sock %d", conn->sock->fd); | |
2591 | status = RELAY_CONNECTION_STATUS_ERROR; | |
a6cd2b97 | 2592 | } |
a8cad461 JG |
2593 | goto end; |
2594 | } else if (ret == 0) { | |
2595 | /* Orderly shutdown. Not necessary to print an error. */ | |
2596 | DBG("Socket %d performed an orderly shutdown (received EOF)", conn->sock->fd); | |
2597 | status = RELAY_CONNECTION_STATUS_CLOSED; | |
b8aa1682 JD |
2598 | goto end; |
2599 | } | |
2600 | ||
a8cad461 JG |
2601 | assert(ret > 0); |
2602 | assert(ret <= state->left_to_receive); | |
2603 | ||
2604 | state->left_to_receive -= ret; | |
2605 | state->received += ret; | |
2606 | ||
2607 | if (state->left_to_receive > 0) { | |
2608 | /* | |
2609 | * Can't transition to the protocol's next state, wait to | |
2610 | * receive the rest of the header. | |
2611 | */ | |
2612 | DBG3("Partial reception of data connection header (received %" PRIu64 " bytes, %" PRIu64 " bytes left to receive, fd = %i)", | |
2613 | state->received, state->left_to_receive, | |
2614 | conn->sock->fd); | |
2615 | ret = 0; | |
7591bab1 | 2616 | goto end; |
b8aa1682 | 2617 | } |
b8aa1682 | 2618 | |
a8cad461 JG |
2619 | /* Transition to next state: receiving the payload. */ |
2620 | conn->protocol.data.state_id = DATA_CONNECTION_STATE_RECEIVE_PAYLOAD; | |
173af62f | 2621 | |
a8cad461 JG |
2622 | memcpy(&header, state->header_reception_buffer, sizeof(header)); |
2623 | header.circuit_id = be64toh(header.circuit_id); | |
2624 | header.stream_id = be64toh(header.stream_id); | |
2625 | header.data_size = be32toh(header.data_size); | |
2626 | header.net_seq_num = be64toh(header.net_seq_num); | |
2627 | header.padding_size = be32toh(header.padding_size); | |
2628 | memcpy(&conn->protocol.data.state.receive_payload.header, &header, sizeof(header)); | |
2629 | ||
2630 | conn->protocol.data.state.receive_payload.left_to_receive = | |
2631 | header.data_size; | |
2632 | conn->protocol.data.state.receive_payload.received = 0; | |
2633 | conn->protocol.data.state.receive_payload.rotate_index = false; | |
2634 | ||
2635 | DBG("Received data connection header on fd %i: circuit_id = %" PRIu64 ", stream_id = %" PRIu64 ", data_size = %" PRIu32 ", net_seq_num = %" PRIu64 ", padding_size = %" PRIu32, | |
2636 | conn->sock->fd, header.circuit_id, | |
2637 | header.stream_id, header.data_size, | |
2638 | header.net_seq_num, header.padding_size); | |
2639 | ||
2640 | stream = stream_get_by_id(header.stream_id); | |
2641 | if (!stream) { | |
2642 | DBG("relay_process_data_receive_payload: Cannot find stream %" PRIu64, | |
2643 | header.stream_id); | |
2644 | /* Protocol error. */ | |
2645 | status = RELAY_CONNECTION_STATUS_ERROR; | |
2646 | goto end; | |
2647 | } | |
b8aa1682 | 2648 | |
7591bab1 MD |
2649 | pthread_mutex_lock(&stream->lock); |
2650 | ||
1c20f0e2 | 2651 | /* Check if a rotation is needed. */ |
0f907de1 | 2652 | if (stream->tracefile_size > 0 && |
a8cad461 | 2653 | (stream->tracefile_size_current + header.data_size) > |
0f907de1 | 2654 | stream->tracefile_size) { |
a44ca2ca MD |
2655 | uint64_t old_id, new_id; |
2656 | ||
2657 | old_id = tracefile_array_get_file_index_head(stream->tfa); | |
2658 | tracefile_array_file_rotate(stream->tfa); | |
2659 | ||
2660 | /* new_id is updated by utils_rotate_stream_file. */ | |
2661 | new_id = old_id; | |
6b6b9a5a | 2662 | |
7591bab1 MD |
2663 | ret = utils_rotate_stream_file(stream->path_name, |
2664 | stream->channel_name, stream->tracefile_size, | |
2665 | stream->tracefile_count, -1, | |
2666 | -1, stream->stream_fd->fd, | |
a44ca2ca | 2667 | &new_id, &stream->stream_fd->fd); |
0f907de1 | 2668 | if (ret < 0) { |
a8cad461 JG |
2669 | ERR("Failed to rotate stream output file"); |
2670 | status = RELAY_CONNECTION_STATUS_ERROR; | |
7591bab1 MD |
2671 | goto end_stream_unlock; |
2672 | } | |
a8cad461 | 2673 | |
7591bab1 MD |
2674 | /* |
2675 | * Reset current size because we just performed a stream | |
2676 | * rotation. | |
2677 | */ | |
a6976990 | 2678 | stream->tracefile_size_current = 0; |
a8cad461 | 2679 | conn->protocol.data.state.receive_payload.rotate_index = true; |
1c20f0e2 JD |
2680 | } |
2681 | ||
a8cad461 JG |
2682 | ret = 0; |
2683 | end_stream_unlock: | |
2684 | pthread_mutex_unlock(&stream->lock); | |
2685 | stream_put(stream); | |
2686 | end: | |
2687 | return status; | |
2688 | } | |
2689 | ||
2690 | static enum relay_connection_status relay_process_data_receive_payload( | |
2691 | struct relay_connection *conn) | |
2692 | { | |
2693 | int ret; | |
2694 | enum relay_connection_status status = RELAY_CONNECTION_STATUS_OK; | |
2695 | struct relay_stream *stream; | |
2696 | struct data_connection_state_receive_payload *state = | |
2697 | &conn->protocol.data.state.receive_payload; | |
2698 | const size_t chunk_size = RECV_DATA_BUFFER_SIZE; | |
2699 | char data_buffer[chunk_size]; | |
2700 | bool partial_recv = false; | |
2701 | bool new_stream = false, close_requested = false; | |
2702 | uint64_t left_to_receive = state->left_to_receive; | |
2703 | struct relay_session *session; | |
2704 | ||
2705 | DBG3("Receiving data for stream id %" PRIu64 " seqnum %" PRIu64 ", %" PRIu64" bytes received, %" PRIu64 " bytes left to receive", | |
2706 | state->header.stream_id, state->header.net_seq_num, | |
2707 | state->received, left_to_receive); | |
2708 | ||
2709 | stream = stream_get_by_id(state->header.stream_id); | |
2710 | if (!stream) { | |
2711 | /* Protocol error. */ | |
2712 | ERR("relay_process_data_receive_payload: cannot find stream %" PRIu64, | |
2713 | state->header.stream_id); | |
2714 | status = RELAY_CONNECTION_STATUS_ERROR; | |
2715 | goto end; | |
2716 | } | |
2717 | ||
2718 | pthread_mutex_lock(&stream->lock); | |
2719 | session = stream->trace->session; | |
2720 | if (!conn->session) { | |
2721 | ret = connection_set_session(conn, session); | |
2722 | if (ret) { | |
2723 | status = RELAY_CONNECTION_STATUS_ERROR; | |
7591bab1 | 2724 | goto end_stream_unlock; |
1c20f0e2 | 2725 | } |
1c20f0e2 JD |
2726 | } |
2727 | ||
a8cad461 JG |
2728 | /* |
2729 | * The size of the "chunk" received on any iteration is bounded by: | |
2730 | * - the data left to receive, | |
2731 | * - the data immediately available on the socket, | |
2732 | * - the on-stack data buffer | |
2733 | */ | |
2734 | while (left_to_receive > 0 && !partial_recv) { | |
2735 | ssize_t write_ret; | |
2736 | size_t recv_size = min(left_to_receive, chunk_size); | |
1d4dfdef | 2737 | |
a8cad461 JG |
2738 | ret = conn->sock->ops->recvmsg(conn->sock, data_buffer, |
2739 | recv_size, MSG_DONTWAIT); | |
2740 | if (ret < 0) { | |
2741 | if (errno != EAGAIN && errno != EWOULDBLOCK) { | |
2742 | PERROR("Socket %d error", conn->sock->fd); | |
2743 | status = RELAY_CONNECTION_STATUS_ERROR; | |
0848dba7 | 2744 | } |
0848dba7 | 2745 | goto end_stream_unlock; |
a8cad461 JG |
2746 | } else if (ret == 0) { |
2747 | /* No more data ready to be consumed on socket. */ | |
2748 | DBG3("No more data ready for consumption on data socket of stream id %" PRIu64, | |
2749 | state->header.stream_id); | |
2750 | status = RELAY_CONNECTION_STATUS_CLOSED; | |
2751 | break; | |
2752 | } else if (ret < (int) recv_size) { | |
2753 | /* | |
2754 | * All the data available on the socket has been | |
2755 | * consumed. | |
2756 | */ | |
2757 | partial_recv = true; | |
0848dba7 MD |
2758 | } |
2759 | ||
a8cad461 JG |
2760 | recv_size = ret; |
2761 | ||
0848dba7 | 2762 | /* Write data to stream output fd. */ |
a8cad461 | 2763 | write_ret = lttng_write(stream->stream_fd->fd, data_buffer, |
0848dba7 | 2764 | recv_size); |
a8cad461 | 2765 | if (write_ret < (ssize_t) recv_size) { |
0848dba7 | 2766 | ERR("Relay error writing data to file"); |
a8cad461 | 2767 | status = RELAY_CONNECTION_STATUS_ERROR; |
0848dba7 MD |
2768 | goto end_stream_unlock; |
2769 | } | |
2770 | ||
a8cad461 JG |
2771 | left_to_receive -= recv_size; |
2772 | state->received += recv_size; | |
2773 | state->left_to_receive = left_to_receive; | |
2774 | ||
0848dba7 | 2775 | DBG2("Relay wrote %zd bytes to tracefile for stream id %" PRIu64, |
a8cad461 JG |
2776 | write_ret, stream->stream_handle); |
2777 | } | |
2778 | ||
2779 | if (state->left_to_receive > 0) { | |
2780 | /* | |
2781 | * Did not receive all the data expected, wait for more data to | |
2782 | * become available on the socket. | |
2783 | */ | |
2784 | DBG3("Partial receive on data connection of stream id %" PRIu64 ", %" PRIu64 " bytes received, %" PRIu64 " bytes left to receive", | |
2785 | state->header.stream_id, state->received, | |
2786 | state->left_to_receive); | |
2787 | goto end_stream_unlock; | |
0848dba7 | 2788 | } |
5ab7344e | 2789 | |
7591bab1 | 2790 | ret = write_padding_to_file(stream->stream_fd->fd, |
a8cad461 JG |
2791 | state->header.padding_size); |
2792 | if ((int64_t) ret < (int64_t) state->header.padding_size) { | |
65d66b19 | 2793 | ERR("write_padding_to_file: fail stream %" PRIu64 " net_seq_num %" PRIu64 " ret %d", |
a8cad461 JG |
2794 | stream->stream_handle, |
2795 | state->header.net_seq_num, ret); | |
2796 | status = RELAY_CONNECTION_STATUS_ERROR; | |
7591bab1 | 2797 | goto end_stream_unlock; |
1d4dfdef | 2798 | } |
a8cad461 JG |
2799 | |
2800 | ||
2801 | if (session->minor >= 4 && !session->snapshot) { | |
2802 | ret = handle_index_data(stream, state->header.net_seq_num, | |
2803 | state->rotate_index); | |
2804 | if (ret < 0) { | |
2805 | ERR("handle_index_data: fail stream %" PRIu64 " net_seq_num %" PRIu64 " ret %d", | |
2806 | stream->stream_handle, | |
2807 | state->header.net_seq_num, ret); | |
2808 | status = RELAY_CONNECTION_STATUS_ERROR; | |
2809 | goto end_stream_unlock; | |
2810 | } | |
2811 | } | |
2812 | ||
2813 | stream->tracefile_size_current += state->header.data_size + | |
2814 | state->header.padding_size; | |
2815 | ||
c0bae11d MD |
2816 | if (stream->prev_seq == -1ULL) { |
2817 | new_stream = true; | |
2818 | } | |
2819 | ||
a8cad461 JG |
2820 | stream->prev_seq = state->header.net_seq_num; |
2821 | ||
2822 | /* | |
2823 | * Resetting the protocol state (to RECEIVE_HEADER) will trash the | |
2824 | * contents of *state which are aliased (union) to the same location as | |
2825 | * the new state. Don't use it beyond this point. | |
2826 | */ | |
2827 | connection_reset_protocol_state(conn); | |
2828 | state = NULL; | |
173af62f | 2829 | |
7591bab1 | 2830 | end_stream_unlock: |
bda7c7b9 | 2831 | close_requested = stream->close_requested; |
7591bab1 | 2832 | pthread_mutex_unlock(&stream->lock); |
a8cad461 | 2833 | if (close_requested && left_to_receive == 0) { |
bda7c7b9 JG |
2834 | try_stream_close(stream); |
2835 | } | |
2836 | ||
c0bae11d MD |
2837 | if (new_stream) { |
2838 | pthread_mutex_lock(&session->lock); | |
2839 | uatomic_set(&session->new_streams, 1); | |
2840 | pthread_mutex_unlock(&session->lock); | |
2841 | } | |
a8cad461 | 2842 | |
7591bab1 | 2843 | stream_put(stream); |
b8aa1682 | 2844 | end: |
a8cad461 JG |
2845 | return status; |
2846 | } | |
2847 | ||
2848 | /* | |
2849 | * relay_process_data: Process the data received on the data socket | |
2850 | */ | |
2851 | static enum relay_connection_status relay_process_data( | |
2852 | struct relay_connection *conn) | |
2853 | { | |
2854 | enum relay_connection_status status; | |
2855 | ||
2856 | switch (conn->protocol.data.state_id) { | |
2857 | case DATA_CONNECTION_STATE_RECEIVE_HEADER: | |
2858 | status = relay_process_data_receive_header(conn); | |
2859 | break; | |
2860 | case DATA_CONNECTION_STATE_RECEIVE_PAYLOAD: | |
2861 | status = relay_process_data_receive_payload(conn); | |
2862 | break; | |
2863 | default: | |
2864 | ERR("Unexpected data connection communication state."); | |
2865 | abort(); | |
2866 | } | |
2867 | ||
2868 | return status; | |
b8aa1682 JD |
2869 | } |
2870 | ||
7591bab1 | 2871 | static void cleanup_connection_pollfd(struct lttng_poll_event *events, int pollfd) |
b8aa1682 JD |
2872 | { |
2873 | int ret; | |
2874 | ||
58eb9381 | 2875 | (void) lttng_poll_del(events, pollfd); |
b8aa1682 JD |
2876 | |
2877 | ret = close(pollfd); | |
2878 | if (ret < 0) { | |
2879 | ERR("Closing pollfd %d", pollfd); | |
2880 | } | |
2881 | } | |
2882 | ||
7591bab1 MD |
2883 | static void relay_thread_close_connection(struct lttng_poll_event *events, |
2884 | int pollfd, struct relay_connection *conn) | |
9d1bbf21 | 2885 | { |
7591bab1 | 2886 | const char *type_str; |
2a174661 | 2887 | |
7591bab1 MD |
2888 | switch (conn->type) { |
2889 | case RELAY_DATA: | |
2890 | type_str = "Data"; | |
2891 | break; | |
2892 | case RELAY_CONTROL: | |
2893 | type_str = "Control"; | |
2894 | break; | |
2895 | case RELAY_VIEWER_COMMAND: | |
2896 | type_str = "Viewer Command"; | |
2897 | break; | |
2898 | case RELAY_VIEWER_NOTIFICATION: | |
2899 | type_str = "Viewer Notification"; | |
2900 | break; | |
2901 | default: | |
2902 | type_str = "Unknown"; | |
9d1bbf21 | 2903 | } |
7591bab1 MD |
2904 | cleanup_connection_pollfd(events, pollfd); |
2905 | connection_put(conn); | |
2906 | DBG("%s connection closed with %d", type_str, pollfd); | |
b8aa1682 JD |
2907 | } |
2908 | ||
2909 | /* | |
2910 | * This thread does the actual work | |
2911 | */ | |
7591bab1 | 2912 | static void *relay_thread_worker(void *data) |
b8aa1682 | 2913 | { |
beaad64c DG |
2914 | int ret, err = -1, last_seen_data_fd = -1; |
2915 | uint32_t nb_fd; | |
b8aa1682 JD |
2916 | struct lttng_poll_event events; |
2917 | struct lttng_ht *relay_connections_ht; | |
b8aa1682 | 2918 | struct lttng_ht_iter iter; |
90e7d72f | 2919 | struct relay_connection *destroy_conn = NULL; |
b8aa1682 JD |
2920 | |
2921 | DBG("[thread] Relay worker started"); | |
2922 | ||
9d1bbf21 MD |
2923 | rcu_register_thread(); |
2924 | ||
55706a7d MD |
2925 | health_register(health_relayd, HEALTH_RELAYD_TYPE_WORKER); |
2926 | ||
9b5e0863 MD |
2927 | if (testpoint(relayd_thread_worker)) { |
2928 | goto error_testpoint; | |
2929 | } | |
2930 | ||
f385ae0a MD |
2931 | health_code_update(); |
2932 | ||
b8aa1682 JD |
2933 | /* table of connections indexed on socket */ |
2934 | relay_connections_ht = lttng_ht_new(0, LTTNG_HT_TYPE_ULONG); | |
095a4ae5 MD |
2935 | if (!relay_connections_ht) { |
2936 | goto relay_connections_ht_error; | |
2937 | } | |
b8aa1682 | 2938 | |
b8aa1682 JD |
2939 | ret = create_thread_poll_set(&events, 2); |
2940 | if (ret < 0) { | |
2941 | goto error_poll_create; | |
2942 | } | |
2943 | ||
58eb9381 | 2944 | ret = lttng_poll_add(&events, relay_conn_pipe[0], LPOLLIN | LPOLLRDHUP); |
b8aa1682 JD |
2945 | if (ret < 0) { |
2946 | goto error; | |
2947 | } | |
2948 | ||
beaad64c | 2949 | restart: |
b8aa1682 | 2950 | while (1) { |
beaad64c DG |
2951 | int idx = -1, i, seen_control = 0, last_notdel_data_fd = -1; |
2952 | ||
f385ae0a MD |
2953 | health_code_update(); |
2954 | ||
b8aa1682 | 2955 | /* Infinite blocking call, waiting for transmission */ |
87c1611d | 2956 | DBG3("Relayd worker thread polling..."); |
f385ae0a | 2957 | health_poll_entry(); |
b8aa1682 | 2958 | ret = lttng_poll_wait(&events, -1); |
f385ae0a | 2959 | health_poll_exit(); |
b8aa1682 JD |
2960 | if (ret < 0) { |
2961 | /* | |
2962 | * Restart interrupted system call. | |
2963 | */ | |
2964 | if (errno == EINTR) { | |
2965 | goto restart; | |
2966 | } | |
2967 | goto error; | |
2968 | } | |
2969 | ||
0d9c5d77 DG |
2970 | nb_fd = ret; |
2971 | ||
beaad64c | 2972 | /* |
7591bab1 MD |
2973 | * Process control. The control connection is |
2974 | * prioritized so we don't starve it with high | |
2975 | * throughput tracing data on the data connection. | |
beaad64c | 2976 | */ |
b8aa1682 JD |
2977 | for (i = 0; i < nb_fd; i++) { |
2978 | /* Fetch once the poll data */ | |
beaad64c DG |
2979 | uint32_t revents = LTTNG_POLL_GETEV(&events, i); |
2980 | int pollfd = LTTNG_POLL_GETFD(&events, i); | |
b8aa1682 | 2981 | |
f385ae0a MD |
2982 | health_code_update(); |
2983 | ||
fd20dac9 | 2984 | if (!revents) { |
7591bab1 MD |
2985 | /* |
2986 | * No activity for this FD (poll | |
2987 | * implementation). | |
2988 | */ | |
fd20dac9 MD |
2989 | continue; |
2990 | } | |
2991 | ||
b8aa1682 JD |
2992 | /* Thread quit pipe has been closed. Killing thread. */ |
2993 | ret = check_thread_quit_pipe(pollfd, revents); | |
2994 | if (ret) { | |
095a4ae5 MD |
2995 | err = 0; |
2996 | goto exit; | |
b8aa1682 JD |
2997 | } |
2998 | ||
58eb9381 DG |
2999 | /* Inspect the relay conn pipe for new connection */ |
3000 | if (pollfd == relay_conn_pipe[0]) { | |
03e43155 | 3001 | if (revents & LPOLLIN) { |
90e7d72f JG |
3002 | struct relay_connection *conn; |
3003 | ||
58eb9381 | 3004 | ret = lttng_read(relay_conn_pipe[0], &conn, sizeof(conn)); |
b8aa1682 JD |
3005 | if (ret < 0) { |
3006 | goto error; | |
3007 | } | |
58eb9381 DG |
3008 | lttng_poll_add(&events, conn->sock->fd, |
3009 | LPOLLIN | LPOLLRDHUP); | |
7591bab1 | 3010 | connection_ht_add(relay_connections_ht, conn); |
58eb9381 | 3011 | DBG("Connection socket %d added", conn->sock->fd); |
03e43155 MD |
3012 | } else if (revents & (LPOLLERR | LPOLLHUP | LPOLLRDHUP)) { |
3013 | ERR("Relay connection pipe error"); | |
3014 | goto error; | |
3015 | } else { | |
3016 | ERR("Unexpected poll events %u for sock %d", revents, pollfd); | |
3017 | goto error; | |
b8aa1682 | 3018 | } |
58eb9381 | 3019 | } else { |
90e7d72f JG |
3020 | struct relay_connection *ctrl_conn; |
3021 | ||
7591bab1 | 3022 | ctrl_conn = connection_get_by_sock(relay_connections_ht, pollfd); |
58eb9381 | 3023 | /* If not found, there is a synchronization issue. */ |
90e7d72f | 3024 | assert(ctrl_conn); |
58eb9381 | 3025 | |
03e43155 MD |
3026 | if (ctrl_conn->type == RELAY_DATA) { |
3027 | if (revents & LPOLLIN) { | |
beaad64c DG |
3028 | /* |
3029 | * Flag the last seen data fd not deleted. It will be | |
3030 | * used as the last seen fd if any fd gets deleted in | |
3031 | * this first loop. | |
3032 | */ | |
3033 | last_notdel_data_fd = pollfd; | |
3034 | } | |
03e43155 MD |
3035 | goto put_ctrl_connection; |
3036 | } | |
3037 | assert(ctrl_conn->type == RELAY_CONTROL); | |
3038 | ||
3039 | if (revents & LPOLLIN) { | |
a8cad461 JG |
3040 | enum relay_connection_status status; |
3041 | ||
3042 | status = relay_process_control(ctrl_conn); | |
3043 | if (status != RELAY_CONNECTION_STATUS_OK) { | |
3044 | /* | |
3045 | * On socket error flag the session as aborted to force | |
3046 | * the cleanup of its stream otherwise it can leak | |
3047 | * during the lifetime of the relayd. | |
3048 | * | |
3049 | * This prevents situations in which streams can be | |
3050 | * left opened because an index was received, the | |
3051 | * control connection is closed, and the data | |
3052 | * connection is closed (uncleanly) before the packet's | |
3053 | * data provided. | |
3054 | * | |
3055 | * Since the control connection encountered an error, | |
3056 | * it is okay to be conservative and close the | |
3057 | * session right now as we can't rely on the protocol | |
3058 | * being respected anymore. | |
3059 | */ | |
3060 | if (status == RELAY_CONNECTION_STATUS_ERROR) { | |
3061 | session_abort(ctrl_conn->session); | |
03e43155 | 3062 | } |
a8cad461 JG |
3063 | |
3064 | /* Clear the connection on error or close. */ | |
3065 | relay_thread_close_connection(&events, | |
3066 | pollfd, | |
3067 | ctrl_conn); | |
03e43155 | 3068 | } |
a8cad461 | 3069 | seen_control = 1; |
03e43155 MD |
3070 | } else if (revents & (LPOLLERR | LPOLLHUP | LPOLLRDHUP)) { |
3071 | relay_thread_close_connection(&events, | |
3072 | pollfd, ctrl_conn); | |
3073 | if (last_seen_data_fd == pollfd) { | |
3074 | last_seen_data_fd = last_notdel_data_fd; | |
3075 | } | |
58eb9381 | 3076 | } else { |
03e43155 MD |
3077 | ERR("Unexpected poll events %u for control sock %d", |
3078 | revents, pollfd); | |
3079 | connection_put(ctrl_conn); | |
3080 | goto error; | |
beaad64c | 3081 | } |
03e43155 | 3082 | put_ctrl_connection: |
7591bab1 | 3083 | connection_put(ctrl_conn); |
beaad64c DG |
3084 | } |
3085 | } | |
3086 | ||
3087 | /* | |
3088 | * The last loop handled a control request, go back to poll to make | |
3089 | * sure we prioritise the control socket. | |
3090 | */ | |
3091 | if (seen_control) { | |
3092 | continue; | |
3093 | } | |
3094 | ||
3095 | if (last_seen_data_fd >= 0) { | |
3096 | for (i = 0; i < nb_fd; i++) { | |
3097 | int pollfd = LTTNG_POLL_GETFD(&events, i); | |
f385ae0a MD |
3098 | |
3099 | health_code_update(); | |
3100 | ||
beaad64c DG |
3101 | if (last_seen_data_fd == pollfd) { |
3102 | idx = i; | |
3103 | break; | |
3104 | } | |
3105 | } | |
3106 | } | |
3107 | ||
3108 | /* Process data connection. */ | |
3109 | for (i = idx + 1; i < nb_fd; i++) { | |
3110 | /* Fetch the poll data. */ | |
3111 | uint32_t revents = LTTNG_POLL_GETEV(&events, i); | |
3112 | int pollfd = LTTNG_POLL_GETFD(&events, i); | |
90e7d72f | 3113 | struct relay_connection *data_conn; |
beaad64c | 3114 | |
f385ae0a MD |
3115 | health_code_update(); |
3116 | ||
fd20dac9 MD |
3117 | if (!revents) { |
3118 | /* No activity for this FD (poll implementation). */ | |
3119 | continue; | |
3120 | } | |
3121 | ||
beaad64c | 3122 | /* Skip the command pipe. It's handled in the first loop. */ |
58eb9381 | 3123 | if (pollfd == relay_conn_pipe[0]) { |
beaad64c DG |
3124 | continue; |
3125 | } | |
3126 | ||
7591bab1 | 3127 | data_conn = connection_get_by_sock(relay_connections_ht, pollfd); |
90e7d72f | 3128 | if (!data_conn) { |
fd20dac9 | 3129 | /* Skip it. Might be removed before. */ |
fd20dac9 MD |
3130 | continue; |
3131 | } | |
03e43155 MD |
3132 | if (data_conn->type == RELAY_CONTROL) { |
3133 | goto put_data_connection; | |
3134 | } | |
3135 | assert(data_conn->type == RELAY_DATA); | |
fd20dac9 MD |
3136 | |
3137 | if (revents & LPOLLIN) { | |
a8cad461 JG |
3138 | enum relay_connection_status status; |
3139 | ||
3140 | status = relay_process_data(data_conn); | |
3141 | /* Connection closed or error. */ | |
3142 | if (status != RELAY_CONNECTION_STATUS_OK) { | |
3143 | /* | |
3144 | * On socket error flag the session as aborted to force | |
3145 | * the cleanup of its stream otherwise it can leak | |
3146 | * during the lifetime of the relayd. | |
3147 | * | |
3148 | * This prevents situations in which streams can be | |
3149 | * left opened because an index was received, the | |
3150 | * control connection is closed, and the data | |
3151 | * connection is closed (uncleanly) before the packet's | |
3152 | * data provided. | |
3153 | * | |
3154 | * Since the data connection encountered an error, | |
3155 | * it is okay to be conservative and close the | |
3156 | * session right now as we can't rely on the protocol | |
3157 | * being respected anymore. | |
3158 | */ | |
3159 | if (status == RELAY_CONNECTION_STATUS_ERROR) { | |
3160 | session_abort(data_conn->session); | |
3161 | } | |
7591bab1 | 3162 | relay_thread_close_connection(&events, pollfd, |
03e43155 | 3163 | data_conn); |
fd20dac9 MD |
3164 | /* |
3165 | * Every goto restart call sets the last seen fd where | |
3166 | * here we don't really care since we gracefully | |
3167 | * continue the loop after the connection is deleted. | |
3168 | */ | |
3169 | } else { | |
3170 | /* Keep last seen port. */ | |
3171 | last_seen_data_fd = pollfd; | |
7591bab1 | 3172 | connection_put(data_conn); |
fd20dac9 | 3173 | goto restart; |
b8aa1682 | 3174 | } |
03e43155 MD |
3175 | } else if (revents & (LPOLLERR | LPOLLHUP | LPOLLRDHUP)) { |
3176 | relay_thread_close_connection(&events, pollfd, | |
3177 | data_conn); | |
3178 | } else { | |
3179 | ERR("Unknown poll events %u for data sock %d", | |
3180 | revents, pollfd); | |
b8aa1682 | 3181 | } |
03e43155 | 3182 | put_data_connection: |
7591bab1 | 3183 | connection_put(data_conn); |
b8aa1682 | 3184 | } |
beaad64c | 3185 | last_seen_data_fd = -1; |
b8aa1682 JD |
3186 | } |
3187 | ||
f385ae0a MD |
3188 | /* Normal exit, no error */ |
3189 | ret = 0; | |
3190 | ||
095a4ae5 | 3191 | exit: |
b8aa1682 | 3192 | error: |
58eb9381 | 3193 | /* Cleanup reamaining connection object. */ |
9d1bbf21 | 3194 | rcu_read_lock(); |
90e7d72f JG |
3195 | cds_lfht_for_each_entry(relay_connections_ht->ht, &iter.iter, |
3196 | destroy_conn, | |
58eb9381 | 3197 | sock_n.node) { |
f385ae0a | 3198 | health_code_update(); |
06a68401 | 3199 | |
a8cad461 | 3200 | session_abort(destroy_conn->session); |
06a68401 | 3201 | |
7591bab1 MD |
3202 | /* |
3203 | * No need to grab another ref, because we own | |
3204 | * destroy_conn. | |
3205 | */ | |
3206 | relay_thread_close_connection(&events, destroy_conn->sock->fd, | |
3207 | destroy_conn); | |
b8aa1682 | 3208 | } |
94d49140 | 3209 | rcu_read_unlock(); |
7591bab1 MD |
3210 | |
3211 | lttng_poll_clean(&events); | |
7d2f7452 | 3212 | error_poll_create: |
b8aa1682 | 3213 | lttng_ht_destroy(relay_connections_ht); |
095a4ae5 | 3214 | relay_connections_ht_error: |
58eb9381 DG |
3215 | /* Close relay conn pipes */ |
3216 | utils_close_pipe(relay_conn_pipe); | |
095a4ae5 MD |
3217 | if (err) { |
3218 | DBG("Thread exited with error"); | |
3219 | } | |
b8aa1682 | 3220 | DBG("Worker thread cleanup complete"); |
9b5e0863 | 3221 | error_testpoint: |
f385ae0a MD |
3222 | if (err) { |
3223 | health_error(); | |
3224 | ERR("Health error occurred in %s", __func__); | |
3225 | } | |
3226 | health_unregister(health_relayd); | |
9d1bbf21 | 3227 | rcu_unregister_thread(); |
b4aacfdc | 3228 | lttng_relay_stop_threads(); |
b8aa1682 JD |
3229 | return NULL; |
3230 | } | |
3231 | ||
3232 | /* | |
3233 | * Create the relay command pipe to wake thread_manage_apps. | |
3234 | * Closed in cleanup(). | |
3235 | */ | |
58eb9381 | 3236 | static int create_relay_conn_pipe(void) |
b8aa1682 | 3237 | { |
a02de639 | 3238 | int ret; |
b8aa1682 | 3239 | |
58eb9381 | 3240 | ret = utils_create_pipe_cloexec(relay_conn_pipe); |
b8aa1682 | 3241 | |
b8aa1682 JD |
3242 | return ret; |
3243 | } | |
3244 | ||
3245 | /* | |
3246 | * main | |
3247 | */ | |
3248 | int main(int argc, char **argv) | |
3249 | { | |
178a0557 | 3250 | int ret = 0, retval = 0; |
b8aa1682 JD |
3251 | void *status; |
3252 | ||
b967f1cc JR |
3253 | /* Parse environment variables */ |
3254 | parse_env_options(); | |
3255 | ||
3256 | /* | |
3257 | * Parse arguments. | |
3258 | * Command line arguments overwrite environment. | |
3259 | */ | |
b8aa1682 | 3260 | progname = argv[0]; |
178a0557 MD |
3261 | if (set_options(argc, argv)) { |
3262 | retval = -1; | |
3263 | goto exit_options; | |
b8aa1682 JD |
3264 | } |
3265 | ||
178a0557 MD |
3266 | if (set_signal_handler()) { |
3267 | retval = -1; | |
3268 | goto exit_options; | |
b8aa1682 JD |
3269 | } |
3270 | ||
cdd566a2 JR |
3271 | relayd_config_log(); |
3272 | ||
3273 | if (opt_print_version) { | |
3274 | print_version(); | |
3275 | retval = 0; | |
3276 | goto exit_options; | |
3277 | } | |
3278 | ||
23c5429a JG |
3279 | ret = fclose(stdin); |
3280 | if (ret) { | |
3281 | PERROR("Failed to close stdin"); | |
3282 | goto exit_options; | |
3283 | } | |
4d513a50 DG |
3284 | /* Try to create directory if -o, --output is specified. */ |
3285 | if (opt_output_path) { | |
994fa64f DG |
3286 | if (*opt_output_path != '/') { |
3287 | ERR("Please specify an absolute path for -o, --output PATH"); | |
178a0557 MD |
3288 | retval = -1; |
3289 | goto exit_options; | |
994fa64f DG |
3290 | } |
3291 | ||
d77dded2 JG |
3292 | ret = utils_mkdir_recursive(opt_output_path, S_IRWXU | S_IRWXG, |
3293 | -1, -1); | |
4d513a50 DG |
3294 | if (ret < 0) { |
3295 | ERR("Unable to create %s", opt_output_path); | |
178a0557 MD |
3296 | retval = -1; |
3297 | goto exit_options; | |
4d513a50 DG |
3298 | } |
3299 | } | |
3300 | ||
b8aa1682 | 3301 | /* Daemonize */ |
b5218ffb | 3302 | if (opt_daemon || opt_background) { |
3fd27398 MD |
3303 | int i; |
3304 | ||
3305 | ret = lttng_daemonize(&child_ppid, &recv_child_signal, | |
3306 | !opt_background); | |
b8aa1682 | 3307 | if (ret < 0) { |
178a0557 MD |
3308 | retval = -1; |
3309 | goto exit_options; | |
b8aa1682 | 3310 | } |
3fd27398 MD |
3311 | |
3312 | /* | |
3313 | * We are in the child. Make sure all other file | |
3314 | * descriptors are closed, in case we are called with | |
3315 | * more opened file descriptors than the standard ones. | |
3316 | */ | |
3317 | for (i = 3; i < sysconf(_SC_OPEN_MAX); i++) { | |
3318 | (void) close(i); | |
3319 | } | |
3320 | } | |
3321 | ||
a174e6fc JR |
3322 | if (opt_working_directory) { |
3323 | ret = utils_change_working_dir(opt_working_directory); | |
3324 | if (ret) { | |
3325 | ERR("Changing working directory"); | |
3326 | goto exit_options; | |
3327 | } | |
3328 | } | |
bf5e21fd JG |
3329 | /* |
3330 | * The RCU thread registration (and use, through the fd-tracker's | |
3331 | * creation) is done after the daemonization to allow us to not | |
3332 | * deal with liburcu's fork() management as the call RCU needs to | |
3333 | * be restored. | |
3334 | */ | |
3335 | rcu_register_thread(); | |
3336 | ||
3337 | the_fd_tracker = fd_tracker_create(lttng_opt_fd_cap); | |
3338 | if (!the_fd_tracker) { | |
3339 | retval = -1; | |
3340 | goto exit_options; | |
3341 | } | |
a174e6fc | 3342 | |
178a0557 MD |
3343 | /* Initialize thread health monitoring */ |
3344 | health_relayd = health_app_create(NR_HEALTH_RELAYD_TYPES); | |
3345 | if (!health_relayd) { | |
3346 | PERROR("health_app_create error"); | |
3347 | retval = -1; | |
3348 | goto exit_health_app_create; | |
3349 | } | |
3350 | ||
3fd27398 | 3351 | /* Create thread quit pipe */ |
178a0557 MD |
3352 | if (init_thread_quit_pipe()) { |
3353 | retval = -1; | |
3354 | goto exit_init_data; | |
b8aa1682 JD |
3355 | } |
3356 | ||
b8aa1682 | 3357 | /* Setup the thread apps communication pipe. */ |
178a0557 MD |
3358 | if (create_relay_conn_pipe()) { |
3359 | retval = -1; | |
3360 | goto exit_init_data; | |
b8aa1682 JD |
3361 | } |
3362 | ||
3363 | /* Init relay command queue. */ | |
8bdee6e2 | 3364 | cds_wfcq_init(&relay_conn_queue.head, &relay_conn_queue.tail); |
b8aa1682 | 3365 | |
554831e7 MD |
3366 | /* Initialize communication library */ |
3367 | lttcomm_init(); | |
87e45c13 | 3368 | lttcomm_inet_init(); |
554831e7 | 3369 | |
d3e2ba59 | 3370 | /* tables of sessions indexed by session ID */ |
7591bab1 MD |
3371 | sessions_ht = lttng_ht_new(0, LTTNG_HT_TYPE_U64); |
3372 | if (!sessions_ht) { | |
178a0557 MD |
3373 | retval = -1; |
3374 | goto exit_init_data; | |
d3e2ba59 JD |
3375 | } |
3376 | ||
3377 | /* tables of streams indexed by stream ID */ | |
2a174661 | 3378 | relay_streams_ht = lttng_ht_new(0, LTTNG_HT_TYPE_U64); |
d3e2ba59 | 3379 | if (!relay_streams_ht) { |
178a0557 MD |
3380 | retval = -1; |
3381 | goto exit_init_data; | |
d3e2ba59 JD |
3382 | } |
3383 | ||
3384 | /* tables of streams indexed by stream ID */ | |
92c6ca54 DG |
3385 | viewer_streams_ht = lttng_ht_new(0, LTTNG_HT_TYPE_U64); |
3386 | if (!viewer_streams_ht) { | |
178a0557 MD |
3387 | retval = -1; |
3388 | goto exit_init_data; | |
55706a7d MD |
3389 | } |
3390 | ||
65931c8b | 3391 | ret = utils_create_pipe(health_quit_pipe); |
178a0557 MD |
3392 | if (ret) { |
3393 | retval = -1; | |
3394 | goto exit_health_quit_pipe; | |
65931c8b MD |
3395 | } |
3396 | ||
3397 | /* Create thread to manage the client socket */ | |
1a1a34b4 | 3398 | ret = pthread_create(&health_thread, default_pthread_attr(), |
65931c8b | 3399 | thread_manage_health, (void *) NULL); |
178a0557 MD |
3400 | if (ret) { |
3401 | errno = ret; | |
65931c8b | 3402 | PERROR("pthread_create health"); |
178a0557 MD |
3403 | retval = -1; |
3404 | goto exit_health_thread; | |
65931c8b MD |
3405 | } |
3406 | ||
b8aa1682 | 3407 | /* Setup the dispatcher thread */ |
1a1a34b4 | 3408 | ret = pthread_create(&dispatcher_thread, default_pthread_attr(), |
b8aa1682 | 3409 | relay_thread_dispatcher, (void *) NULL); |
178a0557 MD |
3410 | if (ret) { |
3411 | errno = ret; | |
b8aa1682 | 3412 | PERROR("pthread_create dispatcher"); |
178a0557 MD |
3413 | retval = -1; |
3414 | goto exit_dispatcher_thread; | |
b8aa1682 JD |
3415 | } |
3416 | ||
3417 | /* Setup the worker thread */ | |
1a1a34b4 | 3418 | ret = pthread_create(&worker_thread, default_pthread_attr(), |
7591bab1 | 3419 | relay_thread_worker, NULL); |
178a0557 MD |
3420 | if (ret) { |
3421 | errno = ret; | |
b8aa1682 | 3422 | PERROR("pthread_create worker"); |
178a0557 MD |
3423 | retval = -1; |
3424 | goto exit_worker_thread; | |
b8aa1682 JD |
3425 | } |
3426 | ||
3427 | /* Setup the listener thread */ | |
1a1a34b4 | 3428 | ret = pthread_create(&listener_thread, default_pthread_attr(), |
b8aa1682 | 3429 | relay_thread_listener, (void *) NULL); |
178a0557 MD |
3430 | if (ret) { |
3431 | errno = ret; | |
b8aa1682 | 3432 | PERROR("pthread_create listener"); |
178a0557 MD |
3433 | retval = -1; |
3434 | goto exit_listener_thread; | |
b8aa1682 JD |
3435 | } |
3436 | ||
7591bab1 | 3437 | ret = relayd_live_create(live_uri); |
178a0557 | 3438 | if (ret) { |
d3e2ba59 | 3439 | ERR("Starting live viewer threads"); |
178a0557 | 3440 | retval = -1; |
50138f51 | 3441 | goto exit_live; |
d3e2ba59 JD |
3442 | } |
3443 | ||
178a0557 MD |
3444 | /* |
3445 | * This is where we start awaiting program completion (e.g. through | |
3446 | * signal that asks threads to teardown). | |
3447 | */ | |
3448 | ||
3449 | ret = relayd_live_join(); | |
3450 | if (ret) { | |
3451 | retval = -1; | |
3452 | } | |
50138f51 | 3453 | exit_live: |
178a0557 | 3454 | |
b8aa1682 | 3455 | ret = pthread_join(listener_thread, &status); |
178a0557 MD |
3456 | if (ret) { |
3457 | errno = ret; | |
3458 | PERROR("pthread_join listener_thread"); | |
3459 | retval = -1; | |
b8aa1682 JD |
3460 | } |
3461 | ||
178a0557 | 3462 | exit_listener_thread: |
b8aa1682 | 3463 | ret = pthread_join(worker_thread, &status); |
178a0557 MD |
3464 | if (ret) { |
3465 | errno = ret; | |
3466 | PERROR("pthread_join worker_thread"); | |
3467 | retval = -1; | |
b8aa1682 JD |
3468 | } |
3469 | ||
178a0557 | 3470 | exit_worker_thread: |
b8aa1682 | 3471 | ret = pthread_join(dispatcher_thread, &status); |
178a0557 MD |
3472 | if (ret) { |
3473 | errno = ret; | |
3474 | PERROR("pthread_join dispatcher_thread"); | |
3475 | retval = -1; | |
b8aa1682 | 3476 | } |
178a0557 | 3477 | exit_dispatcher_thread: |
42415026 | 3478 | |
65931c8b | 3479 | ret = pthread_join(health_thread, &status); |
178a0557 MD |
3480 | if (ret) { |
3481 | errno = ret; | |
3482 | PERROR("pthread_join health_thread"); | |
3483 | retval = -1; | |
65931c8b | 3484 | } |
178a0557 | 3485 | exit_health_thread: |
65931c8b | 3486 | |
65931c8b | 3487 | utils_close_pipe(health_quit_pipe); |
178a0557 | 3488 | exit_health_quit_pipe: |
65931c8b | 3489 | |
178a0557 | 3490 | exit_init_data: |
55706a7d | 3491 | health_app_destroy(health_relayd); |
55706a7d | 3492 | exit_health_app_create: |
178a0557 | 3493 | exit_options: |
2668465a MD |
3494 | /* |
3495 | * Wait for all pending call_rcu work to complete before tearing | |
3496 | * down data structures. call_rcu worker may be trying to | |
3497 | * perform lookups in those structures. | |
3498 | */ | |
3499 | rcu_barrier(); | |
7591bab1 MD |
3500 | relayd_cleanup(); |
3501 | ||
3502 | /* Ensure all prior call_rcu are done. */ | |
3503 | rcu_barrier(); | |
d3e2ba59 | 3504 | |
bf5e21fd JG |
3505 | fd_tracker_destroy(the_fd_tracker); |
3506 | rcu_unregister_thread(); | |
3507 | ||
178a0557 | 3508 | if (!retval) { |
b8aa1682 | 3509 | exit(EXIT_SUCCESS); |
178a0557 MD |
3510 | } else { |
3511 | exit(EXIT_FAILURE); | |
b8aa1682 | 3512 | } |
b8aa1682 | 3513 | } |