From 29aefcc1cfc32097481aea6213a0df09962c7817 Mon Sep 17 00:00:00 2001 From: benoitc Date: Tue, 23 Apr 2013 09:41:29 +0200 Subject: [PATCH] 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. --- gunicorn/arbiter.py | 7 +++---- gunicorn/util.py | 22 ++++++---------------- 2 files changed, 9 insertions(+), 20 deletions(-) diff --git a/gunicorn/arbiter.py b/gunicorn/arbiter.py index 69d0f9e7..60e9c510 100644 --- a/gunicorn/arbiter.py +++ b/gunicorn/arbiter.py @@ -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): diff --git a/gunicorn/util.py b/gunicorn/util.py index 262b9604..71f6de2b 100644 --- a/gunicorn/util.py +++ b/gunicorn/util.py @@ -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():