This file is indexed.

/usr/lib/obs/server/bs_warden is in obs-server 2.7.1-10.

This file is owned by root:root, with mode 0o755.

The actual contents of the file can be viewed below.

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
#!/usr/bin/perl -w
#
# Copyright (c) 2009 Michael Schroeder, Novell Inc.
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License version 2 as
# published by the Free Software Foundation.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program (see the file COPYING); if not, write to the
# Free Software Foundation, Inc.,
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
#
################################################################
#
# Check if all jobs in state building are really built on the workers
#

BEGIN {
  my ($wd) = $0 =~ m-(.*)/- ;
  $wd ||= '.';
  unshift @INC,  "$wd/build";
  unshift @INC,  "$wd";
}

use POSIX;
use Data::Dumper;
use Digest::MD5 ();
use Fcntl qw(:DEFAULT :flock);
use XML::Structured ':bytes';

use BSConfiguration;
use BSRPC;
use BSUtil;
use BSXML;

use strict;

my $bsdir = $BSConfig::bsdir || "/srv/obs";

BSUtil::mkdir_p_chown($bsdir, $BSConfig::bsuser, $BSConfig::bsgroup);
BSUtil::drop_privs_to($BSConfig::bsuser, $BSConfig::bsgroup);

my $rundir = $BSConfig::rundir || "$BSConfig::bsdir/run";
my $workersdir = "$BSConfig::bsdir/workers";
my $jobsdir = "$BSConfig::bsdir/jobs";

$| = 1;
$SIG{'PIPE'} = 'IGNORE';
BSUtil::restartexit($ARGV[0], 'warden', "$rundir/bs_warden");
print BSUtil::isotime().": starting build service worker warden\n";

# get lock
mkdir_p($rundir);
open(RUNLOCK, '>>', "$rundir/bs_warden.lock") || die("$rundir/bs_warden.lock: $!\n");
flock(RUNLOCK, LOCK_EX | LOCK_NB) || die("worker warden is already running!\n");
utime undef, undef, "$rundir/bs_warden.lock";

my %building;
my $nextorphan = 0;
my $xcheck = 0;
my %orphanchecks;

