/usr/share/popfile/POPFile/Mutex.pm is in popfile 1.1.3+dfsg-0ubuntu1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
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 | package POPFile::Mutex;
#----------------------------------------------------------------------------
#
# This is a mutex object that uses mkdir() to provide exclusive access
# to a region on a per thread or per process basis.
#
# Copyright (c) 2001-2011 John Graham-Cumming
#
# This file is part of POPFile
#
# POPFile is free software; you can redistribute it and/or modify it
# under the terms of version 2 of the GNU General Public License as
# published by the Free Software Foundation.
#
# POPFile is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with POPFile; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
#
#----------------------------------------------------------------------------
use strict;
#----------------------------------------------------------------------------
# new
#
# Create a new Mutex object (which may refer to a file referred to by
# other mutexes) with a specific name generated from the name passed
# in.
#
#----------------------------------------------------------------------------
sub new
{
my ( $type, $name ) = @_;
my $self;
$self->{name__} = "popfile_mutex_${name}.mtx";
release( $self );
return bless $self, $type;
}
#----------------------------------------------------------------------------
#
# acquire
#
# Returns 1 if it manages to grab the mutex (and will block if necessary)
# and 0 if it fails.
#
# $self Reference to this object
# $timeout Timeout in seconds to wait (undef = infinite)
#
#----------------------------------------------------------------------------
sub acquire
{
my ( $self, $timeout ) = @_;
# If acquire() has been called without a matching release() then
# fail at once
if ( defined( $self->{locked__} ) ) {
return 0;
}
# Wait a very long time if no timeout is specified
$timeout = 0xFFFFFFFF if ( !defined( $timeout ) );
my $now = time;
# Try to create a directory during the timeout period
do {
if ( mkdir( $self->{name__}, 0755 ) ) { # Create a directory
$self->{locked__} = 1;
return 1;
}
select( undef, undef, undef, 0.01 );
} while ( time < ( $now + $timeout ) );
# Timed out so return 0
return 0;
}
#----------------------------------------------------------------------------
#
# release
#
# Release the lock if we acquired it with a call to acquire()
#
#----------------------------------------------------------------------------
sub release
{
my ( $self ) = @_;
rmdir( $self->{name__} ); # Delete the Mutex directory
$self->{locked__} = undef;
}
1;
|