From 0e9be1b471ce824d22cca156a4747b7306726524 Mon Sep 17 00:00:00 2001 From: Keith Winstein Date: Sat, 14 Apr 2012 12:37:28 -0400 Subject: [PATCH] Close terminal fd's to properly detach on old sshd (e.g. RHEL/CentOS 5.0). May help some sufferers of #114 github issue. --- src/frontend/mosh-server.cc | 19 +++++++++++++++---- 1 file changed, 15 insertions(+), 4 deletions(-) diff --git a/src/frontend/mosh-server.cc b/src/frontend/mosh-server.cc index df6ec90..c32b945 100644 --- a/src/frontend/mosh-server.cc +++ b/src/frontend/mosh-server.cc @@ -66,7 +66,7 @@ void serve( int host_fd, ServerConnection &network ); int run_server( const char *desired_ip, const char *desired_port, - char *command[], const int colors ); + char *command[], const int colors, bool verbose ); using namespace std; @@ -111,6 +111,8 @@ int main( int argc, char *argv[] ) char *desired_port = NULL; char **command = NULL; int colors = 0; + bool verbose = false; /* don't close stdin/stdout/stderr */ + /* Will cause mosh-server not to correctly detach on old versions of sshd. */ /* strip off command */ for ( int i = 0; i < argc; i++ ) { @@ -128,7 +130,7 @@ int main( int argc, char *argv[] ) && (strcmp( argv[ 1 ], "new" ) == 0) ) { /* new option syntax */ int opt; - while ( (opt = getopt( argc - 1, argv + 1, "i:p:c:s" )) != -1 ) { + while ( (opt = getopt( argc - 1, argv + 1, "i:p:c:sv" )) != -1 ) { switch ( opt ) { case 'i': desired_ip = optarg; @@ -143,6 +145,9 @@ int main( int argc, char *argv[] ) case 'c': colors = myatoi( optarg ); break; + case 'v': + verbose = true; + break; default: print_usage( argv[ 0 ] ); /* don't die on unknown options */ @@ -201,7 +206,7 @@ int main( int argc, char *argv[] ) assert_utf8_locale(); try { - return run_server( desired_ip, desired_port, command, colors ); + return run_server( desired_ip, desired_port, command, colors, verbose ); } catch ( Network::NetworkException e ) { fprintf( stderr, "Network exception: %s: %s\n", e.function.c_str(), strerror( e.the_errno ) ); @@ -214,7 +219,7 @@ int main( int argc, char *argv[] ) } int run_server( const char *desired_ip, const char *desired_port, - char *command[], const int colors ) { + char *command[], const int colors, bool verbose ) { /* get initial window size */ struct winsize window_size; if ( ioctl( STDIN_FILENO, TIOCGWINSZ, &window_size ) < 0 ) { @@ -332,6 +337,12 @@ int run_server( const char *desired_ip, const char *desired_port, } } else { /* parent */ + if ( !verbose ) { + /* Necessary to properly detach on old versions of sshd (e.g. RHEL/CentOS 5.0). */ + close( STDIN_FILENO ); + close( STDOUT_FILENO ); + close( STDERR_FILENO ); + } #ifdef HAVE_UTEMPTER /* make utmp entry */