1
0
Fork 0
mirror of git://git.code.sf.net/p/cdesktopenv/code synced 2025-03-09 15:50:02 +00:00

posix: use real pipe(2) instead of socketpair(2)

The POSIX standard requires real UNIX pipes as in pipe(2). But on
systems supporting it (all modern ones), ksh uses socketpair(2)
instead to make it possible for certain commands to peek ahead
without consuming input from the pipe, which is not possible with
real pipes. See features/poll and sh/io.c.

But this can create undesired side effects: applications connected
to a pipe may test if they are connected to a pipe, which will fail
if they are connected to a socket. Also, on Linux:

  $ cat /etc/passwd | head -1 /dev/stdin
  head: cannot open '/dev/stdin' for reading: No such device or address

...which happens because, unlike most systems, Linux cannot open(2)
or openat(2) a socket (a limitation that is allowed by POSIX).

Unfortunately at least two things depend on the peekahead
capability of the _pipe_socketpair feature. One is the non-blocking
behaviour of the -n option of the 'read' built-in:

  -n      Causes at most n bytes to be  read  instead  of  a  full
          line, but will return when reading from a slow device as
          soon as any characters have been read.

The other thing that breaks is the <#pattern and <##pattern
redirection operators that basically grep standard input, which
inherently requires peekahead.

Standard UNIX pipes always block on read and it is not possible to
peek ahead, so these features inevitably break. Which means we
cannot simply use standard pipes without breaking compatibility.

But we can at least fix it in the POSIX mode so that cross-platform
scripts work more correctly.

src/cmd/ksh93/sh/io.c: sh_pipe():
- If _pipe_socketpair is detected at compile time, then use a real
  pipe via sh_rpipe() if the POSIX mode is active. (If
  _pipe_socketpair is not detected, a real pipe is always used.)

src/cmd/ksh93/data/builtins.c:
- sh.1 documents the slow-device behaviour of -n but 'read --man'
  did not. Add that, making it conditional on _pipe_socketpair.

Resolves: https://github.com/ksh93/ksh/issues/327
This commit is contained in:
Martijn Dekker 2022-06-09 15:51:44 +01:00
parent a46c8e74f7
commit b14e79e9d0
6 changed files with 44 additions and 3 deletions

8
NEWS
View file

@ -3,6 +3,14 @@ For full details, see the git log at: https://github.com/ksh93/ksh/tree/1.0
Any uppercase BUG_* names are modernish shell bug IDs.
2022-06-09:
- The POSIX mode has been amended to use a UNIX pipe(2) instead of a
socketpair(2) to connect commands in a pipeline, as the standard requires.
(When reading directly from a pipeline in posix mode, the <#pattern and
<##pattern redirection operators will not work and the -n option to the
read built-in will not return early when reading from a slow device.)
2022-06-08:
- If -B/--braceexpand is turned on in --posix mode, it now only allows brace