What does POSIX mean when it says stderr is expected to be open for reading and writing?
The Austin Group (the joint working group that maintains the POSIX standard) discussed the defect that was reported about the "expected to be" wording during the 2020-12-07 and 2020-12-10 telecons and agreed that the wording in POSIX Issue 7 (2018 edition) is problematic. The wording will be changed in the next version of the POSIX standard as follows (copied from the bug comment):
On page 65 before line 1912 (XBD "interactive shell") insert a new definition and renumber the remaining:
Interactive Device
A terminal device.
Note: This definition is intended to align with the ISO C standard's use of "interactive device".
On page 496 lines 17224-17228 (XSH 2.5 Standard I/O Streams) and page 2017 lines 64723-64727 (XSH stderr, stdin, stdout DESCRIPTION) change:
At program start-up, three streams shall be predefined and need not be opened explicitly: standard input (for reading conventional input), standard output (for writing conventional output), and standard error (for writing diagnostic output). When opened, the standard error stream is not fully buffered; the standard input and standard output streams are fully buffered if and only if the stream can be determined not to refer to an interactive device.
to:
At program start-up, three streams shall be predefined and already open: stdin (standard input, for conventional input) for reading, stdout (standard output, for conventional output) for writing, and stderr (standard error, for diagnostic output) for writing. When opened, stderr shall not be fully buffered; stdin and stdout shall be fully buffered if and only if [CX]the file descriptor associated with the stream is determined not to be associated with an interactive device.[/CX]
On page 2017 line 64733 (XSH stdin DESCRIPTION), change:
The stderr stream is expected to be open for reading and writing.
to:
These file descriptors are often all associated with a single open file description which has access mode O_RDWR (e.g., in the case of a terminal device for a login shell). However, the stderr, stdin, and stdout streams need not be opened for both reading and writing at program start-up in this case.
On page 2017 line 64747 (stdin SEE ALSO) add isatty().
Special thanks to Rich Felker for reporting the defect.