Blame SOURCES/vte291-restore-gnome-pty-helper.patch

ae83b6
From eafad96a6a54c525625ab31cf30cd741ec383492 Mon Sep 17 00:00:00 2001
ae83b6
From: Debarshi Ray <debarshir@gnome.org>
ae83b6
Date: Tue, 25 Apr 2017 17:15:36 +0200
ae83b6
Subject: [PATCH 1/4] Restore gnome-pty-helper
ae83b6
ae83b6
https://bugzilla.redhat.com/show_bug.cgi?id=1443504
ae83b6
---
ae83b6
 Makefile.am                            |   5 +
ae83b6
 configure.ac                           |  32 +-
ae83b6
 gnome-pty-helper/AUTHORS               |   4 +
ae83b6
 gnome-pty-helper/COPYING               | 502 ++++++++++++++++++++++
ae83b6
 gnome-pty-helper/Makefile.am           |  32 ++
ae83b6
 gnome-pty-helper/README                |   9 +
ae83b6
 gnome-pty-helper/acinclude.m4          | 244 +++++++++++
ae83b6
 gnome-pty-helper/configure.ac          |  33 ++
ae83b6
 gnome-pty-helper/git.mk                |   1 +
ae83b6
 gnome-pty-helper/gnome-login-support.c | 380 +++++++++++++++++
ae83b6
 gnome-pty-helper/gnome-login-support.h |  39 ++
ae83b6
 gnome-pty-helper/gnome-pty-helper.c    | 751 +++++++++++++++++++++++++++++++++
ae83b6
 gnome-pty-helper/gnome-pty.h           |  22 +
ae83b6
 gnome-pty-helper/gnome-utmp.c          | 374 ++++++++++++++++
ae83b6
 src/pty.cc                             | 526 ++++++++++++++++++++++-
ae83b6
 src/vte.cc                             |   2 +
ae83b6
 src/vte/vteenums.h                     |  15 +-
ae83b6
 17 files changed, 2953 insertions(+), 18 deletions(-)
ae83b6
 create mode 100644 gnome-pty-helper/AUTHORS
ae83b6
 create mode 100644 gnome-pty-helper/COPYING
ae83b6
 create mode 100644 gnome-pty-helper/Makefile.am
ae83b6
 create mode 100644 gnome-pty-helper/README
ae83b6
 create mode 100644 gnome-pty-helper/acinclude.m4
ae83b6
 create mode 100644 gnome-pty-helper/configure.ac
ae83b6
 create mode 100644 gnome-pty-helper/git.mk
ae83b6
 create mode 100644 gnome-pty-helper/gnome-login-support.c
ae83b6
 create mode 100644 gnome-pty-helper/gnome-login-support.h
ae83b6
 create mode 100644 gnome-pty-helper/gnome-pty-helper.c
ae83b6
 create mode 100644 gnome-pty-helper/gnome-pty.h
ae83b6
 create mode 100644 gnome-pty-helper/gnome-utmp.c
ae83b6
ae83b6
diff --git a/Makefile.am b/Makefile.am
ae83b6
index 10c9c93b84a9..1603cce544f8 100644
ae83b6
--- a/Makefile.am
ae83b6
+++ b/Makefile.am
ae83b6
@@ -12,6 +12,10 @@ EXTRA_DIST = \
ae83b6
 
ae83b6
 DISTCLEANFILES =
ae83b6
 
ae83b6
+if BUILD_GNOME_PTY_HELPER
ae83b6
+SUBDIRS += gnome-pty-helper
ae83b6
+endif
ae83b6
+
ae83b6
 if ENABLE_GLADE_CATALOGUE
ae83b6
 SUBDIRS += glade
ae83b6
 endif
ae83b6
@@ -24,6 +28,7 @@ DISTCHECK_CONFIGURE_FLAGS = \
ae83b6
 	--enable-introspection \
ae83b6
 	--disable-vala \
ae83b6
 	--disable-test-application \
ae83b6
+	--enable-gnome-pty-helper \
ae83b6
 	--disable-silent-rules \
ae83b6
 	--with-gtk=$(GTK_API_VERSION)
ae83b6
 
ae83b6
diff --git a/configure.ac b/configure.ac
ae83b6
index a2f8792e2193..6f73b0a240f5 100644
ae83b6
--- a/configure.ac
ae83b6
+++ b/configure.ac
ae83b6
@@ -278,9 +278,22 @@ VTE_DEFAULT_TERM=xterm-256color
ae83b6
 AC_DEFINE_UNQUOTED(VTE_DEFAULT_TERM,"$VTE_DEFAULT_TERM",[The default value $TERM is set to.])
ae83b6
 
ae83b6
 # Check for headers.
ae83b6
-AC_CHECK_HEADERS([sys/select.h sys/syslimits.h sys/termios.h sys/wait.h stropts.h termios.h util.h wchar.h pty.h])
ae83b6
+AC_CHECK_HEADERS([sys/select.h sys/syslimits.h sys/termios.h sys/un.h sys/wait.h stropts.h termios.h util.h wchar.h pty.h])
ae83b6
 AC_HEADER_TIOCGWINSZ
ae83b6
 
ae83b6
+AC_CHECK_FUNC(socket,[have_socket=1],AC_CHECK_LIB(socket,socket,[have_socket=1; LIBS="$LIBS -lsocket"]))
ae83b6
+AC_CHECK_FUNC(socketpair,[have_socketpair=1],AC_CHECK_LIB(socket,socketpair,[have_socketpair=1; LIBS="$LIBS -lsocket"]))
ae83b6
+AC_CHECK_FUNC(recvmsg,[have_recvmsg=1],AC_CHECK_LIB(socket,recvmsg,[have_recvmsg=1; LIBS="$LIBS -lsocket -lnsl"]))
ae83b6
+if test x$have_socket = x1 ; then
ae83b6
+	AC_DEFINE(HAVE_SOCKET,1,[Define if you have the socket function.])
ae83b6
+fi
ae83b6
+if test x$have_socketpair = x1 ; then
ae83b6
+	AC_DEFINE(HAVE_SOCKETPAIR,1,[Define if you have the socketpair function.])
ae83b6
+fi
ae83b6
+if test x$have_recvmsg = x1 ; then
ae83b6
+	AC_DEFINE(HAVE_RECVMSG,1,[Define if you have the recvmsg function.])
ae83b6
+fi
ae83b6
+
ae83b6
 # Check for how to open a new PTY. We support posix_openpt and BSDs openpty
ae83b6
 
