#1056297 dash: stdin being inappropriately redirected to /dev/null for background sub-shells

Package:
dash
Source:
dash
Description:
POSIX-compliant shell
Submitter:
WHR
Date:
2023-11-20 06:51:03 UTC
Severity:
normal
#1056297#5
Date:
2023-11-20 06:48:59 UTC
From:
To:
Please see the following test case:


# cat redir-stdin-test.sh
#!/bin/sh
set -e
exec 0<> /dev/ttyUSB0 1>&0
{
	ls -l /proc/self/fd/0 1>&2
	stty -a 1>&2
	hexdump -v -e '1/1 "%02x\n"'
} | while read b; do
	echo "$b" 1>&2
done & read_pid=$!
while [ -d /proc/$read_pid ]; do
	echo 1
	sleep 1
done
# bash redir-stdin-test.sh
lrwx------ 1 root root 64 Nov 20 06:26 /proc/self/fd/0 -> /dev/ttyUSB0
speed 9600 baud; rows 0; columns 0; line = 0;
intr = ^C; quit = ^\; erase = ^?; kill = ^U; eof = ^D; eol = <undef>; eol2 = <undef>;
swtch = <undef>; start = ^Q; stop = ^S; susp = ^Z; rprnt = ^R; werase = ^W; lnext = ^V;
discard = ^U; min = 1; time = 0;
-parenb -parodd -cmspar cs8 hupcl -cstopb cread clocal -crtscts
-ignbrk -brkint -ignpar -parmrk -inpck -istrip -inlcr -igncr icrnl ixon -ixoff -iuclc -ixany
-imaxbel -iutf8
opost -olcuc -ocrnl onlcr -onocr -onlret -ofill -ofdel nl0 cr0 tab0 bs0 vt0 ff0
isig icanon iexten echo echoe echok -echonl -noflsh -xcase -tostop -echoprt echoctl echoke -flusho
-extproc
^C
# dash redir-stdin-test.sh
lr-x------ 1 root root 64 Nov 20 06:26 /proc/self/fd/0 -> /dev/null
stty: 'standard input': Inappropriate ioctl for device


Apparently inappropriately redirected stdin to /dev/null for the background
sub-shell, despite the stdin was previously redirect to /dev/ttyUSB0, thus
breaking the workflow. The above test case shows that the same program works
in bash(1), but breaks in dash(1).

dash(1) shouldn't redirect stdin here because /dev/ttyUSB0 isn't the
controlling terminal of the current session, so reading it from a background
job isn't going to block it.