added child reaper
[cloudstore.git] / rsync-piper.pl
index 12e1e79..767ab92 100755 (executable)
@@ -12,6 +12,7 @@ use English;
 
 use lib 'lib';
 use CloudStore::JSON;
+use CloudStore::dedup;
 
 my $dir   = $ENV{RSYNC_DIR}  || '/srv/cloudstore';
 my $port  = $ENV{RSYNC_PORT} || 6501;
@@ -26,8 +27,10 @@ $rsync = 'bin/rsync' if -x 'bin/rsync'; # use 3.1dev version!
 
 my @transfer = qw(
 timestamp:%t:timestamp
-login:%u:text
+login:%m:text
 port:$port:int
+auth:%u:text
+host:%h:text
 pid:%p:int
 perms:%B:text
 itemize:%i:text
@@ -53,6 +56,7 @@ if ( $ENV{SQL} ) {
 
 mkdir "$dir/var" if ! -e "$dir/var";
 
+unlink $log_fifo if -f $log_fifo;
 mkfifo $log_fifo, 0700 unless -p $log_fifo;
 
 my $rsync_config = qq{
@@ -88,18 +92,21 @@ foreach my $path ( glob "$users/*" ) {
        $login =~ s{^.+/([^/]+)$}{$1}; 
 
        if ( -d $path && -d "$path/blob" && -f "$path/secrets" ) {
-               print "INFO: user $login added\n";
+               my @secrets = map { chomp; $_ } read_file "$path/secrets";
+               my $auth_users = join(', ', map { s/:.+$//; $_ } @secrets );
 
                $rsync_config .= <<__RSYNC_MODULE__;
 
 [$login]
        path = $dir/users/$login/blob
-       auth users = $login
+       auth users = $auth_users
        secrets file = $dir/users/$login/secrets
        read only = false
 
 __RSYNC_MODULE__
 
+               print "INFO: added $login = $auth_users\n";
+
        } else {
                warn "skipped $login: $!";
        }
@@ -113,12 +120,37 @@ if ( -e $pid_file ) {
        my $pid = read_file $pid_file;
        chomp($pid);
        if ( kill 0, $pid ) {
-               warn "found rsync pid $pid";
+               warn "found rsync pid $pid\n";
+               kill 2, $pid;
+               while ( -e $pid_file ) {
+                       warn "waiting for rsync to die...\n";
+                       sleep 1;
+               }
+               
+               open(my $fifo, '<', $log_fifo);
+               while ( kill 0, $pid ) {
+                       my $line = <$fifo>;
+                       warn "<<< $line\n";
+               }
+
+               kill 0, $pid && die "can't kill it!";
        } else {
                unlink $pid_file;
        }
 }
 
+use POSIX ":sys_wait_h";
+sub REAPER {
+       my $child;
+       while ((my $waitedpid = waitpid(-1,WNOHANG)) > 0) {
+               warn "reaped $waitedpid" . ($? ? " with exit $?" : '');
+       }
+       $SIG{CHLD} = \&REAPER;  # loathe SysV
+}
+
+$SIG{CHLD} = \&REAPER;
+
+
 if ( ! -e $pid_file ) {
        my $exec = "$rsync --daemon --config $cfg_file --no-detach --port=$port";
        warn "START $exec\n";
@@ -135,20 +167,25 @@ if ( ! -e $pid_file ) {
        }
 }
 
+=for gearman
 use Gearman::Client;
 my $gearman = Gearman::Client->new;
 $gearman->job_servers('127.0.0.1:4730');
+=cut
 
 while(1) {
        warn "# reading log output from $log_fifo\n";
        open(my $fifo, '<', $log_fifo);
        while( my $line = <$fifo> ) {
+               die $line if $line =~ /rsync error:/;
                chomp $line;
                print $line, $/;
+
                if ( $line =~ /transfer-log:(.+)/ ) {
                        my $transfer = $1;
                        $transfer =~ s|(\d\d\d\d)/(\d\d)/(\d\d)[-\s](\d\d:\d\d:\d\d)|$1-$2-$3T$4|g;
                        my ( $yyyy,$mm,$dd,undef,$login,undef ) = split( /[\-T\|]/, $transfer, 6 );
+                       my $host = $1 if $login =~ s/\+(.+)//;
 
                        my $path = "users/$login/log";
                        mkdir $path unless -d $path;
@@ -159,16 +196,19 @@ while(1) {
                        print $log "$transfer\n";
                        close $log;
 
-
                        my @v = split(/\|/,$transfer,$#transfer + 1);
                        my %data;
                        @data{@transfer_names} = @v ; # FIXME validate?
 
                        print ">>> data ",dump( \%data ) if $ENV{DEBUG};
 
+                       CloudStore::dedup::data \%data; # uses deleted json files!
+
                        my $json = CloudStore::JSON::rsync_transfer \%data;
 
+=for gearman
                        $gearman->dispatch_background( 'rsync_transfer' => $json );
+=cut
 
                }
        }