Why dup()?

David Newall openssh at davidnewall.com
Tue Oct 10 05:48:51 AEDT 2017

Hello all,

My scripts, which read stdout from ssh, weren't seeing EOF from the 
remote session.  It was being sent, but lost.  I tracked it down to the 
following code, in ssh.c, at ssh_session2_open:

         if (stdin_null_flag) {
                 in = open(_PATH_DEVNULL, O_RDONLY);
         } else {
                 in = dup(STDIN_FILENO);
         out = dup(STDOUT_FILENO);
         err = dup(STDERR_FILENO);

The remote session did close stdout.  The sshd from which it was spawned 
signaled to close stdout.  The ssh program received that signal and 
closed, well, something, but not stdout.  It closed a copy.  
Importantly, it left a copy open, so my program got no EOF.

Why not:

         if (stdin_null_flag) {
                 in = open(_PATH_DEVNULL, O_RDONLY);
         } else {
                 in = STDIN_FILENO;
         out = STDOUT_FILENO;
         err = STDERR_FILENO;

If not that, how is a program that reads from ssh's output ever going to 
see EOF?



More information about the openssh-unix-dev mailing list