chiark / gitweb /
git-cache-proxy: Use open-coded fork/exec for git gc
[chiark-utils.git] / scripts / git-cache-proxy
index 4d55233a5f1a70d9cddd8efee5022e5ce3357a9c..ec5ff7f2e81aa9c4fd67069e537479e812f5f60c 100755 (executable)
@@ -2,6 +2,12 @@
 #
 # git caching proxy
 
+# Suitable only for exposing to semi-trusted clients: clients are not
+# supposed to be able to take over the server.  However, clients can
+# probably deny service to each other because the current
+# implementation is not very good at handling various out-of-course
+# situations (notably, clients which are too slow).
+
 # usage: run it on some port, and then clone or fetch
 #  "git://<realhost>:<realport>/<real-git-url>[ <options>]"
 # where <real-git-url> is http://<host>/... or git://<host>/...
@@ -13,6 +19,9 @@
 #    fetch=no             just use what is in the cache
 #    fetch=try            use what is in the cache if the fetch/clone fails
 #    timeout=<seconds>    length of time to allow for fetch/clone
+#    housekeeping-interval-days=<integer>  } housekeeping tuning parameters
+#    tree-expire-days=<integer>            }
+#    gc-interval-days=<integer>            }
 
 # example inetd.conf line:
 #  9419 stream tcp nowait git-cache /usr/bin/git-cache-proxy git-cache-proxy
@@ -23,7 +32,8 @@
 
 # git-cache-proxy
 # Copyright 2010 Tony Finch
-# Copyright 2013 Ian Jackson
+# Copyright 2013,2014 Ian Jackson
+# Copyright 2017 Citrix
 # 
 # git-cache-proxy is free software; you can redistribute it and/or
 # modify them under the terms of the GNU General Public License as
@@ -57,9 +67,11 @@ our $us = 'git-cache-proxy';
 
 our $debug = 0;
 our $housekeepingeverydays = 1;
+our $gcintervaldays = 10;
 our $treeexpiredays = 21;
-our $fetchtimeout = 1800;
-our $maxfetchtimeout = 3600;
+our $fetchtimeout = 3600;
+our $maxfetchtimeout = 7200;
+our $servetimeout = 3600;
 our $cachedir = '/var/cache/git-cache-proxy';
 our $housekeepingonly = 0;
 
@@ -133,6 +145,8 @@ sub fail ($) {
     exit 0;
 }
 
