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