Open stdin and stdout to something if they don't arrive open, or bad things happen.
This commit is contained in:
@@ -13,10 +13,14 @@
|
||||
|
||||
static opts_group stdinout_opts;
|
||||
|
||||
static void stdinout_open(int fd, char *path, int flags) {
|
||||
assert(open(path, flags | O_CLOEXEC | O_NOCTTY) == fd);
|
||||
}
|
||||
|
||||
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);
|
||||
stdinout_open(fd, path, flags);
|
||||
}
|
||||
|
||||
static bool stdinout_stdin(const char __attribute__((unused)) *arg) {
|
||||
@@ -35,6 +39,15 @@ static bool stdinout_stdout(const char *arg) {
|
||||
return flow_new_send_hello(fd, send_flow, serializer, NULL);
|
||||
}
|
||||
|
||||
void stdinout_preinit() {
|
||||
if (fcntl(STDIN_FILENO, F_GETFD) == -1) {
|
||||
stdinout_open(STDIN_FILENO, "/dev/null", O_RDONLY);
|
||||
}
|
||||
if (fcntl(STDOUT_FILENO, F_GETFD) == -1) {
|
||||
stdinout_open(STDOUT_FILENO, "/dev/full", O_WRONLY);
|
||||
}
|
||||
}
|
||||
|
||||
void stdinout_opts_add() {
|
||||
opts_add("stdin", NULL, stdinout_stdin, stdinout_opts);
|
||||
opts_add("stdout", "FORMAT", stdinout_stdout, stdinout_opts);
|
||||
|
||||
Reference in New Issue
Block a user