chiark / gitweb /
git-cache-proxy: Use open-coded fork/exec for git gc
[chiark-utils.git] / scripts / git-cache-proxy
index 051888a9682bd49df717e155605d126562068395..ec5ff7f2e81aa9c4fd67069e537479e812f5f60c 100755 (executable)
@@ -145,6 +145,8 @@ sub fail ($) {
     exit 0;
 }
 
+$SIG{ALRM} = sub { fail "timeout" };
+
 sub gitfail ($) {
     my ($msg) = @_;
     close LOCK;
@@ -251,7 +253,6 @@ sub servinfo ($) {
 }
 
 sub readcommand () {
-    $SIG{ALRM} = sub { fail "timeout" };
     alarm 30;
 
     my $hex_len = xread 4;
@@ -320,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) {
@@ -433,12 +447,19 @@ sub housekeeping () {
            $mode_action = sub {
                my $gclog = "$subdir/gc.log";
                unlink $gclog or $!==ENOENT or hkfail "remove $gclog: $!";
-               my $r = system qw(sh -ec),
-                   'cd "$1"; shift; exec "$@"', 'x', "$subdir\\.git",
-                   qw(git gc --quiet);
-               if ($r) {
+               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 (status $r)";
"housekeeping: subdirs $subdir: gc failed (wait status $?)";
                } else {
                    update_gcstamp("$subdir\\.git");
                    logm 'debug',