Fix redbean daemonization

Fixes #94
This commit is contained in:
Justine Tunney 2021-03-02 00:38:11 -08:00
parent 7e4021bf82
commit 8a6a1e25b1

View file

@ -125,6 +125,13 @@ USAGE\n\
for subsequent connections without interrupting active ones. If\n\ for subsequent connections without interrupting active ones. If\n\
SIGINT or SIGTERM is issued then a graceful shutdown is started\n\ SIGINT or SIGTERM is issued then a graceful shutdown is started\n\
but if it's issued a second time, active connections are reset.\n\ but if it's issued a second time, active connections are reset.\n\
\n\
You can have redbean run as a daemon by doing the following:\n\
\n\
redbean.com -vv -d -L redbean.log -P redbean.pid\n\
kill -HUP $(cat redbean.pid)\n\
kill -TERM $(cat redbean.pid)\n\
\n\
\n" \n"
#define HASH_LOAD_FACTOR /* 1. / */ 4 #define HASH_LOAD_FACTOR /* 1. / */ 4
@ -432,23 +439,19 @@ static void Daemonize(void) {
char ibuf[21]; char ibuf[21];
int i, fd, pid; int i, fd, pid;
for (i = 0; i < 128; ++i) close(i); for (i = 0; i < 128; ++i) close(i);
xsigaction(SIGHUP, OnHup, 0, 0, 0); if ((pid = fork()) > 0) exit(0);
CHECK_NE(-1, (pid = fork())); setsid();
if (pid > 0) exit(0); if ((pid = fork()) > 0) _exit(0);
if (pid == -1) return; umask(0);
CHECK_NE(-1, setsid());
CHECK_NE(-1, (pid = fork()));
if (pid > 0) _exit(0);
LOGIFNEG1(umask(0));
if (pidpath) { if (pidpath) {
CHECK_NE(-1, (fd = open(pidpath, O_CREAT | O_EXCL | O_WRONLY, 0644))); fd = open(pidpath, O_CREAT | O_EXCL | O_WRONLY, 0644);
CHECK_NE(-1, write(fd, ibuf, uint64toarray_radix10(getpid(), ibuf))); write(fd, ibuf, uint64toarray_radix10(getpid(), ibuf));
LOGIFNEG1(close(fd)); close(fd);
} }
if (!logpath) logpath = "/dev/null"; if (!logpath) logpath = "/dev/null";
CHECK_NOTNULL(freopen("/dev/null", "r", stdin)); freopen("/dev/null", "r", stdin);
CHECK_NOTNULL(freopen(logpath, "a", stdout)); freopen(logpath, "a", stdout);
CHECK_NOTNULL(freopen(logpath, "a", stderr)); freopen(logpath, "a", stderr);
} }
static int CompareHeaderValue(int h, const char *s) { static int CompareHeaderValue(int h, const char *s) {
@ -1139,11 +1142,12 @@ static void TuneServerSocket(void) {
void RedBean(void) { void RedBean(void) {
uint32_t addrsize; uint32_t addrsize;
if (IsWindows()) uniprocess = true; if (IsWindows()) uniprocess = true;
if (daemonize) Daemonize();
gmtoff = GetGmtOffset(); gmtoff = GetGmtOffset();
programfile = (const char *)getauxval(AT_EXECFN); programfile = (const char *)getauxval(AT_EXECFN);
CHECK(OpenZip(programfile)); CHECK(OpenZip(programfile));
xsigaction(SIGINT, OnTerminate, 0, 0, 0); xsigaction(SIGINT, OnTerminate, 0, 0, 0);
xsigaction(SIGHUP, OnTerminate, 0, 0, 0); xsigaction(SIGHUP, daemonize ? OnHup : OnTerminate, 0, 0, 0);
xsigaction(SIGTERM, OnTerminate, 0, 0, 0); xsigaction(SIGTERM, OnTerminate, 0, 0, 0);
xsigaction(SIGCHLD, SIG_IGN, 0, 0, 0); xsigaction(SIGCHLD, SIG_IGN, 0, 0, 0);
xsigaction(SIGPIPE, SIG_IGN, 0, 0, 0); xsigaction(SIGPIPE, SIG_IGN, 0, 0, 0);
@ -1157,7 +1161,6 @@ void RedBean(void) {
addrsize = sizeof(serveraddr); addrsize = sizeof(serveraddr);
CHECK_NE(-1, getsockname(server, &serveraddr, &addrsize)); CHECK_NE(-1, getsockname(server, &serveraddr, &addrsize));
DescribeAddress(serveraddrstr, &serveraddr); DescribeAddress(serveraddrstr, &serveraddr);
if (daemonize) Daemonize();
VERBOSEF("%s listen", serveraddrstr); VERBOSEF("%s listen", serveraddrstr);
if (printport) { if (printport) {
printf("%d\n", ntohs(serveraddr.sin_port)); printf("%d\n", ntohs(serveraddr.sin_port));