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