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