Move stdin/out handling entirely into stdinout.c
This commit is contained in:
@@ -1,9 +1,4 @@
|
|||||||
#include <assert.h>
|
|
||||||
#include <fcntl.h>
|
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
#include <sys/types.h>
|
|
||||||
#include <sys/stat.h>
|
|
||||||
#include <unistd.h>
|
|
||||||
|
|
||||||
#include "beast.h"
|
#include "beast.h"
|
||||||
#include "exec.h"
|
#include "exec.h"
|
||||||
@@ -26,12 +21,6 @@
|
|||||||
#include "stdinout.h"
|
#include "stdinout.h"
|
||||||
#include "wakeup.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() {
|
static void adsbus_opts_add() {
|
||||||
// This order controls the order in --help, but nothing else.
|
// This order controls the order in --help, but nothing else.
|
||||||
server_opts_add();
|
server_opts_add();
|
||||||
@@ -74,9 +63,6 @@ int main(int argc, char *argv[]) {
|
|||||||
file_init();
|
file_init();
|
||||||
stdinout_init();
|
stdinout_init();
|
||||||
|
|
||||||
reopen(STDIN_FILENO, "/dev/null", O_RDONLY);
|
|
||||||
reopen(STDOUT_FILENO, "/dev/full", O_WRONLY);
|
|
||||||
|
|
||||||
peer_loop();
|
peer_loop();
|
||||||
|
|
||||||
resolve_cleanup();
|
resolve_cleanup();
|
||||||
@@ -101,8 +87,7 @@ int main(int argc, char *argv[]) {
|
|||||||
|
|
||||||
log_cleanup();
|
log_cleanup();
|
||||||
|
|
||||||
assert(!close(STDIN_FILENO));
|
stdinout_cleanup();
|
||||||
assert(!close(STDOUT_FILENO));
|
|
||||||
|
|
||||||
return EXIT_SUCCESS;
|
return EXIT_SUCCESS;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -7,7 +7,6 @@
|
|||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
#include <sys/signalfd.h>
|
#include <sys/signalfd.h>
|
||||||
#include <sys/socket.h>
|
|
||||||
#include <sys/stat.h>
|
#include <sys/stat.h>
|
||||||
#include <sys/types.h>
|
#include <sys/types.h>
|
||||||
#include <time.h>
|
#include <time.h>
|
||||||
|
|||||||
@@ -2,12 +2,7 @@
|
|||||||
#include <errno.h>
|
#include <errno.h>
|
||||||
#include <signal.h>
|
#include <signal.h>
|
||||||
#include <stdbool.h>
|
#include <stdbool.h>
|
||||||
#include <stdlib.h>
|
|
||||||
#include <string.h>
|
|
||||||
#include <sys/signalfd.h>
|
#include <sys/signalfd.h>
|
||||||
#include <sys/socket.h>
|
|
||||||
#include <sys/stat.h>
|
|
||||||
#include <sys/types.h>
|
|
||||||
#include <unistd.h>
|
#include <unistd.h>
|
||||||
|
|
||||||
#include "log.h"
|
#include "log.h"
|
||||||
|
|||||||
@@ -1,4 +1,3 @@
|
|||||||
#include <assert.h>
|
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
#include <sys/capability.h>
|
#include <sys/capability.h>
|
||||||
|
|||||||
@@ -1,5 +1,7 @@
|
|||||||
#include <assert.h>
|
#include <assert.h>
|
||||||
#include <fcntl.h>
|
#include <fcntl.h>
|
||||||
|
#include <sys/stat.h>
|
||||||
|
#include <sys/types.h>
|
||||||
#include <unistd.h>
|
#include <unistd.h>
|
||||||
|
|
||||||
#include "flow.h"
|
#include "flow.h"
|
||||||
@@ -11,6 +13,12 @@
|
|||||||
|
|
||||||
static opts_group stdinout_opts;
|
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) {
|
static bool stdinout_stdin(const char __attribute__((unused)) *arg) {
|
||||||
int fd = fcntl(STDIN_FILENO, F_DUPFD_CLOEXEC, 0);
|
int fd = fcntl(STDIN_FILENO, F_DUPFD_CLOEXEC, 0);
|
||||||
assert(fd >= 0);
|
assert(fd >= 0);
|
||||||
@@ -34,4 +42,11 @@ void stdinout_opts_add() {
|
|||||||
|
|
||||||
void stdinout_init() {
|
void stdinout_init() {
|
||||||
opts_call(stdinout_opts);
|
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));
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -2,3 +2,4 @@
|
|||||||
|
|
||||||
void stdinout_opts_add(void);
|
void stdinout_opts_add(void);
|
||||||
void stdinout_init(void);
|
void stdinout_init(void);
|
||||||
|
void stdinout_cleanup(void);
|
||||||
|
|||||||
Reference in New Issue
Block a user