git.git: Bring to current git/repo.git:girocco
[girocco.git] / mirroring / cloned.pl
blob6b8627e610592b82cfab09d69be41fa83e86b032
1 #!/usr/bin/perl
3 # cloned - Clone repositories on request
5 # cloned is Girocco mirroring servant; it processes requests for clones
6 # of given URLs received over its socket.
8 # When a request is received, new process is spawned that sets up
9 # the repository, ACKs on the socket and reports further progress
10 # to .clonelog within the repository. In case the clone fails,
11 # .clone_failed is touched and .clone_in_progress is removed.
13 # Protocol:
14 # Alice sets up repository and touches .cloning
15 # Alice opens connection to Bob
16 # Alice sends project name through the connection
17 # Bob opens the repository and sends back 0 if ok, error code otherwise
18 # Bob closes connection
19 # Alice polls .clonelog in case of 0.
20 # If Alice reads "@OVER@" from .clonelog, it stops polling.
22 # Based on perlipc example.
24 use strict;
25 use warnings;
27 use Girocco::Config;
28 use Socket;
30 $| = 1;
32 sub logmsg { print '['.(scalar localtime)."] $0 $$: @_\n" }
34 my $NAME = '/tmp/girocco.cloned';
35 my $uaddr = sockaddr_un($NAME);
37 socket(Server, PF_UNIX, SOCK_STREAM, 0) or die "socket: $!";
38 unlink($NAME);
39 bind(Server, $uaddr) or die "bind: $!";
40 listen(Server, SOMAXCONN) or die "listen: $!";
41 if ($Girocco::Config::owning_group) {
42 chmod 0664, $NAME or die "chmod: $!";
43 my $gid = scalar(getgrnam($Girocco::Config::owning_group));
44 chown(-1, $gid, $NAME) or die "chgrp $gid: $!";
45 } else {
46 chmod 0666, $NAME or die "chmod: $!";
50 use POSIX ":sys_wait_h";
51 sub REAPER {
52 my $child;
53 my $waitedpid;
54 while (($waitedpid = waitpid(-1, WNOHANG)) > 0) {
55 logmsg "reaped $waitedpid" . ($? ? " with exit $?" : '');
57 $SIG{CHLD} = \&REAPER; # loathe sysV
60 $SIG{CHLD} = \&REAPER; # Apollo 440
62 sub spawn {
63 my $coderef = shift;
65 my $pid = fork;
66 if (not defined $pid) {
67 logmsg "cannot fork: $!";
68 return;
69 } elsif ($pid) {
70 logmsg "begat $pid";
71 return; # I'm the parent
74 open STDIN, "<&Client" or die "can't dup client to stdin";
75 open STDOUT, ">&Client" or die "can't dup client to stdout";
76 exit &$coderef();
80 while (1) {
81 unless (accept(Client, Server)) {
82 logmsg "accept failed: $!";
83 next;
85 logmsg "connection on $NAME";
86 spawn sub {
87 my $name = <>;
88 print STDERR "read\n";
89 chomp $name;
90 print STDERR "print\n";
91 print "1\n";
92 print STDERR "cloning $name\n";
93 open STDOUT, ">".$Girocco::Config::reporoot."/".$name."/.clonelog" or die "cannot open clonelog: $!";
94 open STDERR, ">&STDOUT";
95 open STDIN, "</dev/null";
96 exec $Girocco::Config::basedir.'/mirroring/clone.sh', $name or die "exec failed: $!";
98 close Client;
99 sleep 1;