GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — bpo-41818: Add os.login_tty() for *nix. by 8vasu · Pull Request #23740 · python/cpython

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

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.

Sign up for GitHub

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

Closed
8vasu wants to merge 5 commits into python:main from 8vasu:os_login_tty

Uh oh!

There was an error while loading. Please reload this page.

Closed

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

Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
5 commits
Select commit Hold shift + click to select a range
ddc3219
Soumendra Ganguly: add os.login_tty() for *nix.
8vasu Dec 17, 2020
b6267a6
Update os.rst versionadded to 3.11
gpshead Aug 27, 2021
e372f55
Add check for login_tty() to configure.ac
8vasu Aug 28, 2021
a70889f
Merge remote-tracking branch 'dummy/os_login_tty' into os_login_tty
8vasu Aug 28, 2021
ec3b44b
Fix merge conflict in Modules/clinic/posixmodule.c.h
8vasu Nov 20, 2021
File filter
Filter by extension

Clear filters
Conversations
Failed to load comments.
Loading

Uh oh!

There was an error while loading. Please reload this page.

Jump to
Jump to file
Failed to load files.
Loading

Uh oh!

There was an error while loading. Please reload this page.

Diff view
Diff view

There are no files selected for viewing

11 changes: 11 additions & 0 deletions 11 Doc/library/os.rst
Show comments
View file
Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -990,6 +990,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
1 change: 1 addition & 0 deletions 1 Misc/NEWS.d/next/Library/2020-12-11-04-56-12.bpo-41818.bVX3sO.rst
Show comments
View file
Open in desktop
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Soumendra Ganguly: New function os.login_tty() for Unix.
41 changes: 40 additions & 1 deletion 41 Modules/clinic/posixmodule.c.h
Show comments
View file
Open in desktop

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

Oops, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

101 changes: 90 additions & 11 deletions 101 Modules/posixmodule.c
Show comments
View file
Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -7118,22 +7118,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
#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 @@ -7235,6 +7234,84 @@ 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]
Expand Down Expand Up @@ -7271,8 +7348,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 @@ -7613,7 +7691,7 @@ os_initgroups_impl(PyObject *module, PyObject *oname, gid_t gid)
const char *username = PyBytes_AS_STRING(oname);

if (initgroups(username, gid) == -1)
return PyErr_SetFromErrno(PyExc_OSError);
return posix_error();

Py_RETURN_NONE;
}
Expand Down Expand Up @@ -14652,6 +14730,7 @@ static PyMethodDef posix_methods[] = {
OS_SCHED_GETAFFINITY_METHODDEF
OS_OPENPTY_METHODDEF
OS_FORKPTY_METHODDEF
OS_LOGIN_TTY_METHODDEF
OS_GETEGID_METHODDEF
OS_GETEUID_METHODDEF
OS_GETGID_METHODDEF
Expand Down
115 changes: 112 additions & 3 deletions 115 configure
Show comments
View file
Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -787,6 +787,7 @@ infodir
docdir
oldincludedir
includedir
runstatedir
localstatedir
sharedstatedir
sysconfdir
Expand Down Expand Up @@ -905,6 +906,7 @@ datadir='${datarootdir}'
sysconfdir='${prefix}/etc'
sharedstatedir='${prefix}/com'
localstatedir='${prefix}/var'
runstatedir='${localstatedir}/run'
includedir='${prefix}/include'
oldincludedir='/usr/include'
docdir='${datarootdir}/doc/${PACKAGE_TARNAME}'
Expand Down Expand Up @@ -1157,6 +1159,15 @@ do
| -silent | --silent | --silen | --sile | --sil)
silent=yes ;;

-runstatedir | --runstatedir | --runstatedi | --runstated \
| --runstate | --runstat | --runsta | --runst | --runs \
| --run | --ru | --r)
ac_prev=runstatedir ;;
-runstatedir=* | --runstatedir=* | --runstatedi=* | --runstated=* \
| --runstate=* | --runstat=* | --runsta=* | --runst=* | --runs=* \
| --run=* | --ru=* | --r=*)
runstatedir=$ac_optarg ;;

-sbindir | --sbindir | --sbindi | --sbind | --sbin | --sbi | --sb)
ac_prev=sbindir ;;
-sbindir=* | --sbindir=* | --sbindi=* | --sbind=* | --sbin=* \
Expand Down Expand Up @@ -1294,7 +1305,7 @@ fi
for ac_var in exec_prefix prefix bindir sbindir libexecdir datarootdir \
datadir sysconfdir sharedstatedir localstatedir includedir \
oldincludedir docdir infodir htmldir dvidir pdfdir psdir \
libdir localedir mandir
libdir localedir mandir runstatedir
do
eval ac_val=\$$ac_var
# Remove trailing slashes.
Expand Down Expand Up @@ -1447,6 +1458,7 @@ Fine tuning of the installation directories:
--sysconfdir=DIR read-only single-machine data [PREFIX/etc]
--sharedstatedir=DIR modifiable architecture-independent data [PREFIX/com]
--localstatedir=DIR modifiable single-machine data [PREFIX/var]
--runstatedir=DIR modifiable per-process data [LOCALSTATEDIR/run]
--libdir=DIR object code libraries [EPREFIX/lib]
--includedir=DIR C header files [PREFIX/include]
--oldincludedir=DIR C header files for non-gcc [/usr/include]
Expand Down Expand Up @@ -8041,7 +8053,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 @@ -12735,7 +12747,7 @@ $as_echo "no" >&6; }
fi
rm -f core conftest.err conftest.$ac_objext conftest.$ac_ext

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

