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 $url = "http://$server::ip:$port/"; |
our $debug = server::debug; |
37 |
|
our $url = "http://$server::ip:$port"; |
38 |
|
|
39 |
use html; |
use html; |
40 |
|
our $static_pids; |
41 |
|
|
42 |
|
use Time::HiRes qw/time/; |
43 |
|
|
44 |
sub static { |
sub static { |
45 |
my ($client,$path) = @_; |
my ($client,$path) = @_; |
46 |
|
|
47 |
$path = "tftp/$path"; |
my $full = "$server::base_dir/tftp/$path"; |
48 |
|
|
49 |
|
return if ! -f $full; |
50 |
|
|
51 |
if ( ! -e $path || -d $path ) { |
my $start_t = time(); |
52 |
print $client "HTTP/1.0 404 $path not found\r\n"; |
|
53 |
return; |
if ( my $pid = fork ) { |
54 |
|
# parent |
55 |
|
close($client); |
56 |
|
print "http static child $pid\n"; |
57 |
|
$static_pids->{$pid} = $path; |
58 |
|
return 1; |
59 |
} |
} |
60 |
|
|
61 |
my $type = 'text/plain'; |
my $type = 'application/octet-stream'; |
62 |
$type = 'text/html' if $path =~ m{\.htm}; |
$type = 'text/html' if $path =~ m{\.htm}; |
63 |
$type = 'application/javascript' if $path =~ m{\.js}; |
$type = 'application/javascript' if $path =~ m{\.js}; |
64 |
|
$type = 'text/plain' if $path =~ m{\.txt}; |
65 |
|
|
66 |
|
my $size = -s $full || return; |
67 |
|
|
68 |
|
$client->autoflush(1); |
69 |
|
|
70 |
|
print $client "HTTP/1.0 200 OK\r\nContent-Type: $type\r\nContent-Length: $size\r\nConnection: close\r\n\r\n"; |
71 |
|
|
72 |
|
open(my $fh, $full); |
73 |
|
|
74 |
|
my $block = 1400; # try not to fragment packages (pxelinux seems to have problems with it) |
75 |
|
my $buff; |
76 |
|
my $pos = 0; |
77 |
|
|
78 |
print $client "HTTP/1.0 200 OK\r\nContent-Type: $type\r\nContent-Length: ", -s $path,"\r\n\r\n"; |
print "static $path $type $size block: $block\n"; |
79 |
open(my $html, $path); |
|
80 |
while(<$html>) { |
while( my $len = read $fh, $buff, $block ) { |
81 |
print $client $_; |
print $client $buff; |
82 |
|
$pos += $len; |
83 |
|
printf "%s %d/%d %.2f%% %.2f K/s\r" |
84 |
|
, $path, $pos |
85 |
|
, $size, $pos * 100 / $size |
86 |
|
, ( $pos / 1024 ) / ( time() - $start_t ) |
87 |
|
; |
88 |
} |
} |
89 |
close($html); |
close($fh); |
90 |
|
close($client); |
91 |
|
|
92 |
return $path; |
print "\n"; |
|
} |
|
93 |
|
|
94 |
my $ok = "HTTP/1.0 200 OK\r\nContent-Type: text/html\r\nConnection: close\r\n\r\n"; |
exit; |
95 |
|
} |
96 |
|
|
97 |
use boolean; |
use boolean; |
98 |
|
|
99 |
|
use screen; |
100 |
|
use kvm; |
101 |
|
|
102 |
|
$SIG{CHLD} = 'IGNORE'; |
103 |
|
|
104 |
|
sub start_stop { |
105 |
|
my $daemon = shift; |
106 |
|
my $pid = $pids->{$daemon}; |
107 |
|
|
108 |
|
warn "start_stop $daemon $pid\n"; |
109 |
|
|
110 |
|
if ( $pid =~ m{^\d+$} ) { |
111 |
|
my $pstree = `pstree -p $pid`; |
112 |
|
my @pids = $pstree =~ m{\((\d+)\)}g; |
113 |
|
warn "pstree $pstree pids ",dump( @pids ); |
114 |
|
kill 1, $_ foreach reverse @pids; |
115 |
|
$pids->{$daemon} = 'stopped'; |
116 |
|
return qq|$daemon pid $pid stopped|; |
117 |
|
} else { |
118 |
|
if ( $pid = fork ) { |
119 |
|
# parent |
120 |
|
$pids->{$daemon} = $pid; |
121 |
|
warn "forked $daemon $pid\n"; |
122 |
|
return qq|$daemon pid $pid started|; |
123 |
|
} elsif ( defined $pid ) { |
124 |
|
# child |
125 |
|
my $invoke = 'start'; |
126 |
|
$invoke = $1 if $daemon =~ s{/(.+)}{}; |
127 |
|
my $eval = $daemon . '::' . $invoke . '(' . ( @_ ? dump(@_) : '' ) . ')'; |
128 |
|
warn "eval $eval"; |
129 |
|
eval $eval; |
130 |
|
warn "can't start $daemon: $@" if $@; |
131 |
|
exit; |
132 |
|
} else { |
133 |
|
die "fork error $!"; |
134 |
|
} |
135 |
|
} |
136 |
|
} |
137 |
|
|
138 |
|
my $ok = qq|HTTP/1.0 200 OK\r\nContent-Type: text/html\r\nConnection: close\r\n\r\n|; |
139 |
|
my $redirect = qq|HTTP/1.1 302 Found\r\nContent-type: text/html\r\nLocation: $url\r\n\r\n|; |
140 |
|
|
141 |
sub get_request { |
sub get_request { |
142 |
my ( $client, $path, $param ) = @_; |
my ( $client, $path, $param ) = @_; |
143 |
|
|
144 |
warn "get_request $client $path $param"; |
warn "get_request $path ", $param ? dump( $param ) : '', "\n"; |
145 |
|
|
146 |
if ( my $found = static( $client,$path ) ) { |
if ( my $found = static( $client,$path ) ) { |
147 |
warn "static $found" if $debug; |
warn "static $found" if $debug; |
148 |
} elsif ( $path eq '/' ) { |
} elsif ( $path eq '/' ) { |
149 |
print $client $ok, |
|
150 |
html::table( 2, |
my $screen = $pids->{screen} ? qq|stop <tt>$pids->{screen}</tt>| : 'start'; |
151 |
'pid', $$, |
my $kvm = $pids->{kvm} ? qq|stop <tt>$pids->{kvm}</tt>| : |
152 |
'debug', qq|<a href=/our/debug/| . boolean::toggle($debug) . qq|>$debug</a>|, |
$pids->{screen} ? qq|start| : qq|start screen first|; |
153 |
|
|
154 |
|
my @rows = ( |
155 |
|
'ip', html::tt( $server::ip ), |
156 |
|
'netmask', html::tt( $server::netmask ), |
157 |
|
|
158 |
|
'debug', qq|<a href=/our/debug/| . boolean::toggle($debug) . qq|>$debug</a>|, |
159 |
); |
); |
160 |
|
|
161 |
|
my $debug_proc = ''; |
162 |
|
|
163 |
|
warn 'pids: ', dump( $pids ) if $debug; |
164 |
|
foreach my $name ( sort keys %$pids ) { |
165 |
|
my $pid = $pids->{$name} || next; |
166 |
|
|
167 |
|
my $html = qq|<a href=/start_stop/$name>$pid</a>|; |
168 |
|
|
169 |
|
my $proc = "/proc/$pid/status"; |
170 |
|
|
171 |
|
if ( -e $proc ) { |
172 |
|
if ( $debug ) { |
173 |
|
$html .= qq| <a name=$pid href=#proc-$pid>?</a>|; |
174 |
|
|
175 |
|
$debug_proc |
176 |
|
.= qq|<a name=proc-$pid href=#$pid>$proc</a><pre style="font-size: 10%">| |
177 |
|
. read_file($proc) |
178 |
|
. qq|</pre>| |
179 |
|
; |
180 |
|
} |
181 |
|
|
182 |
|
if ( $name->can('start_fork') ) { |
183 |
|
$html .= qq| <a href=/start_stop/kvm/$_>$_</a>| foreach $name->start_fork; |
184 |
|
} |
185 |
|
|
186 |
|
if ( $name->can('actions') ) { |
187 |
|
$html .= qq| <a href=/action/kvm/$_>$_</a>| foreach $name->actions; |
188 |
|
} |
189 |
|
} |
190 |
|
|
191 |
|
push @rows, ( $name => $html ); |
192 |
|
} |
193 |
|
|
194 |
|
my $below_table = ''; |
195 |
|
|
196 |
|
warn 'static_pids: ', dump( $static_pids ) if $debug; |
197 |
|
foreach my $pid ( keys %$static_pids ) { |
198 |
|
my $path = $static_pids->{$pid}; |
199 |
|
if ( -d "/proc/$pid" ) { |
200 |
|
push @rows, ( $path => qq|<a href=/kill/static/$pid>$pid</a>| ); |
201 |
|
} elsif ( $param->{clean_completed_downloads} ) { |
202 |
|
delete $static_pids->{$pid} |
203 |
|
} else { |
204 |
|
push @rows, ( $path => "$pid competed" ); |
205 |
|
$below_table = qq|<a href="/?clean_completed_downloads=1">clean completed downloads</a>|; |
206 |
|
} |
207 |
|
} |
208 |
|
|
209 |
|
print $client $ok |
210 |
|
, html::table( 2, @rows ) |
211 |
|
, $below_table |
212 |
|
, html::tabs( log::mac_changes ) |
213 |
|
, $debug_proc |
214 |
|
; |
215 |
|
|
216 |
} elsif ( $path =~ m{^/our/(\w+)/(\S+)} ) { |
} elsif ( $path =~ m{^/our/(\w+)/(\S+)} ) { |
217 |
eval 'our $' . $1 . ' = ' . $2; |
eval 'our $' . $1 . ' = ' . $2; |
218 |
warn $@ if $@; |
warn $@ if $@; |
219 |
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>|; |
220 |
|
server::debug( $debug ) if $1 eq 'debug'; |
221 |
|
} elsif ( $path =~ m{^/start_stop/((?:screen|kvm).*)} ) { # XXX we don't want to stop all classes |
222 |
|
print $client $redirect, start_stop($1); |
223 |
|
} elsif ( $path =~ m{^/action/([^/]+)/(.+)} ) { |
224 |
|
$1->$2(); |
225 |
|
print $client $redirect; |
226 |
|
} elsif ( $path =~ m{^/kill/static/(\d+)} ) { |
227 |
|
print $client $redirect; |
228 |
|
kill 1, $1 || kill 9, $2 && warn "killed $1"; |
229 |
|
} elsif ( $path eq '/exit' ) { |
230 |
|
# DESTROY; |
231 |
|
exit 0; |
232 |
} elsif ( $path =~ m{/boot} ) { |
} elsif ( $path =~ m{/boot} ) { |
233 |
print $client qq{$ok |
print $client qq{$ok |
234 |
#!gpxe |
#!gpxe |
243 |
} |
} |
244 |
|
|
245 |
} |
} |
246 |
|
|
247 |
|
use browser; |
248 |
|
|
249 |
sub start { |
sub start { |
250 |
|
|
251 |
my $server = IO::Socket::INET->new( |
my $server = IO::Socket::INET->new( |
257 |
|
|
258 |
print "url $url\n"; |
print "url $url\n"; |
259 |
|
|
260 |
system "/mnt/llin/rest/cvs/uzbl/uzbl -u $url &"; |
start_stop 'browser', $url; |
261 |
|
start_stop 'screen'; |
262 |
|
start_stop 'kvm'; |
263 |
|
|
264 |
while (my $client = $server->accept()) { |
while (my $client = $server->accept()) { |
265 |
$client->autoflush(1); |
$client->autoflush(1); |
279 |
} |
} |
280 |
warn "param: ",dump( $param ) if $debug; |
warn "param: ",dump( $param ) if $debug; |
281 |
} |
} |
|
warn "path $path param: ",dump( $param ); |
|
282 |
get_request $client, $path, $param; |
get_request $client, $path, $param; |
283 |
} else { |
} else { |
284 |
print $client "HTTP/1.0 500 No method\r\nConnection: close\r\nContent-type: text/plain\r\n\r\n500 $request"; |
print $client "HTTP/1.0 500 No method\r\nConnection: close\r\nContent-type: text/plain\r\n\r\n500 $request"; |
288 |
print $client qq{ |
print $client qq{ |
289 |
<div style="font-size: 80%; color: #888"> |
<div style="font-size: 80%; color: #888"> |
290 |
<a href="">reload</a> |
<a href="">reload</a> |
291 |
<a href="/">index</a> |
<a href=/>index</a> |
292 |
|
<a href=/exit>exit</a> |
293 |
</div> |
</div> |
294 |
} if $client->connected; |
} if $client->connected; |
295 |
|
|
298 |
die "server died"; |
die "server died"; |
299 |
} |
} |
300 |
|
|
301 |
|
warn "loaded"; |
302 |
|
|
303 |
1; |
1; |