19 |
use IO::Socket::INET; |
use IO::Socket::INET; |
20 |
use Module::Refresh; |
use Module::Refresh; |
21 |
|
|
22 |
|
our $pids; |
23 |
|
$pids = { httpd => $$ } unless defined $pids; # keep pids on refresh |
24 |
|
|
25 |
|
sub DESTROY { |
26 |
|
warn "pids ",dump( $pids ); |
27 |
|
foreach ( values %$pids ) { |
28 |
|
warn "kill $_"; |
29 |
|
kill 1,$_ || kill 9, $_; |
30 |
|
} |
31 |
|
} |
32 |
|
|
33 |
our $port = 7777; |
our $port = 7777; |
|
our $debug = 1; |
|
34 |
|
|
35 |
use server; |
use server; |
36 |
|
our $debug = server::debug; |
37 |
our $url = "http://$server::ip:$port"; |
our $url = "http://$server::ip:$port"; |
38 |
|
|
39 |
use html; |
use html; |
40 |
|
our $static_pids; |
41 |
|
|
42 |
sub static { |
sub static { |
43 |
my ($client,$path) = @_; |
my ($client,$path) = @_; |
46 |
|
|
47 |
return if ! -f $full; |
return if ! -f $full; |
48 |
|
|
49 |
|
if ( my $pid = fork ) { |
50 |
|
# parent |
51 |
|
close($client); |
52 |
|
print "http static child $pid\n"; |
53 |
|
$static_pids->{$pid} = $path; |
54 |
|
return 1; |
55 |
|
} |
56 |
|
|
57 |
my $type = 'application/octet-stream'; |
my $type = 'application/octet-stream'; |
58 |
$type = 'text/html' if $path =~ m{\.htm}; |
$type = 'text/html' if $path =~ m{\.htm}; |
59 |
$type = 'application/javascript' if $path =~ m{\.js}; |
$type = 'application/javascript' if $path =~ m{\.js}; |
80 |
|
|
81 |
print "$path $pos == $size OK\n"; |
print "$path $pos == $size OK\n"; |
82 |
|
|
83 |
return $path; |
exit; |
84 |
} |
} |
85 |
|
|
|
my $ok = "HTTP/1.0 200 OK\r\nContent-Type: text/html\r\nConnection: close\r\n\r\n"; |
|
|
|
|
86 |
use boolean; |
use boolean; |
87 |
|
|
88 |
use screen; |
use screen; |
89 |
use kvm; |
use kvm; |
|
our $pids; |
|
90 |
|
|
91 |
$SIG{CHLD} = 'IGNORE'; |
$SIG{CHLD} = 'IGNORE'; |
92 |
|
|
94 |
my $daemon = shift; |
my $daemon = shift; |
95 |
my $pid = $pids->{$daemon}; |
my $pid = $pids->{$daemon}; |
96 |
|
|
97 |
warn "start_stop $daemon pids: ",dump( $pids ); |
warn "start_stop $daemon $pid pids: ",dump( $pids ); |
98 |
|
|
99 |
if ( $pid ) { |
if ( $pid =~ m{^\d+$} ) { |
100 |
warn "kill 9 $pid"; |
my $pstree = `pstree -p $pid`; |
101 |
kill 9, $pid; |
my @pids = $pstree =~ m{\((\d+)\)}g; |
102 |
delete $pids->{$daemon}; |
warn "pstree $pstree pids ",dump( @pids ); |
103 |
|
kill 1, $_ foreach reverse @pids; |
104 |
|
$pids->{$daemon} = 'stopped'; |
105 |
return qq|$daemon pid $pid stopped|; |
return qq|$daemon pid $pid stopped|; |
106 |
} else { |
} else { |
107 |
if ( $pid = fork ) { |
if ( $pid = fork ) { |
111 |
return qq|$daemon pid $pid started|; |
return qq|$daemon pid $pid started|; |
112 |
} elsif ( defined $pid ) { |
} elsif ( defined $pid ) { |
113 |
# child |
# child |
114 |
my $eval = $daemon . '::start'; |
my $invoke = 'start'; |
115 |
|
$invoke = $1 if $daemon =~ s{/(.+)}{}; |
116 |
|
my $eval = $daemon . '::' . $invoke . '(' . ( @_ ? dump(@_) : '' ) . ')'; |
117 |
warn "eval $eval"; |
warn "eval $eval"; |
118 |
eval $eval; |
eval $eval; |
119 |
warn "can't start $daemon: $@" if $@; |
warn "can't start $daemon: $@" if $@; |
124 |
} |
} |
125 |
} |
} |
126 |
|
|
127 |
|
my $ok = qq|HTTP/1.0 200 OK\r\nContent-Type: text/html\r\nConnection: close\r\n\r\n|; |
128 |
|
my $redirect = qq|HTTP/1.1 302 Found\r\nContent-type: text/html\r\nLocation: $url\r\n\r\n|; |
129 |
|
|
130 |
sub get_request { |
sub get_request { |
131 |
my ( $client, $path, $param ) = @_; |
my ( $client, $path, $param ) = @_; |
132 |
|
|
140 |
my $kvm = $pids->{kvm} ? qq|stop <tt>$pids->{kvm}</tt>| : |
my $kvm = $pids->{kvm} ? qq|stop <tt>$pids->{kvm}</tt>| : |
141 |
$pids->{screen} ? qq|start| : qq|start screen first|; |
$pids->{screen} ? qq|start| : qq|start screen first|; |
142 |
|
|
143 |
print $client $ok, |
my @rows = ( |
|
html::table( 2, |
|
|
'pid', html::tt( $$ ), |
|
144 |
'ip', html::tt( $server::ip ), |
'ip', html::tt( $server::ip ), |
145 |
'netmask', html::tt( $server::netmask ), |
'netmask', html::tt( $server::netmask ), |
146 |
|
|
147 |
'debug', qq|<a href=/our/debug/| . boolean::toggle($debug) . qq|>$debug</a>|, |
'debug', qq|<a href=/our/debug/| . boolean::toggle($debug) . qq|>$debug</a>|, |
|
'screen', qq|<a href=/screen>$screen</a>|, |
|
|
'kvm', qq|<a href=/kvm>$kvm</a>|, |
|
148 |
); |
); |
149 |
|
|
150 |
|
my $debug_proc; |
151 |
|
|
152 |
|
warn 'pids: ', dump( $pids ) if $debug; |
153 |
|
foreach my $name ( sort keys %$pids ) { |
154 |
|
my $pid = $pids->{$name} || next; |
155 |
|
|
156 |
|
my $html = qq|<a href=/start_stop/$name>$pid</a>|; |
157 |
|
|
158 |
|
my $proc = "/proc/$pid/status"; |
159 |
|
|
160 |
|
if ( -e $proc ) { |
161 |
|
if ( $debug ) { |
162 |
|
$html .= qq| <a name=$pid href=#proc-$pid>?</a>|; |
163 |
|
|
164 |
|
$debug_proc |
165 |
|
.= qq|<a name=proc-$pid href=#$pid>$proc</a><pre style="font-size: 10%">| |
166 |
|
. read_file($proc) |
167 |
|
. qq|</pre>| |
168 |
|
; |
169 |
|
} |
170 |
|
|
171 |
|
if ( $name->can('start_fork') ) { |
172 |
|
$html .= qq| <a href=/start_stop/kvm/$_>$_</a>| foreach $name->start_fork; |
173 |
|
} |
174 |
|
|
175 |
|
if ( $name->can('actions') ) { |
176 |
|
$html .= qq| <a href=/action/kvm/$_>$_</a>| foreach $name->actions; |
177 |
|
} |
178 |
|
} |
179 |
|
|
180 |
|
push @rows, ( $name => $html ); |
181 |
|
} |
182 |
|
|
183 |
|
warn 'static_pids: ', dump( $static_pids ) if $debug; |
184 |
|
foreach my $pid ( keys %$static_pids ) { |
185 |
|
my $path = $static_pids->{$pid}; |
186 |
|
if ( -d "/proc/$pid" ) { |
187 |
|
push @rows, ( $path => qq|<a href=/kill/static/$pid>$pid</a>| ); |
188 |
|
} else { |
189 |
|
push @rows, ( $path => "$pid competed" ); |
190 |
|
} |
191 |
|
} |
192 |
|
|
193 |
|
print $client $ok |
194 |
|
, html::table( 2, @rows ) |
195 |
|
, html::tabs( log::mac_changes ) |
196 |
|
, $debug_proc |
197 |
|
; |
198 |
|
|
199 |
} elsif ( $path =~ m{^/our/(\w+)/(\S+)} ) { |
} elsif ( $path =~ m{^/our/(\w+)/(\S+)} ) { |
200 |
eval 'our $' . $1 . ' = ' . $2; |
eval 'our $' . $1 . ' = ' . $2; |
201 |
warn $@ if $@; |
warn $@ if $@; |
202 |
print $client qq|HTTP/1.1 302 Found\r\nLocation: $url\r\nContent-type: text/html\r\n\r\n<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>|; |
203 |
} elsif ( $path =~ m{^/(screen|kvm)} ) { |
server::debug( $debug ) if $1 eq 'debug'; |
204 |
print $client $ok, start_stop($1); |
} elsif ( $path =~ m{^/start_stop/((?:screen|kvm).*)} ) { # XXX we don't want to stop all classes |
205 |
|
print $client $redirect, start_stop($1); |
206 |
|
} elsif ( $path =~ m{^/action/([^/]+)/(.+)} ) { |
207 |
|
$1->$2(); |
208 |
|
print $client $redirect; |
209 |
|
} elsif ( $path =~ m{^/kill/static/(\d+)} ) { |
210 |
|
print $client $redirect; |
211 |
|
kill 1, $1 || kill 9, $2 && warn "killed $1"; |
212 |
|
} elsif ( $path eq '/exit' ) { |
213 |
|
# DESTROY; |
214 |
|
exit 0; |
215 |
} elsif ( $path =~ m{/boot} ) { |
} elsif ( $path =~ m{/boot} ) { |
216 |
print $client qq{$ok |
print $client qq{$ok |
217 |
#!gpxe |
#!gpxe |
227 |
|
|
228 |
} |
} |
229 |
|
|
230 |
|
use browser; |
231 |
|
|
232 |
sub start { |
sub start { |
233 |
|
|
234 |
my $server = IO::Socket::INET->new( |
my $server = IO::Socket::INET->new( |
240 |
|
|
241 |
print "url $url\n"; |
print "url $url\n"; |
242 |
|
|
243 |
system "/mnt/llin/rest/cvs/uzbl/uzbl -u $url &"; |
start_stop 'browser', $url; |
244 |
|
start_stop 'screen'; |
245 |
|
start_stop 'kvm'; |
246 |
|
|
247 |
while (my $client = $server->accept()) { |
while (my $client = $server->accept()) { |
248 |
$client->autoflush(1); |
$client->autoflush(1); |
272 |
print $client qq{ |
print $client qq{ |
273 |
<div style="font-size: 80%; color: #888"> |
<div style="font-size: 80%; color: #888"> |
274 |
<a href="">reload</a> |
<a href="">reload</a> |
275 |
<a href="/">index</a> |
<a href=/>index</a> |
276 |
|
<a href=/exit>exit</a> |
277 |
</div> |
</div> |
278 |
} if $client->connected; |
} if $client->connected; |
279 |
|
|