STDERR left open when daemonizing
Lighttpd leaves STDERR open when it goes into the background.
I have a script A which calls:
"system('/usr/sbin/lighttpd -f lighttpd.conf 2>&1');"
This works fine but script A is called by script B:
There is some setuiding going on.
Script B always hangs with a zombie child, until I kill the lighttpd process started by script A. This worked fine with apache. So I added:
to daemonize() in server.c just before the first fork. This stops my problem from happening. I don't have a clue why script B blocks but script A doesn't.
I've never written a daemon before, but the Unix Programming FAQ says step 6 of going into the background is:
`close()' fds 0, 1, and 2. This releases the standard in, out, and
error we inherited from our parent process. We have no way of knowing
where these fds might have been redirected to. Note that many daemons
use `sysconf()' to determine the limit `_SC_OPEN_MAX'. `_SC_OPEN_MAX'
tells you the maximun open files/process. Then in a loop, the daemon
can close all possible file descriptors. You have to decide if you
need to do this or not. If you think that there might be
file-descriptors open you should close them, since there's a limit on
number of concurrent file descriptors.
My one line fix may cause problems if stderr is written to later on. Probably should be redirected to /dev/null like stdin and stdout.
Updated by Anonymous over 13 years ago
OK. Script B was in fact calling script A like this:
if instead B does:
And A does:
system('/usr/sbin/lighttpd -f lighttpd.conf 2>&1');
And lighttpd.conf has:
Everything behaves, I don't get any zombies, B doesn't hang until lighttpd closes. I don't know why, but I am pleased it works. Case closed.
Also available in: Atom