+$SIG{ALRM} = sub { fail "timeout" };
+
 sub gitfail ($) {
     my ($msg) = @_;
     close LOCK;
@@ -165,8 +179,10 @@ for (;;) {
            $cachedir = $1;
        } elsif (s/^--( max-fetch-timeout
                       | fetch-timeout
+                      | serve-timeout
                       | tree-expire-days
                       | housekeeping-interval-days
+                      | gc-interval-days
                       )=(\d+)$//x) {
            my $vn = $1;
            $vn =~ y/-//d;
@@ -237,7 +253,6 @@ sub servinfo ($) {
 }
 
 sub readcommand () {
-    $SIG{ALRM} = sub { fail "timeout" };
     alarm 30;
 
     my $hex_len = xread 4;
@@ -289,6 +304,13 @@ sub readcommand () {
     servinfo "locking";
 }
 
+sub update_gcstamp ($) {
+    my ($gitdir) = (@_);
+    my $gcdone = "$gitdir/cache-proxy-gc.stamp";
+    open GCSTAMP, '>', $gcdone or fail "create $gcdone: $!";
+    close GCSTAMP;
+}
+
 sub clonefetch () {
     lockfile \*LOCK, $lock, LOCK_EX;
 
@@ -299,6 +321,19 @@ sub clonefetch () {
 
     if ($fetch) {
 
+       my $rbits = '';
+       vec($rbits,0,1) = 1;
+       my $ebits = $rbits;
+       my $r=select $rbits,undef,$ebits,0;
+       $r>=0 or fail "select recheck STDOUT failed: $!";
+       if ($r) {
+           servinfo 'client disconnected (stdin unexpectedly'.
+               (vec($rbits,0,1) ? ' readable' : '').
+               (vec($ebits,0,1) ? ' exception' : '').
+               ')';
+           exit 0;
+       }
+
        our @cmd;
 
        if (!$exists) {
@@ -354,8 +389,10 @@ sub clonefetch () {
                servinfo "fetch/clone failed: $fetchfail";
            }
        }
+        alarm 0;
 
        if (!$exists) {
+           update_gcstamp($tmpd);
            rename $tmpd, $gitd or fail "rename fresh $tmpd to $gitd: $!";
            $exists = 1;
        }
@@ -370,10 +407,10 @@ sub clonefetch () {
     servinfo "sharing";
     lockfile \*LOCK, $lock, LOCK_SH; # NB releases and relocks
 
-    if (chdir $gitd) {
+    if (stat $gitd) {
        return 1;
     }
-    $!==ENOENT or fail "chdir $gitd: $!";
+    $!==ENOENT or fail "stat $gitd: $!";
 
     # Well, err, someone must have taken the lock in between
     # and garbage collected it.  How annoying.
@@ -386,36 +423,86 @@ sub housekeeping () {
     logm 'info', "housekeeping started";
     foreach $lock (<[a-z]*\\.lock>) {
        my $subdir = $lock;  $subdir =~ s/\\.lock$//;
+       my $gcdone = "$subdir\\.git/cache-proxy-gc.stamp";
        if (!lstat $lock) {
            $! == ENOENT or hkfail "$lock: lstat: $!";
            next;
        }
+       my ($mode_what,$mode_locknb,$mode_action);
        if (-M _ <= $treeexpiredays) {
-           logm 'debug', "housekeeping: subdirs $subdir: touched recently";
-           next;
+           if (!lstat "$gcdone") {
+               $! == ENOENT or hkfail "$gcdone: lstat: $!";
+               logm 'debug',
+ "housekeeping: subdirs $subdir: touched recently, never gc'd!";
+           } elsif (-M _ <= $gcintervaldays) {
+               logm 'debug',
+ "housekeeping: subdirs $subdir: touched recently, gc'd recently";
+               next;
+           } else {
+               logm 'debug',
+ "housekeeping: subdirs $subdir: touched recently, needs gc";
+           }
+           $mode_what = 'garbage collecting';
+           $mode_locknb = 0;
+           $mode_action = sub {
+               my $gclog = "$subdir/gc.log";
+               unlink $gclog or $!==ENOENT or hkfail "remove $gclog: $!";
+               my $child = fork // hkfail "fork (for $subdir): $!";
+               if (!$child) {
+                   if (!chdir "$subdir\\.git") {
+                       exit 0 if $!==ENOENT;
+                       die "for gc: chdir $subdir: $!\n";
+                   }
+                   exec qw(git gc --quiet);
+                   die "exec git gc (for $subdir): $!\n";
+               }
+               waitpid($child, 0) == $child or hkfail "waitpid failed! $!";
+               if ($?) {
+                   logm 'err',
+ "housekeeping: subdirs $subdir: gc failed (wait status $?)";
+               } else {
+                   update_gcstamp("$subdir\\.git");
+                   logm 'debug',
+                       "housekeeping: subdirs $subdir: gc done";
+               }
+           };
+       } else {
+           $mode_what = 'cleaning';
+           $mode_locknb = LOCK_NB;
+           $mode_action = sub {
+               eval {
+                   foreach my $suffix (qw(tmp git)) {
+                       my $dir = "${subdir}\\.$suffix";
+                       my $tdir = "${subdir}\\.tmp";
+                       if ($dir ne $tdir) {
+                           if (!rename $dir,$tdir) {
+                               next if $! == ENOENT;
+                               die "$dir: cannot rename to $tdir: $!\n";
+                           }
+                       }
+                       system qw(rm -rf --), $tdir;
+                       if (stat $tdir) {
+ die "$dir: problem deleting file(s), rm exited $?\n";
+                       } elsif ($! != ENOENT) {
+                           die "$tdir: cannot stat after deletion: $!\n";
+                       }
+                   }
+               };
+               if (length $@) {
+                   chomp $@;
+ logm 'warning', "housekeeping: $subdir: cleanup prevented: $@";
+               } else {
+                   unlink $lock or hkfail "remove $lock: $!";
+               }
+           };
        }
-       if (!lockfile \*LOCK, $lock, LOCK_EX|LOCK_NB) {
+       if (!lockfile \*LOCK, $lock, LOCK_EX|$mode_locknb) {
+            die $! unless $mode_locknb;
            logm 'info', "housekeeping: subdirs $subdir: lock busy, skipping";
            next;
        }
-       logm 'info', "housekeeping: subdirs $subdir: cleaning";
-       my $ok = 1;
-       foreach my $suffix (qw(tmp git)) {
-           my $dir = "${subdir}\\.$suffix";
-           my $errs;
-           remove_tree($dir, { safe=>1, error=>\$errs });
-           if (stat $dir) {
-               $ok = 0;
-               logm 'warning', "housekeeping: $dir: problems with".
-                   "deletion prevent cleanup:";
-               foreach my $err (@$errs) {
-                   logm 'info', "problem deleting: $err->[0]: $err->[1]";
-               }
-           }
-       }
-       if ($ok) {
-           unlink $lock or hkfail "remove $lock: $!";
-       }
+       logm 'info', "housekeeping: subdirs $subdir: $mode_what";
+       $mode_action->();
     }
     open HS, ">", "Housekeeping.stamp" or hkfail "touch Housekeeping.stamp: $!";
     close HS or hkfail "close Housekeeping.stamp: $!";
@@ -443,8 +530,10 @@ sub housekeepingcheck ($$) {
     }
     if ($dofork) {
        my $child = fork;
-       defined $child or hkfail "fork: $!";
+       defined $child or fail "fork: $!";
        if (!$child) {
+           open STDERR, "|logger -p daemon.warning -t '$us(housekeeping)'"
+               or die "fork: logger $!";
            housekeeping();
            exit 0;
        }
@@ -457,7 +546,10 @@ sub housekeepingcheck ($$) {
 
 sub runcommand () {
     servinfo "serving";
-    exec qw(git-upload-pack --strict --timeout=1000 .)
+
+    chdir $gitd or fail "chdir $gitd: $!";
+
+    exec qw(git-upload-pack --strict), "--timeout=$servetimeout", qw(.)
        or fail "exec git-upload-pack: $!";
 }