Redirect stdin *before* we establish a listen socket
This way we'll avoid listening on fd=0 and have a better chance of having fd=0 as /dev/null git-svn-id: https://svn.musicpd.org/mpd/trunk@6852 09075e82-0dd4-0310-85a5-a0d7c8717e4f
This commit is contained in:
parent
7992ff37d0
commit
ac58dce7df
29
src/listen.c
29
src/listen.c
@ -50,6 +50,34 @@ static int *listenSockets;
|
|||||||
static int numberOfListenSockets;
|
static int numberOfListenSockets;
|
||||||
int boundPort;
|
int boundPort;
|
||||||
|
|
||||||
|
/*
|
||||||
|
* redirect stdin to /dev/null to work around a libao bug
|
||||||
|
* there are likely other bugs in other libraries (and even our code!)
|
||||||
|
* that check for fd > 0, so it's easiest to just keep
|
||||||
|
* fd = 0 == /dev/null for now...
|
||||||
|
*/
|
||||||
|
static void redirect_stdin(void)
|
||||||
|
{
|
||||||
|
int fd, st;
|
||||||
|
struct stat ss;
|
||||||
|
|
||||||
|
if ((st = fstat(STDIN_FILENO, &ss)) < 0) {
|
||||||
|
if ((fd = open("/dev/null", O_RDONLY) > 0)) {
|
||||||
|
DEBUG("stdin closed, and could not open /dev/null "
|
||||||
|
"as fd=0, some external library bugs "
|
||||||
|
"may be exposed...\n");
|
||||||
|
close(fd);
|
||||||
|
}
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
if (!isatty(STDIN_FILENO))
|
||||||
|
return;
|
||||||
|
if ((fd = open("/dev/null", O_RDONLY)) < 0)
|
||||||
|
FATAL("failed to open /dev/null %s\n", strerror(errno));
|
||||||
|
if (dup2(fd, STDIN_FILENO) < 0)
|
||||||
|
FATAL("dup2 stdin: %s\n", strerror(errno));
|
||||||
|
}
|
||||||
|
|
||||||
static int establishListen(unsigned int port,
|
static int establishListen(unsigned int port,
|
||||||
struct sockaddr *addrp, socklen_t addrlen)
|
struct sockaddr *addrp, socklen_t addrlen)
|
||||||
{
|
{
|
||||||
@ -196,6 +224,7 @@ void listenOnPort(void)
|
|||||||
|
|
||||||
boundPort = port;
|
boundPort = port;
|
||||||
|
|
||||||
|
redirect_stdin();
|
||||||
do {
|
do {
|
||||||
parseListenConfigParam(port, param);
|
parseListenConfigParam(port, param);
|
||||||
} while ((param = getNextConfigParam(CONF_BIND_TO_ADDRESS, param)));
|
} while ((param = getNextConfigParam(CONF_BIND_TO_ADDRESS, param)));
|
||||||
|
29
src/log.c
29
src/log.c
@ -39,34 +39,6 @@ static int err_fd = -1;
|
|||||||
static const char *out_filename;
|
static const char *out_filename;
|
||||||
static const char *err_filename;
|
static const char *err_filename;
|
||||||
|
|
||||||
/*
|
|
||||||
* redirect stdin to /dev/null to work around a libao bug
|
|
||||||
* there are likely other bugs in other libraries (and even our code!)
|
|
||||||
* that check for fd > 0, so it's easiest to just keep
|
|
||||||
* fd = 0 == /dev/null for now...
|
|
||||||
*/
|
|
||||||
static void redirect_stdin(void)
|
|
||||||
{
|
|
||||||
int fd, st;
|
|
||||||
struct stat ss;
|
|
||||||
|
|
||||||
if ((st = fstat(STDIN_FILENO, &ss)) < 0) {
|
|
||||||
if ((fd = open("/dev/null", O_RDONLY) > 0)) {
|
|
||||||
DEBUG("stdin closed, and could not open /dev/null "
|
|
||||||
"as fd=0, some external library bugs "
|
|
||||||
"may be exposed...\n");
|
|
||||||
close(fd);
|
|
||||||
}
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
if (!isatty(STDIN_FILENO))
|
|
||||||
return;
|
|
||||||
if ((fd = open("/dev/null", O_RDONLY)) < 0)
|
|
||||||
FATAL("failed to open /dev/null %s\n", strerror(errno));
|
|
||||||
if (dup2(fd, STDIN_FILENO) < 0)
|
|
||||||
FATAL("dup2 stdin: %s\n", strerror(errno));
|
|
||||||
}
|
|
||||||
|
|
||||||
static void redirect_logs(void)
|
static void redirect_logs(void)
|
||||||
{
|
{
|
||||||
assert(out_fd > 0);
|
assert(out_fd > 0);
|
||||||
@ -197,7 +169,6 @@ void setup_log_output(const int use_stdout)
|
|||||||
stdout_mode = 0;
|
stdout_mode = 0;
|
||||||
flushWarningLog();
|
flushWarningLog();
|
||||||
}
|
}
|
||||||
redirect_stdin();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#define log_func(func,level,fp) \
|
#define log_func(func,level,fp) \
|
||||||
|
Loading…
Reference in New Issue
Block a user