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