@gpshead gpshead Aug 27, 2021

Copy link
Copy Markdown
Member

There was a problem hiding this comment.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It looks like you're directly editing configure? this is generated code. edit the input configure.ac instead and regenerate configure. (i suspect might have just forgotten to commit your configure.ac change to the PR branch?)

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions

@8vasu 8vasu Aug 27, 2021

Copy link
Copy Markdown
Contributor Author

There was a problem hiding this comment.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sir, thank you for the prompt review. I will be honest; I actually did manually edit configure. I will learn how to do it and make the requested changes to configure.ac by Saturday morning.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions

@8vasu 8vasu Aug 28, 2021

Copy link
Copy Markdown
Contributor Author

There was a problem hiding this comment.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have edited configure.ac and generated configure using autoconf now. Are there any steps that I might have missed/done improperly? When I generated the configure script, it added the runstatedir option to it which was not there before; please let me know if that was not supposed to happen/how to fix it. Also, it says that this branch has "conflicts that must be resolved"; Modules/clinic/posixmodule.c.h is the conflicting file; what is the best way to fix this problem; I want to avoid the XY problem this time!

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions

for ac_func in openpty
do :
Expand Down Expand Up @@ -12831,6 +12843,103 @@ fi
fi


fi
done

for ac_func in login_tty
do :
ac_fn_c_check_func "$LINENO" "login_tty" "ac_cv_func_login_tty"
if test "x$ac_cv_func_login_tty" = xyes; then :
cat >>confdefs.h <<_ACEOF
#define HAVE_LOGIN_TTY 1
_ACEOF

else
{ $as_echo "$as_me:${as_lineno-$LINENO}: checking for login_tty in -lutil" >&5
$as_echo_n "checking for login_tty in -lutil... " >&6; }
if ${ac_cv_lib_util_login_tty+:} false; then :
$as_echo_n "(cached) " >&6
else
ac_check_lib_save_LIBS=$LIBS
LIBS="-lutil $LIBS"
cat confdefs.h - <<_ACEOF >conftest.$ac_ext
/* end confdefs.h. */

/* Override any GCC internal prototype to avoid an error.
Use char because int might match the return type of a GCC
builtin and then its argument prototype would still apply. */
#ifdef __cplusplus
extern "C"
#endif
char login_tty ();
int
main ()
{
return login_tty ();
;
return 0;
}
_ACEOF
if ac_fn_c_try_link "$LINENO"; then :
ac_cv_lib_util_login_tty=yes
else
ac_cv_lib_util_login_tty=no
fi
rm -f core conftest.err conftest.$ac_objext \
conftest$ac_exeext conftest.$ac_ext
LIBS=$ac_check_lib_save_LIBS
fi
{ $as_echo "$as_me:${as_lineno-$LINENO}: result: $ac_cv_lib_util_login_tty" >&5
$as_echo "$ac_cv_lib_util_login_tty" >&6; }
if test "x$ac_cv_lib_util_login_tty" = xyes; then :
$as_echo "#define HAVE_LOGIN_TTY 1" >>confdefs.h
LIBS="$LIBS -lutil"
else
{ $as_echo "$as_me:${as_lineno-$LINENO}: checking for login_tty in -lbsd" >&5
$as_echo_n "checking for login_tty in -lbsd... " >&6; }
if ${ac_cv_lib_bsd_login_tty+:} false; then :
$as_echo_n "(cached) " >&6
else
ac_check_lib_save_LIBS=$LIBS
LIBS="-lbsd $LIBS"
cat confdefs.h - <<_ACEOF >conftest.$ac_ext
/* end confdefs.h. */

/* Override any GCC internal prototype to avoid an error.
Use char because int might match the return type of a GCC
builtin and then its argument prototype would still apply. */
#ifdef __cplusplus
extern "C"
#endif
char login_tty ();
int
main ()
{
return login_tty ();
;
return 0;
}
_ACEOF
if ac_fn_c_try_link "$LINENO"; then :
ac_cv_lib_bsd_login_tty=yes
else
ac_cv_lib_bsd_login_tty=no
fi
rm -f core conftest.err conftest.$ac_objext \
conftest$ac_exeext conftest.$ac_ext
LIBS=$ac_check_lib_save_LIBS
fi
{ $as_echo "$as_me:${as_lineno-$LINENO}: result: $ac_cv_lib_bsd_login_tty" >&5
$as_echo "$ac_cv_lib_bsd_login_tty" >&6; }
if test "x$ac_cv_lib_bsd_login_tty" = xyes; then :
$as_echo "#define HAVE_LOGIN_TTY 1" >>confdefs.h
LIBS="$LIBS -lbsd"
fi


fi


fi
done

Expand Down
Loading
Oops, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

Add this suggestion to a batch that can be applied as a single commit. This suggestion is invalid because no changes were made to the code. Suggestions cannot be applied while the pull request is closed. Suggestions cannot be applied while viewing a subset of changes. Only one suggestion per line can be applied in a batch. Add this suggestion to a batch that can be applied as a single commit. Applying suggestions on deleted lines is not supported. You must change the existing code in this line in order to create a valid suggestion. Outdated suggestions cannot be applied. This suggestion has been applied or marked resolved. Suggestions cannot be applied from pending reviews. Suggestions cannot be applied on multi-line comments. Suggestions cannot be applied while the pull request is queued to merge. Suggestion cannot be applied right now. Please check back later.
You are visitor 01402503 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.