Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

bpo-41818: Add os.login_tty() for *nix. #29658

Merged
merged 17 commits into from
May 5, 2022
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions Doc/library/os.rst
Original file line number Diff line number Diff line change
Expand Up @@ -991,6 +991,17 @@ as internal buffering of data.
.. versionadded:: 3.3


.. function:: login_tty(fd)

Prepare the tty of which fd is a file descriptor for a new login session.
Make the calling process a session leader; make the tty the controlling tty,
the stdin, the stdout, and the stderr of the calling process; close fd.

.. availability:: Unix.

.. versionadded:: 3.11


.. function:: lseek(fd, pos, how)

Set the current position of file descriptor *fd* to position *pos*, modified
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
New function os.login_tty() for Unix.
41 changes: 40 additions & 1 deletion Modules/clinic/posixmodule.c.h

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

103 changes: 92 additions & 11 deletions Modules/posixmodule.c
Original file line number Diff line number Diff line change
Expand Up @@ -7196,22 +7196,21 @@ os_sched_getaffinity_impl(PyObject *module, pid_t pid)
# define DEV_PTY_FILE "/dev/ptmx"
#endif

#if defined(HAVE_OPENPTY) || defined(HAVE_FORKPTY) || defined(HAVE_DEV_PTMX)
#ifdef HAVE_PTY_H
#if defined(HAVE_OPENPTY) || defined(HAVE_FORKPTY) || defined(HAVE_LOGIN_TTY) || defined(HAVE_DEV_PTMX)
#if defined(HAVE_PTY_H)
#include <pty.h>
#else
#ifdef HAVE_LIBUTIL_H
#if defined(HAVE_UTMP_H)
#include <utmp.h>
#endif /* HAVE_UTMP_H */
#elif defined(HAVE_LIBUTIL_H)
#include <libutil.h>
#else
#ifdef HAVE_UTIL_H
#elif defined(HAVE_UTIL_H)
#include <util.h>
#endif /* HAVE_UTIL_H */
#endif /* HAVE_LIBUTIL_H */
#endif /* HAVE_PTY_H */
#ifdef HAVE_STROPTS_H
#if defined(HAVE_STROPTS_H)
#include <stropts.h>
#endif
#endif /* defined(HAVE_OPENPTY) || defined(HAVE_FORKPTY) || defined(HAVE_DEV_PTMX) */
#endif /* if defined(HAVE_OPENPTY) || defined(HAVE_FORKPTY) || defined(HAVE_LOGIN_TTY) || defined(HAVE_DEV_PTMX) */


#if defined(HAVE_OPENPTY) || defined(HAVE__GETPTY) || defined(HAVE_DEV_PTMX)
Expand Down Expand Up @@ -7314,6 +7313,86 @@ os_openpty_impl(PyObject *module)
#endif /* defined(HAVE_OPENPTY) || defined(HAVE__GETPTY) || defined(HAVE_DEV_PTMX) */


#if defined(HAVE_SETSID)
#if defined(TIOCSCTTY) || defined(HAVE_TTYNAME)
#define HAVE_FALLBACK_LOGIN_TTY 1
#endif /* defined(TIOCSCTTY) || defined(HAVE_TTYNAME) */
#endif /* HAVE_SETSID */

#if defined(HAVE_LOGIN_TTY) || defined(HAVE_FALLBACK_LOGIN_TTY)
/*[clinic input]
os.login_tty

fd: fildes
/

Prepare the tty of which fd is a file descriptor for a new login session.

Make the calling process a session leader; make the tty the
controlling tty, the stdin, the stdout, and the stderr of the
calling process; close fd.
[clinic start generated code]*/

