Make utmp entries using utempter
This commit is contained in:
@@ -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 ) );
|
||||
|
||||
Reference in New Issue
Block a user