From 12288b0a62deeb750183e8368305e75f58f4deb2 Mon Sep 17 00:00:00 2001 From: Florent Villard Date: Tue, 13 Dec 2005 14:36:57 +0000 Subject: Protect pid file checking and creation --- iurt2 | 83 ++++++++++++++++++++++++++++++++++++------------------------------- 1 file changed, 45 insertions(+), 38 deletions(-) (limited to 'iurt2') diff --git a/iurt2 b/iurt2 index aa1d7d7..a490f4c 100755 --- a/iurt2 +++ b/iurt2 @@ -67,34 +67,38 @@ if ($check_queue) { } my $pidfile = "$cache_home/iurt.$distro_tag.$my_arch.pid"; -my (@stat) = stat $pidfile; --f $pidfile and do { - open PID, $pidfile; - my $pid = ; - close PID; - if ($pid && getpgrp $pid != -1) { - my $time = $stat[9]; - if ($time < time - 36000) { - print "iurt: an other iurt pid $pid is running for a very long time, killing it\n"; - my $i; - while ($i < 5 && getpgrp $pid != -1) { - kill_for_good($pid); - $i++; - sleep 1 - } - } else { - print "iurt: an other iurt is running for $my_arch, pid $pid, since ",time - $time," seconds\n"; - exit - } - } else { - print "iurt: a previous iurt for $my_arch seems dead, cleaning.\n"; - unlink $pidfile +open my $lock, "$pidfile.lock"; +flock($lock,LOCK_EX); +if (-f $pidfile) { + my (@stat) = stat $pidfile; + open my $test_PID, $pidfile; + my $pid = <$test_PID>; + close $test_PID; + if ($pid && getpgrp $pid != -1) { + my $time = $stat[9]; + if ($time < time - 36000) { + print "iurt: an other iurt pid $pid is running for a very long time, killing it\n"; + my $i; + while ($i < 5 && getpgrp $pid != -1) { + kill_for_good($pid); + $i++; + sleep 1 + } + } else { + print "iurt: an other iurt is running for $my_arch, pid $pid, since ",time - $time," seconds\n"; + exit } -}; + } else { + print "iurt: a previous iurt for $my_arch seems dead, cleaning.\n"; + unlink $pidfile + } +} +open my $PID, ">$pidfile" or die "FATAL iurt: could not open pidfile $pidfile for writing"; +print $PID $$; +close $PID; -open PID, ">$pidfile"; -print PID $$; -close PID; +flock($lock,LOCK_UN); +close $lock; my $cache = "$cache_home/iurt.$distro_tag.cache"; print "iurt: loading cache file $cache\n"; @@ -111,7 +115,7 @@ my @wrong_rpm; foreach my $arch (@supported_arch) { my $rpms_dir = "/mnt/BIG/dis/$distro_version/$arch/media/$media/"; print "iurt: checking current packages in $rpms_dir\n"; - opendir my $rpmdir, $rpms_dir; + opendir my $rpmdir, $rpms_dir or next; foreach my $rpm (readdir $rpmdir) { my ($rarch, $srpm) = update_srpm($rpms_dir, $rpm); $rarch or next; @@ -125,7 +129,7 @@ foreach my $arch (@supported_arch) { if ($check_queue) { my $dir = "$base_dir/build/RPMS/"; - opendir my $rpmdir, $dir; + opendir my $rpmdir, $dir or next; print "iurt: checking upload queue in $dir\n"; foreach my $rpm (readdir $rpmdir) { @@ -157,8 +161,9 @@ my %done_rpm; # foreach my $dir ("/mnt/BIG/dis/$distro_version/SRPMS/$media/", @special_srpm_dir) { print "iurt: checking SRPMS dir $dir\n"; - opendir my $rpmdir, $dir; + opendir my $rpmdir, $dir or next; foreach my $srpm (readdir $rpmdir) { + # this is for the output of the new svn system if ($srpm =~ /^\@\d+:(.*)/) { link "$dir/$srpm", "$dir/$1"; # unlink "$dir/$srpm"; @@ -240,7 +245,7 @@ foreach my $t (@todo) { hash => 'chroot_init', die => 1); - dump_rpmmacros("$chroot/home/builder/.rpmmacros"); + dump_rpmmacros("$chroot/home/builder/.rpmmacros") or next; my ($srpm_name) = $srpm =~ /(.*)-[^-]+-[^-]+\.src\.rpm$/ or next; my $maintainer = `rpmmon -s -p $srpm_name`; @@ -306,11 +311,14 @@ process_queue(); dump_cache(); print "ERROR iurt: RPM with a wrong SRPM name\n" if @wrong_rpm; -open my $file, ">$local_spool/log/wrong_srpm_names.log"; -foreach (@wrong_rpm) { +if (open my $file, ">$local_spool/log/wrong_srpm_names.log") { + foreach (@wrong_rpm) { print $file "$_->[1] -> $_->[0] (", $::rpm_srpm{$_->[1]},")\n"; + } } +system("rsync -alHPe 'ssh -c arcfour' $local_spool/log/ mandrake\@qa:/var/www/html/build/iurt/$distro_tag/$my_arch/log/"); + unlink $pidfile; exit; @@ -338,8 +346,7 @@ sub usage { sub process_queue { my $dir = "$local_home/iurt/$distro_tag/$my_arch"; #my $dir = "$base_dir/RPMS/"; - opendir my $rpmdir, $dir; - + opendir my $rpmdir, $dir or next; foreach my $rpm (readdir $rpmdir) { my ($rarch, $srpm) = update_srpm($dir, $rpm); $rarch or next; @@ -387,11 +394,11 @@ sub sendmail { do { print "Cannot find sender-email-address [$to]\n"; return } unless defined($to); $from ||= "Iurt the rebuild bot "; my $MAIL; - if (!$debug) { open $MAIL, "| /usr/sbin/sendmail -t" } else { open $MAIL, ">&STDOUT" } + if (!$debug) { open $MAIL, "| /usr/sbin/sendmail -t" or return } else { open $MAIL, ">&STDOUT" or return } my $sender = encode_mimewords($to); my $subject = encode_mimewords($subject); - print $MAIL "To: $to\n"; - if ($cc) { print $MAIL "Cc: $cc\n" } + print $MAIL "To: $sender\n"; + if ($cc) { $cc = endcode_mimewords($cc); print $MAIL "Cc: $cc\n" } print $MAIL "From: $from\n"; print $MAIL "Subject: $subject\n"; print $MAIL "\n"; @@ -487,7 +494,7 @@ sub kill_for_good { sub dump_rpmmacros { my ($file) = @_; - open my $f, qq{| sudo sh -c "cat > $file"}; + open my $f, qq{| sudo sh -c "cat > $file"} or return 0; print $f qq{\%_topdir \%(echo \$HOME)/rpm \%_tmppath \%(echo \$HOME)/rpm/tmp/ \%distribution Mandriva Linux -- cgit v1.2.1