├── ptyopen.1.pdf ├── AUTHORS ├── .gitmodules ├── .gitignore ├── README ├── NEWS ├── Makefile.am ├── ptyopen.spec ├── configure.ac ├── README.md ├── ChangeLog ├── ptyopen.1 ├── ptyopen.1.html ├── COPYING └── ptyopen.c /ptyopen.1.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/F-i-f/ptyopen/master/ptyopen.1.pdf -------------------------------------------------------------------------------- /AUTHORS: -------------------------------------------------------------------------------- 1 | Ptyopen was written by Philippe Troin . 2 | -------------------------------------------------------------------------------- /.gitmodules: -------------------------------------------------------------------------------- 1 | [submodule "m4"] 2 | path = m4 3 | url = https://github.com/F-i-f/fi-autoconf-macros 4 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | \#*# 2 | .#* 3 | *~ 4 | *.backup 5 | 6 | *.[ao] 7 | *.gcda 8 | *.gcno 9 | *.gcov 10 | .deps/ 11 | Makefile 12 | Makefile.in 13 | stamp-h1 14 | 15 | /INSTALL 16 | /aclocal.m4 17 | /autom4te.cache 18 | /config.aux 19 | /config.cache 20 | /config.fi.frag.make 21 | /config.h 22 | /config.h.in 23 | /config.log 24 | /config.status 25 | /configure 26 | 27 | /ptyopen 28 | /ptyopen-*.tar.* 29 | -------------------------------------------------------------------------------- /README: -------------------------------------------------------------------------------- 1 | ptyopen - runs a program on a fake terminal 2 | 3 | Documentation overview: 4 | 5 | * ChangeLog - Detailed change list 6 | * INSTALL - Generic install instructions 7 | * NEWS - Release notes 8 | * README - This file 9 | * README.md - Detailed help file 10 | * ptyopen.1 - Manual page 11 | * ptyopen.1.html - Manual page HTML hard copy 12 | * ptyopen.1.pdf - Manual page PDF hard copy 13 | -------------------------------------------------------------------------------- /NEWS: -------------------------------------------------------------------------------- 1 | NEWS file for ptyopen -*- outline -*- 2 | 3 | * Changes in 0.95. 4 | 5 | ** Updated autoconf/automake build system. 6 | ** Modernized C (remove K&R prototypes). 7 | ** Show git revision in --version. 8 | ** Compiles cleanly with gcc 14. 9 | ** Provide HTML and PDF versions of the manpage. 10 | ** Provide RPM spec file. 11 | 12 | * Changes in 0.94. 13 | 14 | ** Fixed a controlling tty problem on SysV systems. 15 | 16 | Earlier releases of ptyopen did not set the controlling terminal 17 | correctly on SysV systems (lacking a TIOCSCTTY ioctl). 18 | -------------------------------------------------------------------------------- /Makefile.am: -------------------------------------------------------------------------------- 1 | # ptyopen - controls command in a pseudo-terminal 2 | # Makefile.am 3 | # Copyright (C) 2000, 2001, 2024 Philippe Troin 4 | # 5 | # This program is free software; you can redistribute it and/or modify 6 | # it under the terms of the GNU General Public License as published by 7 | # the Free Software Foundation; either version 3 of the License, or 8 | # (at your option) any later version. 9 | # 10 | # This program is distributed in the hope that it will be useful, 11 | # but WITHOUT ANY WARRANTY; without even the implied warranty of 12 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 | # GNU General Public License for more details. 14 | # 15 | # You should have received a copy of the GNU General Public License 16 | # along with this program; if not, write to the Free Software 17 | # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 18 | 19 | ACLOCAL_AMFLAGS = -I m4 20 | 21 | bin_PROGRAMS = ptyopen 22 | ptyopen_SOURCES = ptyopen.c 23 | dist_man_MANS = ptyopen.1 24 | 25 | dist_doc_DATA = ChangeLog \ 26 | README \ 27 | README.md \ 28 | NEWS 29 | 30 | EXTRA_DIST = ptyopen.spec \ 31 | ptyopen.1.html \ 32 | ptyopen.1.pdf 33 | 34 | INSTALL_PROGRAM = $(INSTALL) @exeinstflags@ ${AM_INSTALL_PROGRAM_FLAGS} 35 | 36 | @FI_AUTOMAKE@ 37 | -------------------------------------------------------------------------------- /ptyopen.spec: -------------------------------------------------------------------------------- 1 | Name: ptyopen 2 | Version: 0.95 3 | Release: 1%{?dist} 4 | Summary: Runs programs with a pseudo-terminal 5 | Group: System Environment/System Tools 6 | License: GPLv3 7 | URL: https://github.com/F-i-f/%{name} 8 | Source: https://github.com/F-i-f/%{name}/releases/download/v%{version}/%{name}-%{version}.tar.gz 9 | BuildRequires: gcc 10 | 11 | %description 12 | Ptyopen allows to run programs like if they were connected to a physical terminal. 13 | This is handy for scripting programs that require a terminal 14 | 15 | %prep 16 | %setup -q 17 | 18 | %build 19 | %configure --enable-compiler-warnings --docdir="%{_docdir}/%{name}" 20 | make %{?_smp_mflags} 21 | 22 | %install 23 | rm -rf $RPM_BUILD_ROOT 24 | %make_install 25 | 26 | %files 27 | %{_bindir}/ptyopen 28 | %{_mandir}/man1/ptyopen.1.gz 29 | %{_docdir}/%{name} 30 | 31 | %changelog 32 | * Tue Sep 10 2024 Philippe Troin - 0.95-1 33 | - Upstream updated to 0.95. 34 | - Package extra doc files. 35 | 36 | * Tue Apr 23 2024 Philippe Troin - 0.94-4 37 | - Disable implicit int warnings (configure fails otherwise). 38 | 39 | * Wed Oct 16 2019 Philippe Troin - 0.94-3 40 | - Add BuildRequires: gcc. 41 | - Lint spec. 42 | 43 | * Tue Jun 5 2012 Philippe Troin - 0.94-2 44 | - Add a group. 45 | 46 | * Mon Jun 4 2012 Philippe Troin - 0.94-1 47 | - Created. 48 | -------------------------------------------------------------------------------- /configure.ac: -------------------------------------------------------------------------------- 1 | # ptyopen - controls command in a pseudo-terminal 2 | # configure.in 3 | # Copyright (C) 2000-2001, 2024 Philippe Troin 4 | # 5 | # This program is free software; you can redistribute it and/or modify 6 | # it under the terms of the GNU General Public License as published by 7 | # the Free Software Foundation; either version 3 of the License, or 8 | # (at your option) any later version. 9 | # 10 | # This program is distributed in the hope that it will be useful, 11 | # but WITHOUT ANY WARRANTY; without even the implied warranty of 12 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 | # GNU General Public License for more details. 14 | # 15 | # You should have received a copy of the GNU General Public License 16 | # along with this program; if not, write to the Free Software 17 | # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 18 | 19 | AC_INIT([ptyopen],[0.95]) 20 | AC_CONFIG_AUX_DIR([config.aux]) 21 | FI_PROJECT() 22 | 23 | # Get started 24 | AC_CONFIG_SRCDIR([ptyopen.c]) 25 | AC_LANG([C]) 26 | 27 | ### 1) Checking for system 28 | 29 | ### 2) Checking for programs 30 | AC_PROG_CC 31 | FI_COMPILER_WARNINGS 32 | AC_PROG_CPP 33 | FI_FORMAT_MAN([ptyopen.1], [html pdf]) 34 | 35 | FI_GIT_REVISION 36 | 37 | ### 3) Checking for libraries 38 | ### 4) Checking for header files 39 | AC_CHECK_HEADERS([stropts.h]) 40 | 41 | ### 5) Checking for typedefs 42 | ### 6) Checking for structures 43 | ### 7) Checking for compiler characteristics 44 | ### 8) Checking for library functions 45 | AC_CHECK_FUNCS([strsignal getpt grantpt unlockpt isastream]) 46 | if test "$ac_cv_func_grantpt" = "yes" 47 | then 48 | exeinstflags="-m 755" 49 | else 50 | exeinstflags="-m 4755 -o root -g root" 51 | fi 52 | AC_SUBST(exeinstflags) 53 | 54 | ### 9) Check for "system services" 55 | 56 | ### END 57 | 58 | AC_CONFIG_HEADERS([config.h]) 59 | AC_CONFIG_FILES([Makefile]) 60 | AC_OUTPUT 61 | 62 | # Local variables: 63 | # mode: sh 64 | # end: 65 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | ptyopen 2 | ======= 3 | runs a program on a fake terminal 4 | ================================= 5 | 6 | ## Documentation 7 | 8 | **ptyopen** comes with an [extensive manual 9 | page](https://htmlpreview.github.io/?https://raw.githubusercontent.com/F-i-f/ptyopen/master/ptyopen.1.html). 10 | 11 | [View](https://htmlpreview.github.io/?https://raw.githubusercontent.com/F-i-f/ptyopen/master/ptyopen.1.html) or 12 | download the manual page as: 13 | [[HTML]](https://raw.githubusercontent.com/F-i-f/ptyopen/master/ptyopen.1.html), 14 | [[PDF]](https://raw.githubusercontent.com/F-i-f/ptyopen/master/ptyopen.1.pdf) or 15 | [[ROFF]](https://raw.githubusercontent.com/F-i-f/ptyopen/master/ptyopen.1). 16 | 17 | ## License 18 | 19 | **ptyopen** is free software: you can redistribute it and/or modify it under 20 | the terms of the GNU General Public License as published by the Free 21 | Software Foundation, either version 3 of the License, or (at your 22 | option) any later version. 23 | 24 | This program is distributed in the hope that it will be useful, but 25 | WITHOUT ANY WARRANTY; without even the implied warranty of 26 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 27 | General Public License for more details. 28 | 29 | You should have received a copy of the GNU General Public License 30 | along with this program. If not, see [http://www.gnu.org/licenses/]. 31 | 32 | ## Building from source 33 | 34 | ### Requirements 35 | 36 | * C Compiler (eg. gcc) 37 | 38 | * Make (eg. GNU make) 39 | 40 | * autotools (autoconf, automake) is only required if building from the 41 | repository. 42 | 43 | * groff is optionally needed to generate the man page hard copies 44 | (HTML & PDF). It is only needed if you intend to update the manual 45 | page ROFF source. 46 | 47 | ### From a release 48 | 49 | Download the [latest release from 50 | GitHub](https://github.com/F-i-f/ptyopen/releases/download/v0.95/ptyopen-0.95.tar.gz) 51 | or the [secondary mirror](http://ftp.fifi.org/phil/ptyopen/ptyopen-0.95.tar.gz): 52 | 53 | * [Primary Site (GitHub)](https://github.com/F-i-f/ptyopen/releases/): 54 | 55 | * Source: 56 | [https://github.com/F-i-f/ptyopen/releases/download/v0.95/ptyopen-0.95.tar.gz](https://github.com/F-i-f/ptyopen/releases/download/v0.95/ptyopen-0.95.tar.gz) 57 | 58 | * Signature: 59 | [https://github.com/F-i-f/ptyopen/releases/download/v0.95/ptyopen-0.95.tar.gz.asc](https://github.com/F-i-f/ptyopen/releases/download/v0.95/ptyopen-0.95.tar.gz.asc) 60 | 61 | * [Secondary Site](http://ftp.fifi.org/phil/ptyopen/): 62 | 63 | * Source: 64 | [http://ftp.fifi.org/phil/ptyopen/ptyopen-0.95.tar.gz](http://ftp.fifi.org/phil/ptyopen/ptyopen-0.95.tar.gz) 65 | 66 | * Signature: 67 | [http://ftp.fifi.org/phil/ptyopen/ptyopen-0.95.tar.gz.asc](http://ftp.fifi.org/phil/ptyopen/ptyopen-0.95.tar.gz.asc) 68 | 69 | 70 | The source code release are signed with the GPG key ID `0x88D51582`, 71 | available on your [nearest GPG server](https://pgp.mit.edu/) or 72 | [here](http://ftp.fifi.org/phil/GPG-KEY). 73 | 74 | You can also find all releases on the [GitHub release 75 | page](https://github.com/F-i-f/ptyopen/releases/) and on the [secondary 76 | mirror](http://ftp.fifi.org/phil/ptyopen/). 77 | 78 | When downloading from the GitHub release pages, be careful to download 79 | the source code from the link named with the full file name 80 | (_ptyopen-0.95.tar.gz_), and **not** from the links marked _Source code 81 | (zip)_ or _Source code (tar.gz)_ as these are repository snapshots 82 | generated automatically by GitHub and require specialized tools to 83 | build (see [Building from GitHub](#from-the-github-repository)). 84 | 85 | 86 | After downloading the sources, unpack and build with: 87 | 88 | ```shell 89 | tar xvzf ptyopen-0.95.tar.gz 90 | cd ptyopen-0.95 91 | ./configure 92 | make 93 | make install 94 | make install-pdf install-html # Optional 95 | ``` 96 | 97 | Alternately, you can create a RPM file by moving the source tar file 98 | and the included `ptyopen.spec` in your rpm build directory and running: 99 | 100 | ```shell 101 | rpmbuild -ba SPECS/ptyopen.spec 102 | ``` 103 | 104 | ### From the GitHub repository 105 | 106 | Clone the [repository](https://github.com/F-i-f/ptyopen.git), including submodules: 107 | 108 | ```shell 109 | git clone --recurse-submodules https://github.com/F-i-f/ptyopen.git 110 | cd ptyopen 111 | autoreconf -i 112 | ./configure 113 | make 114 | make install 115 | make install-pdf install-html # Optional 116 | ``` 117 | 118 | ## Changelog 119 | 120 | See release notes in 121 | [NEWS](https://github.com/F-i-f/ptyopen/blob/master/ChangeLog) or the 122 | detailed [ChangeLog](https://github.com/F-i-f/ptyopen/blob/master/ChangeLog). 123 | 124 | ## Credits 125 | 126 | **ptyopen** was written by Philippe Troin ([F-i-f on GitHub](https://github.com/F-i-f)). 127 | -------------------------------------------------------------------------------- /ChangeLog: -------------------------------------------------------------------------------- 1 | 2024-09-10 Philippe Troin 2 | 3 | * Makefile.am (EXTRA_DIST): Ship ptyopen.spec. 4 | 5 | * README.md: Include submodules when cloning from github. 6 | 7 | * ptyopen.spec (Version): Bump version to 0.95. 8 | (URL, Source): Refer to GitHub. 9 | (%build): Don't override CFLAGS, enable compiler warnings, 10 | override docdir. 11 | (%install): User %make_install. 12 | 13 | * configure.ac (AC_INIT): Bump version to 0.95. 14 | 15 | * NEWS, README.md: Release 0.95. 16 | 17 | * ptyopen.spec (%files): Package %{_docdir} rather than individual 18 | files through %doc. Files are already installed. 19 | 20 | * Makefile.am (dist_doc_DATA): Add READMEs, changelog and news. 21 | 22 | * README.md: Create. 23 | 24 | * ptyopen.spec: Import from SRPM. 25 | 26 | * Makefile.am (EXTRA_DIR): Add ptyopen.1.pdf/html. 27 | 28 | * configure.ac (FI_FORMAT_MAN): Provide pre-build PDF and HTML 29 | versions of the manpage. 30 | 31 | * ptyopen.1.html, ptyopen.1.pdf: Add. 32 | 33 | * ptyopen.1: Update copyright. 34 | 35 | * ptyopen.c (main): Update copyright. 36 | 37 | * ptyopen.c (main): Show GIT_REVISION in "ptyopen --version". 38 | 39 | * configure.ac (FI_GIT_REVISION): Add. 40 | 41 | * .gitignore: Create. 42 | 43 | * ptyopen.c (UNUSED): Define. 44 | (getprivs, dropprivs): Pacify gcc, prefer "do {} while" over "do ; 45 | while". 46 | (sig_chld_h, sig_cont_h): Mark sig UNUSED. 47 | (main, getpttypair, saveperms, restoreperms, setperms, loop_on) 48 | (dropprivs, getprivs, ispriv, xmalloc, ring_check) 49 | (ring_construct, ring_delete, ring_space, ring_read, ring_write) 50 | (ring_push_char, cleanup, term_raw, term_restore, term_winsize) 51 | (sig_chld_h, sig_fatal_h, sig_tstp_h, sig_cont_h, sig_winch_h): 52 | Use ANSI prototypes in function definition. 53 | (_P_): Remove. 54 | 55 | * README, AUTHORS: Create. 56 | 57 | * autoboot, config.h.in: Delete. 58 | 59 | * Makefile.am (ACLOCAL_AMFLAGS): Add. 60 | (man_MANS): Rename to dist_man_MANS. 61 | (EXTRA_DIST, CONFIG_FILES, CLEANFILES, MAINTAINERCLEANFILES): 62 | Remove, now handled by FI_PROJECT. 63 | (@FI_AUTOMAKE@): Add marker. 64 | 65 | * configure.ac (AC_INIT): Modernize. 66 | (AC_CONFIG_AUX_DIR): Put automake generated file in subdirectory. 67 | (FI_PROJECT): Use fi-autoconf-macros' FI_PROJECT. 68 | (AC_CONFIG_SRCDIR, AC_LANG): Add. 69 | (AC_CHECK_HEADERS, AC_CHECK_FUNCS): Quote arguments. 70 | (AC_CONFIG_HEADER): Rename to AC_CONFIG_HEADERS, quote argument. 71 | (AC_CONFIG_FILES): Add. 72 | (AC_OUTPUT): Modernize. 73 | 74 | * m4: Instantiate https://github.com/F-i-f/fi-autoconf-macros 75 | submodule. 76 | 77 | * configure.in: Rename to configure.ac. 78 | 79 | 2001-11-16 Philippe Troin 80 | 81 | * Released 0.94. 82 | 83 | * NEWS: Created. 84 | 85 | * configure.in: Bumped version to 0.94. 86 | 87 | * ptyopen.c: Added prototype for ring_check. 88 | (ring_check): Fixed signedness problems. 89 | (ring_write): Same. 90 | (sig_cont_h): Pretend to use the param. 91 | (main): Do reopen on slave tty SysVish systems which do NOT have 92 | TIOCSCTTY. 93 | 94 | 2001-07-29 Philippe Troin 95 | 96 | * Released 0.93. 97 | 98 | * configure.in: Bumped version to 0.93. 99 | 100 | * autoboot: Created. 101 | 102 | * Makefile.am (EXTRA_DIST): Removed Debian stuff. 103 | (EXTRA_DIST): Added an autoboot script. 104 | 105 | 2000-10-17 Philippe Troin 106 | 107 | * ptyopen.1: Added copyright notice in manpage and fixed typos. 108 | 109 | * ptyopen.c (main): Includes copyright notice in --version. 110 | 111 | 2000-09-30, 24 Philippe Troin 112 | 113 | * Released 0.92. 114 | 115 | * configure.in: Bumped version to 0.92. 116 | 117 | * Ported to HP-UX 11. 118 | 119 | * ptyopen.c (loop_on): Fixed an ugly race condition where the 120 | child status changes between the time it's tested and we get into 121 | select(). We use the W.R. Stevens pipe write in signal handler 122 | trick. 123 | (main): Don't complain on -u on platforms not requiring it. 124 | (term_raw): Set VMIN to 1 in termios on current terminal for 125 | consistency. 126 | 127 | 2000-09-26 Philippe Troin 128 | 129 | * Released 0.91. 130 | 131 | * configure.in: Bumped version to 0.91. 132 | 133 | * ptyopen.c (main): Use strsignal only when we have it. 134 | 135 | * configure.in: Added check for strsignal. 136 | 137 | 2000-09-20 Philippe Troin 138 | 139 | * Released 0.90. 140 | 141 | * Makefile.am (EXTRA_DIST): Added debian dir to dist. 142 | 143 | * configure.in: Autoconf variable for passing to "make install" 144 | the right (eventually suid) flags depending on wether or not we 145 | use SVr4 ptys. 146 | 147 | * Makefile.am (INSTALL_PROGRAM): Have the install flags for 148 | ptyopen depend on an autoconf variable. 149 | 150 | * configure.in: Bumped up version to 0.90. 151 | 152 | * ptyopen.c: Added copyright notice. Includes config.h if 153 | needed. Includes stropt.h if needed. Enable SVr4 streams. 154 | (xstrdup): Created. 155 | (term_raw): Make sure to clear VMIN and VMAX control characters to 156 | get zero delays in non-canonical terminal input. 157 | (term_winsize): Missing end-of-lines. 158 | 159 | * ptyopen.1: Clarify the -u usage with SVr4 ptys. 160 | 161 | * configure.in: Auto-detect SVr4 ptys. 162 | 163 | 2000-07-27 Philippe Troin 164 | 165 | * Makefile.am (EXTRA_DIST): Added man_MANS. 166 | 167 | * configure.in: Bumped version to 0.81. 168 | 169 | * ptyopen.c (main): Window size must be set even if we're not on a 170 | tty but a geometry has been passed. 171 | -------------------------------------------------------------------------------- /ptyopen.1: -------------------------------------------------------------------------------- 1 | .\" ptyopen - runs a program on a fake terminal 2 | .\" ptyopen.1 - man page 3 | .\" Copyright (C) 1999-2000, 2024 Philippe Troin 4 | .\" 5 | .\" This program is free software; you can redistribute it and/or modify 6 | .\" it under the terms of the GNU General Public License as published by 7 | .\" the Free Software Foundation; either version 3 of the License, or 8 | .\" (at your option) any later version. 9 | .\" 10 | .\" This program is distributed in the hope that it will be useful, 11 | .\" but WITHOUT ANY WARRANTY; without even the implied warranty of 12 | .\" MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 | .\" GNU General Public License for more details. 14 | .\" 15 | .\" You should have received a copy of the GNU General Public License 16 | .\" along with this program; if not, write to the Free Software 17 | .\" Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 18 | .TH PTYOPEN 1 "" "Debian" "" 19 | 20 | .SH NAME 21 | ptyopen \- Runs a command in a pseudo-terminal and redirects it to stdin/stdout 22 | 23 | .SH SYNOPSIS 24 | .B ptyopen 25 | [-v] [--verbose] [-V] [--version] [-u] [--unsecure] [-w] [--write] [-r 26 | .I size 27 | ] [ --ringsize 28 | .I size 29 | ] [-g 30 | .I geometry 31 | ] [--geometry 32 | .I geometry 33 | ] [-h] [--help] 34 | .I command [arguments...] 35 | 36 | .SH DESCRIPTION 37 | .PP 38 | .B ptyopen 39 | creates a pseudo-terminal and runs the specified 40 | .I command 41 | with its 42 | .I arguments 43 | in it. All tty output of the slave 44 | .I command 45 | is redirected to 46 | .B ptyopen's 47 | stdout. All data fed into 48 | .B ptyopen's 49 | stdin is presented to the slave 50 | .I command 51 | as terminal input. 52 | .PP 53 | .B ptyopen 54 | is useful to control programmatically commands that use the terminal 55 | directly (typically by opening /dev/tty), like 56 | .B dump(1) 57 | or 58 | .B telnet(1) 59 | for example. 60 | 61 | .SH OPTIONS 62 | .TP 63 | .I -v, --verbose 64 | Runs verbosely. Namely, 65 | .B ptyopen 66 | will report "benign" errors while handling terminal capabilities and 67 | window sizing. 68 | .B ptyopen 69 | will also report verbosely the exit code of 70 | .I command. 71 | 72 | .TP 73 | .I -V, --version 74 | Prints 75 | .B ptyopen's 76 | version. 77 | 78 | .TP 79 | .I -u, --unsecure 80 | If running under a system which requires 81 | .B ptyopen 82 | to be setuid root, the unsecure option allows 83 | .B ptyopen 84 | to run without changing the permissions and ownership of the 85 | pseudo-terminal (and indeed makes 86 | .B ptyopen 87 | usable without making it setuid, at the expense of security). This 88 | option is not available on systems not requiring 89 | .B ptyopen 90 | to be setuid root (Linux >= 2.2 with glibc >= 2.1, Solaris, ...) 91 | 92 | .TP 93 | .I -w, --write 94 | Allows write permissions to the pseudo-terminal. This is probably useless, 95 | unless you're running some interactive shell under 96 | .B ptyopen. 97 | 98 | .TP 99 | .I -r, --ringsize 100 | Takes an argument, the number of bytes of 101 | .B ptyopen's 102 | input and output buffers. When writing to 103 | .B ptyopen's 104 | stdin, 105 | .B ptyopen 106 | buffers some amount of data. Similarly, when the 107 | .I program 108 | writes to the terminal, 109 | .B ptyopen 110 | buffers the output before sending it to stdout. 111 | One might want to change the default buffer size for obscure reasons. 112 | 113 | .TP 114 | .I -g, --geometry 115 | Makes 116 | .I program 117 | believe it runs in a terminal whose size is given by 118 | .I . 119 | For example 80x25 or 132x50. 120 | 121 | .TP 122 | .I -h, --help 123 | Prints a summary of these options. 124 | 125 | .SH NOTES 126 | .SS SIGNALS 127 | .B ptyopen 128 | traps almost all common deadly signals so that the pseudo-terminal 129 | permissions are restored correctly. 130 | 131 | .PP 132 | .B ptyopen 133 | also handles SIGTSTP and SIGCONT so that the slave 134 | .I command 135 | gets stopped and restarted when 136 | .B ptyopen's 137 | state changes. 138 | 139 | .PP 140 | Finally, window size changes get communicated correctly through 141 | SIGWINCH, unless the 142 | .I -g 143 | or 144 | .I --geometry 145 | (forced geometry) options are used. 146 | 147 | .SS BUFFERS 148 | .B ptyopen 149 | uses some internal buffers, which can be resized with the 150 | .I -r 151 | or 152 | .I --ringsize 153 | options. These buffers are in used in addition to the buffers used by 154 | the pseudo-terminal driver, and eventually the pipes and sockets 155 | connected to 156 | .B ptyopen's 157 | stdin and stdout. 158 | 159 | .SS END OF FILE 160 | If 161 | .B ptyopen's 162 | stdin is closed, then an EOF character (as determined by the 163 | pseudo-terminal's capabilities) is sent to the terminal. Whether or not 164 | the slave 165 | .I command 166 | will take action on this EOF character depends on the 167 | .I command 168 | itself, and not 169 | .B ptyopen. 170 | 171 | .SS EXIT CODE 172 | .B ptyopen 173 | exits with the exit code of the slave 174 | .I command 175 | unless it encounters some error. In that case it will exit with 255. 176 | 177 | .SH SECURITY 178 | On BSD systems, 179 | .B ptyopen 180 | needs to be setuid root so that the pseudo-terminal's permissions and 181 | ownership can be changed. 182 | .B ptyopen 183 | will refuse to run without being setuid root (unless the 184 | .I -u 185 | or 186 | .I --unsecure 187 | options are used). 188 | .PP 189 | Although great care has been taken to ensure that 190 | .B ptyopen 191 | does not present a security risk, your mileage may vary. 192 | 193 | .SH SEE ALSO 194 | .B expect(1), pty(7). 195 | 196 | .SH AUTHOR 197 | Written and Copyright (C) 1998-2024 Philippe Troin . 198 | THIS PROGRAM COMES WITH ABSOLUTELY NO WARRANTY. 199 | 200 | .SH HISTORY 201 | This program was written as a way to control 202 | .B dump(8) 203 | from a backup script. 204 | \" LocalWords: ptyopen Troin phil Exp Debian stdin stdout unsecure ringsize 205 | \" LocalWords: ptyopen's programmatically setuid glibc Solaris SIGTSTP EOF 206 | \" LocalWords: SIGCONT SIGWINCH pty 207 | -------------------------------------------------------------------------------- /ptyopen.1.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 5 | 6 | 7 | 8 | 9 | 10 | 16 | PTYOPEN 17 | 18 | 19 | 20 | 21 |

