2 ############################################################################
3 # soepkiptng (c) copyright 2000 Eric Lammerts <eric@lammerts.org>.
6 ############################################################################
7 # This program is free software; you can redistribute it and/or modify
8 # it under the terms of the GNU General Public License, version 2, as
9 # published by the Free Software Foundation.
11 # This program is distributed in the hope that it will be useful,
12 # but WITHOUT ANY WARRANTY; without even the implied warranty of
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 # GNU General Public License for more details.
16 # You should have received a copy of the GNU General Public License
17 # along with this program; if not, write to the Free Software
18 # Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20 ############################################################################
33 # find program directory
36 my $l = readlink or die "readlink $_: $!\n";
37 if($l =~ m
|^/|) { $_ = $l; } else { s|[^/]*$|/$l|; }
40 my $progdir = abs_path
($1);
42 require "$progdir/soepkiptng.lib";
43 require "$progdir/soepkiptng_web.lib";
47 ############################################################################
53 return scalar gethostbyaddr($req->peeraddr, AF_INET
);
57 print $cgiquery->header("text/html");
60 sub require_write_access
() {
62 if($conf{write_access_func
} &&
63 !eval $conf{write_access_func
}) {
66 printhdr
($conf{allstyle
});
67 print "<b>Access Denied.</b>\n";
74 local $lifetime = $opt_l || $conf{httpd_lifetime
} || 100;
79 # busy, exit after finishing current request
82 warn "\n=====pid$$ SIGHUP caught, exiting\n";
87 my $dbh = DBI
->connect("DBI:$conf{db_type}:$conf{db_name}:$conf{db_host}",
88 $conf{db_user
}, $conf{db_pass
}) or die "can't connect to database";
91 for($i = 0; $i < $lifetime; $i++) {
92 $conn = $daemon->accept or die "accept: $!\n";
93 my $req = $conn->get_request or next;
94 warn "\n=====pid$$->$i===== req->uri=[" . $req->uri . "]===\n";
96 if($req->method eq "GET") {
97 ($content = $req->uri) =~ s/.*?\?//;
98 } elsif($req->method eq "POST") {
99 $content = $req->content;
101 die "invalid request\n";
103 $cgiquery = new CGI
($content);
104 open STDOUT
, ">&=" . $conn->fileno;
105 handle_request
($dbh, $cgiquery, "/", $conn);
109 warn "$i connections served, exiting.\n";
113 ############################################################################
116 getopts
('dhp:i:l:c:');
118 $opt_h and die <<EOF;
120 Usage: soepkiptng_httpd [-dh] [-p port] [-i servers] [-l maxrequests]
123 -d : don't daemonize, log to stdout/stderr.
125 -p port : port to listen on
126 -i servers : number of child processes the parent process will spawn
127 -l maxrequests : the number of requests each child process is allowed to
128 process before it dies (to avoid problems like memory leaks
133 read_configfile
(\
%conf, $opt_c);
135 my $port = $opt_p || $conf{httpd_port
} || 80;
136 $daemon = new HTTP
::Daemon
(LocalPort
=> $port, ReuseAddr
=> 1)
137 or die "port $port: $!\n";
143 open STDIN
, "</dev/null";
144 if(defined($conf{httpd_errfile
})) {
145 rename $conf{httpd_errfile
}, "$conf{httpd_errfile}.old";
146 open STDOUT
, ">$conf{httpd_errfile}";
148 open STDOUT
, ">/dev/null";
150 open STDERR
, ">&STDOUT";
161 warn "master: killing -9 all children\n";
166 my $httpd_instances = $opt_i || $conf{httpd_instances
} || 2;
167 warn "master starting, pid=$$\n";
172 warn "master $$: exec self [$0]\n";
174 warn "master $$: exec self: $!\n";
178 warn "master: rereading configfile\n";
179 read_configfile
(\
%conf, $opt_c);
183 while(keys %pids < $httpd_instances) {
184 if(($pid = fork) == 0) {
189 next if !defined($pid);
190 warn "forked child, pid=$pid\n";
195 if($?
) { sleep 1; } # rate-limit failing children