/[pxelator]/lib/PXElator/httpd.pm
This is repository of my old source code which isn't updated any more. Go to git.rot13.org for current projects!
ViewVC logotype

Diff of /lib/PXElator/httpd.pm

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

revision 66 by dpavlin, Thu Jul 30 20:15:39 2009 UTC revision 78 by dpavlin, Fri Jul 31 14:43:30 2009 UTC
# Line 19  use File::Slurp; Line 19  use File::Slurp;
19  use IO::Socket::INET;  use IO::Socket::INET;
20  use Module::Refresh;  use Module::Refresh;
21    
22  our $pids = { httpd => $$ };  our $pids;
23    $pids = { httpd => $$ } unless defined $pids; # keep pids on refresh
24    
25  sub DESTROY {  sub DESTROY {
26          warn "pids ",dump( $pids );          warn "pids ",dump( $pids );
# Line 30  sub DESTROY { Line 31  sub DESTROY {
31  }  }
32    
33  our $port = 7777;  our $port = 7777;
 our $debug = 0;  
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) = @_;
# Line 44  sub static { Line 46  sub static {
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};
# Line 70  sub static { Line 80  sub static {
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;
# Line 86  sub start_stop { Line 94  sub start_stop {
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                    warn "pstree $pstree pids ",dump( @pids );
103                    kill 1, $_ foreach reverse @pids;
104                  $pids->{$daemon} = 'stopped';                  $pids->{$daemon} = 'stopped';
105                  return qq|$daemon pid $pid stopped|;                  return qq|$daemon pid $pid stopped|;
106          } else {          } else {
# Line 101  sub start_stop { Line 111  sub start_stop {
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(' . dump(@_) . ')';                          my $eval = $daemon . '::start(' . ( @_ ? dump(@_) : '' ) . ')';
115                          warn "eval $eval";                          warn "eval $eval";
116                          eval $eval;                          eval $eval;
117                          warn "can't start $daemon: $@" if $@;                          warn "can't start $daemon: $@" if $@;
# Line 112  sub start_stop { Line 122  sub start_stop {
122          }          }
123  }  }
124    
125    my $ok =       qq|HTTP/1.0 200 OK\r\nContent-Type: text/html\r\nConnection: close\r\n\r\n|;
126    my $redirect = qq|HTTP/1.1 302 Found\r\nContent-type: text/html\r\nLocation: $url\r\n\r\n|;
127    
128  sub get_request {  sub get_request {
129          my ( $client, $path, $param ) = @_;          my ( $client, $path, $param ) = @_;
130    
# Line 131  sub get_request { Line 144  sub get_request {
144    
145                          'debug',        qq|<a href=/our/debug/| . boolean::toggle($debug) . qq|>$debug</a>|,                          'debug',        qq|<a href=/our/debug/| . boolean::toggle($debug) . qq|>$debug</a>|,
146                  );                  );
147                  foreach my $name ( %$pids ) {  
148                    my $debug_proc;
149    
150                    warn 'pids: ', dump( $pids ) if $debug;
151                    foreach my $name ( keys %$pids ) {
152                          my $pid = $pids->{$name} || next;                          my $pid = $pids->{$name} || next;
153    
154                          my $html = qq|<a href=/$name>$pid</a>|;                          my $html = qq|<a href=/$name>$pid</a>|;
155                          $html   .= qq|<pre style="font-size: 10%">|  
156                                          .  ( $debug ? read_file("/proc/$pid/status") : '' )                          if ( $debug ) {
157                                          .  qq|</pre>| if $debug;                                  $html .= qq| <a name=$pid href=#proc-$pid>?</a>|;
158    
159                                    my $proc = "/proc/$pid/status";
160                                    $debug_proc
161                                            .= qq|<a name=proc-$pid href=#$pid>$proc</a><pre style="font-size: 10%">|
162                                            .  read_file($proc)
163                                            .  qq|</pre>|
164                                            if -e $proc;
165                            }
166    
167                          push @rows, ( $name => $html );                          push @rows, ( $name => $html );
168                  }                  }
169    
170                  print $client $ok, html::table( 2, @rows );                  warn 'static_pids: ', dump( $static_pids ) if $debug;
171                    foreach my $pid ( keys %$static_pids ) {
172                            my $path = $static_pids->{$pid};
173                            if ( -d "/proc/$pid" ) {
174                                    push @rows, ( $path => qq|<a href=/kill/static/$pid>$pid</a>| );
175                            } else {
176                                    push @rows, ( $path => "$pid competed" );
177                            }
178                    }
179    
180                    print $client $ok
181                            , html::table( 2, @rows )
182                            , html::tabs( log::mac_changes )
183                            , $debug_proc
184                            ;
185    
186          } elsif ( $path =~ m{^/our/(\w+)/(\S+)} ) {          } elsif ( $path =~ m{^/our/(\w+)/(\S+)} ) {
187                  eval 'our $' . $1 . ' = ' . $2;                  eval 'our $' . $1 . ' = ' . $2;
188                  warn $@ if $@;                  warn $@ if $@;
189                  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>|;
190                    server::debug( $debug ) if $1 eq 'debug';
191          } elsif ( $path =~ m{^/(screen|kvm)} ) {          } elsif ( $path =~ m{^/(screen|kvm)} ) {
192                  print $client $ok, start_stop($1);                  print $client $redirect, start_stop($1);
193            } elsif ( $path =~ m{^/kill/static/(\d+)} ) {
194                    print $client $redirect;
195                    kill 9, $1 && warn "killed $1";
196          } elsif ( $path eq '/exit' ) {          } elsif ( $path eq '/exit' ) {
197  #               DESTROY;  #               DESTROY;
198                  exit 0;                  exit 0;

Legend:
Removed from v.66  
changed lines
  Added in v.78

  ViewVC Help
Powered by ViewVC 1.1.26