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

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