/[pxelator]/lib/PXElator/daemons.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

Annotation of /lib/PXElator/daemons.pm

Parent Directory Parent Directory | Revision Log Revision Log


Revision 207 - (hide annotations)
Wed Aug 12 22:56:45 2009 UTC (14 years, 8 months ago) by dpavlin
File size: 1733 byte(s)
first verision which boots virtual machine logging using new CouchDB::audit code

it need some more work to make it less ugly (especially console output) and
less redundant/useful, but it's a start

1 dpavlin 185 package daemons;
2    
3     use warnings;
4     use strict;
5    
6     use File::Slurp;
7     use Data::Dump qw/dump/;
8    
9     use x11;
10    
11     our $pids;
12 dpavlin 207 $pids = { httpd => $$, kvm => 'not started' } unless defined $pids; # keep pids on refresh
13 dpavlin 185
14     sub DESTROY {
15     warn "pids ",dump( $pids );
16     foreach ( values %$pids ) {
17     warn "kill $_";
18     kill 1,$_ || kill 9, $_;
19     }
20     }
21    
22     $SIG{CHLD} = 'IGNORE';
23    
24     sub start_stop {
25     my $daemon = shift;
26    
27     my $pid = $pids->{$daemon};
28     my $pid_path = "$server::conf/$daemon.pid";
29    
30     if ( ! $pid && -e $pid_path ) {
31     my $p = read_file $pid_path;
32     if ( kill 0, $p ) {
33     warn "adopted $daemon $p\n";
34     return $pids->{$daemon} = $p;
35     } else {
36     warn "old $daemon $p not running";
37     unlink $pid_path;
38     }
39     }
40    
41     $pid ||= 'not started';
42     warn "start_stop $daemon $pid\n";
43    
44     if ( $pid =~ m{^\d+$} ) {
45     my $pstree = `pstree -p $pid`;
46     my @pids = $pstree =~ m{\((\d+)\)}g;
47     warn "pstree $pstree pids ",dump( @pids );
48     kill 1, $_ foreach reverse @pids;
49     $pids->{$daemon} = 'stopped';
50     return qq|$daemon pid $pid stopped|;
51     } else {
52     if ( $pid = fork ) {
53     # parent
54     $pids->{$daemon} = $pid;
55     write_file $pid_path, $pid;
56     warn "forked $daemon $pid\n";
57     return qq|$daemon pid $pid started|;
58     } elsif ( defined $pid ) {
59     # child
60     my $invoke = 'start';
61     $invoke = $1 if $daemon =~ s{/(.+)}{};
62     if ( $daemon =~ m{dhcpd|tftpd|dnsd} ) {
63     my $exec = "perl -I$server::base_dir/lib -I$server::base_dir/lib/PXElator -M$daemon -e ${daemon}::${invoke}";
64     warn "exec $exec";
65     x11::xterm( $daemon => $exec );
66     } else {
67     my $eval = $daemon . '::' . $invoke . '(' . ( @_ ? dump(@_) : '' ) . ')';
68     warn "eval $eval";
69     eval $eval;
70     warn "can't start $daemon: $@" if $@;
71     }
72     exit;
73     } else {
74     die "fork error $!";
75     }
76     }
77     }
78    

  ViewVC Help
Powered by ViewVC 1.1.26