@@ -114,7 +114,9 @@ static int run_server( const char* desired_ip,
114114 const int colors,
115115 unsigned int verbose,
116116 bool with_motd,
117- bool with_agent_fwd );
117+ bool foreground,
118+ bool with_agent_fwd
119+ );
118120
119121static void print_version ( FILE* file )
120122{
@@ -129,7 +131,7 @@ static void print_version( FILE* file )
129131static void print_usage ( FILE* stream, const char * argv0 )
130132{
131133 fprintf ( stream,
132- " Usage: %s new [-s] [-v] [-i LOCALADDR] [-p PORT[:PORT2]] [-c COLORS] [-l NAME=VALUE] [-- COMMAND...]\n " ,
134+ " Usage: %s new [-D] [- s] [-v] [-i LOCALADDR] [-p PORT[:PORT2]] [-c COLORS] [-l NAME=VALUE] [-- COMMAND...]\n " ,
133135 argv0 );
134136}
135137
@@ -193,6 +195,7 @@ int main( int argc, char* argv[] )
193195 std::string command_path;
194196 char ** command_argv = NULL ;
195197 int colors = 0 ;
198+ bool foreground = false ; /* stay in the foreground, don't fork into background */
196199 bool with_agent_fwd = false ;
197200 unsigned int verbose = 0 ; /* don't close stdin/stdout/stderr */
198201 /* Will cause mosh-server not to correctly detach on old versions of sshd. */
@@ -221,7 +224,7 @@ int main( int argc, char* argv[] )
221224 if ( ( argc >= 2 ) && ( strcmp ( argv[1 ], " new" ) == 0 ) ) {
222225 /* new option syntax */
223226 int opt;
224- while ( ( opt = getopt ( argc - 1 , argv + 1 , " @:i:p:c:svl:A " ) ) != -1 ) {
227+ while ( ( opt = getopt ( argc - 1 , argv + 1 , " @:i:p:c:svl:AD " ) ) != -1 ) {
225228 switch ( opt ) {
226229 /*
227230 * This undocumented option does nothing but eat its argument.
@@ -262,6 +265,9 @@ int main( int argc, char* argv[] )
262265 case ' l' :
263266 locale_vars.push_back ( std::string ( optarg ) );
264267 break ;
268+ case ' D' :
269+ foreground = true ;
270+ break ;
265271 case ' A' :
266272 with_agent_fwd = true ;
267273 break ;
@@ -380,7 +386,7 @@ int main( int argc, char* argv[] )
380386 }
381387
382388 try {
383- return run_server ( desired_ip, desired_port, command_path, command_argv, colors, verbose, with_motd, with_agent_fwd );
389+ return run_server ( desired_ip, desired_port, command_path, command_argv, colors, verbose, with_motd, foreground, with_agent_fwd );
384390 } catch ( const Network::NetworkException& e ) {
385391 fprintf ( stderr, " Network exception: %s\n " , e.what () );
386392 return 1 ;
@@ -397,7 +403,9 @@ static int run_server( const char* desired_ip,
397403 const int colors,
398404 unsigned int verbose,
399405 bool with_motd,
400- bool with_agent_fwd )
406+ bool foreground,
407+ bool with_agent_fwd
408+ )
401409{
402410 /* get network idle timeout */
403411 long network_timeout = 0 ;
@@ -466,35 +474,42 @@ static int run_server( const char* desired_ip,
466474 fatal_assert ( 0 == sigaction ( SIGHUP, &sa, NULL ) );
467475 fatal_assert ( 0 == sigaction ( SIGPIPE, &sa, NULL ) );
468476
469- /* detach from terminal */
470- fflush ( NULL );
471- pid_t the_pid = fork ();
472- if ( the_pid < 0 ) {
477+ pid_t the_pid = -1 ;
478+ if ( !foreground )
479+ /* detach from terminal */
480+ fflush ( NULL );
481+ the_pid = fork ();
482+ }
483+ if ( the_pid < 0 && !foreground ) {
473484 perror ( " fork" );
474- } else if ( the_pid > 0 ) {
485+ } else if ( the_pid > 0 || foreground ) {
475486 fputs ( " \n mosh-server (" PACKAGE_STRING " ) [build " BUILD_VERSION " ]\n "
476487 " Copyright 2012 Keith Winstein <[email protected] >\n " 477488 " License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>.\n "
478489 " This is free software: you are free to change and redistribute it.\n "
479490 " There is NO WARRANTY, to the extent permitted by law.\n\n " ,
480491 stderr );
481492
482- fprintf ( stderr, " [mosh-server detached, pid = %d]\n " , static_cast <int >( the_pid ) );
493+ if ( !foreground ) {
494+ fprintf ( stderr, " [mosh-server detached, pid = %d]\n " , static_cast <int >( the_pid ) );
495+ }
483496#ifndef HAVE_IUTF8
484497 fputs ( " \n Warning: termios IUTF8 flag not defined.\n "
485498 " Character-erase of multibyte character sequence\n "
486499 " probably does not work properly on this platform.\n " ,
487500 stderr );
488501#endif /* HAVE_IUTF8 */
489502
490- fflush ( NULL );
491- if ( isatty ( STDOUT_FILENO ) ) {
492- tcdrain ( STDOUT_FILENO );
493- }
494- if ( isatty ( STDERR_FILENO ) ) {
495- tcdrain ( STDERR_FILENO );
503+ if ( !foreground ) {
504+ fflush ( NULL );
505+ if ( isatty ( STDOUT_FILENO ) ) {
506+ tcdrain ( STDOUT_FILENO );
507+ }
508+ if ( isatty ( STDERR_FILENO ) ) {
509+ tcdrain ( STDERR_FILENO );
510+ }
511+ exit ( 0 );
496512 }
497- exit ( 0 );
498513 }
499514
500515 /* initialize agent listener if requested */
0 commit comments