29 |
|
|
30 |
}} |
}} |
31 |
|
|
|
our $pids; |
|
|
$pids = { httpd => $$ } unless defined $pids; # keep pids on refresh |
|
|
|
|
|
sub DESTROY { |
|
|
warn "pids ",dump( $pids ); |
|
|
foreach ( values %$pids ) { |
|
|
warn "kill $_"; |
|
|
kill 1,$_ || kill 9, $_; |
|
|
} |
|
|
} |
|
|
|
|
32 |
our $port = 7777; |
our $port = 7777; |
33 |
|
|
34 |
use server; |
use server; |
43 |
use log; |
use log; |
44 |
use x11; |
use x11; |
45 |
use amt; |
use amt; |
46 |
|
use boolean; |
47 |
|
use daemons; |
48 |
|
|
49 |
|
use kvm; |
50 |
|
use browser; |
51 |
|
use network; |
52 |
|
|
53 |
|
|
54 |
sub static { |
sub static { |
55 |
my ($client,$path) = @_; |
my ($client,$path) = @_; |
101 |
exit(0); |
exit(0); |
102 |
} |
} |
103 |
|
|
|
use boolean; |
|
|
|
|
|
use kvm; |
|
|
|
|
|
$SIG{CHLD} = 'IGNORE'; |
|
|
|
|
|
sub start_stop { |
|
|
my $daemon = shift; |
|
|
|
|
|
my $pid = $pids->{$daemon}; |
|
|
my $pid_path = "$server::conf/$daemon.pid"; |
|
|
|
|
|
if ( ! $pid && -e $pid_path ) { |
|
|
my $p = read_file $pid_path; |
|
|
if ( kill 0, $p ) { |
|
|
warn "adopted $daemon $p\n"; |
|
|
return $pids->{$daemon} = $p; |
|
|
} else { |
|
|
warn "old $daemon $p not running"; |
|
|
unlink $pid_path; |
|
|
} |
|
|
} |
|
|
|
|
|
$pid ||= 'not started'; |
|
|
warn "start_stop $daemon $pid\n"; |
|
|
|
|
|
if ( $pid =~ m{^\d+$} ) { |
|
|
my $pstree = `pstree -p $pid`; |
|
|
my @pids = $pstree =~ m{\((\d+)\)}g; |
|
|
warn "pstree $pstree pids ",dump( @pids ); |
|
|
kill 1, $_ foreach reverse @pids; |
|
|
$pids->{$daemon} = 'stopped'; |
|
|
return qq|$daemon pid $pid stopped|; |
|
|
} else { |
|
|
if ( $pid = fork ) { |
|
|
# parent |
|
|
$pids->{$daemon} = $pid; |
|
|
write_file $pid_path, $pid; |
|
|
warn "forked $daemon $pid\n"; |
|
|
return qq|$daemon pid $pid started|; |
|
|
} elsif ( defined $pid ) { |
|
|
# child |
|
|
my $invoke = 'start'; |
|
|
$invoke = $1 if $daemon =~ s{/(.+)}{}; |
|
|
if ( $daemon =~ m{dhcpd|tftpd|dnsd} ) { |
|
|
my $exec = "perl -I$server::base_dir/lib -I$server::base_dir/lib/PXElator -M$daemon -e ${daemon}::${invoke}"; |
|
|
warn "exec $exec"; |
|
|
x11::xterm( $daemon => $exec ); |
|
|
} else { |
|
|
my $eval = $daemon . '::' . $invoke . '(' . ( @_ ? dump(@_) : '' ) . ')'; |
|
|
warn "eval $eval"; |
|
|
eval $eval; |
|
|
warn "can't start $daemon: $@" if $@; |
|
|
} |
|
|
exit; |
|
|
} else { |
|
|
die "fork error $!"; |
|
|
} |
|
|
} |
|
|
} |
|
|
|
|
104 |
my $ok = qq|HTTP/1.0 200 OK\r\nContent-Type: text/html\r\nConnection: close\r\n\r\n| . menu(); |
my $ok = qq|HTTP/1.0 200 OK\r\nContent-Type: text/html\r\nConnection: close\r\n\r\n| . menu(); |
105 |
my $redirect = qq|HTTP/1.1 302 Found\r\nContent-type: text/html\r\nLocation: $url\r\n\r\n|; |
my $redirect = qq|HTTP/1.1 302 Found\r\nContent-type: text/html\r\nLocation: $url\r\n\r\n|; |
106 |
|
|
121 |
|
|
122 |
my $debug_proc = ''; |
my $debug_proc = ''; |
123 |
|
|
124 |
warn 'pids: ', dump( $pids ) if $debug; |
foreach my $name ( sort keys %$daemons::pids ) { |
125 |
foreach my $name ( sort keys %$pids ) { |
my $pid = $daemons::pids->{$name} || next; |
|
my $pid = $pids->{$name} || next; |
|
126 |
|
|
127 |
my $html; |
my $html; |
128 |
|
|
178 |
|
|
179 |
} elsif ( $path =~ m{^/server} ) { |
} elsif ( $path =~ m{^/server} ) { |
180 |
print $client $ok |
print $client $ok |
181 |
, html::table( 2, map { ( $_, eval '$server::'.$_ ) } ( 'ip', 'netmask', 'ip_from', 'ip_to', 'domain_name', 'base_dir' ) ) |
, html::table( 2, map { ( $_, html::tt eval '$server::'.$_ ) } ( 'ip', 'netmask', 'ip_from', 'ip_to', 'domain_name', 'base_dir', 'conf' ) ) |
182 |
; |
; |
183 |
} elsif ( $path =~ m!^/client(?:/$RE{net}{IPv4}{-keep})?! ) { |
} elsif ( $path =~ m!^/client(?:/$RE{net}{IPv4}{-keep})?! ) { |
184 |
my $ip = $1 || $client->peerhost; |
my $ip = $1 || $client->peerhost; |
210 |
map { |
map { |
211 |
my $ip = $_; |
my $ip = $_; |
212 |
$ip =~ s{^.+/ip/}{}; |
$ip =~ s{^.+/ip/}{}; |
213 |
( qq|<a href=/client/$ip>$ip</a>|, client::mac($ip), client::conf( $ip, 'hostname' ), client::conf( $ip, 'deploy' ) ); |
( qq|<a href=/client/$ip>$ip</a>|, client::mac($ip), client::conf( $ip, 'hostname' ), html::tt client::conf( $ip, 'deploy' ) ); |
214 |
} |
} |
215 |
glob("$server::conf/ip/*") |
glob("$server::conf/ip/*") |
216 |
) |
) |
222 |
print $client $redirect, qq|<big>$1 = $2</big><br>Location: <a href="$url">$url</a>|; |
print $client $redirect, qq|<big>$1 = $2</big><br>Location: <a href="$url">$url</a>|; |
223 |
server::debug( $debug ) if $1 eq 'debug'; |
server::debug( $debug ) if $1 eq 'debug'; |
224 |
} elsif ( $path =~ m{^/start_stop/(\S+)} ) { |
} elsif ( $path =~ m{^/start_stop/(\S+)} ) { |
225 |
print $client $redirect, start_stop($1); |
print $client $redirect, daemons::start_stop($1); |
226 |
} elsif ( $path =~ m{^/action/([^/]+)/(.+)} ) { |
} elsif ( $path =~ m{^/action/([^/]+)/(.+)} ) { |
227 |
$1->$2(); |
$1->$2(); |
228 |
print $client $redirect; |
print $client $redirect; |
236 |
|
|
237 |
} |
} |
238 |
|
|
|
use browser; |
|
|
use network; |
|
|
|
|
239 |
sub start { |
sub start { |
240 |
|
|
241 |
warn 'tap ', network::tap(); |
warn 'tap ', network::tap(); |
242 |
|
|
243 |
my $server = IO::Socket::INET->new( |
my $server = IO::Socket::INET->new( |
244 |
Proto => 'tcp', |
Proto => 'tcp', |
245 |
|
LocalAddr => $server::ip, |
246 |
LocalPort => $httpd::port, |
LocalPort => $httpd::port, |
247 |
Listen => SOMAXCONN, |
Listen => SOMAXCONN, |
248 |
Reuse => 1 |
Reuse => 1 |
250 |
|
|
251 |
print "url $url\n"; |
print "url $url\n"; |
252 |
|
|
253 |
start_stop 'browser', $url; |
daemons::start_stop 'browser', $url; |
254 |
start_stop 'dhcpd'; |
daemons::start_stop $_ foreach ( qw/dhcpd tftpd dnsd/ ); |
255 |
start_stop 'tftpd'; |
daemons::start_stop 'kvm' unless $ENV{DEV}; # skip kvm statup when running on real device |
|
start_stop 'dnsd'; |
|
|
start_stop 'kvm' unless $ENV{DEV}; # skip kvm statup when running on real device |
|
256 |
|
|
257 |
while (1) { |
while (1) { |
258 |
my $client = $server->accept() || next; # ALARM trickle us |
my $client = $server->accept() || next; # ALARM trickle us |