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