changeset 13036:2aebdd9d61a2

New module 'login_tty'.
author Bruno Haible <bruno@clisp.org>
date Mon, 22 Mar 2010 02:13:15 +0100
parents 103b9afdc2ea
children 4555dd26003f
files ChangeLog doc/glibc-functions/login_tty.texi lib/login_tty.c m4/pty.m4 modules/login_tty
diffstat 5 files changed, 117 insertions(+), 6 deletions(-) [+]
line wrap: on
line diff
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,11 @@
+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.
--- 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 @@
 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,
new file mode 100644
--- /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 <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;
+}
--- 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_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
+])
new file mode 100644
--- /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