Skip to content

Commit 0fbf9fa

Browse files
committed
server: add option to stay in foreground
Fixes mobile-shell#1091
1 parent 03087e7 commit 0fbf9fa

File tree

2 files changed

+45
-27
lines changed

2 files changed

+45
-27
lines changed

man/mosh-server.1

+5
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ mosh-server \- server-side helper for mosh
2020
.SH SYNOPSIS
2121
.B mosh-server
2222
new
23+
[\-D]
2324
[\-s]
2425
[\-v]
2526
[\-i \fIIP\fP]
@@ -54,6 +55,10 @@ and the client's current IP address.
5455
The argument "new" must be first on the command line to use
5556
command-line options.
5657

58+
.TP
59+
.B \-D
60+
stay in the foreground instead of forking into the background
61+
5762
.TP
5863
.B \-s
5964
bind to the local interface used for an incoming SSH connection, given

src/frontend/mosh-server.cc

+40-27
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,8 @@ static void serve( int host_fd,
105105

106106
static int run_server( const char *desired_ip, const char *desired_port,
107107
const string &command_path, char *command_argv[],
108-
const int colors, unsigned int verbose, bool with_motd );
108+
const int colors, unsigned int verbose, bool with_motd,
109+
bool foreground );
109110

110111

111112
static void print_version( FILE *file )
@@ -119,7 +120,7 @@ static void print_version( FILE *file )
119120

120121
static void print_usage( FILE *stream, const char *argv0 )
121122
{
122-
fprintf( stream, "Usage: %s new [-s] [-v] [-i LOCALADDR] [-p PORT[:PORT2]] [-c COLORS] [-l NAME=VALUE] [-- COMMAND...]\n", argv0 );
123+
fprintf( stream, "Usage: %s new [-D] [-s] [-v] [-i LOCALADDR] [-p PORT[:PORT2]] [-c COLORS] [-l NAME=VALUE] [-- COMMAND...]\n", argv0 );
123124
}
124125

125126
static bool print_motd( const char *filename );
@@ -183,6 +184,7 @@ int main( int argc, char *argv[] )
183184
char **command_argv = NULL;
184185
int colors = 0;
185186
unsigned int verbose = 0; /* don't close stdin/stdout/stderr */
187+
bool foreground = false; /* stay in the foreground, don't fork into background */
186188
/* Will cause mosh-server not to correctly detach on old versions of sshd. */
187189
list<string> locale_vars;
188190

@@ -210,7 +212,7 @@ int main( int argc, char *argv[] )
210212
&& (strcmp( argv[ 1 ], "new" ) == 0) ) {
211213
/* new option syntax */
212214
int opt;
213-
while ( (opt = getopt( argc - 1, argv + 1, "@:i:p:c:svl:" )) != -1 ) {
215+
while ( (opt = getopt( argc - 1, argv + 1, "@:i:p:c:svl:D" )) != -1 ) {
214216
switch ( opt ) {
215217
/*
216218
* This undocumented option does nothing but eat its argument.
@@ -251,6 +253,9 @@ int main( int argc, char *argv[] )
251253
case 'l':
252254
locale_vars.push_back( string( optarg ) );
253255
break;
256+
case 'D':
257+
foreground = true;
258+
break;
254259
default:
255260
/* don't die on unknown options */
256261
print_usage( stderr, argv[ 0 ] );
@@ -364,7 +369,7 @@ int main( int argc, char *argv[] )
364369
}
365370

366371
try {
367-
return run_server( desired_ip, desired_port, command_path, command_argv, colors, verbose, with_motd );
372+
return run_server( desired_ip, desired_port, command_path, command_argv, colors, verbose, with_motd, foreground );
368373
} catch ( const Network::NetworkException &e ) {
369374
fprintf( stderr, "Network exception: %s\n",
370375
e.what() );
@@ -378,7 +383,8 @@ int main( int argc, char *argv[] )
378383

379384
static int run_server( const char *desired_ip, const char *desired_port,
380385
const string &command_path, char *command_argv[],
381-
const int colors, unsigned int verbose, bool with_motd ) {
386+
const int colors, unsigned int verbose, bool with_motd,
387+
bool foreground ) {
382388
/* get network idle timeout */
383389
long network_timeout = 0;
384390
char *timeout_envar = getenv( "MOSH_SERVER_NETWORK_TMOUT" );
@@ -448,34 +454,41 @@ static int run_server( const char *desired_ip, const char *desired_port,
448454
fatal_assert( 0 == sigaction( SIGHUP, &sa, NULL ) );
449455
fatal_assert( 0 == sigaction( SIGPIPE, &sa, NULL ) );
450456

457+
pid_t the_pid = -1;
458+
if (!foreground) {
459+
/* detach from terminal */
460+
fflush( NULL );
461+
pid_t the_pid = fork();
462+
}
451463

452-
/* detach from terminal */
453-
fflush( NULL );
454-
pid_t the_pid = fork();
455-
if ( the_pid < 0 ) {
464+
if ( the_pid < 0 && !foreground) {
456465
perror( "fork" );
457-
} else if ( the_pid > 0 ) {
466+
} else if ( the_pid > 0 || foreground ) {
458467
fputs( "\nmosh-server (" PACKAGE_STRING ") [build " BUILD_VERSION "]\n"
459-
"Copyright 2012 Keith Winstein <[email protected]>\n"
460-
"License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>.\n"
461-
"This is free software: you are free to change and redistribute it.\n"
462-
"There is NO WARRANTY, to the extent permitted by law.\n\n", stderr );
463-
464-
fprintf( stderr, "[mosh-server detached, pid = %d]\n", static_cast<int>(the_pid) );
465-
#ifndef HAVE_IUTF8
466-
fputs( "\nWarning: termios IUTF8 flag not defined.\n"
467-
"Character-erase of multibyte character sequence\n"
468-
"probably does not work properly on this platform.\n", stderr );
469-
#endif /* HAVE_IUTF8 */
468+
"Copyright 2012 Keith Winstein <[email protected]>\n"
469+
"License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>.\n"
470+
"This is free software: you are free to change and redistribute it.\n"
471+
"There is NO WARRANTY, to the extent permitted by law.\n\n", stderr );
470472

471-
fflush( NULL );
472-
if ( isatty( STDOUT_FILENO ) ) {
473-
tcdrain( STDOUT_FILENO );
473+
if (!foreground) {
474+
fprintf( stderr, "[mosh-server detached, pid = %d]\n", static_cast<int>(the_pid) );
474475
}
475-
if ( isatty( STDERR_FILENO ) ) {
476-
tcdrain( STDERR_FILENO );
476+
#idef HAVE_IUTF8
477+
fputs( "\nWarning: termios IUTF8 flag not defined.\n"
478+
"Character-erase of multibyte character sequence\n"
479+
"probably does not work properly on this platform.\n", stderr );
480+
#eif /* HAVE_IUTF8 */
481+
482+
if (!foreground) {
483+
fflush( NULL );
484+
if ( isatty( STDOUT_FILENO ) ) {
485+
tcdrain( STDOUT_FILENO );
486+
}
487+
if ( isatty( STDERR_FILENO ) ) {
488+
tcdrain( STDERR_FILENO );
489+
}
490+
exit( 0 );
477491
}
478-
exit( 0 );
479492
}
480493

481494
int master;

0 commit comments

Comments
 (0)