| 1 | #////////////////////////////////////////// | ||
| 2 | package Log::Log4perl::Util::Semaphore; | ||
| 3 | #////////////////////////////////////////// | ||
| 4 | use IPC::SysV qw(IPC_RMID IPC_CREAT IPC_EXCL SEM_UNDO IPC_NOWAIT | ||
| 5 | IPC_SET IPC_STAT SETVAL); | ||
| 6 | use IPC::Semaphore; | ||
| 7 | use POSIX qw(EEXIST); | ||
| 8 | use strict; | ||
| 9 | use warnings; | ||
| 10 | use constant INTERNAL_DEBUG => 0; | ||
| 11 | |||
| 12 | ########################################### | ||
| 13 | sub new { | ||
| 14 | ########################################### | ||
| 15 | my($class, %options) = @_; | ||
| 16 | |||
| 17 | my $self = { | ||
| 18 | key => undef, | ||
| 19 | mode => undef, | ||
| 20 | uid => undef, | ||
| 21 | gid => undef, | ||
| 22 | destroy => undef, | ||
| 23 | semop_wait => .1, | ||
| 24 | semop_retries => 1, | ||
| 25 | creator => $$, | ||
| 26 | %options, | ||
| 27 | }; | ||
| 28 | |||
| 29 | $self->{ikey} = unpack("i", pack("A4", $self->{key})); | ||
| 30 | |||
| 31 | # Accept usernames in the uid field as well | ||
| 32 | if(defined $self->{uid} and | ||
| 33 | $self->{uid} =~ /\D/) { | ||
| 34 | $self->{uid} = (getpwnam $self->{uid})[2]; | ||
| 35 | } | ||
| 36 | |||
| 37 | bless $self, $class; | ||
| 38 | $self->init(); | ||
| 39 | |||
| 40 | my @values = (); | ||
| 41 | for my $param (qw(mode uid gid)) { | ||
| 42 | push @values, $param, $self->{$param} if defined $self->{$param}; | ||
| 43 | } | ||
| 44 | $self->semset(@values) if @values; | ||
| 45 | |||
| 46 | return $self; | ||
| 47 | } | ||
| 48 | |||
| 49 | ########################################### | ||
| 50 | sub init { | ||
| 51 | ########################################### | ||
| 52 | my($self) = @_; | ||
| 53 | |||
| 54 | print "Semaphore init '$self->{key}'/'$self->{ikey}'\n" if INTERNAL_DEBUG; | ||
| 55 | |||
| 56 | $self->{id} = semget( $self->{ikey}, | ||
| 57 | 1, | ||
| 58 | &IPC_EXCL|&IPC_CREAT|($self->{mode}||0777), | ||
| 59 | ); | ||
| 60 | |||
| 61 | if(! defined $self->{id} and | ||
| 62 | $! == EEXIST) { | ||
| 63 | print "Semaphore '$self->{key}' already exists\n" if INTERNAL_DEBUG; | ||
| 64 | defined( $self->{id} = semget( $self->{ikey}, 1, 0 ) ) | ||
| 65 | or die "semget($self->{ikey}) failed: $!"; | ||
| 66 | } elsif($!) { | ||
| 67 | die "Cannot create semaphore $self->{key}/$self->{ikey} ($!)"; | ||
| 68 | } | ||
| 69 | |||
| 70 | print "Semaphore has id $self->{id}\n" if INTERNAL_DEBUG; | ||
| 71 | } | ||
| 72 | |||
| 73 | ########################################### | ||
| 74 | sub status_as_string { | ||
| 75 | ########################################### | ||
| 76 | my($self, @values) = @_; | ||
| 77 | |||
| 78 | my $sem = IPC::Semaphore->new($self->{ikey}, 1, 0); | ||
| 79 | |||
| 80 | my $values = join('/', $sem->getall()); | ||
| 81 | my $ncnt = $sem->getncnt(0); | ||
| 82 | my $pidlast = $sem->getpid(0); | ||
| 83 | my $zcnt = $sem->getzcnt(0); | ||
| 84 | my $id = $sem->id(); | ||
| 85 | |||
| 86 | return <<EOT; | ||
| 87 | Semaphore Status | ||
| 88 | Key ...................................... $self->{key} | ||
| 89 | iKey ..................................... $self->{ikey} | ||
| 90 | Id ....................................... $id | ||
| 91 | Values ................................... $values | ||
| 92 | Processes waiting for counter increase ... $ncnt | ||
| 93 | Processes waiting for counter to hit 0 ... $zcnt | ||
| 94 | Last process to perform an operation ..... $pidlast | ||
| 95 | EOT | ||
| 96 | } | ||
| 97 | |||
| 98 | ########################################### | ||
| 99 | sub semsetval { | ||
| 100 | ########################################### | ||
| 101 | my($self, %keyvalues) = @_; | ||
| 102 | |||
| 103 | my $sem = IPC::Semaphore->new($self->{ikey}, 1, 0); | ||
| 104 | $sem->setval(%keyvalues); | ||
| 105 | } | ||
| 106 | |||
| 107 | ########################################### | ||
| 108 | sub semset { | ||
| 109 | ########################################### | ||
| 110 | my($self, @values) = @_; | ||
| 111 | |||
| 112 | print "Setting values for semaphore $self->{key}/$self->{ikey}\n" if | ||
| 113 | INTERNAL_DEBUG; | ||
| 114 | |||
| 115 | my $sem = IPC::Semaphore->new($self->{ikey}, 1, 0); | ||
| 116 | $sem->set(@values); | ||
| 117 | } | ||
| 118 | |||
| 119 | ########################################### | ||
| 120 | sub semlock { | ||
| 121 | ########################################### | ||
| 122 | my($self) = @_; | ||
| 123 | |||
| 124 | my $operation = pack("s!*", | ||
| 125 | # wait until it's 0 | ||
| 126 | 0, 0, 0, | ||
| 127 | # increment by 1 | ||
| 128 | 0, 1, SEM_UNDO | ||
| 129 | ); | ||
| 130 | |||
| 131 | print "Locking semaphore '$self->{key}'\n" if INTERNAL_DEBUG; | ||
| 132 | $self->semop($self->{id}, $operation); | ||
| 133 | } | ||
| 134 | |||
| 135 | ########################################### | ||
| 136 | sub semunlock { | ||
| 137 | ########################################### | ||
| 138 | my($self) = @_; | ||
| 139 | |||
| 140 | # my $operation = pack("s!*", | ||
| 141 | # # decrement by 1 | ||
| 142 | # 0, -1, SEM_UNDO | ||
| 143 | # ); | ||
| 144 | # | ||
| 145 | print "Unlocking semaphore '$self->{key}'\n" if INTERNAL_DEBUG; | ||
| 146 | |||
| 147 | # # ignore errors, as they might result from trying to unlock an | ||
| 148 | # # already unlocked semaphore. | ||
| 149 | # semop($self->{id}, $operation); | ||
| 150 | |||
| 151 | semctl $self->{id}, 0, SETVAL, 0; | ||
| 152 | } | ||
| 153 | |||
| 154 | ########################################### | ||
| 155 | sub remove { | ||
| 156 | ########################################### | ||
| 157 | my($self) = @_; | ||
| 158 | |||
| 159 | print "Removing semaphore '$self->{key}/$self->{id}'\n" if INTERNAL_DEBUG; | ||
| 160 | |||
| 161 | semctl ($self->{id}, 0, &IPC_RMID, 0) or | ||
| 162 | die "Removing semaphore $self->{key} failed: $!"; | ||
| 163 | } | ||
| 164 | |||
| 165 | ########################################### | ||
| 166 | sub DESTROY { | ||
| 167 | ########################################### | ||
| 168 | my($self) = @_; | ||
| 169 | |||
| 170 | if($self->{destroy} && $$==$self->{creator}) { | ||
| 171 | $self->remove(); | ||
| 172 | } | ||
| 173 | } | ||
| 174 | |||
| 175 | ########################################### | ||
| 176 | sub semop { | ||
| 177 | ########################################### | ||
| 178 | my($self, @args) = @_; | ||
| 179 | |||
| 180 | my $retries = $self->{semop_retries}; | ||
| 181 | |||
| 182 | my $rc; | ||
| 183 | |||
| 184 | { | ||
| 185 | $rc = semop($args[0], $args[1]); | ||
| 186 | |||
| 187 | if(!$rc and | ||
| 188 | $! =~ /temporarily unavailable/ and | ||
| 189 | $retries-- > 0) { | ||
| 190 | $rc = 'undef' unless defined $rc; | ||
| 191 | print "semop failed (rc=$rc), retrying\n", | ||
| 192 | $self->status_as_string if INTERNAL_DEBUG; | ||
| 193 | select undef, undef, undef, $self->{semop_wait}; | ||
| 194 | redo; | ||
| 195 | } | ||
| 196 | } | ||
| 197 | |||
| 198 | $rc or die "semop(@args) failed: $! "; | ||
| 199 | $rc; | ||
| 200 | } | ||
| 201 | |||
| 202 | 1; | ||
| 203 | |||
| 204 | __END__ |