Why dup()?

David Newall openssh at davidnewall.com
Fri Oct 13 23:21:04 AEDT 2017

> I did have a better example.

I do have a better example.

   ssh localhost 'exec cat > /dev/null 2>&1' | { read l; echo ${l:-EOF}; }

(Bonus points for seeing the irony.)

This should immediately print EOF.  It doesn't, because the duplicate 
file descriptor on stdout prevents pipe closure until ssh exits.

Simply removing dup(), that is, saying "out = STDOUT_FILENO", results in 
correct behavior.  I think it's the right change.  I'll go further and 
suggest that the same change should be made to err.  I would suggest it 
should also be made to in, but that breaks tcsetattr on exit.

More information about the openssh-unix-dev mailing list