PTYOPEN

22 | 23 | NAME
24 | SYNOPSIS
25 | DESCRIPTION
26 | OPTIONS
27 | NOTES
28 | SIGNALS
29 | BUFFERS
30 | END OF FILE
31 | EXIT CODE
32 | SECURITY
33 | SEE ALSO
34 | AUTHOR
35 | HISTORY
36 | 37 |
38 | 39 | 40 |

NAME 41 | 42 |

43 | 44 | 45 |

ptyopen − 46 | Runs a command in a pseudo-terminal and redirects it to 47 | stdin/stdout

48 | 49 |

SYNOPSIS 50 | 51 |

52 | 53 | 54 |

ptyopen 55 | [-v] [--verbose] [-V] [--version] [-u] [--unsecure] [-w] 56 | [--write] [-r size ] [ --ringsize size ] [-g 57 | geometry ] [--geometry geometry ] [-h] 58 | [--help] command [arguments...]

59 | 60 |

DESCRIPTION 61 | 62 |

63 | 64 | 65 | 66 |

ptyopen 67 | creates a pseudo-terminal and runs the specified 68 | command with its arguments in it. All tty 69 | output of the slave command is redirected to 70 | ptyopen’s stdout. All data fed into 71 | ptyopen’s stdin is presented to the slave 72 | command as terminal input.

