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