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