Make utmp entries using utempter
This commit is contained in:
@@ -1,7 +1,7 @@
|
||||
AM_CPPFLAGS = -I$(srcdir)/../statesync -I$(srcdir)/../terminal -I$(srcdir)/../network -I$(srcdir)/../crypto -I$(builddir)/../protobufs -I$(srcdir)/../util
|
||||
AM_CXXFLAGS = --std=c++0x -pedantic -Werror -Wall -Wextra -Weffc++ -fno-default-inline -pipe
|
||||
LIBS = $(protobuf_lite_LIBS)
|
||||
LDADD = ../crypto/libmoshcrypto.a ../network/libmoshnetwork.a ../statesync/libmoshstatesync.a ../terminal/libmoshterminal.a ../util/libmoshutil.a ../protobufs/libmoshprotos.a -lutil -lrt -lm
|
||||
LDADD = ../crypto/libmoshcrypto.a ../network/libmoshnetwork.a ../statesync/libmoshstatesync.a ../terminal/libmoshterminal.a ../util/libmoshutil.a ../protobufs/libmoshprotos.a -lutil -lrt -lm -lutempter
|
||||
|
||||
bin_PROGRAMS = mosh-client mosh-server
|
||||
|
||||
|
||||
@@ -31,6 +31,10 @@
|
||||
#include <typeinfo>
|
||||
#include <signal.h>
|
||||
#include <sys/signalfd.h>
|
||||
#include <utempter.h>
|
||||
#include <sys/socket.h>
|
||||
#include <netinet/in.h>
|
||||
#include <arpa/inet.h>
|
||||
|
||||
#include "completeterminal.h"
|
||||
#include "swrite.h"
|
||||
@@ -126,7 +130,7 @@ int main( int argc, char *argv[] )
|
||||
}
|
||||
|
||||
/* Fork child process */
|
||||
pid_t child = forkpty( &master, NULL, &child_termios, NULL );
|
||||
pid_t child = forkpty( &master, NULL, &child_termios, &window_size );
|
||||
|
||||
if ( child == -1 ) {
|
||||
perror( "forkpty" );
|
||||
@@ -178,6 +182,11 @@ int main( int argc, char *argv[] )
|
||||
exit( 0 );
|
||||
} else {
|
||||
/* parent */
|
||||
/* make utmp entry */
|
||||
char tmp[ 64 ];
|
||||
snprintf( tmp, 64, "mosh [%d]", getpid() );
|
||||
utempter_add_record( master, tmp );
|
||||
|
||||
try {
|
||||
serve( master, terminal, network );
|
||||
} catch ( Network::NetworkException e ) {
|
||||
@@ -192,6 +201,8 @@ int main( int argc, char *argv[] )
|
||||
perror( "close" );
|
||||
exit( 1 );
|
||||
}
|
||||
|
||||
utempter_remove_added_record();
|
||||
}
|
||||
|
||||
printf( "\n[mosh-server is exiting.]\n" );
|
||||
@@ -228,6 +239,10 @@ void serve( int host_fd, Terminal::Complete &terminal, ServerConnection &network
|
||||
|
||||
uint64_t last_remote_num = network.get_remote_state_num();
|
||||
|
||||
bool connected_utmp = false;
|
||||
struct in_addr saved_addr;
|
||||
saved_addr.s_addr = 0;
|
||||
|
||||
while ( 1 ) {
|
||||
try {
|
||||
int active_fds = poll( pollfds, 3, network.wait_time() );
|
||||
@@ -271,6 +286,20 @@ void serve( int host_fd, Terminal::Complete &terminal, ServerConnection &network
|
||||
if ( swrite( host_fd, terminal_to_host.c_str(), terminal_to_host.length() ) < 0 ) {
|
||||
break;
|
||||
}
|
||||
|
||||
/* update utmp entry if we have become "connected" */
|
||||
if ( (!connected_utmp)
|
||||
|| ( saved_addr.s_addr != network.get_remote_ip().s_addr ) ) {
|
||||
utempter_remove_added_record();
|
||||
|
||||
saved_addr = network.get_remote_ip();
|
||||
|
||||
char tmp[ 64 ];
|
||||
snprintf( tmp, 64, "%s via mosh [%d]", inet_ntoa( saved_addr ), getpid() );
|
||||
utempter_add_record( host_fd, tmp );
|
||||
|
||||
connected_utmp = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -350,6 +379,19 @@ void serve( int host_fd, Terminal::Complete &terminal, ServerConnection &network
|
||||
break;
|
||||
}
|
||||
|
||||
/* update utmp if has been more than 10 seconds since heard from client */
|
||||
if ( connected_utmp ) {
|
||||
if ( network.get_latest_remote_state().timestamp < Network::timestamp() - 10000 ) {
|
||||
utempter_remove_added_record();
|
||||
|
||||
char tmp[ 64 ];
|
||||
snprintf( tmp, 64, "mosh [%d]", getpid() );
|
||||
utempter_add_record( host_fd, tmp );
|
||||
|
||||
connected_utmp = false;
|
||||
}
|
||||
}
|
||||
|
||||
network.tick();
|
||||
} catch ( Network::NetworkException e ) {
|
||||
fprintf( stderr, "%s: %s\n", e.function.c_str(), strerror( e.the_errno ) );
|
||||
|
||||
@@ -117,6 +117,8 @@ namespace Network {
|
||||
|
||||
uint64_t timeout( void ) const;
|
||||
double get_SRTT( void ) const { return SRTT; }
|
||||
|
||||
const struct in_addr & get_remote_ip( void ) const { return remote_addr.sin_addr; }
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
@@ -101,6 +101,8 @@ namespace Network {
|
||||
uint64_t get_sent_state_late_acked( void ) const { return sent_state_late_acked; }
|
||||
|
||||
unsigned int send_interval( void ) const { return sender.send_interval(); }
|
||||
|
||||
const struct in_addr & get_remote_ip( void ) const { return connection.get_remote_ip(); }
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user