summaryrefslogtreecommitdiff
path: root/configure.ac
diff options
context:
space:
mode:
authorAlex Dowad <alexinbeijing@gmail.com>2020-05-03 17:54:06 +0200
committerNikita Popov <nikita.ppv@gmail.com>2020-05-14 10:25:37 +0200
commita84cd96e865db795edc72b2799eac5c3c67aadfe (patch)
tree06318d924747531757253985e8fd2e603cf30fba /configure.ac
parentae5ca5459f9b724495c967eaa72b414c4ccb8c8b (diff)
downloadphp-git-a84cd96e865db795edc72b2799eac5c3c67aadfe.tar.gz
Add PTY support to proc_open (again after 16 long years)
Back in 2004, a feature was added to proc_open which allowed it to open a PTY, connecting specific FDs in the child process to the slave end of the PTY and returning the master end of the PTY (wrapped as a PHP stream) in the `$pipes` array. However, this feature was disabled just about a month later. Little information is available about why this was done, but from talking to the original implementer, it seems there were portability problems with some rare flavors of Unix. Re-enable this feature with a simplified implementation which uses openpty(). No attempt is made to support PTYs if the platform does not have openpty(). The configure script checks if linking with -lutil is necessary to use openpty(), but if anything else is required, like including some special header or linking with some other library, PTY support will be disabled. The original PTY support for proc_open automatically daemonized the child process (disassociating it from the TTY session and process group of the parent). However, I don't think this is a good idea. Just because a user opens a child process in a PTY, it doesn't mean they want it to continue running even when the parent process is killed. Of course, if the child process is some kind of server, it will likely daemonize itself; but we have no reason to preempt that decision. It turns out that since 2015, there has been one test case for PTY support in proc_open() in the test suite. This test was added in GitHub PR #1588 (https://github.com/php/php-src/pull/1588). That PR mentioned that the PHP binary in the Debian/Ubuntu repositories is patched to *enable* PTY support. Checking the Debian PHP repository (https://salsa.debian.org/php-team/php.git) shows that this is still true. Debian's patch does not modify the implementation from 2004 in any way; it just removes the #if 0 line which disables it. Naturally, the test case is skipped if PTY support is not enabled. This means that ever since it was added, every test run against the 'vanilla' PHP codebase has skipped it. Interestingly, the test case which was added in 2015 fails on my Linux Mint PC... both with this simplified implementation *and* when enabling the original implementation. Investigation reveals the reason: when the child process using the slave end of the PTY exits and its FDs are all closed, and all buffered data is read from the master end of the PTY, any further attempt to read from the master end fails with EIO. The test case seems to expect that reading from the master end will always return an empty string if no data is available. Likely this is because PHP's fread() was updated to report errors from the underlying system calls only recently. One way out of this dilemma: IF at least one FD referring to the slave end of the PTY is kept open *in the parent process*, the failure with EIO will not occur even after the child process exits. However, that would raise another issue: we would need a way to ensure the FD will be closed eventually in long-running programs. Another discovery made while testing this code is that fread() does not always return all the data written to the slave end of the PTY in a single call, even if the data was written with a single syscall and it is only a few bytes long. Specifically, when the child process in the test case writes "foo\n" to the PTY, the parent sometimes receives "foo" (3 bytes) and sometimes "foo\r\n" (5 bytes). (The "\r" is from the TTY line discipline converting "\n" to "\r\n".) A second call to fread() does return the remaining bytes, though sometimes all the data is read in the first call, and by the time the second call is made, the child process has already exited. It seems that liberal use of the @ operator is needed when using fread() on pipes. Thanks to Nikita Popov for suggesting that we should just use openpty() rather than grantpt(), unlockpt(), etc.
Diffstat (limited to 'configure.ac')
-rw-r--r--configure.ac7
1 files changed, 4 insertions, 3 deletions
diff --git a/configure.ac b/configure.ac
index 5f38fd580e..5278718f1a 100644
--- a/configure.ac
+++ b/configure.ac
@@ -392,6 +392,7 @@ malloc.h \
monetary.h \
netdb.h \
poll.h \
+pty.h \
pwd.h \
resolv.h \
strings.h \
@@ -560,7 +561,6 @@ getgrnam_r \
getpwuid_r \
getwd \
glob \
-grantpt \
inet_ntoa \
inet_ntop \
inet_pton \
@@ -572,7 +572,6 @@ mmap \
nice \
nl_langinfo \
poll \
-ptsname \
putenv \
scandir \
setitimer \
@@ -588,7 +587,6 @@ strptime \
strtok_r \
symlink \
tzset \
-unlockpt \
unsetenv \
usleep \
utime \
@@ -713,6 +711,9 @@ if test "$PHP_VALGRIND" != "no"; then
fi
fi
+dnl Check for openpty. It may require linking against libutil.
+PHP_CHECK_FUNC(openpty, util)
+
dnl General settings.
dnl ----------------------------------------------------------------------------
PHP_CONFIGURE_PART(General settings)