chiark / gitweb /
git-cache-proxy: more wip, copyright notice, before housekeeping etc.
[chiark-utils.git] / scripts / git-cache-proxy
index b3cf41293082d40077ce58b69336ad7e3c819ed2..df522a0126238d40d97a0c342ea7e22910269bd5 100755 (executable)
@@ -1,7 +1,7 @@
 #!/usr/bin/perl -w
 #
 # git caching proxy
-#
+
 # 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>/...
 #    fetch=try            use what is in the cache if the fetch/clone fails
 #    timeout=<seconds>    length of time to allow for fetch/clone
 
+# git-cache-proxy is free software; you can redistribute it and/or
+# modify them under the terms of the GNU General Public License as
+# published by the Free Software Foundation; either version 3, or (at
+# your option) any later version.
+# 
+# git-cache-proxy is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+# General Public License for more details.
+# 
+# You should have received a copy of the GNU General Public License along
+# with this program; if not, consult the Free Software Foundation's
+# website at www.fsf.org, or the GNU Project website at www.gnu.org.
+# 
+# (Some code taken from userv-utils's git-daemon.in and git-service.in
+# which were written by Tony Finch <dot@dotat.at> and subsequently
+# heavily modified by Ian Jackson <ijackson@chiark.greenend.org.uk>
+# and were released under CC0 1.0.  The whole program is now GPLv3+.)
+
 use strict;
 use warnings;
 
@@ -23,60 +42,70 @@ use Sys::Syslog;
 use Fcntl qw(:flock SEEK_SET);
 
 our $us = 'git-cache-proxy';
+
+#---------- error handling and logging ----------
+
+# This is a bit fiddly, because we want to catch errors sent to stderr
+# and dump them to syslog if we can, but only if we are running as an
+# inetd service.
+
 our $log; # filehandle (ref), or "1" meaning syslog
 
-BEGIN {
-    open STDERR, ">/dev/null" or exit 255;
-    open TEMPERR, "+>", undef or exit 255;
-    open STDERR, ">&TEMPERR" or exit 255;
+sub ntoa {
+    my $sockaddr = shift;
+    return ('(local)') unless defined $sockaddr;
+    my ($port,$addr) = sockaddr_in $sockaddr;
+    $addr = inet_ntoa $addr;
+    return ("[$addr]:$port",$addr,$port);
+}
 
-    sub ntoa {
-       my $sockaddr = shift;
-       return ('(local)') unless defined $sockaddr;
-       my ($port,$addr) = sockaddr_in $sockaddr;
-       $addr = inet_ntoa $addr;
-       return ("[$addr]:$port",$addr,$port);
-    }
+our ($client) = ntoa getpeername STDIN;
+our ($server) = ntoa getsockname STDIN;
 
-    our ($client,$client_addr,$client_port) = ntoa getpeername STDIN;
-    our ($server,$server_addr,$server_port) = ntoa getsockname STDIN;
+sub ensurelog () {
+    return if $log;
+    openlog $us, qw(pid), 'daemon';
+    $log = 1;
+}
 
-    sub ensurelog () {
-       return if $log;
-       openlog $us, qw(pid), 'daemon';
-       $log = 1;
+sub logm ($$) {
+    my ($pri, $msg) = @_;
+    if ($client eq '(local)') {
+       print STDERR "$us: $pri: $msg\n" or die $!;
+       exit 1;
     }
-
-    sub logm ($$) {
-       my ($pri, $msg) = @_;
-       ensurelog();
-       my $mainmsg = sprintf "%s-%s: %s", $server, $client, $msg;
-       if (ref $log) {
-           my $wholemsg = sprintf("%s [%d] %s: %s\n",
-                                  strftime("%Y-%m-%d %H:%M:%S Z", gmtime),
-                                  $$,
-                                  $pri,
-                                  $mainmsg);
-           print $log $wholemsg;
-       } else {
-           syslog $pri, $mainmsg;
-       }
+    ensurelog();
+    my $mainmsg = sprintf "%s-%s: %s", $server, $client, $msg;
+    if (ref $log) {
+       my $wholemsg = sprintf("%s [%d] %s: %s\n",
+                              strftime("%Y-%m-%d %H:%M:%S Z", gmtime),
+                              $$,
+                              $pri,
+                              $mainmsg);
+       print $log $wholemsg;
+    } else {
+       syslog $pri, $mainmsg;
     }
+}
+
+if ($client ne '(local)') {
+    open STDERR, ">/dev/null" or exit 255;
+    open TEMPERR, "+>", undef or exit 255;
+    open STDERR, ">&TEMPERR" or exit 255;
+}
 
-    END {
+END {
+    if ($client ne '(local)') {
        if ($?) { logm 'crit', "crashing ($?)"; }
        seek TEMPERR, 0, SEEK_SET;
        while (<TEMPERR>) {
            chomp;
            logm 'crit', $_;
        }
-       exit $?;
     }
+    exit $?;
 }
 
-our $fetchtimeout = 1800;
-our $maxfetchtimeout = 3600;
-
 sub fail ($) {
     my ($msg) = @_;
     logm 'err', $msg;
@@ -95,6 +124,10 @@ sub gitfail ($) {
     exit 0;
 }
 
+#---------- argument parsing ----------
+
+our $fetchtimeout = 1800;
+our $maxfetchtimeout = 3600;
 our $cachedir = '/var/cache/git-cache-proxy';
 
 for (;;) {
@@ -119,6 +152,8 @@ for (;;) {
 
 !@ARGV or fail "bad usage: no non-option arguments permitted";
 
+#---------- main program ----------
+
 chdir $cachedir or fail "chdir $cachedir: $!";
 
 our ($service,$specpath,$spechost);