taskd clone: Pass project name without trailing .git
[girocco/radio.git] / taskd / taskd.pl
blob5816b600b823de75d33fb488be9d073e367784c0
1 #!/usr/bin/perl
3 # taskd - Clone repositories on request
5 # taskd 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 = $Girocco::Config::chroot.'/etc/taskd.socket';
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();
79 sub clone {
80 my ($name) = @_;
81 print "1\n";
82 print STDERR "cloning $name\n";
83 open STDOUT, ">".$Girocco::Config::reporoot."/".$name.".git/.clonelog" or die "cannot open clonelog: $!";
84 open STDERR, ">&STDOUT";
85 open STDIN, "</dev/null";
86 exec $Girocco::Config::basedir.'/taskd/clone.sh', "$name.git" or die "exec failed: $!";
89 while (1) {
90 unless (accept(Client, Server)) {
91 logmsg "accept failed: $!";
92 next;
94 logmsg "connection on $NAME";
95 spawn sub {
96 my $inp = <>;
97 chomp $inp;
98 my ($cmd, $arg) = $inp =~ /^(\w+) (.*)$/;
99 if ($cmd eq 'clone') {
100 clone($arg);
101 } else {
102 die "unknown command: $cmd";
105 close Client;
106 sleep 1;