Open stdin and stdout to something if they don't arrive open, or bad things happen.

This commit is contained in:
Ian Gulliver
2016-03-09 18:28:00 -08:00
parent 8316739941
commit 689bfc7939
3 changed files with 17 additions and 1 deletions

View File

@@ -33,6 +33,8 @@ static void adsbus_opts_add() {
}
int main(int argc, char *argv[]) {
stdinout_preinit();
adsbus_opts_add();
opts_init(argc, argv);

View File

@@ -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);

View File

@@ -1,5 +1,6 @@
#pragma once
void stdinout_preinit(void);
void stdinout_opts_add(void);
void stdinout_init(void);
void stdinout_cleanup(void);