forked from mobile-shell/mosh
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
server: add option to stay in foreground
Fixes mobile-shell#1091
- Loading branch information
Showing
2 changed files
with
42 additions
and
24 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -105,7 +105,8 @@ static void serve( int host_fd, | |
|
||
static int run_server( const char *desired_ip, const char *desired_port, | ||
const string &command_path, char *command_argv[], | ||
const int colors, unsigned int verbose, bool with_motd ); | ||
const int colors, unsigned int verbose, bool with_motd, | ||
bool foreground ); | ||
|
||
|
||
static void print_version( FILE *file ) | ||
|
@@ -119,7 +120,7 @@ static void print_version( FILE *file ) | |
|
||
static void print_usage( FILE *stream, const char *argv0 ) | ||
{ | ||
fprintf( stream, "Usage: %s new [-s] [-v] [-i LOCALADDR] [-p PORT[:PORT2]] [-c COLORS] [-l NAME=VALUE] [-- COMMAND...]\n", argv0 ); | ||
fprintf( stream, "Usage: %s new [-D] [-s] [-v] [-i LOCALADDR] [-p PORT[:PORT2]] [-c COLORS] [-l NAME=VALUE] [-- COMMAND...]\n", argv0 ); | ||
} | ||
|
||
static bool print_motd( const char *filename ); | ||
|
@@ -183,6 +184,7 @@ int main( int argc, char *argv[] ) | |
char **command_argv = NULL; | ||
int colors = 0; | ||
unsigned int verbose = 0; /* don't close stdin/stdout/stderr */ | ||
bool foreground = false; /* stay in the foreground, don't fork into background */ | ||
/* Will cause mosh-server not to correctly detach on old versions of sshd. */ | ||
list<string> locale_vars; | ||
|
||
|
@@ -210,7 +212,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:svl:" )) != -1 ) { | ||
while ( (opt = getopt( argc - 1, argv + 1, "@:i:p:c:svl:D" )) != -1 ) { | ||
switch ( opt ) { | ||
/* | ||
* This undocumented option does nothing but eat its argument. | ||
|
@@ -251,6 +253,9 @@ int main( int argc, char *argv[] ) | |
case 'l': | ||
locale_vars.push_back( string( optarg ) ); | ||
break; | ||
case 'D': | ||
foreground = true; | ||
break; | ||
default: | ||
/* don't die on unknown options */ | ||
print_usage( stderr, argv[ 0 ] ); | ||
|
@@ -364,7 +369,7 @@ int main( int argc, char *argv[] ) | |
} | ||
|
||
try { | ||
return run_server( desired_ip, desired_port, command_path, command_argv, colors, verbose, with_motd ); | ||
return run_server( desired_ip, desired_port, command_path, command_argv, colors, verbose, with_motd, foreground ); | ||
} catch ( const Network::NetworkException &e ) { | ||
fprintf( stderr, "Network exception: %s\n", | ||
e.what() ); | ||
|
@@ -378,7 +383,8 @@ int main( int argc, char *argv[] ) | |
|
||
static int run_server( const char *desired_ip, const char *desired_port, | ||
const string &command_path, char *command_argv[], | ||
const int colors, unsigned int verbose, bool with_motd ) { | ||
const int colors, unsigned int verbose, bool with_motd, | ||
bool foreground ) { | ||
/* get network idle timeout */ | ||
long network_timeout = 0; | ||
char *timeout_envar = getenv( "MOSH_SERVER_NETWORK_TMOUT" ); | ||
|
@@ -448,34 +454,41 @@ static int run_server( const char *desired_ip, const char *desired_port, | |
fatal_assert( 0 == sigaction( SIGHUP, &sa, NULL ) ); | ||
fatal_assert( 0 == sigaction( SIGPIPE, &sa, NULL ) ); | ||
|
||
pid_t the_pid = -1; | ||
if (!foreground) { | ||
/* detach from terminal */ | ||
fflush( NULL ); | ||
the_pid = fork(); | ||
} | ||
|
||
/* detach from terminal */ | ||
fflush( NULL ); | ||
pid_t the_pid = fork(); | ||
if ( the_pid < 0 ) { | ||
if ( the_pid < 0 && !foreground) { | ||
perror( "fork" ); | ||
} else if ( the_pid > 0 ) { | ||
} else if ( the_pid > 0 || foreground ) { | ||
fputs( "\nmosh-server (" PACKAGE_STRING ") [build " BUILD_VERSION "]\n" | ||
"Copyright 2012 Keith Winstein <[email protected]>\n" | ||
"License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>.\n" | ||
"This is free software: you are free to change and redistribute it.\n" | ||
"There is NO WARRANTY, to the extent permitted by law.\n\n", stderr ); | ||
"Copyright 2012 Keith Winstein <[email protected]>\n" | ||
"License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>.\n" | ||
"This is free software: you are free to change and redistribute it.\n" | ||
"There is NO WARRANTY, to the extent permitted by law.\n\n", stderr ); | ||
|
||
fprintf( stderr, "[mosh-server detached, pid = %d]\n", static_cast<int>(the_pid) ); | ||
if (!foreground) { | ||
fprintf( stderr, "[mosh-server detached, pid = %d]\n", static_cast<int>(the_pid) ); | ||
} | ||
#ifndef HAVE_IUTF8 | ||
fputs( "\nWarning: termios IUTF8 flag not defined.\n" | ||
"Character-erase of multibyte character sequence\n" | ||
"probably does not work properly on this platform.\n", stderr ); | ||
"Character-erase of multibyte character sequence\n" | ||
"probably does not work properly on this platform.\n", stderr ); | ||
#endif /* HAVE_IUTF8 */ | ||
|
||
fflush( NULL ); | ||
if ( isatty( STDOUT_FILENO ) ) { | ||
tcdrain( STDOUT_FILENO ); | ||
} | ||
if ( isatty( STDERR_FILENO ) ) { | ||
tcdrain( STDERR_FILENO ); | ||
if (!foreground) { | ||
fflush( NULL ); | ||
if ( isatty( STDOUT_FILENO ) ) { | ||
tcdrain( STDOUT_FILENO ); | ||
} | ||
if ( isatty( STDERR_FILENO ) ) { | ||
tcdrain( STDERR_FILENO ); | ||
} | ||
exit( 0 ); | ||
} | ||
exit( 0 ); | ||
} | ||
|
||
int master; | ||
|