73 | 74 |

ptyopen 75 | is useful to control programmatically commands that use the 76 | terminal directly (typically by opening /dev/tty), like 77 | dump(1) or telnet(1) for example.

78 | 79 |

OPTIONS 80 | 81 |

82 | 83 | 84 |

-v, 85 | --verbose

86 | 87 |

Runs verbosely. Namely, 88 | ptyopen will report "benign" errors while 89 | handling terminal capabilities and window sizing. 90 | ptyopen will also report verbosely the exit code of 91 | command.

92 | 93 |

-V, --version

94 | 95 |

Prints ptyopen’s 96 | version.

97 | 98 |

-u, --unsecure

99 | 100 |

If running under a system which 101 | requires ptyopen to be setuid root, the unsecure 102 | option allows ptyopen to run without changing the 103 | permissions and ownership of the pseudo-terminal (and indeed 104 | makes ptyopen usable without making it setuid, at the 105 | expense of security). This option is not available on 106 | systems not requiring ptyopen to be setuid root 107 | (Linux >= 2.2 with glibc >= 2.1, Solaris, ...)

108 | 109 |

-w, --write

110 | 111 |

Allows write permissions to the 112 | pseudo-terminal. This is probably useless, unless 113 | you’re running some interactive shell under 114 | ptyopen.

115 | 116 |

-r, --ringsize 117 | <size>

118 | 119 |

Takes an argument, the number 120 | of bytes of ptyopen’s input and output buffers. 121 | When writing to ptyopen’s stdin, ptyopen 122 | buffers some amount of data. Similarly, when the 123 | program writes to the terminal, ptyopen 124 | buffers the output before sending it to stdout. One might 125 | want to change the default buffer size for obscure 126 | reasons.

127 | 128 |

-g, --geometry 129 | <geometry>

130 | 131 |

Makes program believe it 132 | runs in a terminal whose size is given by 133 | <geometry>. For example 80x25 or 132x50.

134 | 135 |

-h, --help

136 | 137 |

Prints a summary of these 138 | options.

139 | 140 |

NOTES 141 | 142 |

143 | 144 | 145 |

SIGNALS 146 | 147 |

148 | 149 | 150 |

ptyopen 151 | traps almost all common deadly signals so that the 152 | pseudo-terminal permissions are restored correctly.

153 | 154 |

ptyopen 155 | also handles SIGTSTP and SIGCONT so that the slave 156 | command gets stopped and restarted when 157 | ptyopen’s state changes.

158 | 159 |

Finally, window 160 | size changes get communicated correctly through SIGWINCH, 161 | unless the -g or --geometry (forced geometry) 162 | options are used.

163 | 164 |

BUFFERS 165 | 166 |

167 | 168 | 169 |

ptyopen 170 | uses some internal buffers, which can be resized with the 171 | -r or --ringsize options. These buffers are in 172 | used in addition to the buffers used by the pseudo-terminal 173 | driver, and eventually the pipes and sockets connected to 174 | ptyopen’s stdin and stdout.

175 | 176 |

END OF FILE 177 | 178 |

179 | 180 | 181 |

If 182 | ptyopen’s stdin is closed, then an EOF 183 | character (as determined by the pseudo-terminal’s 184 | capabilities) is sent to the terminal. Whether or not the 185 | slave command will take action on this EOF character 186 | depends on the command itself, and not 187 | ptyopen.

188 | 189 |

EXIT CODE 190 | 191 |

192 | 193 | 194 |

ptyopen 195 | exits with the exit code of the slave command unless 196 | it encounters some error. In that case it will exit with 197 | 255.

198 | 199 |

SECURITY 200 | 201 |

202 | 203 | 204 |

On BSD systems, 205 | ptyopen needs to be setuid root so that the 206 | pseudo-terminal’s permissions and ownership can be 207 | changed. ptyopen will refuse to run without being 208 | setuid root (unless the -u or --unsecure 209 | options are used).

210 | 211 |

Although great 212 | care has been taken to ensure that ptyopen does not 213 | present a security risk, your mileage may vary.

214 | 215 |

SEE ALSO 216 | 217 |

218 | 219 | 220 |

expect(1), 221 | pty(7).

222 | 223 |

AUTHOR 224 | 225 |

226 | 227 | 228 |

Written and 229 | Copyright (C) 1998-2024 Philippe Troin 230 | <phil+github-commits@fifi.org>. THIS PROGRAM COMES 231 | WITH ABSOLUTELY NO WARRANTY.

232 | 233 |

HISTORY 234 | 235 |

236 | 237 | 238 |

This program was 239 | written as a way to control dump(8) from a backup 240 | script.

