diff --git a/adsbus/adsbus.c b/adsbus/adsbus.c index a54742d..0f14065 100644 --- a/adsbus/adsbus.c +++ b/adsbus/adsbus.c @@ -1,8 +1,11 @@ #include +#include #include #include #include #include +#include +#include #include #include "beast.h" @@ -166,6 +169,12 @@ static bool parse_opts(int argc, char *argv[]) { return true; } +static void reopen(int fd, char *path, int flags) { + // Presumes that all fds < fd are open + assert(!close(fd)); + assert(open(path, flags | O_CLOEXEC) == fd); +} + int main(int argc, char *argv[]) { hex_init(); rand_init(); @@ -185,8 +194,8 @@ int main(int argc, char *argv[]) { peer_shutdown(0); } - assert(!close(0)); - assert(!close(1)); + reopen(STDIN_FILENO, "/dev/null", O_RDONLY); + reopen(STDOUT_FILENO, "/dev/full", O_WRONLY); peer_loop(); @@ -208,7 +217,9 @@ int main(int argc, char *argv[]) { peer_cleanup(); - assert(!close(2)); + assert(!close(STDIN_FILENO)); + assert(!close(STDOUT_FILENO)); + assert(!close(STDERR_FILENO)); return EXIT_SUCCESS; } diff --git a/adsbus/opts.c b/adsbus/opts.c index b577a46..0b39e88 100644 --- a/adsbus/opts.c +++ b/adsbus/opts.c @@ -146,7 +146,7 @@ bool opts_add_exec_send_receive(char *arg) { } bool opts_add_stdin(char __attribute__((unused)) *arg) { - int fd = fcntl(0, F_DUPFD_CLOEXEC, 0); + int fd = fcntl(STDIN_FILENO, F_DUPFD_CLOEXEC, 0); assert(fd >= 0); return flow_new_send_hello(fd, receive_flow, NULL, NULL); } @@ -156,7 +156,7 @@ bool opts_add_stdout(char *arg) { if (!serializer) { return false; } - int fd = fcntl(1, F_DUPFD_CLOEXEC, 0); + int fd = fcntl(STDOUT_FILENO, F_DUPFD_CLOEXEC, 0); assert(fd >= 0); return flow_new_send_hello(fd, send_flow, serializer, NULL); }