core: split preserve_tty_fp() out into a function

pull/612/head
David Wilson 5 years ago
parent f4cee16526
commit f304ab8dec

@ -3680,13 +3680,15 @@ class ExternalContext(object):
os.dup2(fd, stdfd)
os.close(fd)
def _setup_stdio(self):
# #481: when stderr is a TTY due to being started via
# tty_create_child()/hybrid_tty_create_child(), and some privilege
# escalation tool like prehistoric versions of sudo exec this process
# over the top of itself, there is nothing left to keep the slave PTY
# open after we replace our stdio. Therefore if stderr is a TTY, keep
# around a permanent dup() to avoid receiving SIGHUP.
def _preserve_tty_fp(self):
"""
#481: when stderr is a TTY due to being started via tty_create_child()
or hybrid_tty_create_child(), and some privilege escalation tool like
prehistoric versions of sudo exec this process over the top of itself,
there is nothing left to keep the slave PTY open after we replace our
stdio. Therefore if stderr is a TTY, keep around a permanent dup() to
avoid receiving SIGHUP.
"""
try:
if os.isatty(2):
self.reserve_tty_fp = os.fdopen(os.dup(2), 'r+b', 0)
@ -3694,6 +3696,8 @@ class ExternalContext(object):
except OSError:
pass
def _setup_stdio(self):
self._preserve_tty_fp()
# When sys.stdout was opened by the runtime, overwriting it will not
# close FD 1. However when forking from a child that previously used
# fdopen(), overwriting it /will/ close FD 1. So we must swallow the

Loading…
Cancel
Save