only close 0-2 fds when -R isn't specified

Following some discussion on IRC with @GrahamDumpleton this patch only
close stdios if -R isn't specified. It also let others fds open and
don't try to close them.

This should fix logging around and behave like other daemons. It should
also close #309.
This commit is contained in:
benoitc 2013-04-23 09:41:29 +02:00
parent cc7f595adc
commit 29aefcc1cf
2 changed files with 9 additions and 20 deletions

View File

@ -93,6 +93,9 @@ class Arbiter(object):
if 'GUNICORN_FD' in os.environ:
self.log.reopen_files()
if self.cfg.enable_stdio_inheritance:
util.disable_stdout_buffering()
self.address = self.cfg.address
self.num_workers = self.cfg.workers
self.debug = self.cfg.debug
@ -352,10 +355,6 @@ class Arbiter(object):
os.chdir(self.START_CTX['cwd'])
self.cfg.pre_exec(self)
# close all file descriptors except bound sockets
util.closerange(3, fds[0])
util.closerange(fds[-1] + 1, util.get_maxfd())
os.execvpe(self.START_CTX[0], self.START_CTX['args'], os.environ)
def reload(self):

View File

@ -426,25 +426,15 @@ def daemonize(enable_stdio_inheritance=False):
os.umask(0)
maxfd = get_maxfd()
if not enable_stdio_inheritance:
closerange(0, maxfd)
closerange(0, 3)
os.open(REDIRECT_TO, os.O_RDWR)
os.dup2(0, 1)
os.dup2(0, 2)
fd_null = os.open(REDIRECT_TO, os.O_RDWR)
if fd_null != 0:
os.dup2(fd_null, 0)
os.dup2(fd_null, 1)
os.dup2(fd_null, 2)
else:
closerange(3, maxfd)
os.open(REDIRECT_TO, os.O_RDWR)
for stream in (sys.stdin, sys.stdout, sys.stderr):
fd = stream.fileno()
try:
if stream.isatty():
os.close(fd)
if fd in (1, 2):
os.dup2(0, fd)
except AttributeError:
pass
disable_stdout_buffering()
def seed():