summaryrefslogtreecommitdiff
path: root/cherrypy/process/plugins.py
diff options
context:
space:
mode:
authorJason R. Coombs <jaraco@jaraco.com>2017-11-10 13:49:29 -0500
committerJason R. Coombs <jaraco@jaraco.com>2017-11-10 13:49:29 -0500
commit41bde31333cdea549353fc1f869d5d3566cab4ba (patch)
tree39ef5b118f501cf1cc9e7faa54ab8240d9a19d03 /cherrypy/process/plugins.py
parent12ca3ed8838d2e66e7fef019db98a02e6d927be6 (diff)
downloadcherrypy-git-41bde31333cdea549353fc1f869d5d3566cab4ba.tar.gz
Prefer loops to copy paste
Diffstat (limited to 'cherrypy/process/plugins.py')
-rw-r--r--cherrypy/process/plugins.py35
1 files changed, 13 insertions, 22 deletions
diff --git a/cherrypy/process/plugins.py b/cherrypy/process/plugins.py
index 24588282..b107be39 100644
--- a/cherrypy/process/plugins.py
+++ b/cherrypy/process/plugins.py
@@ -393,28 +393,19 @@ class Daemonizer(SimplePlugin):
"{sys.argv[0]}: fork #{n} failed: ({exc.errno}) {exc.strerror}\n"
)
- # Do first fork.
- try:
- pid = os.fork()
- if pid > 0:
- # This is the parent; exit.
- logger('Forking once.')
- os._exit(0)
- except OSError as exc:
- # Python raises OSError rather than returning negative numbers.
- sys.exit(error_tmpl.format(sys=sys, exc=exc, n=1))
-
- os.setsid()
-
- # Do second fork
- try:
- pid = os.fork()
- if pid > 0:
- # This is the parent; exit.
- logger('Forking twice.')
- os._exit(0)
- except OSError as exc:
- sys.exit(error_tmpl.format(sys=sys, exc=exc, n=2))
+ for fork in range(2):
+ msg = ['Forking once.', 'Forking twice.'][fork]
+ try:
+ pid = os.fork()
+ if pid > 0:
+ # This is the parent; exit.
+ logger(msg)
+ os._exit(0)
+ except OSError as exc:
+ # Python raises OSError rather than returning negative numbers.
+ sys.exit(error_tmpl.format(sys=sys, exc=exc, n=fork + 1))
+ if fork == 0:
+ os.setsid()
os.umask(0)