From c811030cf5c9a216a14d5628089346c0565cf456 Mon Sep 17 00:00:00 2001 From: Bruno Haible Date: Mon, 22 Mar 2010 02:13:15 +0100 Subject: [PATCH] New module 'login_tty'. --- ChangeLog | 8 ++++ doc/glibc-functions/login_tty.texi | 10 ++--- lib/login_tty.c | 66 ++++++++++++++++++++++++++++++ m4/pty.m4 | 12 +++++- modules/login_tty | 27 ++++++++++++ 5 files changed, 117 insertions(+), 6 deletions(-) create mode 100644 lib/login_tty.c create mode 100644 modules/login_tty diff --git a/ChangeLog b/ChangeLog index d063335746..2d26888960 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,11 @@ +2010-03-21 Bruno Haible + + New module 'login_tty'. + * lib/login_tty.c: New file. + * m4/pty.m4 (gl_FUNC_LOGIN_TTY): New macro. + * modules/login_tty: New file. + * doc/glibc-functions/login_tty.texi: Mention the new module. + 2010-03-21 Bruno Haible login_tty: Documentation. diff --git a/doc/glibc-functions/login_tty.texi b/doc/glibc-functions/login_tty.texi index bfeda2ad4e..26382de901 100644 --- a/doc/glibc-functions/login_tty.texi +++ b/doc/glibc-functions/login_tty.texi @@ -2,14 +2,10 @@ @subsection @code{login_tty} @findex login_tty -Gnulib module: --- +Gnulib module: login_tty Portability problems fixed by Gnulib: @itemize -@end itemize - -Portability problems not fixed by Gnulib: -@itemize @item This function is missing on some platforms: AIX 5.1, HP-UX 11, IRIX 6.5, Solaris 10, mingw. @@ -18,6 +14,10 @@ This function requires linking with @code{-lutil} on some platforms: glibc 2.3.6, FreeBSD 6.0, NetBSD 3.0, OpenBSD 3.8. It is available without link options on other platforms: MacOS X 10.3, OSF/1 5.1, Cygwin, Interix 3.5. +@end itemize + +Portability problems not fixed by Gnulib: +@itemize @item This function is declared in @code{} on glibc, Cygwin, in @code{} on MacOS X 10.3, NetBSD 3.0, OpenBSD 3.8, diff --git a/lib/login_tty.c b/lib/login_tty.c new file mode 100644 index 0000000000..0403391774 --- /dev/null +++ b/lib/login_tty.c @@ -0,0 +1,66 @@ +/* Assign a given terminal as controlling terminal and as standard input, + standard output, standard error of the current process. + Copyright (C) 2010 Free Software Foundation, Inc. + + This program is free software: you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation; either version 3 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program. If not, see . */ + +#include + +/* Currently no specification header. */ + +#include +#include + +int +login_tty (int slave_fd) +{ + int i; + + /* Create a new session. */ + setsid (); + + /* Make fd the controlling terminal for the current process. + On Solaris: A terminal becomes the controlling terminal of a session + if it is being open()ed, at a moment when + 1. it is not already the controlling terminal of some session, and + 2. the process that open()s it is a session leader that does not have + a controlling terminal. + We assume condition 1, try to ensure condition 2, and then open() it. */ + for (i = 0; i < 3; i++) + if (i != slave_fd) + close (i); + { + char *slave_name; + int dummy_fd; + + slave_name = ttyname (slave_fd); + if (slave_name == NULL) + return -1; + dummy_fd = open (slave_name, O_RDWR); + if (dummy_fd < 0) + return -1; + close (dummy_fd); + } + + /* Assign fd to the standard input, standard output, and standardd error of + the current process. */ + for (i = 0; i < 3; i++) + if (slave_fd != i) + if (dup2 (slave_fd, i) < 0) + return -1; + if (slave_fd >= 3) + close (slave_fd); + + return 0; +} diff --git a/m4/pty.m4 b/m4/pty.m4 index 6d2393b0a2..f795792e18 100644 --- a/m4/pty.m4 +++ b/m4/pty.m4 @@ -1,4 +1,4 @@ -# pty.m4 serial 3 +# pty.m4 serial 4 dnl Copyright (C) 2010 Free Software Foundation, Inc. dnl This file is free software; the Free Software Foundation dnl gives unlimited permission to copy and/or distribute it, @@ -120,3 +120,13 @@ AC_DEFUN([gl_OPENPTY], AC_CHECK_FUNCS([_getpty posix_openpt]) fi ]) + +AC_DEFUN([gl_FUNC_LOGIN_TTY], +[ + AC_REQUIRE([gl_PTY_LIB]) + + AC_CHECK_FUNCS_ONCE([login_tty]) + if test $ac_cv_func_login_tty = no; then + AC_LIBOBJ([login_tty]) + fi +]) diff --git a/modules/login_tty b/modules/login_tty new file mode 100644 index 0000000000..41cb837f15 --- /dev/null +++ b/modules/login_tty @@ -0,0 +1,27 @@ +Description: +login_tty() function: Assign a given terminal as controlling terminal and as +standard input, standard output, standard error of the current process. + +Files: +lib/login_tty.c +m4/pty.m4 + +Depends-on: + +configure.ac: +gl_FUNC_LOGIN_TTY +gl_PTY_MODULE_INDICATOR([login_tty]) + +Makefile.am: + +Include: +extern int login_tty (int); + +Link: +$(PTY_LIB) + +License: +LGPL + +Maintainer: +Bruno Haible -- 2.30.2