From 8316739941524649bc87529ef6003a84f40ac968 Mon Sep 17 00:00:00 2001 From: Ian Gulliver Date: Wed, 9 Mar 2016 18:07:09 -0800 Subject: [PATCH] Move stdin/out handling entirely into stdinout.c --- adsbus/adsbus.c | 17 +---------------- adsbus/log.c | 1 - adsbus/peer.c | 5 ----- adsbus/server.c | 1 - adsbus/stdinout.c | 15 +++++++++++++++ adsbus/stdinout.h | 1 + 6 files changed, 17 insertions(+), 23 deletions(-) diff --git a/adsbus/adsbus.c b/adsbus/adsbus.c index c1bdded..7c68fc2 100644 --- a/adsbus/adsbus.c +++ b/adsbus/adsbus.c @@ -1,9 +1,4 @@ -#include -#include #include -#include -#include -#include #include "beast.h" #include "exec.h" @@ -26,12 +21,6 @@ #include "stdinout.h" #include "wakeup.h" -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 | O_NOCTTY) == fd); -} - static void adsbus_opts_add() { // This order controls the order in --help, but nothing else. server_opts_add(); @@ -74,9 +63,6 @@ int main(int argc, char *argv[]) { file_init(); stdinout_init(); - reopen(STDIN_FILENO, "/dev/null", O_RDONLY); - reopen(STDOUT_FILENO, "/dev/full", O_WRONLY); - peer_loop(); resolve_cleanup(); @@ -101,8 +87,7 @@ int main(int argc, char *argv[]) { log_cleanup(); - assert(!close(STDIN_FILENO)); - assert(!close(STDOUT_FILENO)); + stdinout_cleanup(); return EXIT_SUCCESS; } diff --git a/adsbus/log.c b/adsbus/log.c index 9fabea2..aa1554e 100644 --- a/adsbus/log.c +++ b/adsbus/log.c @@ -7,7 +7,6 @@ #include #include #include -#include #include #include #include diff --git a/adsbus/peer.c b/adsbus/peer.c index 067f533..ee31a0c 100644 --- a/adsbus/peer.c +++ b/adsbus/peer.c @@ -2,12 +2,7 @@ #include #include #include -#include -#include #include -#include -#include -#include #include #include "log.h" diff --git a/adsbus/server.c b/adsbus/server.c index c5d95b7..e84c0b5 100644 --- a/adsbus/server.c +++ b/adsbus/server.c @@ -1,4 +1,3 @@ -#include #include #include #include diff --git a/adsbus/stdinout.c b/adsbus/stdinout.c index c1c7f38..3a9e88e 100644 --- a/adsbus/stdinout.c +++ b/adsbus/stdinout.c @@ -1,5 +1,7 @@ #include #include +#include +#include #include #include "flow.h" @@ -11,6 +13,12 @@ static opts_group stdinout_opts; +static void stdinout_reopen(int fd, char *path, int flags) { + // Presumes that all fds < fd are open + assert(!close(fd)); + assert(open(path, flags | O_CLOEXEC | O_NOCTTY) == fd); +} + static bool stdinout_stdin(const char __attribute__((unused)) *arg) { int fd = fcntl(STDIN_FILENO, F_DUPFD_CLOEXEC, 0); assert(fd >= 0); @@ -34,4 +42,11 @@ void stdinout_opts_add() { void stdinout_init() { opts_call(stdinout_opts); + stdinout_reopen(STDIN_FILENO, "/dev/null", O_RDONLY); + stdinout_reopen(STDOUT_FILENO, "/dev/full", O_WRONLY); +} + +void stdinout_cleanup() { + assert(!close(STDIN_FILENO)); + assert(!close(STDOUT_FILENO)); } diff --git a/adsbus/stdinout.h b/adsbus/stdinout.h index e1291f4..b741cac 100644 --- a/adsbus/stdinout.h +++ b/adsbus/stdinout.h @@ -2,3 +2,4 @@ void stdinout_opts_add(void); void stdinout_init(void); +void stdinout_cleanup(void);