[mosh-devel] mosh-server stream changes

Timo Sirainen tss at iki.fi
Mon Apr 30 18:44:35 EDT 2012


This is a bit dangerous:

    /* Necessary to properly detach on old versions of sshd (e.g. RHEL/CentOS 5.0). */
    fclose( stdin );
    fclose( stdout );
    fclose( stderr );

It actually closes the 0/1/2 fds and they will be reused by whatever fds are created next. I think this code attempts to create them back:

    /* reopen stdio */
    stdin = fdopen( STDIN_FILENO, "r" );
    stdout = fdopen( STDOUT_FILENO, "w" );
    stderr = fdopen( STDERR_FILENO, "w" );

But that doesn't work, because those fds are already closed. Reads/writes to these will just fail or will access unintended fds. Also this code doesn't compile in Solaris.

A safer way to detach those fds would be to do e.g.

int nullfd = open("/dev/null", O_RDWR);
dup2(nullfd, STDIN_FILENO);
dup2(nullfd, STDOUT_FILENO);
dup2(nullfd, STDERR_FILENO);





More information about the mosh-devel mailing list