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