Why dup()?

Damien Miller djm at mindrot.org
Fri Oct 13 16:52:23 AEDT 2017

On Tue, 10 Oct 2017, David Newall wrote:

> 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);

This is probably because some things interact with stderr (and possibly
stdout) separately than through the channels layer.

>          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?

I don't think communicating closes on stdout separately to ssh itself
terminating is a case we've considered supporting before. Maybe we
could, but there are bound to be some subtleties beyond your suggested

At a minimum, I think we'd have to dup2 a fd to /dev/null to
STDOUT_FILENO so writes to stdout (e.g. from ill-behaved libraries) have
somewhere to go. I think we'd want to leave stderr the way it is too.


More information about the openssh-unix-dev mailing list