while (1) {
  my $now = time();
  my %nbuilding;
  for my $wname (ls("$workersdir/building")) {
    next if $wname =~ /^\./;
    $nbuilding{$wname} = $building{$wname} || {'lastcheck' => $now};
  }
  %building = %nbuilding;
  %nbuilding = ();
  for my $wname (sort keys %building) {
    my $b = $building{$wname};
    my $lastcheck = $b->{'lastcheck'};
    $lastcheck += rand(60 * 60);
    next if $lastcheck > $now;
    last if -e "$rundir/bs_warden.restart";
    last if -e "$rundir/bs_warden.exit";
    my $worker = readxml("$workersdir/building/$wname", $BSXML::worker, 1);
    next unless $worker && $worker->{'job'} && $worker->{'arch'};
    my $job = $worker->{'job'};
    my $arch = $worker->{'arch'};
    $building{$wname}->{'job'} = "$arch/$job";
    my $js;
    if ($worker->{'reposerver'}) {
      # masterdispatched job. ask slave about job status.
      my $param = {
	'uri' => "$worker->{'reposerver'}/jobs/$arch/$job",
	'timeout' => 60,
      };
      eval {
	$js = BSRPC::rpc($param, $BSXML::jobstatus, 'view=status');
      };
    } else {
      $js = readxml("$jobsdir/$arch/$job:status", $BSXML::jobstatus, 1);
    }
    next unless $js && $js->{'code'} eq 'building';
    next unless $js->{'workerid'} eq $worker->{'workerid'};
    #print "checking worker $wname\n";
    my $param = {
      'uri' => "$js->{'uri'}/worker",
      'timeout' => 60,
    };
    eval {
      BSRPC::rpc($param, undef, "jobid=$js->{'jobid'}");
    };
    if ($@) {
      warn($@);
      if ($worker->{'reposerver'}) {
	print BSUtil::isotime().": restarting build of $arch/$job building on $js->{'workerid'}\n";
	my $param = {
	  'uri' => "$worker->{'reposerver'}/jobs/$arch/$job",
	  'request' => 'POST',
	  'timeout' => 60,
	};
	eval {
	  BSRPC::rpc($param, undef, 'cmd=idleworker', "workerid=$worker->{'workerid'}", "jobid=$js->{'jobid'}");
	  unlink("$workersdir/building/$wname");
	  delete $building{$wname};
	};
	warn($@) if $@;
      } else {
	local *F;
	my $js2 = BSUtil::lockopenxml(\*F, '<', "$jobsdir/$arch/$job:status", $BSXML::jobstatus, 1);
	if (!$js2 || $js2->{'code'} ne 'building' || $js2->{'jobid'} ne $js->{'jobid'} || $js2->{'workerid'} ne $js->{'workerid'}) {
	  print "build of $job is done on a different worker\n";
	  close F;
	  next;
	}
	print BSUtil::isotime().": restarting build of $arch/$job building on $js->{'workerid'}\n";
	unlink("$jobsdir/$arch/$job:status");
        unlink("$workersdir/building/$wname");
        delete $building{$wname};
        close F;
      }
    } else {
      $b->{'lastcheck'} = $now;
    }
  }
  if ($now > $nextorphan) {
    $nextorphan = $now + 60;	# every minute
    $xcheck = 0 if $xcheck++ > 10;
    my %buildingjobs = map {($_->{'job'} || '') => 1} values %building;
    for my $arch (sort(ls($jobsdir))) {
      next unless -d "$jobsdir/$arch";
      my @b = sort(grep {!/^\./} ls("$jobsdir/$arch"));
      my %locked = map {$_ => 1} grep {/:status$/} @b;
      # check for orphaned jobs
      my %norphanchecks;
      $orphanchecks{$arch} ||= {};
      for my $job (grep {!/:(?:dir|status|new)$/} @b) {
	next unless $locked{"$job:status"};
	next if $buildingjobs{"$arch/$job"};
	if (!$orphanchecks{$arch}->{$job}) {
	  my @s = stat("$jobsdir/$arch/$job:status");
	  $norphanchecks{$job} = 1 + (((@s ? $s[9] : 0) / 60) % 30);
	} else {
	  $norphanchecks{$job} = $orphanchecks{$arch}->{$job};
	}
	next if $norphanchecks{$job}++ < 30;	# check every 30 minutes
	$norphanchecks{$job} = 1;
	my $js = readxml("$jobsdir/$arch/$job:status", $BSXML::jobstatus, 1);
	if ($js && $js->{'code'} ne 'building') {
	  my @s = stat("$jobsdir/$arch/$job:status");
	  if (@s && $s[9] + 86400 < $now) {
	    print BSUtil::isotime().": restarting build of $arch/$job stuck in code $js->{'code'}\n";
	    unlink("$jobsdir/$arch/$job:status");
	  }
	  next;
	}
        next unless $js && $js->{'code'} eq 'building';
	#print "orphan check for $arch/$job...\n";
	my $param = {
	  'uri' => "$js->{'uri'}/worker",
	  'timeout' => 60,
	};
	eval {
	  BSRPC::rpc($param, undef, "jobid=$js->{'jobid'}");
	};
	if ($@) {
	  warn($@);
	  local *F;
	  my $js2 = BSUtil::lockopenxml(\*F, '<', "$jobsdir/$arch/$job:status", $BSXML::jobstatus, 1);
	  if (!$js2 || $js2->{'code'} ne 'building' || $js2->{'jobid'} ne $js->{'jobid'} || $js2->{'workerid'} ne $js->{'workerid'}) {
	    print "build of $job is done on a different worker\n";
	    close F;
	    next;
	  }
	  print BSUtil::isotime().": restarting orphaned build of $arch/$job building on $js->{'workerid'}\n";
	  unlink("$jobsdir/$arch/$job:status");
          close F;
	}
      }
      $orphanchecks{$arch} = \%norphanchecks;
      if (!$xcheck) {
	# check orphaned :dir or :status files
	my %jobs = map {$_ => 1} grep {!/:(?:dir|status|new)$/} @b;
	for my $job (grep {s/:dir$//} @b) {
	  next if $jobs{$job};
	  my @s = stat("$jobsdir/$arch/$job:dir");
	  if (@s && $s[9] + 86400 < $now) {
	    print BSUtil::isotime().": removing orphaned $arch/$job result directory\n";
	    BSUtil::cleandir("$jobsdir/$arch/$job:dir");
	    rmdir("$jobsdir/$arch/$job:dir");
	  }
	}
	for my $job (grep {s/:status$//} @b) {
	  next if $jobs{$job};
	  my @s = stat("$jobsdir/$arch/$job:status");
	  if (@s && $s[9] + 86400 < $now) {
	    print BSUtil::isotime().": removing orphaned $arch/$job status file\n";
	    rmdir("$jobsdir/$arch/$job:status");
	  }
	}
      }
    }
  }

  #print "sleeping\n";
  for my $i (qw{1 2 3 4 5}) {
    if (-e "$rundir/bs_warden.exit") {
      close(RUNLOCK);
      unlink("$rundir/bs_warden.exit");
      print BSUtil::isotime().": exiting...\n";
      exit(0);
    }
    if (-e "$rundir/bs_warden.restart") {
      close(RUNLOCK);
      unlink("$rundir/bs_warden.restart");
      print BSUtil::isotime().": restarting...\n";
      exec($0);
      die("$0: $!\n");
    }
    sleep(1);
  }
}