New module 'login_tty'.
authorBruno Haible <bruno@clisp.org>
Mon, 22 Mar 2010 01:13:15 +0000 (02:13 +0100)
committerBruno Haible <bruno@clisp.org>
Mon, 22 Mar 2010 01:13:15 +0000 (02:13 +0100)
ChangeLog
doc/glibc-functions/login_tty.texi
lib/login_tty.c [new file with mode: 0644]
m4/pty.m4
modules/login_tty [new file with mode: 0644]

index d063335..2d26888 100644 (file)
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,5 +1,13 @@
 2010-03-21  Bruno Haible  <bruno@clisp.org>
 
+       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  <bruno@clisp.org>
+
        login_tty: Documentation.
        * doc/glibc-functions/login_tty.texi: New file.
        * doc/gnulib.texi (Glibc <utmp.h>): Include it.
index bfeda2a..26382de 100644 (file)
@@ -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{<utmp.h>} on glibc, Cygwin,
 in @code{<util.h>} 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 (file)
index 0000000..0403391
--- /dev/null
@@ -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 <http://www.gnu.org/licenses/>.  */
+
+#include <config.h>
+
+/* Currently no specification header.  */
+
+#include <fcntl.h>
+#include <unistd.h>
+
+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;
+}
index 6d2393b..f795792 100644 (file)
--- 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 (file)
index 0000000..41cb837
--- /dev/null
@@ -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