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

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