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 | ||
26c9d55e | 980 | while (!CMM_LOAD_SHARED(dispatch_thread_exit)) { |
f385ae0a MD |
981 | health_code_update(); |
982 | ||
b8aa1682 | 983 | /* Atomically prepare the queue futex */ |
58eb9381 | 984 | futex_nto1_prepare(&relay_conn_queue.futex); |
b8aa1682 JD |
985 | |
986 | do { | |
f385ae0a MD |
987 | health_code_update(); |
988 | ||
b8aa1682 | 989 | /* Dequeue commands */ |
8bdee6e2 SM |
990 | node = cds_wfcq_dequeue_blocking(&relay_conn_queue.head, |
991 | &relay_conn_queue.tail); | |
b8aa1682 JD |
992 | if (node == NULL) { |
993 | DBG("Woken up but nothing in the relay command queue"); | |
994 | /* Continue thread execution */ | |
995 | break; | |
996 | } | |
58eb9381 | 997 | new_conn = caa_container_of(node, struct relay_connection, qnode); |
b8aa1682 | 998 | |
58eb9381 | 999 | DBG("Dispatching request waiting on sock %d", new_conn->sock->fd); |
b8aa1682 JD |
1000 | |
1001 | /* | |
7591bab1 MD |
1002 | * Inform worker thread of the new request. This |
1003 | * call is blocking so we can be assured that | |
1004 | * the data will be read at some point in time | |
1005 | * or wait to the end of the world :) | |
b8aa1682 | 1006 | */ |
58eb9381 DG |
1007 | ret = lttng_write(relay_conn_pipe[1], &new_conn, sizeof(new_conn)); |
1008 | if (ret < 0) { | |
1009 | PERROR("write connection pipe"); | |
7591bab1 | 1010 | connection_put(new_conn); |
b8aa1682 JD |
1011 | goto error; |
1012 | } | |
1013 | } while (node != NULL); | |
1014 | ||
1015 | /* Futex wait on queue. Blocking call on futex() */ | |
f385ae0a | 1016 | health_poll_entry(); |
58eb9381 | 1017 | futex_nto1_wait(&relay_conn_queue.futex); |
f385ae0a | 1018 | health_poll_exit(); |
b8aa1682 JD |
1019 | } |
1020 | ||
f385ae0a MD |
1021 | /* Normal exit, no error */ |
1022 | err = 0; | |
1023 | ||
b8aa1682 | 1024 | error: |
9b5e0863 | 1025 | error_testpoint: |
f385ae0a MD |
1026 | if (err) { |
1027 | health_error(); | |
1028 | ERR("Health error occurred in %s", __func__); | |
1029 | } | |
55706a7d | 1030 | health_unregister(health_relayd); |
b8aa1682 | 1031 | DBG("Dispatch thread dying"); |
b4aacfdc | 1032 | lttng_relay_stop_threads(); |
b8aa1682 JD |
1033 | return NULL; |
1034 | } | |
1035 | ||
b8aa1682 | 1036 | /* |
7591bab1 | 1037 | * Set index data from the control port to a given index object. |
b8aa1682 | 1038 | */ |
7591bab1 | 1039 | static int set_index_control_data(struct relay_index *index, |
234cd636 JD |
1040 | struct lttcomm_relayd_index *data, |
1041 | struct relay_connection *conn) | |
1c20f0e2 | 1042 | { |
7591bab1 | 1043 | struct ctf_packet_index index_data; |
1c20f0e2 JD |
1044 | |
1045 | /* | |
7591bab1 MD |
1046 | * The index on disk is encoded in big endian, so we don't need |
1047 | * to convert the data received on the network. The data_offset | |
1048 | * value is NEVER modified here and is updated by the data | |
1049 | * thread. | |
1c20f0e2 | 1050 | */ |
7591bab1 MD |
1051 | index_data.packet_size = data->packet_size; |
1052 | index_data.content_size = data->content_size; | |
1053 | index_data.timestamp_begin = data->timestamp_begin; | |
1054 | index_data.timestamp_end = data->timestamp_end; | |
1055 | index_data.events_discarded = data->events_discarded; | |
1056 | index_data.stream_id = data->stream_id; | |
234cd636 JD |
1057 | |
1058 | if (conn->minor >= 8) { | |
1059 | index->index_data.stream_instance_id = data->stream_instance_id; | |
1060 | index->index_data.packet_seq_num = data->packet_seq_num; | |
1061 | } | |
1062 | ||
7591bab1 | 1063 | return relay_index_set_data(index, &index_data); |
1c20f0e2 JD |
1064 | } |
1065 | ||
c5b6f4f0 DG |
1066 | /* |
1067 | * Handle the RELAYD_CREATE_SESSION command. | |
1068 | * | |
1069 | * On success, send back the session id or else return a negative value. | |
1070 | */ | |
7591bab1 | 1071 | static int relay_create_session(struct lttcomm_relayd_hdr *recv_hdr, |
58eb9381 | 1072 | struct relay_connection *conn) |
c5b6f4f0 DG |
1073 | { |
1074 | int ret = 0, send_ret; | |
1075 | struct relay_session *session; | |
1076 | struct lttcomm_relayd_status_session reply; | |
36d2e35d | 1077 | char session_name[LTTNG_NAME_MAX]; |
9cf3eb20 | 1078 | char hostname[LTTNG_HOST_NAME_MAX]; |
7591bab1 MD |
1079 | uint32_t live_timer = 0; |
1080 | bool snapshot = false; | |
c5b6f4f0 | 1081 | |
36d2e35d | 1082 | memset(session_name, 0, LTTNG_NAME_MAX); |
9cf3eb20 | 1083 | memset(hostname, 0, LTTNG_HOST_NAME_MAX); |
c5b6f4f0 DG |
1084 | |
1085 | memset(&reply, 0, sizeof(reply)); | |
1086 | ||
58eb9381 | 1087 | switch (conn->minor) { |
2a174661 DG |
1088 | case 1: |
1089 | case 2: | |
1090 | case 3: | |
1091 | break; | |
1092 | case 4: /* LTTng sessiond 2.4 */ | |
1093 | default: | |
7591bab1 MD |
1094 | ret = cmd_create_session_2_4(conn, session_name, |
1095 | hostname, &live_timer, &snapshot); | |
1096 | } | |
1097 | if (ret < 0) { | |
1098 | goto send_reply; | |
d3e2ba59 JD |
1099 | } |
1100 | ||
7591bab1 MD |
1101 | session = session_create(session_name, hostname, live_timer, |
1102 | snapshot, conn->major, conn->minor); | |
1103 | if (!session) { | |
1104 | ret = -1; | |
1105 | goto send_reply; | |
1106 | } | |
1107 | assert(!conn->session); | |
1108 | conn->session = session; | |
c5b6f4f0 DG |
1109 | DBG("Created session %" PRIu64, session->id); |
1110 | ||
7591bab1 MD |
1111 | reply.session_id = htobe64(session->id); |
1112 | ||
1113 | send_reply: | |
c5b6f4f0 DG |
1114 | if (ret < 0) { |
1115 | reply.ret_code = htobe32(LTTNG_ERR_FATAL); | |
1116 | } else { | |
1117 | reply.ret_code = htobe32(LTTNG_OK); | |
1118 | } | |
1119 | ||
58eb9381 | 1120 | send_ret = conn->sock->ops->sendmsg(conn->sock, &reply, sizeof(reply), 0); |
c5b6f4f0 DG |
1121 | if (send_ret < 0) { |
1122 | ERR("Relayd sending session id"); | |
4169f5ad | 1123 | ret = send_ret; |
c5b6f4f0 DG |
1124 | } |
1125 | ||
1126 | return ret; | |
1127 | } | |
1128 | ||
a4baae1b JD |
1129 | /* |
1130 | * When we have received all the streams and the metadata for a channel, | |
1131 | * we make them visible to the viewer threads. | |
1132 | */ | |
7591bab1 | 1133 | static void publish_connection_local_streams(struct relay_connection *conn) |
a4baae1b | 1134 | { |
7591bab1 MD |
1135 | struct relay_stream *stream; |
1136 | struct relay_session *session = conn->session; | |
a4baae1b | 1137 | |
7591bab1 MD |
1138 | /* |
1139 | * We publish all streams belonging to a session atomically wrt | |
1140 | * session lock. | |
1141 | */ | |
1142 | pthread_mutex_lock(&session->lock); | |
1143 | rcu_read_lock(); | |
1144 | cds_list_for_each_entry_rcu(stream, &session->recv_list, | |
1145 | recv_node) { | |
1146 | stream_publish(stream); | |
a4baae1b | 1147 | } |
7591bab1 | 1148 | rcu_read_unlock(); |
a4baae1b | 1149 | |
7591bab1 MD |
1150 | /* |
1151 | * Inform the viewer that there are new streams in the session. | |
1152 | */ | |
1153 | if (session->viewer_attached) { | |
1154 | uatomic_set(&session->new_streams, 1); | |
1155 | } | |
1156 | pthread_mutex_unlock(&session->lock); | |
a4baae1b JD |
1157 | } |
1158 | ||
b8aa1682 JD |
1159 | /* |
1160 | * relay_add_stream: allocate a new stream for a session | |
1161 | */ | |
7591bab1 | 1162 | static int relay_add_stream(struct lttcomm_relayd_hdr *recv_hdr, |
58eb9381 | 1163 | struct relay_connection *conn) |
b8aa1682 | 1164 | { |
7591bab1 MD |
1165 | int ret; |
1166 | ssize_t send_ret; | |
58eb9381 | 1167 | struct relay_session *session = conn->session; |
b8aa1682 JD |
1168 | struct relay_stream *stream = NULL; |
1169 | struct lttcomm_relayd_status_stream reply; | |
4030a636 | 1170 | struct ctf_trace *trace = NULL; |
7591bab1 MD |
1171 | uint64_t stream_handle = -1ULL; |
1172 | char *path_name = NULL, *channel_name = NULL; | |
1173 | uint64_t tracefile_size = 0, tracefile_count = 0; | |
b8aa1682 | 1174 | |
58eb9381 | 1175 | if (!session || conn->version_check_done == 0) { |
b8aa1682 JD |
1176 | ERR("Trying to add a stream before version check"); |
1177 | ret = -1; | |
1178 | goto end_no_session; | |
1179 | } | |
1180 | ||
7591bab1 MD |
1181 | switch (session->minor) { |
1182 | case 1: /* LTTng sessiond 2.1. Allocates path_name and channel_name. */ | |
1183 | ret = cmd_recv_stream_2_1(conn, &path_name, | |
1184 | &channel_name); | |
0f907de1 | 1185 | break; |
7591bab1 | 1186 | case 2: /* LTTng sessiond 2.2. Allocates path_name and channel_name. */ |
0f907de1 | 1187 | default: |
7591bab1 MD |
1188 | ret = cmd_recv_stream_2_2(conn, &path_name, |
1189 | &channel_name, &tracefile_size, &tracefile_count); | |
0f907de1 JD |
1190 | break; |
1191 | } | |
1192 | if (ret < 0) { | |
7591bab1 | 1193 | goto send_reply; |
b8aa1682 JD |
1194 | } |
1195 | ||
7591bab1 | 1196 | trace = ctf_trace_get_by_path_or_create(session, path_name); |
2a174661 | 1197 | if (!trace) { |
7591bab1 | 1198 | goto send_reply; |
2a174661 | 1199 | } |
7591bab1 | 1200 | /* This stream here has one reference on the trace. */ |
2a174661 | 1201 | |
7591bab1 MD |
1202 | pthread_mutex_lock(&last_relay_stream_id_lock); |
1203 | stream_handle = ++last_relay_stream_id; | |
1204 | pthread_mutex_unlock(&last_relay_stream_id_lock); | |
d3e2ba59 | 1205 | |
7591bab1 MD |
1206 | /* We pass ownership of path_name and channel_name. */ |
1207 | stream = stream_create(trace, stream_handle, path_name, | |
1208 | channel_name, tracefile_size, tracefile_count); | |
1209 | path_name = NULL; | |
1210 | channel_name = NULL; | |
a4baae1b | 1211 | |
2a174661 | 1212 | /* |
7591bab1 MD |
1213 | * Streams are the owners of their trace. Reference to trace is |
1214 | * kept within stream_create(). | |
2a174661 | 1215 | */ |
7591bab1 | 1216 | ctf_trace_put(trace); |
d3e2ba59 | 1217 | |
7591bab1 | 1218 | send_reply: |
53efb85a | 1219 | memset(&reply, 0, sizeof(reply)); |
7591bab1 MD |
1220 | reply.handle = htobe64(stream_handle); |
1221 | if (!stream) { | |
f73fabfd | 1222 | reply.ret_code = htobe32(LTTNG_ERR_UNK); |
b8aa1682 | 1223 | } else { |
f73fabfd | 1224 | reply.ret_code = htobe32(LTTNG_OK); |
b8aa1682 | 1225 | } |
5af40280 | 1226 | |
58eb9381 | 1227 | send_ret = conn->sock->ops->sendmsg(conn->sock, &reply, |
b8aa1682 JD |
1228 | sizeof(struct lttcomm_relayd_status_stream), 0); |
1229 | if (send_ret < 0) { | |
1230 | ERR("Relay sending stream id"); | |
7591bab1 | 1231 | ret = (int) send_ret; |
b8aa1682 JD |
1232 | } |
1233 | ||
1234 | end_no_session: | |
7591bab1 MD |
1235 | free(path_name); |
1236 | free(channel_name); | |
0f907de1 | 1237 | return ret; |
b8aa1682 JD |
1238 | } |
1239 | ||
173af62f DG |
1240 | /* |
1241 | * relay_close_stream: close a specific stream | |
1242 | */ | |
7591bab1 | 1243 | static int relay_close_stream(struct lttcomm_relayd_hdr *recv_hdr, |
58eb9381 | 1244 | struct relay_connection *conn) |
173af62f | 1245 | { |
94d49140 | 1246 | int ret, send_ret; |
58eb9381 | 1247 | struct relay_session *session = conn->session; |
173af62f DG |
1248 | struct lttcomm_relayd_close_stream stream_info; |
1249 | struct lttcomm_relayd_generic_reply reply; | |
1250 | struct relay_stream *stream; | |
173af62f DG |
1251 | |
1252 | DBG("Close stream received"); | |
1253 | ||
58eb9381 | 1254 | if (!session || conn->version_check_done == 0) { |
173af62f DG |
1255 | ERR("Trying to close a stream before version check"); |
1256 | ret = -1; | |
1257 | goto end_no_session; | |
1258 | } | |
1259 | ||
58eb9381 | 1260 | ret = conn->sock->ops->recvmsg(conn->sock, &stream_info, |
7c5aef62 | 1261 | sizeof(struct lttcomm_relayd_close_stream), 0); |
173af62f | 1262 | if (ret < sizeof(struct lttcomm_relayd_close_stream)) { |
a6cd2b97 DG |
1263 | if (ret == 0) { |
1264 | /* Orderly shutdown. Not necessary to print an error. */ | |
58eb9381 | 1265 | DBG("Socket %d did an orderly shutdown", conn->sock->fd); |
a6cd2b97 DG |
1266 | } else { |
1267 | ERR("Relay didn't receive valid add_stream struct size : %d", ret); | |
1268 | } | |
173af62f DG |
1269 | ret = -1; |
1270 | goto end_no_session; | |
1271 | } | |
1272 | ||
7591bab1 | 1273 | stream = stream_get_by_id(be64toh(stream_info.stream_id)); |
173af62f DG |
1274 | if (!stream) { |
1275 | ret = -1; | |
7591bab1 | 1276 | goto end; |
173af62f | 1277 | } |
77f7bd85 MD |
1278 | |
1279 | /* | |
1280 | * Set last_net_seq_num before the close flag. Required by data | |
1281 | * pending check. | |
1282 | */ | |
7591bab1 | 1283 | pthread_mutex_lock(&stream->lock); |
8e2583a4 | 1284 | stream->last_net_seq_num = be64toh(stream_info.last_net_seq_num); |
77f7bd85 MD |
1285 | pthread_mutex_unlock(&stream->lock); |
1286 | ||
bda7c7b9 JG |
1287 | /* |
1288 | * This is one of the conditions which may trigger a stream close | |
1289 | * with the others being: | |
1290 | * 1) A close command is received for a stream | |
1291 | * 2) The control connection owning the stream is closed | |
1292 | * 3) We have received all of the stream's data _after_ a close | |
1293 | * request. | |
1294 | */ | |
1295 | try_stream_close(stream); | |
7591bab1 MD |
1296 | if (stream->is_metadata) { |
1297 | struct relay_viewer_stream *vstream; | |
173af62f | 1298 | |
7591bab1 MD |
1299 | vstream = viewer_stream_get_by_id(stream->stream_handle); |
1300 | if (vstream) { | |
1301 | if (vstream->metadata_sent == stream->metadata_received) { | |
1302 | /* | |
1303 | * Since all the metadata has been sent to the | |
1304 | * viewer and that we have a request to close | |
1305 | * its stream, we can safely teardown the | |
1306 | * corresponding metadata viewer stream. | |
1307 | */ | |
1308 | viewer_stream_put(vstream); | |
1309 | } | |
1310 | /* Put local reference. */ | |
1311 | viewer_stream_put(vstream); | |
1312 | } | |
1313 | } | |
7591bab1 | 1314 | stream_put(stream); |
173af62f | 1315 | |
7591bab1 | 1316 | end: |
53efb85a | 1317 | memset(&reply, 0, sizeof(reply)); |
173af62f | 1318 | if (ret < 0) { |
f73fabfd | 1319 | reply.ret_code = htobe32(LTTNG_ERR_UNK); |
173af62f | 1320 | } else { |
f73fabfd | 1321 | reply.ret_code = htobe32(LTTNG_OK); |
173af62f | 1322 | } |
58eb9381 | 1323 | send_ret = conn->sock->ops->sendmsg(conn->sock, &reply, |
173af62f DG |
1324 | sizeof(struct lttcomm_relayd_generic_reply), 0); |
1325 | if (send_ret < 0) { | |
1326 | ERR("Relay sending stream id"); | |
4169f5ad | 1327 | ret = send_ret; |
173af62f DG |
1328 | } |
1329 | ||
1330 | end_no_session: | |
1331 | return ret; | |
1332 | } | |
1333 | ||
93ec662e JD |
1334 | /* |
1335 | * relay_reset_metadata: reset a metadata stream | |
1336 | */ | |
1337 | static | |
1338 | int relay_reset_metadata(struct lttcomm_relayd_hdr *recv_hdr, | |
1339 | struct relay_connection *conn) | |
1340 | { | |
1341 | int ret, send_ret; | |
1342 | struct relay_session *session = conn->session; | |
1343 | struct lttcomm_relayd_reset_metadata stream_info; | |
1344 | struct lttcomm_relayd_generic_reply reply; | |
1345 | struct relay_stream *stream; | |
1346 | ||
1347 | DBG("Reset metadata received"); | |
1348 | ||
1349 | if (!session || conn->version_check_done == 0) { | |
1350 | ERR("Trying to reset a metadata stream before version check"); | |
1351 | ret = -1; | |
1352 | goto end_no_session; | |
1353 | } | |
1354 | ||
1355 | ret = conn->sock->ops->recvmsg(conn->sock, &stream_info, | |
1356 | sizeof(struct lttcomm_relayd_reset_metadata), 0); | |
1357 | if (ret < sizeof(struct lttcomm_relayd_reset_metadata)) { | |
1358 | if (ret == 0) { | |
1359 | /* Orderly shutdown. Not necessary to print an error. */ | |
1360 | DBG("Socket %d did an orderly shutdown", conn->sock->fd); | |
1361 | } else { | |
1362 | ERR("Relay didn't receive valid reset_metadata struct " | |
1363 | "size : %d", ret); | |
1364 | } | |
1365 | ret = -1; | |
1366 | goto end_no_session; | |
1367 | } | |
1368 | DBG("Update metadata to version %" PRIu64, be64toh(stream_info.version)); | |
1369 | ||
1370 | /* Unsupported for live sessions for now. */ | |
1371 | if (session->live_timer != 0) { | |
1372 | ret = -1; | |
1373 | goto end; | |
1374 | } | |
1375 | ||
1376 | stream = stream_get_by_id(be64toh(stream_info.stream_id)); | |
1377 | if (!stream) { | |
1378 | ret = -1; | |
1379 | goto end; | |
1380 | } | |
1381 | pthread_mutex_lock(&stream->lock); | |
1382 | if (!stream->is_metadata) { | |
1383 | ret = -1; | |
1384 | goto end_unlock; | |
1385 | } | |
1386 | ||
1387 | ret = utils_rotate_stream_file(stream->path_name, stream->channel_name, | |
1388 | 0, 0, -1, -1, stream->stream_fd->fd, NULL, | |
1389 | &stream->stream_fd->fd); | |
1390 | if (ret < 0) { | |
1391 | ERR("Failed to rotate metadata file %s of channel %s", | |
1392 | stream->path_name, stream->channel_name); | |
1393 | goto end_unlock; | |
1394 | } | |
1395 | ||
1396 | end_unlock: | |
1397 | pthread_mutex_unlock(&stream->lock); | |
1398 | stream_put(stream); | |
1399 | ||
1400 | end: | |
1401 | memset(&reply, 0, sizeof(reply)); | |
1402 | if (ret < 0) { | |
1403 | reply.ret_code = htobe32(LTTNG_ERR_UNK); | |
1404 | } else { | |
1405 | reply.ret_code = htobe32(LTTNG_OK); | |
1406 | } | |
1407 | send_ret = conn->sock->ops->sendmsg(conn->sock, &reply, | |
1408 | sizeof(struct lttcomm_relayd_generic_reply), 0); | |
1409 | if (send_ret < 0) { | |
1410 | ERR("Relay sending reset metadata reply"); | |
1411 | ret = send_ret; | |
1412 | } | |
1413 | ||
1414 | end_no_session: | |
1415 | return ret; | |
1416 | } | |
1417 | ||
b8aa1682 JD |
1418 | /* |
1419 | * relay_unknown_command: send -1 if received unknown command | |
1420 | */ | |
7591bab1 | 1421 | static void relay_unknown_command(struct relay_connection *conn) |
b8aa1682 JD |
1422 | { |
1423 | struct lttcomm_relayd_generic_reply reply; | |
1424 | int ret; | |
1425 | ||
53efb85a | 1426 | memset(&reply, 0, sizeof(reply)); |
f73fabfd | 1427 | reply.ret_code = htobe32(LTTNG_ERR_UNK); |
58eb9381 | 1428 | ret = conn->sock->ops->sendmsg(conn->sock, &reply, |
b8aa1682 JD |
1429 | sizeof(struct lttcomm_relayd_generic_reply), 0); |
1430 | if (ret < 0) { | |
1431 | ERR("Relay sending unknown command"); | |
1432 | } | |
1433 | } | |
1434 | ||
1435 | /* | |
1436 | * relay_start: send an acknowledgment to the client to tell if we are | |
1437 | * ready to receive data. We are ready if a session is established. | |
1438 | */ | |
7591bab1 | 1439 | static int relay_start(struct lttcomm_relayd_hdr *recv_hdr, |
58eb9381 | 1440 | struct relay_connection *conn) |
b8aa1682 | 1441 | { |
f73fabfd | 1442 | int ret = htobe32(LTTNG_OK); |
b8aa1682 | 1443 | struct lttcomm_relayd_generic_reply reply; |
58eb9381 | 1444 | struct relay_session *session = conn->session; |
b8aa1682 JD |
1445 | |
1446 | if (!session) { | |
1447 | DBG("Trying to start the streaming without a session established"); | |
f73fabfd | 1448 | ret = htobe32(LTTNG_ERR_UNK); |
b8aa1682 JD |
1449 | } |
1450 | ||
53efb85a | 1451 | memset(&reply, 0, sizeof(reply)); |
b8aa1682 | 1452 | reply.ret_code = ret; |
58eb9381 | 1453 | ret = conn->sock->ops->sendmsg(conn->sock, &reply, |
b8aa1682 JD |
1454 | sizeof(struct lttcomm_relayd_generic_reply), 0); |
1455 | if (ret < 0) { | |
1456 | ERR("Relay sending start ack"); | |
1457 | } | |
1458 | ||
1459 | return ret; | |
1460 | } | |
1461 | ||
1d4dfdef DG |
1462 | /* |
1463 | * Append padding to the file pointed by the file descriptor fd. | |
1464 | */ | |
1465 | static int write_padding_to_file(int fd, uint32_t size) | |
1466 | { | |
6cd525e8 | 1467 | ssize_t ret = 0; |
1d4dfdef DG |
1468 | char *zeros; |
1469 | ||
1470 | if (size == 0) { | |
1471 | goto end; | |
1472 | } | |
1473 | ||
1474 | zeros = zmalloc(size); | |
1475 | if (zeros == NULL) { | |
1476 | PERROR("zmalloc zeros for padding"); | |
1477 | ret = -1; | |
1478 | goto end; | |
1479 | } | |
1480 | ||
6cd525e8 MD |
1481 | ret = lttng_write(fd, zeros, size); |
1482 | if (ret < size) { | |
1d4dfdef DG |
1483 | PERROR("write padding to file"); |
1484 | } | |
1485 | ||
e986c7a1 DG |
1486 | free(zeros); |
1487 | ||
1d4dfdef DG |
1488 | end: |
1489 | return ret; | |
1490 | } | |
1491 | ||
b8aa1682 | 1492 | /* |
7591bab1 | 1493 | * relay_recv_metadata: receive the metadata for the session. |
b8aa1682 | 1494 | */ |
7591bab1 | 1495 | static int relay_recv_metadata(struct lttcomm_relayd_hdr *recv_hdr, |
58eb9381 | 1496 | struct relay_connection *conn) |
b8aa1682 | 1497 | { |
32d1569c | 1498 | int ret = 0; |
6cd525e8 | 1499 | ssize_t size_ret; |
58eb9381 | 1500 | struct relay_session *session = conn->session; |
b8aa1682 JD |
1501 | struct lttcomm_relayd_metadata_payload *metadata_struct; |
1502 | struct relay_stream *metadata_stream; | |
1503 | uint64_t data_size, payload_size; | |
1504 | ||
1505 | if (!session) { | |
1506 | ERR("Metadata sent before version check"); | |
1507 | ret = -1; | |
1508 | goto end; | |
1509 | } | |
1510 | ||
f6416125 MD |
1511 | data_size = payload_size = be64toh(recv_hdr->data_size); |
1512 | if (data_size < sizeof(struct lttcomm_relayd_metadata_payload)) { | |
1513 | ERR("Incorrect data size"); | |
1514 | ret = -1; | |
1515 | goto end; | |
1516 | } | |
1517 | payload_size -= sizeof(struct lttcomm_relayd_metadata_payload); | |
1518 | ||
b8aa1682 | 1519 | if (data_buffer_size < data_size) { |
d7b3776f | 1520 | /* In case the realloc fails, we can free the memory */ |
c617c0c6 MD |
1521 | char *tmp_data_ptr; |
1522 | ||
1523 | tmp_data_ptr = realloc(data_buffer, data_size); | |
1524 | if (!tmp_data_ptr) { | |
b8aa1682 | 1525 | ERR("Allocating data buffer"); |
c617c0c6 | 1526 | free(data_buffer); |
b8aa1682 JD |
1527 | ret = -1; |
1528 | goto end; | |
1529 | } | |
c617c0c6 | 1530 | data_buffer = tmp_data_ptr; |
b8aa1682 JD |
1531 | data_buffer_size = data_size; |
1532 | } | |
1533 | memset(data_buffer, 0, data_size); | |
77c7c900 | 1534 | DBG2("Relay receiving metadata, waiting for %" PRIu64 " bytes", data_size); |
32d1569c JG |
1535 | size_ret = conn->sock->ops->recvmsg(conn->sock, data_buffer, data_size, 0); |
1536 | if (size_ret < 0 || size_ret != data_size) { | |
1537 | if (size_ret == 0) { | |
a6cd2b97 | 1538 | /* Orderly shutdown. Not necessary to print an error. */ |
58eb9381 | 1539 | DBG("Socket %d did an orderly shutdown", conn->sock->fd); |
a6cd2b97 DG |
1540 | } else { |
1541 | ERR("Relay didn't receive the whole metadata"); | |
1542 | } | |
b8aa1682 | 1543 | ret = -1; |
b8aa1682 JD |
1544 | goto end; |
1545 | } | |
1546 | metadata_struct = (struct lttcomm_relayd_metadata_payload *) data_buffer; | |
9d1bbf21 | 1547 | |
7591bab1 | 1548 | metadata_stream = stream_get_by_id(be64toh(metadata_struct->stream_id)); |
b8aa1682 JD |
1549 | if (!metadata_stream) { |
1550 | ret = -1; | |
7591bab1 | 1551 | goto end; |
b8aa1682 JD |
1552 | } |
1553 | ||
7591bab1 MD |
1554 | pthread_mutex_lock(&metadata_stream->lock); |
1555 | ||
1556 | size_ret = lttng_write(metadata_stream->stream_fd->fd, metadata_struct->payload, | |
6cd525e8 MD |
1557 | payload_size); |
1558 | if (size_ret < payload_size) { | |
b8aa1682 JD |
1559 | ERR("Relay error writing metadata on file"); |
1560 | ret = -1; | |
7591bab1 | 1561 | goto end_put; |
b8aa1682 | 1562 | } |
1d4dfdef | 1563 | |
32d1569c | 1564 | size_ret = write_padding_to_file(metadata_stream->stream_fd->fd, |
1d4dfdef | 1565 | be32toh(metadata_struct->padding_size)); |
32d1569c | 1566 | if (size_ret < 0) { |
7591bab1 | 1567 | goto end_put; |
1d4dfdef | 1568 | } |
2a174661 | 1569 | |
7591bab1 | 1570 | metadata_stream->metadata_received += |
d3e2ba59 | 1571 | payload_size + be32toh(metadata_struct->padding_size); |
7591bab1 MD |
1572 | DBG2("Relay metadata written. Updated metadata_received %" PRIu64, |
1573 | metadata_stream->metadata_received); | |
1d4dfdef | 1574 | |
7591bab1 MD |
1575 | end_put: |
1576 | pthread_mutex_unlock(&metadata_stream->lock); | |
1577 | stream_put(metadata_stream); | |
b8aa1682 JD |
1578 | end: |
1579 | return ret; | |
1580 | } | |
1581 | ||
1582 | /* | |
1583 | * relay_send_version: send relayd version number | |
1584 | */ | |
7591bab1 | 1585 | static int relay_send_version(struct lttcomm_relayd_hdr *recv_hdr, |
58eb9381 | 1586 | struct relay_connection *conn) |
b8aa1682 | 1587 | { |
7f51dcba | 1588 | int ret; |
092b6259 | 1589 | struct lttcomm_relayd_version reply, msg; |
b8aa1682 | 1590 | |
58eb9381 | 1591 | conn->version_check_done = 1; |
b8aa1682 | 1592 | |
092b6259 | 1593 | /* Get version from the other side. */ |
58eb9381 | 1594 | ret = conn->sock->ops->recvmsg(conn->sock, &msg, sizeof(msg), 0); |
092b6259 | 1595 | if (ret < 0 || ret != sizeof(msg)) { |
a6cd2b97 DG |
1596 | if (ret == 0) { |
1597 | /* Orderly shutdown. Not necessary to print an error. */ | |
58eb9381 | 1598 | DBG("Socket %d did an orderly shutdown", conn->sock->fd); |
a6cd2b97 DG |
1599 | } else { |
1600 | ERR("Relay failed to receive the version values."); | |
1601 | } | |
092b6259 | 1602 | ret = -1; |
092b6259 DG |
1603 | goto end; |
1604 | } | |
1605 | ||
53efb85a | 1606 | memset(&reply, 0, sizeof(reply)); |
d83a952c MD |
1607 | reply.major = RELAYD_VERSION_COMM_MAJOR; |
1608 | reply.minor = RELAYD_VERSION_COMM_MINOR; | |
d4519fa3 JD |
1609 | |
1610 | /* Major versions must be the same */ | |
1611 | if (reply.major != be32toh(msg.major)) { | |
6151a90f JD |
1612 | DBG("Incompatible major versions (%u vs %u), deleting session", |
1613 | reply.major, be32toh(msg.major)); | |
7591bab1 | 1614 | connection_put(conn); |
d4519fa3 JD |
1615 | ret = 0; |
1616 | goto end; | |
1617 | } | |
1618 | ||
58eb9381 | 1619 | conn->major = reply.major; |
0f907de1 JD |
1620 | /* We adapt to the lowest compatible version */ |
1621 | if (reply.minor <= be32toh(msg.minor)) { | |
58eb9381 | 1622 | conn->minor = reply.minor; |
0f907de1 | 1623 | } else { |
58eb9381 | 1624 | conn->minor = be32toh(msg.minor); |
0f907de1 JD |
1625 | } |
1626 | ||
6151a90f JD |
1627 | reply.major = htobe32(reply.major); |
1628 | reply.minor = htobe32(reply.minor); | |
58eb9381 | 1629 | ret = conn->sock->ops->sendmsg(conn->sock, &reply, |
6151a90f JD |
1630 | sizeof(struct lttcomm_relayd_version), 0); |
1631 | if (ret < 0) { | |
1632 | ERR("Relay sending version"); | |
1633 | } | |
1634 | ||
58eb9381 DG |
1635 | DBG("Version check done using protocol %u.%u", conn->major, |
1636 | conn->minor); | |
b8aa1682 JD |
1637 | |
1638 | end: | |
1639 | return ret; | |
1640 | } | |
1641 | ||
c8f59ee5 | 1642 | /* |
6d805429 | 1643 | * Check for data pending for a given stream id from the session daemon. |
c8f59ee5 | 1644 | */ |
7591bab1 | 1645 | static int relay_data_pending(struct lttcomm_relayd_hdr *recv_hdr, |
58eb9381 | 1646 | struct relay_connection *conn) |
c8f59ee5 | 1647 | { |
58eb9381 | 1648 | struct relay_session *session = conn->session; |
6d805429 | 1649 | struct lttcomm_relayd_data_pending msg; |
c8f59ee5 DG |
1650 | struct lttcomm_relayd_generic_reply reply; |
1651 | struct relay_stream *stream; | |
1652 | int ret; | |
c8f59ee5 DG |
1653 | uint64_t last_net_seq_num, stream_id; |
1654 | ||
6d805429 | 1655 | DBG("Data pending command received"); |
c8f59ee5 | 1656 | |
58eb9381 | 1657 | if (!session || conn->version_check_done == 0) { |
c8f59ee5 DG |
1658 | ERR("Trying to check for data before version check"); |
1659 | ret = -1; | |
1660 | goto end_no_session; | |
1661 | } | |
1662 | ||
58eb9381 | 1663 | ret = conn->sock->ops->recvmsg(conn->sock, &msg, sizeof(msg), 0); |
c8f59ee5 | 1664 | if (ret < sizeof(msg)) { |
a6cd2b97 DG |
1665 | if (ret == 0) { |
1666 | /* Orderly shutdown. Not necessary to print an error. */ | |
58eb9381 | 1667 | DBG("Socket %d did an orderly shutdown", conn->sock->fd); |
a6cd2b97 DG |
1668 | } else { |
1669 | ERR("Relay didn't receive valid data_pending struct size : %d", | |
1670 | ret); | |
1671 | } | |
c8f59ee5 DG |
1672 | ret = -1; |
1673 | goto end_no_session; | |
1674 | } | |
1675 | ||
1676 | stream_id = be64toh(msg.stream_id); | |
1677 | last_net_seq_num = be64toh(msg.last_net_seq_num); | |
1678 | ||
7591bab1 | 1679 | stream = stream_get_by_id(stream_id); |
de91f48a | 1680 | if (stream == NULL) { |
c8f59ee5 | 1681 | ret = -1; |
7591bab1 | 1682 | goto end; |
c8f59ee5 DG |
1683 | } |
1684 | ||
7591bab1 MD |
1685 | pthread_mutex_lock(&stream->lock); |
1686 | ||
6d805429 | 1687 | DBG("Data pending for stream id %" PRIu64 " prev_seq %" PRIu64 |
c8f59ee5 DG |
1688 | " and last_seq %" PRIu64, stream_id, stream->prev_seq, |
1689 | last_net_seq_num); | |
1690 | ||
33832e64 | 1691 | /* Avoid wrapping issue */ |
39df6d9f | 1692 | if (((int64_t) (stream->prev_seq - last_net_seq_num)) >= 0) { |
6d805429 | 1693 | /* Data has in fact been written and is NOT pending */ |
c8f59ee5 | 1694 | ret = 0; |
6d805429 DG |
1695 | } else { |
1696 | /* Data still being streamed thus pending */ | |
1697 | ret = 1; | |
c8f59ee5 DG |
1698 | } |
1699 | ||
7591bab1 MD |
1700 | stream->data_pending_check_done = true; |
1701 | pthread_mutex_unlock(&stream->lock); | |
f7079f67 | 1702 | |
7591bab1 MD |
1703 | stream_put(stream); |
1704 | end: | |
c8f59ee5 | 1705 | |
53efb85a | 1706 | memset(&reply, 0, sizeof(reply)); |
c8f59ee5 | 1707 | reply.ret_code = htobe32(ret); |
58eb9381 | 1708 | ret = conn->sock->ops->sendmsg(conn->sock, &reply, sizeof(reply), 0); |
c8f59ee5 | 1709 | if (ret < 0) { |
6d805429 | 1710 | ERR("Relay data pending ret code failed"); |
c8f59ee5 DG |
1711 | } |
1712 | ||
1713 | end_no_session: | |
1714 | return ret; | |
1715 | } | |
1716 | ||
1717 | /* | |
1718 | * Wait for the control socket to reach a quiescent state. | |
1719 | * | |
7591bab1 MD |
1720 | * Note that for now, when receiving this command from the session |
1721 | * daemon, this means that every subsequent commands or data received on | |
1722 | * the control socket has been handled. So, this is why we simply return | |
1723 | * OK here. | |
c8f59ee5 | 1724 | */ |
7591bab1 | 1725 | static int relay_quiescent_control(struct lttcomm_relayd_hdr *recv_hdr, |
58eb9381 | 1726 | struct relay_connection *conn) |
c8f59ee5 DG |
1727 | { |
1728 | int ret; | |
ad7051c0 DG |
1729 | uint64_t stream_id; |
1730 | struct relay_stream *stream; | |
ad7051c0 | 1731 | struct lttcomm_relayd_quiescent_control msg; |
c8f59ee5 DG |
1732 | struct lttcomm_relayd_generic_reply reply; |
1733 | ||
1734 | DBG("Checking quiescent state on control socket"); | |
1735 | ||
58eb9381 | 1736 | if (!conn->session || conn->version_check_done == 0) { |
ad7051c0 DG |
1737 | ERR("Trying to check for data before version check"); |
1738 | ret = -1; | |
1739 | goto end_no_session; | |
1740 | } | |
1741 | ||
58eb9381 | 1742 | ret = conn->sock->ops->recvmsg(conn->sock, &msg, sizeof(msg), 0); |
ad7051c0 | 1743 | if (ret < sizeof(msg)) { |
a6cd2b97 DG |
1744 | if (ret == 0) { |
1745 | /* Orderly shutdown. Not necessary to print an error. */ | |
58eb9381 | 1746 | DBG("Socket %d did an orderly shutdown", conn->sock->fd); |
a6cd2b97 DG |
1747 | } else { |
1748 | ERR("Relay didn't receive valid begin data_pending struct size: %d", | |
1749 | ret); | |
1750 | } | |
ad7051c0 DG |
1751 | ret = -1; |
1752 | goto end_no_session; | |
1753 | } | |
1754 | ||
1755 | stream_id = be64toh(msg.stream_id); | |
7591bab1 MD |
1756 | stream = stream_get_by_id(stream_id); |
1757 | if (!stream) { | |
1758 | goto reply; | |
1759 | } | |
1760 | pthread_mutex_lock(&stream->lock); | |
1761 | stream->data_pending_check_done = true; | |
1762 | pthread_mutex_unlock(&stream->lock); | |
1763 | DBG("Relay quiescent control pending flag set to %" PRIu64, stream_id); | |
1764 | stream_put(stream); | |
1765 | reply: | |
53efb85a | 1766 | memset(&reply, 0, sizeof(reply)); |
c8f59ee5 | 1767 | reply.ret_code = htobe32(LTTNG_OK); |
58eb9381 | 1768 | ret = conn->sock->ops->sendmsg(conn->sock, &reply, sizeof(reply), 0); |
c8f59ee5 | 1769 | if (ret < 0) { |
6d805429 | 1770 | ERR("Relay data quiescent control ret code failed"); |
c8f59ee5 DG |
1771 | } |
1772 | ||
ad7051c0 | 1773 | end_no_session: |
c8f59ee5 DG |
1774 | return ret; |
1775 | } | |
1776 | ||
f7079f67 | 1777 | /* |
7591bab1 MD |
1778 | * Initialize a data pending command. This means that a consumer is about |
1779 | * to ask for data pending for each stream it holds. Simply iterate over | |
1780 | * all streams of a session and set the data_pending_check_done flag. | |
f7079f67 DG |
1781 | * |
1782 | * This command returns to the client a LTTNG_OK code. | |
1783 | */ | |
7591bab1 | 1784 | static int relay_begin_data_pending(struct lttcomm_relayd_hdr *recv_hdr, |
58eb9381 | 1785 | struct relay_connection *conn) |
f7079f67 DG |
1786 | { |
1787 | int ret; | |
1788 | struct lttng_ht_iter iter; | |
1789 | struct lttcomm_relayd_begin_data_pending msg; | |
1790 | struct lttcomm_relayd_generic_reply reply; | |
1791 | struct relay_stream *stream; | |
1792 | uint64_t session_id; | |
1793 | ||
1794 | assert(recv_hdr); | |
58eb9381 | 1795 | assert(conn); |
f7079f67 DG |
1796 | |
1797 | DBG("Init streams for data pending"); | |
1798 | ||
58eb9381 | 1799 | if (!conn->session || conn->version_check_done == 0) { |
f7079f67 DG |
1800 | ERR("Trying to check for data before version check"); |
1801 | ret = -1; | |
1802 | goto end_no_session; | |
1803 | } | |
1804 | ||
58eb9381 | 1805 | ret = conn->sock->ops->recvmsg(conn->sock, &msg, sizeof(msg), 0); |
f7079f67 | 1806 | if (ret < sizeof(msg)) { |
a6cd2b97 DG |
1807 | if (ret == 0) { |
1808 | /* Orderly shutdown. Not necessary to print an error. */ | |
58eb9381 | 1809 | DBG("Socket %d did an orderly shutdown", conn->sock->fd); |
a6cd2b97 DG |
1810 | } else { |
1811 | ERR("Relay didn't receive valid begin data_pending struct size: %d", | |
1812 | ret); | |
1813 | } | |
f7079f67 DG |
1814 | ret = -1; |
1815 | goto end_no_session; | |
1816 | } | |
1817 | ||
1818 | session_id = be64toh(msg.session_id); | |
1819 | ||
1820 | /* | |
7591bab1 MD |
1821 | * Iterate over all streams to set the begin data pending flag. |
1822 | * For now, the streams are indexed by stream handle so we have | |
1823 | * to iterate over all streams to find the one associated with | |
1824 | * the right session_id. | |
f7079f67 DG |
1825 | */ |
1826 | rcu_read_lock(); | |
d3e2ba59 | 1827 | cds_lfht_for_each_entry(relay_streams_ht->ht, &iter.iter, stream, |
2a174661 | 1828 | node.node) { |
7591bab1 MD |
1829 | if (!stream_get(stream)) { |
1830 | continue; | |
1831 | } | |
1832 | if (stream->trace->session->id == session_id) { | |
1833 | pthread_mutex_lock(&stream->lock); | |
1834 | stream->data_pending_check_done = false; | |
1835 | pthread_mutex_unlock(&stream->lock); | |
f7079f67 DG |
1836 | DBG("Set begin data pending flag to stream %" PRIu64, |
1837 | stream->stream_handle); | |
1838 | } | |
7591bab1 | 1839 | stream_put(stream); |
f7079f67 DG |
1840 | } |
1841 | rcu_read_unlock(); | |
1842 | ||
53efb85a | 1843 | memset(&reply, 0, sizeof(reply)); |
f7079f67 DG |
1844 | /* All good, send back reply. */ |
1845 | reply.ret_code = htobe32(LTTNG_OK); | |
1846 | ||
58eb9381 | 1847 | ret = conn->sock->ops->sendmsg(conn->sock, &reply, sizeof(reply), 0); |
f7079f67 DG |
1848 | if (ret < 0) { |
1849 | ERR("Relay begin data pending send reply failed"); | |
1850 | } | |
1851 | ||
1852 | end_no_session: | |
1853 | return ret; | |
1854 | } | |
1855 | ||
1856 | /* | |
7591bab1 MD |
1857 | * End data pending command. This will check, for a given session id, if |
1858 | * each stream associated with it has its data_pending_check_done flag | |
1859 | * set. If not, this means that the client lost track of the stream but | |
1860 | * the data is still being streamed on our side. In this case, we inform | |
1861 | * the client that data is in flight. | |
f7079f67 DG |
1862 | * |
1863 | * Return to the client if there is data in flight or not with a ret_code. | |
1864 | */ | |
7591bab1 | 1865 | static int relay_end_data_pending(struct lttcomm_relayd_hdr *recv_hdr, |
58eb9381 | 1866 | struct relay_connection *conn) |
f7079f67 DG |
1867 | { |
1868 | int ret; | |
1869 | struct lttng_ht_iter iter; | |
1870 | struct lttcomm_relayd_end_data_pending msg; | |
1871 | struct lttcomm_relayd_generic_reply reply; | |
1872 | struct relay_stream *stream; | |
1873 | uint64_t session_id; | |
1874 | uint32_t is_data_inflight = 0; | |
1875 | ||
f7079f67 DG |
1876 | DBG("End data pending command"); |
1877 | ||
58eb9381 | 1878 | if (!conn->session || conn->version_check_done == 0) { |
f7079f67 DG |
1879 | ERR("Trying to check for data before version check"); |
1880 | ret = -1; | |
1881 | goto end_no_session; | |
1882 | } | |
1883 | ||
58eb9381 | 1884 | ret = conn->sock->ops->recvmsg(conn->sock, &msg, sizeof(msg), 0); |
f7079f67 | 1885 | if (ret < sizeof(msg)) { |
a6cd2b97 DG |
1886 | if (ret == 0) { |
1887 | /* Orderly shutdown. Not necessary to print an error. */ | |
58eb9381 | 1888 | DBG("Socket %d did an orderly shutdown", conn->sock->fd); |
a6cd2b97 DG |
1889 | } else { |
1890 | ERR("Relay didn't receive valid end data_pending struct size: %d", | |
1891 | ret); | |
1892 | } | |
f7079f67 DG |
1893 | ret = -1; |
1894 | goto end_no_session; | |
1895 | } | |
1896 | ||
1897 | session_id = be64toh(msg.session_id); | |
1898 | ||
7591bab1 MD |
1899 | /* |
1900 | * Iterate over all streams to see if the begin data pending | |
1901 | * flag is set. | |
1902 | */ | |
f7079f67 | 1903 | rcu_read_lock(); |
d3e2ba59 | 1904 | cds_lfht_for_each_entry(relay_streams_ht->ht, &iter.iter, stream, |
2a174661 | 1905 | node.node) { |
7591bab1 MD |
1906 | if (!stream_get(stream)) { |
1907 | continue; | |
1908 | } | |
1909 | if (stream->trace->session->id != session_id) { | |
1910 | stream_put(stream); | |
1911 | continue; | |
1912 | } | |
1913 | pthread_mutex_lock(&stream->lock); | |
1914 | if (!stream->data_pending_check_done) { | |
1915 | if (!stream->closed || !(((int64_t) (stream->prev_seq - stream->last_net_seq_num)) >= 0)) { | |
1916 | is_data_inflight = 1; | |
1917 | DBG("Data is still in flight for stream %" PRIu64, | |
1918 | stream->stream_handle); | |
1919 | pthread_mutex_unlock(&stream->lock); | |
1920 | stream_put(stream); | |
1921 | break; | |
1922 | } | |
f7079f67 | 1923 | } |
7591bab1 MD |
1924 | pthread_mutex_unlock(&stream->lock); |
1925 | stream_put(stream); | |
f7079f67 DG |
1926 | } |
1927 | rcu_read_unlock(); | |
1928 | ||
53efb85a | 1929 | memset(&reply, 0, sizeof(reply)); |
f7079f67 DG |
1930 | /* All good, send back reply. */ |
1931 | reply.ret_code = htobe32(is_data_inflight); | |
1932 | ||
58eb9381 | 1933 | ret = conn->sock->ops->sendmsg(conn->sock, &reply, sizeof(reply), 0); |
f7079f67 DG |
1934 | if (ret < 0) { |
1935 | ERR("Relay end data pending send reply failed"); | |
1936 | } | |
1937 | ||
1938 | end_no_session: | |
1939 | return ret; | |
1940 | } | |
1941 | ||
1c20f0e2 JD |
1942 | /* |
1943 | * Receive an index for a specific stream. | |
1944 | * | |
1945 | * Return 0 on success else a negative value. | |
1946 | */ | |
7591bab1 | 1947 | static int relay_recv_index(struct lttcomm_relayd_hdr *recv_hdr, |
58eb9381 | 1948 | struct relay_connection *conn) |
1c20f0e2 | 1949 | { |
7591bab1 | 1950 | int ret, send_ret; |
58eb9381 | 1951 | struct relay_session *session = conn->session; |
1c20f0e2 | 1952 | struct lttcomm_relayd_index index_info; |
7591bab1 | 1953 | struct relay_index *index; |
1c20f0e2 JD |
1954 | struct lttcomm_relayd_generic_reply reply; |
1955 | struct relay_stream *stream; | |
1956 | uint64_t net_seq_num; | |
f8f3885c | 1957 | size_t msg_len; |
1c20f0e2 | 1958 | |
58eb9381 | 1959 | assert(conn); |
1c20f0e2 JD |
1960 | |
1961 | DBG("Relay receiving index"); | |
1962 | ||
58eb9381 | 1963 | if (!session || conn->version_check_done == 0) { |
1c20f0e2 JD |
1964 | ERR("Trying to close a stream before version check"); |
1965 | ret = -1; | |
1966 | goto end_no_session; | |
1967 | } | |
1968 | ||
f8f3885c MD |
1969 | msg_len = lttcomm_relayd_index_len( |
1970 | lttng_to_index_major(conn->major, conn->minor), | |
1971 | lttng_to_index_minor(conn->major, conn->minor)); | |
58eb9381 | 1972 | ret = conn->sock->ops->recvmsg(conn->sock, &index_info, |
f8f3885c MD |
1973 | msg_len, 0); |
1974 | if (ret < msg_len) { | |
1c20f0e2 JD |
1975 | if (ret == 0) { |
1976 | /* Orderly shutdown. Not necessary to print an error. */ | |
58eb9381 | 1977 | DBG("Socket %d did an orderly shutdown", conn->sock->fd); |
1c20f0e2 JD |
1978 | } else { |
1979 | ERR("Relay didn't receive valid index struct size : %d", ret); | |
1980 | } | |
1981 | ret = -1; | |
1982 | goto end_no_session; | |
1983 | } | |
1984 | ||
1985 | net_seq_num = be64toh(index_info.net_seq_num); | |
1986 | ||
7591bab1 | 1987 | stream = stream_get_by_id(be64toh(index_info.relay_stream_id)); |
1c20f0e2 | 1988 | if (!stream) { |
7591bab1 | 1989 | ERR("stream_get_by_id not found"); |
1c20f0e2 | 1990 | ret = -1; |
7591bab1 | 1991 | goto end; |
1c20f0e2 | 1992 | } |
7591bab1 | 1993 | pthread_mutex_lock(&stream->lock); |
1c20f0e2 | 1994 | |
d3e2ba59 JD |
1995 | /* Live beacon handling */ |
1996 | if (index_info.packet_size == 0) { | |
7591bab1 MD |
1997 | DBG("Received live beacon for stream %" PRIu64, |
1998 | stream->stream_handle); | |
d3e2ba59 JD |
1999 | |
2000 | /* | |
7591bab1 MD |
2001 | * Only flag a stream inactive when it has already |
2002 | * received data and no indexes are in flight. | |
d3e2ba59 | 2003 | */ |
a44ca2ca | 2004 | if (stream->index_received_seqcount > 0 |
7591bab1 MD |
2005 | && stream->indexes_in_flight == 0) { |
2006 | stream->beacon_ts_end = | |
2007 | be64toh(index_info.timestamp_end); | |
d3e2ba59 JD |
2008 | } |
2009 | ret = 0; | |
7591bab1 | 2010 | goto end_stream_put; |
d3e2ba59 JD |
2011 | } else { |
2012 | stream->beacon_ts_end = -1ULL; | |
2013 | } | |
2014 | ||
528f2ffa JD |
2015 | if (stream->ctf_stream_id == -1ULL) { |
2016 | stream->ctf_stream_id = be64toh(index_info.stream_id); | |
2017 | } | |
7591bab1 MD |
2018 | index = relay_index_get_by_id_or_create(stream, net_seq_num); |
2019 | if (!index) { | |
2020 | ret = -1; | |
2021 | ERR("relay_index_get_by_id_or_create index NULL"); | |
2022 | goto end_stream_put; | |
1c20f0e2 | 2023 | } |
234cd636 | 2024 | if (set_index_control_data(index, &index_info, conn)) { |
7591bab1 MD |
2025 | ERR("set_index_control_data error"); |
2026 | relay_index_put(index); | |
2027 | ret = -1; | |
2028 | goto end_stream_put; | |
2029 | } | |
2030 | ret = relay_index_try_flush(index); | |
2031 | if (ret == 0) { | |
a44ca2ca MD |
2032 | tracefile_array_commit_seq(stream->tfa); |
2033 | stream->index_received_seqcount++; | |
7591bab1 MD |
2034 | } else if (ret > 0) { |
2035 | /* no flush. */ | |
2036 | ret = 0; | |
2037 | } else { | |
2038 | ERR("relay_index_try_flush error %d", ret); | |
2039 | relay_index_put(index); | |
2040 | ret = -1; | |
1c20f0e2 JD |
2041 | } |
2042 | ||
7591bab1 MD |
2043 | end_stream_put: |
2044 | pthread_mutex_unlock(&stream->lock); | |
2045 | stream_put(stream); | |
2046 | ||
2047 | end: | |
1c20f0e2 | 2048 | |
53efb85a | 2049 | memset(&reply, 0, sizeof(reply)); |
1c20f0e2 JD |
2050 | if (ret < 0) { |
2051 | reply.ret_code = htobe32(LTTNG_ERR_UNK); | |
2052 | } else { | |
2053 | reply.ret_code = htobe32(LTTNG_OK); | |
2054 | } | |
58eb9381 | 2055 | send_ret = conn->sock->ops->sendmsg(conn->sock, &reply, sizeof(reply), 0); |
1c20f0e2 JD |
2056 | if (send_ret < 0) { |
2057 | ERR("Relay sending close index id reply"); | |
2058 | ret = send_ret; | |
2059 | } | |
2060 | ||
2061 | end_no_session: | |
2062 | return ret; | |
2063 | } | |
2064 | ||
a4baae1b JD |
2065 | /* |
2066 | * Receive the streams_sent message. | |
2067 | * | |
2068 | * Return 0 on success else a negative value. | |
2069 | */ | |
7591bab1 | 2070 | static int relay_streams_sent(struct lttcomm_relayd_hdr *recv_hdr, |
58eb9381 | 2071 | struct relay_connection *conn) |
a4baae1b JD |
2072 | { |
2073 | int ret, send_ret; | |
2074 | struct lttcomm_relayd_generic_reply reply; | |
2075 | ||
58eb9381 | 2076 | assert(conn); |
a4baae1b JD |
2077 | |
2078 | DBG("Relay receiving streams_sent"); | |
2079 | ||
58eb9381 | 2080 | if (!conn->session || conn->version_check_done == 0) { |
a4baae1b JD |
2081 | ERR("Trying to close a stream before version check"); |
2082 | ret = -1; | |
2083 | goto end_no_session; | |
2084 | } | |
2085 | ||
2086 | /* | |
7591bab1 MD |
2087 | * Publish every pending stream in the connection recv list which are |
2088 | * now ready to be used by the viewer. | |
4a9daf17 | 2089 | */ |
7591bab1 | 2090 | publish_connection_local_streams(conn); |
4a9daf17 | 2091 | |
53efb85a | 2092 | memset(&reply, 0, sizeof(reply)); |
a4baae1b | 2093 | reply.ret_code = htobe32(LTTNG_OK); |
58eb9381 | 2094 | send_ret = conn->sock->ops->sendmsg(conn->sock, &reply, sizeof(reply), 0); |
a4baae1b JD |
2095 | if (send_ret < 0) { |
2096 | ERR("Relay sending sent_stream reply"); | |
2097 | ret = send_ret; | |
2098 | } else { | |
2099 | /* Success. */ | |
2100 | ret = 0; | |
2101 | } | |
2102 | ||
2103 | end_no_session: | |
2104 | return ret; | |
2105 | } | |
2106 | ||
b8aa1682 | 2107 | /* |
d3e2ba59 | 2108 | * Process the commands received on the control socket |
b8aa1682 | 2109 | */ |
7591bab1 | 2110 | static int relay_process_control(struct lttcomm_relayd_hdr *recv_hdr, |
58eb9381 | 2111 | struct relay_connection *conn) |
b8aa1682 JD |
2112 | { |
2113 | int ret = 0; | |
2114 | ||
2115 | switch (be32toh(recv_hdr->cmd)) { | |
b8aa1682 | 2116 | case RELAYD_CREATE_SESSION: |
58eb9381 | 2117 | ret = relay_create_session(recv_hdr, conn); |
b8aa1682 | 2118 | break; |
b8aa1682 | 2119 | case RELAYD_ADD_STREAM: |
58eb9381 | 2120 | ret = relay_add_stream(recv_hdr, conn); |
b8aa1682 JD |
2121 | break; |
2122 | case RELAYD_START_DATA: | |
58eb9381 | 2123 | ret = relay_start(recv_hdr, conn); |
b8aa1682 JD |
2124 | break; |
2125 | case RELAYD_SEND_METADATA: | |
58eb9381 | 2126 | ret = relay_recv_metadata(recv_hdr, conn); |
b8aa1682 JD |
2127 | break; |
2128 | case RELAYD_VERSION: | |
58eb9381 | 2129 | ret = relay_send_version(recv_hdr, conn); |
b8aa1682 | 2130 | break; |
173af62f | 2131 | case RELAYD_CLOSE_STREAM: |
58eb9381 | 2132 | ret = relay_close_stream(recv_hdr, conn); |
173af62f | 2133 | break; |
6d805429 | 2134 | case RELAYD_DATA_PENDING: |
58eb9381 | 2135 | ret = relay_data_pending(recv_hdr, conn); |
c8f59ee5 DG |
2136 | break; |
2137 | case RELAYD_QUIESCENT_CONTROL: | |
58eb9381 | 2138 | ret = relay_quiescent_control(recv_hdr, conn); |
c8f59ee5 | 2139 | break; |
f7079f67 | 2140 | case RELAYD_BEGIN_DATA_PENDING: |
58eb9381 | 2141 | ret = relay_begin_data_pending(recv_hdr, conn); |
f7079f67 DG |
2142 | break; |
2143 | case RELAYD_END_DATA_PENDING: | |
58eb9381 | 2144 | ret = relay_end_data_pending(recv_hdr, conn); |
f7079f67 | 2145 | break; |
1c20f0e2 | 2146 | case RELAYD_SEND_INDEX: |
58eb9381 | 2147 | ret = relay_recv_index(recv_hdr, conn); |
1c20f0e2 | 2148 | break; |
a4baae1b | 2149 | case RELAYD_STREAMS_SENT: |
58eb9381 | 2150 | ret = relay_streams_sent(recv_hdr, conn); |
a4baae1b | 2151 | break; |
93ec662e JD |
2152 | case RELAYD_RESET_METADATA: |
2153 | ret = relay_reset_metadata(recv_hdr, conn); | |
2154 | break; | |
b8aa1682 JD |
2155 | case RELAYD_UPDATE_SYNC_INFO: |
2156 | default: | |
2157 | ERR("Received unknown command (%u)", be32toh(recv_hdr->cmd)); | |
58eb9381 | 2158 | relay_unknown_command(conn); |
b8aa1682 JD |
2159 | ret = -1; |
2160 | goto end; | |
2161 | } | |
2162 | ||
2163 | end: | |
2164 | return ret; | |
2165 | } | |
2166 | ||
7d2f7452 DG |
2167 | /* |
2168 | * Handle index for a data stream. | |
2169 | * | |
7591bab1 | 2170 | * Called with the stream lock held. |
7d2f7452 DG |
2171 | * |
2172 | * Return 0 on success else a negative value. | |
2173 | */ | |
2174 | static int handle_index_data(struct relay_stream *stream, uint64_t net_seq_num, | |
2175 | int rotate_index) | |
2176 | { | |
7591bab1 MD |
2177 | int ret = 0; |
2178 | uint64_t data_offset; | |
2179 | struct relay_index *index; | |
7d2f7452 | 2180 | |
7d2f7452 DG |
2181 | /* Get data offset because we are about to update the index. */ |
2182 | data_offset = htobe64(stream->tracefile_size_current); | |
2183 | ||
65d66b19 MD |
2184 | DBG("handle_index_data: stream %" PRIu64 " net_seq_num %" PRIu64 " data offset %" PRIu64, |
2185 | stream->stream_handle, net_seq_num, stream->tracefile_size_current); | |
7591bab1 | 2186 | |
7d2f7452 | 2187 | /* |
7591bab1 MD |
2188 | * Lookup for an existing index for that stream id/sequence |
2189 | * number. If it exists, the control thread has already received the | |
2190 | * data for it, thus we need to write it to disk. | |
7d2f7452 | 2191 | */ |
7591bab1 | 2192 | index = relay_index_get_by_id_or_create(stream, net_seq_num); |
7d2f7452 | 2193 | if (!index) { |
7591bab1 MD |
2194 | ret = -1; |
2195 | goto end; | |
7d2f7452 DG |
2196 | } |
2197 | ||
f8f3885c MD |
2198 | if (rotate_index || !stream->index_file) { |
2199 | uint32_t major, minor; | |
7591bab1 | 2200 | |
f8f3885c MD |
2201 | /* Put ref on previous index_file. */ |
2202 | if (stream->index_file) { | |
2203 | lttng_index_file_put(stream->index_file); | |
2204 | stream->index_file = NULL; | |
7d2f7452 | 2205 | } |
f8f3885c MD |
2206 | major = stream->trace->session->major; |
2207 | minor = stream->trace->session->minor; | |
2208 | stream->index_file = lttng_index_file_create(stream->path_name, | |
2209 | stream->channel_name, | |
7591bab1 | 2210 | -1, -1, stream->tracefile_size, |
f8f3885c MD |
2211 | tracefile_array_get_file_index_head(stream->tfa), |
2212 | lttng_to_index_major(major, minor), | |
2213 | lttng_to_index_minor(major, minor)); | |
2214 | if (!stream->index_file) { | |
7591bab1 MD |
2215 | ret = -1; |
2216 | /* Put self-ref for this index due to error. */ | |
2217 | relay_index_put(index); | |
f8f3885c | 2218 | index = NULL; |
7591bab1 | 2219 | goto end; |
7d2f7452 | 2220 | } |
7d2f7452 DG |
2221 | } |
2222 | ||
f8f3885c | 2223 | if (relay_index_set_file(index, stream->index_file, data_offset)) { |
7591bab1 MD |
2224 | ret = -1; |
2225 | /* Put self-ref for this index due to error. */ | |
2226 | relay_index_put(index); | |
f8f3885c | 2227 | index = NULL; |
7591bab1 | 2228 | goto end; |
7d2f7452 DG |
2229 | } |
2230 | ||
7591bab1 MD |
2231 | ret = relay_index_try_flush(index); |
2232 | if (ret == 0) { | |
a44ca2ca MD |
2233 | tracefile_array_commit_seq(stream->tfa); |
2234 | stream->index_received_seqcount++; | |
7591bab1 MD |
2235 | } else if (ret > 0) { |
2236 | /* No flush. */ | |
2237 | ret = 0; | |
2238 | } else { | |
2239 | /* Put self-ref for this index due to error. */ | |
2240 | relay_index_put(index); | |
f8f3885c | 2241 | index = NULL; |
7591bab1 MD |
2242 | ret = -1; |
2243 | } | |
2244 | end: | |
7d2f7452 DG |
2245 | return ret; |
2246 | } | |
2247 | ||
b8aa1682 JD |
2248 | /* |
2249 | * relay_process_data: Process the data received on the data socket | |
2250 | */ | |
7591bab1 | 2251 | static int relay_process_data(struct relay_connection *conn) |
b8aa1682 | 2252 | { |
7d2f7452 | 2253 | int ret = 0, rotate_index = 0; |
6cd525e8 | 2254 | ssize_t size_ret; |
b8aa1682 JD |
2255 | struct relay_stream *stream; |
2256 | struct lttcomm_relayd_data_hdr data_hdr; | |
7d2f7452 | 2257 | uint64_t stream_id; |
173af62f | 2258 | uint64_t net_seq_num; |
b8aa1682 | 2259 | uint32_t data_size; |
2a174661 | 2260 | struct relay_session *session; |
bda7c7b9 | 2261 | bool new_stream = false, close_requested = false; |
0848dba7 MD |
2262 | size_t chunk_size = RECV_DATA_BUFFER_SIZE; |
2263 | size_t recv_off = 0; | |
2264 | char data_buffer[chunk_size]; | |
b8aa1682 | 2265 | |
58eb9381 | 2266 | ret = conn->sock->ops->recvmsg(conn->sock, &data_hdr, |
7c5aef62 | 2267 | sizeof(struct lttcomm_relayd_data_hdr), 0); |
b8aa1682 | 2268 | if (ret <= 0) { |
a6cd2b97 DG |
2269 | if (ret == 0) { |
2270 | /* Orderly shutdown. Not necessary to print an error. */ | |
58eb9381 | 2271 | DBG("Socket %d did an orderly shutdown", conn->sock->fd); |
a6cd2b97 | 2272 | } else { |
58eb9381 | 2273 | ERR("Unable to receive data header on sock %d", conn->sock->fd); |
a6cd2b97 | 2274 | } |
b8aa1682 JD |
2275 | ret = -1; |
2276 | goto end; | |
2277 | } | |
2278 | ||
2279 | stream_id = be64toh(data_hdr.stream_id); | |
7591bab1 | 2280 | stream = stream_get_by_id(stream_id); |
b8aa1682 | 2281 | if (!stream) { |
65d66b19 | 2282 | ERR("relay_process_data: Cannot find stream %" PRIu64, stream_id); |
b8aa1682 | 2283 | ret = -1; |
7591bab1 | 2284 | goto end; |
b8aa1682 | 2285 | } |
7591bab1 | 2286 | session = stream->trace->session; |
b8aa1682 | 2287 | data_size = be32toh(data_hdr.data_size); |
b8aa1682 | 2288 | |
173af62f DG |
2289 | net_seq_num = be64toh(data_hdr.net_seq_num); |
2290 | ||
77c7c900 | 2291 | DBG3("Receiving data of size %u for stream id %" PRIu64 " seqnum %" PRIu64, |
173af62f | 2292 | data_size, stream_id, net_seq_num); |
b8aa1682 | 2293 | |
7591bab1 MD |
2294 | pthread_mutex_lock(&stream->lock); |
2295 | ||
1c20f0e2 | 2296 | /* Check if a rotation is needed. */ |
0f907de1 JD |
2297 | if (stream->tracefile_size > 0 && |
2298 | (stream->tracefile_size_current + data_size) > | |
2299 | stream->tracefile_size) { | |
a44ca2ca MD |
2300 | uint64_t old_id, new_id; |
2301 | ||
2302 | old_id = tracefile_array_get_file_index_head(stream->tfa); | |
2303 | tracefile_array_file_rotate(stream->tfa); | |
2304 | ||
2305 | /* new_id is updated by utils_rotate_stream_file. */ | |
2306 | new_id = old_id; | |
6b6b9a5a | 2307 | |
7591bab1 MD |
2308 | ret = utils_rotate_stream_file(stream->path_name, |
2309 | stream->channel_name, stream->tracefile_size, | |
2310 | stream->tracefile_count, -1, | |
2311 | -1, stream->stream_fd->fd, | |
a44ca2ca | 2312 | &new_id, &stream->stream_fd->fd); |
0f907de1 | 2313 | if (ret < 0) { |
1c20f0e2 | 2314 | ERR("Rotating stream output file"); |
7591bab1 MD |
2315 | goto end_stream_unlock; |
2316 | } | |
7591bab1 MD |
2317 | /* |
2318 | * Reset current size because we just performed a stream | |
2319 | * rotation. | |
2320 | */ | |
a6976990 | 2321 | stream->tracefile_size_current = 0; |
1c20f0e2 JD |
2322 | rotate_index = 1; |
2323 | } | |
2324 | ||
1c20f0e2 | 2325 | /* |
7591bab1 MD |
2326 | * Index are handled in protocol version 2.4 and above. Also, |
2327 | * snapshot and index are NOT supported. | |
1c20f0e2 | 2328 | */ |
2a174661 | 2329 | if (session->minor >= 4 && !session->snapshot) { |
7d2f7452 | 2330 | ret = handle_index_data(stream, net_seq_num, rotate_index); |
1c20f0e2 | 2331 | if (ret < 0) { |
65d66b19 MD |
2332 | ERR("handle_index_data: fail stream %" PRIu64 " net_seq_num %" PRIu64 " ret %d", |
2333 | stream->stream_handle, net_seq_num, ret); | |
7591bab1 | 2334 | goto end_stream_unlock; |
1c20f0e2 | 2335 | } |
1c20f0e2 JD |
2336 | } |
2337 | ||
0848dba7 MD |
2338 | for (recv_off = 0; recv_off < data_size; recv_off += chunk_size) { |
2339 | size_t recv_size = min(data_size - recv_off, chunk_size); | |
1d4dfdef | 2340 | |
0848dba7 MD |
2341 | ret = conn->sock->ops->recvmsg(conn->sock, data_buffer, recv_size, 0); |
2342 | if (ret <= 0) { | |
2343 | if (ret == 0) { | |
2344 | /* Orderly shutdown. Not necessary to print an error. */ | |
2345 | DBG("Socket %d did an orderly shutdown", conn->sock->fd); | |
2346 | } else { | |
2347 | ERR("Socket %d error %d", conn->sock->fd, ret); | |
2348 | } | |
2349 | ret = -1; | |
2350 | goto end_stream_unlock; | |
2351 | } | |
2352 | ||
2353 | /* Write data to stream output fd. */ | |
2354 | size_ret = lttng_write(stream->stream_fd->fd, data_buffer, | |
2355 | recv_size); | |
2356 | if (size_ret < recv_size) { | |
2357 | ERR("Relay error writing data to file"); | |
2358 | ret = -1; | |
2359 | goto end_stream_unlock; | |
2360 | } | |
2361 | ||
2362 | DBG2("Relay wrote %zd bytes to tracefile for stream id %" PRIu64, | |
2363 | size_ret, stream->stream_handle); | |
2364 | } | |
5ab7344e | 2365 | |
7591bab1 MD |
2366 | ret = write_padding_to_file(stream->stream_fd->fd, |
2367 | be32toh(data_hdr.padding_size)); | |
1d4dfdef | 2368 | if (ret < 0) { |
65d66b19 MD |
2369 | ERR("write_padding_to_file: fail stream %" PRIu64 " net_seq_num %" PRIu64 " ret %d", |
2370 | stream->stream_handle, net_seq_num, ret); | |
7591bab1 | 2371 | goto end_stream_unlock; |
1d4dfdef | 2372 | } |
7591bab1 MD |
2373 | stream->tracefile_size_current += |
2374 | data_size + be32toh(data_hdr.padding_size); | |
c0bae11d MD |
2375 | if (stream->prev_seq == -1ULL) { |
2376 | new_stream = true; | |
2377 | } | |
2378 | ||
173af62f DG |
2379 | stream->prev_seq = net_seq_num; |
2380 | ||
7591bab1 | 2381 | end_stream_unlock: |
bda7c7b9 | 2382 | close_requested = stream->close_requested; |
7591bab1 | 2383 | pthread_mutex_unlock(&stream->lock); |
bda7c7b9 JG |
2384 | if (close_requested) { |
2385 | try_stream_close(stream); | |
2386 | } | |
2387 | ||
c0bae11d MD |
2388 | if (new_stream) { |
2389 | pthread_mutex_lock(&session->lock); | |
2390 | uatomic_set(&session->new_streams, 1); | |
2391 | pthread_mutex_unlock(&session->lock); | |
2392 | } | |
7591bab1 | 2393 | stream_put(stream); |
b8aa1682 JD |
2394 | end: |
2395 | return ret; | |
2396 | } | |
2397 | ||
7591bab1 | 2398 | static void cleanup_connection_pollfd(struct lttng_poll_event *events, int pollfd) |
b8aa1682 JD |
2399 | { |
2400 | int ret; | |
2401 | ||
58eb9381 | 2402 | (void) lttng_poll_del(events, pollfd); |
b8aa1682 JD |
2403 | |
2404 | ret = close(pollfd); | |
2405 | if (ret < 0) { | |
2406 | ERR("Closing pollfd %d", pollfd); | |
2407 | } | |
2408 | } | |
2409 | ||
7591bab1 MD |
2410 | static void relay_thread_close_connection(struct lttng_poll_event *events, |
2411 | int pollfd, struct relay_connection *conn) | |
9d1bbf21 | 2412 | { |
7591bab1 | 2413 | const char *type_str; |
2a174661 | 2414 | |
7591bab1 MD |
2415 | switch (conn->type) { |
2416 | case RELAY_DATA: | |
2417 | type_str = "Data"; | |
2418 | break; | |
2419 | case RELAY_CONTROL: | |
2420 | type_str = "Control"; | |
2421 | break; | |
2422 | case RELAY_VIEWER_COMMAND: | |
2423 | type_str = "Viewer Command"; | |
2424 | break; | |
2425 | case RELAY_VIEWER_NOTIFICATION: | |
2426 | type_str = "Viewer Notification"; | |
2427 | break; | |
2428 | default: | |
2429 | type_str = "Unknown"; | |
9d1bbf21 | 2430 | } |
7591bab1 MD |
2431 | cleanup_connection_pollfd(events, pollfd); |
2432 | connection_put(conn); | |
2433 | DBG("%s connection closed with %d", type_str, pollfd); | |
b8aa1682 JD |
2434 | } |
2435 | ||
2436 | /* | |
2437 | * This thread does the actual work | |
2438 | */ | |
7591bab1 | 2439 | static void *relay_thread_worker(void *data) |
b8aa1682 | 2440 | { |
beaad64c DG |
2441 | int ret, err = -1, last_seen_data_fd = -1; |
2442 | uint32_t nb_fd; | |
b8aa1682 JD |
2443 | struct lttng_poll_event events; |
2444 | struct lttng_ht *relay_connections_ht; | |
b8aa1682 | 2445 | struct lttng_ht_iter iter; |
b8aa1682 | 2446 | struct lttcomm_relayd_hdr recv_hdr; |
90e7d72f | 2447 | struct relay_connection *destroy_conn = NULL; |
b8aa1682 JD |
2448 | |
2449 | DBG("[thread] Relay worker started"); | |
2450 | ||
9d1bbf21 MD |
2451 | rcu_register_thread(); |
2452 | ||
55706a7d MD |
2453 | health_register(health_relayd, HEALTH_RELAYD_TYPE_WORKER); |
2454 | ||
9b5e0863 MD |
2455 | if (testpoint(relayd_thread_worker)) { |
2456 | goto error_testpoint; | |
2457 | } | |
2458 | ||
f385ae0a MD |
2459 | health_code_update(); |
2460 | ||
b8aa1682 JD |
2461 | /* table of connections indexed on socket */ |
2462 | relay_connections_ht = lttng_ht_new(0, LTTNG_HT_TYPE_ULONG); | |
095a4ae5 MD |
2463 | if (!relay_connections_ht) { |
2464 | goto relay_connections_ht_error; | |
2465 | } | |
b8aa1682 | 2466 | |
b8aa1682 JD |
2467 | ret = create_thread_poll_set(&events, 2); |
2468 | if (ret < 0) { | |
2469 | goto error_poll_create; | |
2470 | } | |
2471 | ||
58eb9381 | 2472 | ret = lttng_poll_add(&events, relay_conn_pipe[0], LPOLLIN | LPOLLRDHUP); |
b8aa1682 JD |
2473 | if (ret < 0) { |
2474 | goto error; | |
2475 | } | |
2476 | ||
beaad64c | 2477 | restart: |
b8aa1682 | 2478 | while (1) { |
beaad64c DG |
2479 | int idx = -1, i, seen_control = 0, last_notdel_data_fd = -1; |
2480 | ||
f385ae0a MD |
2481 | health_code_update(); |
2482 | ||
b8aa1682 | 2483 | /* Infinite blocking call, waiting for transmission */ |
87c1611d | 2484 | DBG3("Relayd worker thread polling..."); |
f385ae0a | 2485 | health_poll_entry(); |
b8aa1682 | 2486 | ret = lttng_poll_wait(&events, -1); |
f385ae0a | 2487 | health_poll_exit(); |
b8aa1682 JD |
2488 | if (ret < 0) { |
2489 | /* | |
2490 | * Restart interrupted system call. | |
2491 | */ | |
2492 | if (errno == EINTR) { | |
2493 | goto restart; | |
2494 | } | |
2495 | goto error; | |
2496 | } | |
2497 | ||
0d9c5d77 DG |
2498 | nb_fd = ret; |
2499 | ||
beaad64c | 2500 | /* |
7591bab1 MD |
2501 | * Process control. The control connection is |
2502 | * prioritized so we don't starve it with high | |
2503 | * throughput tracing data on the data connection. | |
beaad64c | 2504 | */ |
b8aa1682 JD |
2505 | for (i = 0; i < nb_fd; i++) { |
2506 | /* Fetch once the poll data */ | |
beaad64c DG |
2507 | uint32_t revents = LTTNG_POLL_GETEV(&events, i); |
2508 | int pollfd = LTTNG_POLL_GETFD(&events, i); | |
b8aa1682 | 2509 | |
f385ae0a MD |
2510 | health_code_update(); |
2511 | ||
fd20dac9 | 2512 | if (!revents) { |
7591bab1 MD |
2513 | /* |
2514 | * No activity for this FD (poll | |
2515 | * implementation). | |
2516 | */ | |
fd20dac9 MD |
2517 | continue; |
2518 | } | |
2519 | ||
b8aa1682 JD |
2520 | /* Thread quit pipe has been closed. Killing thread. */ |
2521 | ret = check_thread_quit_pipe(pollfd, revents); | |
2522 | if (ret) { | |
095a4ae5 MD |
2523 | err = 0; |
2524 | goto exit; | |
b8aa1682 JD |
2525 | } |
2526 | ||
58eb9381 DG |
2527 | /* Inspect the relay conn pipe for new connection */ |
2528 | if (pollfd == relay_conn_pipe[0]) { | |
03e43155 | 2529 | if (revents & LPOLLIN) { |
90e7d72f JG |
2530 | struct relay_connection *conn; |
2531 | ||
58eb9381 | 2532 | ret = lttng_read(relay_conn_pipe[0], &conn, sizeof(conn)); |
b8aa1682 JD |
2533 | if (ret < 0) { |
2534 | goto error; | |
2535 | } | |
58eb9381 DG |
2536 | lttng_poll_add(&events, conn->sock->fd, |
2537 | LPOLLIN | LPOLLRDHUP); | |
7591bab1 | 2538 | connection_ht_add(relay_connections_ht, conn); |
58eb9381 | 2539 | DBG("Connection socket %d added", conn->sock->fd); |
03e43155 MD |
2540 | } else if (revents & (LPOLLERR | LPOLLHUP | LPOLLRDHUP)) { |
2541 | ERR("Relay connection pipe error"); | |
2542 | goto error; | |
2543 | } else { | |
2544 | ERR("Unexpected poll events %u for sock %d", revents, pollfd); | |
2545 | goto error; | |
b8aa1682 | 2546 | } |
58eb9381 | 2547 | } else { |
90e7d72f JG |
2548 | struct relay_connection *ctrl_conn; |
2549 | ||
7591bab1 | 2550 | ctrl_conn = connection_get_by_sock(relay_connections_ht, pollfd); |
58eb9381 | 2551 | /* If not found, there is a synchronization issue. */ |
90e7d72f | 2552 | assert(ctrl_conn); |
58eb9381 | 2553 | |
03e43155 MD |
2554 | if (ctrl_conn->type == RELAY_DATA) { |
2555 | if (revents & LPOLLIN) { | |
beaad64c DG |
2556 | /* |
2557 | * Flag the last seen data fd not deleted. It will be | |
2558 | * used as the last seen fd if any fd gets deleted in | |
2559 | * this first loop. | |
2560 | */ | |
2561 | last_notdel_data_fd = pollfd; | |
2562 | } | |
03e43155 MD |
2563 | goto put_ctrl_connection; |
2564 | } | |
2565 | assert(ctrl_conn->type == RELAY_CONTROL); | |
2566 | ||
2567 | if (revents & LPOLLIN) { | |
2568 | ret = ctrl_conn->sock->ops->recvmsg(ctrl_conn->sock, | |
2569 | &recv_hdr, sizeof(recv_hdr), 0); | |
2570 | if (ret <= 0) { | |
2571 | /* Connection closed */ | |
2572 | relay_thread_close_connection(&events, pollfd, | |
2573 | ctrl_conn); | |
2574 | } else { | |
2575 | ret = relay_process_control(&recv_hdr, ctrl_conn); | |
2576 | if (ret < 0) { | |
2577 | /* Clear the session on error. */ | |
2578 | relay_thread_close_connection(&events, | |
2579 | pollfd, ctrl_conn); | |
2580 | } | |
2581 | seen_control = 1; | |
2582 | } | |
2583 | } else if (revents & (LPOLLERR | LPOLLHUP | LPOLLRDHUP)) { | |
2584 | relay_thread_close_connection(&events, | |
2585 | pollfd, ctrl_conn); | |
2586 | if (last_seen_data_fd == pollfd) { | |
2587 | last_seen_data_fd = last_notdel_data_fd; | |
2588 | } | |
58eb9381 | 2589 | } else { |
03e43155 MD |
2590 | ERR("Unexpected poll events %u for control sock %d", |
2591 | revents, pollfd); | |
2592 | connection_put(ctrl_conn); | |
2593 | goto error; | |
beaad64c | 2594 | } |
03e43155 | 2595 | put_ctrl_connection: |
7591bab1 | 2596 | connection_put(ctrl_conn); |
beaad64c DG |
2597 | } |
2598 | } | |
2599 | ||
2600 | /* | |
2601 | * The last loop handled a control request, go back to poll to make | |
2602 | * sure we prioritise the control socket. | |
2603 | */ | |
2604 | if (seen_control) { | |
2605 | continue; | |
2606 | } | |
2607 | ||
2608 | if (last_seen_data_fd >= 0) { | |
2609 | for (i = 0; i < nb_fd; i++) { | |
2610 | int pollfd = LTTNG_POLL_GETFD(&events, i); | |
f385ae0a MD |
2611 | |
2612 | health_code_update(); | |
2613 | ||
beaad64c DG |
2614 | if (last_seen_data_fd == pollfd) { |
2615 | idx = i; | |
2616 | break; | |
2617 | } | |
2618 | } | |
2619 | } | |
2620 | ||
2621 | /* Process data connection. */ | |
2622 | for (i = idx + 1; i < nb_fd; i++) { | |
2623 | /* Fetch the poll data. */ | |
2624 | uint32_t revents = LTTNG_POLL_GETEV(&events, i); | |
2625 | int pollfd = LTTNG_POLL_GETFD(&events, i); | |
90e7d72f | 2626 | struct relay_connection *data_conn; |
beaad64c | 2627 | |
f385ae0a MD |
2628 | health_code_update(); |
2629 | ||
fd20dac9 MD |
2630 | if (!revents) { |
2631 | /* No activity for this FD (poll implementation). */ | |
2632 | continue; | |
2633 | } | |
2634 | ||
beaad64c | 2635 | /* Skip the command pipe. It's handled in the first loop. */ |
58eb9381 | 2636 | if (pollfd == relay_conn_pipe[0]) { |
beaad64c DG |
2637 | continue; |
2638 | } | |
2639 | ||
7591bab1 | 2640 | data_conn = connection_get_by_sock(relay_connections_ht, pollfd); |
90e7d72f | 2641 | if (!data_conn) { |
fd20dac9 | 2642 | /* Skip it. Might be removed before. */ |
fd20dac9 MD |
2643 | continue; |
2644 | } | |
03e43155 MD |
2645 | if (data_conn->type == RELAY_CONTROL) { |
2646 | goto put_data_connection; | |
2647 | } | |
2648 | assert(data_conn->type == RELAY_DATA); | |
fd20dac9 MD |
2649 | |
2650 | if (revents & LPOLLIN) { | |
90e7d72f | 2651 | ret = relay_process_data(data_conn); |
fd20dac9 MD |
2652 | /* Connection closed */ |
2653 | if (ret < 0) { | |
7591bab1 | 2654 | relay_thread_close_connection(&events, pollfd, |
03e43155 | 2655 | data_conn); |
fd20dac9 MD |
2656 | /* |
2657 | * Every goto restart call sets the last seen fd where | |
2658 | * here we don't really care since we gracefully | |
2659 | * continue the loop after the connection is deleted. | |
2660 | */ | |
2661 | } else { | |
2662 | /* Keep last seen port. */ | |
2663 | last_seen_data_fd = pollfd; | |
7591bab1 | 2664 | connection_put(data_conn); |
fd20dac9 | 2665 | goto restart; |
b8aa1682 | 2666 | } |
03e43155 MD |
2667 | } else if (revents & (LPOLLERR | LPOLLHUP | LPOLLRDHUP)) { |
2668 | relay_thread_close_connection(&events, pollfd, | |
2669 | data_conn); | |
2670 | } else { | |
2671 | ERR("Unknown poll events %u for data sock %d", | |
2672 | revents, pollfd); | |
b8aa1682 | 2673 | } |
03e43155 | 2674 | put_data_connection: |
7591bab1 | 2675 | connection_put(data_conn); |
b8aa1682 | 2676 | } |
beaad64c | 2677 | last_seen_data_fd = -1; |
b8aa1682 JD |
2678 | } |
2679 | ||
f385ae0a MD |
2680 | /* Normal exit, no error */ |
2681 | ret = 0; | |
2682 | ||
095a4ae5 | 2683 | exit: |
b8aa1682 | 2684 | error: |
58eb9381 | 2685 | /* Cleanup reamaining connection object. */ |
9d1bbf21 | 2686 | rcu_read_lock(); |
90e7d72f JG |
2687 | cds_lfht_for_each_entry(relay_connections_ht->ht, &iter.iter, |
2688 | destroy_conn, | |
58eb9381 | 2689 | sock_n.node) { |
f385ae0a | 2690 | health_code_update(); |
98ba050e JR |
2691 | |
2692 | if (session_abort(destroy_conn->session)) { | |
2693 | assert(0); | |
2694 | } | |
2695 | ||
7591bab1 MD |
2696 | /* |
2697 | * No need to grab another ref, because we own | |
2698 | * destroy_conn. | |
2699 | */ | |
2700 | relay_thread_close_connection(&events, destroy_conn->sock->fd, | |
2701 | destroy_conn); | |
b8aa1682 | 2702 | } |
94d49140 | 2703 | rcu_read_unlock(); |
7591bab1 MD |
2704 | |
2705 | lttng_poll_clean(&events); | |
7d2f7452 | 2706 | error_poll_create: |
b8aa1682 | 2707 | lttng_ht_destroy(relay_connections_ht); |
095a4ae5 | 2708 | relay_connections_ht_error: |
58eb9381 DG |
2709 | /* Close relay conn pipes */ |
2710 | utils_close_pipe(relay_conn_pipe); | |
095a4ae5 MD |
2711 | if (err) { |
2712 | DBG("Thread exited with error"); | |
2713 | } | |
b8aa1682 | 2714 | DBG("Worker thread cleanup complete"); |
9b5e0863 | 2715 | error_testpoint: |
f385ae0a MD |
2716 | if (err) { |
2717 | health_error(); | |
2718 | ERR("Health error occurred in %s", __func__); | |
2719 | } | |
2720 | health_unregister(health_relayd); | |
9d1bbf21 | 2721 | rcu_unregister_thread(); |
b4aacfdc | 2722 | lttng_relay_stop_threads(); |
b8aa1682 JD |
2723 | return NULL; |
2724 | } | |
2725 | ||
2726 | /* | |
2727 | * Create the relay command pipe to wake thread_manage_apps. | |
2728 | * Closed in cleanup(). | |
2729 | */ | |
58eb9381 | 2730 | static int create_relay_conn_pipe(void) |
b8aa1682 | 2731 | { |
a02de639 | 2732 | int ret; |
b8aa1682 | 2733 | |
58eb9381 | 2734 | ret = utils_create_pipe_cloexec(relay_conn_pipe); |
b8aa1682 | 2735 | |
b8aa1682 JD |
2736 | return ret; |
2737 | } | |
2738 | ||
2739 | /* | |
2740 | * main | |
2741 | */ | |
2742 | int main(int argc, char **argv) | |
2743 | { | |
178a0557 | 2744 | int ret = 0, retval = 0; |
b8aa1682 JD |
2745 | void *status; |
2746 | ||
b8aa1682 JD |
2747 | /* Parse arguments */ |
2748 | progname = argv[0]; | |
178a0557 MD |
2749 | if (set_options(argc, argv)) { |
2750 | retval = -1; | |
2751 | goto exit_options; | |
b8aa1682 JD |
2752 | } |
2753 | ||
178a0557 MD |
2754 | if (set_signal_handler()) { |
2755 | retval = -1; | |
2756 | goto exit_options; | |
b8aa1682 JD |
2757 | } |
2758 | ||
4d513a50 DG |
2759 | /* Try to create directory if -o, --output is specified. */ |
2760 | if (opt_output_path) { | |
994fa64f DG |
2761 | if (*opt_output_path != '/') { |
2762 | ERR("Please specify an absolute path for -o, --output PATH"); | |
178a0557 MD |
2763 | retval = -1; |
2764 | goto exit_options; | |
994fa64f DG |
2765 | } |
2766 | ||
d77dded2 JG |
2767 | ret = utils_mkdir_recursive(opt_output_path, S_IRWXU | S_IRWXG, |
2768 | -1, -1); | |
4d513a50 DG |
2769 | if (ret < 0) { |
2770 | ERR("Unable to create %s", opt_output_path); | |
178a0557 MD |
2771 | retval = -1; |
2772 | goto exit_options; | |
4d513a50 DG |
2773 | } |
2774 | } | |
2775 | ||
b8aa1682 | 2776 | /* Daemonize */ |
b5218ffb | 2777 | if (opt_daemon || opt_background) { |
3fd27398 MD |
2778 | int i; |
2779 | ||
2780 | ret = lttng_daemonize(&child_ppid, &recv_child_signal, | |
2781 | !opt_background); | |
b8aa1682 | 2782 | if (ret < 0) { |
178a0557 MD |
2783 | retval = -1; |
2784 | goto exit_options; | |
b8aa1682 | 2785 | } |
3fd27398 MD |
2786 | |
2787 | /* | |
2788 | * We are in the child. Make sure all other file | |
2789 | * descriptors are closed, in case we are called with | |
2790 | * more opened file descriptors than the standard ones. | |
2791 | */ | |
2792 | for (i = 3; i < sysconf(_SC_OPEN_MAX); i++) { | |
2793 | (void) close(i); | |
2794 | } | |
2795 | } | |
2796 | ||
178a0557 MD |
2797 | /* Initialize thread health monitoring */ |
2798 | health_relayd = health_app_create(NR_HEALTH_RELAYD_TYPES); | |
2799 | if (!health_relayd) { | |
2800 | PERROR("health_app_create error"); | |
2801 | retval = -1; | |
2802 | goto exit_health_app_create; | |
2803 | } | |
2804 | ||
3fd27398 | 2805 | /* Create thread quit pipe */ |
178a0557 MD |
2806 | if (init_thread_quit_pipe()) { |
2807 | retval = -1; | |
2808 | goto exit_init_data; | |
b8aa1682 JD |
2809 | } |
2810 | ||
b8aa1682 | 2811 | /* Setup the thread apps communication pipe. */ |
178a0557 MD |
2812 | if (create_relay_conn_pipe()) { |
2813 | retval = -1; | |
2814 | goto exit_init_data; | |
b8aa1682 JD |
2815 | } |
2816 | ||
2817 | /* Init relay command queue. */ | |
8bdee6e2 | 2818 | cds_wfcq_init(&relay_conn_queue.head, &relay_conn_queue.tail); |
b8aa1682 | 2819 | |
554831e7 MD |
2820 | /* Initialize communication library */ |
2821 | lttcomm_init(); | |
87e45c13 | 2822 | lttcomm_inet_init(); |
554831e7 | 2823 | |
d3e2ba59 | 2824 | /* tables of sessions indexed by session ID */ |
7591bab1 MD |
2825 | sessions_ht = lttng_ht_new(0, LTTNG_HT_TYPE_U64); |
2826 | if (!sessions_ht) { | |
178a0557 MD |
2827 | retval = -1; |
2828 | goto exit_init_data; | |
d3e2ba59 JD |
2829 | } |
2830 | ||
2831 | /* tables of streams indexed by stream ID */ | |
2a174661 | 2832 | relay_streams_ht = lttng_ht_new(0, LTTNG_HT_TYPE_U64); |
d3e2ba59 | 2833 | if (!relay_streams_ht) { |
178a0557 MD |
2834 | retval = -1; |
2835 | goto exit_init_data; | |
d3e2ba59 JD |
2836 | } |
2837 | ||
2838 | /* tables of streams indexed by stream ID */ | |
92c6ca54 DG |
2839 | viewer_streams_ht = lttng_ht_new(0, LTTNG_HT_TYPE_U64); |
2840 | if (!viewer_streams_ht) { | |
178a0557 MD |
2841 | retval = -1; |
2842 | goto exit_init_data; | |
55706a7d MD |
2843 | } |
2844 | ||
65931c8b | 2845 | ret = utils_create_pipe(health_quit_pipe); |
178a0557 MD |
2846 | if (ret) { |
2847 | retval = -1; | |
2848 | goto exit_health_quit_pipe; | |
65931c8b MD |
2849 | } |
2850 | ||
2851 | /* Create thread to manage the client socket */ | |
1a1a34b4 | 2852 | ret = pthread_create(&health_thread, default_pthread_attr(), |
65931c8b | 2853 | thread_manage_health, (void *) NULL); |
178a0557 MD |
2854 | if (ret) { |
2855 | errno = ret; | |
65931c8b | 2856 | PERROR("pthread_create health"); |
178a0557 MD |
2857 | retval = -1; |
2858 | goto exit_health_thread; | |
65931c8b MD |
2859 | } |
2860 | ||
b8aa1682 | 2861 | /* Setup the dispatcher thread */ |
1a1a34b4 | 2862 | ret = pthread_create(&dispatcher_thread, default_pthread_attr(), |
b8aa1682 | 2863 | relay_thread_dispatcher, (void *) NULL); |
178a0557 MD |
2864 | if (ret) { |
2865 | errno = ret; | |
b8aa1682 | 2866 | PERROR("pthread_create dispatcher"); |
178a0557 MD |
2867 | retval = -1; |
2868 | goto exit_dispatcher_thread; | |
b8aa1682 JD |
2869 | } |
2870 | ||
2871 | /* Setup the worker thread */ | |
1a1a34b4 | 2872 | ret = pthread_create(&worker_thread, default_pthread_attr(), |
7591bab1 | 2873 | relay_thread_worker, NULL); |
178a0557 MD |
2874 | if (ret) { |
2875 | errno = ret; | |
b8aa1682 | 2876 | PERROR("pthread_create worker"); |
178a0557 MD |
2877 | retval = -1; |
2878 | goto exit_worker_thread; | |
b8aa1682 JD |
2879 | } |
2880 | ||
2881 | /* Setup the listener thread */ | |
1a1a34b4 | 2882 | ret = pthread_create(&listener_thread, default_pthread_attr(), |
b8aa1682 | 2883 | relay_thread_listener, (void *) NULL); |
178a0557 MD |
2884 | if (ret) { |
2885 | errno = ret; | |
b8aa1682 | 2886 | PERROR("pthread_create listener"); |
178a0557 MD |
2887 | retval = -1; |
2888 | goto exit_listener_thread; | |
b8aa1682 JD |
2889 | } |
2890 | ||
7591bab1 | 2891 | ret = relayd_live_create(live_uri); |
178a0557 | 2892 | if (ret) { |
d3e2ba59 | 2893 | ERR("Starting live viewer threads"); |
178a0557 | 2894 | retval = -1; |
50138f51 | 2895 | goto exit_live; |
d3e2ba59 JD |
2896 | } |
2897 | ||
178a0557 MD |
2898 | /* |
2899 | * This is where we start awaiting program completion (e.g. through | |
2900 | * signal that asks threads to teardown). | |
2901 | */ | |
2902 | ||
2903 | ret = relayd_live_join(); | |
2904 | if (ret) { | |
2905 | retval = -1; | |
2906 | } | |
50138f51 | 2907 | exit_live: |
178a0557 | 2908 | |
b8aa1682 | 2909 | ret = pthread_join(listener_thread, &status); |
178a0557 MD |
2910 | if (ret) { |
2911 | errno = ret; | |
2912 | PERROR("pthread_join listener_thread"); | |
2913 | retval = -1; | |
b8aa1682 JD |
2914 | } |
2915 | ||
178a0557 | 2916 | exit_listener_thread: |
b8aa1682 | 2917 | ret = pthread_join(worker_thread, &status); |
178a0557 MD |
2918 | if (ret) { |
2919 | errno = ret; | |
2920 | PERROR("pthread_join worker_thread"); | |
2921 | retval = -1; | |
b8aa1682 JD |
2922 | } |
2923 | ||
178a0557 | 2924 | exit_worker_thread: |
b8aa1682 | 2925 | ret = pthread_join(dispatcher_thread, &status); |
178a0557 MD |
2926 | if (ret) { |
2927 | errno = ret; | |
2928 | PERROR("pthread_join dispatcher_thread"); | |
2929 | retval = -1; | |
b8aa1682 | 2930 | } |
178a0557 | 2931 | exit_dispatcher_thread: |
42415026 | 2932 | |
65931c8b | 2933 | ret = pthread_join(health_thread, &status); |
178a0557 MD |
2934 | if (ret) { |
2935 | errno = ret; | |
2936 | PERROR("pthread_join health_thread"); | |
2937 | retval = -1; | |
65931c8b | 2938 | } |
178a0557 | 2939 | exit_health_thread: |
65931c8b | 2940 | |
65931c8b | 2941 | utils_close_pipe(health_quit_pipe); |
178a0557 | 2942 | exit_health_quit_pipe: |
65931c8b | 2943 | |
178a0557 | 2944 | exit_init_data: |
55706a7d | 2945 | health_app_destroy(health_relayd); |
55706a7d | 2946 | exit_health_app_create: |
178a0557 | 2947 | exit_options: |
4d62fbf8 MD |
2948 | /* |
2949 | * Wait for all pending call_rcu work to complete before tearing | |
2950 | * down data structures. call_rcu worker may be trying to | |
2951 | * perform lookups in those structures. | |
2952 | */ | |
2953 | rcu_barrier(); | |
7591bab1 MD |
2954 | relayd_cleanup(); |
2955 | ||
2956 | /* Ensure all prior call_rcu are done. */ | |
2957 | rcu_barrier(); | |
d3e2ba59 | 2958 | |
178a0557 | 2959 | if (!retval) { |
b8aa1682 | 2960 | exit(EXIT_SUCCESS); |
178a0557 MD |
2961 | } else { |
2962 | exit(EXIT_FAILURE); | |
b8aa1682 | 2963 | } |
b8aa1682 | 2964 | } |