Use libstddjb's selfpipe instead of signalfd

Use selfpipe in our client, server, and examples.
This commit is contained in:
Quentin Smith
2012-02-26 20:50:17 -05:00
committed by Keith Winstein
parent 6d65fe9f79
commit 9a3ab83783
6 changed files with 79 additions and 93 deletions
+2
View File
@@ -18,6 +18,8 @@ parse_LDADD = ../terminal/libmoshterminal.a ../util/libmoshutil.a -lutil $(BOOST
termemu_SOURCES = termemu.cc termemu_SOURCES = termemu.cc
termemu_CPPFLAGS = -I$(srcdir)/../terminal -I$(srcdir)/../util -I$(srcdir)/../statesync -I$(builddir)/../protobufs termemu_CPPFLAGS = -I$(srcdir)/../terminal -I$(srcdir)/../util -I$(srcdir)/../statesync -I$(builddir)/../protobufs
termemu_LDADD = ../terminal/libmoshterminal.a ../util/libmoshutil.a ../statesync/libmoshstatesync.a ../protobufs/libmoshprotos.a -lutil $(BOOST_LDFLAGS) $(protobuf_LIBS) termemu_LDADD = ../terminal/libmoshterminal.a ../util/libmoshutil.a ../statesync/libmoshstatesync.a ../protobufs/libmoshprotos.a -lutil $(BOOST_LDFLAGS) $(protobuf_LIBS)
termemu_CPPFLAGS += $(STDDJB_CPPFLAGS)
termemu_LDADD += $(STDDJB_LDFLAGS)
ntester_SOURCES = ntester.cc ntester_SOURCES = ntester.cc
ntester_CPPFLAGS = -I$(srcdir)/../statesync -I$(srcdir)/../terminal -I$(srcdir)/../network -I$(srcdir)/../crypto -I$(builddir)/../protobufs $(BOOST_CPPFLAGS) ntester_CPPFLAGS = -I$(srcdir)/../statesync -I$(srcdir)/../terminal -I$(srcdir)/../network -I$(srcdir)/../crypto -I$(builddir)/../protobufs $(BOOST_CPPFLAGS)
+14 -16
View File
@@ -35,7 +35,6 @@
#include <sys/ioctl.h> #include <sys/ioctl.h>
#include <sys/stat.h> #include <sys/stat.h>
#include <fcntl.h> #include <fcntl.h>
#include <sys/signalfd.h>
#include <termios.h> #include <termios.h>
#include <sys/types.h> #include <sys/types.h>
#include <pwd.h> #include <pwd.h>
@@ -51,6 +50,10 @@
#include "completeterminal.h" #include "completeterminal.h"
#include "swrite.h" #include "swrite.h"
extern "C" {
#include "selfpipe.h"
}
const size_t buf_size = 16384; const size_t buf_size = 16384;
void emulate_terminal( int fd ); void emulate_terminal( int fd );
@@ -193,19 +196,14 @@ bool tick( Terminal::Framebuffer &state, const Terminal::Framebuffer &new_frame
void emulate_terminal( int fd ) void emulate_terminal( int fd )
{ {
/* establish WINCH fd and start listening for signal */ /* establish WINCH fd and start listening for signal */
sigset_t signal_mask; int signal_fd = selfpipe_init();
assert( sigemptyset( &signal_mask ) == 0 ); if ( signal_fd < 0 ) {
assert( sigaddset( &signal_mask, SIGWINCH ) == 0 ); perror( "selfpipe" );
/* stop "ignoring" WINCH signal */
assert( sigprocmask( SIG_BLOCK, &signal_mask, NULL ) == 0 );
int winch_fd = signalfd( -1, &signal_mask, 0 );
if ( winch_fd < 0 ) {
perror( "signalfd" );
return; return;
} }
assert( selfpipe_trap(SIGWINCH) == 0 );
/* get current window size */ /* get current window size */
struct winsize window_size; struct winsize window_size;
if ( ioctl( STDIN_FILENO, TIOCGWINSZ, &window_size ) < 0 ) { if ( ioctl( STDIN_FILENO, TIOCGWINSZ, &window_size ) < 0 ) {
@@ -231,7 +229,7 @@ void emulate_terminal( int fd )
pollfds[ 1 ].fd = fd; pollfds[ 1 ].fd = fd;
pollfds[ 1 ].events = POLLIN; pollfds[ 1 ].events = POLLIN;
pollfds[ 2 ].fd = winch_fd; pollfds[ 2 ].fd = signal_fd;
pollfds[ 2 ].events = POLLIN; pollfds[ 2 ].events = POLLIN;
swrite( STDOUT_FILENO, Terminal::Emulator::open().c_str() ); swrite( STDOUT_FILENO, Terminal::Emulator::open().c_str() );
@@ -240,7 +238,9 @@ void emulate_terminal( int fd )
while ( 1 ) { while ( 1 ) {
int active_fds = poll( pollfds, 3, poll_timeout ); int active_fds = poll( pollfds, 3, poll_timeout );
if ( active_fds < 0 ) { if ( active_fds < 0 && errno == EINTR ) {
continue;
} else if ( active_fds < 0 ) {
perror( "poll" ); perror( "poll" );
break; break;
} }
@@ -287,9 +287,7 @@ void emulate_terminal( int fd )
} }
} else if ( pollfds[ 2 ].revents & POLLIN ) { } else if ( pollfds[ 2 ].revents & POLLIN ) {
/* resize */ /* resize */
struct signalfd_siginfo info; assert( selfpipe_read() == SIGWINCH );
assert( read( winch_fd, &info, sizeof( info ) ) == sizeof( info ) );
assert( info.ssi_signo == SIGWINCH );
/* get new size */ /* get new size */
if ( ioctl( STDIN_FILENO, TIOCGWINSZ, &window_size ) < 0 ) { if ( ioctl( STDIN_FILENO, TIOCGWINSZ, &window_size ) < 0 ) {
+2
View File
@@ -1,6 +1,8 @@
AM_CPPFLAGS = -I$(srcdir)/../statesync -I$(srcdir)/../terminal -I$(srcdir)/../network -I$(srcdir)/../crypto -I$(builddir)/../protobufs -I$(srcdir)/../util $(BOOST_CPPFLAGS) $(protobuf_CFLAGS) AM_CPPFLAGS = -I$(srcdir)/../statesync -I$(srcdir)/../terminal -I$(srcdir)/../network -I$(srcdir)/../crypto -I$(builddir)/../protobufs -I$(srcdir)/../util $(BOOST_CPPFLAGS) $(protobuf_CFLAGS)
AM_CXXFLAGS = $(WARNING_CXXFLAGS) $(PICKY_CXXFLAGS) -fno-default-inline -pipe AM_CXXFLAGS = $(WARNING_CXXFLAGS) $(PICKY_CXXFLAGS) -fno-default-inline -pipe
LDADD = ../crypto/libmoshcrypto.a ../network/libmoshnetwork.a ../statesync/libmoshstatesync.a ../terminal/libmoshterminal.a ../util/libmoshutil.a ../protobufs/libmoshprotos.a -lutil -lm $(protobuf_LIBS) LDADD = ../crypto/libmoshcrypto.a ../network/libmoshnetwork.a ../statesync/libmoshstatesync.a ../terminal/libmoshterminal.a ../util/libmoshutil.a ../protobufs/libmoshprotos.a -lutil -lm $(protobuf_LIBS)
AM_CPPFLAGS += $(STDDJB_CPPFLAGS)
LDADD += $(STDDJB_LDFLAGS)
bin_PROGRAMS = mosh-client mosh-server bin_PROGRAMS = mosh-client mosh-server
+25 -18
View File
@@ -18,13 +18,13 @@
#include "config.h" #include "config.h"
#include <errno.h>
#include <locale.h> #include <locale.h>
#include <string.h> #include <string.h>
#include <langinfo.h> #include <langinfo.h>
#include <termios.h> #include <termios.h>
#include <unistd.h> #include <unistd.h>
#include <stdio.h> #include <stdio.h>
#include <pty.h>
#include <stdlib.h> #include <stdlib.h>
#include <poll.h> #include <poll.h>
#include <sys/ioctl.h> #include <sys/ioctl.h>
@@ -32,7 +32,6 @@
#include <pwd.h> #include <pwd.h>
#include <typeinfo> #include <typeinfo>
#include <signal.h> #include <signal.h>
#include <sys/signalfd.h>
#ifdef HAVE_UTEMPTER #ifdef HAVE_UTEMPTER
#include <utempter.h> #include <utempter.h>
#endif #endif
@@ -40,10 +39,20 @@
#include <netinet/in.h> #include <netinet/in.h>
#include <arpa/inet.h> #include <arpa/inet.h>
extern "C" {
#include "selfpipe.h"
}
#include "completeterminal.h" #include "completeterminal.h"
#include "swrite.h" #include "swrite.h"
#include "user.h" #include "user.h"
#if HAVE_PTY_H
#include <pty.h>
#elif HAVE_UTIL_H
#include <util.h>
#endif
#include "networktransport.cc" #include "networktransport.cc"
typedef Network::Transport< Terminal::Complete, Network::UserStream > ServerConnection; typedef Network::Transport< Terminal::Complete, Network::UserStream > ServerConnection;
@@ -222,18 +231,15 @@ int main( int argc, char *argv[] )
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 */ /* establish fd for shutdown signals */
sigset_t signal_mask; int signal_fd = selfpipe_init();
if ( signal_fd < 0 ) {
assert( sigemptyset( &signal_mask ) == 0 ); perror( "selfpipe" );
assert( sigaddset( &signal_mask, SIGTERM ) == 0 );
assert( sigaddset( &signal_mask, SIGINT ) == 0 );
int shutdown_signal_fd = signalfd( -1, &signal_mask, 0 );
if ( shutdown_signal_fd < 0 ) {
perror( "signalfd" );
return; return;
} }
assert( selfpipe_trap( SIGTERM ) == 0 );
assert( selfpipe_trap( SIGINT ) == 0 );
/* prepare to poll for events */ /* prepare to poll for events */
struct pollfd pollfds[ 3 ]; struct pollfd pollfds[ 3 ];
@@ -243,7 +249,7 @@ void serve( int host_fd, Terminal::Complete &terminal, ServerConnection &network
pollfds[ 1 ].fd = host_fd; pollfds[ 1 ].fd = host_fd;
pollfds[ 1 ].events = POLLIN; pollfds[ 1 ].events = POLLIN;
pollfds[ 2 ].fd = shutdown_signal_fd; pollfds[ 2 ].fd = signal_fd;
pollfds[ 2 ].events = POLLIN; pollfds[ 2 ].events = POLLIN;
uint64_t last_remote_num = network.get_remote_state_num(); uint64_t last_remote_num = network.get_remote_state_num();
@@ -266,7 +272,9 @@ void serve( int host_fd, Terminal::Complete &terminal, ServerConnection &network
} }
int active_fds = poll( pollfds, 3, poll_timeout ); int active_fds = poll( pollfds, 3, poll_timeout );
if ( active_fds < 0 ) { if ( active_fds < 0 && errno == EINTR ) {
continue;
} else if ( active_fds < 0 ) {
perror( "poll" ); perror( "poll" );
break; break;
} }
@@ -364,12 +372,11 @@ void serve( int host_fd, Terminal::Complete &terminal, ServerConnection &network
if ( pollfds[ 2 ].revents & POLLIN ) { if ( pollfds[ 2 ].revents & POLLIN ) {
/* shutdown signal */ /* shutdown signal */
struct signalfd_siginfo the_siginfo; int signo = selfpipe_read();
ssize_t bytes_read = read( pollfds[ 2 ].fd, &the_siginfo, sizeof( the_siginfo ) ); if ( signo == 0 ) {
if ( bytes_read == 0 ) {
break; break;
} else if ( bytes_read < 0 ) { } else if ( signo < 0 ) {
perror( "read" ); perror( "selfpipe_read" );
break; break;
} }
+34 -57
View File
@@ -18,6 +18,7 @@
#include "config.h" #include "config.h"
#include <errno.h>
#include <locale.h> #include <locale.h>
#include <string.h> #include <string.h>
#include <langinfo.h> #include <langinfo.h>
@@ -29,7 +30,6 @@
#include <sys/types.h> #include <sys/types.h>
#include <pwd.h> #include <pwd.h>
#include <signal.h> #include <signal.h>
#include <sys/signalfd.h>
#include <time.h> #include <time.h>
#if HAVE_PTY_H #if HAVE_PTY_H
@@ -38,6 +38,10 @@
#include <util.h> #include <util.h>
#endif #endif
extern "C" {
#include "selfpipe.h"
}
#include "stmclient.h" #include "stmclient.h"
#include "swrite.h" #include "swrite.h"
#include "completeterminal.h" #include "completeterminal.h"
@@ -101,37 +105,21 @@ void STMClient::shutdown( void )
void STMClient::main_init( void ) void STMClient::main_init( void )
{ {
/* establish WINCH fd and start listening for signal */ /* establish WINCH fd and start listening for signal */
sigset_t signal_mask; signal_fd = selfpipe_init();
assert( sigemptyset( &signal_mask ) == 0 ); if ( signal_fd < 0 ) {
assert( sigaddset( &signal_mask, SIGWINCH ) == 0 ); perror( "selfpipe" );
/* stop "ignoring" WINCH signal */
assert( sigprocmask( SIG_BLOCK, &signal_mask, NULL ) == 0 );
winch_fd = signalfd( -1, &signal_mask, 0 );
if ( winch_fd < 0 ) {
perror( "signalfd" );
return; return;
} }
assert( selfpipe_trap(SIGWINCH) == 0 );
/* establish fd for shutdown signals */ /* establish fd for shutdown signals */
assert( sigemptyset( &signal_mask ) == 0 ); assert( selfpipe_trap( SIGTERM ) == 0 );
assert( sigaddset( &signal_mask, SIGTERM ) == 0 ); assert( selfpipe_trap( SIGINT ) == 0 );
assert( sigaddset( &signal_mask, SIGINT ) == 0 ); assert( selfpipe_trap( SIGHUP ) == 0 );
assert( sigaddset( &signal_mask, SIGHUP ) == 0 ); assert( selfpipe_trap( SIGPIPE ) == 0 );
assert( sigaddset( &signal_mask, SIGPIPE ) == 0 ); assert( selfpipe_trap( SIGTSTP ) == 0 );
assert( sigaddset( &signal_mask, SIGTSTP ) == 0 ); assert( selfpipe_trap( SIGCONT ) == 0 );
assert( sigaddset( &signal_mask, SIGSTOP ) == 0 );
assert( sigaddset( &signal_mask, SIGCONT ) == 0 );
/* don't let signals kill us */
assert( sigprocmask( SIG_BLOCK, &signal_mask, NULL ) == 0 );
shutdown_signal_fd = signalfd( -1, &signal_mask, 0 );
if ( shutdown_signal_fd < 0 ) {
perror( "signalfd" );
return;
}
/* get initial window size */ /* get initial window size */
if ( ioctl( STDIN_FILENO, TIOCGWINSZ, &window_size ) < 0 ) { if ( ioctl( STDIN_FILENO, TIOCGWINSZ, &window_size ) < 0 ) {
@@ -256,10 +244,6 @@ bool STMClient::process_user_input( int fd )
bool STMClient::process_resize( void ) bool STMClient::process_resize( void )
{ {
struct signalfd_siginfo info;
assert( read( winch_fd, &info, sizeof( info ) ) == sizeof( info ) );
assert( info.ssi_signo == SIGWINCH );
/* get new size */ /* get new size */
if ( ioctl( STDIN_FILENO, TIOCGWINSZ, &window_size ) < 0 ) { if ( ioctl( STDIN_FILENO, TIOCGWINSZ, &window_size ) < 0 ) {
perror( "ioctl TIOCGWINSZ" ); perror( "ioctl TIOCGWINSZ" );
@@ -287,7 +271,7 @@ void STMClient::main( void )
main_init(); main_init();
/* prepare to poll for events */ /* prepare to poll for events */
struct pollfd pollfds[ 4 ]; struct pollfd pollfds[ 3 ];
pollfds[ 0 ].fd = network->fd(); pollfds[ 0 ].fd = network->fd();
pollfds[ 0 ].events = POLLIN; pollfds[ 0 ].events = POLLIN;
@@ -295,18 +279,17 @@ void STMClient::main( void )
pollfds[ 1 ].fd = STDIN_FILENO; pollfds[ 1 ].fd = STDIN_FILENO;
pollfds[ 1 ].events = POLLIN; pollfds[ 1 ].events = POLLIN;
pollfds[ 2 ].fd = winch_fd; pollfds[ 2 ].fd = signal_fd;
pollfds[ 2 ].events = POLLIN; pollfds[ 2 ].events = POLLIN;
pollfds[ 3 ].fd = shutdown_signal_fd;
pollfds[ 3 ].events = POLLIN;
while ( 1 ) { while ( 1 ) {
try { try {
output_new_frame(); output_new_frame();
int active_fds = poll( pollfds, 4, min( network->wait_time(), overlays.wait_time() ) ); int active_fds = poll( pollfds, 3, min( network->wait_time(), overlays.wait_time() ) );
if ( active_fds < 0 ) { if ( active_fds < 0 && errno == EINTR ) {
continue;
} else if ( active_fds < 0 ) {
perror( "poll" ); perror( "poll" );
break; break;
} }
@@ -329,26 +312,20 @@ void STMClient::main( void )
} }
if ( pollfds[ 2 ].revents & POLLIN ) { if ( pollfds[ 2 ].revents & POLLIN ) {
/* resize */ int signo = selfpipe_read();
if ( !process_resize() ) { return; }
}
if ( pollfds[ 3 ].revents & POLLIN ) { if ( signo == SIGWINCH ) {
/* shutdown signal */ /* resize */
struct signalfd_siginfo the_siginfo; if ( !process_resize() ) { return; }
ssize_t bytes_read = read( pollfds[ 3 ].fd, &the_siginfo, sizeof( the_siginfo ) ); } else if ( signo > 0 ) {
if ( bytes_read == 0 ) { /* shutdown signal */
break;
} else if ( bytes_read < 0 ) {
perror( "read" );
break;
}
if ( !network->has_remote_addr() ) { if ( !network->has_remote_addr() ) {
break; break;
} else if ( !network->shutdown_in_progress() ) { } else if ( !network->shutdown_in_progress() ) {
overlays.get_notification_engine().set_notification_string( wstring( L"Signal received, shutting down..." ) ); overlays.get_notification_engine().set_notification_string( wstring( L"Signal received, shutting down..." ) );
network->start_shutdown(); network->start_shutdown();
}
} }
} }
+2 -2
View File
@@ -36,7 +36,7 @@ private:
struct termios saved_termios, raw_termios; struct termios saved_termios, raw_termios;
int winch_fd, shutdown_signal_fd; int signal_fd;
struct winsize window_size; struct winsize window_size;
Terminal::Framebuffer *local_framebuffer; Terminal::Framebuffer *local_framebuffer;
@@ -56,7 +56,7 @@ 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(),
winch_fd(), shutdown_signal_fd(), signal_fd(),
window_size(), window_size(),
local_framebuffer( NULL ), local_framebuffer( NULL ),
overlays(), overlays(),