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