/usr/lib/obs/server/BSStdServer.pm is in obs-server 2.7.1-10.
This file is owned by root:root, with mode 0o644.
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 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 | #
# Copyright (c) 2006, 2007 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
#
################################################################
#
# Common code for Build Service HTTP Servers. Replys are XML,
# automatic restarts can be done by touching <server>.restart
#
package BSStdServer;
use Fcntl;
use BSWatcher;
use BSServer;
use BSDispatch;
use BSVerify;
use BSServerEvents;
use BSXML;
use BSUtil;
use BSConfiguration;
use XML::Structured;
use strict;
our $isajax; # is this the ajax process?
our $return_ok = "<status code=\"ok\" />\n";
my $rundir = $BSConfig::rundir || "$BSConfig::bsdir/run";
sub stdreply {
my @rep = @_;
return unless @rep && defined($rep[0]);
if (ref($rep[0]) eq 'HASH') {
if (ref($rep[1]) eq 'CODE') {
$rep[1] = $rep[1]->($rep[0]);
} else {
$rep[1] = XMLout($rep[1], $rep[0]);
}
shift @rep;
}
push @rep, 'Content-Type: text/xml' if @rep == 1;
BSWatcher::reply(@rep);
}
sub errreply {
my ($err, $code, $tag, @hdrs) = @_;
my $opresult = {'code' => $code, 'summary' => $tag};
$opresult->{'details'} = $err if $err && $err ne $tag && $err ne "$tag\n";
my $opresultxml;
eval {
$opresultxml = XMLout($BSXML::opstatus, $opresult);
};
$opresultxml ||= "<status code=\"500\"\n <summary>internal error in errreply</summary>\n</status>\n";
BSWatcher::reply($opresultxml, "Status: $code $tag", 'Content-Type: text/xml', @hdrs);
}
sub authorize {
my ($conf, $req, $auth) = @_;
return () unless $BSConfig::ipaccess;
my %auths;
my $peer = $req->{'peer'};
for my $ipre (sort keys %$BSConfig::ipaccess) {
next unless $peer =~ /^$ipre$/s;
$auths{$_} = 1 for split(',', $BSConfig::ipaccess->{$ipre});
}
return () if grep {$auths{$_}} split(',', $auth);
warn("500 access denied for $peer by \$ipaccess rules in BSConfig\n");
die("500 access denied by \$ipaccess rules\n");
}
sub dispatch {
my ($conf, $req) = @_;
my @lt = localtime(time);
my $msg = "$req->{'action'} $req->{'path'}?$req->{'query'}";
BSServer::setstatus(2, $msg) if $conf->{'serverstatus'};
$msg = BSUtil::isotime()." [$$]: $msg";
$msg .= " (AJAX)" if $isajax;
print "$msg\n";
if ($isajax) {
BSServerEvents::cloneconnect("OK\n", "Content-Type: text/plain");
}
BSDispatch::dispatch($conf, $req);
}
my $configurationcheck = 0;
sub periodic {
my ($conf) = @_;
if (-e "$rundir/$conf->{'name'}.exit") {
BSServer::msg("$conf->{'name'} exiting...");
unlink("$conf->{'ajaxsocketpath'}.lock") if $conf->{'ajaxsocketpath'};
unlink("$rundir/$conf->{'name'}.exit");
exit(0);
}
if (-e "$rundir/$conf->{'name'}.restart") {
BSServer::msg("$conf->{'name'} restarting...");
if (system($0, "--test")) {
BSServer::msg("$0 failed, aborting restart");
return;
}
unlink("$rundir/$conf->{'name'}.restart");
my $arg;
my $sock = BSServer::getserversocket();
# clear close-on-exec bit
fcntl($sock, F_SETFD, 0);
$arg = fileno($sock);
my $sock2 = BSServer::getserversocket2();
if ($sock2) {
fcntl($sock2, F_SETFD, 0);
$arg .= ','.fileno($sock2);
}
exec($0, '--restart', $arg);
die("$0: $!\n");
}
if ($configurationcheck++ > 10) {
BSConfiguration::check_configuration();
$configurationcheck = 0;
}
}
sub periodic_ajax {
my ($conf) = @_;
my @s = stat(BSServer::getserverlock());
return if $s[3];
my $sev = $conf->{'server_ev'};
close($sev->{'fd'});
BSEvents::rem($sev);
BSServer::msg("AJAX: $conf->{'name'} exiting.");
exit(0);
}
sub serverstatus {
my ($cgi) = @_;
my @res;
for my $s (BSServer::serverstatus()) {
next unless $s->{'state'};
push @res, {
'id' => $s->{'slot'},
'starttime' => $s->{'starttime'},
'pid' => $s->{'pid'},
'request' => $s->{'data'},
};
$res[-1]->{'group'} = $s->{'group'} if $s->{'group'};
}
return ({'job' => \@res}, $BSXML::serverstatus);
}
sub isrunning {
my ($name, $conf) = @_;
return 1 unless $conf; # can't check
# hmm, might want to use a lock instead...
eval {
BSServer::serveropen($conf->{'port'});
BSServer::serverclose();
};
return $@ && "$@" =~ /bind:/ ? 1 : 0;
}
sub server {
my ($name, $args, $conf, $aconf) = @_;
if ($args && @$args) {
if ($args->[0] eq '--test') {
exit 0;
}
if ($args->[0] eq '--stop' || $args->[0] eq '--exit') {
if (!isrunning($name, $conf)) {
print "server not running\n";
exit 0;
}
print("exiting server...\n");
BSUtil::touch("$rundir/$name.exit");
BSUtil::waituntilgone("$rundir/$name.exit");
exit 0;
}
if ($args->[0] eq '--restart' && @$args == 1) {
if (!isrunning($name, $conf)) {
die("server not running\n");
}
print("restarting server...\n");
BSUtil::touch("$rundir/$name.restart");
BSUtil::waituntilgone("$rundir/$name.restart");
exit 0;
}
}
my $bsdir = $BSConfig::bsdir || "/srv/obs";
BSUtil::mkdir_p_chown($bsdir, $BSConfig::bsuser, $BSConfig::bsgroup) || die("unable to create $bsdir\n");
if ($conf) {
$conf->{'verifiers'} ||= $BSVerify::verifiers;
$conf->{'dispatch'} ||= \&dispatch;
$conf->{'stdreply'} ||= \&stdreply;
$conf->{'errorreply'} ||= \&errreply;
$conf->{'authorize'} ||= \&authorize;
$conf->{'periodic'} ||= \&periodic;
$conf->{'periodic_interval'} ||= 1;
$conf->{'serverstatus'} ||= "$rundir/$name.status";
$conf->{'setkeepalive'} = 1 unless defined $conf->{'setkeepalive'};
$conf->{'name'} = $name;
BSDispatch::compile($conf);
}
if ($aconf) {
require BSHandoff;
$aconf->{'verifiers'} ||= $BSVerify::verifiers;
$aconf->{'dispatch'} ||= \&dispatch;
$aconf->{'stdreply'} ||= \&stdreply;
$aconf->{'errorreply'} ||= \&errreply;
$aconf->{'periodic'} ||= \&periodic_ajax;
$aconf->{'periodic_interval'} ||= 1;
$aconf->{'dispatches_call'} ||= \&BSWatcher::dispatches_call;
$aconf->{'getrequest_recvfd'} ||= \&BSHandoff::receivefd;
$aconf->{'setkeepalive'} = 1 unless defined $aconf->{'setkeepalive'};
$aconf->{'getrequest_timeout'} = 10 unless exists $aconf->{'getrequest_timeout'};
$aconf->{'replrequest_timeout'} = 10 unless exists $aconf->{'replrequest_timeout'};
$aconf->{'name'} = $name;
BSDispatch::compile($aconf);
}
BSServer::deamonize(@{$args || []});
if ($conf) {
my $port = $conf->{'port'};
my $port2 = $conf->{'port2'};
if ($args && @$args && $args->[0] eq '--restart') {
my @ports = split(',', $args->[1]);
$port = "&=$ports[0]" if defined $ports[0];
$port2 = "&=$ports[1]" if $port2 && defined $ports[1];
POSIX::close($ports[1]) if !$port2 && defined $ports[1];
}
BSServer::serveropen($port2 ? "$port,$port2" : $port, $BSConfig::bsuser, $BSConfig::bsgroup);
$conf->{'ajaxsocketpath'} = $aconf->{'socketpath'} if $aconf;
$conf->{'handoffpath'} = $aconf->{'socketpath'} if $aconf;
unlink("$aconf->{'socketpath'}.lock") if $aconf;
}
if ($aconf) {
if (!$conf || xfork() == 0) {
$isajax = 1;
BSServer::serverclose() if $conf;
BSServer::serveropen_unix($aconf->{'socketpath'}, $BSConfig::bsuser, $BSConfig::bsgroup);
my $sev = BSServerEvents::addserver(BSServer::getserversocket(), $aconf);
$aconf->{'server_ev'} = $sev; # for periodic_ajax
BSServer::msg("AJAX: $name started");
eval {
BSEvents::schedule();
};
writestr("$rundir/$name.AJAX.died", undef, $@);
die("AJAX: died\n");
}
}
mkdir_p($rundir);
# intialize xml converter to speed things up
XMLin(['startup' => '_content'], '<startup>x</startup>');
if ($conf->{'port2'}) {
BSServer::msg("$name started on ports $conf->{port} and $conf->{port2}");
} else {
BSServer::msg("$name started on port $conf->{port}");
}
BSServer::server($conf);
die("server returned\n");
}
1;
|