@@ -105,7 +105,8 @@ static void serve( int host_fd,
105
105
106
106
static int run_server ( const char *desired_ip, const char *desired_port,
107
107
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 );
109
110
110
111
111
112
static void print_version ( FILE *file )
@@ -119,7 +120,7 @@ static void print_version( FILE *file )
119
120
120
121
static void print_usage ( FILE *stream, const char *argv0 )
121
122
{
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 );
123
124
}
124
125
125
126
static bool print_motd ( const char *filename );
@@ -183,6 +184,7 @@ int main( int argc, char *argv[] )
183
184
char **command_argv = NULL ;
184
185
int colors = 0 ;
185
186
unsigned int verbose = 0 ; /* don't close stdin/stdout/stderr */
187
+ bool foreground = false ; /* stay in the foreground, don't fork into background */
186
188
/* Will cause mosh-server not to correctly detach on old versions of sshd. */
187
189
list<string> locale_vars;
188
190
@@ -210,7 +212,7 @@ int main( int argc, char *argv[] )
210
212
&& (strcmp ( argv[ 1 ], " new" ) == 0 ) ) {
211
213
/* new option syntax */
212
214
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 ) {
214
216
switch ( opt ) {
215
217
/*
216
218
* This undocumented option does nothing but eat its argument.
@@ -251,6 +253,9 @@ int main( int argc, char *argv[] )
251
253
case ' l' :
252
254
locale_vars.push_back ( string ( optarg ) );
253
255
break ;
256
+ case ' D' :
257
+ foreground = true ;
258
+ break ;
254
259
default :
255
260
/* don't die on unknown options */
256
261
print_usage ( stderr, argv[ 0 ] );
@@ -364,7 +369,7 @@ int main( int argc, char *argv[] )
364
369
}
365
370
366
371
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 );
368
373
} catch ( const Network::NetworkException &e ) {
369
374
fprintf ( stderr, " Network exception: %s\n " ,
370
375
e.what () );
@@ -378,7 +383,8 @@ int main( int argc, char *argv[] )
378
383
379
384
static int run_server ( const char *desired_ip, const char *desired_port,
380
385
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 ) {
382
388
/* get network idle timeout */
383
389
long network_timeout = 0 ;
384
390
char *timeout_envar = getenv ( " MOSH_SERVER_NETWORK_TMOUT" );
@@ -448,34 +454,41 @@ static int run_server( const char *desired_ip, const char *desired_port,
448
454
fatal_assert ( 0 == sigaction ( SIGHUP, &sa, NULL ) );
449
455
fatal_assert ( 0 == sigaction ( SIGPIPE, &sa, NULL ) );
450
456
457
+ pid_t the_pid = -1 ;
458
+ if (!foreground) {
459
+ /* detach from terminal */
460
+ fflush ( NULL );
461
+ pid_t the_pid = fork ();
462
+ }
451
463
452
- /* detach from terminal */
453
- fflush ( NULL );
454
- pid_t the_pid = fork ();
455
- if ( the_pid < 0 ) {
464
+ if ( the_pid < 0 && !foreground) {
456
465
perror ( " fork" );
457
- } else if ( the_pid > 0 ) {
466
+ } else if ( the_pid > 0 || foreground ) {
458
467
fputs ( " \n mosh-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 ( " \n Warning: 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 );
470
472
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) );
474
475
}
475
- if ( isatty ( STDERR_FILENO ) ) {
476
- tcdrain ( STDERR_FILENO );
476
+ #idef HAVE_IUTF8
477
+ fputs ( " \n Warning: 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 );
477
491
}
478
- exit ( 0 );
479
492
}
480
493
481
494
int master;
0 commit comments