Skip to content

Commit

Permalink
Fix empty line on login
Browse files Browse the repository at this point in the history
  • Loading branch information
ahedberg committed Feb 3, 2023
1 parent c16108f commit e51f81f
Showing 1 changed file with 31 additions and 7 deletions.
38 changes: 31 additions & 7 deletions src/frontend/mosh-server.cc
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,7 @@
typedef Network::Transport< Terminal::Complete, Network::UserStream > ServerConnection;

static void serve( int host_fd,
int pipe_fd,
Terminal::Complete &terminal,
ServerConnection &network,
long network_timeout,
Expand Down Expand Up @@ -508,6 +509,12 @@ static int run_server( const char *desired_ip, const char *desired_port,
snprintf( utmp_entry, 64, "mosh [%ld]", static_cast<long int>( getpid() ) );

/* Fork child process */
int pipes[2];
int success = pipe(pipes);
if (success == -1) {
perror( "pipe" );
exit( 1 );
}
pid_t child = forkpty( &master, NULL, NULL, &window_size );

if ( child == -1 ) {
Expand All @@ -517,6 +524,10 @@ static int run_server( const char *desired_ip, const char *desired_port,

if ( child == 0 ) {
/* child */
if ( close( pipes[1] ) < 0 ) {
perror( "child write pipe close" );
exit( 1 );
}

/* reenable signals */
struct sigaction sa;
Expand Down Expand Up @@ -592,8 +603,12 @@ static int run_server( const char *desired_ip, const char *desired_port,

/* Wait for parent to release us. */
char linebuf[81];
if (fgets(linebuf, sizeof linebuf, stdin) == NULL) {
err( 1, "parent signal" );
// -1, errno == EINTR -- retry
// otherwise, give up
while ( read( pipes[0], linebuf, sizeof( linebuf ) ) == -1 ) {
if ( errno != EINTR ) {
err( 1, "parent signal" );
}
}

Crypto::reenable_dumping_core();
Expand All @@ -603,8 +618,17 @@ static int run_server( const char *desired_ip, const char *desired_port,
sleep( 3 );
exit( 1 );
}

if ( close( pipes[0] ) < 0 ) {
perror( "child read pipe close" );
exit( 1 );
}
} else {
/* parent */
/* parent */
if ( close(pipes[0]) < 0 ) {
perror( "parent read pipe close" );
exit( 1 );
}

/* Drop unnecessary privileges */
#ifdef HAVE_PLEDGE
Expand All @@ -621,7 +645,7 @@ static int run_server( const char *desired_ip, const char *desired_port,
#endif

try {
serve( master, terminal, *network, network_timeout, network_signaled_timeout );
serve( master, pipes[1], terminal, *network, network_timeout, network_signaled_timeout );
} catch ( const Network::NetworkException &e ) {
fprintf( stderr, "Network exception: %s\n",
e.what() );
Expand All @@ -645,7 +669,7 @@ static int run_server( const char *desired_ip, const char *desired_port,
return 0;
}

static void serve( int host_fd, Terminal::Complete &terminal, ServerConnection &network, long network_timeout, long network_signaled_timeout )
static void serve( int host_fd, int pipe_fd, Terminal::Complete &terminal, ServerConnection &network, long network_timeout, long network_signaled_timeout )
{
/* scale timeouts */
const uint64_t network_timeout_ms = static_cast<uint64_t>( network_timeout ) * 1000;
Expand Down Expand Up @@ -821,8 +845,8 @@ static void serve( int host_fd, Terminal::Complete &terminal, ServerConnection &

/* Tell child to start login session. */
if ( !child_released ) {
if ( swrite( host_fd, "\n", 1 ) < 0) {
err( 1, "child release" );
if ( close( pipe_fd ) < 0 ) {
err( 1, "child release" );
}
child_released = true;
}
Expand Down

0 comments on commit e51f81f

Please sign in to comment.