taskd.pl: avoid displaying spurious accept failed errors
[girocco.git] / taskd / taskd.pl
blobf06dc81d3fbe4f7e5179c69552121a0fa1ee2bf4
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 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 # Clone 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 error code if there is a problem
18 # Bob closes connection
19 # Alice polls .clonelog in case of success.
20 # If Alice reads "@OVER@" from .clonelog, it stops polling.
22 # Ref-change protocol:
23 # Alice opens connection to Bob
24 # Alice sends ref-change command for each changed ref
25 # Alice closes connection
26 # Bob sends out notifications
28 # Based on perlipc example.
30 use strict;
31 use warnings;
33 use Girocco::Config;
34 use Girocco::Notify;
35 use Girocco::Project;
36 use Girocco::User;
37 use Socket;
38 use Errno;
40 $| = 1;
42 sub logmsg { print '['.(scalar localtime)."] $0 $$: @_\n" }
44 my $NAME = $Girocco::Config::chroot.'/etc/taskd.socket';
45 my $uaddr = sockaddr_un($NAME);
47 socket(Server, PF_UNIX, SOCK_STREAM, 0) or die "socket: $!";
48 unlink($NAME);
49 bind(Server, $uaddr) or die "bind: $!";
50 listen(Server, SOMAXCONN) or die "listen: $!";
51 chmod 0666, $NAME or die "chmod: $!";
54 use POSIX ":sys_wait_h";
55 sub REAPER {
56 local $!;
57 my $child;
58 my $waitedpid;
59 while (($waitedpid = waitpid(-1, WNOHANG)) > 0) {
60 logmsg "reaped $waitedpid" . ($? ? " with exit $?" : '');
62 $SIG{CHLD} = \&REAPER; # loathe sysV
65 $SIG{CHLD} = \&REAPER; # Apollo 440
67 sub spawn {
68 my $coderef = shift;
70 my $pid = fork;
71 if (not defined $pid) {
72 logmsg "cannot fork: $!";
73 return;
74 } elsif ($pid) {
75 logmsg "begat $pid";
76 return; # I'm the parent
79 $SIG{CHLD} = 'DEFAULT';
81 open STDIN, "<&Client" or die "can't dup client to stdin";
82 open STDOUT, ">&Client" or die "can't dup client to stdout";
83 exit &$coderef();
86 sub clone {
87 my ($name) = @_;
88 my $proj = Girocco::Project->load($name);
89 $proj or die "failed to load project $name";
90 $proj->{clone_in_progress} or die "project $name is not marked for cloning";
91 $proj->{clone_logged} and die "project $name is already being cloned";
92 print STDERR "cloning $name\n";
93 open STDOUT, ">".$Girocco::Config::reporoot."/".$name.".git/.clonelog" or die "cannot open clonelog: $!";
94 open STDERR, ">&STDOUT";
95 open STDIN, "</dev/null";
96 exec $Girocco::Config::basedir.'/taskd/clone.sh', "$name.git" or die "exec failed: $!";
99 sub ref_change {
100 my ($arg) = @_;
101 my ($username, $name, $oldrev, $newrev, $ref) = split(/\s+/, $arg);
103 my $proj = Girocco::Project->load($name);
104 $proj or die "failed to load project $name";
106 my $user;
107 if ($username && $username !~ /^%.*%$/) {
108 $user = Girocco::User->load($username);
109 $user or die "failed to load user $username";
112 print STDERR "ref-change $username $name ($ref: $oldrev -> $newrev)\n";
113 open STDIN, "</dev/null";
114 Girocco::Notify::ref_change($proj, $user, $ref, $oldrev, $newrev);
115 return 0;
118 while (1) {
119 unless (accept(Client, Server)) {
120 logmsg "accept failed: $!" unless $!{EINTR};
121 next;
123 logmsg "connection on $NAME";
124 spawn sub {
125 my $inp = <>;
126 chomp $inp;
127 $inp or exit 0; # ignore empty connects
128 my ($cmd, $arg) = $inp =~ /^([a-zA-Z0-9-]+)\s+(.*)$/;
129 if ($cmd eq 'clone') {
130 clone($arg);
131 } elsif ($cmd eq 'ref-change') {
132 ref_change($arg);
133 } else {
134 die "unknown command: $cmd";
137 close Client;
138 sleep 1;