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