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