Skip to content
This repository
tree: 0e095f6969
Fetching contributors…

Cannot retrieve contributors at this time

file 191 lines (180 sloc) 4.585 kb
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191
#================
# FILE : KIWISharedMem.pm
#----------------
# PROJECT : OpenSUSE Build-Service
# COPYRIGHT : (c) 2006 SUSE LINUX Products GmbH, Germany
# :
# AUTHOR : Marcus Schaefer <ms@suse.de>
# :
# BELONGS TO : Operating System images
# :
# DESCRIPTION : This module is used to provide shared memory
# : operations used in the log server process
# : the segment will contain information used by
# : multiple processes
# :
# :
# STATUS : Development
#----------------
package KIWISharedMem;
#==========================================
# Modules
#------------------------------------------
use strict;
use Carp qw (cluck);
use IPC::SysV qw(IPC_PRIVATE IPC_RMID IPC_CREAT S_IRWXU);
use IPC::Semaphore;
use KIWIQX qw (qxx);
sub MAXBUF { 2000 }

#==========================================
# Constructor
#------------------------------------------
sub new {
# ...
# Create a new KIWISharedMem object which is used to allow
# operations on a shared memory segment
# ---
#==========================================
# Object setup
#------------------------------------------
my $this = {};
my $class = shift;
bless $this,$class;
#==========================================
# Module Parameters
#------------------------------------------
my $kiwi = shift;
my $value = shift;
#==========================================
# Constructor setup
#------------------------------------------
my $key = shmget(IPC_PRIVATE, MAXBUF, S_IRWXU);
if (! defined $key) {
$kiwi -> error ("shmget: $!");
$kiwi -> failed ();
return;
}
my $sem = IPC::Semaphore -> new (IPC_PRIVATE, 1, S_IRWXU | IPC_CREAT);
if (! defined $sem) {
$kiwi -> error ("IPC::Semaphore->new: $!");
$kiwi -> failed ();
return;
}
if (! $sem -> setval (0,1)) {
$kiwi -> error ("sem setval: $!");
$kiwi -> failed ();
return;
}
#==========================================
# Store object data
#------------------------------------------
$this->{kiwi} = $kiwi;
$this->{OWNER} = $$;
$this->{SHMKEY} = $key;
$this->{SEMA} = $sem;

#==========================================
# Store data to segment
#------------------------------------------
$this -> unlock;
$this -> put ($value);
return $this;
}

#==========================================
# get
#------------------------------------------
sub get {
my $this = shift;
$this -> lock;
my $value = $this -> peek(@_);
$this -> unlock;
return $value;
}

#==========================================
# peek
#------------------------------------------
sub peek {
my $this = shift;
my $kiwi = $this->{kiwi};
my $buff = '';
if (! shmread($this->{SHMKEY}, $buff, 0, MAXBUF)) {
$kiwi -> error ("shmread: $!");
$kiwi -> failed ();
return;
}
substr($buff, index($buff, "\0")) = '';
return $buff;
}

#==========================================
# put
#------------------------------------------
sub put {
my $this = shift;
$this -> lock;
$this -> poke(@_);
$this -> unlock;
}

#==========================================
# poke
#------------------------------------------
sub poke {
my ($this,$msg) = @_;
my $kiwi = $this->{kiwi};
if (! shmwrite($this->{SHMKEY}, $msg, 0, MAXBUF)) {
$kiwi -> error ("shmwrite: $!");
$kiwi -> failed ();
return;
}
return $this;
}

#==========================================
# lock
#------------------------------------------
sub lock {
my $this = shift;
my $kiwi = $this->{kiwi};
if (! $this->{SEMA}->op(0,-1,0)) {
$kiwi -> error ("semop: $!");
$kiwi -> failed ();
return;
}
return $this;
}

#==========================================
# unlock
#------------------------------------------
sub unlock {
my $this = shift;
my $kiwi = $this->{kiwi};
if (! $this->{SEMA}->op(0,1,0)) {
$kiwi -> error ("semop: $!");
$kiwi -> failed ();
return;
}
return $this;
}

#==========================================
# closeSegment
#------------------------------------------
sub closeSegment {
my $this = shift;
my $kiwi = $this->{kiwi};
if (! defined $this->{OWNER}) {
$this->{OWNER} = 0;
}
return unless $this->{OWNER} == $$; # avoid dup dealloc
if (! shmctl($this->{SHMKEY}, IPC_RMID, 0)) {
$kiwi -> loginfo ("shmctl RMID: $!\n");
}
if (! $this->{SEMA}->remove()) {
$kiwi -> loginfo ("sema->remove: $!\n");
}
return $this;
}

#==========================================
# DESTRUCTOR
#------------------------------------------
sub DESTROY {
my $this = shift;
$this -> closeSegment();
}

1;
Something went wrong with that request. Please try again.