Bug #1160


STDERR left open when daemonizing

Added by Anonymous almost 17 years ago. Updated over 15 years ago.

Target version:


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.

-- richard

Actions #1

Updated by Anonymous almost 17 years ago

I now see that STDERR is closed in log_error_open() from log.c if a logfile is specified in the config file. But this is called after daemonizing.

Working through the ramifications of this...

-- richard

Actions #2

Updated by Anonymous almost 17 years ago

OK. Script B was in fact calling script A like this:

system('A 2>&1');

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.

-- richard

Actions #3

Updated by jan over 16 years ago

  • Status changed from New to Fixed
  • Resolution set to invalid

closed by reporter.

Actions #4

Updated by stbuehler over 15 years ago

  • Status changed from Fixed to Invalid

Also available in: Atom