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