self-service keepalives
[freeside.git] / fs_selfservice / FS-SelfService / freeside-selfservice-clientd
index 319d425..95e9b9b 100644 (file)
@@ -5,25 +5,30 @@
 # This is run REMOTELY over ssh by freeside-selfservice-server
 
 use strict;
-use subs qw(spawn logmsg);
+use subs qw(spawn logmsg lock_write unlock_write);
 use Fcntl qw(:flock);
 use POSIX qw(:sys_wait_h);
 use Socket;
-use Storable qw(nstore_fd fd_retrieve);
+use Storable 2.09 qw(nstore_fd fd_retrieve);
 use IO::Handle qw(_IONBF);
 use IO::Select;
 use IO::File;
 
-STDOUT->setbuf('');
+#STDOUT->setbuf('');
+
+my $tag = scalar(@ARGV) ? '.'.shift : '';
 
 use vars qw( $Debug );
 $Debug = 2; #2 will turn on child logging, 3 will log packet contents,
-            #including potentially compromising information
+            #including potentially compromising information, 4 will log
+            #receipts of all packets from server including keepalives (big!)
 
-my $socket = "/usr/local/freeside/selfservice_socket";
+my $socket = "/usr/local/freeside/selfservice_socket$tag";
 my $pid_file = "$socket.pid";
 
-my $log_file = "/usr/local/freeside/selfservice.log";
+my $log_file = "/usr/local/freeside/selfservice$tag.log";
+
+my $lock_file = "/usr/local/freeside/selfservice$tag.writelock";
 
 #my $me = '[client]';
 
@@ -35,6 +40,9 @@ $SIG{__WARN__} = \&_logmsg;
 #warn "$me Reading init data\n" if $Debug;
 #my $signup_init = 
 
+warn "Creating $lock_file\n" if $Debug;
+open(LOCKFILE,">$lock_file") or die "can't open $lock_file: $!";
+
 warn "Creating $socket\n" if $Debug;
 my $uaddr = sockaddr_un($socket);
 my $proto = getprotobyname('tcp');
@@ -58,6 +66,9 @@ close PIDFILE;
 #sub REAPER { $waitedpid = wait; $SIG{CHLD} = \&REAPER; }
 #$SIG{CHLD} =  \&REAPER;
 
+warn "enabling keep alives\n" if $Debug;
+nstore_fd( { _packet => '_enable_keepalive' } , \*STDOUT );
+
 warn "entering main loop\n" if $Debug;
 
 my %kids;
@@ -94,10 +105,16 @@ while (1) {
 
     if ( $handle == \*STDIN ) {
 
-      warn "receiving packet from server\n" if $Debug;
+      warn "receiving packet from server\n" if $Debug > 3;
 
       my $packet = fd_retrieve(\*STDIN);
       my $token = $packet->{'_token'};
+
+      if ( $token eq '_keepalive' ) {
+        $undisp = 1;
+        next;
+      }
+
       warn "received packet from server with token $token\n".
            ( $Debug > 2
              ? join('', map { " $_=>$packet->{$_}\n" } keys %$packet )
@@ -138,14 +155,22 @@ while (1) {
         #handle some commands weirdly?
         $packet->{_token}=$$;
 
-        warn "[child-$$] sending packet to remote server" if $Debug > 1;
-        flock(STDOUT, LOCK_EX) or die "FATAL: can't lock write stream: $!";
+        warn "[child-$$] locking write stream\n" if $Debug > 1;
+        lock_write;
+
+        warn "[child-$$] sending packet to remote server\n" if $Debug > 1;
         nstore_fd($packet, \*STDOUT) or die "FATAL: can't send response: $!";
+        
+        warn "[child-$$] flushing write stream\n" if $Debug > 1;
         STDOUT->flush or die "FATAL: can't flush: $!";
-        flock(STDOUT, LOCK_UN) or die "FATAL: can't release write lock: $!";
+        
+        warn "[child-$$] releasing write lock\n" if $Debug > 1;
+        unlock_write;
+
+        warn "[child-$$] closing write stream\n" if $Debug > 1;
         close STDOUT or die "FATAL: can't close write stream: $!"; #??!
 
-        warn "[child-$$] waiting for response from parent" if $Debug > 1;
+        warn "[child-$$] waiting for response from parent\n" if $Debug > 1;
         my $w = new IO::Select;
         $w->add(\*STDIN);
         until ( $w->can_read ) {
@@ -218,9 +243,24 @@ sub spawn {
 sub _logmsg {
   chomp( my $msg = shift );
   my $log = new IO::File ">>$log_file";
+  die "can't open $log_file: $!" unless defined($log);
   flock($log, LOCK_EX);
   seek($log, 0, 2);
   print $log "[client] [". scalar(localtime). "] [$$] $msg\n";
   flock($log, LOCK_UN);
   close $log;
 }
+
+sub lock_write {
+  #broken on freebsd?
+  #flock(STDOUT, LOCK_EX) or die "FATAL: can't lock write stream: $!";
+
+  flock(LOCKFILE, LOCK_EX) or die "FATAL: can't lock $lock_file: $!";
+}
+
+sub unlock_write {
+  #broken on freebsd?
+  #flock(STDOUT, LOCK_UN) or die "FATAL: can't release write lock: $!";
+
+  flock(LOCKFILE, LOCK_UN) or die "FATAL: can't unlock $lock_file: $!";
+}