static PyObject *
os_login_tty_impl(PyObject *module, int fd)
/*[clinic end generated code: output=495a79911b4cc1bc input=5f298565099903a2]*/
{
#if defined(HAVE_LOGIN_TTY)
if (login_tty(fd) == -1) {
return posix_error();
}
#else /* defined(HAVE_FALLBACK_LOGIN_TTY) */
/* Establish a new session. */
if (setsid() == -1) {
return posix_error();
}

/* The tty becomes the controlling terminal. */
#if defined(TIOCSCTTY)
if (ioctl(fd, TIOCSCTTY, (char *)NULL) == -1) {
return posix_error();
}
#else /* defined(HAVE_TTYNAME) */
/* Fallback method (archaic); from Advanced Programming in the UNIX(R)
* Environment, Third edition, 2013, Section 9.6 - Controlling Terminal:
* "Systems derived from UNIX System V allocate the controlling
* terminal for a session when the session leader opens the first
* terminal device that is not already associated with a session, as
* long as the call to open does not specify the O_NOCTTY flag." */
char *tmppath = ttyname(fd);
if (tmppath == NULL) {
return posix_error();
}

#define CLOSE_IF_NOT_FD(otherfd) \
if (fd != otherfd) { \
close(otherfd); \
} \

CLOSE_IF_NOT_FD(0);
CLOSE_IF_NOT_FD(1);
CLOSE_IF_NOT_FD(2);

int tmpfd = open(tmppath, O_RDWR);
if (tmpfd == -1) {
return posix_error();
}
close(tmpfd);
#endif /* defined(TIOCSCTTY) */

/* The tty becomes stdin/stdout/stderr */
if (dup2(fd, 0) == -1 || dup2(fd, 1) == -1 || dup2(fd, 2) == -1) {
return posix_error();
}
if (fd > 2) {
close(fd);
}
#endif /* defined(HAVE_LOGIN_TTY) */
Py_RETURN_NONE;
}
#endif /* defined(HAVE_LOGIN_TTY) || defined(HAVE_FALLBACK_LOGIN_TTY) */


#ifdef HAVE_FORKPTY
/*[clinic input]
os.forkpty
Expand Down Expand Up @@ -7349,8 +7428,9 @@ os_forkpty_impl(PyObject *module)
/* parent: release the import lock. */
PyOS_AfterFork_Parent();
}
if (pid == -1)
if (pid == -1) {
return posix_error();
}
return Py_BuildValue("(Ni)", PyLong_FromPid(pid), master_fd);
}
#endif /* HAVE_FORKPTY */
Expand Down Expand Up @@ -14745,6 +14825,7 @@ static PyMethodDef posix_methods[] = {
OS_SCHED_SETAFFINITY_METHODDEF
OS_SCHED_GETAFFINITY_METHODDEF
OS_OPENPTY_METHODDEF
OS_LOGIN_TTY_METHODDEF
OS_FORKPTY_METHODDEF
OS_GETEGID_METHODDEF
OS_GETEUID_METHODDEF
Expand Down
101 changes: 99 additions & 2 deletions configure

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 8 additions & 2 deletions configure.ac
Original file line number Diff line number Diff line change
Expand Up @@ -2158,7 +2158,7 @@ sys/audioio.h sys/xattr.h sys/bsdtty.h sys/event.h sys/file.h sys/ioctl.h \
sys/kern_control.h sys/loadavg.h sys/lock.h sys/mkdev.h sys/modem.h \
sys/param.h sys/random.h sys/select.h sys/sendfile.h sys/socket.h sys/statvfs.h \
sys/stat.h sys/syscall.h sys/sys_domain.h sys/termio.h sys/time.h \
sys/times.h sys/types.h sys/uio.h sys/un.h sys/utsname.h sys/wait.h pty.h \
sys/times.h sys/types.h sys/uio.h sys/un.h sys/utsname.h sys/wait.h pty.h utmp.h \
libutil.h sys/resource.h netpacket/packet.h sysexits.h bluetooth.h \
linux/tipc.h linux/random.h spawn.h util.h alloca.h endian.h \
sys/endian.h sys/sysmacros.h linux/memfd.h linux/wait.h sys/memfd.h \
Expand Down Expand Up @@ -4081,14 +4081,20 @@ PY_CHECK_FUNC([setgroups], [
#endif
])

# check for openpty and forkpty
# check for openpty, login_tty, and forkpty

AC_CHECK_FUNCS(openpty,,
AC_CHECK_LIB(util,openpty,
[AC_DEFINE(HAVE_OPENPTY) LIBS="$LIBS -lutil"],
AC_CHECK_LIB(bsd,openpty, [AC_DEFINE(HAVE_OPENPTY) LIBS="$LIBS -lbsd"])
)
)
AC_CHECK_FUNCS(login_tty,,
AC_CHECK_LIB(util,login_tty,
[AC_DEFINE(HAVE_LOGIN_TTY) LIBS="$LIBS -lutil"],
AC_CHECK_LIB(bsd,login_tty, [AC_DEFINE(HAVE_LOGIN_TTY) LIBS="$LIBS -lbsd"])
)
)
AC_CHECK_FUNCS(forkpty,,
AC_CHECK_LIB(util,forkpty,
[AC_DEFINE(HAVE_FORKPTY) LIBS="$LIBS -lutil"],
Expand Down