Move stdin/out handling entirely into stdinout.c

This commit is contained in:
Ian Gulliver
2016-03-09 18:07:09 -08:00
parent 8898142c97
commit 8316739941
6 changed files with 17 additions and 23 deletions

View File

@@ -1,5 +1,7 @@
#include <assert.h>
#include <fcntl.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <unistd.h>
#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));
}