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.
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.
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: $!";
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: $!";
46 chmod 0666, $NAME or die "chmod: $!";
50 use POSIX
":sys_wait_h";
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
66 if (not defined $pid) {
67 logmsg
"cannot fork: $!";
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";
81 unless (accept(Client
, Server
)) {
82 logmsg
"accept failed: $!";
85 logmsg
"connection on $NAME";
88 print STDERR
"read\n";
90 print STDERR
"print\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
.'/daemons/clone.sh', $name or die "exec failed: $!";