Show
Ignore:
Timestamp:
05/11/10 22:00:27 (5 years ago)
Author:
Mark Harrison <mark@omniti.com>
git-committer:
Mark Harrison <mark@omniti.com> 1273615227 +0000
git-parent:

[1ae78969a80271c40db2faa5d7182d26082b71c3]

git-author:
Mark Harrison <mark@omniti.com> 1273615227 +0000
Message:

Remove the sysv IPC shared memory code in favor of using files instead.

This fixes an issue with resmon not correctly cleaning up the shared memory
segments when it dies.

Currently no locking is done on the file to prevent reading of a partial
status dump. This needs fixing.

Also, the file is deleted immediately after opening. This deals with the issue
of having a stale file around if resmon dies and also limits the ability of
somebody to inject invalid data into the file.

git-svn-id: https://labs.omniti.com/resmon/branches/resmon2@367 8c0face9-b7db-6ec6-c4b3-d5f7145c7d55

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • lib/Resmon/Status.pm

    r1ae7896 r98ad146  
    99use Socket; 
    1010use Fcntl qw/:flock/; 
    11 use IPC::SysV qw /IPC_PRIVATE IPC_CREAT IPC_RMID S_IRWXU S_IRWXG S_IRWXO/; 
    1211use Data::Dumper; 
    1312 
    14 my $SEGSIZE = 1024*256; 
    1513my $KEEPALIVE_TIMEOUT = 5; 
    1614my $REQUEST_TIMEOUT = 60; 
     
    1816    my $class = shift; 
    1917    my $file = shift; 
     18    my $fh = IO::File->new(".$file.state", "+>"); 
     19    # Delete the just opened file - it stays open, but doesn't show on disk 
     20    unlink ".$file.state"; 
    2021    return bless { 
    21         file => $file 
     22        file => $file, 
     23        shared_state => $fh 
    2224    }, $class; 
    2325} 
     
    2527sub get_shared_state { 
    2628    my $self = shift; 
    27     my $blob; 
    28     my $len; 
    29     return unless(defined($self->{shared_state})); 
    30     # Lock shared segment 
    31     # Read in 
    32     shmread($self->{shared_state}, $len, 0, length(pack('i', 0))); 
    33     $len = unpack('i', $len); 
    34     shmread($self->{shared_state}, $blob, length(pack('i', 0)), $len); 
    35     # unlock 
    36     my $VAR1; 
    37     eval $blob; 
    38     die $@ if ($@); 
    39     $self->{store} = $VAR1; 
     29    my $fh = $self->{shared_state}; 
     30    if (defined $fh) { 
     31        my $VAR1; 
     32        $fh->seek(0, 0); 
     33        my $blob; 
     34        { 
     35            local $/ = undef; 
     36            $blob = <$fh>; 
     37        } 
     38        eval $blob; 
     39        die $@ if ($@); 
     40        $self->{store} = $VAR1; 
     41    } else { 
     42        die "Unable to read shared state"; 
     43    }; 
    4044    return $self->{store}; 
    4145} 
     
    4347sub store_shared_state { 
    4448    my $self = shift; 
    45     return unless(defined($self->{shared_state})); 
    46     my $blob = Dumper($self->{store}); 
    47  
    48     # Lock shared segment 
    49     # Write state and flush 
    50     shmwrite($self->{shared_state}, pack('i', length($blob)), 
    51         0, length(pack('i', 0))) || die "$!"; 
    52     shmwrite($self->{shared_state}, $blob, length(pack('i', 0)), 
    53         length($blob)) || die "$!"; 
    54     # unlock 
     49    my $fh = $self->{shared_state}; 
     50    if (defined($fh)) { 
     51        $fh->truncate(0); 
     52        $fh->seek(0,0); 
     53        print $fh Dumper($self->{store}); 
     54        $fh->flush(); 
     55    } else { 
     56        die "Unable to store shared state"; 
     57    }; 
    5558} 
    5659 
     
    468471    chmod 0644, "$self->{file}.swap"; 
    469472 
    470     unless(defined($self->{shared_state})) { 
    471         $self->{shared_state} = shmget(IPC_PRIVATE, $SEGSIZE, 
    472             IPC_CREAT|S_IRWXU|S_IRWXG|S_IRWXO); 
    473         die "$0: $!" unless (defined $self->{shared_state}); 
    474     } 
    475473    return 1; 
    476474} 
     
    531529        waitpid(-1,WNOHANG); 
    532530    } 
    533     if(defined($self->{shared_state})) { 
    534         shmctl($self->{shared_state}, IPC_RMID, 0); 
    535     } 
    536531} 
    5375321;