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