obtain a new descriptor for the lock in kids, this should fix locking problems
[freeside.git] / fs_selfservice / FS-SelfService / freeside-selfservice-clientd
index 925bce6..ededfa6 100644 (file)
@@ -9,7 +9,7 @@ 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;
@@ -19,8 +19,10 @@ use IO::File;
 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
+$Debug = 2; #2 will turn on child logging
+            #3 will log packet contents,#including passwords
+            #4 will log receipts of all packets from server including
+            #  keepalives (big!)
 
 my $socket = "/usr/local/freeside/selfservice_socket$tag";
 my $pid_file = "$socket.pid";
@@ -41,6 +43,7 @@ $SIG{__WARN__} = \&_logmsg;
 
 warn "Creating $lock_file\n" if $Debug;
 open(LOCKFILE,">$lock_file") or die "can't open $lock_file: $!";
+close LOCKFILE;
 
 warn "Creating $socket\n" if $Debug;
 my $uaddr = sockaddr_un($socket);
@@ -65,6 +68,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;
@@ -101,10 +107,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 )
@@ -233,6 +245,7 @@ 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";
@@ -244,6 +257,9 @@ sub lock_write {
   #broken on freebsd?
   #flock(STDOUT, LOCK_EX) or die "FATAL: can't lock write stream: $!";
 
+  #open a new one for each kid to get a unique lock
+  open(LOCKFILE,">$lock_file") or die "can't open $lock_file: $!";
+
   flock(LOCKFILE, LOCK_EX) or die "FATAL: can't lock $lock_file: $!";
 }