From 6c0f5fc6125a478fd50074880ed7fcac9fd9063e Mon Sep 17 00:00:00 2001 From: Max Kellermann Date: Tue, 30 Dec 2008 16:28:13 +0100 Subject: listen: moved redirect_stdin() to daemon.c redirect_stdin() is a daemonization function, and disconnecting from the standard input is always a good idea for MPD. --- src/daemon.c | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) (limited to 'src/daemon.c') diff --git a/src/daemon.c b/src/daemon.c index 09cd5721a..569494908 100644 --- a/src/daemon.c +++ b/src/daemon.c @@ -24,6 +24,32 @@ #include #include #include +#include +#include +#include + +void +daemonize_close_stdin(void) +{ + int fd, st; + struct stat ss; + + if ((st = fstat(STDIN_FILENO, &ss)) < 0) { + if ((fd = open("/dev/null", O_RDONLY) > 0)) { + g_debug("stdin closed, and could not open /dev/null " + "as fd=0, some external library bugs " + "may be exposed..."); + close(fd); + } + return; + } + if (!isatty(STDIN_FILENO)) + return; + if ((fd = open("/dev/null", O_RDONLY)) < 0) + g_error("failed to open /dev/null %s", strerror(errno)); + if (dup2(fd, STDIN_FILENO) < 0) + g_error("dup2 stdin: %s", strerror(errno)); +} void daemonize(Options *options) -- cgit v1.2.3