Why dup()?

Douglas E Engert deengert at gmail.com
Sat Oct 14 00:49:27 AEDT 2017



On 10/13/2017 7:21 AM, David Newall wrote:
>> 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. 

No. Because cat is reading from stdin which is from the ssh command line that is still open
so cat is stil running.

If you type in a few lines, then hit Ctrl-D It does type EOF

To make it clear what is going on, changing /dev/null to /tmp/xxx

ssh localhost 'exec cat > /tmp/out 2>&1' | { read l; echo ${l:-EOF}; }
type in a few lines then Ctrl-D will type EOF



ssh localhost 'exec echo XYZ | cat > /tmp/xxx 2>&1' | { read l; echo ${l:-EOF}; }
will immediately type EOF and /tmp/xxx will have XYZ


ssh localhost 'exec echo XYZ | cat > /etc/nofilehere 2>&1' | { read l; echo ${l:-EOF}; }
outputs
bash: /etc/nofilehere: Permission denied
EOF

becase of the redirect of cat's stderr to stdout



  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.
> _______________________________________________
> openssh-unix-dev mailing list
> openssh-unix-dev at mindrot.org
> https://lists.mindrot.org/mailman/listinfo/openssh-unix-dev

-- 

  Douglas E. Engert  <DEEngert at gmail.com>



More information about the openssh-unix-dev mailing list