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> | |
d0ae499d | 40 | #include <urcu/rculist.h> |
b8aa1682 JD |
41 | #include <unistd.h> |
42 | #include <fcntl.h> | |
b8aa1682 JD |
43 | |
44 | #include <lttng/lttng.h> | |
45 | #include <common/common.h> | |
46 | #include <common/compat/poll.h> | |
47 | #include <common/compat/socket.h> | |
f263b7fd | 48 | #include <common/compat/endian.h> |
e8fa9fb0 | 49 | #include <common/compat/getenv.h> |
b8aa1682 | 50 | #include <common/defaults.h> |
3fd27398 | 51 | #include <common/daemonize.h> |
b8aa1682 JD |
52 | #include <common/futex.h> |
53 | #include <common/sessiond-comm/sessiond-comm.h> | |
54 | #include <common/sessiond-comm/inet.h> | |
b8aa1682 JD |
55 | #include <common/sessiond-comm/relayd.h> |
56 | #include <common/uri.h> | |
a02de639 | 57 | #include <common/utils.h> |
d3ecc550 | 58 | #include <common/align.h> |
f40ef1d5 | 59 | #include <common/config/session-config.h> |
5312a3ed JG |
60 | #include <common/dynamic-buffer.h> |
61 | #include <common/buffer-view.h> | |
d0ae499d | 62 | #include <common/string-utils/format.h> |
b8aa1682 | 63 | |
0f907de1 | 64 | #include "cmd.h" |
d3e2ba59 | 65 | #include "ctf-trace.h" |
1c20f0e2 | 66 | #include "index.h" |
0f907de1 | 67 | #include "utils.h" |
b8aa1682 | 68 | #include "lttng-relayd.h" |
d3e2ba59 | 69 | #include "live.h" |
55706a7d | 70 | #include "health-relayd.h" |
9b5e0863 | 71 | #include "testpoint.h" |
2f8f53af | 72 | #include "viewer-stream.h" |
2a174661 DG |
73 | #include "session.h" |
74 | #include "stream.h" | |
58eb9381 | 75 | #include "connection.h" |
a44ca2ca | 76 | #include "tracefile-array.h" |
f056029c | 77 | #include "tcp_keep_alive.h" |
1f916b5d | 78 | #include "sessiond-trace-chunks.h" |
b8aa1682 | 79 | |
4fc83d94 PP |
80 | static const char *help_msg = |
81 | #ifdef LTTNG_EMBED_HELP | |
82 | #include <lttng-relayd.8.h> | |
83 | #else | |
84 | NULL | |
85 | #endif | |
86 | ; | |
87 | ||
5569b118 JG |
88 | enum relay_connection_status { |
89 | RELAY_CONNECTION_STATUS_OK, | |
e6a80fab | 90 | /* An error occurred while processing an event on the connection. */ |
5569b118 JG |
91 | RELAY_CONNECTION_STATUS_ERROR, |
92 | /* Connection closed/shutdown cleanly. */ | |
93 | RELAY_CONNECTION_STATUS_CLOSED, | |
94 | }; | |
95 | ||
b8aa1682 | 96 | /* command line options */ |
0f907de1 | 97 | char *opt_output_path; |
b5218ffb | 98 | static int opt_daemon, opt_background; |
3fd27398 MD |
99 | |
100 | /* | |
101 | * We need to wait for listener and live listener threads, as well as | |
102 | * health check thread, before being ready to signal readiness. | |
103 | */ | |
104 | #define NR_LTTNG_RELAY_READY 3 | |
105 | static int lttng_relay_ready = NR_LTTNG_RELAY_READY; | |
0848dba7 MD |
106 | |
107 | /* Size of receive buffer. */ | |
108 | #define RECV_DATA_BUFFER_SIZE 65536 | |
109 | ||
3fd27398 MD |
110 | static int recv_child_signal; /* Set to 1 when a SIGUSR1 signal is received. */ |
111 | static pid_t child_ppid; /* Internal parent PID use with daemonize. */ | |
112 | ||
095a4ae5 MD |
113 | static struct lttng_uri *control_uri; |
114 | static struct lttng_uri *data_uri; | |
d3e2ba59 | 115 | static struct lttng_uri *live_uri; |
b8aa1682 JD |
116 | |
117 | const char *progname; | |
b8aa1682 | 118 | |
65931c8b | 119 | const char *tracing_group_name = DEFAULT_TRACING_GROUP; |
cd60b05a JG |
120 | static int tracing_group_name_override; |
121 | ||
122 | const char * const config_section_name = "relayd"; | |
65931c8b | 123 | |
b8aa1682 JD |
124 | /* |
125 | * Quit pipe for all threads. This permits a single cancellation point | |
126 | * for all threads when receiving an event on the pipe. | |
127 | */ | |
0b242f62 | 128 | int thread_quit_pipe[2] = { -1, -1 }; |
b8aa1682 JD |
129 | |
130 | /* | |
131 | * This pipe is used to inform the worker thread that a command is queued and | |
132 | * ready to be processed. | |
133 | */ | |
58eb9381 | 134 | static int relay_conn_pipe[2] = { -1, -1 }; |
b8aa1682 | 135 | |
26c9d55e | 136 | /* Shared between threads */ |
b8aa1682 JD |
137 | static int dispatch_thread_exit; |
138 | ||
139 | static pthread_t listener_thread; | |
140 | static pthread_t dispatcher_thread; | |
141 | static pthread_t worker_thread; | |
65931c8b | 142 | static pthread_t health_thread; |
b8aa1682 | 143 | |
7591bab1 MD |
144 | /* |
145 | * last_relay_stream_id_lock protects last_relay_stream_id increment | |
146 | * atomicity on 32-bit architectures. | |
147 | */ | |
148 | static pthread_mutex_t last_relay_stream_id_lock = PTHREAD_MUTEX_INITIALIZER; | |
095a4ae5 | 149 | static uint64_t last_relay_stream_id; |
b8aa1682 JD |
150 | |
151 | /* | |
152 | * Relay command queue. | |
153 | * | |
154 | * The relay_thread_listener and relay_thread_dispatcher communicate with this | |
155 | * queue. | |
156 | */ | |
58eb9381 | 157 | static struct relay_conn_queue relay_conn_queue; |
b8aa1682 | 158 | |
d3e2ba59 JD |
159 | /* Global relay stream hash table. */ |
160 | struct lttng_ht *relay_streams_ht; | |
161 | ||
92c6ca54 DG |
162 | /* Global relay viewer stream hash table. */ |
163 | struct lttng_ht *viewer_streams_ht; | |
164 | ||
7591bab1 MD |
165 | /* Global relay sessions hash table. */ |
166 | struct lttng_ht *sessions_ht; | |
0a6518b0 | 167 | |
55706a7d | 168 | /* Relayd health monitoring */ |
eea7556c | 169 | struct health_app *health_relayd; |
55706a7d | 170 | |
1f916b5d JG |
171 | struct sessiond_trace_chunk_registry *sessiond_trace_chunk_registry; |
172 | ||
cd60b05a JG |
173 | static struct option long_options[] = { |
174 | { "control-port", 1, 0, 'C', }, | |
175 | { "data-port", 1, 0, 'D', }, | |
8d5c808e | 176 | { "live-port", 1, 0, 'L', }, |
cd60b05a | 177 | { "daemonize", 0, 0, 'd', }, |
b5218ffb | 178 | { "background", 0, 0, 'b', }, |
cd60b05a JG |
179 | { "group", 1, 0, 'g', }, |
180 | { "help", 0, 0, 'h', }, | |
181 | { "output", 1, 0, 'o', }, | |
182 | { "verbose", 0, 0, 'v', }, | |
183 | { "config", 1, 0, 'f' }, | |
3a904098 | 184 | { "version", 0, 0, 'V' }, |
cd60b05a JG |
185 | { NULL, 0, 0, 0, }, |
186 | }; | |
187 | ||
3a904098 | 188 | static const char *config_ignore_options[] = { "help", "config", "version" }; |
cd60b05a | 189 | |
cd60b05a JG |
190 | /* |
191 | * Take an option from the getopt output and set it in the right variable to be | |
192 | * used later. | |
193 | * | |
194 | * Return 0 on success else a negative value. | |
195 | */ | |
7591bab1 | 196 | static int set_option(int opt, const char *arg, const char *optname) |
b8aa1682 | 197 | { |
cd60b05a JG |
198 | int ret; |
199 | ||
200 | switch (opt) { | |
201 | case 0: | |
202 | fprintf(stderr, "option %s", optname); | |
203 | if (arg) { | |
204 | fprintf(stderr, " with arg %s\n", arg); | |
205 | } | |
206 | break; | |
207 | case 'C': | |
e8fa9fb0 MD |
208 | if (lttng_is_setuid_setgid()) { |
209 | WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.", | |
210 | "-C, --control-port"); | |
211 | } else { | |
212 | ret = uri_parse(arg, &control_uri); | |
213 | if (ret < 0) { | |
214 | ERR("Invalid control URI specified"); | |
215 | goto end; | |
216 | } | |
217 | if (control_uri->port == 0) { | |
218 | control_uri->port = DEFAULT_NETWORK_CONTROL_PORT; | |
219 | } | |
cd60b05a JG |
220 | } |
221 | break; | |
222 | case 'D': | |
e8fa9fb0 MD |
223 | if (lttng_is_setuid_setgid()) { |
224 | WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.", | |
225 | "-D, -data-port"); | |
226 | } else { | |
227 | ret = uri_parse(arg, &data_uri); | |
228 | if (ret < 0) { | |
229 | ERR("Invalid data URI specified"); | |
230 | goto end; | |
231 | } | |
232 | if (data_uri->port == 0) { | |
233 | data_uri->port = DEFAULT_NETWORK_DATA_PORT; | |
234 | } | |
cd60b05a JG |
235 | } |
236 | break; | |
8d5c808e | 237 | case 'L': |
e8fa9fb0 MD |
238 | if (lttng_is_setuid_setgid()) { |
239 | WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.", | |
240 | "-L, -live-port"); | |
241 | } else { | |
242 | ret = uri_parse(arg, &live_uri); | |
243 | if (ret < 0) { | |
244 | ERR("Invalid live URI specified"); | |
245 | goto end; | |
246 | } | |
247 | if (live_uri->port == 0) { | |
248 | live_uri->port = DEFAULT_NETWORK_VIEWER_PORT; | |
249 | } | |
8d5c808e AM |
250 | } |
251 | break; | |
cd60b05a JG |
252 | case 'd': |
253 | opt_daemon = 1; | |
254 | break; | |
b5218ffb MD |
255 | case 'b': |
256 | opt_background = 1; | |
257 | break; | |
cd60b05a | 258 | case 'g': |
e8fa9fb0 MD |
259 | if (lttng_is_setuid_setgid()) { |
260 | WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.", | |
261 | "-g, --group"); | |
262 | } else { | |
263 | tracing_group_name = strdup(arg); | |
264 | if (tracing_group_name == NULL) { | |
265 | ret = -errno; | |
266 | PERROR("strdup"); | |
267 | goto end; | |
268 | } | |
269 | tracing_group_name_override = 1; | |
330a40bb | 270 | } |
cd60b05a JG |
271 | break; |
272 | case 'h': | |
4fc83d94 | 273 | ret = utils_show_help(8, "lttng-relayd", help_msg); |
655b5cc1 | 274 | if (ret) { |
4fc83d94 | 275 | ERR("Cannot show --help for `lttng-relayd`"); |
655b5cc1 PP |
276 | perror("exec"); |
277 | } | |
cd60b05a | 278 | exit(EXIT_FAILURE); |
3a904098 AW |
279 | case 'V': |
280 | fprintf(stdout, "%s\n", VERSION); | |
281 | exit(EXIT_SUCCESS); | |
cd60b05a | 282 | case 'o': |
e8fa9fb0 MD |
283 | if (lttng_is_setuid_setgid()) { |
284 | WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.", | |
285 | "-o, --output"); | |
286 | } else { | |
287 | ret = asprintf(&opt_output_path, "%s", arg); | |
288 | if (ret < 0) { | |
289 | ret = -errno; | |
290 | PERROR("asprintf opt_output_path"); | |
291 | goto end; | |
292 | } | |
cd60b05a JG |
293 | } |
294 | break; | |
295 | case 'v': | |
296 | /* Verbose level can increase using multiple -v */ | |
297 | if (arg) { | |
298 | lttng_opt_verbose = config_parse_value(arg); | |
299 | } else { | |
849e5b7b DG |
300 | /* Only 3 level of verbosity (-vvv). */ |
301 | if (lttng_opt_verbose < 3) { | |
302 | lttng_opt_verbose += 1; | |
303 | } | |
cd60b05a JG |
304 | } |
305 | break; | |
306 | default: | |
307 | /* Unknown option or other error. | |
308 | * Error is printed by getopt, just return */ | |
309 | ret = -1; | |
310 | goto end; | |
311 | } | |
312 | ||
313 | /* All good. */ | |
314 | ret = 0; | |
315 | ||
316 | end: | |
317 | return ret; | |
318 | } | |
319 | ||
320 | /* | |
321 | * config_entry_handler_cb used to handle options read from a config file. | |
f40ef1d5 | 322 | * See config_entry_handler_cb comment in common/config/session-config.h for the |
cd60b05a JG |
323 | * return value conventions. |
324 | */ | |
7591bab1 | 325 | static int config_entry_handler(const struct config_entry *entry, void *unused) |
cd60b05a JG |
326 | { |
327 | int ret = 0, i; | |
328 | ||
329 | if (!entry || !entry->name || !entry->value) { | |
330 | ret = -EINVAL; | |
331 | goto end; | |
332 | } | |
333 | ||
334 | /* Check if the option is to be ignored */ | |
335 | for (i = 0; i < sizeof(config_ignore_options) / sizeof(char *); i++) { | |
336 | if (!strcmp(entry->name, config_ignore_options[i])) { | |
337 | goto end; | |
338 | } | |
339 | } | |
340 | ||
341 | for (i = 0; i < (sizeof(long_options) / sizeof(struct option)) - 1; i++) { | |
342 | /* Ignore if entry name is not fully matched. */ | |
343 | if (strcmp(entry->name, long_options[i].name)) { | |
344 | continue; | |
345 | } | |
346 | ||
347 | /* | |
7591bab1 MD |
348 | * If the option takes no argument on the command line, |
349 | * we have to check if the value is "true". We support | |
350 | * non-zero numeric values, true, on and yes. | |
cd60b05a JG |
351 | */ |
352 | if (!long_options[i].has_arg) { | |
353 | ret = config_parse_value(entry->value); | |
354 | if (ret <= 0) { | |
355 | if (ret) { | |
356 | WARN("Invalid configuration value \"%s\" for option %s", | |
357 | entry->value, entry->name); | |
358 | } | |
359 | /* False, skip boolean config option. */ | |
360 | goto end; | |
361 | } | |
362 | } | |
363 | ||
364 | ret = set_option(long_options[i].val, entry->value, entry->name); | |
365 | goto end; | |
366 | } | |
367 | ||
368 | WARN("Unrecognized option \"%s\" in daemon configuration file.", | |
369 | entry->name); | |
370 | ||
371 | end: | |
372 | return ret; | |
373 | } | |
374 | ||
7591bab1 | 375 | static int set_options(int argc, char **argv) |
cd60b05a | 376 | { |
178a0557 | 377 | int c, ret = 0, option_index = 0, retval = 0; |
cd60b05a JG |
378 | int orig_optopt = optopt, orig_optind = optind; |
379 | char *default_address, *optstring; | |
380 | const char *config_path = NULL; | |
381 | ||
382 | optstring = utils_generate_optstring(long_options, | |
383 | sizeof(long_options) / sizeof(struct option)); | |
384 | if (!optstring) { | |
178a0557 | 385 | retval = -ENOMEM; |
cd60b05a JG |
386 | goto exit; |
387 | } | |
388 | ||
389 | /* Check for the --config option */ | |
390 | ||
391 | while ((c = getopt_long(argc, argv, optstring, long_options, | |
392 | &option_index)) != -1) { | |
393 | if (c == '?') { | |
178a0557 | 394 | retval = -EINVAL; |
cd60b05a JG |
395 | goto exit; |
396 | } else if (c != 'f') { | |
397 | continue; | |
398 | } | |
399 | ||
e8fa9fb0 MD |
400 | if (lttng_is_setuid_setgid()) { |
401 | WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.", | |
402 | "-f, --config"); | |
403 | } else { | |
404 | config_path = utils_expand_path(optarg); | |
405 | if (!config_path) { | |
406 | ERR("Failed to resolve path: %s", optarg); | |
407 | } | |
cd60b05a JG |
408 | } |
409 | } | |
410 | ||
411 | ret = config_get_section_entries(config_path, config_section_name, | |
412 | config_entry_handler, NULL); | |
413 | if (ret) { | |
414 | if (ret > 0) { | |
415 | ERR("Invalid configuration option at line %i", ret); | |
cd60b05a | 416 | } |
178a0557 | 417 | retval = -1; |
cd60b05a JG |
418 | goto exit; |
419 | } | |
b8aa1682 | 420 | |
cd60b05a JG |
421 | /* Reset getopt's global state */ |
422 | optopt = orig_optopt; | |
423 | optind = orig_optind; | |
b8aa1682 | 424 | while (1) { |
cd60b05a | 425 | c = getopt_long(argc, argv, optstring, long_options, &option_index); |
b8aa1682 JD |
426 | if (c == -1) { |
427 | break; | |
428 | } | |
429 | ||
cd60b05a JG |
430 | ret = set_option(c, optarg, long_options[option_index].name); |
431 | if (ret < 0) { | |
178a0557 | 432 | retval = -1; |
b8aa1682 JD |
433 | goto exit; |
434 | } | |
435 | } | |
436 | ||
437 | /* assign default values */ | |
438 | if (control_uri == NULL) { | |
fa91dc52 MD |
439 | ret = asprintf(&default_address, |
440 | "tcp://" DEFAULT_NETWORK_CONTROL_BIND_ADDRESS ":%d", | |
441 | DEFAULT_NETWORK_CONTROL_PORT); | |
b8aa1682 JD |
442 | if (ret < 0) { |
443 | PERROR("asprintf default data address"); | |
178a0557 | 444 | retval = -1; |
b8aa1682 JD |
445 | goto exit; |
446 | } | |
447 | ||
448 | ret = uri_parse(default_address, &control_uri); | |
449 | free(default_address); | |
450 | if (ret < 0) { | |
451 | ERR("Invalid control URI specified"); | |
178a0557 | 452 | retval = -1; |
b8aa1682 JD |
453 | goto exit; |
454 | } | |
455 | } | |
456 | if (data_uri == NULL) { | |
fa91dc52 MD |
457 | ret = asprintf(&default_address, |
458 | "tcp://" DEFAULT_NETWORK_DATA_BIND_ADDRESS ":%d", | |
459 | DEFAULT_NETWORK_DATA_PORT); | |
b8aa1682 JD |
460 | if (ret < 0) { |
461 | PERROR("asprintf default data address"); | |
178a0557 | 462 | retval = -1; |
b8aa1682 JD |
463 | goto exit; |
464 | } | |
465 | ||
466 | ret = uri_parse(default_address, &data_uri); | |
467 | free(default_address); | |
468 | if (ret < 0) { | |
469 | ERR("Invalid data URI specified"); | |
178a0557 | 470 | retval = -1; |
b8aa1682 JD |
471 | goto exit; |
472 | } | |
473 | } | |
d3e2ba59 | 474 | if (live_uri == NULL) { |
fa91dc52 MD |
475 | ret = asprintf(&default_address, |
476 | "tcp://" DEFAULT_NETWORK_VIEWER_BIND_ADDRESS ":%d", | |
477 | DEFAULT_NETWORK_VIEWER_PORT); | |
d3e2ba59 JD |
478 | if (ret < 0) { |
479 | PERROR("asprintf default viewer control address"); | |
178a0557 | 480 | retval = -1; |
d3e2ba59 JD |
481 | goto exit; |
482 | } | |
483 | ||
484 | ret = uri_parse(default_address, &live_uri); | |
485 | free(default_address); | |
486 | if (ret < 0) { | |
487 | ERR("Invalid viewer control URI specified"); | |
178a0557 | 488 | retval = -1; |
d3e2ba59 JD |
489 | goto exit; |
490 | } | |
491 | } | |
b8aa1682 JD |
492 | |
493 | exit: | |
cd60b05a | 494 | free(optstring); |
178a0557 | 495 | return retval; |
b8aa1682 JD |
496 | } |
497 | ||
7591bab1 MD |
498 | static void print_global_objects(void) |
499 | { | |
500 | rcu_register_thread(); | |
501 | ||
502 | print_viewer_streams(); | |
503 | print_relay_streams(); | |
504 | print_sessions(); | |
505 | ||
506 | rcu_unregister_thread(); | |
507 | } | |
508 | ||
b8aa1682 JD |
509 | /* |
510 | * Cleanup the daemon | |
511 | */ | |
7591bab1 | 512 | static void relayd_cleanup(void) |
b8aa1682 | 513 | { |
7591bab1 MD |
514 | print_global_objects(); |
515 | ||
b8aa1682 JD |
516 | DBG("Cleaning up"); |
517 | ||
178a0557 MD |
518 | if (viewer_streams_ht) |
519 | lttng_ht_destroy(viewer_streams_ht); | |
520 | if (relay_streams_ht) | |
521 | lttng_ht_destroy(relay_streams_ht); | |
7591bab1 MD |
522 | if (sessions_ht) |
523 | lttng_ht_destroy(sessions_ht); | |
178a0557 | 524 | |
095a4ae5 MD |
525 | /* free the dynamically allocated opt_output_path */ |
526 | free(opt_output_path); | |
527 | ||
a02de639 CB |
528 | /* Close thread quit pipes */ |
529 | utils_close_pipe(thread_quit_pipe); | |
530 | ||
710c1f73 DG |
531 | uri_free(control_uri); |
532 | uri_free(data_uri); | |
8d5c808e | 533 | /* Live URI is freed in the live thread. */ |
cd60b05a JG |
534 | |
535 | if (tracing_group_name_override) { | |
536 | free((void *) tracing_group_name); | |
537 | } | |
b8aa1682 JD |
538 | } |
539 | ||
540 | /* | |
541 | * Write to writable pipe used to notify a thread. | |
542 | */ | |
7591bab1 | 543 | static int notify_thread_pipe(int wpipe) |
b8aa1682 | 544 | { |
6cd525e8 | 545 | ssize_t ret; |
b8aa1682 | 546 | |
6cd525e8 MD |
547 | ret = lttng_write(wpipe, "!", 1); |
548 | if (ret < 1) { | |
b8aa1682 | 549 | PERROR("write poll pipe"); |
b4aacfdc | 550 | goto end; |
b8aa1682 | 551 | } |
b4aacfdc MD |
552 | ret = 0; |
553 | end: | |
b8aa1682 JD |
554 | return ret; |
555 | } | |
556 | ||
7591bab1 | 557 | static int notify_health_quit_pipe(int *pipe) |
65931c8b | 558 | { |
6cd525e8 | 559 | ssize_t ret; |
65931c8b | 560 | |
6cd525e8 MD |
561 | ret = lttng_write(pipe[1], "4", 1); |
562 | if (ret < 1) { | |
65931c8b | 563 | PERROR("write relay health quit"); |
b4aacfdc | 564 | goto end; |
65931c8b | 565 | } |
b4aacfdc MD |
566 | ret = 0; |
567 | end: | |
568 | return ret; | |
65931c8b MD |
569 | } |
570 | ||
b8aa1682 | 571 | /* |
b4aacfdc | 572 | * Stop all relayd and relayd-live threads. |
b8aa1682 | 573 | */ |
b4aacfdc | 574 | int lttng_relay_stop_threads(void) |
b8aa1682 | 575 | { |
b4aacfdc | 576 | int retval = 0; |
b8aa1682 JD |
577 | |
578 | /* Stopping all threads */ | |
579 | DBG("Terminating all threads"); | |
b4aacfdc | 580 | if (notify_thread_pipe(thread_quit_pipe[1])) { |
b8aa1682 | 581 | ERR("write error on thread quit pipe"); |
b4aacfdc | 582 | retval = -1; |
b8aa1682 JD |
583 | } |
584 | ||
b4aacfdc MD |
585 | if (notify_health_quit_pipe(health_quit_pipe)) { |
586 | ERR("write error on health quit pipe"); | |
587 | } | |
65931c8b | 588 | |
b8aa1682 | 589 | /* Dispatch thread */ |
26c9d55e | 590 | CMM_STORE_SHARED(dispatch_thread_exit, 1); |
58eb9381 | 591 | futex_nto1_wake(&relay_conn_queue.futex); |
178a0557 | 592 | |
b4aacfdc | 593 | if (relayd_live_stop()) { |
178a0557 | 594 | ERR("Error stopping live threads"); |
b4aacfdc | 595 | retval = -1; |
178a0557 | 596 | } |
b4aacfdc | 597 | return retval; |
b8aa1682 JD |
598 | } |
599 | ||
600 | /* | |
601 | * Signal handler for the daemon | |
602 | * | |
603 | * Simply stop all worker threads, leaving main() return gracefully after | |
604 | * joining all threads and calling cleanup(). | |
605 | */ | |
7591bab1 | 606 | static void sighandler(int sig) |
b8aa1682 JD |
607 | { |
608 | switch (sig) { | |
b8aa1682 JD |
609 | case SIGINT: |
610 | DBG("SIGINT caught"); | |
b4aacfdc MD |
611 | if (lttng_relay_stop_threads()) { |
612 | ERR("Error stopping threads"); | |
613 | } | |
b8aa1682 JD |
614 | break; |
615 | case SIGTERM: | |
616 | DBG("SIGTERM caught"); | |
b4aacfdc MD |
617 | if (lttng_relay_stop_threads()) { |
618 | ERR("Error stopping threads"); | |
619 | } | |
b8aa1682 | 620 | break; |
3fd27398 MD |
621 | case SIGUSR1: |
622 | CMM_STORE_SHARED(recv_child_signal, 1); | |
623 | break; | |
b8aa1682 JD |
624 | default: |
625 | break; | |
626 | } | |
627 | } | |
628 | ||
629 | /* | |
630 | * Setup signal handler for : | |
631 | * SIGINT, SIGTERM, SIGPIPE | |
632 | */ | |
7591bab1 | 633 | static int set_signal_handler(void) |
b8aa1682 JD |
634 | { |
635 | int ret = 0; | |
636 | struct sigaction sa; | |
637 | sigset_t sigset; | |
638 | ||
639 | if ((ret = sigemptyset(&sigset)) < 0) { | |
640 | PERROR("sigemptyset"); | |
641 | return ret; | |
642 | } | |
643 | ||
b8aa1682 JD |
644 | sa.sa_mask = sigset; |
645 | sa.sa_flags = 0; | |
0072e5e2 MD |
646 | |
647 | sa.sa_handler = sighandler; | |
b8aa1682 JD |
648 | if ((ret = sigaction(SIGTERM, &sa, NULL)) < 0) { |
649 | PERROR("sigaction"); | |
650 | return ret; | |
651 | } | |
652 | ||
653 | if ((ret = sigaction(SIGINT, &sa, NULL)) < 0) { | |
654 | PERROR("sigaction"); | |
655 | return ret; | |
656 | } | |
657 | ||
0072e5e2 | 658 | if ((ret = sigaction(SIGUSR1, &sa, NULL)) < 0) { |
b8aa1682 JD |
659 | PERROR("sigaction"); |
660 | return ret; | |
661 | } | |
662 | ||
0072e5e2 MD |
663 | sa.sa_handler = SIG_IGN; |
664 | if ((ret = sigaction(SIGPIPE, &sa, NULL)) < 0) { | |
3fd27398 MD |
665 | PERROR("sigaction"); |
666 | return ret; | |
667 | } | |
668 | ||
669 | DBG("Signal handler set for SIGTERM, SIGUSR1, SIGPIPE and SIGINT"); | |
b8aa1682 JD |
670 | |
671 | return ret; | |
672 | } | |
673 | ||
3fd27398 MD |
674 | void lttng_relay_notify_ready(void) |
675 | { | |
676 | /* Notify the parent of the fork() process that we are ready. */ | |
677 | if (opt_daemon || opt_background) { | |
678 | if (uatomic_sub_return(<tng_relay_ready, 1) == 0) { | |
679 | kill(child_ppid, SIGUSR1); | |
680 | } | |
681 | } | |
682 | } | |
683 | ||
b8aa1682 JD |
684 | /* |
685 | * Init thread quit pipe. | |
686 | * | |
687 | * Return -1 on error or 0 if all pipes are created. | |
688 | */ | |
7591bab1 | 689 | static int init_thread_quit_pipe(void) |
b8aa1682 | 690 | { |
a02de639 | 691 | int ret; |
b8aa1682 | 692 | |
a02de639 | 693 | ret = utils_create_pipe_cloexec(thread_quit_pipe); |
b8aa1682 | 694 | |
b8aa1682 JD |
695 | return ret; |
696 | } | |
697 | ||
698 | /* | |
699 | * Create a poll set with O_CLOEXEC and add the thread quit pipe to the set. | |
700 | */ | |
7591bab1 | 701 | static int create_thread_poll_set(struct lttng_poll_event *events, int size) |
b8aa1682 JD |
702 | { |
703 | int ret; | |
704 | ||
705 | if (events == NULL || size == 0) { | |
706 | ret = -1; | |
707 | goto error; | |
708 | } | |
709 | ||
710 | ret = lttng_poll_create(events, size, LTTNG_CLOEXEC); | |
711 | if (ret < 0) { | |
712 | goto error; | |
713 | } | |
714 | ||
715 | /* Add quit pipe */ | |
c7759e6a | 716 | ret = lttng_poll_add(events, thread_quit_pipe[0], LPOLLIN | LPOLLERR); |
b8aa1682 JD |
717 | if (ret < 0) { |
718 | goto error; | |
719 | } | |
720 | ||
721 | return 0; | |
722 | ||
723 | error: | |
724 | return ret; | |
725 | } | |
726 | ||
727 | /* | |
728 | * Check if the thread quit pipe was triggered. | |
729 | * | |
730 | * Return 1 if it was triggered else 0; | |
731 | */ | |
7591bab1 | 732 | static int check_thread_quit_pipe(int fd, uint32_t events) |
b8aa1682 JD |
733 | { |
734 | if (fd == thread_quit_pipe[0] && (events & LPOLLIN)) { | |
735 | return 1; | |
736 | } | |
737 | ||
738 | return 0; | |
739 | } | |
740 | ||
741 | /* | |
742 | * Create and init socket from uri. | |
743 | */ | |
7591bab1 | 744 | static struct lttcomm_sock *relay_socket_create(struct lttng_uri *uri) |
b8aa1682 JD |
745 | { |
746 | int ret; | |
747 | struct lttcomm_sock *sock = NULL; | |
748 | ||
749 | sock = lttcomm_alloc_sock_from_uri(uri); | |
750 | if (sock == NULL) { | |
751 | ERR("Allocating socket"); | |
752 | goto error; | |
753 | } | |
754 | ||
755 | ret = lttcomm_create_sock(sock); | |
756 | if (ret < 0) { | |
757 | goto error; | |
758 | } | |
759 | DBG("Listening on sock %d", sock->fd); | |
760 | ||
761 | ret = sock->ops->bind(sock); | |
762 | if (ret < 0) { | |
2288467f | 763 | PERROR("Failed to bind socket"); |
b8aa1682 JD |
764 | goto error; |
765 | } | |
766 | ||
767 | ret = sock->ops->listen(sock, -1); | |
768 | if (ret < 0) { | |
769 | goto error; | |
770 | ||
771 | } | |
772 | ||
773 | return sock; | |
774 | ||
775 | error: | |
776 | if (sock) { | |
777 | lttcomm_destroy_sock(sock); | |
778 | } | |
779 | return NULL; | |
780 | } | |
781 | ||
782 | /* | |
783 | * This thread manages the listening for new connections on the network | |
784 | */ | |
7591bab1 | 785 | static void *relay_thread_listener(void *data) |
b8aa1682 | 786 | { |
095a4ae5 | 787 | int i, ret, pollfd, err = -1; |
b8aa1682 JD |
788 | uint32_t revents, nb_fd; |
789 | struct lttng_poll_event events; | |
790 | struct lttcomm_sock *control_sock, *data_sock; | |
791 | ||
b8aa1682 JD |
792 | DBG("[thread] Relay listener started"); |
793 | ||
55706a7d MD |
794 | health_register(health_relayd, HEALTH_RELAYD_TYPE_LISTENER); |
795 | ||
f385ae0a MD |
796 | health_code_update(); |
797 | ||
7591bab1 | 798 | control_sock = relay_socket_create(control_uri); |
b8aa1682 | 799 | if (!control_sock) { |
095a4ae5 | 800 | goto error_sock_control; |
b8aa1682 JD |
801 | } |
802 | ||
7591bab1 | 803 | data_sock = relay_socket_create(data_uri); |
b8aa1682 | 804 | if (!data_sock) { |
095a4ae5 | 805 | goto error_sock_relay; |
b8aa1682 JD |
806 | } |
807 | ||
808 | /* | |
7591bab1 MD |
809 | * Pass 3 as size here for the thread quit pipe, control and |
810 | * data socket. | |
b8aa1682 JD |
811 | */ |
812 | ret = create_thread_poll_set(&events, 3); | |
813 | if (ret < 0) { | |
814 | goto error_create_poll; | |
815 | } | |
816 | ||
817 | /* Add the control socket */ | |
818 | ret = lttng_poll_add(&events, control_sock->fd, LPOLLIN | LPOLLRDHUP); | |
819 | if (ret < 0) { | |
820 | goto error_poll_add; | |
821 | } | |
822 | ||
823 | /* Add the data socket */ | |
824 | ret = lttng_poll_add(&events, data_sock->fd, LPOLLIN | LPOLLRDHUP); | |
825 | if (ret < 0) { | |
826 | goto error_poll_add; | |
827 | } | |
828 | ||
3fd27398 MD |
829 | lttng_relay_notify_ready(); |
830 | ||
9b5e0863 MD |
831 | if (testpoint(relayd_thread_listener)) { |
832 | goto error_testpoint; | |
833 | } | |
834 | ||
b8aa1682 | 835 | while (1) { |
f385ae0a MD |
836 | health_code_update(); |
837 | ||
b8aa1682 JD |
838 | DBG("Listener accepting connections"); |
839 | ||
b8aa1682 | 840 | restart: |
f385ae0a | 841 | health_poll_entry(); |
b8aa1682 | 842 | ret = lttng_poll_wait(&events, -1); |
f385ae0a | 843 | health_poll_exit(); |
b8aa1682 JD |
844 | if (ret < 0) { |
845 | /* | |
846 | * Restart interrupted system call. | |
847 | */ | |
848 | if (errno == EINTR) { | |
849 | goto restart; | |
850 | } | |
851 | goto error; | |
852 | } | |
853 | ||
0d9c5d77 DG |
854 | nb_fd = ret; |
855 | ||
b8aa1682 JD |
856 | DBG("Relay new connection received"); |
857 | for (i = 0; i < nb_fd; i++) { | |
f385ae0a MD |
858 | health_code_update(); |
859 | ||
b8aa1682 JD |
860 | /* Fetch once the poll data */ |
861 | revents = LTTNG_POLL_GETEV(&events, i); | |
862 | pollfd = LTTNG_POLL_GETFD(&events, i); | |
863 | ||
864 | /* Thread quit pipe has been closed. Killing thread. */ | |
865 | ret = check_thread_quit_pipe(pollfd, revents); | |
866 | if (ret) { | |
095a4ae5 MD |
867 | err = 0; |
868 | goto exit; | |
b8aa1682 JD |
869 | } |
870 | ||
03e43155 | 871 | if (revents & LPOLLIN) { |
4b7f17b2 | 872 | /* |
7591bab1 MD |
873 | * A new connection is requested, therefore a |
874 | * sessiond/consumerd connection is allocated in | |
875 | * this thread, enqueued to a global queue and | |
876 | * dequeued (and freed) in the worker thread. | |
4b7f17b2 | 877 | */ |
58eb9381 DG |
878 | int val = 1; |
879 | struct relay_connection *new_conn; | |
4b7f17b2 | 880 | struct lttcomm_sock *newsock; |
7591bab1 | 881 | enum connection_type type; |
b8aa1682 JD |
882 | |
883 | if (pollfd == data_sock->fd) { | |
7591bab1 | 884 | type = RELAY_DATA; |
b8aa1682 | 885 | newsock = data_sock->ops->accept(data_sock); |
58eb9381 DG |
886 | DBG("Relay data connection accepted, socket %d", |
887 | newsock->fd); | |
4b7f17b2 MD |
888 | } else { |
889 | assert(pollfd == control_sock->fd); | |
7591bab1 | 890 | type = RELAY_CONTROL; |
b8aa1682 | 891 | newsock = control_sock->ops->accept(control_sock); |
58eb9381 DG |
892 | DBG("Relay control connection accepted, socket %d", |
893 | newsock->fd); | |
b8aa1682 | 894 | } |
58eb9381 DG |
895 | if (!newsock) { |
896 | PERROR("accepting sock"); | |
58eb9381 DG |
897 | goto error; |
898 | } | |
899 | ||
900 | ret = setsockopt(newsock->fd, SOL_SOCKET, SO_REUSEADDR, &val, | |
901 | sizeof(val)); | |
b8aa1682 JD |
902 | if (ret < 0) { |
903 | PERROR("setsockopt inet"); | |
4b7f17b2 | 904 | lttcomm_destroy_sock(newsock); |
b8aa1682 JD |
905 | goto error; |
906 | } | |
f056029c JR |
907 | |
908 | ret = socket_apply_keep_alive_config(newsock->fd); | |
909 | if (ret < 0) { | |
910 | ERR("Failed to apply TCP keep-alive configuration on socket (%i)", | |
911 | newsock->fd); | |
912 | lttcomm_destroy_sock(newsock); | |
913 | goto error; | |
914 | } | |
915 | ||
7591bab1 MD |
916 | new_conn = connection_create(newsock, type); |
917 | if (!new_conn) { | |
918 | lttcomm_destroy_sock(newsock); | |
919 | goto error; | |
920 | } | |
58eb9381 DG |
921 | |
922 | /* Enqueue request for the dispatcher thread. */ | |
8bdee6e2 SM |
923 | cds_wfcq_enqueue(&relay_conn_queue.head, &relay_conn_queue.tail, |
924 | &new_conn->qnode); | |
b8aa1682 JD |
925 | |
926 | /* | |
7591bab1 MD |
927 | * Wake the dispatch queue futex. |
928 | * Implicit memory barrier with the | |
929 | * exchange in cds_wfcq_enqueue. | |
b8aa1682 | 930 | */ |
58eb9381 | 931 | futex_nto1_wake(&relay_conn_queue.futex); |
03e43155 MD |
932 | } else if (revents & (LPOLLERR | LPOLLHUP | LPOLLRDHUP)) { |
933 | ERR("socket poll error"); | |
934 | goto error; | |
935 | } else { | |
936 | ERR("Unexpected poll events %u for sock %d", revents, pollfd); | |
937 | goto error; | |
b8aa1682 JD |
938 | } |
939 | } | |
940 | } | |
941 | ||
095a4ae5 | 942 | exit: |
b8aa1682 JD |
943 | error: |
944 | error_poll_add: | |
9b5e0863 | 945 | error_testpoint: |
b8aa1682 JD |
946 | lttng_poll_clean(&events); |
947 | error_create_poll: | |
095a4ae5 MD |
948 | if (data_sock->fd >= 0) { |
949 | ret = data_sock->ops->close(data_sock); | |
b8aa1682 JD |
950 | if (ret) { |
951 | PERROR("close"); | |
952 | } | |
b8aa1682 | 953 | } |
095a4ae5 MD |
954 | lttcomm_destroy_sock(data_sock); |
955 | error_sock_relay: | |
956 | if (control_sock->fd >= 0) { | |
957 | ret = control_sock->ops->close(control_sock); | |
b8aa1682 JD |
958 | if (ret) { |
959 | PERROR("close"); | |
960 | } | |
b8aa1682 | 961 | } |
095a4ae5 MD |
962 | lttcomm_destroy_sock(control_sock); |
963 | error_sock_control: | |
964 | if (err) { | |
f385ae0a MD |
965 | health_error(); |
966 | ERR("Health error occurred in %s", __func__); | |
095a4ae5 | 967 | } |
55706a7d | 968 | health_unregister(health_relayd); |
b8aa1682 | 969 | DBG("Relay listener thread cleanup complete"); |
b4aacfdc | 970 | lttng_relay_stop_threads(); |
b8aa1682 JD |
971 | return NULL; |
972 | } | |
973 | ||
974 | /* | |
975 | * This thread manages the dispatching of the requests to worker threads | |
976 | */ | |
7591bab1 | 977 | static void *relay_thread_dispatcher(void *data) |
b8aa1682 | 978 | { |
6cd525e8 MD |
979 | int err = -1; |
980 | ssize_t ret; | |
8bdee6e2 | 981 | struct cds_wfcq_node *node; |
58eb9381 | 982 | struct relay_connection *new_conn = NULL; |
b8aa1682 JD |
983 | |
984 | DBG("[thread] Relay dispatcher started"); | |
985 | ||
55706a7d MD |
986 | health_register(health_relayd, HEALTH_RELAYD_TYPE_DISPATCHER); |
987 | ||
9b5e0863 MD |
988 | if (testpoint(relayd_thread_dispatcher)) { |
989 | goto error_testpoint; | |
990 | } | |
991 | ||
f385ae0a MD |
992 | health_code_update(); |
993 | ||
0ed3b1a8 | 994 | for (;;) { |
f385ae0a MD |
995 | health_code_update(); |
996 | ||
b8aa1682 | 997 | /* Atomically prepare the queue futex */ |
58eb9381 | 998 | futex_nto1_prepare(&relay_conn_queue.futex); |
b8aa1682 | 999 | |
0ed3b1a8 MD |
1000 | if (CMM_LOAD_SHARED(dispatch_thread_exit)) { |
1001 | break; | |
1002 | } | |
1003 | ||
b8aa1682 | 1004 | do { |
f385ae0a MD |
1005 | health_code_update(); |
1006 | ||
b8aa1682 | 1007 | /* Dequeue commands */ |
8bdee6e2 SM |
1008 | node = cds_wfcq_dequeue_blocking(&relay_conn_queue.head, |
1009 | &relay_conn_queue.tail); | |
b8aa1682 JD |
1010 | if (node == NULL) { |
1011 | DBG("Woken up but nothing in the relay command queue"); | |
1012 | /* Continue thread execution */ | |
1013 | break; | |
1014 | } | |
58eb9381 | 1015 | new_conn = caa_container_of(node, struct relay_connection, qnode); |
b8aa1682 | 1016 | |
58eb9381 | 1017 | DBG("Dispatching request waiting on sock %d", new_conn->sock->fd); |
b8aa1682 JD |
1018 | |
1019 | /* | |
7591bab1 MD |
1020 | * Inform worker thread of the new request. This |
1021 | * call is blocking so we can be assured that | |
1022 | * the data will be read at some point in time | |
1023 | * or wait to the end of the world :) | |
b8aa1682 | 1024 | */ |
58eb9381 DG |
1025 | ret = lttng_write(relay_conn_pipe[1], &new_conn, sizeof(new_conn)); |
1026 | if (ret < 0) { | |
1027 | PERROR("write connection pipe"); | |
7591bab1 | 1028 | connection_put(new_conn); |
b8aa1682 JD |
1029 | goto error; |
1030 | } | |
1031 | } while (node != NULL); | |
1032 | ||
1033 | /* Futex wait on queue. Blocking call on futex() */ | |
f385ae0a | 1034 | health_poll_entry(); |
58eb9381 | 1035 | futex_nto1_wait(&relay_conn_queue.futex); |
f385ae0a | 1036 | health_poll_exit(); |
b8aa1682 JD |
1037 | } |
1038 | ||
f385ae0a MD |
1039 | /* Normal exit, no error */ |
1040 | err = 0; | |
1041 | ||
b8aa1682 | 1042 | error: |
9b5e0863 | 1043 | error_testpoint: |
f385ae0a MD |
1044 | if (err) { |
1045 | health_error(); | |
1046 | ERR("Health error occurred in %s", __func__); | |
1047 | } | |
55706a7d | 1048 | health_unregister(health_relayd); |
b8aa1682 | 1049 | DBG("Dispatch thread dying"); |
b4aacfdc | 1050 | lttng_relay_stop_threads(); |
b8aa1682 JD |
1051 | return NULL; |
1052 | } | |
1053 | ||
01921b6d JG |
1054 | static bool session_streams_have_index(const struct relay_session *session) |
1055 | { | |
1056 | return session->minor >= 4 && !session->snapshot; | |
1057 | } | |
1058 | ||
c5b6f4f0 DG |
1059 | /* |
1060 | * Handle the RELAYD_CREATE_SESSION command. | |
1061 | * | |
1062 | * On success, send back the session id or else return a negative value. | |
1063 | */ | |
5312a3ed JG |
1064 | static int relay_create_session(const struct lttcomm_relayd_hdr *recv_hdr, |
1065 | struct relay_connection *conn, | |
1066 | const struct lttng_buffer_view *payload) | |
c5b6f4f0 | 1067 | { |
5312a3ed JG |
1068 | int ret = 0; |
1069 | ssize_t send_ret; | |
c7bb28d3 | 1070 | struct relay_session *session = NULL; |
41b23598 | 1071 | struct lttcomm_relayd_create_session_reply_2_11 reply = {}; |
b2c25d00 JG |
1072 | char session_name[LTTNG_NAME_MAX] = {}; |
1073 | char hostname[LTTNG_HOST_NAME_MAX] = {}; | |
7591bab1 MD |
1074 | uint32_t live_timer = 0; |
1075 | bool snapshot = false; | |
55ce50b6 | 1076 | bool session_name_contains_creation_timestamp = false; |
1f916b5d | 1077 | /* Left nil for peers < 2.11. */ |
6ef36453 | 1078 | char base_path[LTTNG_PATH_MAX] = {}; |
1f916b5d | 1079 | lttng_uuid sessiond_uuid = {}; |
b2c25d00 JG |
1080 | LTTNG_OPTIONAL(uint64_t) id_sessiond = {}; |
1081 | LTTNG_OPTIONAL(uint64_t) current_chunk_id = {}; | |
0fb564b1 | 1082 | LTTNG_OPTIONAL(time_t) creation_time = {}; |
41b23598 MD |
1083 | struct lttng_dynamic_buffer reply_payload; |
1084 | ||
1085 | lttng_dynamic_buffer_init(&reply_payload); | |
c5b6f4f0 | 1086 | |
f86f6389 JR |
1087 | if (conn->minor < 4) { |
1088 | /* From 2.1 to 2.3 */ | |
1089 | ret = 0; | |
1090 | } else if (conn->minor >= 4 && conn->minor < 11) { | |
1091 | /* From 2.4 to 2.10 */ | |
5312a3ed | 1092 | ret = cmd_create_session_2_4(payload, session_name, |
7591bab1 | 1093 | hostname, &live_timer, &snapshot); |
f86f6389 | 1094 | } else { |
cb034ca2 | 1095 | bool has_current_chunk; |
0fb564b1 JG |
1096 | uint64_t current_chunk_id_value; |
1097 | time_t creation_time_value; | |
1098 | uint64_t id_sessiond_value; | |
cb034ca2 | 1099 | |
f86f6389 | 1100 | /* From 2.11 to ... */ |
0fb564b1 | 1101 | ret = cmd_create_session_2_11(payload, session_name, hostname, |
6ef36453 | 1102 | base_path, &live_timer, &snapshot, &id_sessiond_value, |
0fb564b1 | 1103 | sessiond_uuid, &has_current_chunk, |
55ce50b6 MD |
1104 | ¤t_chunk_id_value, &creation_time_value, |
1105 | &session_name_contains_creation_timestamp); | |
1f916b5d JG |
1106 | if (lttng_uuid_is_nil(sessiond_uuid)) { |
1107 | /* The nil UUID is reserved for pre-2.11 clients. */ | |
1108 | ERR("Illegal nil UUID announced by peer in create session command"); | |
1109 | ret = -1; | |
1110 | goto send_reply; | |
1111 | } | |
0fb564b1 JG |
1112 | LTTNG_OPTIONAL_SET(&id_sessiond, id_sessiond_value); |
1113 | LTTNG_OPTIONAL_SET(&creation_time, creation_time_value); | |
1114 | if (has_current_chunk) { | |
1115 | LTTNG_OPTIONAL_SET(¤t_chunk_id, | |
1116 | current_chunk_id_value); | |
1117 | } | |
7591bab1 | 1118 | } |
f86f6389 | 1119 | |
7591bab1 MD |
1120 | if (ret < 0) { |
1121 | goto send_reply; | |
d3e2ba59 JD |
1122 | } |
1123 | ||
6ef36453 | 1124 | session = session_create(session_name, hostname, base_path, live_timer, |
b2c25d00 JG |
1125 | snapshot, sessiond_uuid, |
1126 | id_sessiond.is_set ? &id_sessiond.value : NULL, | |
1127 | current_chunk_id.is_set ? ¤t_chunk_id.value : NULL, | |
0fb564b1 | 1128 | creation_time.is_set ? &creation_time.value : NULL, |
55ce50b6 MD |
1129 | conn->major, conn->minor, |
1130 | session_name_contains_creation_timestamp); | |
7591bab1 MD |
1131 | if (!session) { |
1132 | ret = -1; | |
1133 | goto send_reply; | |
1134 | } | |
1135 | assert(!conn->session); | |
1136 | conn->session = session; | |
c5b6f4f0 DG |
1137 | DBG("Created session %" PRIu64, session->id); |
1138 | ||
41b23598 | 1139 | reply.generic.session_id = htobe64(session->id); |
7591bab1 MD |
1140 | |
1141 | send_reply: | |
c5b6f4f0 | 1142 | if (ret < 0) { |
41b23598 | 1143 | reply.generic.ret_code = htobe32(LTTNG_ERR_FATAL); |
c5b6f4f0 | 1144 | } else { |
41b23598 | 1145 | reply.generic.ret_code = htobe32(LTTNG_OK); |
c5b6f4f0 DG |
1146 | } |
1147 | ||
41b23598 MD |
1148 | if (conn->minor < 11) { |
1149 | /* From 2.1 to 2.10 */ | |
1150 | ret = lttng_dynamic_buffer_append(&reply_payload, | |
1151 | &reply.generic, sizeof(reply.generic)); | |
1152 | if (ret) { | |
1153 | ERR("Failed to append \"create session\" command reply header to payload buffer"); | |
1154 | ret = -1; | |
1155 | goto end; | |
1156 | } | |
1157 | } else { | |
1158 | const uint32_t output_path_length = | |
1159 | strlen(session->output_path) + 1; | |
1160 | ||
1161 | reply.output_path_length = htobe32(output_path_length); | |
1162 | ret = lttng_dynamic_buffer_append(&reply_payload, &reply, | |
1163 | sizeof(reply)); | |
1164 | if (ret) { | |
1165 | ERR("Failed to append \"create session\" command reply header to payload buffer"); | |
1166 | goto end; | |
1167 | } | |
1168 | ||
1169 | ret = lttng_dynamic_buffer_append(&reply_payload, | |
1170 | session->output_path, output_path_length); | |
1171 | if (ret) { | |
1172 | ERR("Failed to append \"create session\" command reply path to payload buffer"); | |
1173 | goto end; | |
1174 | } | |
1175 | } | |
1176 | ||
1177 | send_ret = conn->sock->ops->sendmsg(conn->sock, reply_payload.data, | |
1178 | reply_payload.size, 0); | |
1179 | if (send_ret < (ssize_t) reply_payload.size) { | |
1180 | ERR("Failed to send \"create session\" command reply of %zu bytes (ret = %zd)", | |
1181 | reply_payload.size, send_ret); | |
5312a3ed | 1182 | ret = -1; |
c5b6f4f0 | 1183 | } |
41b23598 | 1184 | end: |
c7bb28d3 JG |
1185 | if (ret < 0 && session) { |
1186 | session_put(session); | |
1187 | } | |
41b23598 | 1188 | lttng_dynamic_buffer_reset(&reply_payload); |
c5b6f4f0 DG |
1189 | return ret; |
1190 | } | |
1191 | ||
a4baae1b JD |
1192 | /* |
1193 | * When we have received all the streams and the metadata for a channel, | |
1194 | * we make them visible to the viewer threads. | |
1195 | */ | |
7591bab1 | 1196 | static void publish_connection_local_streams(struct relay_connection *conn) |
a4baae1b | 1197 | { |
7591bab1 MD |
1198 | struct relay_stream *stream; |
1199 | struct relay_session *session = conn->session; | |
a4baae1b | 1200 | |
7591bab1 MD |
1201 | /* |
1202 | * We publish all streams belonging to a session atomically wrt | |
1203 | * session lock. | |
1204 | */ | |
1205 | pthread_mutex_lock(&session->lock); | |
1206 | rcu_read_lock(); | |
1207 | cds_list_for_each_entry_rcu(stream, &session->recv_list, | |
1208 | recv_node) { | |
1209 | stream_publish(stream); | |
a4baae1b | 1210 | } |
7591bab1 | 1211 | rcu_read_unlock(); |
a4baae1b | 1212 | |
7591bab1 MD |
1213 | /* |
1214 | * Inform the viewer that there are new streams in the session. | |
1215 | */ | |
1216 | if (session->viewer_attached) { | |
1217 | uatomic_set(&session->new_streams, 1); | |
1218 | } | |
1219 | pthread_mutex_unlock(&session->lock); | |
a4baae1b JD |
1220 | } |
1221 | ||
61ace1d3 JG |
1222 | static int conform_channel_path(char *channel_path) |
1223 | { | |
1224 | int ret = 0; | |
1225 | ||
1226 | if (strstr("../", channel_path)) { | |
1227 | ERR("Refusing channel path as it walks up the path hierarchy: \"%s\"", | |
1228 | channel_path); | |
1229 | ret = -1; | |
1230 | goto end; | |
1231 | } | |
1232 | ||
1233 | if (*channel_path == '/') { | |
1234 | const size_t len = strlen(channel_path); | |
1235 | ||
1236 | /* | |
1237 | * Channel paths from peers prior to 2.11 are expressed as an | |
1238 | * absolute path that is, in reality, relative to the relay | |
1239 | * daemon's output directory. Remove the leading slash so it | |
1240 | * is correctly interpreted as a relative path later on. | |
1241 | * | |
1242 | * len (and not len - 1) is used to copy the trailing NULL. | |
1243 | */ | |
1244 | bcopy(channel_path + 1, channel_path, len); | |
1245 | } | |
1246 | end: | |
1247 | return ret; | |
1248 | } | |
1249 | ||
b8aa1682 JD |
1250 | /* |
1251 | * relay_add_stream: allocate a new stream for a session | |
1252 | */ | |
5312a3ed JG |
1253 | static int relay_add_stream(const struct lttcomm_relayd_hdr *recv_hdr, |
1254 | struct relay_connection *conn, | |
1255 | const struct lttng_buffer_view *payload) | |
b8aa1682 | 1256 | { |
7591bab1 MD |
1257 | int ret; |
1258 | ssize_t send_ret; | |
58eb9381 | 1259 | struct relay_session *session = conn->session; |
b8aa1682 JD |
1260 | struct relay_stream *stream = NULL; |
1261 | struct lttcomm_relayd_status_stream reply; | |
4030a636 | 1262 | struct ctf_trace *trace = NULL; |
7591bab1 MD |
1263 | uint64_t stream_handle = -1ULL; |
1264 | char *path_name = NULL, *channel_name = NULL; | |
1265 | uint64_t tracefile_size = 0, tracefile_count = 0; | |
61ace1d3 | 1266 | LTTNG_OPTIONAL(uint64_t) stream_chunk_id = {}; |
b8aa1682 | 1267 | |
5312a3ed | 1268 | if (!session || !conn->version_check_done) { |
b8aa1682 JD |
1269 | ERR("Trying to add a stream before version check"); |
1270 | ret = -1; | |
1271 | goto end_no_session; | |
1272 | } | |
1273 | ||
2f21a469 JR |
1274 | if (session->minor == 1) { |
1275 | /* For 2.1 */ | |
5312a3ed | 1276 | ret = cmd_recv_stream_2_1(payload, &path_name, |
7591bab1 | 1277 | &channel_name); |
2f21a469 JR |
1278 | } else if (session->minor > 1 && session->minor < 11) { |
1279 | /* From 2.2 to 2.10 */ | |
5312a3ed | 1280 | ret = cmd_recv_stream_2_2(payload, &path_name, |
7591bab1 | 1281 | &channel_name, &tracefile_size, &tracefile_count); |
2f21a469 JR |
1282 | } else { |
1283 | /* From 2.11 to ... */ | |
1284 | ret = cmd_recv_stream_2_11(payload, &path_name, | |
0b50e4b3 JG |
1285 | &channel_name, &tracefile_size, &tracefile_count, |
1286 | &stream_chunk_id.value); | |
1287 | stream_chunk_id.is_set = true; | |
0f907de1 | 1288 | } |
2f21a469 | 1289 | |
0f907de1 | 1290 | if (ret < 0) { |
7591bab1 | 1291 | goto send_reply; |
b8aa1682 JD |
1292 | } |
1293 | ||
61ace1d3 JG |
1294 | if (conform_channel_path(path_name)) { |
1295 | goto send_reply; | |
1296 | } | |
1297 | ||
7591bab1 | 1298 | trace = ctf_trace_get_by_path_or_create(session, path_name); |
2a174661 | 1299 | if (!trace) { |
7591bab1 | 1300 | goto send_reply; |
2a174661 | 1301 | } |
7591bab1 | 1302 | /* This stream here has one reference on the trace. */ |
2a174661 | 1303 | |
7591bab1 MD |
1304 | pthread_mutex_lock(&last_relay_stream_id_lock); |
1305 | stream_handle = ++last_relay_stream_id; | |
1306 | pthread_mutex_unlock(&last_relay_stream_id_lock); | |
d3e2ba59 | 1307 | |
7591bab1 MD |
1308 | /* We pass ownership of path_name and channel_name. */ |
1309 | stream = stream_create(trace, stream_handle, path_name, | |
61ace1d3 | 1310 | channel_name, tracefile_size, tracefile_count); |
7591bab1 MD |
1311 | path_name = NULL; |
1312 | channel_name = NULL; | |
a4baae1b | 1313 | |
2a174661 | 1314 | /* |
7591bab1 MD |
1315 | * Streams are the owners of their trace. Reference to trace is |
1316 | * kept within stream_create(). | |
2a174661 | 1317 | */ |
7591bab1 | 1318 | ctf_trace_put(trace); |
d3e2ba59 | 1319 | |
7591bab1 | 1320 | send_reply: |
53efb85a | 1321 | memset(&reply, 0, sizeof(reply)); |
7591bab1 MD |
1322 | reply.handle = htobe64(stream_handle); |
1323 | if (!stream) { | |
f73fabfd | 1324 | reply.ret_code = htobe32(LTTNG_ERR_UNK); |
b8aa1682 | 1325 | } else { |
f73fabfd | 1326 | reply.ret_code = htobe32(LTTNG_OK); |
b8aa1682 | 1327 | } |
5af40280 | 1328 | |
58eb9381 | 1329 | send_ret = conn->sock->ops->sendmsg(conn->sock, &reply, |
b8aa1682 | 1330 | sizeof(struct lttcomm_relayd_status_stream), 0); |
5312a3ed JG |
1331 | if (send_ret < (ssize_t) sizeof(reply)) { |
1332 | ERR("Failed to send \"add stream\" command reply (ret = %zd)", | |
1333 | send_ret); | |
1334 | ret = -1; | |
b8aa1682 JD |
1335 | } |
1336 | ||
1337 | end_no_session: | |
7591bab1 MD |
1338 | free(path_name); |
1339 | free(channel_name); | |
0f907de1 | 1340 | return ret; |
b8aa1682 JD |
1341 | } |
1342 | ||
173af62f DG |
1343 | /* |
1344 | * relay_close_stream: close a specific stream | |
1345 | */ | |
5312a3ed JG |
1346 | static int relay_close_stream(const struct lttcomm_relayd_hdr *recv_hdr, |
1347 | struct relay_connection *conn, | |
1348 | const struct lttng_buffer_view *payload) | |
173af62f | 1349 | { |
5312a3ed JG |
1350 | int ret; |
1351 | ssize_t send_ret; | |
58eb9381 | 1352 | struct relay_session *session = conn->session; |
173af62f DG |
1353 | struct lttcomm_relayd_close_stream stream_info; |
1354 | struct lttcomm_relayd_generic_reply reply; | |
1355 | struct relay_stream *stream; | |
173af62f DG |
1356 | |
1357 | DBG("Close stream received"); | |
1358 | ||
5312a3ed | 1359 | if (!session || !conn->version_check_done) { |
173af62f DG |
1360 | ERR("Trying to close a stream before version check"); |
1361 | ret = -1; | |
1362 | goto end_no_session; | |
1363 | } | |
1364 | ||
5312a3ed JG |
1365 | if (payload->size < sizeof(stream_info)) { |
1366 | ERR("Unexpected payload size in \"relay_close_stream\": expected >= %zu bytes, got %zu bytes", | |
1367 | sizeof(stream_info), payload->size); | |
173af62f DG |
1368 | ret = -1; |
1369 | goto end_no_session; | |
1370 | } | |
5312a3ed JG |
1371 | memcpy(&stream_info, payload->data, sizeof(stream_info)); |
1372 | stream_info.stream_id = be64toh(stream_info.stream_id); | |
1373 | stream_info.last_net_seq_num = be64toh(stream_info.last_net_seq_num); | |
173af62f | 1374 | |
5312a3ed | 1375 | stream = stream_get_by_id(stream_info.stream_id); |
173af62f DG |
1376 | if (!stream) { |
1377 | ret = -1; | |
7591bab1 | 1378 | goto end; |
173af62f | 1379 | } |
77f7bd85 MD |
1380 | |
1381 | /* | |
1382 | * Set last_net_seq_num before the close flag. Required by data | |
1383 | * pending check. | |
1384 | */ | |
7591bab1 | 1385 | pthread_mutex_lock(&stream->lock); |
5312a3ed | 1386 | stream->last_net_seq_num = stream_info.last_net_seq_num; |
77f7bd85 MD |
1387 | pthread_mutex_unlock(&stream->lock); |
1388 | ||
bda7c7b9 JG |
1389 | /* |
1390 | * This is one of the conditions which may trigger a stream close | |
1391 | * with the others being: | |
1392 | * 1) A close command is received for a stream | |
1393 | * 2) The control connection owning the stream is closed | |
1394 | * 3) We have received all of the stream's data _after_ a close | |
1395 | * request. | |
1396 | */ | |
1397 | try_stream_close(stream); | |
7591bab1 MD |
1398 | if (stream->is_metadata) { |
1399 | struct relay_viewer_stream *vstream; | |
173af62f | 1400 | |
7591bab1 MD |
1401 | vstream = viewer_stream_get_by_id(stream->stream_handle); |
1402 | if (vstream) { | |
1403 | if (vstream->metadata_sent == stream->metadata_received) { | |
1404 | /* | |
1405 | * Since all the metadata has been sent to the | |
1406 | * viewer and that we have a request to close | |
1407 | * its stream, we can safely teardown the | |
1408 | * corresponding metadata viewer stream. | |
1409 | */ | |
1410 | viewer_stream_put(vstream); | |
1411 | } | |
1412 | /* Put local reference. */ | |
1413 | viewer_stream_put(vstream); | |
1414 | } | |
1415 | } | |
7591bab1 | 1416 | stream_put(stream); |
5312a3ed | 1417 | ret = 0; |
173af62f | 1418 | |
7591bab1 | 1419 | end: |
53efb85a | 1420 | memset(&reply, 0, sizeof(reply)); |
173af62f | 1421 | if (ret < 0) { |
f73fabfd | 1422 | reply.ret_code = htobe32(LTTNG_ERR_UNK); |
173af62f | 1423 | } else { |
f73fabfd | 1424 | reply.ret_code = htobe32(LTTNG_OK); |
173af62f | 1425 | } |
58eb9381 | 1426 | send_ret = conn->sock->ops->sendmsg(conn->sock, &reply, |
173af62f | 1427 | sizeof(struct lttcomm_relayd_generic_reply), 0); |
5312a3ed JG |
1428 | if (send_ret < (ssize_t) sizeof(reply)) { |
1429 | ERR("Failed to send \"close stream\" command reply (ret = %zd)", | |
1430 | send_ret); | |
1431 | ret = -1; | |
173af62f DG |
1432 | } |
1433 | ||
1434 | end_no_session: | |
1435 | return ret; | |
1436 | } | |
1437 | ||
93ec662e JD |
1438 | /* |
1439 | * relay_reset_metadata: reset a metadata stream | |
1440 | */ | |
1441 | static | |
5312a3ed JG |
1442 | int relay_reset_metadata(const struct lttcomm_relayd_hdr *recv_hdr, |
1443 | struct relay_connection *conn, | |
1444 | const struct lttng_buffer_view *payload) | |
93ec662e | 1445 | { |
5312a3ed JG |
1446 | int ret; |
1447 | ssize_t send_ret; | |
93ec662e JD |
1448 | struct relay_session *session = conn->session; |
1449 | struct lttcomm_relayd_reset_metadata stream_info; | |
1450 | struct lttcomm_relayd_generic_reply reply; | |
1451 | struct relay_stream *stream; | |
1452 | ||
1453 | DBG("Reset metadata received"); | |
1454 | ||
5312a3ed | 1455 | if (!session || !conn->version_check_done) { |
93ec662e JD |
1456 | ERR("Trying to reset a metadata stream before version check"); |
1457 | ret = -1; | |
1458 | goto end_no_session; | |
1459 | } | |
1460 | ||
5312a3ed JG |
1461 | if (payload->size < sizeof(stream_info)) { |
1462 | ERR("Unexpected payload size in \"relay_reset_metadata\": expected >= %zu bytes, got %zu bytes", | |
1463 | sizeof(stream_info), payload->size); | |
93ec662e JD |
1464 | ret = -1; |
1465 | goto end_no_session; | |
1466 | } | |
5312a3ed JG |
1467 | memcpy(&stream_info, payload->data, sizeof(stream_info)); |
1468 | stream_info.stream_id = be64toh(stream_info.stream_id); | |
1469 | stream_info.version = be64toh(stream_info.version); | |
1470 | ||
1471 | DBG("Update metadata to version %" PRIu64, stream_info.version); | |
93ec662e JD |
1472 | |
1473 | /* Unsupported for live sessions for now. */ | |
1474 | if (session->live_timer != 0) { | |
1475 | ret = -1; | |
1476 | goto end; | |
1477 | } | |
1478 | ||
5312a3ed | 1479 | stream = stream_get_by_id(stream_info.stream_id); |
93ec662e JD |
1480 | if (!stream) { |
1481 | ret = -1; | |
1482 | goto end; | |
1483 | } | |
1484 | pthread_mutex_lock(&stream->lock); | |
1485 | if (!stream->is_metadata) { | |
1486 | ret = -1; | |
1487 | goto end_unlock; | |
1488 | } | |
1489 | ||
84a93c08 | 1490 | ret = stream_reset_file(stream); |
93ec662e | 1491 | if (ret < 0) { |
84a93c08 JG |
1492 | ERR("Failed to reset metadata stream %" PRIu64 |
1493 | ": stream_path = %s, channel = %s", | |
1494 | stream->stream_handle, stream->path_name, | |
1495 | stream->channel_name); | |
93ec662e JD |
1496 | goto end_unlock; |
1497 | } | |
93ec662e JD |
1498 | end_unlock: |
1499 | pthread_mutex_unlock(&stream->lock); | |
1500 | stream_put(stream); | |
1501 | ||
1502 | end: | |
1503 | memset(&reply, 0, sizeof(reply)); | |
1504 | if (ret < 0) { | |
1505 | reply.ret_code = htobe32(LTTNG_ERR_UNK); | |
1506 | } else { | |
1507 | reply.ret_code = htobe32(LTTNG_OK); | |
1508 | } | |
1509 | send_ret = conn->sock->ops->sendmsg(conn->sock, &reply, | |
1510 | sizeof(struct lttcomm_relayd_generic_reply), 0); | |
5312a3ed JG |
1511 | if (send_ret < (ssize_t) sizeof(reply)) { |
1512 | ERR("Failed to send \"reset metadata\" command reply (ret = %zd)", | |
1513 | send_ret); | |
1514 | ret = -1; | |
93ec662e JD |
1515 | } |
1516 | ||
1517 | end_no_session: | |
1518 | return ret; | |
1519 | } | |
1520 | ||
b8aa1682 JD |
1521 | /* |
1522 | * relay_unknown_command: send -1 if received unknown command | |
1523 | */ | |
7591bab1 | 1524 | static void relay_unknown_command(struct relay_connection *conn) |
b8aa1682 JD |
1525 | { |
1526 | struct lttcomm_relayd_generic_reply reply; | |
5312a3ed | 1527 | ssize_t send_ret; |
b8aa1682 | 1528 | |
53efb85a | 1529 | memset(&reply, 0, sizeof(reply)); |
f73fabfd | 1530 | reply.ret_code = htobe32(LTTNG_ERR_UNK); |
5312a3ed JG |
1531 | send_ret = conn->sock->ops->sendmsg(conn->sock, &reply, sizeof(reply), 0); |
1532 | if (send_ret < sizeof(reply)) { | |
1533 | ERR("Failed to send \"unknown command\" command reply (ret = %zd)", send_ret); | |
b8aa1682 JD |
1534 | } |
1535 | } | |
1536 | ||
1537 | /* | |
1538 | * relay_start: send an acknowledgment to the client to tell if we are | |
1539 | * ready to receive data. We are ready if a session is established. | |
1540 | */ | |
5312a3ed JG |
1541 | static int relay_start(const struct lttcomm_relayd_hdr *recv_hdr, |
1542 | struct relay_connection *conn, | |
1543 | const struct lttng_buffer_view *payload) | |
b8aa1682 | 1544 | { |
5312a3ed JG |
1545 | int ret = 0; |
1546 | ssize_t send_ret; | |
b8aa1682 | 1547 | struct lttcomm_relayd_generic_reply reply; |
58eb9381 | 1548 | struct relay_session *session = conn->session; |
b8aa1682 JD |
1549 | |
1550 | if (!session) { | |
1551 | DBG("Trying to start the streaming without a session established"); | |
f73fabfd | 1552 | ret = htobe32(LTTNG_ERR_UNK); |
b8aa1682 JD |
1553 | } |
1554 | ||
53efb85a | 1555 | memset(&reply, 0, sizeof(reply)); |
5312a3ed JG |
1556 | reply.ret_code = htobe32(LTTNG_OK); |
1557 | send_ret = conn->sock->ops->sendmsg(conn->sock, &reply, | |
1558 | sizeof(reply), 0); | |
1559 | if (send_ret < (ssize_t) sizeof(reply)) { | |
1560 | ERR("Failed to send \"relay_start\" command reply (ret = %zd)", | |
1561 | send_ret); | |
1562 | ret = -1; | |
b8aa1682 JD |
1563 | } |
1564 | ||
1565 | return ret; | |
1566 | } | |
1567 | ||
b8aa1682 | 1568 | /* |
7591bab1 | 1569 | * relay_recv_metadata: receive the metadata for the session. |
b8aa1682 | 1570 | */ |
5312a3ed JG |
1571 | static int relay_recv_metadata(const struct lttcomm_relayd_hdr *recv_hdr, |
1572 | struct relay_connection *conn, | |
1573 | const struct lttng_buffer_view *payload) | |
b8aa1682 | 1574 | { |
32d1569c | 1575 | int ret = 0; |
58eb9381 | 1576 | struct relay_session *session = conn->session; |
5312a3ed | 1577 | struct lttcomm_relayd_metadata_payload metadata_payload_header; |
b8aa1682 | 1578 | struct relay_stream *metadata_stream; |
5312a3ed | 1579 | uint64_t metadata_payload_size; |
84a93c08 | 1580 | struct lttng_buffer_view packet_view; |
b8aa1682 JD |
1581 | |
1582 | if (!session) { | |
1583 | ERR("Metadata sent before version check"); | |
1584 | ret = -1; | |
1585 | goto end; | |
1586 | } | |
1587 | ||
5312a3ed | 1588 | if (recv_hdr->data_size < sizeof(struct lttcomm_relayd_metadata_payload)) { |
f6416125 MD |
1589 | ERR("Incorrect data size"); |
1590 | ret = -1; | |
1591 | goto end; | |
1592 | } | |
5312a3ed JG |
1593 | metadata_payload_size = recv_hdr->data_size - |
1594 | sizeof(struct lttcomm_relayd_metadata_payload); | |
f6416125 | 1595 | |
5312a3ed JG |
1596 | memcpy(&metadata_payload_header, payload->data, |
1597 | sizeof(metadata_payload_header)); | |
1598 | metadata_payload_header.stream_id = be64toh( | |
1599 | metadata_payload_header.stream_id); | |
1600 | metadata_payload_header.padding_size = be32toh( | |
1601 | metadata_payload_header.padding_size); | |
9d1bbf21 | 1602 | |
5312a3ed | 1603 | metadata_stream = stream_get_by_id(metadata_payload_header.stream_id); |
b8aa1682 JD |
1604 | if (!metadata_stream) { |
1605 | ret = -1; | |
7591bab1 | 1606 | goto end; |
b8aa1682 JD |
1607 | } |
1608 | ||
84a93c08 JG |
1609 | packet_view = lttng_buffer_view_from_view(payload, |
1610 | sizeof(metadata_payload_header), metadata_payload_size); | |
1611 | if (!packet_view.data) { | |
1612 | ERR("Invalid metadata packet length announced by header"); | |
b8aa1682 | 1613 | ret = -1; |
7591bab1 | 1614 | goto end_put; |
b8aa1682 | 1615 | } |
1d4dfdef | 1616 | |
84a93c08 JG |
1617 | pthread_mutex_lock(&metadata_stream->lock); |
1618 | ret = stream_write(metadata_stream, &packet_view, | |
5312a3ed | 1619 | metadata_payload_header.padding_size); |
84a93c08 JG |
1620 | pthread_mutex_unlock(&metadata_stream->lock); |
1621 | if (ret){ | |
5312a3ed | 1622 | ret = -1; |
7591bab1 | 1623 | goto end_put; |
1d4dfdef | 1624 | } |
7591bab1 | 1625 | end_put: |
7591bab1 | 1626 | stream_put(metadata_stream); |
b8aa1682 JD |
1627 | end: |
1628 | return ret; | |
1629 | } | |
1630 | ||
1631 | /* | |
1632 | * relay_send_version: send relayd version number | |
1633 | */ | |
5312a3ed JG |
1634 | static int relay_send_version(const struct lttcomm_relayd_hdr *recv_hdr, |
1635 | struct relay_connection *conn, | |
1636 | const struct lttng_buffer_view *payload) | |
b8aa1682 | 1637 | { |
7f51dcba | 1638 | int ret; |
5312a3ed | 1639 | ssize_t send_ret; |
092b6259 | 1640 | struct lttcomm_relayd_version reply, msg; |
87cb6359 | 1641 | bool compatible = true; |
b8aa1682 | 1642 | |
5312a3ed | 1643 | conn->version_check_done = true; |
b8aa1682 | 1644 | |
092b6259 | 1645 | /* Get version from the other side. */ |
5312a3ed JG |
1646 | if (payload->size < sizeof(msg)) { |
1647 | ERR("Unexpected payload size in \"relay_send_version\": expected >= %zu bytes, got %zu bytes", | |
1648 | sizeof(msg), payload->size); | |
092b6259 | 1649 | ret = -1; |
092b6259 DG |
1650 | goto end; |
1651 | } | |
1652 | ||
5312a3ed JG |
1653 | memcpy(&msg, payload->data, sizeof(msg)); |
1654 | msg.major = be32toh(msg.major); | |
1655 | msg.minor = be32toh(msg.minor); | |
1656 | ||
53efb85a | 1657 | memset(&reply, 0, sizeof(reply)); |
d83a952c MD |
1658 | reply.major = RELAYD_VERSION_COMM_MAJOR; |
1659 | reply.minor = RELAYD_VERSION_COMM_MINOR; | |
d4519fa3 JD |
1660 | |
1661 | /* Major versions must be the same */ | |
5312a3ed | 1662 | if (reply.major != msg.major) { |
6151a90f | 1663 | DBG("Incompatible major versions (%u vs %u), deleting session", |
5312a3ed | 1664 | reply.major, msg.major); |
87cb6359 | 1665 | compatible = false; |
d4519fa3 JD |
1666 | } |
1667 | ||
58eb9381 | 1668 | conn->major = reply.major; |
0f907de1 | 1669 | /* We adapt to the lowest compatible version */ |
5312a3ed | 1670 | if (reply.minor <= msg.minor) { |
58eb9381 | 1671 | conn->minor = reply.minor; |
0f907de1 | 1672 | } else { |
5312a3ed | 1673 | conn->minor = msg.minor; |
0f907de1 JD |
1674 | } |
1675 | ||
6151a90f JD |
1676 | reply.major = htobe32(reply.major); |
1677 | reply.minor = htobe32(reply.minor); | |
5312a3ed JG |
1678 | send_ret = conn->sock->ops->sendmsg(conn->sock, &reply, |
1679 | sizeof(reply), 0); | |
1680 | if (send_ret < (ssize_t) sizeof(reply)) { | |
1681 | ERR("Failed to send \"send version\" command reply (ret = %zd)", | |
1682 | send_ret); | |
1683 | ret = -1; | |
1684 | goto end; | |
1685 | } else { | |
1686 | ret = 0; | |
6151a90f JD |
1687 | } |
1688 | ||
87cb6359 JD |
1689 | if (!compatible) { |
1690 | ret = -1; | |
1691 | goto end; | |
1692 | } | |
1693 | ||
58eb9381 DG |
1694 | DBG("Version check done using protocol %u.%u", conn->major, |
1695 | conn->minor); | |
b8aa1682 JD |
1696 | |
1697 | end: | |
1698 | return ret; | |
1699 | } | |
1700 | ||
c8f59ee5 | 1701 | /* |
6d805429 | 1702 | * Check for data pending for a given stream id from the session daemon. |
c8f59ee5 | 1703 | */ |
5312a3ed JG |
1704 | static int relay_data_pending(const struct lttcomm_relayd_hdr *recv_hdr, |
1705 | struct relay_connection *conn, | |
1706 | const struct lttng_buffer_view *payload) | |
c8f59ee5 | 1707 | { |
58eb9381 | 1708 | struct relay_session *session = conn->session; |
6d805429 | 1709 | struct lttcomm_relayd_data_pending msg; |
c8f59ee5 DG |
1710 | struct lttcomm_relayd_generic_reply reply; |
1711 | struct relay_stream *stream; | |
5312a3ed | 1712 | ssize_t send_ret; |
c8f59ee5 | 1713 | int ret; |
01921b6d | 1714 | uint64_t stream_seq; |
c8f59ee5 | 1715 | |
6d805429 | 1716 | DBG("Data pending command received"); |
c8f59ee5 | 1717 | |
5312a3ed | 1718 | if (!session || !conn->version_check_done) { |
c8f59ee5 DG |
1719 | ERR("Trying to check for data before version check"); |
1720 | ret = -1; | |
1721 | goto end_no_session; | |
1722 | } | |
1723 | ||
5312a3ed JG |
1724 | if (payload->size < sizeof(msg)) { |
1725 | ERR("Unexpected payload size in \"relay_data_pending\": expected >= %zu bytes, got %zu bytes", | |
1726 | sizeof(msg), payload->size); | |
c8f59ee5 DG |
1727 | ret = -1; |
1728 | goto end_no_session; | |
1729 | } | |
5312a3ed JG |
1730 | memcpy(&msg, payload->data, sizeof(msg)); |
1731 | msg.stream_id = be64toh(msg.stream_id); | |
1732 | msg.last_net_seq_num = be64toh(msg.last_net_seq_num); | |
c8f59ee5 | 1733 | |
5312a3ed | 1734 | stream = stream_get_by_id(msg.stream_id); |
de91f48a | 1735 | if (stream == NULL) { |
c8f59ee5 | 1736 | ret = -1; |
7591bab1 | 1737 | goto end; |
c8f59ee5 DG |
1738 | } |
1739 | ||
7591bab1 MD |
1740 | pthread_mutex_lock(&stream->lock); |
1741 | ||
01921b6d JG |
1742 | if (session_streams_have_index(session)) { |
1743 | /* | |
1744 | * Ensure that both the index and stream data have been | |
1745 | * flushed up to the requested point. | |
1746 | */ | |
f15741c1 | 1747 | stream_seq = min(stream->prev_data_seq, stream->prev_index_seq); |
01921b6d | 1748 | } else { |
f15741c1 | 1749 | stream_seq = stream->prev_data_seq; |
01921b6d | 1750 | } |
f15741c1 | 1751 | DBG("Data pending for stream id %" PRIu64 ": prev_data_seq %" PRIu64 |
01921b6d JG |
1752 | ", prev_index_seq %" PRIu64 |
1753 | ", and last_seq %" PRIu64, msg.stream_id, | |
f15741c1 | 1754 | stream->prev_data_seq, stream->prev_index_seq, |
01921b6d | 1755 | msg.last_net_seq_num); |
c8f59ee5 | 1756 | |
33832e64 | 1757 | /* Avoid wrapping issue */ |
01921b6d | 1758 | if (((int64_t) (stream_seq - msg.last_net_seq_num)) >= 0) { |
6d805429 | 1759 | /* Data has in fact been written and is NOT pending */ |
c8f59ee5 | 1760 | ret = 0; |
6d805429 DG |
1761 | } else { |
1762 | /* Data still being streamed thus pending */ | |
1763 | ret = 1; | |
c8f59ee5 DG |
1764 | } |
1765 | ||
7591bab1 MD |
1766 | stream->data_pending_check_done = true; |
1767 | pthread_mutex_unlock(&stream->lock); | |
f7079f67 | 1768 | |
7591bab1 MD |
1769 | stream_put(stream); |
1770 | end: | |
c8f59ee5 | 1771 | |
53efb85a | 1772 | memset(&reply, 0, sizeof(reply)); |
c8f59ee5 | 1773 | reply.ret_code = htobe32(ret); |
5312a3ed JG |
1774 | send_ret = conn->sock->ops->sendmsg(conn->sock, &reply, sizeof(reply), 0); |
1775 | if (send_ret < (ssize_t) sizeof(reply)) { | |
1776 | ERR("Failed to send \"data pending\" command reply (ret = %zd)", | |
1777 | send_ret); | |
1778 | ret = -1; | |
c8f59ee5 DG |
1779 | } |
1780 | ||
1781 | end_no_session: | |
1782 | return ret; | |
1783 | } | |
1784 | ||
1785 | /* | |
1786 | * Wait for the control socket to reach a quiescent state. | |
1787 | * | |
7591bab1 MD |
1788 | * Note that for now, when receiving this command from the session |
1789 | * daemon, this means that every subsequent commands or data received on | |
1790 | * the control socket has been handled. So, this is why we simply return | |
1791 | * OK here. | |
c8f59ee5 | 1792 | */ |
5312a3ed JG |
1793 | static int relay_quiescent_control(const struct lttcomm_relayd_hdr *recv_hdr, |
1794 | struct relay_connection *conn, | |
1795 | const struct lttng_buffer_view *payload) | |
c8f59ee5 DG |
1796 | { |
1797 | int ret; | |
5312a3ed | 1798 | ssize_t send_ret; |
ad7051c0 | 1799 | struct relay_stream *stream; |
ad7051c0 | 1800 | struct lttcomm_relayd_quiescent_control msg; |
c8f59ee5 DG |
1801 | struct lttcomm_relayd_generic_reply reply; |
1802 | ||
1803 | DBG("Checking quiescent state on control socket"); | |
1804 | ||
5312a3ed | 1805 | if (!conn->session || !conn->version_check_done) { |
ad7051c0 DG |
1806 | ERR("Trying to check for data before version check"); |
1807 | ret = -1; | |
1808 | goto end_no_session; | |
1809 | } | |
1810 | ||
5312a3ed JG |
1811 | if (payload->size < sizeof(msg)) { |
1812 | ERR("Unexpected payload size in \"relay_quiescent_control\": expected >= %zu bytes, got %zu bytes", | |
1813 | sizeof(msg), payload->size); | |
ad7051c0 DG |
1814 | ret = -1; |
1815 | goto end_no_session; | |
1816 | } | |
5312a3ed JG |
1817 | memcpy(&msg, payload->data, sizeof(msg)); |
1818 | msg.stream_id = be64toh(msg.stream_id); | |
ad7051c0 | 1819 | |
5312a3ed | 1820 | stream = stream_get_by_id(msg.stream_id); |
7591bab1 MD |
1821 | if (!stream) { |
1822 | goto reply; | |
1823 | } | |
1824 | pthread_mutex_lock(&stream->lock); | |
1825 | stream->data_pending_check_done = true; | |
1826 | pthread_mutex_unlock(&stream->lock); | |
5312a3ed JG |
1827 | |
1828 | DBG("Relay quiescent control pending flag set to %" PRIu64, msg.stream_id); | |
7591bab1 MD |
1829 | stream_put(stream); |
1830 | reply: | |
53efb85a | 1831 | memset(&reply, 0, sizeof(reply)); |
c8f59ee5 | 1832 | reply.ret_code = htobe32(LTTNG_OK); |
5312a3ed JG |
1833 | send_ret = conn->sock->ops->sendmsg(conn->sock, &reply, sizeof(reply), 0); |
1834 | if (send_ret < (ssize_t) sizeof(reply)) { | |
1835 | ERR("Failed to send \"quiescent control\" command reply (ret = %zd)", | |
1836 | send_ret); | |
1837 | ret = -1; | |
1838 | } else { | |
1839 | ret = 0; | |
c8f59ee5 DG |
1840 | } |
1841 | ||
ad7051c0 | 1842 | end_no_session: |
c8f59ee5 DG |
1843 | return ret; |
1844 | } | |
1845 | ||
f7079f67 | 1846 | /* |
7591bab1 MD |
1847 | * Initialize a data pending command. This means that a consumer is about |
1848 | * to ask for data pending for each stream it holds. Simply iterate over | |
1849 | * all streams of a session and set the data_pending_check_done flag. | |
f7079f67 DG |
1850 | * |
1851 | * This command returns to the client a LTTNG_OK code. | |
1852 | */ | |
5312a3ed JG |
1853 | static int relay_begin_data_pending(const struct lttcomm_relayd_hdr *recv_hdr, |
1854 | struct relay_connection *conn, | |
1855 | const struct lttng_buffer_view *payload) | |
f7079f67 DG |
1856 | { |
1857 | int ret; | |
5312a3ed | 1858 | ssize_t send_ret; |
f7079f67 DG |
1859 | struct lttng_ht_iter iter; |
1860 | struct lttcomm_relayd_begin_data_pending msg; | |
1861 | struct lttcomm_relayd_generic_reply reply; | |
1862 | struct relay_stream *stream; | |
f7079f67 DG |
1863 | |
1864 | assert(recv_hdr); | |
58eb9381 | 1865 | assert(conn); |
f7079f67 DG |
1866 | |
1867 | DBG("Init streams for data pending"); | |
1868 | ||
5312a3ed | 1869 | if (!conn->session || !conn->version_check_done) { |
f7079f67 DG |
1870 | ERR("Trying to check for data before version check"); |
1871 | ret = -1; | |
1872 | goto end_no_session; | |
1873 | } | |
1874 | ||
5312a3ed JG |
1875 | if (payload->size < sizeof(msg)) { |
1876 | ERR("Unexpected payload size in \"relay_begin_data_pending\": expected >= %zu bytes, got %zu bytes", | |
1877 | sizeof(msg), payload->size); | |
f7079f67 DG |
1878 | ret = -1; |
1879 | goto end_no_session; | |
1880 | } | |
5312a3ed JG |
1881 | memcpy(&msg, payload->data, sizeof(msg)); |
1882 | msg.session_id = be64toh(msg.session_id); | |
f7079f67 DG |
1883 | |
1884 | /* | |
7591bab1 MD |
1885 | * Iterate over all streams to set the begin data pending flag. |
1886 | * For now, the streams are indexed by stream handle so we have | |
1887 | * to iterate over all streams to find the one associated with | |
1888 | * the right session_id. | |
f7079f67 DG |
1889 | */ |
1890 | rcu_read_lock(); | |
d3e2ba59 | 1891 | cds_lfht_for_each_entry(relay_streams_ht->ht, &iter.iter, stream, |
2a174661 | 1892 | node.node) { |
7591bab1 MD |
1893 | if (!stream_get(stream)) { |
1894 | continue; | |
1895 | } | |
5312a3ed | 1896 | if (stream->trace->session->id == msg.session_id) { |
7591bab1 MD |
1897 | pthread_mutex_lock(&stream->lock); |
1898 | stream->data_pending_check_done = false; | |
1899 | pthread_mutex_unlock(&stream->lock); | |
f7079f67 DG |
1900 | DBG("Set begin data pending flag to stream %" PRIu64, |
1901 | stream->stream_handle); | |
1902 | } | |
7591bab1 | 1903 | stream_put(stream); |
f7079f67 DG |
1904 | } |
1905 | rcu_read_unlock(); | |
1906 | ||
53efb85a | 1907 | memset(&reply, 0, sizeof(reply)); |
f7079f67 DG |
1908 | /* All good, send back reply. */ |
1909 | reply.ret_code = htobe32(LTTNG_OK); | |
1910 | ||
5312a3ed JG |
1911 | send_ret = conn->sock->ops->sendmsg(conn->sock, &reply, sizeof(reply), 0); |
1912 | if (send_ret < (ssize_t) sizeof(reply)) { | |
1913 | ERR("Failed to send \"begin data pending\" command reply (ret = %zd)", | |
1914 | send_ret); | |
1915 | ret = -1; | |
1916 | } else { | |
1917 | ret = 0; | |
f7079f67 DG |
1918 | } |
1919 | ||
1920 | end_no_session: | |
1921 | return ret; | |
1922 | } | |
1923 | ||
1924 | /* | |
7591bab1 MD |
1925 | * End data pending command. This will check, for a given session id, if |
1926 | * each stream associated with it has its data_pending_check_done flag | |
1927 | * set. If not, this means that the client lost track of the stream but | |
1928 | * the data is still being streamed on our side. In this case, we inform | |
1929 | * the client that data is in flight. | |
f7079f67 DG |
1930 | * |
1931 | * Return to the client if there is data in flight or not with a ret_code. | |
1932 | */ | |
5312a3ed JG |
1933 | static int relay_end_data_pending(const struct lttcomm_relayd_hdr *recv_hdr, |
1934 | struct relay_connection *conn, | |
1935 | const struct lttng_buffer_view *payload) | |
f7079f67 DG |
1936 | { |
1937 | int ret; | |
5312a3ed | 1938 | ssize_t send_ret; |
f7079f67 DG |
1939 | struct lttng_ht_iter iter; |
1940 | struct lttcomm_relayd_end_data_pending msg; | |
1941 | struct lttcomm_relayd_generic_reply reply; | |
1942 | struct relay_stream *stream; | |
f7079f67 DG |
1943 | uint32_t is_data_inflight = 0; |
1944 | ||
f7079f67 DG |
1945 | DBG("End data pending command"); |
1946 | ||
5312a3ed | 1947 | if (!conn->session || !conn->version_check_done) { |
f7079f67 DG |
1948 | ERR("Trying to check for data before version check"); |
1949 | ret = -1; | |
1950 | goto end_no_session; | |
1951 | } | |
1952 | ||
5312a3ed JG |
1953 | if (payload->size < sizeof(msg)) { |
1954 | ERR("Unexpected payload size in \"relay_end_data_pending\": expected >= %zu bytes, got %zu bytes", | |
1955 | sizeof(msg), payload->size); | |
f7079f67 DG |
1956 | ret = -1; |
1957 | goto end_no_session; | |
1958 | } | |
5312a3ed JG |
1959 | memcpy(&msg, payload->data, sizeof(msg)); |
1960 | msg.session_id = be64toh(msg.session_id); | |
f7079f67 | 1961 | |
7591bab1 MD |
1962 | /* |
1963 | * Iterate over all streams to see if the begin data pending | |
1964 | * flag is set. | |
1965 | */ | |
f7079f67 | 1966 | rcu_read_lock(); |
d3e2ba59 | 1967 | cds_lfht_for_each_entry(relay_streams_ht->ht, &iter.iter, stream, |
2a174661 | 1968 | node.node) { |
7591bab1 MD |
1969 | if (!stream_get(stream)) { |
1970 | continue; | |
1971 | } | |
5312a3ed | 1972 | if (stream->trace->session->id != msg.session_id) { |
7591bab1 MD |
1973 | stream_put(stream); |
1974 | continue; | |
1975 | } | |
1976 | pthread_mutex_lock(&stream->lock); | |
1977 | if (!stream->data_pending_check_done) { | |
01921b6d JG |
1978 | uint64_t stream_seq; |
1979 | ||
1980 | if (session_streams_have_index(conn->session)) { | |
1981 | /* | |
1982 | * Ensure that both the index and stream data have been | |
1983 | * flushed up to the requested point. | |
1984 | */ | |
f15741c1 | 1985 | stream_seq = min(stream->prev_data_seq, stream->prev_index_seq); |
01921b6d | 1986 | } else { |
f15741c1 | 1987 | stream_seq = stream->prev_data_seq; |
01921b6d JG |
1988 | } |
1989 | if (!stream->closed || !(((int64_t) (stream_seq - stream->last_net_seq_num)) >= 0)) { | |
7591bab1 MD |
1990 | is_data_inflight = 1; |
1991 | DBG("Data is still in flight for stream %" PRIu64, | |
1992 | stream->stream_handle); | |
1993 | pthread_mutex_unlock(&stream->lock); | |
1994 | stream_put(stream); | |
1995 | break; | |
1996 | } | |
f7079f67 | 1997 | } |
7591bab1 MD |
1998 | pthread_mutex_unlock(&stream->lock); |
1999 | stream_put(stream); | |
f7079f67 DG |
2000 | } |
2001 | rcu_read_unlock(); | |
2002 | ||
53efb85a | 2003 | memset(&reply, 0, sizeof(reply)); |
f7079f67 DG |
2004 | /* All good, send back reply. */ |
2005 | reply.ret_code = htobe32(is_data_inflight); | |
2006 | ||
5312a3ed JG |
2007 | send_ret = conn->sock->ops->sendmsg(conn->sock, &reply, sizeof(reply), 0); |
2008 | if (send_ret < (ssize_t) sizeof(reply)) { | |
2009 | ERR("Failed to send \"end data pending\" command reply (ret = %zd)", | |
2010 | send_ret); | |
2011 | ret = -1; | |
2012 | } else { | |
2013 | ret = 0; | |
f7079f67 DG |
2014 | } |
2015 | ||
2016 | end_no_session: | |
2017 | return ret; | |
2018 | } | |
2019 | ||
1c20f0e2 JD |
2020 | /* |
2021 | * Receive an index for a specific stream. | |
2022 | * | |
2023 | * Return 0 on success else a negative value. | |
2024 | */ | |
5312a3ed JG |
2025 | static int relay_recv_index(const struct lttcomm_relayd_hdr *recv_hdr, |
2026 | struct relay_connection *conn, | |
2027 | const struct lttng_buffer_view *payload) | |
1c20f0e2 | 2028 | { |
5312a3ed JG |
2029 | int ret; |
2030 | ssize_t send_ret; | |
58eb9381 | 2031 | struct relay_session *session = conn->session; |
1c20f0e2 | 2032 | struct lttcomm_relayd_index index_info; |
1c20f0e2 JD |
2033 | struct lttcomm_relayd_generic_reply reply; |
2034 | struct relay_stream *stream; | |
f8f3885c | 2035 | size_t msg_len; |
1c20f0e2 | 2036 | |
58eb9381 | 2037 | assert(conn); |
1c20f0e2 JD |
2038 | |
2039 | DBG("Relay receiving index"); | |
2040 | ||
5312a3ed | 2041 | if (!session || !conn->version_check_done) { |
1c20f0e2 JD |
2042 | ERR("Trying to close a stream before version check"); |
2043 | ret = -1; | |
2044 | goto end_no_session; | |
2045 | } | |
2046 | ||
f8f3885c MD |
2047 | msg_len = lttcomm_relayd_index_len( |
2048 | lttng_to_index_major(conn->major, conn->minor), | |
2049 | lttng_to_index_minor(conn->major, conn->minor)); | |
5312a3ed JG |
2050 | if (payload->size < msg_len) { |
2051 | ERR("Unexpected payload size in \"relay_recv_index\": expected >= %zu bytes, got %zu bytes", | |
2052 | msg_len, payload->size); | |
1c20f0e2 JD |
2053 | ret = -1; |
2054 | goto end_no_session; | |
2055 | } | |
5312a3ed JG |
2056 | memcpy(&index_info, payload->data, msg_len); |
2057 | index_info.relay_stream_id = be64toh(index_info.relay_stream_id); | |
2058 | index_info.net_seq_num = be64toh(index_info.net_seq_num); | |
2059 | index_info.packet_size = be64toh(index_info.packet_size); | |
2060 | index_info.content_size = be64toh(index_info.content_size); | |
2061 | index_info.timestamp_begin = be64toh(index_info.timestamp_begin); | |
2062 | index_info.timestamp_end = be64toh(index_info.timestamp_end); | |
2063 | index_info.events_discarded = be64toh(index_info.events_discarded); | |
2064 | index_info.stream_id = be64toh(index_info.stream_id); | |
81df238b JR |
2065 | |
2066 | if (conn->minor >= 8) { | |
2067 | index_info.stream_instance_id = | |
2068 | be64toh(index_info.stream_instance_id); | |
2069 | index_info.packet_seq_num = be64toh(index_info.packet_seq_num); | |
2070 | } | |
5312a3ed JG |
2071 | |
2072 | stream = stream_get_by_id(index_info.relay_stream_id); | |
1c20f0e2 | 2073 | if (!stream) { |
7591bab1 | 2074 | ERR("stream_get_by_id not found"); |
1c20f0e2 | 2075 | ret = -1; |
7591bab1 | 2076 | goto end; |
1c20f0e2 | 2077 | } |
d3e2ba59 | 2078 | |
84a93c08 JG |
2079 | pthread_mutex_lock(&stream->lock); |
2080 | ret = stream_add_index(stream, &index_info); | |
2081 | pthread_mutex_unlock(&stream->lock); | |
2082 | if (ret) { | |
7591bab1 MD |
2083 | goto end_stream_put; |
2084 | } | |
1c20f0e2 | 2085 | |
7591bab1 | 2086 | end_stream_put: |
7591bab1 | 2087 | stream_put(stream); |
7591bab1 | 2088 | end: |
53efb85a | 2089 | memset(&reply, 0, sizeof(reply)); |
1c20f0e2 JD |
2090 | if (ret < 0) { |
2091 | reply.ret_code = htobe32(LTTNG_ERR_UNK); | |
2092 | } else { | |
2093 | reply.ret_code = htobe32(LTTNG_OK); | |
2094 | } | |
58eb9381 | 2095 | send_ret = conn->sock->ops->sendmsg(conn->sock, &reply, sizeof(reply), 0); |
5312a3ed JG |
2096 | if (send_ret < (ssize_t) sizeof(reply)) { |
2097 | ERR("Failed to send \"recv index\" command reply (ret = %zd)", send_ret); | |
2098 | ret = -1; | |
1c20f0e2 JD |
2099 | } |
2100 | ||
2101 | end_no_session: | |
2102 | return ret; | |
2103 | } | |
2104 | ||
a4baae1b JD |
2105 | /* |
2106 | * Receive the streams_sent message. | |
2107 | * | |
2108 | * Return 0 on success else a negative value. | |
2109 | */ | |
5312a3ed JG |
2110 | static int relay_streams_sent(const struct lttcomm_relayd_hdr *recv_hdr, |
2111 | struct relay_connection *conn, | |
2112 | const struct lttng_buffer_view *payload) | |
a4baae1b | 2113 | { |
5312a3ed JG |
2114 | int ret; |
2115 | ssize_t send_ret; | |
a4baae1b JD |
2116 | struct lttcomm_relayd_generic_reply reply; |
2117 | ||
58eb9381 | 2118 | assert(conn); |
a4baae1b JD |
2119 | |
2120 | DBG("Relay receiving streams_sent"); | |
2121 | ||
5312a3ed | 2122 | if (!conn->session || !conn->version_check_done) { |
a4baae1b JD |
2123 | ERR("Trying to close a stream before version check"); |
2124 | ret = -1; | |
2125 | goto end_no_session; | |
2126 | } | |
2127 | ||
2128 | /* | |
7591bab1 MD |
2129 | * Publish every pending stream in the connection recv list which are |
2130 | * now ready to be used by the viewer. | |
4a9daf17 | 2131 | */ |
7591bab1 | 2132 | publish_connection_local_streams(conn); |
4a9daf17 | 2133 | |
53efb85a | 2134 | memset(&reply, 0, sizeof(reply)); |
a4baae1b | 2135 | reply.ret_code = htobe32(LTTNG_OK); |
58eb9381 | 2136 | send_ret = conn->sock->ops->sendmsg(conn->sock, &reply, sizeof(reply), 0); |
5312a3ed JG |
2137 | if (send_ret < (ssize_t) sizeof(reply)) { |
2138 | ERR("Failed to send \"streams sent\" command reply (ret = %zd)", | |
2139 | send_ret); | |
2140 | ret = -1; | |
a4baae1b JD |
2141 | } else { |
2142 | /* Success. */ | |
2143 | ret = 0; | |
2144 | } | |
2145 | ||
2146 | end_no_session: | |
2147 | return ret; | |
2148 | } | |
2149 | ||
d3ecc550 | 2150 | /* |
84a93c08 JG |
2151 | * relay_rotate_session_stream: rotate a stream to a new tracefile for the |
2152 | * session rotation feature (not the tracefile rotation feature). | |
d3ecc550 | 2153 | */ |
84a93c08 JG |
2154 | static int relay_rotate_session_streams( |
2155 | const struct lttcomm_relayd_hdr *recv_hdr, | |
5312a3ed JG |
2156 | struct relay_connection *conn, |
2157 | const struct lttng_buffer_view *payload) | |
d3ecc550 | 2158 | { |
09621468 | 2159 | int ret = 0; |
84a93c08 | 2160 | uint32_t i; |
5312a3ed | 2161 | ssize_t send_ret; |
84a93c08 | 2162 | enum lttng_error_code reply_code = LTTNG_ERR_UNK; |
d3ecc550 | 2163 | struct relay_session *session = conn->session; |
84a93c08 JG |
2164 | struct lttcomm_relayd_rotate_streams rotate_streams; |
2165 | struct lttcomm_relayd_generic_reply reply = {}; | |
2166 | struct relay_stream *stream = NULL; | |
2167 | const size_t header_len = sizeof(struct lttcomm_relayd_rotate_streams); | |
2168 | struct lttng_trace_chunk *next_trace_chunk = NULL; | |
2169 | struct lttng_buffer_view stream_positions; | |
d0ae499d JG |
2170 | char chunk_id_buf[MAX_INT_DEC_LEN(uint64_t)]; |
2171 | const char *chunk_id_str = "none"; | |
d3ecc550 | 2172 | |
d3ecc550 JD |
2173 | if (!session || !conn->version_check_done) { |
2174 | ERR("Trying to rotate a stream before version check"); | |
2175 | ret = -1; | |
2176 | goto end_no_reply; | |
2177 | } | |
2178 | ||
2179 | if (session->major == 2 && session->minor < 11) { | |
2180 | ERR("Unsupported feature before 2.11"); | |
2181 | ret = -1; | |
2182 | goto end_no_reply; | |
2183 | } | |
2184 | ||
5312a3ed JG |
2185 | if (payload->size < header_len) { |
2186 | ERR("Unexpected payload size in \"relay_rotate_session_stream\": expected >= %zu bytes, got %zu bytes", | |
2187 | header_len, payload->size); | |
d3ecc550 JD |
2188 | ret = -1; |
2189 | goto end_no_reply; | |
2190 | } | |
2191 | ||
84a93c08 | 2192 | memcpy(&rotate_streams, payload->data, header_len); |
5312a3ed | 2193 | |
84a93c08 JG |
2194 | /* Convert header to host endianness. */ |
2195 | rotate_streams = (typeof(rotate_streams)) { | |
2196 | .stream_count = be32toh(rotate_streams.stream_count), | |
2197 | .new_chunk_id = (typeof(rotate_streams.new_chunk_id)) { | |
2198 | .is_set = !!rotate_streams.new_chunk_id.is_set, | |
2199 | .value = be64toh(rotate_streams.new_chunk_id.value), | |
2200 | } | |
2201 | }; | |
d3ecc550 | 2202 | |
84a93c08 JG |
2203 | if (rotate_streams.new_chunk_id.is_set) { |
2204 | /* | |
2205 | * Retrieve the trace chunk the stream must transition to. As | |
2206 | * per the protocol, this chunk should have been created | |
2207 | * before this command is received. | |
2208 | */ | |
2209 | next_trace_chunk = sessiond_trace_chunk_registry_get_chunk( | |
2210 | sessiond_trace_chunk_registry, | |
2211 | session->sessiond_uuid, session->id, | |
2212 | rotate_streams.new_chunk_id.value); | |
2213 | if (!next_trace_chunk) { | |
2214 | char uuid_str[UUID_STR_LEN]; | |
2215 | ||
2216 | lttng_uuid_to_str(session->sessiond_uuid, uuid_str); | |
2217 | ERR("Unknown next trace chunk in ROTATE_STREAMS command: sessiond_uuid = {%s}, session_id = %" PRIu64 | |
2218 | ", trace_chunk_id = %" PRIu64, | |
2219 | uuid_str, session->id, | |
2220 | rotate_streams.new_chunk_id.value); | |
2221 | reply_code = LTTNG_ERR_INVALID_PROTOCOL; | |
2222 | ret = -1; | |
2223 | goto end; | |
2224 | } | |
d0ae499d JG |
2225 | |
2226 | ret = snprintf(chunk_id_buf, sizeof(chunk_id_buf), "%" PRIu64, | |
2227 | rotate_streams.new_chunk_id.value); | |
2228 | if (ret < 0 || ret >= sizeof(chunk_id_buf)) { | |
2229 | chunk_id_str = "formatting error"; | |
2230 | } else { | |
2231 | chunk_id_str = chunk_id_buf; | |
2232 | } | |
41b23598 | 2233 | session->has_rotated = true; |
d3ecc550 JD |
2234 | } |
2235 | ||
d0ae499d JG |
2236 | DBG("Rotate %" PRIu32 " streams of session \"%s\" to chunk \"%s\"", |
2237 | rotate_streams.stream_count, session->session_name, | |
2238 | chunk_id_str); | |
2239 | ||
84a93c08 JG |
2240 | stream_positions = lttng_buffer_view_from_view(payload, |
2241 | sizeof(rotate_streams), -1); | |
2242 | if (!stream_positions.data || | |
2243 | stream_positions.size < | |
2244 | (rotate_streams.stream_count * | |
2245 | sizeof(struct lttcomm_relayd_stream_rotation_position))) { | |
2246 | reply_code = LTTNG_ERR_INVALID_PROTOCOL; | |
d3ecc550 | 2247 | ret = -1; |
5312a3ed | 2248 | goto end; |
d3ecc550 JD |
2249 | } |
2250 | ||
84a93c08 JG |
2251 | for (i = 0; i < rotate_streams.stream_count; i++) { |
2252 | struct lttcomm_relayd_stream_rotation_position *position_comm = | |
2253 | &((typeof(position_comm)) stream_positions.data)[i]; | |
2254 | const struct lttcomm_relayd_stream_rotation_position pos = { | |
2255 | .stream_id = be64toh(position_comm->stream_id), | |
2256 | .rotate_at_seq_num = be64toh( | |
2257 | position_comm->rotate_at_seq_num), | |
2258 | }; | |
5312a3ed | 2259 | |
84a93c08 JG |
2260 | stream = stream_get_by_id(pos.stream_id); |
2261 | if (!stream) { | |
2262 | reply_code = LTTNG_ERR_INVALID; | |
2263 | ret = -1; | |
2264 | goto end; | |
547333b5 JG |
2265 | } |
2266 | ||
84a93c08 JG |
2267 | pthread_mutex_lock(&stream->lock); |
2268 | ret = stream_set_pending_rotation(stream, next_trace_chunk, | |
2269 | pos.rotate_at_seq_num); | |
2270 | pthread_mutex_unlock(&stream->lock); | |
2271 | if (ret) { | |
2272 | reply_code = LTTNG_ERR_FILE_CREATION_ERROR; | |
2273 | goto end; | |
547333b5 | 2274 | } |
84a93c08 JG |
2275 | |
2276 | stream_put(stream); | |
2277 | stream = NULL; | |
d3ecc550 JD |
2278 | } |
2279 | ||
84a93c08 | 2280 | reply_code = LTTNG_OK; |
d3ecc550 | 2281 | end: |
84a93c08 JG |
2282 | if (stream) { |
2283 | stream_put(stream); | |
d3ecc550 | 2284 | } |
84a93c08 JG |
2285 | |
2286 | reply.ret_code = htobe32((uint32_t) reply_code); | |
d3ecc550 JD |
2287 | send_ret = conn->sock->ops->sendmsg(conn->sock, &reply, |
2288 | sizeof(struct lttcomm_relayd_generic_reply), 0); | |
5312a3ed JG |
2289 | if (send_ret < (ssize_t) sizeof(reply)) { |
2290 | ERR("Failed to send \"rotate session stream\" command reply (ret = %zd)", | |
2291 | send_ret); | |
2292 | ret = -1; | |
d3ecc550 JD |
2293 | } |
2294 | ||
9be27ca7 | 2295 | ret = 0; |
d3ecc550 | 2296 | end_no_reply: |
84a93c08 | 2297 | lttng_trace_chunk_put(next_trace_chunk); |
d3ecc550 JD |
2298 | return ret; |
2299 | } | |
2300 | ||
0ebdafe0 | 2301 | |
0ebdafe0 JG |
2302 | |
2303 | /* | |
2304 | * relay_create_trace_chunk: create a new trace chunk | |
2305 | */ | |
2306 | static int relay_create_trace_chunk(const struct lttcomm_relayd_hdr *recv_hdr, | |
2307 | struct relay_connection *conn, | |
2308 | const struct lttng_buffer_view *payload) | |
2309 | { | |
2310 | int ret = 0; | |
2311 | ssize_t send_ret; | |
2312 | struct relay_session *session = conn->session; | |
2313 | struct lttcomm_relayd_create_trace_chunk *msg; | |
2314 | struct lttcomm_relayd_generic_reply reply = {}; | |
2315 | struct lttng_buffer_view header_view; | |
2316 | struct lttng_buffer_view chunk_name_view; | |
2317 | struct lttng_trace_chunk *chunk = NULL, *published_chunk = NULL; | |
2318 | enum lttng_error_code reply_code = LTTNG_OK; | |
2319 | enum lttng_trace_chunk_status chunk_status; | |
2320 | struct lttng_directory_handle session_output; | |
2321 | ||
2322 | if (!session || !conn->version_check_done) { | |
2323 | ERR("Trying to create a trace chunk before version check"); | |
2324 | ret = -1; | |
2325 | goto end_no_reply; | |
2326 | } | |
2327 | ||
2328 | if (session->major == 2 && session->minor < 11) { | |
2329 | ERR("Chunk creation command is unsupported before 2.11"); | |
2330 | ret = -1; | |
2331 | goto end_no_reply; | |
2332 | } | |
2333 | ||
2334 | header_view = lttng_buffer_view_from_view(payload, 0, sizeof(*msg)); | |
2335 | if (!header_view.data) { | |
2336 | ERR("Failed to receive payload of chunk creation command"); | |
2337 | ret = -1; | |
2338 | goto end_no_reply; | |
2339 | } | |
2340 | ||
2341 | /* Convert to host endianness. */ | |
2342 | msg = (typeof(msg)) header_view.data; | |
2343 | msg->chunk_id = be64toh(msg->chunk_id); | |
2344 | msg->creation_timestamp = be64toh(msg->creation_timestamp); | |
2345 | msg->override_name_length = be32toh(msg->override_name_length); | |
2346 | ||
2347 | chunk = lttng_trace_chunk_create( | |
2348 | msg->chunk_id, msg->creation_timestamp); | |
2349 | if (!chunk) { | |
2350 | ERR("Failed to create trace chunk in trace chunk creation command"); | |
2351 | ret = -1; | |
2352 | reply_code = LTTNG_ERR_NOMEM; | |
2353 | goto end; | |
2354 | } | |
2355 | ||
2356 | if (msg->override_name_length) { | |
2357 | const char *name; | |
2358 | ||
2359 | chunk_name_view = lttng_buffer_view_from_view(payload, | |
2360 | sizeof(*msg), | |
2361 | msg->override_name_length); | |
2362 | name = chunk_name_view.data; | |
2363 | if (!name || name[msg->override_name_length - 1]) { | |
2364 | ERR("Failed to receive payload of chunk creation command"); | |
2365 | ret = -1; | |
2366 | reply_code = LTTNG_ERR_INVALID; | |
2367 | goto end; | |
2368 | } | |
2369 | ||
2370 | chunk_status = lttng_trace_chunk_override_name( | |
2371 | chunk, chunk_name_view.data); | |
2372 | switch (chunk_status) { | |
2373 | case LTTNG_TRACE_CHUNK_STATUS_OK: | |
2374 | break; | |
2375 | case LTTNG_TRACE_CHUNK_STATUS_INVALID_ARGUMENT: | |
2376 | ERR("Failed to set the name of new trace chunk in trace chunk creation command (invalid name)"); | |
2377 | reply_code = LTTNG_ERR_INVALID; | |
2378 | ret = -1; | |
2379 | goto end; | |
2380 | default: | |
2381 | ERR("Failed to set the name of new trace chunk in trace chunk creation command (unknown error)"); | |
2382 | reply_code = LTTNG_ERR_UNK; | |
2383 | ret = -1; | |
2384 | goto end; | |
2385 | } | |
2386 | } | |
2387 | ||
335875f6 | 2388 | ret = session_init_output_directory_handle( |
0ebdafe0 JG |
2389 | conn->session, &session_output); |
2390 | if (ret) { | |
2391 | reply_code = LTTNG_ERR_CREATE_DIR_FAIL; | |
2392 | goto end; | |
2393 | } | |
2394 | ||
2395 | chunk_status = lttng_trace_chunk_set_credentials_current_user(chunk); | |
2396 | if (chunk_status != LTTNG_TRACE_CHUNK_STATUS_OK) { | |
2397 | reply_code = LTTNG_ERR_UNK; | |
2398 | ret = -1; | |
2399 | goto end; | |
2400 | } | |
2401 | ||
2402 | chunk_status = lttng_trace_chunk_set_as_owner(chunk, &session_output); | |
2403 | if (chunk_status != LTTNG_TRACE_CHUNK_STATUS_OK) { | |
2404 | reply_code = LTTNG_ERR_UNK; | |
2405 | ret = -1; | |
2406 | goto end; | |
2407 | } | |
2408 | ||
2409 | published_chunk = sessiond_trace_chunk_registry_publish_chunk( | |
2410 | sessiond_trace_chunk_registry, | |
2411 | conn->session->sessiond_uuid, | |
2412 | conn->session->id, | |
2413 | chunk); | |
2414 | if (!published_chunk) { | |
2415 | char uuid_str[UUID_STR_LEN]; | |
2416 | ||
2417 | lttng_uuid_to_str(conn->session->sessiond_uuid, uuid_str); | |
2418 | ERR("Failed to publish chunk: sessiond_uuid = %s, session_id = %" PRIu64 ", chunk_id = %" PRIu64, | |
2419 | uuid_str, | |
2420 | conn->session->id, | |
2421 | msg->chunk_id); | |
2422 | ret = -1; | |
2423 | reply_code = LTTNG_ERR_NOMEM; | |
2424 | goto end; | |
2425 | } | |
2426 | ||
2427 | pthread_mutex_lock(&conn->session->lock); | |
a57bb1b0 JG |
2428 | if (conn->session->pending_closure_trace_chunk) { |
2429 | /* | |
2430 | * Invalid; this means a second create_trace_chunk command was | |
2431 | * received before a close_trace_chunk. | |
2432 | */ | |
2433 | ERR("Invalid trace chunk close command received; a trace chunk is already waiting for a trace chunk close command"); | |
2434 | reply_code = LTTNG_ERR_INVALID_PROTOCOL; | |
2435 | ret = -1; | |
2436 | goto end_unlock_session; | |
2437 | } | |
2438 | conn->session->pending_closure_trace_chunk = | |
2439 | conn->session->current_trace_chunk; | |
0ebdafe0 | 2440 | conn->session->current_trace_chunk = published_chunk; |
0ebdafe0 | 2441 | published_chunk = NULL; |
a57bb1b0 | 2442 | end_unlock_session: |
84a93c08 | 2443 | pthread_mutex_unlock(&conn->session->lock); |
0ebdafe0 JG |
2444 | end: |
2445 | reply.ret_code = htobe32((uint32_t) reply_code); | |
2446 | send_ret = conn->sock->ops->sendmsg(conn->sock, | |
2447 | &reply, | |
2448 | sizeof(struct lttcomm_relayd_generic_reply), | |
2449 | 0); | |
2450 | if (send_ret < (ssize_t) sizeof(reply)) { | |
2451 | ERR("Failed to send \"create trace chunk\" command reply (ret = %zd)", | |
2452 | send_ret); | |
2453 | ret = -1; | |
2454 | } | |
2455 | end_no_reply: | |
2456 | lttng_trace_chunk_put(chunk); | |
2457 | lttng_trace_chunk_put(published_chunk); | |
2458 | lttng_directory_handle_fini(&session_output); | |
2459 | return ret; | |
2460 | } | |
2461 | ||
6bbcff33 JG |
2462 | /* |
2463 | * relay_close_trace_chunk: close a trace chunk | |
2464 | */ | |
2465 | static int relay_close_trace_chunk(const struct lttcomm_relayd_hdr *recv_hdr, | |
2466 | struct relay_connection *conn, | |
2467 | const struct lttng_buffer_view *payload) | |
2468 | { | |
2469 | int ret = 0; | |
2470 | ssize_t send_ret; | |
2471 | struct relay_session *session = conn->session; | |
2472 | struct lttcomm_relayd_close_trace_chunk *msg; | |
41b23598 | 2473 | struct lttcomm_relayd_close_trace_chunk_reply reply = {}; |
6bbcff33 JG |
2474 | struct lttng_buffer_view header_view; |
2475 | struct lttng_trace_chunk *chunk = NULL; | |
2476 | enum lttng_error_code reply_code = LTTNG_OK; | |
2477 | enum lttng_trace_chunk_status chunk_status; | |
2478 | uint64_t chunk_id; | |
84a93c08 | 2479 | LTTNG_OPTIONAL(enum lttng_trace_chunk_command_type) close_command = {}; |
6bbcff33 | 2480 | time_t close_timestamp; |
41b23598 MD |
2481 | char closed_trace_chunk_path[LTTNG_PATH_MAX]; |
2482 | size_t path_length = 0; | |
2483 | const char *chunk_name = NULL; | |
2484 | struct lttng_dynamic_buffer reply_payload; | |
2485 | ||
2486 | lttng_dynamic_buffer_init(&reply_payload); | |
6bbcff33 JG |
2487 | |
2488 | if (!session || !conn->version_check_done) { | |
2489 | ERR("Trying to close a trace chunk before version check"); | |
2490 | ret = -1; | |
2491 | goto end_no_reply; | |
2492 | } | |
2493 | ||
2494 | if (session->major == 2 && session->minor < 11) { | |
2495 | ERR("Chunk close command is unsupported before 2.11"); | |
2496 | ret = -1; | |
2497 | goto end_no_reply; | |
2498 | } | |
2499 | ||
2500 | header_view = lttng_buffer_view_from_view(payload, 0, sizeof(*msg)); | |
2501 | if (!header_view.data) { | |
2502 | ERR("Failed to receive payload of chunk close command"); | |
2503 | ret = -1; | |
2504 | goto end_no_reply; | |
2505 | } | |
2506 | ||
2507 | /* Convert to host endianness. */ | |
2508 | msg = (typeof(msg)) header_view.data; | |
2509 | chunk_id = be64toh(msg->chunk_id); | |
2510 | close_timestamp = (time_t) be64toh(msg->close_timestamp); | |
2511 | close_command = (typeof(close_command)){ | |
2512 | .value = be32toh(msg->close_command.value), | |
2513 | .is_set = msg->close_command.is_set, | |
2514 | }; | |
2515 | ||
2516 | chunk = sessiond_trace_chunk_registry_get_chunk( | |
2517 | sessiond_trace_chunk_registry, | |
2518 | conn->session->sessiond_uuid, | |
2519 | conn->session->id, | |
2520 | chunk_id); | |
2521 | if (!chunk) { | |
2522 | char uuid_str[UUID_STR_LEN]; | |
2523 | ||
2524 | lttng_uuid_to_str(conn->session->sessiond_uuid, uuid_str); | |
2525 | ERR("Failed to find chunk to close: sessiond_uuid = %s, session_id = %" PRIu64 ", chunk_id = %" PRIu64, | |
2526 | uuid_str, | |
2527 | conn->session->id, | |
2528 | msg->chunk_id); | |
2529 | ret = -1; | |
2530 | reply_code = LTTNG_ERR_NOMEM; | |
2531 | goto end; | |
2532 | } | |
2533 | ||
a57bb1b0 JG |
2534 | pthread_mutex_lock(&session->lock); |
2535 | if (session->pending_closure_trace_chunk && | |
2536 | session->pending_closure_trace_chunk != chunk) { | |
2537 | ERR("Trace chunk close command for session \"%s\" does not target the trace chunk pending closure", | |
2538 | session->session_name); | |
2539 | reply_code = LTTNG_ERR_INVALID_PROTOCOL; | |
2540 | ret = -1; | |
2541 | goto end_unlock_session; | |
2542 | } | |
2543 | ||
6bbcff33 JG |
2544 | chunk_status = lttng_trace_chunk_set_close_timestamp( |
2545 | chunk, close_timestamp); | |
2546 | if (chunk_status != LTTNG_TRACE_CHUNK_STATUS_OK) { | |
2547 | ERR("Failed to set trace chunk close timestamp"); | |
2548 | ret = -1; | |
2549 | reply_code = LTTNG_ERR_UNK; | |
a57bb1b0 | 2550 | goto end_unlock_session; |
6bbcff33 JG |
2551 | } |
2552 | ||
2553 | if (close_command.is_set) { | |
2554 | chunk_status = lttng_trace_chunk_set_close_command( | |
2555 | chunk, close_command.value); | |
2556 | if (chunk_status != LTTNG_TRACE_CHUNK_STATUS_OK) { | |
2557 | ret = -1; | |
2558 | reply_code = LTTNG_ERR_INVALID; | |
a57bb1b0 | 2559 | goto end_unlock_session; |
6bbcff33 JG |
2560 | } |
2561 | } | |
41b23598 MD |
2562 | chunk_status = lttng_trace_chunk_get_name(chunk, &chunk_name, NULL); |
2563 | if (chunk_status != LTTNG_TRACE_CHUNK_STATUS_OK) { | |
2564 | ERR("Failed to get chunk name"); | |
2565 | ret = -1; | |
2566 | reply_code = LTTNG_ERR_UNK; | |
2567 | goto end_unlock_session; | |
2568 | } | |
2569 | if (!session->has_rotated && !session->snapshot) { | |
2570 | ret = lttng_strncpy(closed_trace_chunk_path, | |
2571 | session->output_path, | |
2572 | sizeof(closed_trace_chunk_path)); | |
2573 | if (ret) { | |
2574 | ERR("Failed to send trace chunk path: path length of %zu bytes exceeds the maximal allowed length of %zu bytes", | |
2575 | strlen(session->output_path), | |
2576 | sizeof(closed_trace_chunk_path)); | |
2577 | reply_code = LTTNG_ERR_NOMEM; | |
2578 | ret = -1; | |
2579 | goto end_unlock_session; | |
2580 | } | |
2581 | } else { | |
2582 | if (session->snapshot) { | |
2583 | ret = snprintf(closed_trace_chunk_path, | |
2584 | sizeof(closed_trace_chunk_path), | |
2585 | "%s/%s", session->output_path, | |
2586 | chunk_name); | |
2587 | } else { | |
2588 | ret = snprintf(closed_trace_chunk_path, | |
2589 | sizeof(closed_trace_chunk_path), | |
2590 | "%s/" DEFAULT_ARCHIVED_TRACE_CHUNKS_DIRECTORY | |
2591 | "/%s", | |
2592 | session->output_path, chunk_name); | |
2593 | } | |
2594 | if (ret < 0 || ret == sizeof(closed_trace_chunk_path)) { | |
2595 | ERR("Failed to format closed trace chunk resulting path"); | |
2596 | reply_code = ret < 0 ? LTTNG_ERR_UNK : LTTNG_ERR_NOMEM; | |
2597 | ret = -1; | |
2598 | goto end_unlock_session; | |
2599 | } | |
2600 | } | |
2601 | DBG("Reply chunk path on close: %s", closed_trace_chunk_path); | |
2602 | path_length = strlen(closed_trace_chunk_path) + 1; | |
2603 | if (path_length > UINT32_MAX) { | |
2604 | ERR("Closed trace chunk path exceeds the maximal length allowed by the protocol"); | |
2605 | ret = -1; | |
2606 | reply_code = LTTNG_ERR_INVALID_PROTOCOL; | |
2607 | goto end_unlock_session; | |
2608 | } | |
6bbcff33 | 2609 | |
84a93c08 JG |
2610 | if (session->current_trace_chunk == chunk) { |
2611 | /* | |
2612 | * After a trace chunk close command, no new streams | |
2613 | * referencing the chunk may be created. Hence, on the | |
2614 | * event that no new trace chunk have been created for | |
2615 | * the session, the reference to the current trace chunk | |
2616 | * is released in order to allow it to be reclaimed when | |
2617 | * the last stream releases its reference to it. | |
2618 | */ | |
2619 | lttng_trace_chunk_put(session->current_trace_chunk); | |
2620 | session->current_trace_chunk = NULL; | |
2621 | } | |
a57bb1b0 JG |
2622 | lttng_trace_chunk_put(session->pending_closure_trace_chunk); |
2623 | session->pending_closure_trace_chunk = NULL; | |
2624 | end_unlock_session: | |
84a93c08 JG |
2625 | pthread_mutex_unlock(&session->lock); |
2626 | ||
6bbcff33 | 2627 | end: |
41b23598 MD |
2628 | reply.generic.ret_code = htobe32((uint32_t) reply_code); |
2629 | reply.path_length = htobe32((uint32_t) path_length); | |
2630 | ret = lttng_dynamic_buffer_append( | |
2631 | &reply_payload, &reply, sizeof(reply)); | |
2632 | if (ret) { | |
2633 | ERR("Failed to append \"close trace chunk\" command reply header to payload buffer"); | |
2634 | goto end_no_reply; | |
2635 | } | |
2636 | ||
2637 | if (reply_code == LTTNG_OK) { | |
2638 | ret = lttng_dynamic_buffer_append(&reply_payload, | |
2639 | closed_trace_chunk_path, path_length); | |
2640 | if (ret) { | |
2641 | ERR("Failed to append \"close trace chunk\" command reply path to payload buffer"); | |
2642 | goto end_no_reply; | |
2643 | } | |
2644 | } | |
2645 | ||
6bbcff33 | 2646 | send_ret = conn->sock->ops->sendmsg(conn->sock, |
41b23598 MD |
2647 | reply_payload.data, |
2648 | reply_payload.size, | |
6bbcff33 | 2649 | 0); |
41b23598 MD |
2650 | if (send_ret < reply_payload.size) { |
2651 | ERR("Failed to send \"close trace chunk\" command reply of %zu bytes (ret = %zd)", | |
2652 | reply_payload.size, send_ret); | |
6bbcff33 | 2653 | ret = -1; |
41b23598 | 2654 | goto end_no_reply; |
6bbcff33 JG |
2655 | } |
2656 | end_no_reply: | |
2657 | lttng_trace_chunk_put(chunk); | |
41b23598 | 2658 | lttng_dynamic_buffer_reset(&reply_payload); |
6bbcff33 JG |
2659 | return ret; |
2660 | } | |
2661 | ||
84a93c08 JG |
2662 | /* |
2663 | * relay_trace_chunk_exists: check if a trace chunk exists | |
2664 | */ | |
2665 | static int relay_trace_chunk_exists(const struct lttcomm_relayd_hdr *recv_hdr, | |
2666 | struct relay_connection *conn, | |
2667 | const struct lttng_buffer_view *payload) | |
2668 | { | |
2669 | int ret = 0; | |
2670 | ssize_t send_ret; | |
2671 | struct relay_session *session = conn->session; | |
2672 | struct lttcomm_relayd_trace_chunk_exists *msg; | |
2673 | struct lttcomm_relayd_trace_chunk_exists_reply reply = {}; | |
2674 | struct lttng_buffer_view header_view; | |
2675 | struct lttng_trace_chunk *chunk = NULL; | |
2676 | uint64_t chunk_id; | |
2677 | ||
2678 | if (!session || !conn->version_check_done) { | |
2679 | ERR("Trying to close a trace chunk before version check"); | |
2680 | ret = -1; | |
2681 | goto end_no_reply; | |
2682 | } | |
2683 | ||
2684 | if (session->major == 2 && session->minor < 11) { | |
2685 | ERR("Chunk close command is unsupported before 2.11"); | |
2686 | ret = -1; | |
2687 | goto end_no_reply; | |
2688 | } | |
2689 | ||
2690 | header_view = lttng_buffer_view_from_view(payload, 0, sizeof(*msg)); | |
2691 | if (!header_view.data) { | |
2692 | ERR("Failed to receive payload of chunk close command"); | |
2693 | ret = -1; | |
2694 | goto end_no_reply; | |
2695 | } | |
2696 | ||
2697 | /* Convert to host endianness. */ | |
2698 | msg = (typeof(msg)) header_view.data; | |
2699 | chunk_id = be64toh(msg->chunk_id); | |
2700 | ||
2701 | chunk = sessiond_trace_chunk_registry_get_chunk( | |
2702 | sessiond_trace_chunk_registry, | |
2703 | conn->session->sessiond_uuid, | |
2704 | conn->session->id, | |
2705 | chunk_id); | |
2706 | ||
2707 | reply = (typeof(reply)) { | |
2708 | .generic.ret_code = htobe32((uint32_t) LTTNG_OK), | |
2709 | .trace_chunk_exists = !!chunk, | |
2710 | }; | |
2711 | send_ret = conn->sock->ops->sendmsg(conn->sock, | |
2712 | &reply, sizeof(reply), 0); | |
2713 | if (send_ret < (ssize_t) sizeof(reply)) { | |
2714 | ERR("Failed to send \"create trace chunk\" command reply (ret = %zd)", | |
2715 | send_ret); | |
2716 | ret = -1; | |
2717 | } | |
2718 | end_no_reply: | |
2719 | lttng_trace_chunk_put(chunk); | |
2720 | return ret; | |
2721 | } | |
2722 | ||
5312a3ed JG |
2723 | #define DBG_CMD(cmd_name, conn) \ |
2724 | DBG3("Processing \"%s\" command for socket %i", cmd_name, conn->sock->fd); | |
2725 | ||
2726 | static int relay_process_control_command(struct relay_connection *conn, | |
2727 | const struct lttcomm_relayd_hdr *header, | |
2728 | const struct lttng_buffer_view *payload) | |
b8aa1682 JD |
2729 | { |
2730 | int ret = 0; | |
2731 | ||
5312a3ed | 2732 | switch (header->cmd) { |
b8aa1682 | 2733 | case RELAYD_CREATE_SESSION: |
5312a3ed JG |
2734 | DBG_CMD("RELAYD_CREATE_SESSION", conn); |
2735 | ret = relay_create_session(header, conn, payload); | |
b8aa1682 | 2736 | break; |
b8aa1682 | 2737 | case RELAYD_ADD_STREAM: |
5312a3ed JG |
2738 | DBG_CMD("RELAYD_ADD_STREAM", conn); |
2739 | ret = relay_add_stream(header, conn, payload); | |
b8aa1682 JD |
2740 | break; |
2741 | case RELAYD_START_DATA: | |
5312a3ed JG |
2742 | DBG_CMD("RELAYD_START_DATA", conn); |
2743 | ret = relay_start(header, conn, payload); | |
b8aa1682 JD |
2744 | break; |
2745 | case RELAYD_SEND_METADATA: | |
5312a3ed JG |
2746 | DBG_CMD("RELAYD_SEND_METADATA", conn); |
2747 | ret = relay_recv_metadata(header, conn, payload); | |
b8aa1682 JD |
2748 | break; |
2749 | case RELAYD_VERSION: | |
5312a3ed JG |
2750 | DBG_CMD("RELAYD_VERSION", conn); |
2751 | ret = relay_send_version(header, conn, payload); | |
b8aa1682 | 2752 | break; |
173af62f | 2753 | case RELAYD_CLOSE_STREAM: |
5312a3ed JG |
2754 | DBG_CMD("RELAYD_CLOSE_STREAM", conn); |
2755 | ret = relay_close_stream(header, conn, payload); | |
173af62f | 2756 | break; |
6d805429 | 2757 | case RELAYD_DATA_PENDING: |
5312a3ed JG |
2758 | DBG_CMD("RELAYD_DATA_PENDING", conn); |
2759 | ret = relay_data_pending(header, conn, payload); | |
c8f59ee5 DG |
2760 | break; |
2761 | case RELAYD_QUIESCENT_CONTROL: | |
5312a3ed JG |
2762 | DBG_CMD("RELAYD_QUIESCENT_CONTROL", conn); |
2763 | ret = relay_quiescent_control(header, conn, payload); | |
c8f59ee5 | 2764 | break; |
f7079f67 | 2765 | case RELAYD_BEGIN_DATA_PENDING: |
5312a3ed JG |
2766 | DBG_CMD("RELAYD_BEGIN_DATA_PENDING", conn); |
2767 | ret = relay_begin_data_pending(header, conn, payload); | |
f7079f67 DG |
2768 | break; |
2769 | case RELAYD_END_DATA_PENDING: | |
5312a3ed JG |
2770 | DBG_CMD("RELAYD_END_DATA_PENDING", conn); |
2771 | ret = relay_end_data_pending(header, conn, payload); | |
f7079f67 | 2772 | break; |
1c20f0e2 | 2773 | case RELAYD_SEND_INDEX: |
5312a3ed JG |
2774 | DBG_CMD("RELAYD_SEND_INDEX", conn); |
2775 | ret = relay_recv_index(header, conn, payload); | |
1c20f0e2 | 2776 | break; |
a4baae1b | 2777 | case RELAYD_STREAMS_SENT: |
5312a3ed JG |
2778 | DBG_CMD("RELAYD_STREAMS_SENT", conn); |
2779 | ret = relay_streams_sent(header, conn, payload); | |
a4baae1b | 2780 | break; |
93ec662e | 2781 | case RELAYD_RESET_METADATA: |
5312a3ed JG |
2782 | DBG_CMD("RELAYD_RESET_METADATA", conn); |
2783 | ret = relay_reset_metadata(header, conn, payload); | |
93ec662e | 2784 | break; |
84a93c08 JG |
2785 | case RELAYD_ROTATE_STREAMS: |
2786 | DBG_CMD("RELAYD_ROTATE_STREAMS", conn); | |
2787 | ret = relay_rotate_session_streams(header, conn, payload); | |
d3ecc550 | 2788 | break; |
0ebdafe0 JG |
2789 | case RELAYD_CREATE_TRACE_CHUNK: |
2790 | DBG_CMD("RELAYD_CREATE_TRACE_CHUNK", conn); | |
2791 | ret = relay_create_trace_chunk(header, conn, payload); | |
2792 | break; | |
6bbcff33 JG |
2793 | case RELAYD_CLOSE_TRACE_CHUNK: |
2794 | DBG_CMD("RELAYD_CLOSE_TRACE_CHUNK", conn); | |
2795 | ret = relay_close_trace_chunk(header, conn, payload); | |
2796 | break; | |
84a93c08 JG |
2797 | case RELAYD_TRACE_CHUNK_EXISTS: |
2798 | DBG_CMD("RELAYD_TRACE_CHUNK_EXISTS", conn); | |
2799 | ret = relay_trace_chunk_exists(header, conn, payload); | |
2800 | break; | |
b8aa1682 JD |
2801 | case RELAYD_UPDATE_SYNC_INFO: |
2802 | default: | |
5312a3ed | 2803 | ERR("Received unknown command (%u)", header->cmd); |
58eb9381 | 2804 | relay_unknown_command(conn); |
b8aa1682 JD |
2805 | ret = -1; |
2806 | goto end; | |
2807 | } | |
2808 | ||
2809 | end: | |
2810 | return ret; | |
2811 | } | |
2812 | ||
5569b118 JG |
2813 | static enum relay_connection_status relay_process_control_receive_payload( |
2814 | struct relay_connection *conn) | |
5312a3ed JG |
2815 | { |
2816 | int ret = 0; | |
5569b118 | 2817 | enum relay_connection_status status = RELAY_CONNECTION_STATUS_OK; |
5312a3ed JG |
2818 | struct lttng_dynamic_buffer *reception_buffer = |
2819 | &conn->protocol.ctrl.reception_buffer; | |
2820 | struct ctrl_connection_state_receive_payload *state = | |
2821 | &conn->protocol.ctrl.state.receive_payload; | |
2822 | struct lttng_buffer_view payload_view; | |
2823 | ||
2824 | if (state->left_to_receive == 0) { | |
2825 | /* Short-circuit for payload-less commands. */ | |
2826 | goto reception_complete; | |
2827 | } | |
2828 | ||
2829 | ret = conn->sock->ops->recvmsg(conn->sock, | |
2830 | reception_buffer->data + state->received, | |
2831 | state->left_to_receive, MSG_DONTWAIT); | |
2832 | if (ret < 0) { | |
5569b118 JG |
2833 | if (errno != EAGAIN && errno != EWOULDBLOCK) { |
2834 | PERROR("Unable to receive command payload on sock %d", | |
2835 | conn->sock->fd); | |
2836 | status = RELAY_CONNECTION_STATUS_ERROR; | |
2837 | } | |
5312a3ed JG |
2838 | goto end; |
2839 | } else if (ret == 0) { | |
2840 | DBG("Socket %d performed an orderly shutdown (received EOF)", conn->sock->fd); | |
5569b118 | 2841 | status = RELAY_CONNECTION_STATUS_CLOSED; |
5312a3ed JG |
2842 | goto end; |
2843 | } | |
2844 | ||
2845 | assert(ret > 0); | |
2846 | assert(ret <= state->left_to_receive); | |
2847 | ||
2848 | state->left_to_receive -= ret; | |
2849 | state->received += ret; | |
2850 | ||
2851 | if (state->left_to_receive > 0) { | |
2852 | /* | |
2853 | * Can't transition to the protocol's next state, wait to | |
2854 | * receive the rest of the header. | |
2855 | */ | |
2856 | DBG3("Partial reception of control connection protocol payload (received %" PRIu64 " bytes, %" PRIu64 " bytes left to receive, fd = %i)", | |
2857 | state->received, state->left_to_receive, | |
2858 | conn->sock->fd); | |
5312a3ed JG |
2859 | goto end; |
2860 | } | |
2861 | ||
2862 | reception_complete: | |
2863 | DBG("Done receiving control command payload: fd = %i, payload size = %" PRIu64 " bytes", | |
2864 | conn->sock->fd, state->received); | |
2865 | /* | |
2866 | * The payload required to process the command has been received. | |
2867 | * A view to the reception buffer is forwarded to the various | |
2868 | * commands and the state of the control is reset on success. | |
2869 | * | |
2870 | * Commands are responsible for sending their reply to the peer. | |
2871 | */ | |
2872 | payload_view = lttng_buffer_view_from_dynamic_buffer(reception_buffer, | |
2873 | 0, -1); | |
2874 | ret = relay_process_control_command(conn, | |
2875 | &state->header, &payload_view); | |
2876 | if (ret < 0) { | |
5569b118 | 2877 | status = RELAY_CONNECTION_STATUS_ERROR; |
5312a3ed JG |
2878 | goto end; |
2879 | } | |
2880 | ||
2881 | ret = connection_reset_protocol_state(conn); | |
5569b118 JG |
2882 | if (ret) { |
2883 | status = RELAY_CONNECTION_STATUS_ERROR; | |
2884 | } | |
5312a3ed | 2885 | end: |
5569b118 | 2886 | return status; |
5312a3ed JG |
2887 | } |
2888 | ||
5569b118 JG |
2889 | static enum relay_connection_status relay_process_control_receive_header( |
2890 | struct relay_connection *conn) | |
5312a3ed JG |
2891 | { |
2892 | int ret = 0; | |
5569b118 | 2893 | enum relay_connection_status status = RELAY_CONNECTION_STATUS_OK; |
5312a3ed JG |
2894 | struct lttcomm_relayd_hdr header; |
2895 | struct lttng_dynamic_buffer *reception_buffer = | |
2896 | &conn->protocol.ctrl.reception_buffer; | |
2897 | struct ctrl_connection_state_receive_header *state = | |
2898 | &conn->protocol.ctrl.state.receive_header; | |
2899 | ||
2900 | assert(state->left_to_receive != 0); | |
2901 | ||
2902 | ret = conn->sock->ops->recvmsg(conn->sock, | |
2903 | reception_buffer->data + state->received, | |
2904 | state->left_to_receive, MSG_DONTWAIT); | |
2905 | if (ret < 0) { | |
5569b118 JG |
2906 | if (errno != EAGAIN && errno != EWOULDBLOCK) { |
2907 | PERROR("Unable to receive control command header on sock %d", | |
2908 | conn->sock->fd); | |
2909 | status = RELAY_CONNECTION_STATUS_ERROR; | |
2910 | } | |
5312a3ed JG |
2911 | goto end; |
2912 | } else if (ret == 0) { | |
2913 | DBG("Socket %d performed an orderly shutdown (received EOF)", conn->sock->fd); | |
5569b118 | 2914 | status = RELAY_CONNECTION_STATUS_CLOSED; |
5312a3ed JG |
2915 | goto end; |
2916 | } | |
2917 | ||
2918 | assert(ret > 0); | |
2919 | assert(ret <= state->left_to_receive); | |
2920 | ||
2921 | state->left_to_receive -= ret; | |
2922 | state->received += ret; | |
2923 | ||
2924 | if (state->left_to_receive > 0) { | |
2925 | /* | |
2926 | * Can't transition to the protocol's next state, wait to | |
2927 | * receive the rest of the header. | |
2928 | */ | |
2929 | DBG3("Partial reception of control connection protocol header (received %" PRIu64 " bytes, %" PRIu64 " bytes left to receive, fd = %i)", | |
2930 | state->received, state->left_to_receive, | |
2931 | conn->sock->fd); | |
5312a3ed JG |
2932 | goto end; |
2933 | } | |
2934 | ||
2935 | /* Transition to next state: receiving the command's payload. */ | |
2936 | conn->protocol.ctrl.state_id = | |
2937 | CTRL_CONNECTION_STATE_RECEIVE_PAYLOAD; | |
2938 | memcpy(&header, reception_buffer->data, sizeof(header)); | |
2939 | header.circuit_id = be64toh(header.circuit_id); | |
2940 | header.data_size = be64toh(header.data_size); | |
2941 | header.cmd = be32toh(header.cmd); | |
2942 | header.cmd_version = be32toh(header.cmd_version); | |
2943 | memcpy(&conn->protocol.ctrl.state.receive_payload.header, | |
2944 | &header, sizeof(header)); | |
2945 | ||
2946 | DBG("Done receiving control command header: fd = %i, cmd = %" PRIu32 ", cmd_version = %" PRIu32 ", payload size = %" PRIu64 " bytes", | |
2947 | conn->sock->fd, header.cmd, header.cmd_version, | |
2948 | header.data_size); | |
2949 | ||
715e6fb1 | 2950 | if (header.data_size > DEFAULT_NETWORK_RELAYD_CTRL_MAX_PAYLOAD_SIZE) { |
5312a3ed JG |
2951 | ERR("Command header indicates a payload (%" PRIu64 " bytes) that exceeds the maximal payload size allowed on a control connection.", |
2952 | header.data_size); | |
5569b118 | 2953 | status = RELAY_CONNECTION_STATUS_ERROR; |
5312a3ed JG |
2954 | goto end; |
2955 | } | |
2956 | ||
2957 | conn->protocol.ctrl.state.receive_payload.left_to_receive = | |
2958 | header.data_size; | |
2959 | conn->protocol.ctrl.state.receive_payload.received = 0; | |
2960 | ret = lttng_dynamic_buffer_set_size(reception_buffer, | |
2961 | header.data_size); | |
2962 | if (ret) { | |
5569b118 | 2963 | status = RELAY_CONNECTION_STATUS_ERROR; |
5312a3ed JG |
2964 | goto end; |
2965 | } | |
2966 | ||
2967 | if (header.data_size == 0) { | |
2968 | /* | |
2969 | * Manually invoke the next state as the poll loop | |
2970 | * will not wake-up to allow us to proceed further. | |
2971 | */ | |
5569b118 | 2972 | status = relay_process_control_receive_payload(conn); |
5312a3ed JG |
2973 | } |
2974 | end: | |
5569b118 | 2975 | return status; |
5312a3ed JG |
2976 | } |
2977 | ||
2978 | /* | |
2979 | * Process the commands received on the control socket | |
2980 | */ | |
5569b118 JG |
2981 | static enum relay_connection_status relay_process_control( |
2982 | struct relay_connection *conn) | |
5312a3ed | 2983 | { |
5569b118 | 2984 | enum relay_connection_status status; |
5312a3ed JG |
2985 | |
2986 | switch (conn->protocol.ctrl.state_id) { | |
2987 | case CTRL_CONNECTION_STATE_RECEIVE_HEADER: | |
5569b118 | 2988 | status = relay_process_control_receive_header(conn); |
5312a3ed JG |
2989 | break; |
2990 | case CTRL_CONNECTION_STATE_RECEIVE_PAYLOAD: | |
5569b118 | 2991 | status = relay_process_control_receive_payload(conn); |
5312a3ed JG |
2992 | break; |
2993 | default: | |
2994 | ERR("Unknown control connection protocol state encountered."); | |
2995 | abort(); | |
2996 | } | |
2997 | ||
5569b118 | 2998 | return status; |
5312a3ed JG |
2999 | } |
3000 | ||
5569b118 JG |
3001 | static enum relay_connection_status relay_process_data_receive_header( |
3002 | struct relay_connection *conn) | |
b8aa1682 | 3003 | { |
5312a3ed | 3004 | int ret; |
5569b118 | 3005 | enum relay_connection_status status = RELAY_CONNECTION_STATUS_OK; |
5312a3ed JG |
3006 | struct data_connection_state_receive_header *state = |
3007 | &conn->protocol.data.state.receive_header; | |
3008 | struct lttcomm_relayd_data_hdr header; | |
b8aa1682 | 3009 | struct relay_stream *stream; |
5312a3ed JG |
3010 | |
3011 | assert(state->left_to_receive != 0); | |
3012 | ||
3013 | ret = conn->sock->ops->recvmsg(conn->sock, | |
3014 | state->header_reception_buffer + state->received, | |
3015 | state->left_to_receive, MSG_DONTWAIT); | |
3016 | if (ret < 0) { | |
5569b118 JG |
3017 | if (errno != EAGAIN && errno != EWOULDBLOCK) { |
3018 | PERROR("Unable to receive data header on sock %d", conn->sock->fd); | |
3019 | status = RELAY_CONNECTION_STATUS_ERROR; | |
3020 | } | |
5312a3ed JG |
3021 | goto end; |
3022 | } else if (ret == 0) { | |
3023 | /* Orderly shutdown. Not necessary to print an error. */ | |
3024 | DBG("Socket %d performed an orderly shutdown (received EOF)", conn->sock->fd); | |
5569b118 | 3025 | status = RELAY_CONNECTION_STATUS_CLOSED; |
b8aa1682 JD |
3026 | goto end; |
3027 | } | |
3028 | ||
5312a3ed JG |
3029 | assert(ret > 0); |
3030 | assert(ret <= state->left_to_receive); | |
3031 | ||
3032 | state->left_to_receive -= ret; | |
3033 | state->received += ret; | |
3034 | ||
3035 | if (state->left_to_receive > 0) { | |
3036 | /* | |
3037 | * Can't transition to the protocol's next state, wait to | |
3038 | * receive the rest of the header. | |
3039 | */ | |
3040 | DBG3("Partial reception of data connection header (received %" PRIu64 " bytes, %" PRIu64 " bytes left to receive, fd = %i)", | |
3041 | state->received, state->left_to_receive, | |
3042 | conn->sock->fd); | |
7591bab1 | 3043 | goto end; |
b8aa1682 | 3044 | } |
b8aa1682 | 3045 | |
5312a3ed JG |
3046 | /* Transition to next state: receiving the payload. */ |
3047 | conn->protocol.data.state_id = DATA_CONNECTION_STATE_RECEIVE_PAYLOAD; | |
173af62f | 3048 | |
5312a3ed JG |
3049 | memcpy(&header, state->header_reception_buffer, sizeof(header)); |
3050 | header.circuit_id = be64toh(header.circuit_id); | |
3051 | header.stream_id = be64toh(header.stream_id); | |
3052 | header.data_size = be32toh(header.data_size); | |
3053 | header.net_seq_num = be64toh(header.net_seq_num); | |
3054 | header.padding_size = be32toh(header.padding_size); | |
3055 | memcpy(&conn->protocol.data.state.receive_payload.header, &header, sizeof(header)); | |
3056 | ||
3057 | conn->protocol.data.state.receive_payload.left_to_receive = | |
3058 | header.data_size; | |
3059 | conn->protocol.data.state.receive_payload.received = 0; | |
3060 | conn->protocol.data.state.receive_payload.rotate_index = false; | |
3061 | ||
3062 | DBG("Received data connection header on fd %i: circuit_id = %" PRIu64 ", stream_id = %" PRIu64 ", data_size = %" PRIu32 ", net_seq_num = %" PRIu64 ", padding_size = %" PRIu32, | |
3063 | conn->sock->fd, header.circuit_id, | |
3064 | header.stream_id, header.data_size, | |
3065 | header.net_seq_num, header.padding_size); | |
3066 | ||
3067 | stream = stream_get_by_id(header.stream_id); | |
3068 | if (!stream) { | |
3069 | DBG("relay_process_data_receive_payload: Cannot find stream %" PRIu64, | |
3070 | header.stream_id); | |
5569b118 JG |
3071 | /* Protocol error. */ |
3072 | status = RELAY_CONNECTION_STATUS_ERROR; | |
5312a3ed JG |
3073 | goto end; |
3074 | } | |
b8aa1682 | 3075 | |
7591bab1 | 3076 | pthread_mutex_lock(&stream->lock); |
84a93c08 JG |
3077 | /* Prepare stream for the reception of a new packet. */ |
3078 | ret = stream_init_packet(stream, header.data_size, | |
3079 | &conn->protocol.data.state.receive_payload.rotate_index); | |
3080 | pthread_mutex_unlock(&stream->lock); | |
3081 | if (ret) { | |
3082 | ERR("Failed to rotate stream output file"); | |
3083 | status = RELAY_CONNECTION_STATUS_ERROR; | |
3084 | goto end_stream_unlock; | |
1c20f0e2 JD |
3085 | } |
3086 | ||
5312a3ed | 3087 | end_stream_unlock: |
5312a3ed JG |
3088 | stream_put(stream); |
3089 | end: | |
5569b118 | 3090 | return status; |
5312a3ed JG |
3091 | } |
3092 | ||
5569b118 JG |
3093 | static enum relay_connection_status relay_process_data_receive_payload( |
3094 | struct relay_connection *conn) | |
5312a3ed JG |
3095 | { |
3096 | int ret; | |
5569b118 | 3097 | enum relay_connection_status status = RELAY_CONNECTION_STATUS_OK; |
5312a3ed JG |
3098 | struct relay_stream *stream; |
3099 | struct data_connection_state_receive_payload *state = | |
3100 | &conn->protocol.data.state.receive_payload; | |
3101 | const size_t chunk_size = RECV_DATA_BUFFER_SIZE; | |
3102 | char data_buffer[chunk_size]; | |
3103 | bool partial_recv = false; | |
3104 | bool new_stream = false, close_requested = false, index_flushed = false; | |
3105 | uint64_t left_to_receive = state->left_to_receive; | |
3106 | struct relay_session *session; | |
3107 | ||
fd0f1e3e JR |
3108 | DBG3("Receiving data for stream id %" PRIu64 " seqnum %" PRIu64 ", %" PRIu64" bytes received, %" PRIu64 " bytes left to receive", |
3109 | state->header.stream_id, state->header.net_seq_num, | |
3110 | state->received, left_to_receive); | |
3111 | ||
5312a3ed JG |
3112 | stream = stream_get_by_id(state->header.stream_id); |
3113 | if (!stream) { | |
5569b118 | 3114 | /* Protocol error. */ |
fd0f1e3e | 3115 | ERR("relay_process_data_receive_payload: cannot find stream %" PRIu64, |
5312a3ed | 3116 | state->header.stream_id); |
5569b118 | 3117 | status = RELAY_CONNECTION_STATUS_ERROR; |
5312a3ed | 3118 | goto end; |
1c20f0e2 JD |
3119 | } |
3120 | ||
5312a3ed JG |
3121 | pthread_mutex_lock(&stream->lock); |
3122 | session = stream->trace->session; | |
fd0f1e3e JR |
3123 | if (!conn->session) { |
3124 | ret = connection_set_session(conn, session); | |
3125 | if (ret) { | |
3126 | status = RELAY_CONNECTION_STATUS_ERROR; | |
3127 | goto end_stream_unlock; | |
3128 | } | |
3129 | } | |
5312a3ed JG |
3130 | |
3131 | /* | |
3132 | * The size of the "chunk" received on any iteration is bounded by: | |
3133 | * - the data left to receive, | |
3134 | * - the data immediately available on the socket, | |
3135 | * - the on-stack data buffer | |
3136 | */ | |
3137 | while (left_to_receive > 0 && !partial_recv) { | |
5312a3ed | 3138 | size_t recv_size = min(left_to_receive, chunk_size); |
84a93c08 | 3139 | struct lttng_buffer_view packet_chunk; |
5312a3ed JG |
3140 | |
3141 | ret = conn->sock->ops->recvmsg(conn->sock, data_buffer, | |
3142 | recv_size, MSG_DONTWAIT); | |
3143 | if (ret < 0) { | |
5569b118 JG |
3144 | if (errno != EAGAIN && errno != EWOULDBLOCK) { |
3145 | PERROR("Socket %d error", conn->sock->fd); | |
3146 | status = RELAY_CONNECTION_STATUS_ERROR; | |
3147 | } | |
0848dba7 | 3148 | goto end_stream_unlock; |
5312a3ed JG |
3149 | } else if (ret == 0) { |
3150 | /* No more data ready to be consumed on socket. */ | |
3151 | DBG3("No more data ready for consumption on data socket of stream id %" PRIu64, | |
3152 | state->header.stream_id); | |
5569b118 | 3153 | status = RELAY_CONNECTION_STATUS_CLOSED; |
5312a3ed JG |
3154 | break; |
3155 | } else if (ret < (int) recv_size) { | |
3156 | /* | |
3157 | * All the data available on the socket has been | |
3158 | * consumed. | |
3159 | */ | |
3160 | partial_recv = true; | |
84a93c08 | 3161 | recv_size = ret; |
0848dba7 MD |
3162 | } |
3163 | ||
84a93c08 JG |
3164 | packet_chunk = lttng_buffer_view_init(data_buffer, |
3165 | 0, recv_size); | |
3166 | assert(packet_chunk.data); | |
5312a3ed | 3167 | |
84a93c08 JG |
3168 | ret = stream_write(stream, &packet_chunk, 0); |
3169 | if (ret) { | |
0848dba7 | 3170 | ERR("Relay error writing data to file"); |
5569b118 | 3171 | status = RELAY_CONNECTION_STATUS_ERROR; |
0848dba7 MD |
3172 | goto end_stream_unlock; |
3173 | } | |
3174 | ||
5312a3ed JG |
3175 | left_to_receive -= recv_size; |
3176 | state->received += recv_size; | |
3177 | state->left_to_receive = left_to_receive; | |
5312a3ed JG |
3178 | } |
3179 | ||
3180 | if (state->left_to_receive > 0) { | |
3181 | /* | |
3182 | * Did not receive all the data expected, wait for more data to | |
3183 | * become available on the socket. | |
3184 | */ | |
3185 | DBG3("Partial receive on data connection of stream id %" PRIu64 ", %" PRIu64 " bytes received, %" PRIu64 " bytes left to receive", | |
3186 | state->header.stream_id, state->received, | |
3187 | state->left_to_receive); | |
5312a3ed | 3188 | goto end_stream_unlock; |
0848dba7 | 3189 | } |
5ab7344e | 3190 | |
84a93c08 JG |
3191 | ret = stream_write(stream, NULL, state->header.padding_size); |
3192 | if (ret) { | |
5569b118 | 3193 | status = RELAY_CONNECTION_STATUS_ERROR; |
7591bab1 | 3194 | goto end_stream_unlock; |
1d4dfdef | 3195 | } |
5312a3ed | 3196 | |
01921b6d | 3197 | if (session_streams_have_index(session)) { |
84a93c08 JG |
3198 | ret = stream_update_index(stream, state->header.net_seq_num, |
3199 | state->rotate_index, &index_flushed, | |
3200 | state->header.data_size + state->header.padding_size); | |
5312a3ed | 3201 | if (ret < 0) { |
84a93c08 | 3202 | ERR("Failed to update index: stream %" PRIu64 " net_seq_num %" PRIu64 " ret %d", |
5312a3ed JG |
3203 | stream->stream_handle, |
3204 | state->header.net_seq_num, ret); | |
5569b118 | 3205 | status = RELAY_CONNECTION_STATUS_ERROR; |
5312a3ed JG |
3206 | goto end_stream_unlock; |
3207 | } | |
3208 | } | |
3209 | ||
f15741c1 | 3210 | if (stream->prev_data_seq == -1ULL) { |
c0bae11d MD |
3211 | new_stream = true; |
3212 | } | |
3213 | ||
84a93c08 JG |
3214 | ret = stream_complete_packet(stream, state->header.data_size + |
3215 | state->header.padding_size, state->header.net_seq_num, | |
3216 | index_flushed); | |
3217 | if (ret) { | |
3218 | status = RELAY_CONNECTION_STATUS_ERROR; | |
3219 | goto end_stream_unlock; | |
3220 | } | |
5312a3ed JG |
3221 | |
3222 | /* | |
3223 | * Resetting the protocol state (to RECEIVE_HEADER) will trash the | |
3224 | * contents of *state which are aliased (union) to the same location as | |
3225 | * the new state. Don't use it beyond this point. | |
3226 | */ | |
3227 | connection_reset_protocol_state(conn); | |
3228 | state = NULL; | |
173af62f | 3229 | |
7591bab1 | 3230 | end_stream_unlock: |
bda7c7b9 | 3231 | close_requested = stream->close_requested; |
7591bab1 | 3232 | pthread_mutex_unlock(&stream->lock); |
5312a3ed | 3233 | if (close_requested && left_to_receive == 0) { |
bda7c7b9 JG |
3234 | try_stream_close(stream); |
3235 | } | |
3236 | ||
c0bae11d MD |
3237 | if (new_stream) { |
3238 | pthread_mutex_lock(&session->lock); | |
3239 | uatomic_set(&session->new_streams, 1); | |
3240 | pthread_mutex_unlock(&session->lock); | |
3241 | } | |
5312a3ed | 3242 | |
7591bab1 | 3243 | stream_put(stream); |
b8aa1682 | 3244 | end: |
5569b118 | 3245 | return status; |
b8aa1682 JD |
3246 | } |
3247 | ||
5312a3ed JG |
3248 | /* |
3249 | * relay_process_data: Process the data received on the data socket | |
3250 | */ | |
5569b118 JG |
3251 | static enum relay_connection_status relay_process_data( |
3252 | struct relay_connection *conn) | |
5312a3ed | 3253 | { |
5569b118 | 3254 | enum relay_connection_status status; |
5312a3ed JG |
3255 | |
3256 | switch (conn->protocol.data.state_id) { | |
3257 | case DATA_CONNECTION_STATE_RECEIVE_HEADER: | |
5569b118 | 3258 | status = relay_process_data_receive_header(conn); |
5312a3ed JG |
3259 | break; |
3260 | case DATA_CONNECTION_STATE_RECEIVE_PAYLOAD: | |
5569b118 | 3261 | status = relay_process_data_receive_payload(conn); |
5312a3ed JG |
3262 | break; |
3263 | default: | |
3264 | ERR("Unexpected data connection communication state."); | |
3265 | abort(); | |
3266 | } | |
3267 | ||
5569b118 | 3268 | return status; |
5312a3ed JG |
3269 | } |
3270 | ||
7591bab1 | 3271 | static void cleanup_connection_pollfd(struct lttng_poll_event *events, int pollfd) |
b8aa1682 JD |
3272 | { |
3273 | int ret; | |
3274 | ||
58eb9381 | 3275 | (void) lttng_poll_del(events, pollfd); |
b8aa1682 JD |
3276 | |
3277 | ret = close(pollfd); | |
3278 | if (ret < 0) { | |
3279 | ERR("Closing pollfd %d", pollfd); | |
3280 | } | |
3281 | } | |
3282 | ||
7591bab1 MD |
3283 | static void relay_thread_close_connection(struct lttng_poll_event *events, |
3284 | int pollfd, struct relay_connection *conn) | |
9d1bbf21 | 3285 | { |
7591bab1 | 3286 | const char *type_str; |
2a174661 | 3287 | |
7591bab1 MD |
3288 | switch (conn->type) { |
3289 | case RELAY_DATA: | |
3290 | type_str = "Data"; | |
3291 | break; | |
3292 | case RELAY_CONTROL: | |
3293 | type_str = "Control"; | |
3294 | break; | |
3295 | case RELAY_VIEWER_COMMAND: | |
3296 | type_str = "Viewer Command"; | |
3297 | break; | |
3298 | case RELAY_VIEWER_NOTIFICATION: | |
3299 | type_str = "Viewer Notification"; | |
3300 | break; | |
3301 | default: | |
3302 | type_str = "Unknown"; | |
9d1bbf21 | 3303 | } |
7591bab1 MD |
3304 | cleanup_connection_pollfd(events, pollfd); |
3305 | connection_put(conn); | |
3306 | DBG("%s connection closed with %d", type_str, pollfd); | |
b8aa1682 JD |
3307 | } |
3308 | ||
3309 | /* | |
3310 | * This thread does the actual work | |
3311 | */ | |
7591bab1 | 3312 | static void *relay_thread_worker(void *data) |
b8aa1682 | 3313 | { |
beaad64c DG |
3314 | int ret, err = -1, last_seen_data_fd = -1; |
3315 | uint32_t nb_fd; | |
b8aa1682 JD |
3316 | struct lttng_poll_event events; |
3317 | struct lttng_ht *relay_connections_ht; | |
b8aa1682 | 3318 | struct lttng_ht_iter iter; |
90e7d72f | 3319 | struct relay_connection *destroy_conn = NULL; |
b8aa1682 JD |
3320 | |
3321 | DBG("[thread] Relay worker started"); | |
3322 | ||
9d1bbf21 MD |
3323 | rcu_register_thread(); |
3324 | ||
55706a7d MD |
3325 | health_register(health_relayd, HEALTH_RELAYD_TYPE_WORKER); |
3326 | ||
9b5e0863 MD |
3327 | if (testpoint(relayd_thread_worker)) { |
3328 | goto error_testpoint; | |
3329 | } | |
3330 | ||
f385ae0a MD |
3331 | health_code_update(); |
3332 | ||
b8aa1682 JD |
3333 | /* table of connections indexed on socket */ |
3334 | relay_connections_ht = lttng_ht_new(0, LTTNG_HT_TYPE_ULONG); | |
095a4ae5 MD |
3335 | if (!relay_connections_ht) { |
3336 | goto relay_connections_ht_error; | |
3337 | } | |
b8aa1682 | 3338 | |
b8aa1682 JD |
3339 | ret = create_thread_poll_set(&events, 2); |
3340 | if (ret < 0) { | |
3341 | goto error_poll_create; | |
3342 | } | |
3343 | ||
58eb9381 | 3344 | ret = lttng_poll_add(&events, relay_conn_pipe[0], LPOLLIN | LPOLLRDHUP); |
b8aa1682 JD |
3345 | if (ret < 0) { |
3346 | goto error; | |
3347 | } | |
3348 | ||
beaad64c | 3349 | restart: |
b8aa1682 | 3350 | while (1) { |
beaad64c DG |
3351 | int idx = -1, i, seen_control = 0, last_notdel_data_fd = -1; |
3352 | ||
f385ae0a MD |
3353 | health_code_update(); |
3354 | ||
b8aa1682 | 3355 | /* Infinite blocking call, waiting for transmission */ |
87c1611d | 3356 | DBG3("Relayd worker thread polling..."); |
f385ae0a | 3357 | health_poll_entry(); |
b8aa1682 | 3358 | ret = lttng_poll_wait(&events, -1); |
f385ae0a | 3359 | health_poll_exit(); |
b8aa1682 JD |
3360 | if (ret < 0) { |
3361 | /* | |
3362 | * Restart interrupted system call. | |
3363 | */ | |
3364 | if (errno == EINTR) { | |
3365 | goto restart; | |
3366 | } | |
3367 | goto error; | |
3368 | } | |
3369 | ||
0d9c5d77 DG |
3370 | nb_fd = ret; |
3371 | ||
beaad64c | 3372 | /* |
7591bab1 MD |
3373 | * Process control. The control connection is |
3374 | * prioritized so we don't starve it with high | |
3375 | * throughput tracing data on the data connection. | |
beaad64c | 3376 | */ |
b8aa1682 JD |
3377 | for (i = 0; i < nb_fd; i++) { |
3378 | /* Fetch once the poll data */ | |
beaad64c DG |
3379 | uint32_t revents = LTTNG_POLL_GETEV(&events, i); |
3380 | int pollfd = LTTNG_POLL_GETFD(&events, i); | |
b8aa1682 | 3381 | |
f385ae0a MD |
3382 | health_code_update(); |
3383 | ||
b8aa1682 JD |
3384 | /* Thread quit pipe has been closed. Killing thread. */ |
3385 | ret = check_thread_quit_pipe(pollfd, revents); | |
3386 | if (ret) { | |
095a4ae5 MD |
3387 | err = 0; |
3388 | goto exit; | |
b8aa1682 JD |
3389 | } |
3390 | ||
58eb9381 DG |
3391 | /* Inspect the relay conn pipe for new connection */ |
3392 | if (pollfd == relay_conn_pipe[0]) { | |
03e43155 | 3393 | if (revents & LPOLLIN) { |
90e7d72f JG |
3394 | struct relay_connection *conn; |
3395 | ||
58eb9381 | 3396 | ret = lttng_read(relay_conn_pipe[0], &conn, sizeof(conn)); |
b8aa1682 JD |
3397 | if (ret < 0) { |
3398 | goto error; | |
3399 | } | |
58eb9381 DG |
3400 | lttng_poll_add(&events, conn->sock->fd, |
3401 | LPOLLIN | LPOLLRDHUP); | |
7591bab1 | 3402 | connection_ht_add(relay_connections_ht, conn); |
58eb9381 | 3403 | DBG("Connection socket %d added", conn->sock->fd); |
03e43155 MD |
3404 | } else if (revents & (LPOLLERR | LPOLLHUP | LPOLLRDHUP)) { |
3405 | ERR("Relay connection pipe error"); | |
3406 | goto error; | |
3407 | } else { | |
3408 | ERR("Unexpected poll events %u for sock %d", revents, pollfd); | |
3409 | goto error; | |
b8aa1682 | 3410 | } |
58eb9381 | 3411 | } else { |
90e7d72f JG |
3412 | struct relay_connection *ctrl_conn; |
3413 | ||
7591bab1 | 3414 | ctrl_conn = connection_get_by_sock(relay_connections_ht, pollfd); |
58eb9381 | 3415 | /* If not found, there is a synchronization issue. */ |
90e7d72f | 3416 | assert(ctrl_conn); |
58eb9381 | 3417 | |
03e43155 MD |
3418 | if (ctrl_conn->type == RELAY_DATA) { |
3419 | if (revents & LPOLLIN) { | |
beaad64c DG |
3420 | /* |
3421 | * Flag the last seen data fd not deleted. It will be | |
3422 | * used as the last seen fd if any fd gets deleted in | |
3423 | * this first loop. | |
3424 | */ | |
3425 | last_notdel_data_fd = pollfd; | |
3426 | } | |
03e43155 MD |
3427 | goto put_ctrl_connection; |
3428 | } | |
3429 | assert(ctrl_conn->type == RELAY_CONTROL); | |
3430 | ||
3431 | if (revents & LPOLLIN) { | |
5569b118 JG |
3432 | enum relay_connection_status status; |
3433 | ||
3434 | status = relay_process_control(ctrl_conn); | |
3435 | if (status != RELAY_CONNECTION_STATUS_OK) { | |
fd0f1e3e JR |
3436 | /* |
3437 | * On socket error flag the session as aborted to force | |
3438 | * the cleanup of its stream otherwise it can leak | |
3439 | * during the lifetime of the relayd. | |
3440 | * | |
3441 | * This prevents situations in which streams can be | |
3442 | * left opened because an index was received, the | |
3443 | * control connection is closed, and the data | |
3444 | * connection is closed (uncleanly) before the packet's | |
3445 | * data provided. | |
3446 | * | |
3447 | * Since the control connection encountered an error, | |
3448 | * it is okay to be conservative and close the | |
3449 | * session right now as we can't rely on the protocol | |
3450 | * being respected anymore. | |
3451 | */ | |
3452 | if (status == RELAY_CONNECTION_STATUS_ERROR) { | |
3453 | session_abort(ctrl_conn->session); | |
3454 | } | |
3455 | ||
5569b118 | 3456 | /* Clear the connection on error or close. */ |
5312a3ed JG |
3457 | relay_thread_close_connection(&events, |
3458 | pollfd, | |
03e43155 | 3459 | ctrl_conn); |
03e43155 | 3460 | } |
5312a3ed | 3461 | seen_control = 1; |
03e43155 MD |
3462 | } else if (revents & (LPOLLERR | LPOLLHUP | LPOLLRDHUP)) { |
3463 | relay_thread_close_connection(&events, | |
3464 | pollfd, ctrl_conn); | |
3465 | if (last_seen_data_fd == pollfd) { | |
3466 | last_seen_data_fd = last_notdel_data_fd; | |
3467 | } | |
58eb9381 | 3468 | } else { |
03e43155 MD |
3469 | ERR("Unexpected poll events %u for control sock %d", |
3470 | revents, pollfd); | |
3471 | connection_put(ctrl_conn); | |
3472 | goto error; | |
beaad64c | 3473 | } |
03e43155 | 3474 | put_ctrl_connection: |
7591bab1 | 3475 | connection_put(ctrl_conn); |
beaad64c DG |
3476 | } |
3477 | } | |
3478 | ||
3479 | /* | |
3480 | * The last loop handled a control request, go back to poll to make | |
3481 | * sure we prioritise the control socket. | |
3482 | */ | |
3483 | if (seen_control) { | |
3484 | continue; | |
3485 | } | |
3486 | ||
3487 | if (last_seen_data_fd >= 0) { | |
3488 | for (i = 0; i < nb_fd; i++) { | |
3489 | int pollfd = LTTNG_POLL_GETFD(&events, i); | |
f385ae0a MD |
3490 | |
3491 | health_code_update(); | |
3492 | ||
beaad64c DG |
3493 | if (last_seen_data_fd == pollfd) { |
3494 | idx = i; | |
3495 | break; | |
3496 | } | |
3497 | } | |
3498 | } | |
3499 | ||
3500 | /* Process data connection. */ | |
3501 | for (i = idx + 1; i < nb_fd; i++) { | |
3502 | /* Fetch the poll data. */ | |
3503 | uint32_t revents = LTTNG_POLL_GETEV(&events, i); | |
3504 | int pollfd = LTTNG_POLL_GETFD(&events, i); | |
90e7d72f | 3505 | struct relay_connection *data_conn; |
beaad64c | 3506 | |
f385ae0a MD |
3507 | health_code_update(); |
3508 | ||
fd20dac9 MD |
3509 | if (!revents) { |
3510 | /* No activity for this FD (poll implementation). */ | |
3511 | continue; | |
3512 | } | |
3513 | ||
beaad64c | 3514 | /* Skip the command pipe. It's handled in the first loop. */ |
58eb9381 | 3515 | if (pollfd == relay_conn_pipe[0]) { |
beaad64c DG |
3516 | continue; |
3517 | } | |
3518 | ||
7591bab1 | 3519 | data_conn = connection_get_by_sock(relay_connections_ht, pollfd); |
90e7d72f | 3520 | if (!data_conn) { |
fd20dac9 | 3521 | /* Skip it. Might be removed before. */ |
fd20dac9 MD |
3522 | continue; |
3523 | } | |
03e43155 MD |
3524 | if (data_conn->type == RELAY_CONTROL) { |
3525 | goto put_data_connection; | |
3526 | } | |
3527 | assert(data_conn->type == RELAY_DATA); | |
fd20dac9 MD |
3528 | |
3529 | if (revents & LPOLLIN) { | |
5569b118 JG |
3530 | enum relay_connection_status status; |
3531 | ||
3532 | status = relay_process_data(data_conn); | |
3533 | /* Connection closed or error. */ | |
3534 | if (status != RELAY_CONNECTION_STATUS_OK) { | |
fd0f1e3e JR |
3535 | /* |
3536 | * On socket error flag the session as aborted to force | |
3537 | * the cleanup of its stream otherwise it can leak | |
3538 | * during the lifetime of the relayd. | |
3539 | * | |
3540 | * This prevents situations in which streams can be | |
3541 | * left opened because an index was received, the | |
3542 | * control connection is closed, and the data | |
3543 | * connection is closed (uncleanly) before the packet's | |
3544 | * data provided. | |
3545 | * | |
3546 | * Since the data connection encountered an error, | |
3547 | * it is okay to be conservative and close the | |
3548 | * session right now as we can't rely on the protocol | |
3549 | * being respected anymore. | |
3550 | */ | |
3551 | if (status == RELAY_CONNECTION_STATUS_ERROR) { | |
3552 | session_abort(data_conn->session); | |
3553 | } | |
7591bab1 | 3554 | relay_thread_close_connection(&events, pollfd, |
03e43155 | 3555 | data_conn); |
fd20dac9 MD |
3556 | /* |
3557 | * Every goto restart call sets the last seen fd where | |
3558 | * here we don't really care since we gracefully | |
3559 | * continue the loop after the connection is deleted. | |
3560 | */ | |
3561 | } else { | |
3562 | /* Keep last seen port. */ | |
3563 | last_seen_data_fd = pollfd; | |
7591bab1 | 3564 | connection_put(data_conn); |
fd20dac9 | 3565 | goto restart; |
b8aa1682 | 3566 | } |
03e43155 MD |
3567 | } else if (revents & (LPOLLERR | LPOLLHUP | LPOLLRDHUP)) { |
3568 | relay_thread_close_connection(&events, pollfd, | |
3569 | data_conn); | |
3570 | } else { | |
3571 | ERR("Unknown poll events %u for data sock %d", | |
3572 | revents, pollfd); | |
b8aa1682 | 3573 | } |
03e43155 | 3574 | put_data_connection: |
7591bab1 | 3575 | connection_put(data_conn); |
b8aa1682 | 3576 | } |
beaad64c | 3577 | last_seen_data_fd = -1; |
b8aa1682 JD |
3578 | } |
3579 | ||
f385ae0a MD |
3580 | /* Normal exit, no error */ |
3581 | ret = 0; | |
3582 | ||
095a4ae5 | 3583 | exit: |
b8aa1682 | 3584 | error: |
71efa8ef | 3585 | /* Cleanup remaining connection object. */ |
9d1bbf21 | 3586 | rcu_read_lock(); |
90e7d72f JG |
3587 | cds_lfht_for_each_entry(relay_connections_ht->ht, &iter.iter, |
3588 | destroy_conn, | |
58eb9381 | 3589 | sock_n.node) { |
f385ae0a | 3590 | health_code_update(); |
98ba050e | 3591 | |
fd0f1e3e | 3592 | session_abort(destroy_conn->session); |
98ba050e | 3593 | |
7591bab1 MD |
3594 | /* |
3595 | * No need to grab another ref, because we own | |
3596 | * destroy_conn. | |
3597 | */ | |
3598 | relay_thread_close_connection(&events, destroy_conn->sock->fd, | |
3599 | destroy_conn); | |
b8aa1682 | 3600 | } |
94d49140 | 3601 | rcu_read_unlock(); |
7591bab1 MD |
3602 | |
3603 | lttng_poll_clean(&events); | |
7d2f7452 | 3604 | error_poll_create: |
b8aa1682 | 3605 | lttng_ht_destroy(relay_connections_ht); |
095a4ae5 | 3606 | relay_connections_ht_error: |
58eb9381 DG |
3607 | /* Close relay conn pipes */ |
3608 | utils_close_pipe(relay_conn_pipe); | |
095a4ae5 MD |
3609 | if (err) { |
3610 | DBG("Thread exited with error"); | |
3611 | } | |
b8aa1682 | 3612 | DBG("Worker thread cleanup complete"); |
9b5e0863 | 3613 | error_testpoint: |
f385ae0a MD |
3614 | if (err) { |
3615 | health_error(); | |
3616 | ERR("Health error occurred in %s", __func__); | |
3617 | } | |
3618 | health_unregister(health_relayd); | |
9d1bbf21 | 3619 | rcu_unregister_thread(); |
b4aacfdc | 3620 | lttng_relay_stop_threads(); |
b8aa1682 JD |
3621 | return NULL; |
3622 | } | |
3623 | ||
3624 | /* | |
3625 | * Create the relay command pipe to wake thread_manage_apps. | |
3626 | * Closed in cleanup(). | |
3627 | */ | |
58eb9381 | 3628 | static int create_relay_conn_pipe(void) |
b8aa1682 | 3629 | { |
a02de639 | 3630 | int ret; |
b8aa1682 | 3631 | |
58eb9381 | 3632 | ret = utils_create_pipe_cloexec(relay_conn_pipe); |
b8aa1682 | 3633 | |
b8aa1682 JD |
3634 | return ret; |
3635 | } | |
3636 | ||
3637 | /* | |
3638 | * main | |
3639 | */ | |
3640 | int main(int argc, char **argv) | |
3641 | { | |
178a0557 | 3642 | int ret = 0, retval = 0; |
b8aa1682 JD |
3643 | void *status; |
3644 | ||
b8aa1682 JD |
3645 | /* Parse arguments */ |
3646 | progname = argv[0]; | |
178a0557 MD |
3647 | if (set_options(argc, argv)) { |
3648 | retval = -1; | |
3649 | goto exit_options; | |
b8aa1682 JD |
3650 | } |
3651 | ||
178a0557 MD |
3652 | if (set_signal_handler()) { |
3653 | retval = -1; | |
3654 | goto exit_options; | |
b8aa1682 JD |
3655 | } |
3656 | ||
4d513a50 DG |
3657 | /* Try to create directory if -o, --output is specified. */ |
3658 | if (opt_output_path) { | |
994fa64f DG |
3659 | if (*opt_output_path != '/') { |
3660 | ERR("Please specify an absolute path for -o, --output PATH"); | |
178a0557 MD |
3661 | retval = -1; |
3662 | goto exit_options; | |
994fa64f DG |
3663 | } |
3664 | ||
d77dded2 JG |
3665 | ret = utils_mkdir_recursive(opt_output_path, S_IRWXU | S_IRWXG, |
3666 | -1, -1); | |
4d513a50 DG |
3667 | if (ret < 0) { |
3668 | ERR("Unable to create %s", opt_output_path); | |
178a0557 MD |
3669 | retval = -1; |
3670 | goto exit_options; | |
4d513a50 DG |
3671 | } |
3672 | } | |
3673 | ||
b8aa1682 | 3674 | /* Daemonize */ |
b5218ffb | 3675 | if (opt_daemon || opt_background) { |
3fd27398 MD |
3676 | int i; |
3677 | ||
3678 | ret = lttng_daemonize(&child_ppid, &recv_child_signal, | |
3679 | !opt_background); | |
b8aa1682 | 3680 | if (ret < 0) { |
178a0557 MD |
3681 | retval = -1; |
3682 | goto exit_options; | |
b8aa1682 | 3683 | } |
3fd27398 MD |
3684 | |
3685 | /* | |
3686 | * We are in the child. Make sure all other file | |
3687 | * descriptors are closed, in case we are called with | |
3688 | * more opened file descriptors than the standard ones. | |
3689 | */ | |
3690 | for (i = 3; i < sysconf(_SC_OPEN_MAX); i++) { | |
3691 | (void) close(i); | |
3692 | } | |
3693 | } | |
3694 | ||
1f916b5d JG |
3695 | sessiond_trace_chunk_registry = sessiond_trace_chunk_registry_create(); |
3696 | if (!sessiond_trace_chunk_registry) { | |
3697 | ERR("Failed to initialize session daemon trace chunk registry"); | |
3698 | retval = -1; | |
3699 | goto exit_sessiond_trace_chunk_registry; | |
3700 | } | |
3701 | ||
178a0557 MD |
3702 | /* Initialize thread health monitoring */ |
3703 | health_relayd = health_app_create(NR_HEALTH_RELAYD_TYPES); | |
3704 | if (!health_relayd) { | |
3705 | PERROR("health_app_create error"); | |
3706 | retval = -1; | |
3707 | goto exit_health_app_create; | |
3708 | } | |
3709 | ||
3fd27398 | 3710 | /* Create thread quit pipe */ |
178a0557 MD |
3711 | if (init_thread_quit_pipe()) { |
3712 | retval = -1; | |
3713 | goto exit_init_data; | |
b8aa1682 JD |
3714 | } |
3715 | ||
b8aa1682 | 3716 | /* Setup the thread apps communication pipe. */ |
178a0557 MD |
3717 | if (create_relay_conn_pipe()) { |
3718 | retval = -1; | |
3719 | goto exit_init_data; | |
b8aa1682 JD |
3720 | } |
3721 | ||
3722 | /* Init relay command queue. */ | |
8bdee6e2 | 3723 | cds_wfcq_init(&relay_conn_queue.head, &relay_conn_queue.tail); |
b8aa1682 | 3724 | |
554831e7 MD |
3725 | /* Initialize communication library */ |
3726 | lttcomm_init(); | |
87e45c13 | 3727 | lttcomm_inet_init(); |
554831e7 | 3728 | |
d3e2ba59 | 3729 | /* tables of sessions indexed by session ID */ |
7591bab1 MD |
3730 | sessions_ht = lttng_ht_new(0, LTTNG_HT_TYPE_U64); |
3731 | if (!sessions_ht) { | |
178a0557 MD |
3732 | retval = -1; |
3733 | goto exit_init_data; | |
d3e2ba59 JD |
3734 | } |
3735 | ||
3736 | /* tables of streams indexed by stream ID */ | |
2a174661 | 3737 | relay_streams_ht = lttng_ht_new(0, LTTNG_HT_TYPE_U64); |
d3e2ba59 | 3738 | if (!relay_streams_ht) { |
178a0557 MD |
3739 | retval = -1; |
3740 | goto exit_init_data; | |
d3e2ba59 JD |
3741 | } |
3742 | ||
3743 | /* tables of streams indexed by stream ID */ | |
92c6ca54 DG |
3744 | viewer_streams_ht = lttng_ht_new(0, LTTNG_HT_TYPE_U64); |
3745 | if (!viewer_streams_ht) { | |
178a0557 MD |
3746 | retval = -1; |
3747 | goto exit_init_data; | |
55706a7d MD |
3748 | } |
3749 | ||
65931c8b | 3750 | ret = utils_create_pipe(health_quit_pipe); |
178a0557 MD |
3751 | if (ret) { |
3752 | retval = -1; | |
3753 | goto exit_health_quit_pipe; | |
65931c8b MD |
3754 | } |
3755 | ||
3756 | /* Create thread to manage the client socket */ | |
1a1a34b4 | 3757 | ret = pthread_create(&health_thread, default_pthread_attr(), |
65931c8b | 3758 | thread_manage_health, (void *) NULL); |
178a0557 MD |
3759 | if (ret) { |
3760 | errno = ret; | |
65931c8b | 3761 | PERROR("pthread_create health"); |
178a0557 MD |
3762 | retval = -1; |
3763 | goto exit_health_thread; | |
65931c8b MD |
3764 | } |
3765 | ||
b8aa1682 | 3766 | /* Setup the dispatcher thread */ |
1a1a34b4 | 3767 | ret = pthread_create(&dispatcher_thread, default_pthread_attr(), |
b8aa1682 | 3768 | relay_thread_dispatcher, (void *) NULL); |
178a0557 MD |
3769 | if (ret) { |
3770 | errno = ret; | |
b8aa1682 | 3771 | PERROR("pthread_create dispatcher"); |
178a0557 MD |
3772 | retval = -1; |
3773 | goto exit_dispatcher_thread; | |
b8aa1682 JD |
3774 | } |
3775 | ||
3776 | /* Setup the worker thread */ | |
1a1a34b4 | 3777 | ret = pthread_create(&worker_thread, default_pthread_attr(), |
7591bab1 | 3778 | relay_thread_worker, NULL); |
178a0557 MD |
3779 | if (ret) { |
3780 | errno = ret; | |
b8aa1682 | 3781 | PERROR("pthread_create worker"); |
178a0557 MD |
3782 | retval = -1; |
3783 | goto exit_worker_thread; | |
b8aa1682 JD |
3784 | } |
3785 | ||
3786 | /* Setup the listener thread */ | |
1a1a34b4 | 3787 | ret = pthread_create(&listener_thread, default_pthread_attr(), |
b8aa1682 | 3788 | relay_thread_listener, (void *) NULL); |
178a0557 MD |
3789 | if (ret) { |
3790 | errno = ret; | |
b8aa1682 | 3791 | PERROR("pthread_create listener"); |
178a0557 MD |
3792 | retval = -1; |
3793 | goto exit_listener_thread; | |
b8aa1682 JD |
3794 | } |
3795 | ||
7591bab1 | 3796 | ret = relayd_live_create(live_uri); |
178a0557 | 3797 | if (ret) { |
d3e2ba59 | 3798 | ERR("Starting live viewer threads"); |
178a0557 | 3799 | retval = -1; |
50138f51 | 3800 | goto exit_live; |
d3e2ba59 JD |
3801 | } |
3802 | ||
178a0557 MD |
3803 | /* |
3804 | * This is where we start awaiting program completion (e.g. through | |
3805 | * signal that asks threads to teardown). | |
3806 | */ | |
3807 | ||
3808 | ret = relayd_live_join(); | |
3809 | if (ret) { | |
3810 | retval = -1; | |
3811 | } | |
50138f51 | 3812 | exit_live: |
178a0557 | 3813 | |
b8aa1682 | 3814 | ret = pthread_join(listener_thread, &status); |
178a0557 MD |
3815 | if (ret) { |
3816 | errno = ret; | |
3817 | PERROR("pthread_join listener_thread"); | |
3818 | retval = -1; | |
b8aa1682 JD |
3819 | } |
3820 | ||
178a0557 | 3821 | exit_listener_thread: |
b8aa1682 | 3822 | ret = pthread_join(worker_thread, &status); |
178a0557 MD |
3823 | if (ret) { |
3824 | errno = ret; | |
3825 | PERROR("pthread_join worker_thread"); | |
3826 | retval = -1; | |
b8aa1682 JD |
3827 | } |
3828 | ||
178a0557 | 3829 | exit_worker_thread: |
b8aa1682 | 3830 | ret = pthread_join(dispatcher_thread, &status); |
178a0557 MD |
3831 | if (ret) { |
3832 | errno = ret; | |
3833 | PERROR("pthread_join dispatcher_thread"); | |
3834 | retval = -1; | |
b8aa1682 | 3835 | } |
178a0557 | 3836 | exit_dispatcher_thread: |
42415026 | 3837 | |
65931c8b | 3838 | ret = pthread_join(health_thread, &status); |
178a0557 MD |
3839 | if (ret) { |
3840 | errno = ret; | |
3841 | PERROR("pthread_join health_thread"); | |
3842 | retval = -1; | |
65931c8b | 3843 | } |
178a0557 | 3844 | exit_health_thread: |
65931c8b | 3845 | |
65931c8b | 3846 | utils_close_pipe(health_quit_pipe); |
178a0557 | 3847 | exit_health_quit_pipe: |
65931c8b | 3848 | |
178a0557 | 3849 | exit_init_data: |
55706a7d | 3850 | health_app_destroy(health_relayd); |
1f916b5d | 3851 | sessiond_trace_chunk_registry_destroy(sessiond_trace_chunk_registry); |
55706a7d | 3852 | exit_health_app_create: |
1f916b5d | 3853 | exit_sessiond_trace_chunk_registry: |
178a0557 | 3854 | exit_options: |
4d62fbf8 MD |
3855 | /* |
3856 | * Wait for all pending call_rcu work to complete before tearing | |
3857 | * down data structures. call_rcu worker may be trying to | |
3858 | * perform lookups in those structures. | |
3859 | */ | |
3860 | rcu_barrier(); | |
7591bab1 MD |
3861 | relayd_cleanup(); |
3862 | ||
3863 | /* Ensure all prior call_rcu are done. */ | |
3864 | rcu_barrier(); | |
d3e2ba59 | 3865 | |
178a0557 | 3866 | if (!retval) { |
b8aa1682 | 3867 | exit(EXIT_SUCCESS); |
178a0557 MD |
3868 | } else { |
3869 | exit(EXIT_FAILURE); | |
b8aa1682 | 3870 | } |
b8aa1682 | 3871 | } |