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.
This commit is contained in:
26
src/daemon.c
26
src/daemon.c
@@ -24,6 +24,32 @@
|
|||||||
#include <unistd.h>
|
#include <unistd.h>
|
||||||
#include <errno.h>
|
#include <errno.h>
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
|
#include <sys/types.h>
|
||||||
|
#include <sys/stat.h>
|
||||||
|
#include <fcntl.h>
|
||||||
|
|
||||||
|
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
|
void
|
||||||
daemonize(Options *options)
|
daemonize(Options *options)
|
||||||
|
@@ -21,6 +21,12 @@
|
|||||||
|
|
||||||
#include "cmdline.h"
|
#include "cmdline.h"
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Close stdin (fd 0) and re-open it as /dev/null.
|
||||||
|
*/
|
||||||
|
void
|
||||||
|
daemonize_close_stdin(void);
|
||||||
|
|
||||||
void
|
void
|
||||||
daemonize(Options *options);
|
daemonize(Options *options);
|
||||||
|
|
||||||
|
29
src/listen.c
29
src/listen.c
@@ -49,34 +49,6 @@ 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)) {
|
|
||||||
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));
|
|
||||||
}
|
|
||||||
|
|
||||||
static int establishListen(int pf, const struct sockaddr *addrp,
|
static int establishListen(int pf, const struct sockaddr *addrp,
|
||||||
socklen_t addrlen)
|
socklen_t addrlen)
|
||||||
{
|
{
|
||||||
@@ -245,7 +217,6 @@ 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)));
|
||||||
|
@@ -186,6 +186,8 @@ int main(int argc, char *argv[])
|
|||||||
clock_t start;
|
clock_t start;
|
||||||
GTimer *save_state_timer;
|
GTimer *save_state_timer;
|
||||||
|
|
||||||
|
daemonize_close_stdin();
|
||||||
|
|
||||||
#ifdef HAVE_LOCALE
|
#ifdef HAVE_LOCALE
|
||||||
/* initialize locale */
|
/* initialize locale */
|
||||||
setlocale(LC_CTYPE,"");
|
setlocale(LC_CTYPE,"");
|
||||||
|
Reference in New Issue
Block a user