Skip to content

Commit 2aee2fa

Browse files
miss-islingtonserhiy-storchakaclaude
authored
[3.15] gh-154225: Fix os.openpty() acquiring a controlling terminal on Solaris (GH-154229) (GH-154238)
(cherry picked from commit a0caab9) Co-authored-by: Serhiy Storchaka <storchaka@gmail.com> Co-authored-by: Claude Fable 5 <noreply@anthropic.com>
1 parent b05ed78 commit 2aee2fa

2 files changed

Lines changed: 21 additions & 0 deletions

File tree

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Fix :func:`os.openpty` on Solaris and illumos: it no longer leaves the
2+
pseudo-terminal as the controlling terminal of the calling process.

Modules/posixmodule.c

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9429,11 +9429,30 @@ os_openpty_impl(PyObject *module)
94299429
goto posix_error;
94309430

94319431
#if defined(HAVE_STROPTS_H) && !defined(HAVE_DEV_PTC)
9432+
// Pushing "ptem" makes the slave a terminal, which a session leader
9433+
// without a controlling terminal then acquires as one despite O_NOCTTY.
9434+
// Note whether we already had one, so a new one can be disowned below.
9435+
int had_ctty = 0;
9436+
#ifdef TIOCNOTTY
9437+
int tty_fd = open("/dev/tty", O_RDONLY | O_NOCTTY);
9438+
if (tty_fd >= 0) {
9439+
had_ctty = 1;
9440+
close(tty_fd);
9441+
}
9442+
#endif
94329443
ioctl(slave_fd, I_PUSH, "ptem"); /* push ptem */
94339444
ioctl(slave_fd, I_PUSH, "ldterm"); /* push ldterm */
94349445
#ifndef __hpux
94359446
ioctl(slave_fd, I_PUSH, "ttcompat"); /* push ttcompat */
94369447
#endif /* __hpux */
9448+
#ifdef TIOCNOTTY
9449+
if (!had_ctty && getsid(0) == getpid()) {
9450+
// Disown it; TIOCNOTTY sends SIGHUP to the session leader.
9451+
PyOS_sighandler_t sig_saved = PyOS_setsig(SIGHUP, SIG_IGN);
9452+
ioctl(slave_fd, TIOCNOTTY);
9453+
PyOS_setsig(SIGHUP, sig_saved);
9454+
}
9455+
#endif
94379456
#endif /* defined(HAVE_STROPTS_H) && !defined(HAVE_DEV_PTC) */
94389457
#endif /* HAVE_OPENPTY */
94399458

0 commit comments

Comments
 (0)