Use Select signal handling in mosh-{client,server}
This commit is contained in:
committed by
Keith Winstein
parent
1d0b16881c
commit
4f23326a92
@@ -44,7 +44,6 @@
|
|||||||
#include <paths.h>
|
#include <paths.h>
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
#include "sigfd.h"
|
|
||||||
#include "completeterminal.h"
|
#include "completeterminal.h"
|
||||||
#include "swrite.h"
|
#include "swrite.h"
|
||||||
#include "user.h"
|
#include "user.h"
|
||||||
@@ -471,21 +470,12 @@ int run_server( const char *desired_ip, const char *desired_port,
|
|||||||
|
|
||||||
void serve( int host_fd, Terminal::Complete &terminal, ServerConnection &network )
|
void serve( int host_fd, Terminal::Complete &terminal, ServerConnection &network )
|
||||||
{
|
{
|
||||||
/* establish fd for shutdown signals */
|
|
||||||
int signal_fd = sigfd_init();
|
|
||||||
if ( signal_fd < 0 ) {
|
|
||||||
perror( "sigfd_init" );
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
fatal_assert( sigfd_trap( SIGTERM ) == 0 );
|
|
||||||
fatal_assert( sigfd_trap( SIGINT ) == 0 );
|
|
||||||
|
|
||||||
/* prepare to poll for events */
|
/* prepare to poll for events */
|
||||||
Select &sel = Select::get_instance();
|
Select &sel = Select::get_instance();
|
||||||
sel.add_fd( network.fd() );
|
sel.add_fd( network.fd() );
|
||||||
sel.add_fd( host_fd );
|
sel.add_fd( host_fd );
|
||||||
sel.add_fd( signal_fd );
|
sel.add_signal( SIGTERM );
|
||||||
|
sel.add_signal( SIGINT );
|
||||||
|
|
||||||
uint64_t last_remote_num = network.get_remote_state_num();
|
uint64_t last_remote_num = network.get_remote_state_num();
|
||||||
|
|
||||||
@@ -507,9 +497,7 @@ void serve( int host_fd, Terminal::Complete &terminal, ServerConnection &network
|
|||||||
}
|
}
|
||||||
|
|
||||||
int active_fds = sel.select( timeout );
|
int active_fds = sel.select( timeout );
|
||||||
if ( active_fds < 0 && errno == EINTR ) {
|
if ( active_fds < 0 ) {
|
||||||
continue;
|
|
||||||
} else if ( active_fds < 0 ) {
|
|
||||||
perror( "select" );
|
perror( "select" );
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
@@ -614,16 +602,8 @@ void serve( int host_fd, Terminal::Complete &terminal, ServerConnection &network
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if ( sel.read( signal_fd ) ) {
|
if ( sel.any_signal() ) {
|
||||||
/* shutdown signal */
|
/* shutdown signal */
|
||||||
int signo = sigfd_read();
|
|
||||||
if ( signo == 0 ) {
|
|
||||||
break;
|
|
||||||
} else if ( signo < 0 ) {
|
|
||||||
perror( "sigfd_read" );
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
|
|
||||||
if ( network.has_remote_addr() && (!network.shutdown_in_progress()) ) {
|
if ( network.has_remote_addr() && (!network.shutdown_in_progress()) ) {
|
||||||
network.start_shutdown();
|
network.start_shutdown();
|
||||||
} else {
|
} else {
|
||||||
|
|||||||
+18
-26
@@ -36,7 +36,6 @@
|
|||||||
#include <util.h>
|
#include <util.h>
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
#include "sigfd.h"
|
|
||||||
#include "stmclient.h"
|
#include "stmclient.h"
|
||||||
#include "swrite.h"
|
#include "swrite.h"
|
||||||
#include "completeterminal.h"
|
#include "completeterminal.h"
|
||||||
@@ -125,20 +124,14 @@ void STMClient::shutdown( void )
|
|||||||
|
|
||||||
void STMClient::main_init( void )
|
void STMClient::main_init( void )
|
||||||
{
|
{
|
||||||
/* establish a fd for signals */
|
Select &sel = Select::get_instance();
|
||||||
signal_fd = sigfd_init();
|
sel.add_signal( SIGWINCH );
|
||||||
if ( signal_fd < 0 ) {
|
sel.add_signal( SIGTERM );
|
||||||
perror( "sigfd_init" );
|
sel.add_signal( SIGINT );
|
||||||
return;
|
sel.add_signal( SIGHUP );
|
||||||
}
|
sel.add_signal( SIGPIPE );
|
||||||
|
sel.add_signal( SIGTSTP );
|
||||||
fatal_assert( sigfd_trap( SIGWINCH ) == 0 );
|
sel.add_signal( SIGCONT );
|
||||||
fatal_assert( sigfd_trap( SIGTERM ) == 0 );
|
|
||||||
fatal_assert( sigfd_trap( SIGINT ) == 0 );
|
|
||||||
fatal_assert( sigfd_trap( SIGHUP ) == 0 );
|
|
||||||
fatal_assert( sigfd_trap( SIGPIPE ) == 0 );
|
|
||||||
fatal_assert( sigfd_trap( SIGTSTP ) == 0 );
|
|
||||||
fatal_assert( sigfd_trap( SIGCONT ) == 0 );
|
|
||||||
|
|
||||||
/* get initial window size */
|
/* get initial window size */
|
||||||
if ( ioctl( STDIN_FILENO, TIOCGWINSZ, &window_size ) < 0 ) {
|
if ( ioctl( STDIN_FILENO, TIOCGWINSZ, &window_size ) < 0 ) {
|
||||||
@@ -302,7 +295,6 @@ void STMClient::main( void )
|
|||||||
Select &sel = Select::get_instance();
|
Select &sel = Select::get_instance();
|
||||||
sel.add_fd( network->fd() );
|
sel.add_fd( network->fd() );
|
||||||
sel.add_fd( STDIN_FILENO );
|
sel.add_fd( STDIN_FILENO );
|
||||||
sel.add_fd( signal_fd );
|
|
||||||
|
|
||||||
while ( 1 ) {
|
while ( 1 ) {
|
||||||
try {
|
try {
|
||||||
@@ -316,9 +308,7 @@ void STMClient::main( void )
|
|||||||
}
|
}
|
||||||
|
|
||||||
int active_fds = sel.select( wait_time );
|
int active_fds = sel.select( wait_time );
|
||||||
if ( active_fds < 0 && errno == EINTR ) {
|
if ( active_fds < 0 ) {
|
||||||
continue;
|
|
||||||
} else if ( active_fds < 0 ) {
|
|
||||||
perror( "select" );
|
perror( "select" );
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
@@ -340,15 +330,18 @@ void STMClient::main( void )
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if ( sel.read( signal_fd ) ) {
|
if ( sel.signal( SIGWINCH ) ) {
|
||||||
int signo = sigfd_read();
|
|
||||||
|
|
||||||
if ( signo == SIGWINCH ) {
|
|
||||||
/* resize */
|
/* resize */
|
||||||
if ( !process_resize() ) { return; }
|
if ( !process_resize() ) { return; }
|
||||||
} else if ( signo > 0 ) {
|
}
|
||||||
/* shutdown signal */
|
|
||||||
|
|
||||||
|
if ( sel.signal( SIGTERM )
|
||||||
|
|| sel.signal( SIGINT )
|
||||||
|
|| sel.signal( SIGHUP )
|
||||||
|
|| sel.signal( SIGPIPE )
|
||||||
|
|| sel.signal( SIGTSTP )
|
||||||
|
|| sel.signal( SIGCONT ) ) {
|
||||||
|
/* shutdown signal */
|
||||||
if ( !network->has_remote_addr() ) {
|
if ( !network->has_remote_addr() ) {
|
||||||
break;
|
break;
|
||||||
} else if ( !network->shutdown_in_progress() ) {
|
} else if ( !network->shutdown_in_progress() ) {
|
||||||
@@ -356,7 +349,6 @@ void STMClient::main( void )
|
|||||||
network->start_shutdown();
|
network->start_shutdown();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
if ( sel.error( network->fd() ) ) {
|
if ( sel.error( network->fd() ) ) {
|
||||||
/* network problem */
|
/* network problem */
|
||||||
|
|||||||
@@ -36,7 +36,6 @@ private:
|
|||||||
|
|
||||||
struct termios saved_termios, raw_termios;
|
struct termios saved_termios, raw_termios;
|
||||||
|
|
||||||
int signal_fd;
|
|
||||||
struct winsize window_size;
|
struct winsize window_size;
|
||||||
|
|
||||||
Terminal::Framebuffer *local_framebuffer, *new_state;
|
Terminal::Framebuffer *local_framebuffer, *new_state;
|
||||||
@@ -65,7 +64,6 @@ public:
|
|||||||
STMClient( const char *s_ip, int s_port, const char *s_key, const char *predict_mode )
|
STMClient( const char *s_ip, int s_port, const char *s_key, const char *predict_mode )
|
||||||
: ip( s_ip ), port( s_port ), key( s_key ),
|
: ip( s_ip ), port( s_port ), key( s_key ),
|
||||||
saved_termios(), raw_termios(),
|
saved_termios(), raw_termios(),
|
||||||
signal_fd(),
|
|
||||||
window_size(),
|
window_size(),
|
||||||
local_framebuffer( NULL ),
|
local_framebuffer( NULL ),
|
||||||
new_state( NULL ),
|
new_state( NULL ),
|
||||||
|
|||||||
Reference in New Issue
Block a user