Skip to content

Commit eedc3a3

Browse files
committed
Refactoring: common stdin </dev/null.
1 parent 835a4fc commit eedc3a3

File tree

1 file changed

+16
-23
lines changed

1 file changed

+16
-23
lines changed

gunicorn/util.py

Lines changed: 16 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -472,44 +472,37 @@ def daemonize(enable_stdio_inheritance=False):
472472

473473
os.umask(0o22)
474474

475+
# Always redirect stdin to /dev/null as we would
476+
# never expect to need to read interactive input.
477+
478+
os.close(0)
479+
480+
fd_null = os.open(REDIRECT_TO, os.O_RDWR)
481+
# PEP 446, make fd for /dev/null inheritable
482+
os.set_inheritable(fd_null, True)
483+
484+
# expect fd_null to be always 0 here, but in-case not ...
485+
if fd_null != 0:
486+
os.dup2(fd_null, 0)
487+
475488
# In both the following any file descriptors above stdin
476489
# stdout and stderr are left untouched. The inheritance
477490
# option simply allows one to have output go to a file
478491
# specified by way of shell redirection when not wanting
479492
# to use --error-log option.
480493

481494
if not enable_stdio_inheritance:
482-
# Remap all of stdin, stdout and stderr on to
495+
# Remap remaining fds stdout and stderr on to
483496
# /dev/null. The expectation is that users have
484497
# specified the --error-log option.
485498

486-
closerange(0, 3)
487-
488-
fd_null = os.open(REDIRECT_TO, os.O_RDWR)
489-
# PEP 446, make fd for /dev/null inheritable
490-
os.set_inheritable(fd_null, True)
491-
492-
# expect fd_null to be always 0 here, but in-case not ...
493-
if fd_null != 0:
494-
os.dup2(fd_null, 0)
499+
# 1/stdout, 2/stderr - 0/stdin already done above
500+
closerange(1, 3)
495501

496502
os.dup2(fd_null, 1)
497503
os.dup2(fd_null, 2)
498504

499505
else:
500-
# Always redirect stdin to /dev/null as we would
501-
# never expect to need to read interactive input.
502-
503-
os.close(0)
504-
505-
fd_null = os.open(REDIRECT_TO, os.O_RDWR)
506-
# PEP 446, make fd for /dev/null inheritable
507-
os.set_inheritable(fd_null, True)
508-
509-
# expect fd_null to be always 0 here, but in-case not ...
510-
if fd_null != 0:
511-
os.dup2(fd_null, 0)
512-
513506
# If stdout and stderr are still connected to
514507
# their original file descriptors we check to see
515508
# if they are associated with terminal devices.

0 commit comments

Comments
 (0)