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