241 |
242 | 243 | 244 | -------------------------------------------------------------------------------- /COPYING: -------------------------------------------------------------------------------- 1 | GNU GENERAL PUBLIC LICENSE 2 | Version 3, 29 June 2007 3 | 4 | Copyright (C) 2007 Free Software Foundation, Inc. 5 | Everyone is permitted to copy and distribute verbatim copies 6 | of this license document, but changing it is not allowed. 7 | 8 | Preamble 9 | 10 | The GNU General Public License is a free, copyleft license for 11 | software and other kinds of works. 12 | 13 | The licenses for most software and other practical works are designed 14 | to take away your freedom to share and change the works. By contrast, 15 | the GNU General Public License is intended to guarantee your freedom to 16 | share and change all versions of a program--to make sure it remains free 17 | software for all its users. We, the Free Software Foundation, use the 18 | GNU General Public License for most of our software; it applies also to 19 | any other work released this way by its authors. You can apply it to 20 | your programs, too. 21 | 22 | When we speak of free software, we are referring to freedom, not 23 | price. Our General Public Licenses are designed to make sure that you 24 | have the freedom to distribute copies of free software (and charge for 25 | them if you wish), that you receive source code or can get it if you 26 | want it, that you can change the software or use pieces of it in new 27 | free programs, and that you know you can do these things. 28 | 29 | To protect your rights, we need to prevent others from denying you 30 | these rights or asking you to surrender the rights. Therefore, you have 31 | certain responsibilities if you distribute copies of the software, or if 32 | you modify it: responsibilities to respect the freedom of others. 33 | 34 | For example, if you distribute copies of such a program, whether 35 | gratis or for a fee, you must pass on to the recipients the same 36 | freedoms that you received. You must make sure that they, too, receive 37 | or can get the source code. And you must show them these terms so they 38 | know their rights. 39 | 40 | Developers that use the GNU GPL protect your rights with two steps: 41 | (1) assert copyright on the software, and (2) offer you this License 42 | giving you legal permission to copy, distribute and/or modify it. 43 | 44 | For the developers' and authors' protection, the GPL clearly explains 45 | that there is no warranty for this free software. For both users' and 46 | authors' sake, the GPL requires that modified versions be marked as 47 | changed, so that their problems will not be attributed erroneously to 48 | authors of previous versions. 49 | 50 | Some devices are designed to deny users access to install or run 51 | modified versions of the software inside them, although the manufacturer 52 | can do so. This is fundamentally incompatible with the aim of 53 | protecting users' freedom to change the software. The systematic 54 | pattern of such abuse occurs in the area of products for individuals to 55 | use, which is precisely where it is most unacceptable. Therefore, we 56 | have designed this version of the GPL to prohibit the practice for those 57 | products. If such problems arise substantially in other domains, we 58 | stand ready to extend this provision to those domains in future versions 59 | of the GPL, as needed to protect the freedom of users. 60 | 61 | Finally, every program is threatened constantly by software patents. 62 | States should not allow patents to restrict development and use of 63 | software on general-purpose computers, but in those that do, we wish to 64 | avoid the special danger that patents applied to a free program could 65 | make it effectively proprietary. To prevent this, the GPL assures that 66 | patents cannot be used to render the program non-free. 67 | 68 | The precise terms and conditions for copying, distribution and 69 | modification follow. 70 | 71 | TERMS AND CONDITIONS 72 | 73 | 0. Definitions. 74 | 75 | "This License" refers to version 3 of the GNU General Public License. 76 | 77 | "Copyright" also means copyright-like laws that apply to other kinds of 78 | works, such as semiconductor masks. 79 | 80 | "The Program" refers to any copyrightable work licensed under this 81 | License. Each licensee is addressed as "you". "Licensees" and 82 | "recipients" may be individuals or organizations. 83 | 84 | To "modify" a work means to copy from or adapt all or part of the work 85 | in a fashion requiring copyright permission, other than the making of an 86 | exact copy. The resulting work is called a "modified version" of the 87 | earlier work or a work "based on" the earlier work. 88 | 89 | A "covered work" means either the unmodified Program or a work based 90 | on the Program. 91 | 92 | To "propagate" a work means to do anything with it that, without 93 | permission, would make you directly or secondarily liable for 94 | infringement under applicable copyright law, except executing it on a 95 | computer or modifying a private copy. Propagation includes copying, 96 | distribution (with or without modification), making available to the 97 | public, and in some countries other activities as well. 98 | 99 | To "convey" a work means any kind of propagation that enables other 100 | parties to make or receive copies. Mere interaction with a user through 101 | a computer network, with no transfer of a copy, is not conveying. 102 | 103 | An interactive user interface displays "Appropriate Legal Notices" 104 | to the extent that it includes a convenient and prominently visible 105 | feature that (1) displays an appropriate copyright notice, and (2) 106 | tells the user that there is no warranty for the work (except to the 107 | extent that warranties are provided), that licensees may convey the 108 | work under this License, and how to view a copy of this License. If 109 | the interface presents a list of user commands or options, such as a 110 | menu, a prominent item in the list meets this criterion. 111 | 112 | 1. Source Code. 113 | 114 | The "source code" for a work means the preferred form of the work 115 | for making modifications to it. "Object code" means any non-source 116 | form of a work. 117 | 118 | A "Standard Interface" means an interface that either is an official 119 | standard defined by a recognized standards body, or, in the case of 120 | interfaces specified for a particular programming language, one that 121 | is widely used among developers working in that language. 122 | 123 | The "System Libraries" of an executable work include anything, other 124 | than the work as a whole, that (a) is included in the normal form of 125 | packaging a Major Component, but which is not part of that Major 126 | Component, and (b) serves only to enable use of the work with that 127 | Major Component, or to implement a Standard Interface for which an 128 | implementation is available to the public in source code form. A 129 | "Major Component", in this context, means a major essential component 130 | (kernel, window system, and so on) of the specific operating system 131 | (if any) on which the executable work runs, or a compiler used to 132 | produce the work, or an object code interpreter used to run it. 133 | 134 | The "Corresponding Source" for a work in object code form means all 135 | the source code needed to generate, install, and (for an executable 136 | work) run the object code and to modify the work, including scripts to 137 | control those activities. However, it does not include the work's 138 | System Libraries, or general-purpose tools or generally available free 139 | programs which are used unmodified in performing those activities but 140 | which are not part of the work. For example, Corresponding Source 141 | includes interface definition files associated with source files for 142 | the work, and the source code for shared libraries and dynamically 143 | linked subprograms that the work is specifically designed to require, 144 | such as by intimate data communication or control flow between those 145 | subprograms and other parts of the work. 146 | 147 | The Corresponding Source need not include anything that users 148 | can regenerate automatically from other parts of the Corresponding 149 | Source. 150 | 151 | The Corresponding Source for a work in source code form is that 152 | same work. 153 | 154 | 2. Basic Permissions. 155 | 156 | All rights granted under this License are granted for the term of 157 | copyright on the Program, and are irrevocable provided the stated 158 | conditions are met. This License explicitly affirms your unlimited 159 | permission to run the unmodified Program. The output from running a 160 | covered work is covered by this License only if the output, given its 161 | content, constitutes a covered work. This License acknowledges your 162 | rights of fair use or other equivalent, as provided by copyright law. 163 | 164 | You may make, run and propagate covered works that you do not 165 | convey, without conditions so long as your license otherwise remains 166 | in force. You may convey covered works to others for the sole purpose 167 | of having them make modifications exclusively for you, or provide you 168 | with facilities for running those works, provided that you comply with 169 | the terms of this License in conveying all material for which you do 170 | not control copyright. Those thus making or running the covered works 171 | for you must do so exclusively on your behalf, under your direction 172 | and control, on terms that prohibit them from making any copies of 173 | your copyrighted material outside their relationship with you. 174 | 175 | Conveying under any other circumstances is permitted solely under 176 | the conditions stated below. Sublicensing is not allowed; section 10 177 | makes it unnecessary. 178 | 179 | 3. Protecting Users' Legal Rights From Anti-Circumvention Law. 180 | 181 | No covered work shall be deemed part of an effective technological 182 | measure under any applicable law fulfilling obligations under article 183 | 11 of the WIPO copyright treaty adopted on 20 December 1996, or 184 | similar laws prohibiting or restricting circumvention of such 185 | measures. 186 | 187 | When you convey a covered work, you waive any legal power to forbid 188 | circumvention of technological measures to the extent such circumvention 189 | is effected by exercising rights under this License with respect to 190 | the covered work, and you disclaim any intention to limit operation or 191 | modification of the work as a means of enforcing, against the work's 192 | users, your or third parties' legal rights to forbid circumvention of 193 | technological measures. 194 | 195 | 4. Conveying Verbatim Copies. 196 | 197 | You may convey verbatim copies of the Program's source code as you 198 | receive it, in any medium, provided that you conspicuously and 199 | appropriately publish on each copy an appropriate copyright notice; 200 | keep intact all notices stating that this License and any 201 | non-permissive terms added in accord with section 7 apply to the code; 202 | keep intact all notices of the absence of any warranty; and give all 203 | recipients a copy of this License along with the Program. 204 | 205 | You may charge any price or no price for each copy that you convey, 206 | and you may offer support or warranty protection for a fee. 207 | 208 | 5. Conveying Modified Source Versions. 209 | 210 | You may convey a work based on the Program, or the modifications to 211 | produce it from the Program, in the form of source code under the 212 | terms of section 4, provided that you also meet all of these conditions: 213 | 214 | a) The work must carry prominent notices stating that you modified 215 | it, and giving a relevant date. 216 | 217 | b) The work must carry prominent notices stating that it is 218 | released under this License and any conditions added under section 219 | 7. This requirement modifies the requirement in section 4 to 220 | "keep intact all notices". 221 | 222 | c) You must license the entire work, as a whole, under this 223 | License to anyone who comes into possession of a copy. This 224 | License will therefore apply, along with any applicable section 7 225 | additional terms, to the whole of the work, and all its parts, 226 | regardless of how they are packaged. This License gives no 227 | permission to license the work in any other way, but it does not 228 | invalidate such permission if you have separately received it. 229 | 230 | d) If the work has interactive user interfaces, each must display 231 | Appropriate Legal Notices; however, if the Program has interactive 232 | interfaces that do not display Appropriate Legal Notices, your 233 | work need not make them do so. 234 | 235 | A compilation of a covered work with other separate and independent 236 | works, which are not by their nature extensions of the covered work, 237 | and which are not combined with it such as to form a larger program, 238 | in or on a volume of a storage or distribution medium, is called an 239 | "aggregate" if the compilation and its resulting copyright are not 240 | used to limit the access or legal rights of the compilation's users 241 | beyond what the individual works permit. Inclusion of a covered work 242 | in an aggregate does not cause this License to apply to the other 243 | parts of the aggregate. 244 | 245 | 6. Conveying Non-Source Forms. 246 | 247 | You may convey a covered work in object code form under the terms 248 | of sections 4 and 5, provided that you also convey the 249 | machine-readable Corresponding Source under the terms of this License, 250 | in one of these ways: 251 | 252 | a) Convey the object code in, or embodied in, a physical product 253 | (including a physical distribution medium), accompanied by the 254 | Corresponding Source fixed on a durable physical medium 255 | customarily used for software interchange. 256 | 257 | b) Convey the object code in, or embodied in, a physical product 258 | (including a physical distribution medium), accompanied by a 259 | written offer, valid for at least three years and valid for as 260 | long as you offer spare parts or customer support for that product 261 | model, to give anyone who possesses the object code either (1) a 262 | copy of the Corresponding Source for all the software in the 263 | product that is covered by this License, on a durable physical 264 | medium customarily used for software interchange, for a price no 265 | more than your reasonable cost of physically performing this 266 | conveying of source, or (2) access to copy the 267 | Corresponding Source from a network server at no charge. 268 | 269 | c) Convey individual copies of the object code with a copy of the 270 | written offer to provide the Corresponding Source. This 271 | alternative is allowed only occasionally and noncommercially, and 272 | only if you received the object code with such an offer, in accord 273 | with subsection 6b. 274 | 275 | d) Convey the object code by offering access from a designated 276 | place (gratis or for a charge), and offer equivalent access to the 277 | Corresponding Source in the same way through the same place at no 278 | further charge. You need not require recipients to copy the 279 | Corresponding Source along with the object code. If the place to 280 | copy the object code is a network server, the Corresponding Source 281 | may be on a different server (operated by you or a third party) 282 | that supports equivalent copying facilities, provided you maintain 283 | clear directions next to the object code saying where to find the 284 | Corresponding Source. Regardless of what server hosts the 285 | Corresponding Source, you remain obligated to ensure that it is 286 | available for as long as needed to satisfy these requirements. 287 | 288 | e) Convey the object code using peer-to-peer transmission, provided 289 | you inform other peers where the object code and Corresponding 290 | Source of the work are being offered to the general public at no 291 | charge under subsection 6d. 292 | 293 | A separable portion of the object code, whose source code is excluded 294 | from the Corresponding Source as a System Library, need not be 295 | included in conveying the object code work. 296 | 297 | A "User Product" is either (1) a "consumer product", which means any 298 | tangible personal property which is normally used for personal, family, 299 | or household purposes, or (2) anything designed or sold for incorporation 300 | into a dwelling. In determining whether a product is a consumer product, 301 | doubtful cases shall be resolved in favor of coverage. For a particular 302 | product received by a particular user, "normally used" refers to a 303 | typical or common use of that class of product, regardless of the status 304 | of the particular user or of the way in which the particular user 305 | actually uses, or expects or is expected to use, the product. A product 306 | is a consumer product regardless of whether the product has substantial 307 | commercial, industrial or non-consumer uses, unless such uses represent 308 | the only significant mode of use of the product. 309 | 310 | "Installation Information" for a User Product means any methods, 311 | procedures, authorization keys, or other information required to install 312 | and execute modified versions of a covered work in that User Product from 313 | a modified version of its Corresponding Source. The information must 314 | suffice to ensure that the continued functioning of the modified object 315 | code is in no case prevented or interfered with solely because 316 | modification has been made. 317 | 318 | If you convey an object code work under this section in, or with, or 319 | specifically for use in, a User Product, and the conveying occurs as 320 | part of a transaction in which the right of possession and use of the 321 | User Product is transferred to the recipient in perpetuity or for a 322 | fixed term (regardless of how the transaction is characterized), the 323 | Corresponding Source conveyed under this section must be accompanied 324 | by the Installation Information. But this requirement does not apply 325 | if neither you nor any third party retains the ability to install 326 | modified object code on the User Product (for example, the work has 327 | been installed in ROM). 328 | 329 | The requirement to provide Installation Information does not include a 330 | requirement to continue to provide support service, warranty, or updates 331 | for a work that has been modified or installed by the recipient, or for 332 | the User Product in which it has been modified or installed. Access to a 333 | network may be denied when the modification itself materially and 334 | adversely affects the operation of the network or violates the rules and 335 | protocols for communication across the network. 336 | 337 | Corresponding Source conveyed, and Installation Information provided, 338 | in accord with this section must be in a format that is publicly 339 | documented (and with an implementation available to the public in 340 | source code form), and must require no special password or key for 341 | unpacking, reading or copying. 342 | 343 | 7. Additional Terms. 344 | 345 | "Additional permissions" are terms that supplement the terms of this 346 | License by making exceptions from one or more of its conditions. 347 | Additional permissions that are applicable to the entire Program shall 348 | be treated as though they were included in this License, to the extent 349 | that they are valid under applicable law. If additional permissions 350 | apply only to part of the Program, that part may be used separately 351 | under those permissions, but the entire Program remains governed by 352 | this License without regard to the additional permissions. 353 | 354 | When you convey a copy of a covered work, you may at your option 355 | remove any additional permissions from that copy, or from any part of 356 | it. (Additional permissions may be written to require their own 357 | removal in certain cases when you modify the work.) You may place 358 | additional permissions on material, added by you to a covered work, 359 | for which you have or can give appropriate copyright permission. 360 | 361 | Notwithstanding any other provision of this License, for material you 362 | add to a covered work, you may (if authorized by the copyright holders of 363 | that material) supplement the terms of this License with terms: 364 | 365 | a) Disclaiming warranty or limiting liability differently from the 366 | terms of sections 15 and 16 of this License; or 367 | 368 | b) Requiring preservation of specified reasonable legal notices or 369 | author attributions in that material or in the Appropriate Legal 370 | Notices displayed by works containing it; or 371 | 372 | c) Prohibiting misrepresentation of the origin of that material, or 373 | requiring that modified versions of such material be marked in 374 | reasonable ways as different from the original version; or 375 | 376 | d) Limiting the use for publicity purposes of names of licensors or 377 | authors of the material; or 378 | 379 | e) Declining to grant rights under trademark law for use of some 380 | trade names, trademarks, or service marks; or 381 | 382 | f) Requiring indemnification of licensors and authors of that 383 | material by anyone who conveys the material (or modified versions of 384 | it) with contractual assumptions of liability to the recipient, for 385 | any liability that these contractual assumptions directly impose on 386 | those licensors and authors. 387 | 388 | All other non-permissive additional terms are considered "further 389 | restrictions" within the meaning of section 10. If the Program as you 390 | received it, or any part of it, contains a notice stating that it is 391 | governed by this License along with a term that is a further 392 | restriction, you may remove that term. If a license document contains 393 | a further restriction but permits relicensing or conveying under this 394 | License, you may add to a covered work material governed by the terms 395 | of that license document, provided that the further restriction does 396 | not survive such relicensing or conveying. 397 | 398 | If you add terms to a covered work in accord with this section, you 399 | must place, in the relevant source files, a statement of the 400 | additional terms that apply to those files, or a notice indicating 401 | where to find the applicable terms. 402 | 403 | Additional terms, permissive or non-permissive, may be stated in the 404 | form of a separately written license, or stated as exceptions; 405 | the above requirements apply either way. 406 | 407 | 8. Termination. 408 | 409 | You may not propagate or modify a covered work except as expressly 410 | provided under this License. Any attempt otherwise to propagate or 411 | modify it is void, and will automatically terminate your rights under 412 | this License (including any patent licenses granted under the third 413 | paragraph of section 11). 414 | 415 | However, if you cease all violation of this License, then your 416 | license from a particular copyright holder is reinstated (a) 417 | provisionally, unless and until the copyright holder explicitly and 418 | finally terminates your license, and (b) permanently, if the copyright 419 | holder fails to notify you of the violation by some reasonable means 420 | prior to 60 days after the cessation. 421 | 422 | Moreover, your license from a particular copyright holder is 423 | reinstated permanently if the copyright holder notifies you of the 424 | violation by some reasonable means, this is the first time you have 425 | received notice of violation of this License (for any work) from that 426 | copyright holder, and you cure the violation prior to 30 days after 427 | your receipt of the notice. 428 | 429 | Termination of your rights under this section does not terminate the 430 | licenses of parties who have received copies or rights from you under 431 | this License. If your rights have been terminated and not permanently 432 | reinstated, you do not qualify to receive new licenses for the same 433 | material under section 10. 434 | 435 | 9. Acceptance Not Required for Having Copies. 436 | 437 | You are not required to accept this License in order to receive or 438 | run a copy of the Program. Ancillary propagation of a covered work 439 | occurring solely as a consequence of using peer-to-peer transmission 440 | to receive a copy likewise does not require acceptance. However, 441 | nothing other than this License grants you permission to propagate or 442 | modify any covered work. These actions infringe copyright if you do 443 | not accept this License. Therefore, by modifying or propagating a 444 | covered work, you indicate your acceptance of this License to do so. 445 | 446 | 10. Automatic Licensing of Downstream Recipients. 447 | 448 | Each time you convey a covered work, the recipient automatically 449 | receives a license from the original licensors, to run, modify and 450 | propagate that work, subject to this License. You are not responsible 451 | for enforcing compliance by third parties with this License. 452 | 453 | An "entity transaction" is a transaction transferring control of an 454 | organization, or substantially all assets of one, or subdividing an 455 | organization, or merging organizations. If propagation of a covered 456 | work results from an entity transaction, each party to that 457 | transaction who receives a copy of the work also receives whatever 458 | licenses to the work the party's predecessor in interest had or could 459 | give under the previous paragraph, plus a right to possession of the 460 | Corresponding Source of the work from the predecessor in interest, if 461 | the predecessor has it or can get it with reasonable efforts. 462 | 463 | You may not impose any further restrictions on the exercise of the 464 | rights granted or affirmed under this License. For example, you may 465 | not impose a license fee, royalty, or other charge for exercise of 466 | rights granted under this License, and you may not initiate litigation 467 | (including a cross-claim or counterclaim in a lawsuit) alleging that 468 | any patent claim is infringed by making, using, selling, offering for 469 | sale, or importing the Program or any portion of it. 470 | 471 | 11. Patents. 472 | 473 | A "contributor" is a copyright holder who authorizes use under this 474 | License of the Program or a work on which the Program is based. The 475 | work thus licensed is called the contributor's "contributor version". 476 | 477 | A contributor's "essential patent claims" are all patent claims 478 | owned or controlled by the contributor, whether already acquired or 479 | hereafter acquired, that would be infringed by some manner, permitted 480 | by this License, of making, using, or selling its contributor version, 481 | but do not include claims that would be infringed only as a 482 | consequence of further modification of the contributor version. For 483 | purposes of this definition, "control" includes the right to grant 484 | patent sublicenses in a manner consistent with the requirements of 485 | this License. 486 | 487 | Each contributor grants you a non-exclusive, worldwide, royalty-free 488 | patent license under the contributor's essential patent claims, to 489 | make, use, sell, offer for sale, import and otherwise run, modify and 490 | propagate the contents of its contributor version. 491 | 492 | In the following three paragraphs, a "patent license" is any express 493 | agreement or commitment, however denominated, not to enforce a patent 494 | (such as an express permission to practice a patent or covenant not to 495 | sue for patent infringement). To "grant" such a patent license to a 496 | party means to make such an agreement or commitment not to enforce a 497 | patent against the party. 498 | 499 | If you convey a covered work, knowingly relying on a patent license, 500 | and the Corresponding Source of the work is not available for anyone 501 | to copy, free of charge and under the terms of this License, through a 502 | publicly available network server or other readily accessible means, 503 | then you must either (1) cause the Corresponding Source to be so 504 | available, or (2) arrange to deprive yourself of the benefit of the 505 | patent license for this particular work, or (3) arrange, in a manner 506 | consistent with the requirements of this License, to extend the patent 507 | license to downstream recipients. "Knowingly relying" means you have 508 | actual knowledge that, but for the patent license, your conveying the 509 | covered work in a country, or your recipient's use of the covered work 510 | in a country, would infringe one or more identifiable patents in that 511 | country that you have reason to believe are valid. 512 | 513 | If, pursuant to or in connection with a single transaction or 514 | arrangement, you convey, or propagate by procuring conveyance of, a 515 | covered work, and grant a patent license to some of the parties 516 | receiving the covered work authorizing them to use, propagate, modify 517 | or convey a specific copy of the covered work, then the patent license 518 | you grant is automatically extended to all recipients of the covered 519 | work and works based on it. 520 | 521 | A patent license is "discriminatory" if it does not include within 522 | the scope of its coverage, prohibits the exercise of, or is 523 | conditioned on the non-exercise of one or more of the rights that are 524 | specifically granted under this License. You may not convey a covered 525 | work if you are a party to an arrangement with a third party that is 526 | in the business of distributing software, under which you make payment 527 | to the third party based on the extent of your activity of conveying 528 | the work, and under which the third party grants, to any of the 529 | parties who would receive the covered work from you, a discriminatory 530 | patent license (a) in connection with copies of the covered work 531 | conveyed by you (or copies made from those copies), or (b) primarily 532 | for and in connection with specific products or compilations that 533 | contain the covered work, unless you entered into that arrangement, 534 | or that patent license was granted, prior to 28 March 2007. 535 | 536 | Nothing in this License shall be construed as excluding or limiting 537 | any implied license or other defenses to infringement that may 538 | otherwise be available to you under applicable patent law. 539 | 540 | 12. No Surrender of Others' Freedom. 541 | 542 | If conditions are imposed on you (whether by court order, agreement or 543 | otherwise) that contradict the conditions of this License, they do not 544 | excuse you from the conditions of this License. If you cannot convey a 545 | covered work so as to satisfy simultaneously your obligations under this 546 | License and any other pertinent obligations, then as a consequence you may 547 | not convey it at all. For example, if you agree to terms that obligate you 548 | to collect a royalty for further conveying from those to whom you convey 549 | the Program, the only way you could satisfy both those terms and this 550 | License would be to refrain entirely from conveying the Program. 551 | 552 | 13. Use with the GNU Affero General Public License. 553 | 554 | Notwithstanding any other provision of this License, you have 555 | permission to link or combine any covered work with a work licensed 556 | under version 3 of the GNU Affero General Public License into a single 557 | combined work, and to convey the resulting work. The terms of this 558 | License will continue to apply to the part which is the covered work, 559 | but the special requirements of the GNU Affero General Public License, 560 | section 13, concerning interaction through a network will apply to the 561 | combination as such. 562 | 563 | 14. Revised Versions of this License. 564 | 565 | The Free Software Foundation may publish revised and/or new versions of 566 | the GNU General Public License from time to time. Such new versions will 567 | be similar in spirit to the present version, but may differ in detail to 568 | address new problems or concerns. 569 | 570 | Each version is given a distinguishing version number. If the 571 | Program specifies that a certain numbered version of the GNU General 572 | Public License "or any later version" applies to it, you have the 573 | option of following the terms and conditions either of that numbered 574 | version or of any later version published by the Free Software 575 | Foundation. If the Program does not specify a version number of the 576 | GNU General Public License, you may choose any version ever published 577 | by the Free Software Foundation. 578 | 579 | If the Program specifies that a proxy can decide which future 580 | versions of the GNU General Public License can be used, that proxy's 581 | public statement of acceptance of a version permanently authorizes you 582 | to choose that version for the Program. 583 | 584 | Later license versions may give you additional or different 585 | permissions. However, no additional obligations are imposed on any 586 | author or copyright holder as a result of your choosing to follow a 587 | later version. 588 | 589 | 15. Disclaimer of Warranty. 590 | 591 | THERE IS NO WARRANTY FOR THE PROGRAM, TO THE EXTENT PERMITTED BY 592 | APPLICABLE LAW. EXCEPT WHEN OTHERWISE STATED IN WRITING THE COPYRIGHT 593 | HOLDERS AND/OR OTHER PARTIES PROVIDE THE PROGRAM "AS IS" WITHOUT WARRANTY 594 | OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, 595 | THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 596 | PURPOSE. THE ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE PROGRAM 597 | IS WITH YOU. SHOULD THE PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF 598 | ALL NECESSARY SERVICING, REPAIR OR CORRECTION. 599 | 600 | 16. Limitation of Liability. 601 | 602 | IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING 603 | WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MODIFIES AND/OR CONVEYS 604 | THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES, INCLUDING ANY 605 | GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE 606 | USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED TO LOSS OF 607 | DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY YOU OR THIRD 608 | PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER PROGRAMS), 609 | EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF 610 | SUCH DAMAGES. 611 | 612 | 17. Interpretation of Sections 15 and 16. 613 | 614 | If the disclaimer of warranty and limitation of liability provided 615 | above cannot be given local legal effect according to their terms, 616 | reviewing courts shall apply local law that most closely approximates 617 | an absolute waiver of all civil liability in connection with the 618 | Program, unless a warranty or assumption of liability accompanies a 619 | copy of the Program in return for a fee. 620 | 621 | END OF TERMS AND CONDITIONS 622 | 623 | How to Apply These Terms to Your New Programs 624 | 625 | If you develop a new program, and you want it to be of the greatest 626 | possible use to the public, the best way to achieve this is to make it 627 | free software which everyone can redistribute and change under these terms. 628 | 629 | To do so, attach the following notices to the program. It is safest 630 | to attach them to the start of each source file to most effectively 631 | state the exclusion of warranty; and each file should have at least 632 | the "copyright" line and a pointer to where the full notice is found. 633 | 634 | 635 | Copyright (C) 636 | 637 | This program is free software: you can redistribute it and/or modify 638 | it under the terms of the GNU General Public License as published by 639 | the Free Software Foundation, either version 3 of the License, or 640 | (at your option) any later version. 641 | 642 | This program is distributed in the hope that it will be useful, 643 | but WITHOUT ANY WARRANTY; without even the implied warranty of 644 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 645 | GNU General Public License for more details. 646 | 647 | You should have received a copy of the GNU General Public License 648 | along with this program. If not, see . 649 | 650 | Also add information on how to contact you by electronic and paper mail. 651 | 652 | If the program does terminal interaction, make it output a short 653 | notice like this when it starts in an interactive mode: 654 | 655 | Copyright (C) 656 | This program comes with ABSOLUTELY NO WARRANTY; for details type `show w'. 657 | This is free software, and you are welcome to redistribute it 658 | under certain conditions; type `show c' for details. 659 | 660 | The hypothetical commands `show w' and `show c' should show the appropriate 661 | parts of the General Public License. Of course, your program's commands 662 | might be different; for a GUI interface, you would use an "about box". 663 | 664 | You should also get your employer (if you work as a programmer) or school, 665 | if any, to sign a "copyright disclaimer" for the program, if necessary. 666 | For more information on this, and how to apply and follow the GNU GPL, see 667 | . 668 | 669 | The GNU General Public License does not permit incorporating your program 670 | into proprietary programs. If your program is a subroutine library, you 671 | may consider it more useful to permit linking proprietary applications with 672 | the library. If this is what you want to do, use the GNU Lesser General 673 | Public License instead of this License. But first, please read 674 | . 675 | -------------------------------------------------------------------------------- /ptyopen.c: -------------------------------------------------------------------------------- 1 | /* 2 | ptyopen - runs a program on a fake terminal 3 | ptyopen.c - main program 4 | Copyright (C) 1999-2001, 2024 Philippe Troin 5 | 6 | This program is free software; you can redistribute it and/or modify 7 | it under the terms of the GNU General Public License as published by 8 | the Free Software Foundation; either version 3 of the License, or 9 | (at your option) any later version. 10 | 11 | This program is distributed in the hope that it will be useful, 12 | but WITHOUT ANY WARRANTY; without even the implied warranty of 13 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 | GNU General Public License for more details. 15 | 16 | You should have received a copy of the GNU General Public License 17 | along with this program; if not, write to the Free Software 18 | Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 19 | */ 20 | 21 | #define _GNU_SOURCE 22 | 23 | #ifdef HAVE_CONFIG_H 24 | # include "config.h" 25 | #endif 26 | 27 | #include 28 | #include 29 | #include 30 | #include 31 | #include 32 | #include 33 | #include 34 | #include 35 | #include 36 | #ifdef HAVE_STROPTS_H 37 | # include 38 | #endif 39 | #include 40 | #include 41 | #include 42 | #include 43 | #include 44 | 45 | #define UNUSED(x) ((void)(x)) 46 | 47 | #ifndef NDEBUG 48 | #include 49 | #define RING_CHECK(ring) ring_check(ring) 50 | #else 51 | #define RING_CHECK(ring) do ; while(0) 52 | #endif 53 | 54 | /* Structures */ 55 | typedef struct { 56 | uid_t uid; 57 | gid_t gid; 58 | mode_t mode; 59 | } savedperms_t; 60 | 61 | struct ring_st; 62 | typedef struct ring_st ring_t; 63 | 64 | /* Prototypes */ 65 | int main (int, char*[]); 66 | #ifdef HAVE_GRANTPT 67 | # define getprivs() do {} while(0) 68 | # define dropprivs() do {} while(0) 69 | # define ispriv() 1 70 | #else /* ! HAVE_GRANTPT */ 71 | void getprivs (void); 72 | void dropprivs (void); 73 | int ispriv (void); 74 | #endif /* HAVE_GRANTPT */ 75 | const char* getpttypair (int[2]); 76 | void* xmalloc (size_t); 77 | char* xstrdup (const char*); 78 | #ifdef HAVE_GRANTPT 79 | # define saveperms(a,b) 0 80 | # define restoreperms(a,b) 0 81 | #else /* ! HAVE_GRANTPT */ 82 | int saveperms (int,savedperms_t*); 83 | int restoreperms (const char*, savedperms_t*); 84 | #endif /* HAVE_GRANTPT */ 85 | int setperms (int, mode_t); 86 | void loop_on (int, size_t, const char); 87 | #ifndef NDEBUG 88 | void ring_check (ring_t*); 89 | #endif /* NDEBUG */ 90 | ring_t* ring_construct (size_t); 91 | void ring_delete (ring_t*); 92 | size_t ring_space (ring_t*); 93 | int ring_read (ring_t*, int); 94 | int ring_write (ring_t*, int); 95 | int ring_push_char (ring_t*, const char); 96 | int term_raw (int); 97 | int term_restore (int); 98 | int term_winsize (int); 99 | void cleanup (void); 100 | void sig_chld_h (int); 101 | void sig_fatal_h (int); 102 | void sig_tstp_h (int); 103 | void sig_cont_h (int); 104 | void sig_winch_h (int); 105 | #undef _P_ 106 | 107 | /* Configurable options */ 108 | #ifndef HAVE_GRANTPT 109 | const char *tty_prefix = "/dev/tty"; 110 | const char *pty_prefix = "/dev/pty"; 111 | const char *ptty_1st_char = "pqrstuvwxyz"; 112 | const char *ptty_2nd_char = "0123456789abcdef"; 113 | const char *tty_group = "tty"; 114 | #endif /* HAVE_GRANTPT */ 115 | #ifndef HAVE_GETPT 116 | const char *devptmx = "/dev/ptmx"; 117 | #endif /* HAVE_PGETPT */ 118 | #define DEFAULT_RING_SIZE 10240 119 | #define MAX_RING_SIZE (1<<30) 120 | 121 | /* Global variables */ 122 | char* progname = ""; 123 | int opt_verbose; 124 | unsigned long opt_geometry_width; 125 | unsigned long opt_geometry_height; 126 | #ifndef HAVE_GRANTPT 127 | uid_t unsecure_uid; 128 | gid_t unsecure_gid; 129 | uid_t secure_uid; 130 | gid_t secure_gid; 131 | #endif 132 | int child_exit_pipe = -1; /* Some invalid fd value */ 133 | const char* state_tty_name; 134 | savedperms_t state_tty_perms; 135 | int state_saved_stdin_flags = -1; 136 | int state_saved_stdout_flags = -1; 137 | struct termios* state_orig_termios; 138 | pid_t state_child_pid; 139 | int state_pty_fd; 140 | 141 | /* Main function */ 142 | int 143 | main(int argc, char *argv[]) 144 | { 145 | char *cptr; 146 | int pty_pair[2]; 147 | int opt_unsecure; 148 | int opt_write; 149 | size_t opt_ring_size; 150 | int child_status; 151 | int parent_exit=0; 152 | struct sigaction sa; 153 | struct termios tios; 154 | sigset_t sset; 155 | /**/ 156 | 157 | #ifndef HAVE_GRANTPT 158 | /* Remember our initial ids */ 159 | unsecure_uid = geteuid(); 160 | unsecure_gid = getegid(); 161 | secure_uid = getuid(); 162 | secure_gid = getgid(); 163 | #endif 164 | 165 | /* Drop privileges at startup */ 166 | dropprivs(); 167 | 168 | /* Get our name */ 169 | cptr=progname=argv[0]; 170 | while (*cptr) 171 | { 172 | if (*cptr=='/') 173 | { 174 | progname = ++cptr; 175 | } 176 | else 177 | { 178 | ++cptr; 179 | } 180 | } 181 | 182 | /* Set default options */ 183 | opt_verbose=0; 184 | opt_unsecure=0; 185 | opt_write=0; 186 | opt_ring_size = DEFAULT_RING_SIZE; 187 | opt_geometry_width = 0; 188 | opt_geometry_height = 0; 189 | 190 | /* Check options */ 191 | ++argv; --argc; 192 | while (argc && argv[0][0]=='-') 193 | { 194 | if (strcmp("-v", argv[0])==0 || strcmp("--verbose", argv[0])==0) 195 | { 196 | opt_verbose=1; 197 | } 198 | else if (strcmp("-u", argv[0])==0 || strcmp("--unsecure", argv[0])==0) 199 | #ifdef HAVE_GRANTPT 200 | ; /* Eat it and don't complain */ 201 | #else /* ! HAVE_GRANTPT */ 202 | { 203 | opt_unsecure=1; 204 | } 205 | #endif 206 | else if (strcmp("-w", argv[0])==0 || strcmp("--write", argv[0])==0) 207 | { 208 | opt_write=1; 209 | } 210 | else if (strcmp("-r", argv[0])==0 211 | || strcmp ("--ringsize", argv[0])==0) 212 | { 213 | char *end; 214 | /**/ 215 | 216 | if (argc<2) 217 | { 218 | fprintf(stderr, "%s: option %s requires an argument\n", 219 | progname, argv[0]); 220 | exit(255); 221 | } 222 | opt_ring_size=strtoul(argv[1], &end, 10); 223 | if (*end) 224 | { 225 | fprintf(stderr, "%s: %s is not a number\n", 226 | progname, argv[1]); 227 | exit(255); 228 | } 229 | if (opt_ring_size==0 || opt_ring_size>MAX_RING_SIZE) 230 | { 231 | fprintf(stderr, "%s: ring size out of range\n", progname); 232 | exit(255); 233 | } 234 | ++argv; --argc; 235 | } 236 | else if (strncmp("--ringsize=", argv[0], 11)==0) 237 | { 238 | char *start; 239 | char *end; 240 | /**/ 241 | 242 | start = argv[0]+11; 243 | if ( !*start) 244 | { 245 | fprintf(stderr, "%s: option %s requires an argument\n", 246 | progname, argv[0]); 247 | exit(255); 248 | } 249 | opt_ring_size=strtoul(start, &end, 10); 250 | if (*end) 251 | { 252 | fprintf(stderr, "%s: %s is not a number\n", 253 | progname, start); 254 | exit(255); 255 | } 256 | if (opt_ring_size==0 || opt_ring_size>MAX_RING_SIZE) 257 | { 258 | fprintf(stderr, "%s: ring size out of range\n", progname); 259 | exit(255); 260 | } 261 | } 262 | else if (strcmp("-g", argv[0])==0 || strcmp("--geometry", argv[0])==0) 263 | { 264 | char *start; 265 | char *end; 266 | /**/ 267 | 268 | if (argc<2) 269 | { 270 | fprintf(stderr, "%s: option %s requires an argument\n", 271 | progname, argv[0]); 272 | exit(255); 273 | } 274 | opt_geometry_width = strtoul(argv[1], &end, 10); 275 | if (*end != 'x') 276 | { 277 | bad_geom: 278 | fprintf(stderr, "%s: bad geometry specification \"%s\"\n", 279 | progname, argv[1]); 280 | exit(255); 281 | } 282 | start = end+1; 283 | opt_geometry_height = strtoul(start, &end, 10); 284 | if (*end) goto bad_geom; 285 | 286 | if (opt_geometry_width == 0 || opt_geometry_width >= 65536 287 | || opt_geometry_height == 0 || opt_geometry_height >= 65536) 288 | goto bad_geom; 289 | --argc, ++argv; 290 | } 291 | else if (strcmp("-h", argv[0])==0 || strcmp("--help", argv[0])==0) 292 | { 293 | printf("usage: %s ...\n" 294 | " where options can be any of:\n" 295 | " -v, --verbose : prints warnings, problems, etc...\n" 296 | " -V, --version : reports version of %s\n" 297 | #ifndef HAVE_GRANTPT 298 | " -u, --unsecure : runs unsecurely\n" 299 | #endif 300 | " -w, --write: : allows tty to be write(1)n to\n" 301 | " -r, --ringsize : use a ring of bytes\n" 302 | " -g, --geometry : pretends the terminal size is " 303 | " (eg. 123x50)\n" 304 | " -h, --help : prints this help\n", 305 | progname, 306 | progname); 307 | exit(0); 308 | } 309 | else if (strcmp("-V", argv[0])==0 || strcmp("--version", argv[0])==0) 310 | { 311 | printf("%s " VERSION 312 | #ifdef GIT_REVISION 313 | " (git: " GIT_REVISION ")" 314 | #endif 315 | "\n" 316 | "Copyright (C) 2000-2024 Philippe Troin \n" 317 | "This is free software; see the source for copying conditions. There is NO\n" 318 | "warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.\n", 319 | progname); 320 | exit(0); 321 | } 322 | else 323 | { 324 | fprintf(stderr, "%s: bad option \"%s\", try \"%s --help\"\n", 325 | progname, argv[0], progname); 326 | exit(255); 327 | } 328 | ++argv; --argc; 329 | } 330 | 331 | /* Check number of arguments */ 332 | if (argc==0) 333 | { 334 | fprintf(stderr, "%s: not enough arguments, try \"%s --help\"\n", 335 | progname, progname); 336 | exit(255); 337 | } 338 | 339 | /* Set up traps for all deadly signals to clean up */ 340 | sa.sa_handler = sig_fatal_h; 341 | sigfillset(&sa.sa_mask); 342 | sa.sa_flags = SA_RESTART; 343 | if (sigaction(SIGHUP, &sa, NULL)==-1) 344 | { 345 | fprintf(stderr, "%s: sigaction(SIGHUP): %s\n", 346 | progname, strerror(errno)); 347 | exit(255); 348 | } 349 | if (sigaction(SIGINT, &sa, NULL)==-1) 350 | { 351 | fprintf(stderr, "%s: sigaction(SIGINT): %s\n", 352 | progname, strerror(errno)); 353 | exit(255); 354 | } 355 | if (sigaction(SIGQUIT, &sa, NULL)==-1) 356 | { 357 | fprintf(stderr, "%s: sigaction(SIGQUIT): %s\n", 358 | progname, strerror(errno)); 359 | exit(255); 360 | } 361 | if (sigaction(SIGABRT, &sa, NULL)==-1) 362 | { 363 | fprintf(stderr, "%s: sigaction(SIGABRT): %s\n", 364 | progname, strerror(errno)); 365 | exit(255); 366 | } 367 | if (sigaction(SIGILL, &sa, NULL)==-1) 368 | { 369 | fprintf(stderr, "%s: sigaction(SIGILL): %s\n", 370 | progname, strerror(errno)); 371 | exit(255); 372 | } 373 | if (sigaction(SIGSEGV, &sa, NULL)==-1) 374 | { 375 | fprintf(stderr, "%s: sigaction(SIGSEGV): %s\n", 376 | progname, strerror(errno)); 377 | exit(255); 378 | } 379 | if (sigaction(SIGPIPE, &sa, NULL)==-1) 380 | { 381 | fprintf(stderr, "%s: sigaction(SIGPIPE): %s\n", 382 | progname, strerror(errno)); 383 | exit(255); 384 | } 385 | if (sigaction(SIGTERM, &sa, NULL)==-1) 386 | { 387 | fprintf(stderr, "%s: sigaction(SIGTERM): %s\n", 388 | progname, strerror(errno)); 389 | exit(255); 390 | } 391 | if (sigaction(SIGTRAP, &sa, NULL)==-1) 392 | { 393 | fprintf(stderr, "%s: sigaction(SIGTRAP): %s\n", 394 | progname, strerror(errno)); 395 | exit(255); 396 | } 397 | if (sigaction(SIGBUS, &sa, NULL)==-1) 398 | { 399 | fprintf(stderr, "%s: sigaction(SIGBUS): %s\n", 400 | progname, strerror(errno)); 401 | exit(255); 402 | } 403 | 404 | /* Special handling for SIGSTOP and SIGCONT */ 405 | sa.sa_handler = sig_tstp_h; 406 | sigemptyset(&sa.sa_mask); 407 | sa.sa_flags = SA_RESTART|SA_NODEFER; 408 | if (sigaction(SIGTSTP, &sa, NULL)==-1) 409 | { 410 | fprintf(stderr, "%s: sigaction(SIGTSTP): %s\n", 411 | progname, strerror(errno)); 412 | exit(255); 413 | } 414 | sa.sa_handler = sig_cont_h; 415 | sigemptyset(&sa.sa_mask); 416 | sa.sa_flags = SA_RESTART; 417 | if (sigaction(SIGCONT, &sa, NULL)==-1) 418 | { 419 | fprintf(stderr, "%s: sigaction(SIGCONT): %s\n", 420 | progname, strerror(errno)); 421 | exit(255); 422 | } 423 | 424 | /* Handle window size changes */ 425 | sa.sa_handler = sig_winch_h; 426 | sigemptyset(&sa.sa_mask); 427 | sa.sa_flags = SA_RESTART; 428 | if (sigaction(SIGWINCH, &sa, NULL)==-1) 429 | { 430 | fprintf(stderr, "%s: sigaction(SIGWINCH): %s\n", 431 | progname, strerror(errno)); 432 | exit(255); 433 | } 434 | 435 | /* Don't forget to cleanup at exit too ! */ 436 | atexit(cleanup); 437 | 438 | /* Ok, we're rolling: get the ptys */ 439 | state_tty_name = getpttypair(pty_pair); 440 | state_pty_fd = pty_pair[0]; 441 | 442 | /* Check/change perms */ 443 | if (!opt_unsecure) 444 | { 445 | if (saveperms(pty_pair[1],&state_tty_perms)==-1) 446 | { 447 | fprintf(stderr, "%s: saving permissions on tty: %s\n", 448 | progname, strerror(errno)); 449 | exit(255); 450 | } 451 | if (setperms(pty_pair[1], 452 | S_IRUSR | S_IWUSR | (opt_write ? S_IWGRP : 0))==-1) { 453 | fprintf(stderr, "%s: setting permissions on tty: %s\n", 454 | progname, strerror(errno)); 455 | exit(255); 456 | } 457 | } 458 | 459 | /* Get the termios EOF char on the slave */ 460 | if (tcgetattr(pty_pair[1], &tios)==-1) 461 | { 462 | fprintf(stderr, "%s: tcgetattr on pty slave: %s\n", 463 | progname, strerror(errno)); 464 | exit(255); 465 | } 466 | 467 | /* Are we on a tty ? If yes, then go into raw mode on this term */ 468 | if (isatty(0)) 469 | term_raw(1); 470 | /* If we're on a tty OR we forced a geometry, set the window size */ 471 | if (isatty(0) || opt_geometry_height > 0) 472 | term_winsize(1); 473 | 474 | /* Prepare to fork: add signal handler and block sigshld */ 475 | sigemptyset(&sset); 476 | sigaddset(&sset, SIGCHLD); 477 | if (sigprocmask(SIG_BLOCK, &sset, NULL)<0) 478 | { 479 | fprintf(stderr, "%s: sigprocmask(SIG_BLOCK, SIGCHLD): %s\n", 480 | progname, strerror(errno)); 481 | exit(255); 482 | } 483 | sa.sa_handler = sig_chld_h; 484 | sigemptyset(&sa.sa_mask); 485 | sa.sa_flags = SA_NOCLDSTOP|SA_RESTART; 486 | if (sigaction(SIGCHLD, &sa, NULL)==-1) 487 | { 488 | fprintf(stderr, "%s: sigaction(SIGCHLD): %s\n", 489 | progname, strerror(errno)); 490 | exit(255); 491 | } 492 | 493 | /* Fork it ! */ 494 | fflush(stdin); 495 | fflush(stdout); 496 | fflush(stderr); 497 | switch (state_child_pid = fork()) 498 | { 499 | case -1: 500 | /* Error */ 501 | fprintf(stderr, "%s: fork(): %s", progname, strerror(errno)); 502 | exit(255); 503 | case 0: 504 | /* Child */ 505 | { 506 | int i; 507 | int fdflags; 508 | /**/ 509 | 510 | /* Close the pty master */ 511 | close(pty_pair[0]); 512 | 513 | /* Create a new session */ 514 | if (setsid()==-1) 515 | { 516 | fprintf(stderr, "%s: cannot create a new session: %s\n", 517 | progname, strerror(errno)); 518 | exit(255); 519 | } 520 | 521 | /* Dup the pty slave to fds 0,1 and 2 */ 522 | for (i=0; i<=2; i++) 523 | { 524 | if (dup2(pty_pair[1], i)==-1) 525 | { 526 | fprintf(i==2 ? stdout : stderr, 527 | "%s[child]: cannot dup2 tty to fd %d: %s\n", 528 | progname, i, strerror(errno)); 529 | exit(255); 530 | } 531 | } 532 | 533 | /* Close the tty original fd */ 534 | close(pty_pair[1]); 535 | 536 | /* Clear CLOEXEC on these fds */ 537 | for (i=0; i<=2; i++) 538 | { 539 | if ((fdflags=fcntl(i, F_GETFD))==-1) 540 | { 541 | fprintf(stderr, "%s[child]: cannot F_GETFL on fd %d: %s\n", 542 | progname, i, strerror(errno)); 543 | exit(255); 544 | } 545 | fdflags &= ~FD_CLOEXEC; 546 | if (fcntl(i, F_SETFD, fdflags)==-1) { 547 | fprintf(stderr, "%s[child]: cannot F_SETFL on fd %d: %s\n", 548 | progname, i, strerror(errno)); 549 | exit(255); 550 | } 551 | } 552 | 553 | /* Set the controlling terminal */ 554 | #ifdef TIOCSCTTY 555 | /* BSD method, with ioctl */ 556 | if (ioctl(0, TIOCSCTTY)==-1) 557 | { 558 | fprintf(stderr, "%s[child]: TIOCSCTTY: %s\n", 559 | progname, strerror(errno)); 560 | exit(255); 561 | } 562 | #else 563 | /* SysV method, do open... */ 564 | if (close(0)<0) 565 | { 566 | fprintf(stderr, "%s[child]: cannot close stdin: %s\n", 567 | progname, strerror(errno)); 568 | exit(255); 569 | } 570 | { 571 | int fd; 572 | fd = open(state_tty_name, O_RDONLY); 573 | if (fd < 0) 574 | { 575 | fprintf(stderr, "%s[child]: cannot open %s read-only: %s\n", 576 | progname, state_tty_name, strerror(errno)); 577 | exit(255); 578 | } 579 | if (fd != 0) 580 | { 581 | fprintf(stderr, "%s[child]: open returned unexpected fd %d\n", 582 | progname, fd); 583 | exit(255); 584 | } 585 | } 586 | #endif /* def TIOCSCTTY */ 587 | 588 | /* Ready to exec */ 589 | if (execvp(argv[0], argv)==-1) 590 | { 591 | fprintf(stderr, "%s[child]: cannot exec %s: %s\n", 592 | progname, argv[0], strerror(errno)); 593 | exit(255); 594 | } 595 | /* Never reached */ 596 | } 597 | default: 598 | /* Parent */ 599 | break; 600 | } 601 | 602 | /* Close the slave */ 603 | close(pty_pair[1]); 604 | 605 | /* Run the loop on the master */ 606 | loop_on(pty_pair[0], opt_ring_size, tios.c_cc[VEOF]); 607 | 608 | /* Close the master now */ 609 | close(pty_pair[0]); 610 | state_pty_fd = 0; 611 | 612 | /* Wait for the child to complete */ 613 | if (waitpid(state_child_pid, &child_status, 0)==-1) 614 | { 615 | fprintf(stderr, "%s: waitpid() for child: %s\n", 616 | progname, strerror(errno)); 617 | exit(255); 618 | } 619 | if (WIFEXITED(child_status)) 620 | { 621 | parent_exit = WEXITSTATUS(child_status); 622 | if (opt_verbose) 623 | { 624 | fprintf(stderr, "%s: child exited with code %d\n", 625 | progname, parent_exit); 626 | } 627 | } 628 | else if (WIFSIGNALED(child_status)) 629 | { 630 | parent_exit = 254; 631 | if (opt_verbose) 632 | { 633 | fprintf(stderr, "%s: child got signal %d%s%s%s\n", 634 | progname, WTERMSIG(child_status), 635 | #ifdef HAVE_STRSIGNAL 636 | ": ", strsignal(WTERMSIG(child_status)), 637 | #else /* ! HAVE_STRSIGNAL */ 638 | "", "", 639 | #endif /* HAVE_STRSIGNAL */ 640 | WCOREDUMP(child_status) ? " (core dumped)" : ""); 641 | } 642 | } 643 | else 644 | { 645 | fprintf(stderr, "%s: exotic exit status from child %08X\n", 646 | progname, child_status); 647 | exit(255); 648 | } 649 | 650 | /* Restore perms */ 651 | if (!opt_unsecure) 652 | { 653 | if (restoreperms(state_tty_name, &state_tty_perms)==-1) 654 | { 655 | fprintf(stderr, "%s: restoring permissions on tty: %s\n", 656 | progname, strerror(errno)); 657 | exit(255); 658 | } 659 | } 660 | free((void*)state_tty_name); 661 | state_tty_name=0; 662 | 663 | /* Restore terminal */ 664 | term_restore(1); 665 | 666 | return parent_exit; 667 | } 668 | 669 | #ifdef HAVE_GRANTPT 670 | const char* 671 | getpttypair(int fds[2]) 672 | { 673 | int ptmx; 674 | int slave; 675 | const char* name; 676 | /**/ 677 | 678 | #ifdef HAVE_GETPT 679 | if ((ptmx = getpt())<0) 680 | { 681 | fprintf(stderr, "%s: getpt(): %s\n", progname, strerror(errno)); 682 | exit(255); 683 | } 684 | #else /* ! HAVE_GETPT */ 685 | if ((ptmx = open(devptmx, O_RDWR))<0) 686 | { 687 | fprintf(stderr, "%s: open(\"%s\"): %s\n", progname, 688 | devptmx, strerror(errno)); 689 | exit(255); 690 | } 691 | #endif /* HAVE_GETPT */ 692 | 693 | if (grantpt(ptmx)<0) 694 | { 695 | fprintf(stderr, "%s: grantpt(): %s\n", progname, strerror(errno)); 696 | exit(255); 697 | } 698 | 699 | #ifdef HAVE_UNLOCKPT 700 | if (unlockpt(ptmx)<0) 701 | { 702 | fprintf(stderr, "%s: unlockpt(): %s\n", progname, strerror(errno)); 703 | exit(255); 704 | } 705 | #endif 706 | 707 | if (!(name=ptsname(ptmx))) 708 | { 709 | fprintf(stderr, "%s: ptsname(): %s\n", progname, strerror(errno)); 710 | exit(255); 711 | } 712 | 713 | if ((slave = open(name, O_RDWR))<0) 714 | { 715 | fprintf(stderr, "%s: open(\"%s\"): %s\n", progname, name, 716 | strerror(errno)); 717 | exit(255); 718 | } 719 | 720 | #if defined(HAVE_ISASTREAM) && defined(I_PUSH) 721 | if (isastream(slave)) 722 | { 723 | if (ioctl(slave, I_PUSH, "ptem")<0) 724 | { 725 | fprintf(stderr, "%s: I_PUSH ptem: %s\n", progname, strerror(errno)); 726 | exit(255); 727 | } 728 | if (ioctl(slave, I_PUSH, "ldterm")<0) 729 | { 730 | fprintf(stderr, "%s: I_PUSH ldterm: %s\n", progname, 731 | strerror(errno)); 732 | exit(255); 733 | } 734 | } 735 | #endif /* HAVE_ISASTREAM && I_PUSH */ 736 | 737 | fds[0] = ptmx; 738 | fds[1] = slave; 739 | 740 | return xstrdup(name); 741 | } 742 | #else /* ! HAVE_GRANTPT */ 743 | /* Get a pty/tty pair, return open file descriptors in the arguments 744 | (a la pipe) 745 | */ 746 | const char* 747 | getpttypair(int fds[2]) 748 | { 749 | char *pty_string; 750 | char *tty_string; 751 | size_t pty_size; 752 | size_t tty_size; 753 | const char* last_1st_char; 754 | const char* last_2nd_char; 755 | const char* p_1st_char; 756 | const char* p_2nd_char; 757 | /**/ 758 | 759 | /* Allocate and copy prefixes */ 760 | pty_size = strlen(pty_prefix); 761 | tty_size = strlen(tty_prefix); 762 | pty_string = xmalloc(pty_size+3); 763 | tty_string = xmalloc(tty_size+3); 764 | strcpy(pty_string, pty_prefix); 765 | strcpy(tty_string, tty_prefix); 766 | pty_string[pty_size+2]=0; 767 | tty_string[tty_size+2]=0; 768 | 769 | /* How many chars in the arrays ? */ 770 | last_1st_char = ptty_1st_char+strlen(ptty_1st_char); 771 | last_2nd_char = ptty_2nd_char+strlen(ptty_2nd_char); 772 | 773 | /* Loop, and we need privileges to do the open() */ 774 | for ( p_1st_char=ptty_1st_char; p_1st_char < last_1st_char; ++p_1st_char) 775 | { 776 | for ( p_2nd_char=ptty_2nd_char; p_2nd_char < last_2nd_char; ++p_2nd_char) 777 | { 778 | pty_string[pty_size] = *p_1st_char; 779 | pty_string[pty_size+1] = *p_2nd_char; 780 | getprivs(); 781 | fds[0]=open(pty_string, O_RDWR|O_NOCTTY); 782 | dropprivs(); 783 | if (fds[0]==-1) 784 | { 785 | switch(errno) 786 | { 787 | case ENOENT: 788 | /* File not found */ 789 | goto no_more_ptys; 790 | case EIO: 791 | /* Pty busy */ 792 | break; 793 | case EACCES: 794 | /* File unreadable */ 795 | if (!ispriv()) 796 | { 797 | break; 798 | } 799 | /* If we're privileged *AND* we get EACCESS, fall 800 | back on default: */ 801 | default: 802 | fprintf(stderr, "%s: open %s: %s\n", 803 | progname, pty_string, strerror(errno)); 804 | exit(255); 805 | } 806 | } 807 | else 808 | { 809 | /* Found the pty, try open the tty */ 810 | tty_string[tty_size] = *p_1st_char; 811 | tty_string[tty_size+1] = *p_2nd_char; 812 | getprivs(); 813 | fds[1]=open(tty_string, O_RDWR|O_NOCTTY); 814 | dropprivs(); 815 | if (fds[1]==-1) 816 | { 817 | switch(errno) { 818 | case ENOENT: 819 | /* File not found */ 820 | if (opt_verbose) 821 | { 822 | fprintf(stderr, "%s: found %s without matching %s\n", 823 | progname, pty_string, tty_string); 824 | } 825 | break; 826 | case EACCES: 827 | if (!ispriv()) 828 | { 829 | break; 830 | } 831 | /* If we're privileged *AND* we get EACCESS, fall 832 | back on default: */ 833 | default: 834 | fprintf(stderr, "%s: open %s: %s\n", 835 | progname, tty_string, strerror(errno)); 836 | exit(255); 837 | } 838 | /* Close the pty then, and start over */ 839 | if (close(fds[0])==-1) 840 | { 841 | fprintf(stderr, "%s: close %s: %s\n", 842 | progname, tty_string, strerror(errno)); 843 | exit(255); 844 | } 845 | } 846 | else 847 | { 848 | /* Okie */ 849 | free(pty_string); 850 | return tty_string; 851 | } 852 | } 853 | } 854 | } 855 | no_more_ptys: 856 | /* Ran out of ptys ! */ 857 | fprintf(stderr, "%s: no more ptys available\n", progname); 858 | exit(255); 859 | } 860 | #endif /* HAVE_GRANTPT */ 861 | 862 | #ifndef HAVE_GRANTPT 863 | /* Save permissions on a fd for future restoration eventually */ 864 | int 865 | saveperms(int fd,savedperms_t* perms) 866 | { 867 | struct stat statbuf; 868 | 869 | if (fstat(fd, &statbuf)==1) 870 | { 871 | return -1; 872 | } 873 | else 874 | { 875 | perms->uid = statbuf.st_uid; 876 | perms->gid = statbuf.st_gid; 877 | perms->mode = statbuf.st_mode 878 | & (S_IRWXU|S_IRWXG|S_IRWXO|S_ISGID|S_ISUID|S_ISVTX); 879 | return 0; 880 | } 881 | } 882 | 883 | /* Restore permissions on a fd */ 884 | int 885 | restoreperms(const char* file,savedperms_t* perms) 886 | { 887 | int rv; 888 | 889 | if (chmod(file, perms->mode)==-1) return -1; 890 | 891 | /* Need privileges to chown it */ 892 | getprivs(); 893 | rv=chown(file, perms->uid, perms->gid); 894 | dropprivs(); 895 | 896 | return rv; 897 | } 898 | #endif /* ndef HAVE_GRANTPT */ 899 | 900 | /* Set tty-permissions on a fd */ 901 | int 902 | setperms(int fd,mode_t mode) 903 | { 904 | /* The chowning is only necessary if we're dealing with BSD-style ptys */ 905 | #ifndef HAVE_GRANTPT 906 | struct group *grbuf; 907 | gid_t group; 908 | int rv; 909 | /**/ 910 | 911 | /* Retrieve the tty group */ 912 | grbuf = getgrnam(tty_group); 913 | if (grbuf==NULL) 914 | { 915 | if (opt_verbose) 916 | { 917 | fprintf(stderr, "%s: cannot find a group named %s\n", 918 | progname, tty_group); 919 | } 920 | /* Use the user group, and remove permissions on group */ 921 | group = secure_gid; 922 | mode &= ~S_IRWXG; 923 | } 924 | else 925 | { 926 | group = grbuf->gr_gid; 927 | } 928 | 929 | /* Chown it with privs */ 930 | getprivs(); 931 | rv = fchown(fd, secure_uid, group); 932 | dropprivs(); 933 | if (rv==-1) return -1; 934 | #endif /* def HAVE_GRANTPT */ 935 | 936 | /* Chmod it */ 937 | return fchmod(fd, mode); 938 | } 939 | 940 | int 941 | xselect(int n, fd_set *readfds, fd_set *writefds, 942 | fd_set *exceptfds, struct timeval *timeout) 943 | { 944 | return select(n, readfds, writefds, exceptfds, timeout); 945 | } 946 | 947 | /* Master loop */ 948 | void 949 | loop_on(int fd, size_t ring_size, const char eof_char) 950 | { 951 | int chld_pipes[2]; 952 | sigset_t sset; 953 | int child_exited; 954 | ring_t* in_ring; 955 | ring_t* out_ring; 956 | fd_set readable_set; 957 | fd_set writable_set; 958 | int maxfd; 959 | int activefds; 960 | size_t space; 961 | int pty_flags; 962 | int eof_pty; 963 | int eof_stdin; 964 | /**/ 965 | 966 | /* Prepare a pipe for the signal handler, and finally unblock the SIGCHLD */ 967 | if (pipe(chld_pipes)<0) 968 | { 969 | fprintf(stderr, "%s: pipe(): %s\n", progname, strerror(errno)); 970 | exit(255); 971 | } 972 | child_exit_pipe = chld_pipes[1]; 973 | child_exited = 0; 974 | 975 | sigemptyset(&sset); 976 | sigaddset(&sset, SIGCHLD); 977 | if (sigprocmask(SIG_UNBLOCK, &sset, NULL)<0) 978 | { 979 | fprintf(stderr, "%s: sigprocmask(SIG_UNBLOCK, SIGCHLD): %s\n", 980 | progname, strerror(errno)); 981 | exit(255); 982 | } 983 | 984 | /* Prepare the rings */ 985 | in_ring = ring_construct(ring_size); 986 | out_ring = ring_construct(ring_size); 987 | 988 | /* Biggest fd */ 989 | maxfd = fd > 2 ? fd+1 : 3; 990 | maxfd = chld_pipes[0] >= maxfd ? chld_pipes[0]+1 : maxfd; 991 | 992 | /* Save the file descriptor flags for all the fds we'll manipulate */ 993 | if ((state_saved_stdin_flags=fcntl(0, F_GETFL))==-1) 994 | { 995 | fprintf(stderr, "%s: F_GETFL on stdin: %s\n", 996 | progname, strerror(errno)); 997 | exit(255); 998 | } 999 | if ((state_saved_stdout_flags=fcntl(1, F_GETFL))==-1) 1000 | { 1001 | fprintf(stderr, "%s: F_GETFL on stdout: %s\n", 1002 | progname, strerror(errno)); 1003 | exit(255); 1004 | } 1005 | if ((pty_flags=fcntl(fd, F_GETFL))==-1) 1006 | { 1007 | fprintf(stderr, "%s: F_GETFL on pty master: %s\n", 1008 | progname, strerror(errno)); 1009 | exit(255); 1010 | } 1011 | 1012 | /* Set these descriptors to non blocking mode */ 1013 | if (fcntl(0, F_SETFL, state_saved_stdin_flags | O_NONBLOCK)==-1) { 1014 | fprintf(stderr, "%s: F_SETFL on stdin: %s\n", 1015 | progname, strerror(errno)); 1016 | exit(255); 1017 | } 1018 | if (fcntl(1, F_SETFL, state_saved_stdout_flags | O_NONBLOCK)==-1) { 1019 | fprintf(stderr, "%s: F_SETFL on stdout: %s\n", 1020 | progname, strerror(errno)); 1021 | exit(255); 1022 | } 1023 | if (fcntl(fd, F_SETFL, pty_flags | O_NONBLOCK)==-1) { 1024 | fprintf(stderr, "%s: F_SETFL on pty master: %s\n", 1025 | progname, strerror(errno)); 1026 | exit(255); 1027 | } 1028 | 1029 | /* Initially, all our fds are open */ 1030 | eof_pty = 0; 1031 | eof_stdin = 0; 1032 | 1033 | /* Master loop */ 1034 | while(1) 1035 | { 1036 | /* Determine the ring states and set the fds to look at*/ 1037 | FD_ZERO(&readable_set); 1038 | FD_ZERO(&writable_set); 1039 | space = ring_space(in_ring); 1040 | if (space>0 && !eof_stdin) 1041 | FD_SET(0, &readable_set); 1042 | if (space0 && !eof_pty) 1046 | FD_SET(fd, &readable_set); 1047 | if (spaceend - self->start; 1265 | assert(size > 0); 1266 | assert(self->start <= self->head && self->head < self->end); 1267 | assert(self->start <= self->tail && self->tail < self->end); 1268 | if (self->tail==self->head) 1269 | assert(self->space == 0 || self->space == (size_t)size); 1270 | else if (self->tail > self->head) 1271 | assert((ssize_t)self->space == size - (self->tail - self->head)); 1272 | else 1273 | assert((ssize_t)self->space == self->head - self->tail); 1274 | } 1275 | #endif 1276 | 1277 | /* Ring allocation */ 1278 | ring_t* 1279 | ring_construct(size_t size) 1280 | { 1281 | ring_t* self; 1282 | self = xmalloc(sizeof(ring_t)); 1283 | self->start = xmalloc(size); 1284 | self->end = self->start + size; 1285 | self->head = self->tail = self->start; 1286 | self->space = size; 1287 | 1288 | RING_CHECK(self); 1289 | return self; 1290 | } 1291 | 1292 | /* Ring deallocation */ 1293 | void 1294 | ring_delete(ring_t *self) 1295 | { 1296 | RING_CHECK(self); 1297 | free(self->start); 1298 | free(self); 1299 | } 1300 | 1301 | /* Return free space in ring */ 1302 | size_t 1303 | ring_space(ring_t *self) 1304 | { 1305 | RING_CHECK(self); 1306 | return self->space; 1307 | } 1308 | 1309 | /* Read the biggest possible block in a ring from a fd */ 1310 | int 1311 | ring_read(ring_t *self, int fd) 1312 | { 1313 | int readcount=0; 1314 | /**/ 1315 | 1316 | if (self->space) 1317 | { 1318 | size_t chunk; 1319 | /**/ 1320 | 1321 | /* Determine largest readable chunk */ 1322 | if (self->tail >= self->head) 1323 | { 1324 | chunk = self->end - self->tail; 1325 | } 1326 | else 1327 | { 1328 | chunk = self->head - self->tail; 1329 | } 1330 | 1331 | /* Read the chunk */ 1332 | readcount = read(fd, self->tail, chunk); 1333 | if (readcount>0) 1334 | { 1335 | self->tail += readcount; 1336 | self->space -= readcount; 1337 | if (self->tail == self->end) 1338 | { 1339 | self->tail = self->start; 1340 | } 1341 | } 1342 | 1343 | } 1344 | 1345 | RING_CHECK(self); 1346 | return readcount; 1347 | } 1348 | 1349 | /* Write the biggest possible block in a ring to a fd */ 1350 | int 1351 | ring_write(ring_t *self, int fd) 1352 | { 1353 | int writtencount=0; 1354 | /**/ 1355 | 1356 | if ((ssize_t)self->space != self->end - self->start) 1357 | { 1358 | size_t chunk; 1359 | /**/ 1360 | 1361 | /* Determine largest writable chunk */ 1362 | if (self->head >= self->tail) 1363 | { 1364 | chunk = self->end - self->head; 1365 | } 1366 | else 1367 | { 1368 | chunk = self->tail - self->head; 1369 | } 1370 | 1371 | /* Write the chunk */ 1372 | writtencount = write(fd, self->head, chunk); 1373 | if (writtencount>0) 1374 | { 1375 | self->head += writtencount; 1376 | self->space += writtencount; 1377 | if (self->head == self->end) 1378 | { 1379 | self->head = self->start; 1380 | } 1381 | } 1382 | } 1383 | 1384 | RING_CHECK(self); 1385 | return writtencount; 1386 | } 1387 | 1388 | /* Push a characted in a ring */ 1389 | int 1390 | ring_push_char(ring_t* self, const char c) 1391 | { 1392 | int writtencount=0; 1393 | /**/ 1394 | 1395 | if (self->space != 0) 1396 | { 1397 | /* Write the byte for output */ 1398 | writtencount = 1; 1399 | *(char*)self->tail = c; 1400 | self->tail += 1; 1401 | self->space -= 1; 1402 | if (self->tail == self->end) 1403 | { 1404 | self->tail = self->start; 1405 | } 1406 | } 1407 | 1408 | RING_CHECK(self); 1409 | return writtencount; 1410 | } 1411 | 1412 | /* Cleanup everything */ 1413 | void 1414 | cleanup(void) 1415 | { 1416 | /* Restore our state */ 1417 | if (state_saved_stdin_flags!=-1) 1418 | { 1419 | fcntl(0, F_SETFL, state_saved_stdin_flags); 1420 | } 1421 | if (state_saved_stdout_flags!=-1) 1422 | { 1423 | fcntl(1, F_SETFL, state_saved_stdout_flags); 1424 | } 1425 | if (state_tty_name) 1426 | { 1427 | if (restoreperms(state_tty_name, &state_tty_perms)) 1428 | do {} while (0); 1429 | } 1430 | 1431 | /* Restore terminal */ 1432 | term_restore(0); 1433 | } 1434 | 1435 | /* Pass into terminal raw mode and save the old mode */ 1436 | int 1437 | term_raw(int verbose) 1438 | { 1439 | struct termios tios; 1440 | sigset_t blockedset; 1441 | sigset_t oldset; 1442 | /**/ 1443 | 1444 | /* Block all signals */ 1445 | sigfillset(&blockedset); 1446 | sigprocmask(SIG_SETMASK, &blockedset, &oldset); 1447 | 1448 | /* Change the terminal */ 1449 | if (tcgetattr(0, &tios)==-1) 1450 | { 1451 | if (verbose) 1452 | { 1453 | fprintf(stderr, "%s: tcgetattr on current terminal: %s\n", 1454 | progname, strerror(errno)); 1455 | exit(255); 1456 | } 1457 | return -1; 1458 | } 1459 | if (state_orig_termios==NULL) 1460 | { 1461 | state_orig_termios = xmalloc(sizeof(struct termios)); 1462 | *state_orig_termios = tios; 1463 | } 1464 | tios.c_lflag &= ~(ICANON|ECHO); 1465 | tios.c_cc[VMIN] = 1; 1466 | tios.c_cc[VTIME] = 0; 1467 | if (tcsetattr(0, TCSAFLUSH, &tios)==-1) 1468 | { 1469 | if (verbose) 1470 | { 1471 | fprintf(stderr, "%s: tcsetattr on current terminal: %s\n", 1472 | progname, strerror(errno)); 1473 | exit(255); 1474 | } 1475 | return -1; 1476 | } 1477 | 1478 | /* Restore signals */ 1479 | sigprocmask(SIG_SETMASK, &oldset, NULL); 1480 | return 0; 1481 | } 1482 | 1483 | /* Restore terminal state */ 1484 | int 1485 | term_restore(int verbose) 1486 | { 1487 | if (state_orig_termios!=NULL) 1488 | { 1489 | sigset_t blockedset; 1490 | sigset_t oldset; 1491 | /**/ 1492 | 1493 | /* Block all signals */ 1494 | sigfillset(&blockedset); 1495 | sigprocmask(SIG_SETMASK, &blockedset, &oldset); 1496 | 1497 | /* Restore the terminal */ 1498 | if (tcsetattr(0, TCSAFLUSH, state_orig_termios)==-1) 1499 | { 1500 | if (verbose) 1501 | { 1502 | fprintf(stderr, "%s: tcsetattr on current terminal: %s\n", 1503 | progname, strerror(errno)); 1504 | exit(255); 1505 | } 1506 | return -1; 1507 | } 1508 | free(state_orig_termios); 1509 | state_orig_termios=0; 1510 | 1511 | /* Restore signals */ 1512 | sigprocmask(SIG_SETMASK, &oldset, NULL); 1513 | } 1514 | return 0; 1515 | } 1516 | 1517 | /* Communicate window size to child */ 1518 | int 1519 | term_winsize(int verbose) 1520 | { 1521 | if (state_pty_fd) 1522 | { 1523 | struct winsize ws; 1524 | if (opt_geometry_height == 0 && opt_geometry_width == 0) 1525 | { 1526 | if (ioctl(0, TIOCGWINSZ, &ws)==-1) 1527 | { 1528 | if (verbose) 1529 | { 1530 | fprintf(stderr, "%s: TIOCGWINSZ on stdin: %s\n", 1531 | progname, strerror(errno)); 1532 | exit(255); 1533 | } 1534 | return -1; 1535 | } 1536 | } 1537 | else 1538 | { 1539 | ws.ws_row = opt_geometry_height; 1540 | ws.ws_col = opt_geometry_width; 1541 | ws.ws_xpixel = 0; 1542 | ws.ws_ypixel = 0; 1543 | } 1544 | if (ioctl(state_pty_fd, TIOCSWINSZ, &ws)==-1) 1545 | { 1546 | if (verbose) 1547 | { 1548 | fprintf(stderr, "%s: TIOCSWINSZ on pty slave: %s\n", 1549 | progname, strerror(errno)); 1550 | exit(255); 1551 | } 1552 | return -1; 1553 | } 1554 | } 1555 | return 0; 1556 | } 1557 | 1558 | /* SIGCHLD handler */ 1559 | void 1560 | sig_chld_h(int sig) 1561 | { 1562 | UNUSED(sig); 1563 | /* write to a pipe to avoid race condition trick courtesy R.W. Stevens */ 1564 | if (write(child_exit_pipe, "D", 1)!=1) 1565 | { 1566 | fprintf(stderr, "%s: in sig_chld_h: write(child_exit_pipe): %s\n", 1567 | progname, strerror(errno)); 1568 | exit(255); 1569 | } 1570 | } 1571 | 1572 | /* all fatal signals handler */ 1573 | void 1574 | sig_fatal_h(int sig) 1575 | { 1576 | struct sigaction sa; 1577 | /**/ 1578 | 1579 | /* Do the cleanup */ 1580 | cleanup(); 1581 | 1582 | /* Disarm this signal handler */ 1583 | sa.sa_handler = SIG_DFL; 1584 | sigemptyset(&sa.sa_mask); 1585 | sa.sa_flags = SA_NODEFER; 1586 | sigaction(sig, &sa, NULL); 1587 | 1588 | /* Raise the signal */ 1589 | raise(sig); 1590 | } 1591 | 1592 | /* Terminal stop */ 1593 | void 1594 | sig_tstp_h(int sig) 1595 | { 1596 | struct sigaction sa; 1597 | struct sigaction oldsa; 1598 | /**/ 1599 | 1600 | /* Restore terminal */ 1601 | if (term_restore(0)==-1) _exit(255); 1602 | 1603 | /* Warn child */ 1604 | kill(state_child_pid, sig); 1605 | 1606 | /* Restore default signal bindings */ 1607 | sa.sa_handler = SIG_DFL; 1608 | sigemptyset(&sa.sa_mask); 1609 | sa.sa_flags = SA_RESTART; 1610 | sigaction(SIGTSTP, &sa, &oldsa); 1611 | 1612 | /* Raise a new TSTP */ 1613 | raise(SIGTSTP); 1614 | 1615 | /* Restore old action */ 1616 | sigaction(SIGTSTP, &oldsa, NULL); 1617 | 1618 | } 1619 | 1620 | /* Continuation */ 1621 | void 1622 | sig_cont_h(int sig) 1623 | { 1624 | UNUSED(sig); 1625 | if (term_raw(0)==-1) _exit(255); 1626 | kill(state_child_pid, SIGWINCH); 1627 | } 1628 | 1629 | /* Window size change */ 1630 | void 1631 | sig_winch_h(int sig) 1632 | { 1633 | if (term_winsize(0)!=-1) 1634 | { 1635 | kill(state_child_pid, sig); 1636 | } 1637 | } 1638 | --------------------------------------------------------------------------------