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