ae83b6
 AC_CHECK_FUNCS([posix_openpt grantpt unlockpt ptsname],[],
ae83b6
@@ -322,6 +335,22 @@ PKG_CHECK_MODULES([APP],[
ae83b6
   gdk-pixbuf-2.0
ae83b6
   gtk+-$GTK_API_VERSION >= $GTK_REQUIRED])
ae83b6
 
ae83b6
+################################################################################
ae83b6
+# GNOME PTY Helper
ae83b6
+################################################################################
ae83b6
+
ae83b6
+AC_MSG_CHECKING([whether to build the PTY helper binary])
ae83b6
+AC_ARG_ENABLE(gnome-pty-helper,
ae83b6
+  [AS_HELP_STRING([--disable-gnome-pty-helper],
ae83b6
+                  [Build a setuid helper for opening ptys])],
ae83b6
+  [],[enable_gnome_pty_helper=no])
ae83b6
+AC_MSG_RESULT([$enable_gnome_pty_helper])
ae83b6
+if test "$enable_gnome_pty_helper" != no; then
ae83b6
+	AC_DEFINE(VTE_USE_GNOME_PTY_HELPER,1,[Define if you intend to use gnome-pty-helper.])
ae83b6
+	AC_CONFIG_SUBDIRS(gnome-pty-helper)
ae83b6
+fi
ae83b6
+AM_CONDITIONAL(BUILD_GNOME_PTY_HELPER,[test "$enable_gnome_pty_helper" != no])
ae83b6
+
ae83b6
 ################################################################################
ae83b6
 # Glade catalogue
ae83b6
 ################################################################################
ae83b6
@@ -465,4 +494,5 @@ Configuration for libvte $VERSION for gtk+-$GTK_API_VERSION
ae83b6
 	Debugging: $enable_debug
ae83b6
 	Introspection: $enable_introspection
ae83b6
         Vala bindings: $enable_vala
ae83b6
+	PTY helper: $enable_gnome_pty_helper
ae83b6
 EOF
ae83b6
diff --git a/gnome-pty-helper/AUTHORS b/gnome-pty-helper/AUTHORS
ae83b6
new file mode 100644
ae83b6
index 000000000000..99a686ffedff
ae83b6
--- /dev/null
ae83b6
+++ b/gnome-pty-helper/AUTHORS
ae83b6
@@ -0,0 +1,4 @@
ae83b6
+Michael Zucchi <zucchi@helixcode.com>
ae83b6
+Dugan Porter
ae83b6
+Miguel de Icaza <miguel@gnu.org>
ae83b6
+Timur Bakeyev <timur@gnu.org>
ae83b6
diff --git a/gnome-pty-helper/COPYING b/gnome-pty-helper/COPYING
ae83b6
new file mode 100644
ae83b6
index 000000000000..4362b49151d7
ae83b6
--- /dev/null
ae83b6
+++ b/gnome-pty-helper/COPYING
ae83b6
@@ -0,0 +1,502 @@
ae83b6
+                  GNU LESSER GENERAL PUBLIC LICENSE
ae83b6
+                       Version 2.1, February 1999
ae83b6
+
ae83b6
+ Copyright (C) 1991, 1999 Free Software Foundation, Inc.
ae83b6
+ 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
ae83b6
+ Everyone is permitted to copy and distribute verbatim copies
ae83b6
+ of this license document, but changing it is not allowed.
ae83b6
+
ae83b6
+[This is the first released version of the Lesser GPL.  It also counts
ae83b6
+ as the successor of the GNU Library Public License, version 2, hence
ae83b6
+ the version number 2.1.]
ae83b6
+
ae83b6
+                            Preamble
ae83b6
+
ae83b6
+  The licenses for most software are designed to take away your
ae83b6
+freedom to share and change it.  By contrast, the GNU General Public
ae83b6
+Licenses are intended to guarantee your freedom to share and change
ae83b6
+free software--to make sure the software is free for all its users.
ae83b6
+
ae83b6
+  This license, the Lesser General Public License, applies to some
ae83b6
+specially designated software packages--typically libraries--of the
ae83b6
+Free Software Foundation and other authors who decide to use it.  You
ae83b6
+can use it too, but we suggest you first think carefully about whether
ae83b6
+this license or the ordinary General Public License is the better
ae83b6
+strategy to use in any particular case, based on the explanations below.
ae83b6
+
ae83b6
+  When we speak of free software, we are referring to freedom of use,
ae83b6
+not price.  Our General Public Licenses are designed to make sure that
ae83b6
+you have the freedom to distribute copies of free software (and charge
ae83b6
+for this service if you wish); that you receive source code or can get
ae83b6
+it if you want it; that you can change the software and use pieces of
ae83b6
+it in new free programs; and that you are informed that you can do
ae83b6
+these things.
ae83b6
+
ae83b6
+  To protect your rights, we need to make restrictions that forbid
ae83b6
+distributors to deny you these rights or to ask you to surrender these
ae83b6
+rights.  These restrictions translate to certain responsibilities for
ae83b6
+you if you distribute copies of the library or if you modify it.
ae83b6
+
ae83b6
+  For example, if you distribute copies of the library, whether gratis
ae83b6
+or for a fee, you must give the recipients all the rights that we gave
ae83b6
+you.  You must make sure that they, too, receive or can get the source
ae83b6
+code.  If you link other code with the library, you must provide
ae83b6
+complete object files to the recipients, so that they can relink them
ae83b6
+with the library after making changes to the library and recompiling
ae83b6
+it.  And you must show them these terms so they know their rights.
ae83b6
+
ae83b6
+  We protect your rights with a two-step method: (1) we copyright the
ae83b6
+library, and (2) we offer you this license, which gives you legal
ae83b6
+permission to copy, distribute and/or modify the library.
ae83b6
+
ae83b6
+  To protect each distributor, we want to make it very clear that
ae83b6
+there is no warranty for the free library.  Also, if the library is
ae83b6
+modified by someone else and passed on, the recipients should know
ae83b6
+that what they have is not the original version, so that the original
ae83b6
+author's reputation will not be affected by problems that might be
ae83b6
+introduced by others.
ae83b6
+
ae83b6
+  Finally, software patents pose a constant threat to the existence of
ae83b6
+any free program.  We wish to make sure that a company cannot
ae83b6
+effectively restrict the users of a free program by obtaining a
ae83b6
+restrictive license from a patent holder.  Therefore, we insist that
ae83b6
+any patent license obtained for a version of the library must be
ae83b6
+consistent with the full freedom of use specified in this license.
ae83b6
+
ae83b6
+  Most GNU software, including some libraries, is covered by the
ae83b6
+ordinary GNU General Public License.  This license, the GNU Lesser
ae83b6
+General Public License, applies to certain designated libraries, and
ae83b6
+is quite different from the ordinary General Public License.  We use
ae83b6
+this license for certain libraries in order to permit linking those
ae83b6
+libraries into non-free programs.
ae83b6
+
ae83b6
+  When a program is linked with a library, whether statically or using
ae83b6
+a shared library, the combination of the two is legally speaking a
ae83b6
+combined work, a derivative of the original library.  The ordinary
ae83b6
+General Public License therefore permits such linking only if the
ae83b6
+entire combination fits its criteria of freedom.  The Lesser General
ae83b6
+Public License permits more lax criteria for linking other code with
ae83b6
+the library.
ae83b6
+
ae83b6
+  We call this license the "Lesser" General Public License because it
ae83b6
+does Less to protect the user's freedom than the ordinary General
ae83b6
+Public License.  It also provides other free software developers Less
ae83b6
+of an advantage over competing non-free programs.  These disadvantages
ae83b6
+are the reason we use the ordinary General Public License for many
ae83b6
+libraries.  However, the Lesser license provides advantages in certain
ae83b6
+special circumstances.
ae83b6
+
ae83b6
+  For example, on rare occasions, there may be a special need to
ae83b6
+encourage the widest possible use of a certain library, so that it becomes
ae83b6
+a de-facto standard.  To achieve this, non-free programs must be
ae83b6
+allowed to use the library.  A more frequent case is that a free
ae83b6
+library does the same job as widely used non-free libraries.  In this
ae83b6
+case, there is little to gain by limiting the free library to free
ae83b6
+software only, so we use the Lesser General Public License.
ae83b6
+
ae83b6
+  In other cases, permission to use a particular library in non-free
ae83b6
+programs enables a greater number of people to use a large body of
ae83b6
+free software.  For example, permission to use the GNU C Library in
ae83b6
+non-free programs enables many more people to use the whole GNU
ae83b6
+operating system, as well as its variant, the GNU/Linux operating
ae83b6
+system.
ae83b6
+
ae83b6
+  Although the Lesser General Public License is Less protective of the
ae83b6
+users' freedom, it does ensure that the user of a program that is
ae83b6
+linked with the Library has the freedom and the wherewithal to run
ae83b6
+that program using a modified version of the Library.
ae83b6
+
ae83b6
+  The precise terms and conditions for copying, distribution and
ae83b6
+modification follow.  Pay close attention to the difference between a
ae83b6
+"work based on the library" and a "work that uses the library".  The
ae83b6
+former contains code derived from the library, whereas the latter must
ae83b6
+be combined with the library in order to run.
ae83b6
+
ae83b6
+                  GNU LESSER GENERAL PUBLIC LICENSE
ae83b6
+   TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION
ae83b6
+
ae83b6
+  0. This License Agreement applies to any software library or other
ae83b6
+program which contains a notice placed by the copyright holder or
ae83b6
+other authorized party saying it may be distributed under the terms of
ae83b6
+this Lesser General Public License (also called "this License").
ae83b6
+Each licensee is addressed as "you".
ae83b6
+
ae83b6
+  A "library" means a collection of software functions and/or data
ae83b6
+prepared so as to be conveniently linked with application programs
ae83b6
+(which use some of those functions and data) to form executables.
ae83b6
+
ae83b6
+  The "Library", below, refers to any such software library or work
ae83b6
+which has been distributed under these terms.  A "work based on the
ae83b6
+Library" means either the Library or any derivative work under
ae83b6
+copyright law: that is to say, a work containing the Library or a
ae83b6
+portion of it, either verbatim or with modifications and/or translated
ae83b6
+straightforwardly into another language.  (Hereinafter, translation is
ae83b6
+included without limitation in the term "modification".)
ae83b6
+
ae83b6
+  "Source code" for a work means the preferred form of the work for
ae83b6
+making modifications to it.  For a library, complete source code means
ae83b6
+all the source code for all modules it contains, plus any associated
ae83b6
+interface definition files, plus the scripts used to control compilation
ae83b6
+and installation of the library.
ae83b6
+
ae83b6
+  Activities other than copying, distribution and modification are not
ae83b6
+covered by this License; they are outside its scope.  The act of
ae83b6
+running a program using the Library is not restricted, and output from
ae83b6
+such a program is covered only if its contents constitute a work based
ae83b6
+on the Library (independent of the use of the Library in a tool for
ae83b6
+writing it).  Whether that is true depends on what the Library does
ae83b6
+and what the program that uses the Library does.
ae83b6
+
ae83b6
+  1. You may copy and distribute verbatim copies of the Library's
ae83b6
+complete source code as you receive it, in any medium, provided that
ae83b6
+you conspicuously and appropriately publish on each copy an
ae83b6
+appropriate copyright notice and disclaimer of warranty; keep intact
ae83b6
+all the notices that refer to this License and to the absence of any
ae83b6
+warranty; and distribute a copy of this License along with the
ae83b6
+Library.
ae83b6
+
ae83b6
+  You may charge a fee for the physical act of transferring a copy,
ae83b6
+and you may at your option offer warranty protection in exchange for a
ae83b6
+fee.
ae83b6
+
ae83b6
+  2. You may modify your copy or copies of the Library or any portion
ae83b6
+of it, thus forming a work based on the Library, and copy and
ae83b6
+distribute such modifications or work under the terms of Section 1
ae83b6
+above, provided that you also meet all of these conditions:
ae83b6
+
ae83b6
+    a) The modified work must itself be a software library.
ae83b6
+
ae83b6
+    b) You must cause the files modified to carry prominent notices
ae83b6
+    stating that you changed the files and the date of any change.
ae83b6
+
ae83b6
+    c) You must cause the whole of the work to be licensed at no
ae83b6
+    charge to all third parties under the terms of this License.
ae83b6
+
ae83b6
+    d) If a facility in the modified Library refers to a function or a
ae83b6
+    table of data to be supplied by an application program that uses
ae83b6
+    the facility, other than as an argument passed when the facility
ae83b6
+    is invoked, then you must make a good faith effort to ensure that,
ae83b6
+    in the event an application does not supply such function or
ae83b6
+    table, the facility still operates, and performs whatever part of
ae83b6
+    its purpose remains meaningful.
ae83b6
+
ae83b6
+    (For example, a function in a library to compute square roots has
ae83b6
+    a purpose that is entirely well-defined independent of the
ae83b6
+    application.  Therefore, Subsection 2d requires that any
ae83b6
+    application-supplied function or table used by this function must
ae83b6
+    be optional: if the application does not supply it, the square
ae83b6
+    root function must still compute square roots.)
ae83b6
+
ae83b6
+These requirements apply to the modified work as a whole.  If
ae83b6
+identifiable sections of that work are not derived from the Library,
ae83b6
+and can be reasonably considered independent and separate works in
ae83b6
+themselves, then this License, and its terms, do not apply to those
ae83b6
+sections when you distribute them as separate works.  But when you
ae83b6
+distribute the same sections as part of a whole which is a work based
ae83b6
+on the Library, the distribution of the whole must be on the terms of
ae83b6
+this License, whose permissions for other licensees extend to the
ae83b6
+entire whole, and thus to each and every part regardless of who wrote
ae83b6
+it.
ae83b6
+
ae83b6
+Thus, it is not the intent of this section to claim rights or contest
ae83b6
+your rights to work written entirely by you; rather, the intent is to
ae83b6
+exercise the right to control the distribution of derivative or
ae83b6
+collective works based on the Library.
ae83b6
+
ae83b6
+In addition, mere aggregation of another work not based on the Library
ae83b6
+with the Library (or with a work based on the Library) on a volume of
ae83b6
+a storage or distribution medium does not bring the other work under
ae83b6
+the scope of this License.
ae83b6
+
ae83b6
+  3. You may opt to apply the terms of the ordinary GNU General Public
ae83b6
+License instead of this License to a given copy of the Library.  To do
ae83b6
+this, you must alter all the notices that refer to this License, so
ae83b6
+that they refer to the ordinary GNU General Public License, version 2,
ae83b6
+instead of to this License.  (If a newer version than version 2 of the
ae83b6
+ordinary GNU General Public License has appeared, then you can specify
ae83b6
+that version instead if you wish.)  Do not make any other change in
ae83b6
+these notices.
ae83b6
+
ae83b6
+  Once this change is made in a given copy, it is irreversible for
ae83b6
+that copy, so the ordinary GNU General Public License applies to all
ae83b6
+subsequent copies and derivative works made from that copy.
ae83b6
+
ae83b6
+  This option is useful when you wish to copy part of the code of
ae83b6
+the Library into a program that is not a library.
ae83b6
+
ae83b6
+  4. You may copy and distribute the Library (or a portion or
ae83b6
+derivative of it, under Section 2) in object code or executable form
ae83b6
+under the terms of Sections 1 and 2 above provided that you accompany
ae83b6
+it with the complete corresponding machine-readable source code, which
ae83b6
+must be distributed under the terms of Sections 1 and 2 above on a
ae83b6
+medium customarily used for software interchange.
ae83b6
+
ae83b6
+  If distribution of object code is made by offering access to copy
ae83b6
+from a designated place, then offering equivalent access to copy the
ae83b6
+source code from the same place satisfies the requirement to
ae83b6
+distribute the source code, even though third parties are not
ae83b6
+compelled to copy the source along with the object code.
ae83b6
+
ae83b6
+  5. A program that contains no derivative of any portion of the
ae83b6
+Library, but is designed to work with the Library by being compiled or
ae83b6
+linked with it, is called a "work that uses the Library".  Such a
ae83b6
+work, in isolation, is not a derivative work of the Library, and
ae83b6
+therefore falls outside the scope of this License.
ae83b6
+
ae83b6
+  However, linking a "work that uses the Library" with the Library
ae83b6
+creates an executable that is a derivative of the Library (because it
ae83b6
+contains portions of the Library), rather than a "work that uses the
ae83b6
+library".  The executable is therefore covered by this License.
ae83b6
+Section 6 states terms for distribution of such executables.
ae83b6
+
ae83b6
+  When a "work that uses the Library" uses material from a header file
ae83b6
+that is part of the Library, the object code for the work may be a
ae83b6
+derivative work of the Library even though the source code is not.
ae83b6
+Whether this is true is especially significant if the work can be
ae83b6
+linked without the Library, or if the work is itself a library.  The
ae83b6
+threshold for this to be true is not precisely defined by law.
ae83b6
+
ae83b6
+  If such an object file uses only numerical parameters, data
ae83b6
+structure layouts and accessors, and small macros and small inline
ae83b6
+functions (ten lines or less in length), then the use of the object
ae83b6
+file is unrestricted, regardless of whether it is legally a derivative
ae83b6
+work.  (Executables containing this object code plus portions of the
ae83b6
+Library will still fall under Section 6.)
ae83b6
+
ae83b6
+  Otherwise, if the work is a derivative of the Library, you may
ae83b6
+distribute the object code for the work under the terms of Section 6.
ae83b6
+Any executables containing that work also fall under Section 6,
ae83b6
+whether or not they are linked directly with the Library itself.
ae83b6
+
ae83b6
+  6. As an exception to the Sections above, you may also combine or
ae83b6
+link a "work that uses the Library" with the Library to produce a
ae83b6
+work containing portions of the Library, and distribute that work
ae83b6
+under terms of your choice, provided that the terms permit
ae83b6
+modification of the work for the customer's own use and reverse
ae83b6
+engineering for debugging such modifications.
ae83b6
+
ae83b6
+  You must give prominent notice with each copy of the work that the
ae83b6
+Library is used in it and that the Library and its use are covered by
ae83b6
+this License.  You must supply a copy of this License.  If the work
ae83b6
+during execution displays copyright notices, you must include the
ae83b6
+copyright notice for the Library among them, as well as a reference
ae83b6
+directing the user to the copy of this License.  Also, you must do one
ae83b6
+of these things:
ae83b6
+
ae83b6
+    a) Accompany the work with the complete corresponding
ae83b6
+    machine-readable source code for the Library including whatever
ae83b6
+    changes were used in the work (which must be distributed under
ae83b6
+    Sections 1 and 2 above); and, if the work is an executable linked
ae83b6
+    with the Library, with the complete machine-readable "work that
ae83b6
+    uses the Library", as object code and/or source code, so that the
ae83b6
+    user can modify the Library and then relink to produce a modified
ae83b6
+    executable containing the modified Library.  (It is understood
ae83b6
+    that the user who changes the contents of definitions files in the
ae83b6
+    Library will not necessarily be able to recompile the application
ae83b6
+    to use the modified definitions.)
ae83b6
+
ae83b6
+    b) Use a suitable shared library mechanism for linking with the
ae83b6
+    Library.  A suitable mechanism is one that (1) uses at run time a
ae83b6
+    copy of the library already present on the user's computer system,
ae83b6
+    rather than copying library functions into the executable, and (2)
ae83b6
+    will operate properly with a modified version of the library, if
ae83b6
+    the user installs one, as long as the modified version is
ae83b6
+    interface-compatible with the version that the work was made with.
ae83b6
+
ae83b6
+    c) Accompany the work with a written offer, valid for at
ae83b6
+    least three years, to give the same user the materials
ae83b6
+    specified in Subsection 6a, above, for a charge no more
ae83b6
+    than the cost of performing this distribution.
ae83b6
+
ae83b6
+    d) If distribution of the work is made by offering access to copy
ae83b6
+    from a designated place, offer equivalent access to copy the above
ae83b6
+    specified materials from the same place.
ae83b6
+
ae83b6
+    e) Verify that the user has already received a copy of these
ae83b6
+    materials or that you have already sent this user a copy.
ae83b6
+
ae83b6
+  For an executable, the required form of the "work that uses the
ae83b6
+Library" must include any data and utility programs needed for
ae83b6
+reproducing the executable from it.  However, as a special exception,
ae83b6
+the materials to be distributed need not include anything that is
ae83b6
+normally distributed (in either source or binary form) with the major
ae83b6
+components (compiler, kernel, and so on) of the operating system on
ae83b6
+which the executable runs, unless that component itself accompanies
ae83b6
+the executable.
ae83b6
+
ae83b6
+  It may happen that this requirement contradicts the license
ae83b6
+restrictions of other proprietary libraries that do not normally
ae83b6
+accompany the operating system.  Such a contradiction means you cannot
ae83b6
+use both them and the Library together in an executable that you
ae83b6
+distribute.
ae83b6
+
ae83b6
+  7. You may place library facilities that are a work based on the
ae83b6
+Library side-by-side in a single library together with other library
ae83b6
+facilities not covered by this License, and distribute such a combined
ae83b6
+library, provided that the separate distribution of the work based on
ae83b6
+the Library and of the other library facilities is otherwise
ae83b6
+permitted, and provided that you do these two things:
ae83b6
+
ae83b6
+    a) Accompany the combined library with a copy of the same work
ae83b6
+    based on the Library, uncombined with any other library
ae83b6
+    facilities.  This must be distributed under the terms of the
ae83b6
+    Sections above.
ae83b6
+
ae83b6
+    b) Give prominent notice with the combined library of the fact
ae83b6
+    that part of it is a work based on the Library, and explaining
ae83b6
+    where to find the accompanying uncombined form of the same work.
ae83b6
+
ae83b6
+  8. You may not copy, modify, sublicense, link with, or distribute
ae83b6
+the Library except as expressly provided under this License.  Any
ae83b6
+attempt otherwise to copy, modify, sublicense, link with, or
ae83b6
+distribute the Library is void, and will automatically terminate your
ae83b6
+rights under this License.  However, parties who have received copies,
ae83b6
+or rights, from you under this License will not have their licenses
ae83b6
+terminated so long as such parties remain in full compliance.
ae83b6
+
ae83b6
+  9. You are not required to accept this License, since you have not
ae83b6
+signed it.  However, nothing else grants you permission to modify or
ae83b6
+distribute the Library or its derivative works.  These actions are
ae83b6
+prohibited by law if you do not accept this License.  Therefore, by
ae83b6
+modifying or distributing the Library (or any work based on the
ae83b6
+Library), you indicate your acceptance of this License to do so, and
ae83b6
+all its terms and conditions for copying, distributing or modifying
ae83b6
+the Library or works based on it.
ae83b6
+
ae83b6
+  10. Each time you redistribute the Library (or any work based on the
ae83b6
+Library), the recipient automatically receives a license from the
ae83b6
+original licensor to copy, distribute, link with or modify the Library
ae83b6
+subject to these terms and conditions.  You may not impose any further
ae83b6
+restrictions on the recipients' exercise of the rights granted herein.
ae83b6
+You are not responsible for enforcing compliance by third parties with
ae83b6
+this License.
ae83b6
+
ae83b6
+  11. If, as a consequence of a court judgment or allegation of patent
ae83b6
+infringement or for any other reason (not limited to patent issues),
ae83b6
+conditions are imposed on you (whether by court order, agreement or
ae83b6
+otherwise) that contradict the conditions of this License, they do not
ae83b6
+excuse you from the conditions of this License.  If you cannot
ae83b6
+distribute so as to satisfy simultaneously your obligations under this
ae83b6
+License and any other pertinent obligations, then as a consequence you
ae83b6
+may not distribute the Library at all.  For example, if a patent
ae83b6
+license would not permit royalty-free redistribution of the Library by
ae83b6
+all those who receive copies directly or indirectly through you, then
ae83b6
+the only way you could satisfy both it and this License would be to
ae83b6
+refrain entirely from distribution of the Library.
ae83b6
+
ae83b6
+If any portion of this section is held invalid or unenforceable under any
ae83b6
+particular circumstance, the balance of the section is intended to apply,
ae83b6
+and the section as a whole is intended to apply in other circumstances.
ae83b6
+
ae83b6
+It is not the purpose of this section to induce you to infringe any
ae83b6
+patents or other property right claims or to contest validity of any
ae83b6
+such claims; this section has the sole purpose of protecting the
ae83b6
+integrity of the free software distribution system which is
ae83b6
+implemented by public license practices.  Many people have made
ae83b6
+generous contributions to the wide range of software distributed
ae83b6
+through that system in reliance on consistent application of that
ae83b6
+system; it is up to the author/donor to decide if he or she is willing
ae83b6
+to distribute software through any other system and a licensee cannot
ae83b6
+impose that choice.
ae83b6
+
ae83b6
+This section is intended to make thoroughly clear what is believed to
ae83b6
+be a consequence of the rest of this License.
ae83b6
+
ae83b6
+  12. If the distribution and/or use of the Library is restricted in
ae83b6
+certain countries either by patents or by copyrighted interfaces, the
ae83b6
+original copyright holder who places the Library under this License may add
ae83b6
+an explicit geographical distribution limitation excluding those countries,
ae83b6
+so that distribution is permitted only in or among countries not thus
ae83b6
+excluded.  In such case, this License incorporates the limitation as if
ae83b6
+written in the body of this License.
ae83b6
+
ae83b6
+  13. The Free Software Foundation may publish revised and/or new
ae83b6
+versions of the Lesser General Public License from time to time.
ae83b6
+Such new versions will be similar in spirit to the present version,
ae83b6
+but may differ in detail to address new problems or concerns.
ae83b6
+
ae83b6
+Each version is given a distinguishing version number.  If the Library
ae83b6
+specifies a version number of this License which applies to it and
ae83b6
+"any later version", you have the option of following the terms and
ae83b6
+conditions either of that version or of any later version published by
ae83b6
+the Free Software Foundation.  If the Library does not specify a
ae83b6
+license version number, you may choose any version ever published by
ae83b6
+the Free Software Foundation.
ae83b6
+
ae83b6
+  14. If you wish to incorporate parts of the Library into other free
ae83b6
+programs whose distribution conditions are incompatible with these,
ae83b6
+write to the author to ask for permission.  For software which is
ae83b6
+copyrighted by the Free Software Foundation, write to the Free
ae83b6
+Software Foundation; we sometimes make exceptions for this.  Our
ae83b6
+decision will be guided by the two goals of preserving the free status
ae83b6
+of all derivatives of our free software and of promoting the sharing
ae83b6
+and reuse of software generally.
ae83b6
+
ae83b6
+                            NO WARRANTY
ae83b6
+
ae83b6
+  15. BECAUSE THE LIBRARY IS LICENSED FREE OF CHARGE, THERE IS NO
ae83b6
+WARRANTY FOR THE LIBRARY, TO THE EXTENT PERMITTED BY APPLICABLE LAW.
ae83b6
+EXCEPT WHEN OTHERWISE STATED IN WRITING THE COPYRIGHT HOLDERS AND/OR
ae83b6
+OTHER PARTIES PROVIDE THE LIBRARY "AS IS" WITHOUT WARRANTY OF ANY
ae83b6
+KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE
ae83b6
+IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
ae83b6
+PURPOSE.  THE ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE
ae83b6
+LIBRARY IS WITH YOU.  SHOULD THE LIBRARY PROVE DEFECTIVE, YOU ASSUME
ae83b6
+THE COST OF ALL NECESSARY SERVICING, REPAIR OR CORRECTION.
ae83b6
+
ae83b6
+  16. IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN
ae83b6
+WRITING WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MAY MODIFY
ae83b6
+AND/OR REDISTRIBUTE THE LIBRARY AS PERMITTED ABOVE, BE LIABLE TO YOU
ae83b6
+FOR DAMAGES, INCLUDING ANY GENERAL, SPECIAL, INCIDENTAL OR
ae83b6
+CONSEQUENTIAL DAMAGES ARISING OUT OF THE USE OR INABILITY TO USE THE
ae83b6
+LIBRARY (INCLUDING BUT NOT LIMITED TO LOSS OF DATA OR DATA BEING
ae83b6
+RENDERED INACCURATE OR LOSSES SUSTAINED BY YOU OR THIRD PARTIES OR A
ae83b6
+FAILURE OF THE LIBRARY TO OPERATE WITH ANY OTHER SOFTWARE), EVEN IF
ae83b6
+SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH
ae83b6
+DAMAGES.
ae83b6
+
ae83b6
+                     END OF TERMS AND CONDITIONS
ae83b6
+
ae83b6
+           How to Apply These Terms to Your New Libraries
ae83b6
+
ae83b6
+  If you develop a new library, and you want it to be of the greatest
ae83b6
+possible use to the public, we recommend making it free software that
ae83b6
+everyone can redistribute and change.  You can do so by permitting
ae83b6
+redistribution under these terms (or, alternatively, under the terms of the
ae83b6
+ordinary General Public License).
ae83b6
+
ae83b6
+  To apply these terms, attach the following notices to the library.  It is
ae83b6
+safest to attach them to the start of each source file to most effectively
ae83b6
+convey the exclusion of warranty; and each file should have at least the
ae83b6
+"copyright" line and a pointer to where the full notice is found.
ae83b6
+
ae83b6
+    <one line to give the library's name and a brief idea of what it does.>
ae83b6
+    Copyright (C) <year>  <name of author>
ae83b6
+
ae83b6
+    This library is free software; you can redistribute it and/or
ae83b6
+    modify it under the terms of the GNU Lesser General Public
ae83b6
+    License as published by the Free Software Foundation; either
ae83b6
+    version 2.1 of the License, or (at your option) any later version.
ae83b6
+
ae83b6
+    This library is distributed in the hope that it will be useful,
ae83b6
+    but WITHOUT ANY WARRANTY; without even the implied warranty of
ae83b6
+    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
ae83b6
+    Lesser General Public License for more details.
ae83b6
+
ae83b6
+    You should have received a copy of the GNU Lesser General Public
ae83b6
+    License along with this library; if not, write to the Free Software
ae83b6
+    Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
ae83b6
+
ae83b6
+Also add information on how to contact you by electronic and paper mail.
ae83b6
+
ae83b6
+You should also get your employer (if you work as a programmer) or your
ae83b6
+school, if any, to sign a "copyright disclaimer" for the library, if
ae83b6
+necessary.  Here is a sample; alter the names:
ae83b6
+
ae83b6
+  Yoyodyne, Inc., hereby disclaims all copyright interest in the
ae83b6
+  library `Frob' (a library for tweaking knobs) written by James Random Hacker.
ae83b6
+
ae83b6
+  <signature of Ty Coon>, 1 April 1990
ae83b6
+  Ty Coon, President of Vice
ae83b6
+
ae83b6
+That's all there is to it!
ae83b6
diff --git a/gnome-pty-helper/Makefile.am b/gnome-pty-helper/Makefile.am
ae83b6
new file mode 100644
ae83b6
index 000000000000..10a1c896e958
ae83b6
--- /dev/null
ae83b6
+++ b/gnome-pty-helper/Makefile.am
ae83b6
@@ -0,0 +1,32 @@
ae83b6
+libexec_PROGRAMS = gnome-pty-helper
ae83b6
+
ae83b6
+gnome_pty_helper_SOURCES = \
ae83b6
+	gnome-pty.h \
ae83b6
+	gnome-login-support.c \
ae83b6
+	gnome-login-support.h \
ae83b6
+	gnome-pty-helper.c \
ae83b6
+	gnome-utmp.c
ae83b6
+
ae83b6
+gnome_pty_helper_CFLAGS = $(SUID_CFLAGS) $(AM_CFLAGS)
ae83b6
+gnome_pty_helper_LDFLAGS = $(SUID_LDFLAGS) $(AM_LDFLAGS)
ae83b6
+
ae83b6
+install-exec-hook:
ae83b6
+	chown root.utmp $(DESTDIR)$(libexecdir)/gnome-pty-helper || true
ae83b6
+	chmod g+s       $(DESTDIR)$(libexecdir)/gnome-pty-helper || true
ae83b6
+
ae83b6
+MAINTAINERCLEANFILES = \
ae83b6
+		$(srcdir)/INSTALL \
ae83b6
+		$(srcdir)/aclocal.m4 \
ae83b6
+		$(srcdir)/config.guess \
ae83b6
+		$(srcdir)/config.h.in \
ae83b6
+		$(srcdir)/config.sub \
ae83b6
+		$(srcdir)/configure \
ae83b6
+		$(srcdir)/depcomp \
ae83b6
+		$(srcdir)/install-sh \
ae83b6
+		$(srcdir)/mkinstalldirs \
ae83b6
+		$(srcdir)/missing \
ae83b6
+		$(srcdir)/omf.make \
ae83b6
+		$(srcdir)/xmldocs.make \
ae83b6
+		`find "$(srcdir)" -type f -name Makefile.in -print`
ae83b6
+
ae83b6
+-include $(top_srcdir)/git.mk
ae83b6
diff --git a/gnome-pty-helper/README b/gnome-pty-helper/README
ae83b6
new file mode 100644
ae83b6
index 000000000000..b760129a0dde
ae83b6
--- /dev/null
ae83b6
+++ b/gnome-pty-helper/README
ae83b6
@@ -0,0 +1,9 @@
ae83b6
+This is some bits from libzvt CVS extracted to make a more-or-less standalone
ae83b6
+gnome-pty-helper kit.  Presumably it is licensed under the LGPL, as libzvt
ae83b6
+itself is.
ae83b6
+
ae83b6
+Note that for utmp/wtmp/lastlog logging to work properly, you *must* ensure
ae83b6
+that gnome-pty-helper is installed with the correct permissions.  By default,
ae83b6
+gnome-pty-helper is installed setgid "utmp".  If your system does not have a
ae83b6
+group named "utmp" (or if this group does not have write access to the
ae83b6
+corresponding log files), then it will probably need to be made setuid "root".
ae83b6
diff --git a/gnome-pty-helper/acinclude.m4 b/gnome-pty-helper/acinclude.m4
ae83b6
new file mode 100644
ae83b6
index 000000000000..6e8eadbc51f6
ae83b6
--- /dev/null
ae83b6
+++ b/gnome-pty-helper/acinclude.m4
ae83b6
@@ -0,0 +1,244 @@
ae83b6
+# Checks for availability of various utmp fields
ae83b6
+#
ae83b6
+# Original code by Bernhard Rosenkraenzer (bero@linux.net.eu.org), 1998.
ae83b6
+# Modifications by Timur Bakeyev (timur@gnu.org), 1999.
ae83b6
+# Patched from http://bugzilla.gnome.org/show_bug.cgi?id=93774
ae83b6
+#
ae83b6
+
ae83b6
+dnl GPH_CHECK_UTMP()
ae83b6
+dnl Test for presence of the field and define HAVE_UT_UT_field macro
ae83b6
+dnl
ae83b6
+
ae83b6
+AC_DEFUN([GPH_CHECK_UTMP],[
ae83b6
+
ae83b6
+AC_CHECK_HEADERS(sys/time.h utmp.h utmpx.h)
ae83b6
+AC_HEADER_TIME
ae83b6
+
ae83b6
+if test "$ac_cv_header_utmpx_h" = "yes"; then
ae83b6
+    AC_DEFINE(UTMP,[struct utmpx],[Define to the name of a structure which holds utmp data.])
ae83b6
+else
ae83b6
+    AC_DEFINE(UTMP,[struct utmp],[Define to the name of a structure which holds utmp data.])
ae83b6
+fi
ae83b6
+
ae83b6
+dnl some systems (BSD4.4-like) require time.h to be included before utmp.h :/
ae83b6
+AC_MSG_CHECKING(for ut_host field in the utmp structure)
ae83b6
+AC_TRY_COMPILE([#ifdef TIME_WITH_SYS_TIME
ae83b6
+#include <sys/time.h>
ae83b6
+#include <time.h>
ae83b6
+#else
ae83b6
+#ifdef HAVE_SYS_TIME_H
ae83b6
+#include <sys/time.h>
ae83b6
+#else
ae83b6
+#include <time.h>
ae83b6
+#endif
ae83b6
+#endif
ae83b6
+#ifdef HAVE_UTMP_H
ae83b6
+#include <utmp.h>
ae83b6
+#endif
ae83b6
+#ifdef HAVE_UTMPX_H
ae83b6
+#include <utmpx.h>
ae83b6
+#endif],[UTMP ut; char *p; p=ut.ut_host;],result=yes,result=no)
ae83b6
+if test "$result" = "yes"; then
ae83b6
+  AC_DEFINE(HAVE_UT_UT_HOST,1,[Define if your utmp struct contains a ut_host field.])
ae83b6
+fi
ae83b6
+AC_MSG_RESULT($result)
ae83b6
+
ae83b6
+AC_MSG_CHECKING(for ut_pid field in the utmp structure)
ae83b6
+AC_TRY_COMPILE([#ifdef TIME_WITH_SYS_TIME
ae83b6
+#include <sys/time.h>
ae83b6
+#include <time.h>
ae83b6
+#else
ae83b6
+#ifdef HAVE_SYS_TIME_H
ae83b6
+#include <sys/time.h>
ae83b6
+#else
ae83b6
+#include <time.h>
ae83b6
+#endif
ae83b6
+#endif
ae83b6
+#ifdef HAVE_UTMP_H
ae83b6
+#include <utmp.h>
ae83b6
+#endif
ae83b6
+#ifdef HAVE_UTMPX_H
ae83b6
+#include <utmpx.h>
ae83b6
+#endif],[UTMP ut; int i; i=ut.ut_pid;],result=yes,result=no)
ae83b6
+if test "$result" = "yes"; then
ae83b6
+  AC_DEFINE(HAVE_UT_UT_PID,1,[Define if your utmp struct contains a ut_pid field.])
ae83b6
+fi
ae83b6
+AC_MSG_RESULT($result)
ae83b6
+
ae83b6
+AC_MSG_CHECKING(for ut_id field in the utmp structure)
ae83b6
+AC_TRY_COMPILE([#ifdef TIME_WITH_SYS_TIME
ae83b6
+#include <sys/time.h>
ae83b6
+#include <time.h>
ae83b6
+#else
ae83b6
+#ifdef HAVE_SYS_TIME_H
ae83b6
+#include <sys/time.h>
ae83b6
+#else
ae83b6
+#include <time.h>
ae83b6
+#endif
ae83b6
+#endif
ae83b6
+#ifdef HAVE_UTMP_H
ae83b6
+#include <utmp.h>
ae83b6
+#endif
ae83b6
+#ifdef HAVE_UTMPX_H
ae83b6
+#include <utmpx.h>
ae83b6
+#endif],[UTMP ut; char *p; p=ut.ut_id;],result=yes,result=no)
ae83b6
+if test "$result" = "yes"; then
ae83b6
+  AC_DEFINE(HAVE_UT_UT_ID,1,[Define if your utmp struct contains a ut_id field.])
ae83b6
+fi
ae83b6
+AC_MSG_RESULT($result)
ae83b6
+
ae83b6
+AC_MSG_CHECKING(for ut_name field in the utmp structure)
ae83b6
+AC_TRY_COMPILE([#ifdef TIME_WITH_SYS_TIME
ae83b6
+#include <sys/time.h>
ae83b6
+#include <time.h>
ae83b6
+#else
ae83b6
+#ifdef HAVE_SYS_TIME_H
ae83b6
+#include <sys/time.h>
ae83b6
+#else
ae83b6
+#include <time.h>
ae83b6
+#endif
ae83b6
+#endif
ae83b6
+#ifdef HAVE_UTMP_H
ae83b6
+#include <utmp.h>
ae83b6
+#endif
ae83b6
+#ifdef HAVE_UTMPX_H
ae83b6
+#include <utmpx.h>
ae83b6
+#endif],[UTMP ut; char *p; p=ut.ut_name;],result=yes,result=no)
ae83b6
+if test "$result" = "yes"; then
ae83b6
+  AC_DEFINE(HAVE_UT_UT_NAME,1,[Define if your utmp struct contains a ut_name field.])
ae83b6
+fi
ae83b6
+AC_MSG_RESULT($result)
ae83b6
+
ae83b6
+AC_MSG_CHECKING(for ut_type field in the utmp structure)
ae83b6
+AC_TRY_COMPILE([#ifdef TIME_WITH_SYS_TIME
ae83b6
+#include <sys/time.h>
ae83b6
+#include <time.h>
ae83b6
+#else
ae83b6
+#ifdef HAVE_SYS_TIME_H
ae83b6
+#include <sys/time.h>
ae83b6
+#else
ae83b6
+#include <time.h>
ae83b6
+#endif
ae83b6
+#endif
ae83b6
+#ifdef HAVE_UTMP_H
ae83b6
+#include <utmp.h>
ae83b6
+#endif
ae83b6
+#ifdef HAVE_UTMPX_H
ae83b6
+#include <utmpx.h>
ae83b6
+#endif],[UTMP ut; int i; i=(int) ut.ut_type;],result=yes,result=no)
ae83b6
+if test "$result" = "yes"; then
ae83b6
+  AC_DEFINE(HAVE_UT_UT_TYPE,1,[Define if your utmp struct contains a ut_type field.])
ae83b6
+fi
ae83b6
+AC_MSG_RESULT($result)
ae83b6
+
ae83b6
+AC_MSG_CHECKING(for ut_exit.e_termination field in the utmp structure)
ae83b6
+AC_TRY_COMPILE([#ifdef TIME_WITH_SYS_TIME
ae83b6
+#include <sys/time.h>
ae83b6
+#include <time.h>
ae83b6
+#else
ae83b6
+#ifdef HAVE_SYS_TIME_H
ae83b6
+#include <sys/time.h>
ae83b6
+#else
ae83b6
+#include <time.h>
ae83b6
+#endif
ae83b6
+#endif
ae83b6
+#ifdef HAVE_UTMP_H
ae83b6
+#include <utmp.h>
ae83b6
+#endif
ae83b6
+#ifdef HAVE_UTMPX_H
ae83b6
+#include <utmpx.h>
ae83b6
+#endif],[UTMP ut; ut.ut_exit.e_termination=0;],result=yes,result=no)
ae83b6
+if test "$result" = "yes"; then
ae83b6
+  AC_DEFINE(HAVE_UT_UT_EXIT_E_TERMINATION,1,[Define if your utmp struct contains a ut_exit.e_termination field.])
ae83b6
+fi
ae83b6
+AC_MSG_RESULT($result)
ae83b6
+
ae83b6
+AC_MSG_CHECKING(for ut_user field in the utmp structure)
ae83b6
+AC_TRY_COMPILE([#ifdef TIME_WITH_SYS_TIME
ae83b6
+#include <sys/time.h>
ae83b6
+#include <time.h>
ae83b6
+#else
ae83b6
+#ifdef HAVE_SYS_TIME_H
ae83b6
+#include <sys/time.h>
ae83b6
+#else
ae83b6
+#include <time.h>
ae83b6
+#endif
ae83b6
+#endif
ae83b6
+#ifdef HAVE_UTMP_H
ae83b6
+#include <utmp.h>
ae83b6
+#endif
ae83b6
+#ifdef HAVE_UTMPX_H
ae83b6
+#include <utmpx.h>
ae83b6
+#endif],[UTMP ut; char *p; p=ut.ut_user;],result=yes,result=no)
ae83b6
+if test "$result" = "yes"; then
ae83b6
+  AC_DEFINE(HAVE_UT_UT_USER,1,[Define if your utmp struct contains a ut_user field.])
ae83b6
+fi
ae83b6
+AC_MSG_RESULT($result)
ae83b6
+
ae83b6
+AC_MSG_CHECKING(for ut_time field in the utmp structure)
ae83b6
+AC_TRY_COMPILE([#ifdef TIME_WITH_SYS_TIME
ae83b6
+#include <sys/time.h>
ae83b6
+#include <time.h>
ae83b6
+#else
ae83b6
+#ifdef HAVE_SYS_TIME_H
ae83b6
+#include <sys/time.h>
ae83b6
+#else
ae83b6
+#include <time.h>
ae83b6
+#endif
ae83b6
+#endif
ae83b6
+#ifdef HAVE_UTMP_H
ae83b6
+#include <utmp.h>
ae83b6
+#endif
ae83b6
+#ifdef HAVE_UTMPX_H
ae83b6
+#include <utmpx.h>
ae83b6
+#endif],[UTMP ut; ut.ut_time=0;],result=yes,result=no)
ae83b6
+if test "$result" = "yes"; then
ae83b6
+  AC_DEFINE(HAVE_UT_UT_TIME,1,[Define if your utmp struct contains a ut_time field.])
ae83b6
+fi
ae83b6
+AC_MSG_RESULT($result)
ae83b6
+
ae83b6
+AC_MSG_CHECKING(for ut_tv field in the utmp structure)
ae83b6
+AC_TRY_COMPILE([#ifdef TIME_WITH_SYS_TIME
ae83b6
+#include <sys/time.h>
ae83b6
+#include <time.h>
ae83b6
+#else
ae83b6
+#ifdef HAVE_SYS_TIME_H
ae83b6
+#include <sys/time.h>
ae83b6
+#else
ae83b6
+#include <time.h>
ae83b6
+#endif
ae83b6
+#endif
ae83b6
+#ifdef HAVE_UTMP_H
ae83b6
+#include <utmp.h>
ae83b6
+#endif
ae83b6
+#ifdef HAVE_UTMPX_H
ae83b6
+#include <utmpx.h>
ae83b6
+#endif],[UTMP ut; ut.ut_tv.tv_sec=0; ut.ut_tv.tv_usec=0; ],result=yes,result=no)
ae83b6
+if test "$result" = "yes"; then
ae83b6
+  AC_DEFINE(HAVE_UT_UT_TV,1,[Define if your utmp struct contains a ut_tv field.])
ae83b6
+fi
ae83b6
+AC_MSG_RESULT($result)
ae83b6
+
ae83b6
+AC_MSG_CHECKING(for ut_syslen field in the utmp structure)
ae83b6
+AC_TRY_COMPILE([#ifdef TIME_WITH_SYS_TIME
ae83b6
+#include <sys/time.h>
ae83b6
+#include <time.h>
ae83b6
+#else
ae83b6
+#ifdef HAVE_SYS_TIME_H
ae83b6
+#include <sys/time.h>
ae83b6
+#else
ae83b6
+#include <time.h>
ae83b6
+#endif
ae83b6
+#endif
ae83b6
+#ifdef HAVE_UTMP_H
ae83b6
+#include <utmp.h>
ae83b6
+#endif
ae83b6
+#ifdef HAVE_UTMPX_H
ae83b6
+#include <utmpx.h>
ae83b6
+#endif],[UTMP ut; ut.ut_syslen=0;],result=yes,result=no)
ae83b6
+if test "$result" = "yes"; then
ae83b6
+  AC_DEFINE(HAVE_UT_UT_SYSLEN,1,[Define if your utmp struct contains a ut_syslen field.])
ae83b6
+fi
ae83b6
+AC_MSG_RESULT($result)
ae83b6
+
ae83b6
+])
ae83b6
diff --git a/gnome-pty-helper/configure.ac b/gnome-pty-helper/configure.ac
ae83b6
new file mode 100644
ae83b6
index 000000000000..c9f0aa460029
ae83b6
--- /dev/null
ae83b6
+++ b/gnome-pty-helper/configure.ac
ae83b6
@@ -0,0 +1,33 @@
ae83b6
+AC_INIT([gnome-pty-helper],[1.95.0],[http://bugzilla.gnome.org/enter_bug.cgi?product=vte])
ae83b6
+
ae83b6
+AM_INIT_AUTOMAKE([1.9 foreign no-dist-gzip dist-bzip2])
ae83b6
+
ae83b6
+AC_CONFIG_HEADERS([config.h])
ae83b6
+AC_CONFIG_SRCDIR([gnome-pty.h])
ae83b6
+
ae83b6
+AC_PROG_CC
ae83b6
+AC_USE_SYSTEM_EXTENSIONS
ae83b6
+AC_ISC_POSIX
ae83b6
+AC_PROG_CC
ae83b6
+AC_STDC_HEADERS
ae83b6
+
ae83b6
+AM_MAINTAINER_MODE([enable])
ae83b6
+
ae83b6
+AC_CHECK_HEADERS(sys/syslimits.h sys/time.h sys/types.h sys/un.h alloca.h lastlog.h libutil.h paths.h pty.h stropts.h termios.h ttyent.h util.h utmp.h utmpx.h pty.h util.h libutil.h ttyent.h)
ae83b6
+have_openpty=0
ae83b6
+AC_CHECK_LIB(c,grantpt,true,[AC_CHECK_LIB(pt,grantpt)])
ae83b6
+AC_CHECK_LIB(c,openpty,true,[AC_CHECK_LIB(util,openpty)])
ae83b6
+AC_CHECK_LIB(c,sendmsg,true,[AC_CHECK_LIB(socket,sendmsg,LIBS="$LIBS -lsocket -lnsl",,-lnsl)])
ae83b6
+AC_CHECK_FUNCS(endutent fcntl forkpty getttyent getutent getutmpx grantpt flock login_tty openpty revoke sendmsg seteuid setreuid setutent strrchr updwtmp updwtmpx utmpname utmpxname)
ae83b6
+GPH_CHECK_UTMP
ae83b6
+
ae83b6
+AC_CHECK_HEADERS(stropts.h)
ae83b6
+
ae83b6
+AC_ARG_VAR([SUID_CFLAGS],
ae83b6
+	   [CFLAGS used for binaries which are usually with the suid bit])
ae83b6
+AC_ARG_VAR([SUID_LDFLAGS],
ae83b6
+	   [LDFLAGS used for binaries which are usually with the suid bit])
ae83b6
+
ae83b6
+AC_CONFIG_FILES([Makefile])
ae83b6
+
ae83b6
+AC_OUTPUT
ae83b6
diff --git a/gnome-pty-helper/git.mk b/gnome-pty-helper/git.mk
ae83b6
new file mode 100644
ae83b6
index 000000000000..9e5123cf4490
ae83b6
--- /dev/null
ae83b6
+++ b/gnome-pty-helper/git.mk
ae83b6
@@ -0,0 +1 @@
ae83b6
+include $(top_srcdir)/../git.mk
ae83b6
diff --git a/gnome-pty-helper/gnome-login-support.c b/gnome-pty-helper/gnome-login-support.c
ae83b6
new file mode 100644
ae83b6
index 000000000000..c1a3ac67f1f5
ae83b6
--- /dev/null
ae83b6
+++ b/gnome-pty-helper/gnome-login-support.c
ae83b6
@@ -0,0 +1,380 @@
ae83b6
+/*
ae83b6
+ * gnome-login-support.c:
ae83b6
+ *    Replacement for systems that lack login_tty, open_pty and forkpty
ae83b6
+ *
ae83b6
+ * Author:
ae83b6
+ *    Miguel de Icaza (miguel@gnu.org)
ae83b6
+ *
ae83b6
+ *
ae83b6
+ */
ae83b6
+#include <config.h>
ae83b6
+#include <termios.h>
ae83b6
+#include <unistd.h>
ae83b6
+#include <stdio.h>
ae83b6
+#include <stdlib.h>
ae83b6
+#include <fcntl.h>
ae83b6
+#include <string.h>
ae83b6
+#include <sys/ioctl.h>
ae83b6
+#include <termios.h>
ae83b6
+#include <stdlib.h>
ae83b6
+#include <sys/stat.h>
ae83b6
+#include <errno.h>
ae83b6
+#include <grp.h>
ae83b6
+#include <sys/types.h>
ae83b6
+#include "gnome-login-support.h"
ae83b6
+
ae83b6
+/*
ae83b6
+ * HAVE_OPENPTY => HAVE_FORKPTY
ae83b6
+ */
ae83b6
+
ae83b6
+#ifndef HAVE_LOGIN_TTY
ae83b6
+int
ae83b6
+login_tty (int fd)
ae83b6
+{
ae83b6
+	pid_t pid = getpid ();
ae83b6
+
ae83b6
+	/* Create the session */
ae83b6
+	setsid ();
ae83b6
+
ae83b6
+#ifdef TIOCSCTTY
ae83b6
+	if (ioctl (fd, TIOCSCTTY, 0) == -1)
ae83b6
+		return -1;
ae83b6
+#else /* !TIOCSTTY */
ae83b6
+        /* Hackery to set controlling tty on SVR4 -
ae83b6
+           on SVR4 the first terminal we open after sesid()
ae83b6
+           becomes our controlling terminal, thus we must
ae83b6
+           find the name of, open, and re-close the tty
ae83b6
+           since we already have it open at this point. */
ae83b6
+        {
ae83b6
+                char *ctty;
ae83b6
+                int ct_fdes;
ae83b6
+
ae83b6
+                ctty = ttyname(fd);
ae83b6
+                ct_fdes = open(ctty, O_RDWR);
ae83b6
+                close(ct_fdes);
ae83b6
+        }
ae83b6
+#endif /* !TIOCSTTY */
ae83b6
+
ae83b6
+#if defined (_POSIX_VERSION) || defined (__svr4__)
ae83b6
+	tcsetpgrp (0, pid);
ae83b6
+#elif defined (TIOCSPGRP)
ae83b6
+	ioctl (0, TIOCSPGRP, &pid;;
ae83b6
+#endif
ae83b6
+
ae83b6
+	dup2 (fd, 0);
ae83b6
+	dup2 (fd, 1);
ae83b6
+	dup2 (fd, 2);
ae83b6
+	if (fd > 2)
ae83b6
+		close (fd);
ae83b6
+
ae83b6
+	return 0;
ae83b6
+}
ae83b6
+#endif
ae83b6
+
ae83b6
+#ifndef HAVE_OPENPTY
ae83b6
+static int
ae83b6
+pty_open_master_bsd (char *pty_name, int *used_bsd)
ae83b6
+{
ae83b6
+	int pty_master;
ae83b6
+	char *ptr1, *ptr2;
ae83b6
+
ae83b6
+	*used_bsd = 1;
ae83b6
+
ae83b6
+	strcpy (pty_name, "/dev/ptyXX");
ae83b6
+	for (ptr1 = "pqrstuvwxyzPQRST"; *ptr1; ++ptr1)
ae83b6
+	{
ae83b6
+		pty_name [8] = *ptr1;
ae83b6
+		for (ptr2 = "0123456789abcdef"; *ptr2; ++ptr2)
ae83b6
+		{
ae83b6
+			pty_name [9] = *ptr2;
ae83b6
+
ae83b6
+			/* Try to open master */
ae83b6
+			if ((pty_master = open (pty_name, O_RDWR)) == -1) {
ae83b6
+				if (errno == ENOENT)  /* Different from EIO */
ae83b6
+					return -1;    /* Out of pty devices */
ae83b6
+				else
ae83b6
+					continue;      /* Try next pty device */
ae83b6
+			}
ae83b6
+			pty_name [5] = 't';	       /* Change "pty" to "tty" */
ae83b6
+			if (access (pty_name, (R_OK | W_OK))){
ae83b6
+				close (pty_master);
ae83b6
+				pty_name [5] = 'p';
ae83b6
+				continue;
ae83b6
+			}
ae83b6
+			return pty_master;
ae83b6
+		}
ae83b6
+	}
ae83b6
+	return -1;  /* Ran out of pty devices */
ae83b6
+}
ae83b6
+
ae83b6
+static int
ae83b6
+pty_open_slave_bsd (const char *pty_name)
ae83b6
+{
ae83b6
+	int pty_slave;
ae83b6
+	struct group *group_info = getgrnam ("tty");
ae83b6
+
ae83b6
+	if (group_info != NULL)
ae83b6
+	{
ae83b6
+		/* The following two calls will only succeed if we are root */
ae83b6
+
ae83b6
+		chown (pty_name, getuid (), group_info->gr_gid);
ae83b6
+		chmod (pty_name, S_IRUSR | S_IWUSR | S_IWGRP);
ae83b6
+	}
ae83b6
+	else
ae83b6
+	{
ae83b6
+		chown (pty_name, getuid (), -1);
ae83b6
+		chmod (pty_name, S_IRUSR | S_IWUSR | S_IWGRP);
ae83b6
+	}
ae83b6
+
ae83b6
+#ifdef HAVE_REVOKE
ae83b6
+	revoke (pty_name);
ae83b6
+#endif
ae83b6
+
ae83b6
+	if ((pty_slave = open (pty_name, O_RDWR)) == -1){
ae83b6
+		return -1;
ae83b6
+	}
ae83b6
+
ae83b6
+	return pty_slave;
ae83b6
+}
ae83b6
+
ae83b6
+/* SystemVish pty opening */
ae83b6
+#if defined (HAVE_GRANTPT)
ae83b6
+
ae83b6
+#ifdef HAVE_STROPTS_H
ae83b6
+#    include <stropts.h>
ae83b6
+#endif
ae83b6
+
ae83b6
+static int
ae83b6
+pty_open_slave (const char *pty_name)
ae83b6
+{
ae83b6
+	int pty_slave = open (pty_name, O_RDWR);
ae83b6
+
ae83b6
+	if (pty_slave == -1)
ae83b6
+		return -1;
ae83b6
+
ae83b6
+#ifdef HAVE_STROPTS_H
ae83b6
+#if !defined(__osf__)
ae83b6
+	if (!ioctl (pty_slave, I_FIND, "ptem"))
ae83b6
+		if (ioctl (pty_slave, I_PUSH, "ptem") == -1){
ae83b6
+			close (pty_slave);
ae83b6
+			return -1;
ae83b6
+		}
ae83b6
+
ae83b6
+    if (!ioctl (pty_slave, I_FIND, "ldterm"))
ae83b6
+	    if (ioctl (pty_slave, I_PUSH, "ldterm") == -1){
ae83b6
+		    close (pty_slave);
ae83b6
+		    return -1;
ae83b6
+	    }
ae83b6
+
ae83b6
+#if !defined(sgi) && !defined(__sgi)
ae83b6
+    if (!ioctl (pty_slave, I_FIND, "ttcompat"))
ae83b6
+	    if (ioctl (pty_slave, I_PUSH, "ttcompat") == -1)
ae83b6
+	    {
ae83b6
+		    perror ("ioctl (pty_slave, I_PUSH, \"ttcompat\")");
ae83b6
+		    close (pty_slave);
ae83b6
+		    return -1;
ae83b6
+	    }
ae83b6
+#endif /* sgi || __sgi */
ae83b6
+#endif /* __osf__ */
ae83b6
+#endif /* HAVE_STROPTS_H */
ae83b6
+
ae83b6
+    return pty_slave;
ae83b6
+}
ae83b6
+
ae83b6
+static int
ae83b6
+pty_open_master (char *pty_name, int *used_bsd)
ae83b6
+{
ae83b6
+	int pty_master;
ae83b6
+	char *slave_name;
ae83b6
+
ae83b6
+	strcpy (pty_name, "/dev/ptmx");
ae83b6
+
ae83b6
+	pty_master = open (pty_name, O_RDWR);
ae83b6
+
ae83b6
+	if ((pty_master == -1) && (errno == ENOENT)) {
ae83b6
+		strcpy (pty_name, "/dev/ptc"); /* AIX */
ae83b6
+		pty_master = open (pty_name, O_RDWR);
ae83b6
+	}
ae83b6
+
ae83b6
+	/*
ae83b6
+	 * Try BSD open, this is needed for Linux which
ae83b6
+	 * might have Unix98 devices but no kernel support
ae83b6
+	 * for those.
ae83b6
+	 */
ae83b6
+	if (pty_master == -1) {
ae83b6
+		*used_bsd = 1;
ae83b6
+		return pty_open_master_bsd (pty_name, used_bsd);
ae83b6
+	}
ae83b6
+	*used_bsd = 0;
ae83b6
+
ae83b6
+	if (grantpt (pty_master) == -1 || unlockpt (pty_master) == -1) {
ae83b6
+		close (pty_master);
ae83b6
+		return -1;
ae83b6
+	}
ae83b6
+	if ((slave_name = ptsname (pty_master)) == NULL){
ae83b6
+		close (pty_master);
ae83b6
+		return -1;
ae83b6
+	}
ae83b6
+	strcpy (pty_name, slave_name);
ae83b6
+	return pty_master;
ae83b6
+}
ae83b6
+#else
ae83b6
+#    define pty_open_master pty_open_master_bsd
ae83b6
+#    define pty_open_slave  pty_open_slave_bsd
ae83b6
+#endif
ae83b6
+
ae83b6
+int
ae83b6
+openpty (int *master_fd, int *slave_fd, char *name,
ae83b6
+	 struct termios *termp, struct winsize *winp)
ae83b6
+{
ae83b6
+	int pty_master, pty_slave, used_bsd = 0;
ae83b6
+	struct group *group_info;
ae83b6
+	char line [256];
ae83b6
+
ae83b6
+	pty_master = pty_open_master (line, &used_bsd);
ae83b6
+	fcntl (pty_master, F_SETFD, FD_CLOEXEC);
ae83b6
+
ae83b6
+	if (pty_master == -1)
ae83b6
+		return -1;
ae83b6
+
ae83b6
+	group_info = getgrnam ("tty");
ae83b6
+
ae83b6
+	if (group_info != NULL){
ae83b6
+		chown (line, getuid (), group_info->gr_gid);
ae83b6
+		chmod (line, S_IRUSR | S_IWUSR | S_IWGRP);
ae83b6
+	} else {
ae83b6
+		chown (line, getuid (), -1);
ae83b6
+		chmod (line, S_IRUSR | S_IWUSR | S_IWGRP);
ae83b6
+	}
ae83b6
+
ae83b6
+#ifdef HAVE_REVOKE
ae83b6
+	revoke (line);
ae83b6
+#endif
ae83b6
+
ae83b6
+	/* Open slave side */
ae83b6
+	if (used_bsd)
ae83b6
+		pty_slave = pty_open_slave_bsd (line);
ae83b6
+	else
ae83b6
+		pty_slave = pty_open_slave (line);
ae83b6
+
ae83b6
+	if (pty_slave == -1){
ae83b6
+		close (pty_master);
ae83b6
+
ae83b6
+		errno = ENOENT;
ae83b6
+		return -1;
ae83b6
+	}
ae83b6
+	fcntl (pty_slave, F_SETFD, FD_CLOEXEC);
ae83b6
+
ae83b6
+	*master_fd = pty_master;
ae83b6
+	*slave_fd  = pty_slave;
ae83b6
+
ae83b6
+	if (termp)
ae83b6
+		tcsetattr (pty_slave, TCSAFLUSH, termp);
ae83b6
+
ae83b6
+	if (winp)
ae83b6
+		ioctl (pty_slave, TIOCSWINSZ, winp);
ae83b6
+
ae83b6
+	if (name)
ae83b6
+		strcpy (name, line);
ae83b6
+
ae83b6
+	return 0;
ae83b6
+}
ae83b6
+
ae83b6
+pid_t
ae83b6
+forkpty (int *master_fd, char *name, struct termios *termp, struct winsize *winp)
ae83b6
+{
ae83b6
+	int master, slave;
ae83b6
+	pid_t pid;
ae83b6
+
ae83b6
+	if (openpty (&master, &slave, name, termp, winp) == -1)
ae83b6
+		return -1;
ae83b6
+
ae83b6
+	pid = fork ();
ae83b6
+
ae83b6
+	if (pid == -1)
ae83b6
+		return -1;
ae83b6
+
ae83b6
+	/* Child */
ae83b6
+	if (pid == 0){
ae83b6
+		close (master);
ae83b6
+		login_tty (slave);
ae83b6
+	} else {
ae83b6
+		*master_fd = master;
ae83b6
+		close (slave);
ae83b6
+	}
ae83b6
+
ae83b6
+	return pid;
ae83b6
+}
ae83b6
+#endif /* HAVE_OPENPTY */
ae83b6
+
ae83b6
+int
ae83b6
+n_read (int fd, void *buf, int count)
ae83b6
+{
ae83b6
+	int n = 0, i;
ae83b6
+	char *buffer;
ae83b6
+
ae83b6
+	buffer = (char*) buf;
ae83b6
+	while (n < count) {
ae83b6
+		i = read (fd, buffer + n, count - n);
ae83b6
+		switch (i) {
ae83b6
+		case -1:
ae83b6
+			switch (errno) {
ae83b6
+			case EINTR:
ae83b6
+			case EAGAIN:
ae83b6
+#ifdef ERESTART
ae83b6
+			case ERESTART:
ae83b6
+#endif
ae83b6
+				/* suppress these errors */
ae83b6
+				break;
ae83b6
+			default:
ae83b6
+				return -1;
ae83b6
+				break;
ae83b6
+			}
ae83b6
+			break;
ae83b6
+		case 0:
ae83b6
+			return n;
ae83b6
+			break;
ae83b6
+		default:
ae83b6
+			n += i;
ae83b6
+			break;
ae83b6
+		}
ae83b6
+	}
ae83b6
+
ae83b6
+	return n;
ae83b6
+}
ae83b6
+
ae83b6
+int
ae83b6
+n_write (int fd, const void *buf, int count)
ae83b6
+{
ae83b6
+	int n = 0, i;
ae83b6
+	const char *buffer;
ae83b6
+
ae83b6
+	buffer = (char*) buf;
ae83b6
+	while (n < count) {
ae83b6
+		i = write (fd, buffer + n, count - n);
ae83b6
+		switch (i) {
ae83b6
+		case -1:
ae83b6
+			switch (errno) {
ae83b6
+			case EINTR:
ae83b6
+			case EAGAIN:
ae83b6
+#ifdef ERESTART
ae83b6
+			case ERESTART:
ae83b6
+#endif
ae83b6
+				/* suppress these errors */
ae83b6
+				break;
ae83b6
+			default:
ae83b6
+				return -1;
ae83b6
+				break;
ae83b6
+			}
ae83b6
+			break;
ae83b6
+		case 0:
ae83b6
+			return n;
ae83b6
+			break;
ae83b6
+		default:
ae83b6
+			n += i;
ae83b6
+			break;
ae83b6
+		}
ae83b6
+	}
ae83b6
+
ae83b6
+	return n;
ae83b6
+}
ae83b6
diff --git a/gnome-pty-helper/gnome-login-support.h b/gnome-pty-helper/gnome-login-support.h
ae83b6
new file mode 100644
ae83b6
index 000000000000..cc80a46ff3b1
ae83b6
--- /dev/null
ae83b6
+++ b/gnome-pty-helper/gnome-login-support.h
ae83b6
@@ -0,0 +1,39 @@
ae83b6
+#ifndef _GNOME_LOGIN_SUPPORT_H
ae83b6
+#define _GNOME_LOGIN_SUPPORT_H
ae83b6
+
ae83b6
+#ifdef HAVE_OPENPTY
ae83b6
+#if defined(HAVE_PTY_H)
ae83b6
+#    include <pty.h>
ae83b6
+#elif defined(HAVE_UTIL_H) /* OpenBSD */
ae83b6
+#    include <util.h>
ae83b6
+#elif defined(HAVE_LIBUTIL_H) /* FreeBSD */
ae83b6
+#    include <libutil.h>
ae83b6
+#elif defined(HAVE_LIBUTIL) /* BSDI has libutil, but no libutil.h */
ae83b6
+/* Avoid pulling in all the include files for no need */
ae83b6
+struct termios;
ae83b6
+struct winsize;
ae83b6
+struct utmp;
ae83b6
+	    
ae83b6
+void login (struct utmp *ut);
ae83b6
+int  login_tty (int fd);
ae83b6
+int  logout (char *line);
ae83b6
+void logwtmp (const char *line, const char *name, const char *host);
ae83b6
+int  openpty (int *amaster, int *aslave, char *name, struct termios *termp, struct winsize *winp);
ae83b6
+int  forkpty (int *amaster, char *name, struct termios *termp, struct winsize *winp);
ae83b6
+#endif
ae83b6
+#else
ae83b6
+int openpty (int *master_fd, int *slavefd, char *name, struct termios *termp, struct winsize *winp);
ae83b6
+pid_t forkpty (int *master_fd, char *name, struct termios *termp, struct winsize *winp);
ae83b6
+#endif
ae83b6
+
ae83b6
+#ifndef HAVE_LOGIN_TTY
ae83b6
+int login_tty (int fd);
ae83b6
+#elif defined(HAVE_UTMP_H)
ae83b6
+/* Get the prototype from utmp.h */
ae83b6
+#include <utmp.h>
ae83b6
+#endif
ae83b6
+
ae83b6
+int n_read (int fd, void *buffer, int size);
ae83b6
+int n_write (int fd, const void *buffer, int size);
ae83b6
+
ae83b6
+#endif /* _GNOME_LOGIN_SUPPORT_H */
ae83b6
diff --git a/gnome-pty-helper/gnome-pty-helper.c b/gnome-pty-helper/gnome-pty-helper.c
ae83b6
new file mode 100644
ae83b6
index 000000000000..2666bf4cd46d
ae83b6
--- /dev/null
ae83b6
+++ b/gnome-pty-helper/gnome-pty-helper.c
ae83b6
@@ -0,0 +1,751 @@
ae83b6
+/*
ae83b6
+ * gnome-pty.c:  Helper setuid application used to open a pseudo-
ae83b6
+ * terminal, set the permissions, ownership and record user login
ae83b6
+ * information
ae83b6
+ *
ae83b6
+ * Author:
ae83b6
+ *    Miguel de Icaza (miguel@gnu.org)
ae83b6
+ *
ae83b6
+ * Parent application talks to us via a couple of sockets that are strategically
ae83b6
+ * placed on file descriptors 0 and 1 (STDIN_FILENO and STDOUT_FILENO).
ae83b6
+ *
ae83b6
+ * We use the STDIN_FILENO to read and write the protocol information and we use
ae83b6
+ * the STDOUT_FILENO to pass the file descriptors (we need two different file
ae83b6
+ * descriptors as using a socket for both data transfers and file descriptor
ae83b6
+ * passing crashes some BSD kernels according to Theo de Raadt)
ae83b6
+ *
ae83b6
+ * A sample protocol is used:
ae83b6
+ *
ae83b6
+ * OPEN_PTY             => 1 <tag> <master-pty-fd> <slave-pty-fd>
ae83b6
+ *                      => 0
ae83b6
+ *
ae83b6
+ * CLOSE_PTY  <tag>     => void
ae83b6
+ *
ae83b6
+ * <tag> is a pointer.  If tag is NULL, then the ptys were not allocated.
ae83b6
+ * ptys are passed using file descriptor passing on the stdin file descriptor
ae83b6
+ *
ae83b6
+ * We use as little as possible external libraries.
ae83b6
+ */
ae83b6
+#include <config.h>
ae83b6
+
ae83b6
+/* Use this to pull SCM_RIGHTS definition on IRIX */
ae83b6
+#if defined(irix) || defined (__irix__) || defined(sgi) || defined (__sgi__)
ae83b6
+#    define _XOPEN_SOURCE 1
ae83b6
+extern char *strdup(const char *);
ae83b6
+#endif
ae83b6
+
ae83b6
+#include <sys/types.h>
ae83b6
+#include <sys/time.h>
ae83b6
+#include <sys/resource.h>
ae83b6
+#include <sys/stat.h>
ae83b6
+#include <limits.h>
ae83b6
+#include <unistd.h>
ae83b6
+#include <string.h>
ae83b6
+#include <signal.h>
ae83b6
+#include <sys/param.h>
ae83b6
+#include <fcntl.h>
ae83b6
+#include <termios.h>
ae83b6
+#include <errno.h>
ae83b6
+#include <termios.h>
ae83b6
+#include <pwd.h>
ae83b6
+#include <stdlib.h>
ae83b6
+#include <string.h>
ae83b6
+#include <stdio.h>
ae83b6
+#include <utmp.h>
ae83b6
+#include <grp.h>
ae83b6
+#include "gnome-pty.h"
ae83b6
+#include "gnome-login-support.h"
ae83b6
+
ae83b6
+/* For PATH_MAX on BSD-like systems. */
ae83b6
+#ifdef HAVE_SYS_SYSLIMITS_H
ae83b6
+#include <sys/syslimits.h>
ae83b6
+#endif
ae83b6
+
ae83b6
+static struct passwd *pwent;
ae83b6
+static char login_name_buffer [48];
ae83b6
+static char *login_name, *display_name;
ae83b6
+
ae83b6
+struct pty_info {
ae83b6
+	char   *login_name;
ae83b6
+	struct pty_info *next;
ae83b6
+	char   *line;
ae83b6
+	void   *data;
ae83b6
+	char   utmp, wtmp, lastlog;
ae83b6
+};
ae83b6
+
ae83b6
+typedef struct pty_info pty_info;
ae83b6
+
ae83b6
+static pty_info *pty_list;
ae83b6
+
ae83b6
+#ifdef HAVE_SENDMSG
ae83b6
+#include <sys/socket.h>
ae83b6
+#include <sys/uio.h>
ae83b6
+
ae83b6
+#ifdef HAVE_SYS_UN_H /* Linux libc5 */
ae83b6
+#include <sys/un.h>
ae83b6
+#endif
ae83b6
+
ae83b6
+#ifndef CMSG_DATA /* Linux libc5 */
ae83b6
+/* Ancillary data object manipulation macros.  */
ae83b6
+#if !defined __STRICT_ANSI__ && defined __GNUC__ && __GNUC__ >= 2
ae83b6
+# define CMSG_DATA(cmsg) ((cmsg)->cmsg_data)
ae83b6
+#else
ae83b6
+# define CMSG_DATA(cmsg) ((unsigned char *) ((struct cmsghdr *) (cmsg) + 1))
ae83b6
+#endif
ae83b6
+#endif /* CMSG_DATA */
ae83b6
+
ae83b6
+/* Solaris doesn't define these */
ae83b6
+#ifndef CMSG_ALIGN
ae83b6
+#define CMSG_ALIGN(len) (((len) + sizeof (size_t) - 1) & (size_t) ~(sizeof (size_t) - 1))
ae83b6
+#endif
ae83b6
+#ifndef CMSG_SPACE
ae83b6
+#define CMSG_SPACE(len) (CMSG_ALIGN (len) + CMSG_ALIGN (sizeof (struct cmsghdr)))
ae83b6
+#endif
ae83b6
+#ifndef CMSG_LEN
ae83b6
+#define CMSG_LEN(len) (CMSG_ALIGN (sizeof (struct cmsghdr)) + (len))
ae83b6
+#endif
ae83b6
+
ae83b6
+static int
ae83b6
+pass_fd (int client_fd, int fd)
ae83b6
+{
ae83b6
+        struct iovec  iov[1];
ae83b6
+        struct msghdr msg;
ae83b6
+        char          buf [1];
ae83b6
+        char    cmsgbuf[CMSG_SPACE(sizeof(int))];
ae83b6
+        struct  cmsghdr *cmptr;
ae83b6
+        int *fdptr;
ae83b6
+
ae83b6
+	iov [0].iov_base = buf;
ae83b6
+	iov [0].iov_len  = 1;
ae83b6
+
ae83b6
+	msg.msg_iov        = iov;
ae83b6
+	msg.msg_iovlen     = 1;
ae83b6
+	msg.msg_name       = NULL;
ae83b6
+	msg.msg_namelen    = 0;
ae83b6
+	msg.msg_control    = (caddr_t) cmsgbuf;
ae83b6
+	msg.msg_controllen = sizeof(cmsgbuf);
ae83b6
+
ae83b6
+        cmptr = CMSG_FIRSTHDR(&msg;;
ae83b6
+	cmptr->cmsg_level = SOL_SOCKET;
ae83b6
+	cmptr->cmsg_type  = SCM_RIGHTS;
ae83b6
+	cmptr->cmsg_len   = CMSG_LEN(sizeof(int));
ae83b6
+        fdptr = (int *) CMSG_DATA(cmptr);
ae83b6
+        memcpy (fdptr, &fd, sizeof(int));
ae83b6
+	if (sendmsg (client_fd, &msg, 0) != 1)
ae83b6
+		return -1;
ae83b6
+
ae83b6
+	return 0;
ae83b6
+}
ae83b6
+
ae83b6
+#elif defined(__sgi) && !defined(HAVE_SENDMSG)
ae83b6
+
ae83b6
+/*
ae83b6
+ * IRIX 6.2 is like 4.3BSD; it will not have HAVE_SENDMSG set,
ae83b6
+ * because msghdr used msg_accrights and msg_accrightslen rather
ae83b6
+ * than the newer msg_control and msg_controllen fields configure
ae83b6
+ * checks.  The SVR4 code below doesn't work because pipe()
ae83b6
+ * semantics are controlled by the svr3pipe systune variable,
ae83b6
+ * which defaults to uni-directional pipes.  Also sending
ae83b6
+ * file descriptors through pipes isn't implemented.
ae83b6
+ */
ae83b6
+
ae83b6
+#include <sys/socket.h>
ae83b6
+#include <sys/uio.h>
ae83b6
+
ae83b6
+static int
ae83b6
+pass_fd (int client_fd, int fd)
ae83b6
+{
ae83b6
+  struct iovec  iov[1];
ae83b6
+  struct msghdr msg;
ae83b6
+  char          buf [1];
ae83b6
+
ae83b6
+  iov [0].iov_base = buf;
ae83b6
+  iov [0].iov_len  = 1;
ae83b6
+
ae83b6
+  msg.msg_iov        = iov;
ae83b6
+  msg.msg_iovlen     = 1;
ae83b6
+  msg.msg_name       = NULL;
ae83b6
+  msg.msg_namelen    = 0;
ae83b6
+  msg.msg_accrights    = (caddr_t) &fd;
ae83b6
+  msg.msg_accrightslen = sizeof(fd);
ae83b6
+
ae83b6
+  if (sendmsg (client_fd, &msg, 0) != 1)
ae83b6
+    return -1;
ae83b6
+
ae83b6
+  return 0;
ae83b6
+}
ae83b6
+
ae83b6
+#else
ae83b6
+#include <stropts.h>
ae83b6
+#ifdef I_SENDFD
ae83b6
+
ae83b6
+int
ae83b6
+pass_fd (int client_fd, int fd)
ae83b6
+{
ae83b6
+	if (ioctl (client_fd, I_SENDFD, fd) < 0)
ae83b6
+		return -1;
ae83b6
+	return 0;
ae83b6
+}
ae83b6
+#endif
ae83b6
+#endif
ae83b6
+
ae83b6
+static void
ae83b6
+pty_free (pty_info *pi)
ae83b6
+{
ae83b6
+	free (pi);
ae83b6
+}
ae83b6
+
ae83b6
+static void
ae83b6
+pty_remove (pty_info *pi)
ae83b6
+{
ae83b6
+	pty_info *l, *last;
ae83b6
+
ae83b6
+	last = (void *) 0;
ae83b6
+
ae83b6
+	for (l = pty_list; l; l = l->next) {
ae83b6
+		if (l == pi) {
ae83b6
+			if (last == (void *) 0)
ae83b6
+				pty_list = pi->next;
ae83b6
+			else
ae83b6
+				last->next = pi->next;
ae83b6
+			free (pi->line);
ae83b6
+			free (pi->login_name);
ae83b6
+			pty_free (pi);
ae83b6
+			return;
ae83b6
+		}
ae83b6
+		last = l;
ae83b6
+	}
ae83b6
+
ae83b6
+	exit (1);
ae83b6
+}
ae83b6
+
ae83b6
+static void
ae83b6
+shutdown_pty (pty_info *pi)
ae83b6
+{
ae83b6
+	if (pi->utmp || pi->wtmp || pi->lastlog)
ae83b6
+		if (pi->data)
ae83b6
+			write_logout_record (pi->login_name, pi->data, pi->utmp, pi->wtmp);
ae83b6
+
ae83b6
+	pty_remove (pi);
ae83b6
+}
ae83b6
+
ae83b6
+static void
ae83b6
+shutdown_helper (void)
ae83b6
+{
ae83b6
+	pty_info *pi;
ae83b6
+
ae83b6
+	for (pi = pty_list; pi; pi = pty_list)
ae83b6
+		shutdown_pty (pi);
ae83b6
+}
ae83b6
+
ae83b6
+static pty_info *
ae83b6
+pty_add (int utmp, int wtmp, int lastlog, char *line, char *login_name)
ae83b6
+{
ae83b6
+	pty_info *pi = malloc (sizeof (pty_info));
ae83b6
+
ae83b6
+	if (pi == NULL) {
ae83b6
+		shutdown_helper ();
ae83b6
+		exit (1);
ae83b6
+	}
ae83b6
+
ae83b6
+	memset (pi, 0, sizeof (pty_info));
ae83b6
+
ae83b6
+	if (strncmp (line, "/dev/", 5))
ae83b6
+		pi->line = strdup (line);
ae83b6
+	else
ae83b6
+		pi->line = strdup (line+5);
ae83b6
+
ae83b6
+	if (pi->line == NULL) {
ae83b6
+		shutdown_helper ();
ae83b6
+		exit (1);
ae83b6
+	}
ae83b6
+
ae83b6
+	pi->next = pty_list;
ae83b6
+	pi->utmp = utmp;
ae83b6
+	pi->wtmp = wtmp;
ae83b6
+	pi->lastlog = lastlog;
ae83b6
+	pi->login_name = strdup (login_name);
ae83b6
+
ae83b6
+	pty_list = pi;
ae83b6
+
ae83b6
+	return pi;
ae83b6
+}
ae83b6
+
ae83b6
+static struct termios*
ae83b6
+init_term_with_defaults(struct termios* term)
ae83b6
+{
ae83b6
+	/*
ae83b6
+	 *	openpty assumes POSIX termios so this should be portable.
ae83b6
+	 *	Don't change this to a structure init - POSIX doesn't say
ae83b6
+	 *	anything about field order.
ae83b6
+	 */
ae83b6
+	memset(term, 0, sizeof(struct termios));
ae83b6
+
ae83b6
+	term->c_iflag = 0
ae83b6
+#ifdef BRKINT
ae83b6
+	  | BRKINT
ae83b6
+#endif
ae83b6
+#ifdef ICRNL
ae83b6
+	  | ICRNL
ae83b6
+#endif
ae83b6
+#ifdef IMAXBEL
ae83b6
+	  | IMAXBEL
ae83b6
+#endif
ae83b6
+#ifdef IXON
ae83b6
+	  | IXON
ae83b6
+#endif
ae83b6
+#ifdef IXANY
ae83b6
+	  | IXANY
ae83b6
+#endif
ae83b6
+	  ;
ae83b6
+	term->c_oflag = 0
ae83b6
+#ifdef OPOST
ae83b6
+	  | OPOST
ae83b6
+#endif
ae83b6
+#ifdef ONLCR
ae83b6
+	  | ONLCR
ae83b6
+#endif
ae83b6
+#ifdef NL0
ae83b6
+	  | NL0
ae83b6
+#endif
ae83b6
+#ifdef CR0
ae83b6
+	  | CR0
ae83b6
+#endif
ae83b6
+#ifdef TAB0
ae83b6
+	  | TAB0
ae83b6
+#endif
ae83b6
+#ifdef BS0
ae83b6
+	  | BS0
ae83b6
+#endif
ae83b6
+#ifdef VT0
ae83b6
+	  | VT0
ae83b6
+#endif
ae83b6
+#ifdef FF0
ae83b6
+	  | FF0
ae83b6
+#endif
ae83b6
+	  ;
ae83b6
+	term->c_cflag = 0
ae83b6
+#ifdef CREAD
ae83b6
+	  | CREAD
ae83b6
+#endif
ae83b6
+#ifdef CS8
ae83b6
+	  | CS8
ae83b6
+#endif
ae83b6
+#ifdef HUPCL
ae83b6
+	  | HUPCL
ae83b6
+#endif
ae83b6
+	  ;
ae83b6
+#ifdef EXTB
ae83b6
+	cfsetispeed(term, EXTB);
ae83b6
+	cfsetospeed(term, EXTB);
ae83b6
+#else
ae83b6
+#   ifdef B38400
ae83b6
+        cfsetispeed(term, B38400);
ae83b6
+        cfsetospeed(term, B38400);
ae83b6
+#   else
ae83b6
+#       ifdef B9600
ae83b6
+        cfsetispeed(term, B9600);
ae83b6
+        cfsetospeed(term, B9600);
ae83b6
+#       endif
ae83b6
+#   endif
ae83b6
+#endif /* EXTB */
ae83b6
+
ae83b6
+	term->c_lflag = 0
ae83b6
+#ifdef ECHO
ae83b6
+	  | ECHO
ae83b6
+#endif
ae83b6
+#ifdef ICANON
ae83b6
+	  | ICANON
ae83b6
+#endif
ae83b6
+#ifdef ISIG
ae83b6
+	  | ISIG
ae83b6
+#endif
ae83b6
+#ifdef IEXTEN
ae83b6
+	  | IEXTEN
ae83b6
+#endif
ae83b6
+#ifdef ECHOE
ae83b6
+	  | ECHOE
ae83b6
+#endif
ae83b6
+#ifdef ECHOKE
ae83b6
+	  | ECHOKE
ae83b6
+#endif
ae83b6
+#ifdef ECHOK
ae83b6
+	  | ECHOK
ae83b6
+#endif
ae83b6
+#ifdef ECHOCTL
ae83b6
+	  | ECHOCTL
ae83b6
+#endif
ae83b6
+	  ;
ae83b6
+
ae83b6
+#ifdef N_TTY
ae83b6
+	/* should really be a check for c_line, but maybe this is good enough */
ae83b6
+	term->c_line = N_TTY;
ae83b6
+#endif
ae83b6
+
ae83b6
+	/* These two may overlap so set them first */
ae83b6
+	/* That setup means, that read() will be blocked until  */
ae83b6
+	/* at least 1 symbol will be read.                      */
ae83b6
+	term->c_cc[VMIN]  = 1;
ae83b6
+	term->c_cc[VTIME] = 0;
ae83b6
+
ae83b6
+	/*
ae83b6
+	 * Now set the characters. This is of course a religious matter
ae83b6
+	 * but we use the defaults, with erase bound to the key gnome-terminal
ae83b6
+	 * maps.
ae83b6
+	 *
ae83b6
+	 * These are the ones set by "stty sane".
ae83b6
+	 */
ae83b6
+
ae83b6
+	term->c_cc[VINTR]  = 'C'-64;
ae83b6
+	term->c_cc[VQUIT]  = '\\'-64;
ae83b6
+	term->c_cc[VERASE] = 127;
ae83b6
+	term->c_cc[VKILL]  = 'U'-64;
ae83b6
+	term->c_cc[VEOF]   = 'D'-64;
ae83b6
+#ifdef VSWTC
ae83b6
+	term->c_cc[VSWTC]  = 255;
ae83b6
+#endif
ae83b6
+	term->c_cc[VSTART] = 'Q'-64;
ae83b6
+	term->c_cc[VSTOP]  = 'S'-64;
ae83b6
+	term->c_cc[VSUSP]  = 'Z'-64;
ae83b6
+	term->c_cc[VEOL]   = 255;
ae83b6
+
ae83b6
+	/*
ae83b6
+	 *	Extended stuff.
ae83b6
+	 */
ae83b6
+
ae83b6
+#ifdef VREPRINT
ae83b6
+	term->c_cc[VREPRINT] = 'R'-64;
ae83b6
+#endif
ae83b6
+#ifdef VSTATUS
ae83b6
+	term->c_cc[VSTATUS]  = 'T'-64;
ae83b6
+#endif
ae83b6
+#ifdef VDISCARD
ae83b6
+	term->c_cc[VDISCARD] = 'O'-64;
ae83b6
+#endif
ae83b6
+#ifdef VWERASE
ae83b6
+	term->c_cc[VWERASE]  = 'W'-64;
ae83b6
+#endif
ae83b6
+#ifdef VLNEXT
ae83b6
+	term->c_cc[VLNEXT]   = 'V'-64;
ae83b6
+#endif
ae83b6
+#ifdef VDSUSP
ae83b6
+	term->c_cc[VDSUSP]   = 'Y'-64;
ae83b6
+#endif
ae83b6
+#ifdef VEOL2
ae83b6
+	term->c_cc[VEOL2]    = 255;
ae83b6
+#endif
ae83b6
+    return term;
ae83b6
+}
ae83b6
+
ae83b6
+static int
ae83b6
+open_ptys (int utmp, int wtmp, int lastlog)
ae83b6
+{
ae83b6
+	const char *term_name;
ae83b6
+	int status, master_pty, slave_pty;
ae83b6
+	pty_info *p;
ae83b6
+	int result;
ae83b6
+	uid_t savedUid;
ae83b6
+	gid_t savedGid;
ae83b6
+	struct group *group_info;
ae83b6
+	struct termios term;
ae83b6
+
ae83b6
+	/* Initialize term */
ae83b6
+	init_term_with_defaults(&term);
ae83b6
+
ae83b6
+	/* root privileges */
ae83b6
+	savedUid = geteuid();
ae83b6
+	savedGid = getegid();
ae83b6
+
ae83b6
+	/* drop privileges to the user level */
ae83b6
+#if defined(HAVE_SETEUID)
ae83b6
+	seteuid (pwent->pw_uid);
ae83b6
+	setegid (pwent->pw_gid);
ae83b6
+#elif defined(HAVE_SETREUID)
ae83b6
+	setreuid (savedUid, pwent->pw_uid);
ae83b6
+	setregid (savedGid, pwent->pw_gid);
ae83b6
+#else
ae83b6
+#error "No means to drop privileges! Huge security risk! Won't compile."
ae83b6
+#endif
ae83b6
+	/* Open pty with privileges of the user */
ae83b6
+	status = openpty (&master_pty, &slave_pty, NULL, &term, NULL);
ae83b6
+
ae83b6
+	/* Restore saved privileges to root */
ae83b6
+#ifdef HAVE_SETEUID
ae83b6
+	seteuid (savedUid);
ae83b6
+	setegid (savedGid);
ae83b6
+#elif defined(HAVE_SETREUID)
ae83b6
+	setreuid (pwent->pw_uid, savedUid);
ae83b6
+	setregid (pwent->pw_gid, savedGid);
ae83b6
+#else
ae83b6
+#error "No means to raise privileges! Huge security risk! Won't compile."
ae83b6
+#endif
ae83b6
+	/* openpty() failed, reject request */
ae83b6
+	if (status == -1 || (term_name = ttyname(slave_pty)) == NULL) {
ae83b6
+		result = 0;
ae83b6
+		n_write (STDIN_FILENO, &result, sizeof (result));
ae83b6
+		return 0;
ae83b6
+	}
ae83b6
+
ae83b6
+	/* a bit tricky, we re-do the part of the openpty()  */
ae83b6
+	/* that required root privileges, and, hence, failed */
ae83b6
+	group_info = getgrnam ("tty");
ae83b6
+	fchown (slave_pty, getuid (), group_info ? group_info->gr_gid : -1);
ae83b6
+	fchmod (slave_pty, S_IRUSR | S_IWUSR | S_IWGRP);
ae83b6
+	/* It's too late to call revoke at this time... */
ae83b6
+	/* revoke(term_name); */
ae83b6
+
ae83b6
+	/* add pty to the list of allocated by us */
ae83b6
+	p = pty_add (utmp, wtmp, lastlog, term_name, login_name);
ae83b6
+	result = 1;
ae83b6
+
ae83b6
+	if (n_write (STDIN_FILENO, &result, sizeof (result)) != sizeof (result) ||
ae83b6
+	    n_write (STDIN_FILENO, &p, sizeof (p)) != sizeof (p) ||
ae83b6
+	    pass_fd (STDOUT_FILENO, master_pty)  == -1 ||
ae83b6
+	    pass_fd (STDOUT_FILENO, slave_pty)   == -1) {
ae83b6
+		exit (0);
ae83b6
+	}
ae83b6
+
ae83b6
+	if (utmp || wtmp || lastlog) {
ae83b6
+		p->data = write_login_record (login_name, display_name,
ae83b6
+					      term_name, utmp, wtmp, lastlog);
ae83b6
+	}
ae83b6
+
ae83b6
+	close (master_pty);
ae83b6
+	close (slave_pty);
ae83b6
+
ae83b6
+	return 1;
ae83b6
+}
ae83b6
+
ae83b6
+static void
ae83b6
+close_pty_pair (void *tag)
ae83b6
+{
ae83b6
+	pty_info *pi;
ae83b6
+
ae83b6
+	for (pi = pty_list; pi; pi = pi->next) {
ae83b6
+		if (tag == pi) {
ae83b6
+			shutdown_pty (pi);
ae83b6
+			break;
ae83b6
+		}
ae83b6
+	}
ae83b6
+}
ae83b6
+
ae83b6
+#define MB (1024*1024)
ae83b6
+
ae83b6
+struct {
ae83b6
+	int limit;
ae83b6
+	int value;
ae83b6
+} sensible_limits [] = {
ae83b6
+	{ RLIMIT_CPU,    120 },
ae83b6
+	{ RLIMIT_FSIZE,  1 * MB },
ae83b6
+	{ RLIMIT_DATA,   1 * MB },
ae83b6
+	{ RLIMIT_STACK,  1 * MB },
ae83b6
+#ifdef RLIMIT_AS
ae83b6
+	{ RLIMIT_AS,     1 * MB },
ae83b6
+#endif
ae83b6
+	{ RLIMIT_NOFILE, 10 },
ae83b6
+#ifdef RLIMIT_NPROC
ae83b6
+	{ RLIMIT_NPROC,  5 },
ae83b6
+#endif
ae83b6
+	{ -1, -1 }
ae83b6
+};
ae83b6
+
ae83b6
+static void
ae83b6
+sanity_checks (void)
ae83b6
+{
ae83b6
+	int stderr_fd;
ae83b6
+	int i, open_max;
ae83b6
+	int flag;
ae83b6
+
ae83b6
+	/*
ae83b6
+	 * Make sure stdin/stdout are open.  This is a requirement
ae83b6
+	 * for our program to work and closes potential security holes.
ae83b6
+	 */
ae83b6
+	if ((fcntl (0, F_GETFL, &flag) == -1 && errno == EBADF) ||
ae83b6
+	    (fcntl (1, F_GETFL, &flag) == -1 && errno == EBADF)) {
ae83b6
+		exit (1);
ae83b6
+	}
ae83b6
+
ae83b6
+	/*
ae83b6
+	 * File descriptors 0 and 1 have been setup by the parent process
ae83b6
+	 * to be used for the protocol exchange and for transfering
ae83b6
+	 * file descriptors.
ae83b6
+	 *
ae83b6
+	 * Make stderr point to a terminal.
ae83b6
+	 */
ae83b6
+	if (fcntl (2, F_GETFL, &flag) == -1 && errno == EBADF) {
ae83b6
+		stderr_fd = open ("/dev/tty", O_RDWR);
ae83b6
+		if (stderr_fd == -1) {
ae83b6
+			stderr_fd = open ("/dev/null", O_RDWR);
ae83b6
+			if (stderr_fd == -1)
ae83b6
+				exit (1);
ae83b6
+		}
ae83b6
+
ae83b6
+		if (stderr_fd != 2)
ae83b6
+			while (dup2 (stderr_fd, 2) == -1 && errno == EINTR)
ae83b6
+				;
ae83b6
+	}
ae83b6
+
ae83b6
+	/* Close any file descriptor we do not use */
ae83b6
+	open_max = sysconf (_SC_OPEN_MAX);
ae83b6
+	for (i = 3; i < open_max; i++) {
ae83b6
+		close (i);
ae83b6
+	}
ae83b6
+
ae83b6
+	/* Check sensible resource limits */
ae83b6
+	for (i = 0; sensible_limits [i].value != -1; i++) {
ae83b6
+		struct rlimit rlim;
ae83b6
+
ae83b6
+		if (getrlimit (sensible_limits [i].limit, &rlim) != 0)
ae83b6
+			continue;
ae83b6
+
ae83b6
+		if (rlim.rlim_cur != RLIM_INFINITY &&
ae83b6
+		    rlim.rlim_cur < sensible_limits [i].value) {
ae83b6
+			if (setrlimit (sensible_limits [i].limit, &rlim) != 0) {
ae83b6
+				fprintf (stderr, "Living environment not ok\n");
ae83b6
+				exit (1);
ae83b6
+			}
ae83b6
+		}
ae83b6
+	}
ae83b6
+
ae83b6
+	/* Make sure SIGIO/SIGINT is SIG_IGN */
ae83b6
+	{
ae83b6
+		struct sigaction sa;
ae83b6
+		sigset_t sigset;
ae83b6
+
ae83b6
+		sa.sa_handler = SIG_IGN;
ae83b6
+		sigemptyset (&sa.sa_mask);
ae83b6
+		sa.sa_flags = 0;
ae83b6
+
ae83b6
+		sigemptyset(&sigset);
ae83b6
+		sigaddset(&sigset, SIGIO);
ae83b6
+		sigaddset(&sigset, SIGINT);
ae83b6
+		sigprocmask(SIG_UNBLOCK, &sigset, NULL);
ae83b6
+
ae83b6
+		sigaction (SIGIO, &sa, NULL);
ae83b6
+		sigaction (SIGINT, &sa, NULL);
ae83b6
+	}
ae83b6
+}
ae83b6
+
ae83b6
+static volatile int done;
ae83b6
+
ae83b6
+static void
ae83b6
+exit_handler (int signum)
ae83b6
+{
ae83b6
+	done = 1;
ae83b6
+}
ae83b6
+
ae83b6
+
ae83b6
+int
ae83b6
+main (int argc, char *argv [])
ae83b6
+{
ae83b6
+	int res, n;
ae83b6
+	void *tag;
ae83b6
+	GnomePtyOps op;
ae83b6
+	const char *logname;
ae83b6
+
ae83b6
+	sanity_checks ();
ae83b6
+
ae83b6
+	pwent = NULL;
ae83b6
+
ae83b6
+	logname = getenv ("LOGNAME");
ae83b6
+	if (logname != NULL) {
ae83b6
+		pwent = getpwnam (logname);
ae83b6
+		if (pwent != NULL && pwent->pw_uid != getuid ()) {
ae83b6
+			/* LOGNAME is lying, fall back to looking up the uid */
ae83b6
+			pwent = NULL;
ae83b6
+		}
ae83b6
+	}
ae83b6
+
ae83b6
+	if (pwent == NULL)
ae83b6
+		pwent = getpwuid (getuid ());
ae83b6
+
ae83b6
+	if (pwent)
ae83b6
+		login_name = pwent->pw_name;
ae83b6
+	else {
ae83b6
+		sprintf (login_name_buffer, "#%u", (unsigned int)getuid ());
ae83b6
+		login_name = login_name_buffer;
ae83b6
+	}
ae83b6
+
ae83b6
+        /* Change directory so we don't prevent unmounting in case the initial cwd
ae83b6
+         * is on an external device (see bug #574491). 
ae83b6
+         */
ae83b6
+	if (chdir ("/") < 0)
ae83b6
+                fprintf (stderr, "Failed to chdir to /: %s\n", strerror (errno));
ae83b6
+ 
ae83b6
+	display_name = getenv ("DISPLAY");
ae83b6
+	if (!display_name)
ae83b6
+		display_name = "localhost";
ae83b6
+
ae83b6
+	done = 0;
ae83b6
+
ae83b6
+	/* Make sure we clean up utmp/wtmp even under vncserver */
ae83b6
+	signal (SIGHUP, exit_handler);
ae83b6
+	signal (SIGTERM, exit_handler);
ae83b6
+
ae83b6
+	while (!done) {
ae83b6
+		res = n_read (STDIN_FILENO, &op, sizeof (op));
ae83b6
+
ae83b6
+		if (res != sizeof (op)) {
ae83b6
+			done = 1;
ae83b6
+			continue;
ae83b6
+		}
ae83b6
+
ae83b6
+		switch (op) {
ae83b6
+		case GNOME_PTY_OPEN_PTY_UTMP:
ae83b6
+			open_ptys (1, 0, 0);
ae83b6
+			break;
ae83b6
+
ae83b6
+		case GNOME_PTY_OPEN_PTY_UWTMP:
ae83b6
+			open_ptys (1, 1, 0);
ae83b6
+			break;
ae83b6
+
ae83b6
+		case GNOME_PTY_OPEN_PTY_WTMP:
ae83b6
+			open_ptys (0, 1, 0);
ae83b6
+			break;
ae83b6
+
ae83b6
+		case GNOME_PTY_OPEN_PTY_LASTLOG:
ae83b6
+			open_ptys (0, 0, 1);
ae83b6
+			break;
ae83b6
+
ae83b6
+		case GNOME_PTY_OPEN_PTY_LASTLOGUTMP:
ae83b6
+			open_ptys (1, 0, 1);
ae83b6
+			break;
ae83b6
+
ae83b6
+		case GNOME_PTY_OPEN_PTY_LASTLOGUWTMP:
ae83b6
+			open_ptys (1, 1, 1);
ae83b6
+			break;
ae83b6
+
ae83b6
+		case GNOME_PTY_OPEN_PTY_LASTLOGWTMP:
ae83b6
+			open_ptys (0, 1, 1);
ae83b6
+			break;
ae83b6
+
ae83b6
+		case GNOME_PTY_OPEN_NO_DB_UPDATE:
ae83b6
+			open_ptys (0, 0, 0);
ae83b6
+			break;
ae83b6
+
ae83b6
+		case GNOME_PTY_RESET_TO_DEFAULTS:
ae83b6
+			break;
ae83b6
+
ae83b6
+		case GNOME_PTY_CLOSE_PTY:
ae83b6
+			n = n_read (STDIN_FILENO, &tag, sizeof (tag));
ae83b6
+			if (n != sizeof (tag)) {
ae83b6
+				shutdown_helper ();
ae83b6
+				exit (1);
ae83b6
+			}
ae83b6
+			close_pty_pair (tag);
ae83b6
+			break;
ae83b6
+
ae83b6
+		case GNOME_PTY_SYNCH:
ae83b6
+			{
ae83b6
+				int result = 0;
ae83b6
+				n_write (STDIN_FILENO, &result, 1);
ae83b6
+			}
ae83b6
+			break;
ae83b6
+		}
ae83b6
+
ae83b6
+	}
ae83b6
+
ae83b6
+	shutdown_helper ();
ae83b6
+	return 0;
ae83b6
+}
ae83b6
+
ae83b6
diff --git a/gnome-pty-helper/gnome-pty.h b/gnome-pty-helper/gnome-pty.h
ae83b6
new file mode 100644
ae83b6
index 000000000000..94f94f95e248
ae83b6
--- /dev/null
ae83b6
+++ b/gnome-pty-helper/gnome-pty.h
ae83b6
@@ -0,0 +1,22 @@
ae83b6
+#ifndef GNOME_PTY_H
ae83b6
+#define GNOME_PTY_H
ae83b6
+
ae83b6
+typedef enum {
ae83b6
+	GNOME_PTY_OPEN_PTY_UTMP = 1,
ae83b6
+	GNOME_PTY_OPEN_PTY_UWTMP,
ae83b6
+	GNOME_PTY_OPEN_PTY_WTMP,
ae83b6
+	GNOME_PTY_OPEN_PTY_LASTLOG,
ae83b6
+	GNOME_PTY_OPEN_PTY_LASTLOGUTMP,
ae83b6
+	GNOME_PTY_OPEN_PTY_LASTLOGUWTMP,
ae83b6
+	GNOME_PTY_OPEN_PTY_LASTLOGWTMP,
ae83b6
+	GNOME_PTY_OPEN_NO_DB_UPDATE,
ae83b6
+	GNOME_PTY_RESET_TO_DEFAULTS,
ae83b6
+	GNOME_PTY_CLOSE_PTY,
ae83b6
+	GNOME_PTY_SYNCH
ae83b6
+} GnomePtyOps;
ae83b6
+
ae83b6
+void *update_dbs         (int utmp, int wtmp, int lastlog, char *login_name, char *display_name, char *term_name);
ae83b6
+void *write_login_record (char *login_name, char *display_name, char *term_name, int utmp, int wtmp, int lastlog);
ae83b6
+void write_logout_record (char *login_name, void *data, int utmp, int wtmp);
ae83b6
+
ae83b6
+#endif
ae83b6
diff --git a/gnome-pty-helper/gnome-utmp.c b/gnome-pty-helper/gnome-utmp.c
ae83b6
new file mode 100644
ae83b6
index 000000000000..1a090ec8ee38
ae83b6
--- /dev/null
ae83b6
+++ b/gnome-pty-helper/gnome-utmp.c
ae83b6
@@ -0,0 +1,374 @@
ae83b6
+/*
ae83b6
+ * utmp/wtmp file updating
ae83b6
+ *
ae83b6
+ * Authors:
ae83b6
+ *    Miguel de Icaza (miguel@gnu.org).
ae83b6
+ *    Timur I. Bakeyev (timur@gnu.org).
ae83b6
+ *
ae83b6
+ * FIXME: Do we want to register the PID of the process running *under* the
ae83b6
+ * subshell or the PID of the parent process? (we are doing the latter now).
ae83b6
+ *
ae83b6
+ * FIXME: Solaris (utmpx) stuff need to be checked.
ae83b6
+ */
ae83b6
+
ae83b6
+#include <config.h>
ae83b6
+#include <sys/types.h>
ae83b6
+#include <sys/file.h>
ae83b6
+#include <stdio.h>
ae83b6
+#include <unistd.h>
ae83b6
+#include <stdlib.h>
ae83b6
+#include <string.h>
ae83b6
+#include <fcntl.h>
ae83b6
+#include <pwd.h>
ae83b6
+#include <errno.h>
ae83b6
+
ae83b6
+#if defined(TIME_WITH_SYS_TIME)
ae83b6
+#    include <sys/time.h>
ae83b6
+#include <time.h>
ae83b6
+#else
ae83b6
+#  if defined(HAVE_SYS_TIME_H)
ae83b6
+#    include <sys/time.h>
ae83b6
+#  else
ae83b6
+#    include <time.h>
ae83b6
+#  endif
ae83b6
+#endif
ae83b6
+
ae83b6
+#if defined(HAVE_LASTLOG_H)
ae83b6
+#    include <lastlog.h>
ae83b6
+#endif
ae83b6
+
ae83b6
+#if defined(HAVE_PATHS_H)
ae83b6
+#    include <paths.h>
ae83b6
+#endif
ae83b6
+
ae83b6
+#if defined(HAVE_UTMP_H)
ae83b6
+#    include <utmp.h>
ae83b6
+#endif
ae83b6
+
ae83b6
+#if defined(HAVE_UTMPX_H)
ae83b6
+#    include <utmpx.h>
ae83b6
+#endif
ae83b6
+
ae83b6
+#if defined(HAVE_TTYENT_H)
ae83b6
+#    include <ttyent.h>
ae83b6
+#endif
ae83b6
+
ae83b6
+#include "gnome-pty.h"
ae83b6
+#include "gnome-login-support.h"
ae83b6
+
ae83b6
+
ae83b6
+
ae83b6
+#if !defined(UTMP_OUTPUT_FILENAME)
ae83b6
+#    if defined(UTMP_FILE)
ae83b6
+#        define UTMP_OUTPUT_FILENAME UTMP_FILE
ae83b6
+#    elif defined(_PATH_UTMP) /* BSD systems */
ae83b6
+#        define UTMP_OUTPUT_FILENAME _PATH_UTMP
ae83b6
+#    else
ae83b6
+#        define UTMP_OUTPUT_FILENAME "/etc/utmp"
ae83b6
+#    endif
ae83b6
+#endif
ae83b6
+
ae83b6
+#if !defined(WTMP_OUTPUT_FILENAME)
ae83b6
+#    if defined(WTMPX_FILE)
ae83b6
+#        define WTMP_OUTPUT_FILENAME WTMPX_FILE
ae83b6
+#    elif defined(_PATH_WTMPX)
ae83b6
+#        define WTMP_OUTPUT_FILENAME _PATH_WTMPX
ae83b6
+#    elif defined(WTMPX_FILENAME)
ae83b6
+#        define WTMP_OUTPUT_FILENAME WTMPX_FILENAME
ae83b6
+#    elif defined(WTMP_FILE)
ae83b6
+#        define WTMP_OUTPUT_FILENAME WTMP_FILE
ae83b6
+#    elif defined(_PATH_WTMP) /* BSD systems */
ae83b6
+#        define WTMP_OUTPUT_FILENAME _PATH_WTMP
ae83b6
+#    else
ae83b6
+#        define WTMP_OUTPUT_FILENAME "/etc/wtmp"
ae83b6
+#    endif
ae83b6
+#endif
ae83b6
+
ae83b6
+#if defined(_PATH_LASTLOG) /* BSD systems */
ae83b6
+#    define LASTLOG_OUTPUT_FILE	_PATH_LASTLOG
ae83b6
+#else
ae83b6
+#    define LASTLOG_OUTPUT_FILE	"/var/log/lastlog"
ae83b6
+#endif
ae83b6
+
ae83b6
+#if defined(HAVE_UPDWTMPX)
ae83b6
+#include <utmpx.h>
ae83b6
+#define update_wtmp updwtmpx
ae83b6
+#elif defined(HAVE_UPDWTMP)
ae83b6
+#define update_wtmp updwtmp
ae83b6
+#else /* !HAVE_UPDWTMPX && !HAVE_UPDWTMP */
ae83b6
+static void
ae83b6
+update_wtmp (char *file, UTMP *putmp)
ae83b6
+{
ae83b6
+	int fd, times = 3;
ae83b6
+#if defined(HAVE_FCNTL)
ae83b6
+	struct flock lck;
ae83b6
+
ae83b6
+	lck.l_whence = SEEK_END;
ae83b6
+	lck.l_len    = 0;
ae83b6
+	lck.l_start  = 0;
ae83b6
+	lck.l_type   = F_WRLCK;
ae83b6
+#endif
ae83b6
+
ae83b6
+	if ((fd = open (file, O_WRONLY|O_APPEND, 0)) < 0)
ae83b6
+		return;
ae83b6
+
ae83b6
+#if defined (HAVE_FCNTL)
ae83b6
+	while (times--)
ae83b6
+	  if ((fcntl (fd, F_SETLK, &lck) < 0))
ae83b6
+	    {
ae83b6
+	      if (errno != EAGAIN && errno != EACCES) {
ae83b6
+		close (fd);
ae83b6
+		return;
ae83b6
+	      }
ae83b6
+	      sleep (1); /*?!*/
ae83b6
+	    } else
ae83b6
+	      break;
ae83b6
+#elif defined(HAVE_FLOCK)
ae83b6
+	while (times--)
ae83b6
+	  if (flock (fd, LOCK_EX | LOCK_NB) < 0)
ae83b6
+	    {
ae83b6
+	      if (errno != EWOULDBLOCK)
ae83b6
+		{
ae83b6
+		  close (fd);
ae83b6
+		  return;
ae83b6
+		}
ae83b6
+	      sleep (1); /*?!*/
ae83b6
+	    } else
ae83b6
+	      break;
ae83b6
+#endif /* HAVE_FLOCK */
ae83b6
+
ae83b6
+	lseek (fd, 0, SEEK_END);
ae83b6
+	write (fd, putmp, sizeof(UTMP));
ae83b6
+
ae83b6
+	/* unlock the file */
ae83b6
+#if defined(HAVE_FCNTL)
ae83b6
+	lck.l_type = F_UNLCK;
ae83b6
+	fcntl (fd, F_SETLK, &lck;;
ae83b6
+#elif defined(HAVE_FLOCK)
ae83b6
+	flock (fd, LOCK_UN);
ae83b6
+#endif
ae83b6
+	close (fd);
ae83b6
+}
ae83b6
+#endif /* !HAVE_GETUTMPX */
ae83b6
+
ae83b6
+
ae83b6
+#if defined(HAVE_GETUTMPX)
ae83b6
+static void
ae83b6
+update_utmp (UTMP *ut)
ae83b6
+{
ae83b6
+	setutxent();
ae83b6
+	pututxline (ut);
ae83b6
+	endutxent();
ae83b6
+}
ae83b6
+#elif defined(HAVE_GETUTENT)
ae83b6
+static void
ae83b6
+update_utmp (UTMP *ut)
ae83b6
+{
ae83b6
+	setutent();
ae83b6
+	pututline (ut);
ae83b6
+	endutent();
ae83b6
+}
ae83b6
+#elif defined(HAVE_GETTTYENT)
ae83b6
+/* This variant is sutable for most BSD */
ae83b6
+static void
ae83b6
+update_utmp (UTMP *ut)
ae83b6
+{
ae83b6
+	struct ttyent *ty;
ae83b6
+	int fd, pos = 0;
ae83b6
+
ae83b6
+	if ((fd = open (UTMP_OUTPUT_FILENAME, O_RDWR|O_CREAT, 0644)) < 0)
ae83b6
+		return;
ae83b6
+
ae83b6
+	setttyent ();
ae83b6
+	while ((ty = getttyent ()) != NULL)
ae83b6
+	{
ae83b6
+		++pos;
ae83b6
+		if (strncmp (ty->ty_name, ut->ut_line, sizeof (ut->ut_line)) == 0)
ae83b6
+		{
ae83b6
+			lseek (fd, (off_t)(pos * sizeof(UTMP)), SEEK_SET);
ae83b6
+			write(fd, ut, sizeof(UTMP));
ae83b6
+		}
ae83b6
+	}
ae83b6
+	endttyent ();
ae83b6
+
ae83b6
+	close(fd);
ae83b6
+}
ae83b6
+#else
ae83b6
+#define update_utmp(ut)
ae83b6
+#endif
ae83b6
+
ae83b6
+#if !defined(HAVE_LASTLOG)
ae83b6
+#define update_lastlog(login_name, ut)
ae83b6
+#else
ae83b6
+static void
ae83b6
+update_lastlog(char* login_name, UTMP *ut)
ae83b6
+{
ae83b6
+    struct passwd *pwd;
ae83b6
+    struct lastlog ll;
ae83b6
+    int fd;
ae83b6
+
ae83b6
+    if ((fd = open(LASTLOG_OUTPUT_FILE, O_WRONLY, 0)) < 0)
ae83b6
+	return;
ae83b6
+
ae83b6
+    if ((pwd=getpwnam(login_name)) == NULL)
ae83b6
+	return;
ae83b6
+
ae83b6
+    memset (&ll, 0, sizeof(ll));
ae83b6
+
ae83b6
+    lseek (fd, (off_t)pwd->pw_uid * sizeof (ll), SEEK_SET);
ae83b6
+
ae83b6
+    time (&ll.ll_time);
ae83b6
+
ae83b6
+    strncpy (ll.ll_line, ut->ut_line, sizeof (ll.ll_line));
ae83b6
+
ae83b6
+#if defined(HAVE_UT_UT_HOST)
ae83b6
+    if (ut->ut_host)
ae83b6
+	strncpy (ll.ll_host, ut->ut_host, sizeof (ll.ll_host));
ae83b6
+#endif
ae83b6
+
ae83b6
+    write (fd, (void *)&ll, sizeof (ll));
ae83b6
+    close (fd);
ae83b6
+}
ae83b6
+#endif /* HAVE_LASTLOG */
ae83b6
+
ae83b6
+void
ae83b6
+write_logout_record (char *login_name, void *data, int utmp, int wtmp)
ae83b6
+{
ae83b6
+	UTMP put, *ut = data;
ae83b6
+	struct timeval tv;
ae83b6
+
ae83b6
+	memset (&put, 0, sizeof(UTMP));
ae83b6
+
ae83b6
+#if defined(HAVE_UT_UT_TYPE)
ae83b6
+	put.ut_type = DEAD_PROCESS;
ae83b6
+#endif
ae83b6
+#if defined(HAVE_UT_UT_ID)
ae83b6
+	strncpy (put.ut_id, ut->ut_id, sizeof (put.ut_id));
ae83b6
+#endif
ae83b6
+
ae83b6
+	strncpy (put.ut_line, ut->ut_line, sizeof (put.ut_line));
ae83b6
+
ae83b6
+#if defined(HAVE_UT_UT_TV)
ae83b6
+	gettimeofday(&tv, NULL);
ae83b6
+	put.ut_tv.tv_sec = tv.tv_sec;
ae83b6
+	put.ut_tv.tv_usec = tv.tv_usec;
ae83b6
+#elif defined(HAVE_UT_UT_TIME)
ae83b6
+	time (&put.ut_time);
ae83b6
+#endif
ae83b6
+
ae83b6
+#if defined(HAVE_UT_UT_NAME)
ae83b6
+	strncpy (put.ut_name, login_name, sizeof (put.ut_name));
ae83b6
+#elif defined(HAVE_UT_UT_USER)
ae83b6
+	strncpy (put.ut_user, login_name, sizeof (put.ut_user));
ae83b6
+#endif
ae83b6
+
ae83b6
+	if (utmp)
ae83b6
+		update_utmp (&put;;
ae83b6
+
ae83b6
+	if (wtmp)
ae83b6
+		update_wtmp (WTMP_OUTPUT_FILENAME, &put;;
ae83b6
+
ae83b6
+	free (ut);
ae83b6
+}
ae83b6
+
ae83b6
+void *
ae83b6
+write_login_record (char *login_name, char *display_name,
ae83b6
+		    char *term_name, int utmp, int wtmp, int lastlog)
ae83b6
+{
ae83b6
+	UTMP *ut;
ae83b6
+	char *pty = term_name;
ae83b6
+	struct timeval tv;
ae83b6
+
ae83b6
+	if ((ut=(UTMP *) malloc (sizeof (UTMP))) == NULL)
ae83b6
+		return NULL;
ae83b6
+
ae83b6
+	memset (ut, 0, sizeof (UTMP));
ae83b6
+
ae83b6
+#if defined(HAVE_UT_UT_NAME)
ae83b6
+	strncpy (ut->ut_name, login_name, sizeof (ut->ut_name));
ae83b6
+#elif defined(HAVE_UT_UT_USER)
ae83b6
+	strncpy (ut->ut_user, login_name, sizeof (ut->ut_user));
ae83b6
+#endif
ae83b6
+
ae83b6
+	/* This shouldn't happen */
ae83b6
+	if (strncmp (pty, "/dev/", 5) == 0)
ae83b6
+	    pty += 5;
ae83b6
+
ae83b6
+#if defined(HAVE_STRRCHR)
ae83b6
+	{
ae83b6
+		char *p;
ae83b6
+
ae83b6
+		if (strncmp (pty, "pts", 3) &&
ae83b6
+		    (p = strrchr (pty, '/')) != NULL)
ae83b6
+			pty = p + 1;
ae83b6
+	}
ae83b6
+#endif
ae83b6
+
ae83b6
+#if defined(HAVE_UT_UT_ID)
ae83b6
+	/* Just a safe-guard */
ae83b6
+	ut->ut_id [0] = '\0';
ae83b6
+
ae83b6
+	/* BSD-like terminal name */
ae83b6
+	if (strncmp (pty, "pts", 3) == 0 ||
ae83b6
+	    strncmp (pty, "pty", 3) == 0 ||
ae83b6
+	    strncmp (pty, "tty", 3) == 0) {
ae83b6
+		strncpy (ut->ut_id, pty+3, sizeof (ut->ut_id));
ae83b6
+	} else {
ae83b6
+		unsigned int num;
ae83b6
+		char buf[10];
ae83b6
+		/* Try to get device number and convert it to gnome-terminal # */
ae83b6
+		if (sscanf (pty, "%*[^0-9a-f]%x", &num) == 1) {
ae83b6
+			sprintf (buf, "gt%2.2x", num);
ae83b6
+			strncpy (ut->ut_id, buf, sizeof (ut->ut_id));
ae83b6
+		}
ae83b6
+	}
ae83b6
+#endif
ae83b6
+
ae83b6
+	/* For utmpx ut_line should be null terminated */
ae83b6
+	/* We do that for both cases to be sure */
ae83b6
+	strncpy (ut->ut_line, pty, sizeof (ut->ut_line));
ae83b6
+	ut->ut_line[sizeof (ut->ut_line)-1] = '\0';
ae83b6
+
ae83b6
+	/* We want parent's pid, not our own */
ae83b6
+#if defined(HAVE_UT_UT_PID)
ae83b6
+	ut->ut_pid  = getppid ();
ae83b6
+#endif
ae83b6
+
ae83b6
+#if defined(HAVE_UT_UT_TYPE)
ae83b6
+	ut->ut_type = USER_PROCESS;
ae83b6
+#endif
ae83b6
+	/* If structure has ut_tv it doesn't need ut_time */
ae83b6
+#if defined(HAVE_UT_UT_TV)
ae83b6
+	gettimeofday(&tv, NULL);
ae83b6
+	ut->ut_tv.tv_sec = tv.tv_sec;
ae83b6
+	ut->ut_tv.tv_usec = tv.tv_usec;
ae83b6
+#elif defined(HAVE_UT_UT_TIME)
ae83b6
+	time (&ut->ut_time);
ae83b6
+#endif
ae83b6
+	/* ut_ host supposed to be null terminated or len should */
ae83b6
+	/* be specifid in additional field. We do both :)  */
ae83b6
+#if defined(HAVE_UT_UT_HOST)
ae83b6
+	strncpy (ut->ut_host, display_name, sizeof (ut->ut_host));
ae83b6
+	ut->ut_host [sizeof (ut->ut_host)-1] = '\0';
ae83b6
+#    if defined(HAVE_UT_UT_SYSLEN)
ae83b6
+	ut->ut_syslen = strlen (ut->ut_host);
ae83b6
+#    endif
ae83b6
+#endif
ae83b6
+	if (utmp)
ae83b6
+		update_utmp (ut);
ae83b6
+
ae83b6
+	if (wtmp)
ae83b6
+		update_wtmp (WTMP_OUTPUT_FILENAME, ut);
ae83b6
+
ae83b6
+	if (lastlog)
ae83b6
+		update_lastlog(login_name, ut);
ae83b6
+
ae83b6
+	return ut;
ae83b6
+}
ae83b6
+
ae83b6
+void *
ae83b6
+update_dbs (int utmp, int wtmp, int lastlog, char *login_name, char *display_name, char *term_name)
ae83b6
+{
ae83b6
+	return write_login_record (login_name, display_name,
ae83b6
+				   term_name, utmp, wtmp, lastlog);
ae83b6
+}
ae83b6
diff --git a/src/pty.cc b/src/pty.cc
ae83b6
index aa03c5a5c148..4e5c897a4857 100644
ae83b6
--- a/src/pty.cc
ae83b6
+++ b/src/pty.cc
ae83b6
@@ -67,8 +67,25 @@
ae83b6
 #include <gio/gio.h>
ae83b6
 #include "debug.h"
ae83b6
 
ae83b6
+#ifdef MSG_NOSIGNAL
ae83b6
+#define PTY_RECVMSG_FLAGS MSG_NOSIGNAL
ae83b6
+#else
ae83b6
+#define PTY_RECVMSG_FLAGS 0
ae83b6
+#endif
ae83b6
+
ae83b6
 #include <glib/gi18n-lib.h>
ae83b6
 
ae83b6
+#ifdef VTE_USE_GNOME_PTY_HELPER
ae83b6
+#include <sys/uio.h>
ae83b6
+#ifdef HAVE_SYS_UN_H
ae83b6
+#include <sys/un.h>
ae83b6
+#endif
ae83b6
+#include "../gnome-pty-helper/gnome-pty.h"
ae83b6
+static gboolean _vte_pty_helper_started = FALSE;
ae83b6
+static pid_t _vte_pty_helper_pid = -1;
ae83b6
+static int _vte_pty_helper_tunnel = -1;
ae83b6
+#endif
ae83b6
+
ae83b6
 /* NSIG isn't in POSIX, so if it doesn't exist use this here. See bug #759196 */
ae83b6
 #ifndef NSIG
ae83b6
 #define NSIG (8 * sizeof(sigset_t))
ae83b6
@@ -105,8 +122,11 @@ struct _VtePtyPrivate {
ae83b6
 
ae83b6
         VtePtyChildSetupData child_setup_data;
ae83b6
 
ae83b6
+        gpointer helper_tag; /* only use when using_helper is TRUE */
ae83b6
+
ae83b6
         guint utf8 : 1;
ae83b6
         guint foreign : 1;
ae83b6
+        guint using_helper : 1;
ae83b6
 };
ae83b6
 
ae83b6
 struct _VtePtyClass {
ae83b6
@@ -146,14 +166,16 @@ vte_pty_child_setup (VtePty *pty)
ae83b6
         if (masterfd == -1)
ae83b6
                 _exit(127);
ae83b6
 
ae83b6
-        if (grantpt(masterfd) != 0) {
ae83b6
-                _vte_debug_print(VTE_DEBUG_PTY, "%s failed: %m", "grantpt");
ae83b6
-                _exit(127);
ae83b6
-        }
ae83b6
+        if (!priv->using_helper) {
ae83b6
+                if (grantpt(masterfd) != 0) {
ae83b6
+                        _vte_debug_print(VTE_DEBUG_PTY, "%s failed: %m", "grantpt");
ae83b6
+                        _exit(127);
ae83b6
+                }
ae83b6
 
ae83b6
-	if (unlockpt(masterfd) != 0) {
ae83b6
-                _vte_debug_print(VTE_DEBUG_PTY, "%s failed: %m", "unlockpt");
ae83b6
-                _exit(127);
ae83b6
+                if (unlockpt(masterfd) != 0) {
ae83b6
+                        _vte_debug_print(VTE_DEBUG_PTY, "%s failed: %m", "unlockpt");
ae83b6
+                        _exit(127);
ae83b6
+                }
ae83b6
         }
ae83b6
 
ae83b6
 	char *name = ptsname(masterfd);
ae83b6
@@ -695,6 +717,405 @@ _vte_pty_open_foreign(int masterfd /* consumed */)
ae83b6
         return fd.steal();
ae83b6
 }
ae83b6
 
ae83b6
+#ifdef VTE_USE_GNOME_PTY_HELPER
ae83b6
+#ifdef HAVE_RECVMSG
ae83b6
+static void
ae83b6
+_vte_pty_read_ptypair(int tunnel, int *parentfd, int *childfd)
ae83b6
+{
ae83b6
+	int i, ret;
ae83b6
+	char control[LINE_MAX], iobuf[LINE_MAX];
ae83b6
+	struct cmsghdr *cmsg;
ae83b6
+	struct msghdr msg;
ae83b6
+	struct iovec vec;
ae83b6
+
ae83b6
+	for (i = 0; i < 2; i++) {
ae83b6
+		vec.iov_base = iobuf;
ae83b6
+		vec.iov_len = sizeof(iobuf);
ae83b6
+		msg.msg_name = NULL;
ae83b6
+		msg.msg_namelen = 0;
ae83b6
+		msg.msg_iov = &vec;
ae83b6
+		msg.msg_iovlen = 1;
ae83b6
+		msg.msg_control = control;
ae83b6
+		msg.msg_controllen = sizeof(control);
ae83b6
+		ret = recvmsg(tunnel, &msg, PTY_RECVMSG_FLAGS);
ae83b6
+		if (ret == -1) {
ae83b6
+			return;
ae83b6
+		}
ae83b6
+		for (cmsg = CMSG_FIRSTHDR(&msg;;
ae83b6
+		     cmsg != NULL;
ae83b6
+		     cmsg = CMSG_NXTHDR(&msg, cmsg)) {
ae83b6
+			if (cmsg->cmsg_type == SCM_RIGHTS) {
ae83b6
+				memcpy(&ret, CMSG_DATA(cmsg), sizeof(ret));
ae83b6
+				switch (i) {
ae83b6
+					case 0:
ae83b6
+						*parentfd = ret;
ae83b6
+						break;
ae83b6
+					case 1:
ae83b6
+						*childfd = ret;
ae83b6
+						break;
ae83b6
+					default:
ae83b6
+						g_assert_not_reached();
ae83b6
+						break;
ae83b6
+				}
ae83b6
+			}
ae83b6
+		}
ae83b6
+	}
ae83b6
+}
ae83b6
+#elif defined (I_RECVFD)
ae83b6
+static void
ae83b6
+_vte_pty_read_ptypair(int tunnel, int *parentfd, int *childfd)
ae83b6
+{
ae83b6
+	int i;
ae83b6
+	if (ioctl(tunnel, I_RECVFD, &i) == -1) {
ae83b6
+		return;
ae83b6
+	}
ae83b6
+	*parentfd = i;
ae83b6
+	if (ioctl(tunnel, I_RECVFD, &i) == -1) {
ae83b6
+		return;
ae83b6
+	}
ae83b6
+	*childfd = i;
ae83b6
+}
ae83b6
+#endif
ae83b6
+
ae83b6
+#ifdef HAVE_SOCKETPAIR
ae83b6
+static int
ae83b6
+_vte_pty_pipe_open(int *a, int *b)
ae83b6
+{
ae83b6
+	int p[2], ret = -1;
ae83b6
+#ifdef PF_UNIX
ae83b6
+#ifdef SOCK_STREAM
ae83b6
+	ret = socketpair(PF_UNIX, SOCK_STREAM, 0, p);
ae83b6
+#else
ae83b6
+#ifdef SOCK_DGRAM
ae83b6
+	ret = socketpair(PF_UNIX, SOCK_DGRAM, 0, p);
ae83b6
+#endif
ae83b6
+#endif
ae83b6
+	if (ret == 0) {
ae83b6
+		*a = p[0];
ae83b6
+		*b = p[1];
ae83b6
+		return 0;
ae83b6
+	}
ae83b6
+#endif
ae83b6
+	return ret;
ae83b6
+}
ae83b6
+#else
ae83b6
+static int
ae83b6
+_vte_pty_pipe_open(int *a, int *b)
ae83b6
+{
ae83b6
+	int p[2], ret = -1;
ae83b6
+
ae83b6
+	ret = pipe(p);
ae83b6
+
ae83b6
+	if (ret == 0) {
ae83b6
+		*a = p[0];
ae83b6
+		*b = p[1];
ae83b6
+	}
ae83b6
+	return ret;
ae83b6
+}
ae83b6
+#endif
ae83b6
+
ae83b6
+/* Like read, but hide EINTR and EAGAIN. */
ae83b6
+static ssize_t
ae83b6
+n_read(int fd, void *buffer, size_t count)
ae83b6
+{
ae83b6
+	size_t n = 0;
ae83b6
+	char *buf = static_cast<char *>(buffer);
ae83b6
+	int i;
ae83b6
+	while (n < count) {
ae83b6
+		i = read(fd, buf + n, count - n);
ae83b6
+		switch (i) {
ae83b6
+		case 0:
ae83b6
+			return n;
ae83b6
+		case -1:
ae83b6
+			switch (errno) {
ae83b6
+			case EINTR:
ae83b6
+			case EAGAIN:
ae83b6
+#ifdef ERESTART
ae83b6
+			case ERESTART:
ae83b6
+#endif
ae83b6
+				break;
ae83b6
+			default:
ae83b6
+				return -1;
ae83b6
+			}
ae83b6
+			break;
ae83b6
+		default:
ae83b6
+			n += i;
ae83b6
+			break;
ae83b6
+		}
ae83b6
+	}
ae83b6
+	return n;
ae83b6
+}
ae83b6
+
ae83b6
+/* Like write, but hide EINTR and EAGAIN. */
ae83b6
+static ssize_t
ae83b6
+n_write(int fd, const void *buffer, size_t count)
ae83b6
+{
ae83b6
+	size_t n = 0;
ae83b6
+	const char *buf = static_cast<const char *>(buffer);
ae83b6
+	int i;
ae83b6
+	while (n < count) {
ae83b6
+		i = write(fd, buf + n, count - n);
ae83b6
+		switch (i) {
ae83b6
+		case 0:
ae83b6
+			return n;
ae83b6
+		case -1:
ae83b6
+			switch (errno) {
ae83b6
+			case EINTR:
ae83b6
+			case EAGAIN:
ae83b6
+#ifdef ERESTART
ae83b6
+			case ERESTART:
ae83b6
+#endif
ae83b6
+				break;
ae83b6
+			default:
ae83b6
+				return -1;
ae83b6
+			}
ae83b6
+			break;
ae83b6
+		default:
ae83b6
+			n += i;
ae83b6
+			break;
ae83b6
+		}
ae83b6
+	}
ae83b6
+	return n;
ae83b6
+}
ae83b6
+
ae83b6
+/*
ae83b6
+ * _vte_pty_stop_helper:
ae83b6
+ *
ae83b6
+ * Terminates the running GNOME PTY helper.
ae83b6
+ */
ae83b6
+static void
ae83b6
+_vte_pty_stop_helper(void)
ae83b6
+{
ae83b6
+	if (_vte_pty_helper_started) {
ae83b6
+		close(_vte_pty_helper_tunnel);
ae83b6
+		_vte_pty_helper_tunnel = -1;
ae83b6
+		kill(_vte_pty_helper_pid, SIGTERM);
ae83b6
+		_vte_pty_helper_pid = -1;
ae83b6
+		_vte_pty_helper_started = FALSE;
ae83b6
+	}
ae83b6
+}
ae83b6
+
ae83b6
+/*
ae83b6
+ * _vte_pty_start_helper:
ae83b6
+ * @error: a location to store a #GError, or %NULL
ae83b6
+ *
ae83b6
+ * Starts the GNOME PTY helper process, if it is not already running.
ae83b6
+ *
ae83b6
+ * Returns: %TRUE if the helper was already started, or starting it succeeded,
ae83b6
+ *   %FALSE on failure with @error filled in
ae83b6
+ */
ae83b6
+static gboolean
ae83b6
+_vte_pty_start_helper(GError **error)
ae83b6
+{
ae83b6
+	int i, errsv;
ae83b6
+        int tunnel = -1;
ae83b6
+        int tmp[2] = { -1, -1 };
ae83b6
+
ae83b6
+        if (_vte_pty_helper_started)
ae83b6
+                return TRUE;
ae83b6
+
ae83b6
+	/* Create a communication link for use with the helper. */
ae83b6
+	tmp[0] = open("/dev/null", O_RDONLY);
ae83b6
+	if (tmp[0] == -1) {
ae83b6
+		goto failure;
ae83b6
+	}
ae83b6
+	tmp[1] = open("/dev/null", O_RDONLY);
ae83b6
+	if (tmp[1] == -1) {
ae83b6
+		goto failure;
ae83b6
+	}
ae83b6
+	if (_vte_pty_pipe_open(&_vte_pty_helper_tunnel, &tunnel) != 0) {
ae83b6
+		goto failure;
ae83b6
+	}
ae83b6
+	close(tmp[0]);
ae83b6
+	close(tmp[1]);
ae83b6
+        tmp[0] = tmp[1] = -1;
ae83b6
+
ae83b6
+	/* Now fork and start the helper. */
ae83b6
+	_vte_pty_helper_pid = fork();
ae83b6
+	if (_vte_pty_helper_pid == -1) {
ae83b6
+		goto failure;
ae83b6
+	}
ae83b6
+	if (_vte_pty_helper_pid == 0) {
ae83b6
+		/* Child.  Close descriptors.  No need to close all,
ae83b6
+		 * gnome-pty-helper does that anyway. */
ae83b6
+		for (i = 0; i < 3; i++) {
ae83b6
+			close(i);
ae83b6
+		}
ae83b6
+		/* Reassign the socket pair to stdio. */
ae83b6
+		dup2(tunnel, STDIN_FILENO);
ae83b6
+		dup2(tunnel, STDOUT_FILENO);
ae83b6
+		close(tunnel);
ae83b6
+		close(_vte_pty_helper_tunnel);
ae83b6
+		/* Exec our helper. */
ae83b6
+		execl(LIBEXECDIR "/gnome-pty-helper",
ae83b6
+		      "gnome-pty-helper", (char *) NULL);
ae83b6
+		/* Bail. */
ae83b6
+		_exit(1);
ae83b6
+	}
ae83b6
+	close(tunnel);
ae83b6
+	atexit(_vte_pty_stop_helper);
ae83b6
+
ae83b6
+        _vte_pty_helper_started = TRUE;
ae83b6
+	return TRUE;
ae83b6
+
ae83b6
+failure:
ae83b6
+        errsv = errno;
ae83b6
+
ae83b6
+        g_set_error(error, VTE_PTY_ERROR,
ae83b6
+                    VTE_PTY_ERROR_PTY_HELPER_FAILED,
ae83b6
+                    "Failed to start gnome-pty-helper: %s",
ae83b6
+                    g_strerror (errsv));
ae83b6
+
ae83b6
+        if (tmp[0] != -1)
ae83b6
+                close(tmp[0]);
ae83b6
+        if (tmp[1] != -1)
ae83b6
+                close(tmp[1]);
ae83b6
+        if (tunnel != -1)
ae83b6
+                close(tunnel);
ae83b6
+        if (_vte_pty_helper_tunnel != -1)
ae83b6
+                close(_vte_pty_helper_tunnel);
ae83b6
+
ae83b6
+        _vte_pty_helper_pid = -1;
ae83b6
+        _vte_pty_helper_tunnel = -1;
ae83b6
+
ae83b6
+        errno = errsv;
ae83b6
+        return FALSE;
ae83b6
+}
ae83b6
+
ae83b6
+/*
ae83b6
+ * _vte_pty_helper_ops_from_flags:
ae83b6
+ * @flags: flags from #VtePtyFlags
ae83b6
+ *
ae83b6
+ * Translates @flags into the corresponding op code for the
ae83b6
+ * GNOME PTY helper.
ae83b6
+ *
ae83b6
+ * Returns: the #GnomePtyOps corresponding to @flags
ae83b6
+ */
ae83b6
+static int
ae83b6
+_vte_pty_helper_ops_from_flags (VtePtyFlags flags)
ae83b6
+{
ae83b6
+	int op = 0;
ae83b6
+	static const int opmap[8] = {
ae83b6
+		GNOME_PTY_OPEN_NO_DB_UPDATE,		/* 0 0 0 */
ae83b6
+		GNOME_PTY_OPEN_PTY_LASTLOG,		/* 0 0 1 */
ae83b6
+		GNOME_PTY_OPEN_PTY_UTMP,		/* 0 1 0 */
ae83b6
+		GNOME_PTY_OPEN_PTY_LASTLOGUTMP,		/* 0 1 1 */
ae83b6
+		GNOME_PTY_OPEN_PTY_WTMP,		/* 1 0 0 */
ae83b6
+		GNOME_PTY_OPEN_PTY_LASTLOGWTMP,		/* 1 0 1 */
ae83b6
+		GNOME_PTY_OPEN_PTY_UWTMP,		/* 1 1 0 */
ae83b6
+		GNOME_PTY_OPEN_PTY_LASTLOGUWTMP,	/* 1 1 1 */
ae83b6
+	};
ae83b6
+	if ((flags & VTE_PTY_NO_LASTLOG) == 0) {
ae83b6
+		op += 1;
ae83b6
+	}
ae83b6
+	if ((flags & VTE_PTY_NO_UTMP) == 0) {
ae83b6
+		op += 2;
ae83b6
+	}
ae83b6
+	if ((flags & VTE_PTY_NO_WTMP) == 0) {
ae83b6
+		op += 4;
ae83b6
+	}
ae83b6
+	g_assert(op >= 0 && op < (int) G_N_ELEMENTS(opmap));
ae83b6
+
ae83b6
+        return opmap[op];
ae83b6
+}
ae83b6
+
ae83b6
+/*
ae83b6
+ * _vte_pty_open_with_helper:
ae83b6
+ * @pty: a #VtePty
ae83b6
+ * @error: a location to store a #GError, or %NULL
ae83b6
+ *
ae83b6
+ * Opens a new file descriptor to a new PTY master using the
ae83b6
+ * GNOME PTY helper.
ae83b6
+ *
ae83b6
+ * Returns: %TRUE on success, %FALSE on failure with @error filled in
ae83b6
+ */
ae83b6
+static gboolean
ae83b6
+_vte_pty_open_with_helper(VtePty *pty,
ae83b6
+                          GError **error)
ae83b6
+{
ae83b6
+        VtePtyPrivate *priv = pty->priv;
ae83b6
+	GnomePtyOps ops;
ae83b6
+	int ret;
ae83b6
+	int parentfd = -1, childfd = -1;
ae83b6
+	gpointer tag;
ae83b6
+
ae83b6
+	/* We have to use the pty helper here. */
ae83b6
+	if (!_vte_pty_start_helper(error))
ae83b6
+                return FALSE;
ae83b6
+
ae83b6
+	/* Try to open a new descriptor. */
ae83b6
+
ae83b6
+        ops = static_cast<GnomePtyOps>(_vte_pty_helper_ops_from_flags(priv->flags));
ae83b6
+        /* Send our request. */
ae83b6
+        if (n_write(_vte_pty_helper_tunnel,
ae83b6
+                    &ops, sizeof(ops)) != sizeof(ops)) {
ae83b6
+                g_set_error (error, VTE_PTY_ERROR,
ae83b6
+                              VTE_PTY_ERROR_PTY_HELPER_FAILED,
ae83b6
+                              "Failed to send request to gnome-pty-helper: %s",
ae83b6
+                              g_strerror(errno));
ae83b6
+                return FALSE;
ae83b6
+        }
ae83b6
+        _vte_debug_print(VTE_DEBUG_PTY, "Sent request to helper.\n");
ae83b6
+        /* Read back the response. */
ae83b6
+        if (n_read(_vte_pty_helper_tunnel,
ae83b6
+                    &ret, sizeof(ret)) != sizeof(ret)) {
ae83b6
+                g_set_error (error, VTE_PTY_ERROR,
ae83b6
+                              VTE_PTY_ERROR_PTY_HELPER_FAILED,
ae83b6
+                              "Failed to read response from gnome-pty-helper: %s",
ae83b6
+                              g_strerror(errno));
ae83b6
+                return FALSE;
ae83b6
+        }
ae83b6
+        _vte_debug_print(VTE_DEBUG_PTY,
ae83b6
+                        "Received response from helper.\n");
ae83b6
+        if (ret == 0) {
ae83b6
+                g_set_error_literal (error, VTE_PTY_ERROR,
ae83b6
+                                      VTE_PTY_ERROR_PTY_HELPER_FAILED,
ae83b6
+                                      "gnome-pty-helper failed to open pty");
ae83b6
+                return FALSE;
ae83b6
+        }
ae83b6
+        _vte_debug_print(VTE_DEBUG_PTY, "Helper returns success.\n");
ae83b6
+        /* Read back a tag. */
ae83b6
+        if (n_read(_vte_pty_helper_tunnel,
ae83b6
+                    &tag, sizeof(tag)) != sizeof(tag)) {
ae83b6
+                g_set_error (error, VTE_PTY_ERROR,
ae83b6
+                              VTE_PTY_ERROR_PTY_HELPER_FAILED,
ae83b6
+                              "Failed to read tag from gnome-pty-helper: %s",
ae83b6
+                              g_strerror(errno));
ae83b6
+                return FALSE;
ae83b6
+        }
ae83b6
+        _vte_debug_print(VTE_DEBUG_PTY, "Tag = %p.\n", tag);
ae83b6
+        /* Receive the master and slave ptys. */
ae83b6
+        _vte_pty_read_ptypair(_vte_pty_helper_tunnel,
ae83b6
+                              &parentfd, &childfd);
ae83b6
+
ae83b6
+        _vte_debug_print(VTE_DEBUG_PTY,
ae83b6
+                        "Got master pty %d and slave pty %d.\n",
ae83b6
+                        parentfd, childfd);
ae83b6
+        close(childfd);
ae83b6
+
ae83b6
+        if ((parentfd == -1) ||
ae83b6
+            fd_set_nonblocking(parentfd) < 0 || fd_set_cpkt(parentfd) < 0) {
ae83b6
+                int errsv = errno;
ae83b6
+
ae83b6
+                close(parentfd);
ae83b6
+
ae83b6
+                g_set_error (error, VTE_PTY_ERROR,
ae83b6
+                              VTE_PTY_ERROR_PTY_HELPER_FAILED,
ae83b6
+                              "Failed to read and setup master pty from gnome-pty-helper: %s",
ae83b6
+                              g_strerror(errsv));
ae83b6
+                errno = errsv;
ae83b6
+                return FALSE;
ae83b6
+        }
ae83b6
+
ae83b6
+        priv->using_helper = TRUE;
ae83b6
+        priv->helper_tag = tag;
ae83b6
+        priv->pty_fd = parentfd;
ae83b6
+
ae83b6
+        return TRUE;
ae83b6
+}
ae83b6
+
ae83b6
+#endif /* VTE_USE_GNOME_PTY_HELPER */
ae83b6
+
ae83b6
 /**
ae83b6
  * vte_pty_set_utf8:
ae83b6
  * @pty: a #VtePty
ae83b6
@@ -753,13 +1174,45 @@ vte_pty_set_utf8(VtePty *pty,
ae83b6
  * vte_pty_close:
ae83b6
  * @pty: a #VtePty
ae83b6
  *
ae83b6
- * Since 0.42 this is a no-op.
ae83b6
+ * Cleans up the PTY, specifically any logging performed for the
ae83b6
+ * session. The file descriptor to the PTY master remains open.
ae83b6
  *
ae83b6
  * Deprecated: 0.42
ae83b6
  */
ae83b6
 void
ae83b6
 vte_pty_close (VtePty *pty)
ae83b6
 {
ae83b6
+#ifdef VTE_USE_GNOME_PTY_HELPER
ae83b6
+        VtePtyPrivate *priv = pty->priv;
ae83b6
+	gpointer tag;
ae83b6
+	GnomePtyOps ops;
ae83b6
+
ae83b6
+        if (!priv->using_helper)
ae83b6
+                return;
ae83b6
+
ae83b6
+        /* Signal the helper that it needs to close its connection. */
ae83b6
+        tag = priv->helper_tag;
ae83b6
+
ae83b6
+        ops = GNOME_PTY_CLOSE_PTY;
ae83b6
+        if (n_write(_vte_pty_helper_tunnel,
ae83b6
+                    &ops, sizeof(ops)) != sizeof(ops)) {
ae83b6
+                return;
ae83b6
+        }
ae83b6
+        if (n_write(_vte_pty_helper_tunnel,
ae83b6
+                    &tag, sizeof(tag)) != sizeof(tag)) {
ae83b6
+                return;
ae83b6
+        }
ae83b6
+
ae83b6
+        ops = GNOME_PTY_SYNCH;
ae83b6
+        if (n_write(_vte_pty_helper_tunnel,
ae83b6
+                    &ops, sizeof(ops)) != sizeof(ops)) {
ae83b6
+                return;
ae83b6
+        }
ae83b6
+        n_read(_vte_pty_helper_tunnel, &ops, 1);
ae83b6
+
ae83b6
+        priv->helper_tag = NULL;
ae83b6
+        priv->using_helper = FALSE;
ae83b6
+#endif
ae83b6
 }
ae83b6
 
ae83b6
 /* VTE PTY class */
ae83b6
@@ -788,10 +1241,47 @@ vte_pty_initable_init (GInitable *initable,
ae83b6
 
ae83b6
         if (priv->foreign) {
ae83b6
                 priv->pty_fd = _vte_pty_open_foreign(priv->pty_fd);
ae83b6
-        } else {
ae83b6
-                priv->pty_fd = _vte_pty_open_posix();
ae83b6
+                goto out;
ae83b6
         }
ae83b6
 
ae83b6
+#ifdef VTE_USE_GNOME_PTY_HELPER
ae83b6
+	if ((priv->flags & VTE_PTY_NO_HELPER) == 0) {
ae83b6
+                GError *err = NULL;
ae83b6
+                gboolean ret = FALSE;
ae83b6
+
ae83b6
+		ret = _vte_pty_open_with_helper(pty, &err;;
ae83b6
+                g_assert(ret || err != NULL);
ae83b6
+
ae83b6
+                if (ret)
ae83b6
+                        return TRUE;
ae83b6
+
ae83b6
+                _vte_debug_print(VTE_DEBUG_PTY,
ae83b6
+                                 "_vte_pty_open_with_helper failed: %s\n",
ae83b6
+                                 err->message);
ae83b6
+
ae83b6
+                /* Only do fallback if gnome-pty-helper failed! */
ae83b6
+                if ((priv->flags & VTE_PTY_NO_FALLBACK) ||
ae83b6
+                    !g_error_matches(err,
ae83b6
+                                     VTE_PTY_ERROR,
ae83b6
+                                     VTE_PTY_ERROR_PTY_HELPER_FAILED)) {
ae83b6
+                        g_propagate_error (error, err);
ae83b6
+                        return FALSE;
ae83b6
+                }
ae83b6
+
ae83b6
+                g_error_free(err);
ae83b6
+                /* Fall back to unix98 or bsd PTY */
ae83b6
+        }
ae83b6
+#else
ae83b6
+        if (priv->flags & VTE_PTY_NO_FALLBACK) {
ae83b6
+                g_set_error_literal(error, VTE_PTY_ERROR, VTE_PTY_ERROR_PTY_HELPER_FAILED,
ae83b6
+                                    "VTE compiled without GNOME PTY helper");
ae83b6
+                return FALSE;
ae83b6
+        }
ae83b6
+#endif /* VTE_USE_GNOME_PTY_HELPER */
ae83b6
+
ae83b6
+        priv->pty_fd = _vte_pty_open_posix();
ae83b6
+
ae83b6
+  out:
ae83b6
         if (priv->pty_fd == -1) {
ae83b6
                 vte::util::restore_errno errsv;
ae83b6
                 g_set_error(error, G_IO_ERROR, g_io_error_from_errno(errsv),
ae83b6
@@ -832,6 +1322,8 @@ vte_pty_finalize (GObject *object)
ae83b6
         VtePty *pty = VTE_PTY (object);
ae83b6
         VtePtyPrivate *priv = pty->priv;
ae83b6
 
ae83b6
+        vte_pty_close(pty);
ae83b6
+
ae83b6
         /* Close the master FD */
ae83b6
         if (priv->pty_fd != -1) {
ae83b6
                 close(priv->pty_fd);
ae83b6
@@ -899,7 +1391,8 @@ vte_pty_class_init (VtePtyClass *klass)
ae83b6
         /**
ae83b6
          * VtePty:flags:
ae83b6
          *
ae83b6
-         * Flags.
ae83b6
+         * Controls how the session is recorded in lastlog, utmp, and
ae83b6
+         * wtmp, and whether to use the GNOME PTY helper.
ae83b6
          */
ae83b6
         g_object_class_install_property
ae83b6
                 (object_class,
ae83b6
@@ -970,6 +1463,17 @@ vte_pty_error_quark(void)
ae83b6
  *
ae83b6
  * Also, you MUST pass the %G_SPAWN_DO_NOT_REAP_CHILD flag.
ae83b6
  *
ae83b6
+ * If GNOME PTY Helper is available and
ae83b6
+ * unless some of the %VTE_PTY_NO_LASTLOG, %VTE_PTY_NO_UTMP or
ae83b6
+ * %VTE_PTY_NO_WTMP flags are passed in @flags, the
ae83b6
+ * session is logged in the corresponding lastlog, utmp or wtmp
ae83b6
+ * system files.  When passing %VTE_PTY_NO_HELPER in @flags, the
ae83b6
+ * GNOME PTY Helper is bypassed entirely.
ae83b6
+ *
ae83b6
+ * When passing %VTE_PTY_NO_FALLBACK in @flags,
ae83b6
+ * and opening a PTY using the PTY helper fails, there will
ae83b6
+ * be no fallback to allocate a PTY using POSIX PTY functions.
ae83b6
+ *
ae83b6
  * Returns: (transfer full): a new #VtePty, or %NULL on error with @error filled in
ae83b6
  */
ae83b6
 VtePty *
ae83b6
diff --git a/src/vte.cc b/src/vte.cc
ae83b6
index abb0abec9d23..472b9d3dbacc 100644
ae83b6
--- a/src/vte.cc
ae83b6
+++ b/src/vte.cc
ae83b6
@@ -8608,6 +8608,7 @@ VteTerminalPrivate::~VteTerminalPrivate()
ae83b6
 		g_io_channel_unref (m_pty_channel);
ae83b6
 	}
ae83b6
 	if (m_pty != NULL) {
ae83b6
+                vte_pty_close(m_pty);
ae83b6
                 g_object_unref(m_pty);
ae83b6
 	}
ae83b6
 
ae83b6
@@ -10716,6 +10717,7 @@ VteTerminalPrivate::set_pty(VtePty *new_pty)
ae83b6
 		/* Clear the outgoing buffer as well. */
ae83b6
 		_vte_byte_array_clear(m_outgoing);
ae83b6
 
ae83b6
+                vte_pty_close(m_pty);
ae83b6
                 g_object_unref(m_pty);
ae83b6
                 m_pty = NULL;
ae83b6
         }
ae83b6
diff --git a/src/vte/vteenums.h b/src/vte/vteenums.h
ae83b6
index 54c4ee08579b..c0ec7184b415 100644
ae83b6
--- a/src/vte/vteenums.h
ae83b6
+++ b/src/vte/vteenums.h
ae83b6
@@ -99,7 +99,8 @@ typedef enum {
ae83b6
 
ae83b6
 /**
ae83b6
  * VtePtyError:
ae83b6
- * @VTE_PTY_ERROR_PTY_HELPER_FAILED: Obsolete. Deprecated: 0.42
ae83b6
+ * @VTE_PTY_ERROR_PTY_HELPER_FAILED: failure when using the GNOME PTY
ae83b6
+ * helper to allocate the PTY. Deprecated: 0.42
ae83b6
  * @VTE_PTY_ERROR_PTY98_FAILED: failure when using PTY98 to allocate the PTY
ae83b6
  */
ae83b6
 typedef enum {
ae83b6
@@ -109,11 +110,13 @@ typedef enum {
ae83b6
 
ae83b6
 /**
ae83b6
  * VtePtyFlags:
ae83b6
- * @VTE_PTY_NO_LASTLOG: Unused. Deprecated: 0.38
ae83b6
- * @VTE_PTY_NO_UTMP: Unused. Deprecated: 0.38
ae83b6
- * @VTE_PTY_NO_WTMP: Unused. Deprecated: 0.38
ae83b6
- * @VTE_PTY_NO_HELPER: Unused. Deprecated: 0.38
ae83b6
- * @VTE_PTY_NO_FALLBACK: Unused. Deprecated: 0.38
ae83b6
+ * @VTE_PTY_NO_LASTLOG: don't record the session in lastlog. Deprecated: 0.38
ae83b6
+ * @VTE_PTY_NO_UTMP: don't record the session in utmp. Deprecated: 0.38
ae83b6
+ * @VTE_PTY_NO_WTMP: don't record the session in wtmp.  Deprecated: 0.38
ae83b6
+ * @VTE_PTY_NO_HELPER: don't use the GNOME PTY helper to allocate the
ae83b6
+ * PTY. Deprecated: 0.38
ae83b6
+ * @VTE_PTY_NO_FALLBACK: when allocating the PTY with the PTY helper
ae83b6
+ * fails, don't fall back to try using POSIX. Deprecated: 0.38
ae83b6
  * @VTE_PTY_DEFAULT: the default flags
ae83b6
  */
ae83b6
 typedef enum {
ae83b6
-- 
ae83b6
2.14.4
ae83b6
ae83b6
ae83b6
From c3d59c71cd91a54ba5b00a685809b21ffef5a36a Mon Sep 17 00:00:00 2001
ae83b6
From: Debarshi Ray <debarshir@gnome.org>
ae83b6
Date: Fri, 22 Jun 2018 13:19:56 +0200
ae83b6
Subject: [PATCH 2/4] widget: Make vte_terminal_spawn_async match its
ae83b6
 synchronous variant
ae83b6
ae83b6
It doesn't cause any visible change in behaviour on its own.
ae83b6
ae83b6
However, for downstreams that continue to use gnome-pty-helper, this
ae83b6
avoids a race condition where the helper closes its copy of the
ae83b6
pseudo-terminal slave file descriptor right after VteTerminal has
ae83b6
started polling the master for input, but before the child process has
ae83b6
been forked. This causes VteTerminal to receive a G_IO_HUP and it
ae83b6
stops reading the master for further input. The subsequently forked
ae83b6
child process gets left in a defunct state, and this CRITICAL is
ae83b6
logged:
ae83b6
  Vte-CRITICAL **: void vte_terminal_watch_child(VteTerminal*, GPid):
ae83b6
    assertion 'impl->m_pty != NULL' failed
ae83b6
ae83b6
Polling the pseudo-terminal master device after the child process has
ae83b6
been set up, avoids this race because the child keeps its copy of the
ae83b6
slave file descriptor open. This prevents VteTerminal from receiving
ae83b6
any G_IO_HUP even if the helper closes its copy afterwards.
ae83b6
ae83b6
Fixes GNOME/vte#7:
ae83b6
https://gitlab.gnome.org/GNOME/vte/issues/7
ae83b6
---
ae83b6
 src/vtegtk.cc | 8 ++++----
ae83b6
 1 file changed, 4 insertions(+), 4 deletions(-)
ae83b6
ae83b6
diff --git a/src/vtegtk.cc b/src/vtegtk.cc
ae83b6
index 838d54a152ae..b07eb678e9f7 100644
ae83b6
--- a/src/vtegtk.cc
ae83b6
+++ b/src/vtegtk.cc
ae83b6
@@ -2581,10 +2581,12 @@ spawn_async_cb (GObject *source,
ae83b6
 
ae83b6
         /* Automatically watch the child */
ae83b6
         if (terminal != nullptr) {
ae83b6
-                if (pid != -1)
ae83b6
+                if (pid != -1) {
ae83b6
+                        vte_terminal_set_pty(terminal, pty);
ae83b6
                         vte_terminal_watch_child(terminal, pid);
ae83b6
-                else
ae83b6
+                } else {
ae83b6
                         vte_terminal_set_pty(terminal, nullptr);
ae83b6
+                }
ae83b6
         } else {
ae83b6
                 if (pid != -1) {
ae83b6
                         vte_reaper_add_child(pid);
ae83b6
@@ -2709,8 +2711,6 @@ vte_terminal_spawn_async(VteTerminal *terminal,
ae83b6
                 return;
ae83b6
         }
ae83b6
 
ae83b6
-        vte_terminal_set_pty(terminal, pty);
ae83b6
-
ae83b6
         guint spawn_flags = (guint)spawn_flags_;
ae83b6
 
ae83b6
         /* We do NOT support this flag. If you want to have some FD open in the child
ae83b6
-- 
ae83b6
2.14.4
ae83b6
ae83b6
ae83b6
From feb4f8b4ea9959b0592bbbcbce36667b32f66dbb Mon Sep 17 00:00:00 2001
ae83b6
From: Debarshi Ray <debarshir@gnome.org>
ae83b6
Date: Fri, 22 Jun 2018 14:14:33 +0200
ae83b6
Subject: [PATCH 3/4] gnome-pty-helper: Don't discard the 'const' qualifier
ae83b6
ae83b6
... from pointer target type.
ae83b6
---
ae83b6
 gnome-pty-helper/gnome-pty-helper.c | 2 +-
ae83b6
 gnome-pty-helper/gnome-pty.h        | 2 +-
ae83b6
 gnome-pty-helper/gnome-utmp.c       | 4 ++--
ae83b6
 3 files changed, 4 insertions(+), 4 deletions(-)
ae83b6
ae83b6
diff --git a/gnome-pty-helper/gnome-pty-helper.c b/gnome-pty-helper/gnome-pty-helper.c
ae83b6
index 2666bf4cd46d..1c3cb558e70f 100644
ae83b6
--- a/gnome-pty-helper/gnome-pty-helper.c
ae83b6
+++ b/gnome-pty-helper/gnome-pty-helper.c
ae83b6
@@ -239,7 +239,7 @@ shutdown_helper (void)
ae83b6
 }
ae83b6
 
ae83b6
 static pty_info *
ae83b6
-pty_add (int utmp, int wtmp, int lastlog, char *line, char *login_name)
ae83b6
+pty_add (int utmp, int wtmp, int lastlog, const char *line, char *login_name)
ae83b6
 {
ae83b6
 	pty_info *pi = malloc (sizeof (pty_info));
ae83b6
 
ae83b6
diff --git a/gnome-pty-helper/gnome-pty.h b/gnome-pty-helper/gnome-pty.h
ae83b6
index 94f94f95e248..8aade84d46c8 100644
ae83b6
--- a/gnome-pty-helper/gnome-pty.h
ae83b6
+++ b/gnome-pty-helper/gnome-pty.h
ae83b6
@@ -16,7 +16,7 @@ typedef enum {
ae83b6
 } GnomePtyOps;
ae83b6
 
ae83b6
 void *update_dbs         (int utmp, int wtmp, int lastlog, char *login_name, char *display_name, char *term_name);
ae83b6
-void *write_login_record (char *login_name, char *display_name, char *term_name, int utmp, int wtmp, int lastlog);
ae83b6
+void *write_login_record (char *login_name, char *display_name, const char *term_name, int utmp, int wtmp, int lastlog);
ae83b6
 void write_logout_record (char *login_name, void *data, int utmp, int wtmp);
ae83b6
 
ae83b6
 #endif
ae83b6
diff --git a/gnome-pty-helper/gnome-utmp.c b/gnome-pty-helper/gnome-utmp.c
ae83b6
index 1a090ec8ee38..f4584c2493bb 100644
ae83b6
--- a/gnome-pty-helper/gnome-utmp.c
ae83b6
+++ b/gnome-pty-helper/gnome-utmp.c
ae83b6
@@ -273,10 +273,10 @@ write_logout_record (char *login_name, void *data, int utmp, int wtmp)
ae83b6
 
ae83b6
 void *
ae83b6
 write_login_record (char *login_name, char *display_name,
ae83b6
-		    char *term_name, int utmp, int wtmp, int lastlog)
ae83b6
+		    const char *term_name, int utmp, int wtmp, int lastlog)
ae83b6
 {
ae83b6
 	UTMP *ut;
ae83b6
-	char *pty = term_name;
ae83b6
+	const char *pty = term_name;
ae83b6
 	struct timeval tv;
ae83b6
 
ae83b6
 	if ((ut=(UTMP *) malloc (sizeof (UTMP))) == NULL)
ae83b6
-- 
ae83b6
2.14.4
ae83b6
ae83b6
ae83b6
From 4d53ce761d75d0d830b31fd7d829de8a20c494e2 Mon Sep 17 00:00:00 2001
ae83b6
From: Debarshi Ray <debarshir@gnome.org>
ae83b6
Date: Fri, 22 Jun 2018 13:21:42 +0200
ae83b6
Subject: [PATCH 4/4] gnome-pty-helper: Start removing code for ancient,
ae83b6
 non-GNU/Linux OSes
ae83b6
ae83b6
---
ae83b6
 gnome-pty-helper/Makefile.am           |   3 +
ae83b6
 gnome-pty-helper/configure.ac          |   4 +-
ae83b6
 gnome-pty-helper/gnome-login-support.c | 296 ---------------------------------
ae83b6
 gnome-pty-helper/gnome-login-support.h |  32 ----
ae83b6
 gnome-pty-helper/gnome-pty-helper.c    |   1 +
ae83b6
 5 files changed, 5 insertions(+), 331 deletions(-)
ae83b6
ae83b6
diff --git a/gnome-pty-helper/Makefile.am b/gnome-pty-helper/Makefile.am
ae83b6
index 10a1c896e958..752f1e24b236 100644
ae83b6
--- a/gnome-pty-helper/Makefile.am
ae83b6
+++ b/gnome-pty-helper/Makefile.am
ae83b6
@@ -1,5 +1,8 @@
ae83b6
 libexec_PROGRAMS = gnome-pty-helper
ae83b6
 
ae83b6
+gnome_pty_helper_LDADD = \
ae83b6
+	-lutil
ae83b6
+
ae83b6
 gnome_pty_helper_SOURCES = \
ae83b6
 	gnome-pty.h \
ae83b6
 	gnome-login-support.c \
ae83b6
diff --git a/gnome-pty-helper/configure.ac b/gnome-pty-helper/configure.ac
ae83b6
index c9f0aa460029..e8ee9dcca6cf 100644
ae83b6
--- a/gnome-pty-helper/configure.ac
ae83b6
+++ b/gnome-pty-helper/configure.ac
ae83b6
@@ -15,10 +15,8 @@ AM_MAINTAINER_MODE([enable])
ae83b6
 
ae83b6
 AC_CHECK_HEADERS(sys/syslimits.h sys/time.h sys/types.h sys/un.h alloca.h lastlog.h libutil.h paths.h pty.h stropts.h termios.h ttyent.h util.h utmp.h utmpx.h pty.h util.h libutil.h ttyent.h)
ae83b6
 have_openpty=0
ae83b6
-AC_CHECK_LIB(c,grantpt,true,[AC_CHECK_LIB(pt,grantpt)])
ae83b6
-AC_CHECK_LIB(c,openpty,true,[AC_CHECK_LIB(util,openpty)])
ae83b6
 AC_CHECK_LIB(c,sendmsg,true,[AC_CHECK_LIB(socket,sendmsg,LIBS="$LIBS -lsocket -lnsl",,-lnsl)])
ae83b6
-AC_CHECK_FUNCS(endutent fcntl forkpty getttyent getutent getutmpx grantpt flock login_tty openpty revoke sendmsg seteuid setreuid setutent strrchr updwtmp updwtmpx utmpname utmpxname)
ae83b6
+AC_CHECK_FUNCS(endutent fcntl getttyent getutent getutmpx flock sendmsg seteuid setreuid setutent strrchr updwtmp updwtmpx utmpxname)
ae83b6
 GPH_CHECK_UTMP
ae83b6
 
ae83b6
 AC_CHECK_HEADERS(stropts.h)
ae83b6
diff --git a/gnome-pty-helper/gnome-login-support.c b/gnome-pty-helper/gnome-login-support.c
ae83b6
index c1a3ac67f1f5..531a99959907 100644
ae83b6
--- a/gnome-pty-helper/gnome-login-support.c
ae83b6
+++ b/gnome-pty-helper/gnome-login-support.c
ae83b6
@@ -1,6 +1,5 @@
ae83b6
 /*
ae83b6
  * gnome-login-support.c:
ae83b6
- *    Replacement for systems that lack login_tty, open_pty and forkpty
ae83b6
  *
ae83b6
  * Author:
ae83b6
  *    Miguel de Icaza (miguel@gnu.org)
ae83b6
@@ -8,305 +7,10 @@
ae83b6
  *
ae83b6
  */
ae83b6
 #include <config.h>
ae83b6
-#include <termios.h>
ae83b6
 #include <unistd.h>
ae83b6
-#include <stdio.h>
ae83b6
-#include <stdlib.h>
ae83b6
-#include <fcntl.h>
ae83b6
-#include <string.h>
ae83b6
-#include <sys/ioctl.h>
ae83b6
-#include <termios.h>
ae83b6
-#include <stdlib.h>
ae83b6
-#include <sys/stat.h>
ae83b6
 #include <errno.h>
ae83b6
-#include <grp.h>
ae83b6
-#include <sys/types.h>
ae83b6
 #include "gnome-login-support.h"
ae83b6
 
ae83b6
-/*
ae83b6
- * HAVE_OPENPTY => HAVE_FORKPTY
ae83b6
- */
ae83b6
-
ae83b6
-#ifndef HAVE_LOGIN_TTY
ae83b6
-int
ae83b6
-login_tty (int fd)
ae83b6
-{
ae83b6
-	pid_t pid = getpid ();
ae83b6
-
ae83b6
-	/* Create the session */
ae83b6
-	setsid ();
ae83b6
-
ae83b6
-#ifdef TIOCSCTTY
ae83b6
-	if (ioctl (fd, TIOCSCTTY, 0) == -1)
ae83b6
-		return -1;
ae83b6
-#else /* !TIOCSTTY */
ae83b6
-        /* Hackery to set controlling tty on SVR4 -
ae83b6
-           on SVR4 the first terminal we open after sesid()
ae83b6
-           becomes our controlling terminal, thus we must
ae83b6
-           find the name of, open, and re-close the tty
ae83b6
-           since we already have it open at this point. */
ae83b6
-        {
ae83b6
-                char *ctty;
ae83b6
-                int ct_fdes;
ae83b6
-
ae83b6
-                ctty = ttyname(fd);
ae83b6
-                ct_fdes = open(ctty, O_RDWR);
ae83b6
-                close(ct_fdes);
ae83b6
-        }
ae83b6
-#endif /* !TIOCSTTY */
ae83b6
-
ae83b6
-#if defined (_POSIX_VERSION) || defined (__svr4__)
ae83b6
-	tcsetpgrp (0, pid);
ae83b6
-#elif defined (TIOCSPGRP)
ae83b6
-	ioctl (0, TIOCSPGRP, &pid;;
ae83b6
-#endif
ae83b6
-
ae83b6
-	dup2 (fd, 0);
ae83b6
-	dup2 (fd, 1);
ae83b6
-	dup2 (fd, 2);
ae83b6
-	if (fd > 2)
ae83b6
-		close (fd);
ae83b6
-
ae83b6
-	return 0;
ae83b6
-}
ae83b6
-#endif
ae83b6
-
ae83b6
-#ifndef HAVE_OPENPTY
ae83b6
-static int
ae83b6
-pty_open_master_bsd (char *pty_name, int *used_bsd)
ae83b6
-{
ae83b6
-	int pty_master;
ae83b6
-	char *ptr1, *ptr2;
ae83b6
-
ae83b6
-	*used_bsd = 1;
ae83b6
-
ae83b6
-	strcpy (pty_name, "/dev/ptyXX");
ae83b6
-	for (ptr1 = "pqrstuvwxyzPQRST"; *ptr1; ++ptr1)
ae83b6
-	{
ae83b6
-		pty_name [8] = *ptr1;
ae83b6
-		for (ptr2 = "0123456789abcdef"; *ptr2; ++ptr2)
ae83b6
-		{
ae83b6
-			pty_name [9] = *ptr2;
ae83b6
-
ae83b6
-			/* Try to open master */
ae83b6
-			if ((pty_master = open (pty_name, O_RDWR)) == -1) {
ae83b6
-				if (errno == ENOENT)  /* Different from EIO */
ae83b6
-					return -1;    /* Out of pty devices */
ae83b6
-				else
ae83b6
-					continue;      /* Try next pty device */
ae83b6
-			}
ae83b6
-			pty_name [5] = 't';	       /* Change "pty" to "tty" */
ae83b6
-			if (access (pty_name, (R_OK | W_OK))){
ae83b6
-				close (pty_master);
ae83b6
-				pty_name [5] = 'p';
ae83b6
-				continue;
ae83b6
-			}
ae83b6
-			return pty_master;
ae83b6
-		}
ae83b6
-	}
ae83b6
-	return -1;  /* Ran out of pty devices */
ae83b6
-}
ae83b6
-
ae83b6
-static int
ae83b6
-pty_open_slave_bsd (const char *pty_name)
ae83b6
-{
ae83b6
-	int pty_slave;
ae83b6
-	struct group *group_info = getgrnam ("tty");
ae83b6
-
ae83b6
-	if (group_info != NULL)
ae83b6
-	{
ae83b6
-		/* The following two calls will only succeed if we are root */
ae83b6
-
ae83b6
-		chown (pty_name, getuid (), group_info->gr_gid);
ae83b6
-		chmod (pty_name, S_IRUSR | S_IWUSR | S_IWGRP);
ae83b6
-	}
ae83b6
-	else
ae83b6
-	{
ae83b6
-		chown (pty_name, getuid (), -1);
ae83b6
-		chmod (pty_name, S_IRUSR | S_IWUSR | S_IWGRP);
ae83b6
-	}
ae83b6
-
ae83b6
-#ifdef HAVE_REVOKE
ae83b6
-	revoke (pty_name);
ae83b6
-#endif
ae83b6
-
ae83b6
-	if ((pty_slave = open (pty_name, O_RDWR)) == -1){
ae83b6
-		return -1;
ae83b6
-	}
ae83b6
-
ae83b6
-	return pty_slave;
ae83b6
-}
ae83b6
-
ae83b6
-/* SystemVish pty opening */
ae83b6
-#if defined (HAVE_GRANTPT)
ae83b6
-
ae83b6
-#ifdef HAVE_STROPTS_H
ae83b6
-#    include <stropts.h>
ae83b6
-#endif
ae83b6
-
ae83b6
-static int
ae83b6
-pty_open_slave (const char *pty_name)
ae83b6
-{
ae83b6
-	int pty_slave = open (pty_name, O_RDWR);
ae83b6
-
ae83b6
-	if (pty_slave == -1)
ae83b6
-		return -1;
ae83b6
-
ae83b6
-#ifdef HAVE_STROPTS_H
ae83b6
-#if !defined(__osf__)
ae83b6
-	if (!ioctl (pty_slave, I_FIND, "ptem"))
ae83b6
-		if (ioctl (pty_slave, I_PUSH, "ptem") == -1){
ae83b6
-			close (pty_slave);
ae83b6
-			return -1;
ae83b6
-		}
ae83b6
-
ae83b6
-    if (!ioctl (pty_slave, I_FIND, "ldterm"))
ae83b6
-	    if (ioctl (pty_slave, I_PUSH, "ldterm") == -1){
ae83b6
-		    close (pty_slave);
ae83b6
-		    return -1;
ae83b6
-	    }
ae83b6
-
ae83b6
-#if !defined(sgi) && !defined(__sgi)
ae83b6
-    if (!ioctl (pty_slave, I_FIND, "ttcompat"))
ae83b6
-	    if (ioctl (pty_slave, I_PUSH, "ttcompat") == -1)
ae83b6
-	    {
ae83b6
-		    perror ("ioctl (pty_slave, I_PUSH, \"ttcompat\")");
ae83b6
-		    close (pty_slave);
ae83b6
-		    return -1;
ae83b6
-	    }
ae83b6
-#endif /* sgi || __sgi */
ae83b6
-#endif /* __osf__ */
ae83b6
-#endif /* HAVE_STROPTS_H */
ae83b6
-
ae83b6
-    return pty_slave;
ae83b6
-}
ae83b6
-
ae83b6
-static int
ae83b6
-pty_open_master (char *pty_name, int *used_bsd)
ae83b6
-{
ae83b6
-	int pty_master;
ae83b6
-	char *slave_name;
ae83b6
-
ae83b6
-	strcpy (pty_name, "/dev/ptmx");
ae83b6
-
ae83b6
-	pty_master = open (pty_name, O_RDWR);
ae83b6
-
ae83b6
-	if ((pty_master == -1) && (errno == ENOENT)) {
ae83b6
-		strcpy (pty_name, "/dev/ptc"); /* AIX */
ae83b6
-		pty_master = open (pty_name, O_RDWR);
ae83b6
-	}
ae83b6
-
ae83b6
-	/*
ae83b6
-	 * Try BSD open, this is needed for Linux which
ae83b6
-	 * might have Unix98 devices but no kernel support
ae83b6
-	 * for those.
ae83b6
-	 */
ae83b6
-	if (pty_master == -1) {
ae83b6
-		*used_bsd = 1;
ae83b6
-		return pty_open_master_bsd (pty_name, used_bsd);
ae83b6
-	}
ae83b6
-	*used_bsd = 0;
ae83b6
-
ae83b6
-	if (grantpt (pty_master) == -1 || unlockpt (pty_master) == -1) {
ae83b6
-		close (pty_master);
ae83b6
-		return -1;
ae83b6
-	}
ae83b6
-	if ((slave_name = ptsname (pty_master)) == NULL){
ae83b6
-		close (pty_master);
ae83b6
-		return -1;
ae83b6
-	}
ae83b6
-	strcpy (pty_name, slave_name);
ae83b6
-	return pty_master;
ae83b6
-}
ae83b6
-#else
ae83b6
-#    define pty_open_master pty_open_master_bsd
ae83b6
-#    define pty_open_slave  pty_open_slave_bsd
ae83b6
-#endif
ae83b6
-
ae83b6
-int
ae83b6
-openpty (int *master_fd, int *slave_fd, char *name,
ae83b6
-	 struct termios *termp, struct winsize *winp)
ae83b6
-{
ae83b6
-	int pty_master, pty_slave, used_bsd = 0;
ae83b6
-	struct group *group_info;
ae83b6
-	char line [256];
ae83b6
-
ae83b6
-	pty_master = pty_open_master (line, &used_bsd);
ae83b6
-	fcntl (pty_master, F_SETFD, FD_CLOEXEC);
ae83b6
-
ae83b6
-	if (pty_master == -1)
ae83b6
-		return -1;
ae83b6
-
ae83b6
-	group_info = getgrnam ("tty");
ae83b6
-
ae83b6
-	if (group_info != NULL){
ae83b6
-		chown (line, getuid (), group_info->gr_gid);
ae83b6
-		chmod (line, S_IRUSR | S_IWUSR | S_IWGRP);
ae83b6
-	} else {
ae83b6
-		chown (line, getuid (), -1);
ae83b6
-		chmod (line, S_IRUSR | S_IWUSR | S_IWGRP);
ae83b6
-	}
ae83b6
-
ae83b6
-#ifdef HAVE_REVOKE
ae83b6
-	revoke (line);
ae83b6
-#endif
ae83b6
-
ae83b6
-	/* Open slave side */
ae83b6
-	if (used_bsd)
ae83b6
-		pty_slave = pty_open_slave_bsd (line);
ae83b6
-	else
ae83b6
-		pty_slave = pty_open_slave (line);
ae83b6
-
ae83b6
-	if (pty_slave == -1){
ae83b6
-		close (pty_master);
ae83b6
-
ae83b6
-		errno = ENOENT;
ae83b6
-		return -1;
ae83b6
-	}
ae83b6
-	fcntl (pty_slave, F_SETFD, FD_CLOEXEC);
ae83b6
-
ae83b6
-	*master_fd = pty_master;
ae83b6
-	*slave_fd  = pty_slave;
ae83b6
-
ae83b6
-	if (termp)
ae83b6
-		tcsetattr (pty_slave, TCSAFLUSH, termp);
ae83b6
-
ae83b6
-	if (winp)
ae83b6
-		ioctl (pty_slave, TIOCSWINSZ, winp);
ae83b6
-
ae83b6
-	if (name)
ae83b6
-		strcpy (name, line);
ae83b6
-
ae83b6
-	return 0;
ae83b6
-}
ae83b6
-
ae83b6
-pid_t
ae83b6
-forkpty (int *master_fd, char *name, struct termios *termp, struct winsize *winp)
ae83b6
-{
ae83b6
-	int master, slave;
ae83b6
-	pid_t pid;
ae83b6
-
ae83b6
-	if (openpty (&master, &slave, name, termp, winp) == -1)
ae83b6
-		return -1;
ae83b6
-
ae83b6
-	pid = fork ();
ae83b6
-
ae83b6
-	if (pid == -1)
ae83b6
-		return -1;
ae83b6
-
ae83b6
-	/* Child */
ae83b6
-	if (pid == 0){
ae83b6
-		close (master);
ae83b6
-		login_tty (slave);
ae83b6
-	} else {
ae83b6
-		*master_fd = master;
ae83b6
-		close (slave);
ae83b6
-	}
ae83b6
-
ae83b6
-	return pid;
ae83b6
-}
ae83b6
-#endif /* HAVE_OPENPTY */
ae83b6
-
ae83b6
 int
ae83b6
 n_read (int fd, void *buf, int count)
ae83b6
 {
ae83b6
diff --git a/gnome-pty-helper/gnome-login-support.h b/gnome-pty-helper/gnome-login-support.h
ae83b6
index cc80a46ff3b1..4ed4593f8848 100644
ae83b6
--- a/gnome-pty-helper/gnome-login-support.h
ae83b6
+++ b/gnome-pty-helper/gnome-login-support.h
ae83b6
@@ -1,38 +1,6 @@
ae83b6
 #ifndef _GNOME_LOGIN_SUPPORT_H
ae83b6
 #define _GNOME_LOGIN_SUPPORT_H
ae83b6
 
ae83b6
-#ifdef HAVE_OPENPTY
ae83b6
-#if defined(HAVE_PTY_H)
ae83b6
-#    include <pty.h>
ae83b6
-#elif defined(HAVE_UTIL_H) /* OpenBSD */
ae83b6
-#    include <util.h>
ae83b6
-#elif defined(HAVE_LIBUTIL_H) /* FreeBSD */
ae83b6
-#    include <libutil.h>
ae83b6
-#elif defined(HAVE_LIBUTIL) /* BSDI has libutil, but no libutil.h */
ae83b6
-/* Avoid pulling in all the include files for no need */
ae83b6
-struct termios;
ae83b6
-struct winsize;
ae83b6
-struct utmp;
ae83b6
-	    
ae83b6
-void login (struct utmp *ut);
ae83b6
-int  login_tty (int fd);
ae83b6
-int  logout (char *line);
ae83b6
-void logwtmp (const char *line, const char *name, const char *host);
ae83b6
-int  openpty (int *amaster, int *aslave, char *name, struct termios *termp, struct winsize *winp);
ae83b6
-int  forkpty (int *amaster, char *name, struct termios *termp, struct winsize *winp);
ae83b6
-#endif
ae83b6
-#else
ae83b6
-int openpty (int *master_fd, int *slavefd, char *name, struct termios *termp, struct winsize *winp);
ae83b6
-pid_t forkpty (int *master_fd, char *name, struct termios *termp, struct winsize *winp);
ae83b6
-#endif
ae83b6
-
ae83b6
-#ifndef HAVE_LOGIN_TTY
ae83b6
-int login_tty (int fd);
ae83b6
-#elif defined(HAVE_UTMP_H)
ae83b6
-/* Get the prototype from utmp.h */
ae83b6
-#include <utmp.h>
ae83b6
-#endif
ae83b6
-
ae83b6
 int n_read (int fd, void *buffer, int size);
ae83b6
 int n_write (int fd, const void *buffer, int size);
ae83b6
 
ae83b6
diff --git a/gnome-pty-helper/gnome-pty-helper.c b/gnome-pty-helper/gnome-pty-helper.c
ae83b6
index 1c3cb558e70f..5a8d9f7f95fd 100644
ae83b6
--- a/gnome-pty-helper/gnome-pty-helper.c
ae83b6
+++ b/gnome-pty-helper/gnome-pty-helper.c
ae83b6
@@ -51,6 +51,7 @@ extern char *strdup(const char *);
ae83b6
 #include <stdlib.h>
ae83b6
 #include <string.h>
ae83b6
 #include <stdio.h>
ae83b6
+#include <pty.h>
ae83b6
 #include <utmp.h>
ae83b6
 #include <grp.h>
ae83b6
 #include "gnome-pty.h"
ae83b6
-- 
ae83b6
2.14.4
ae83b6