Qmail::Control::Lock - Perl extension for locking Qmail's control file


Qmail-Control documentation Contained in the Qmail-Control distribution.

Index


Code Index:

NAME

Top

Qmail::Control::Lock - Perl extension for locking Qmail's control file subsystem.

SYNOPSIS

Top

  use Qmail::Control::Lock;

  my $lock = Qmail::Control::Lock->new();

  # Get a shared lock on the control subsystem. 
  $lock->lock_shared() or die "Couldn't get sh lock: $!\n";

  # Get an exclusive lock on the control subsystem. 
  $lock2->lock_exclusive() or die "Couldn't get ex lock: $!\n";

  # Change a shared lock to an exclusive lock.
  $lock->relock('exclusive') or die "Couldn't change sh to ex lock: $!\n";

  # Unlock the control subsystem.
  $lock->unlock();
  $lock2->unlock();

DESCRIPTION

Top

Qmail::Control::Lock provides and interface for locking Qmail's control file subsystem.

Dan Bernstein does not endorse this module or this locking method, and as far as I know, only Qmail::Control::Lock uses it.

EXPORTS

Top

None by default.

METHODS

Top

Qmail::Control::Lock->new()

Creates a new Qmail::Control::Lock object.

Returns a reference to the newly created object.

Takes no arguments, currently. This may change, but the argumentless form will always exist.

$lock->lock_shared();

Gets a shared lock on the Qmail control file subsystem.

Returns true on success or undef on a serious error.

Takes no arguments.

$lock->lock_exclusive();

Gets an exclusive lock on the Qmail control file subsystem.

Returns true on success or undef on a serious error.

Takes no arguments.

$lock->relock();

Changes one type of lock (either shared or exclusive) into another.

Returns true on success or undef on a serious error.

Takes a single argument, either 'shared' or 'exclusive', which indicates which type of lock to engage. If you pass 'shared', and the lock is already 'shared', this is a no-op.

$lock->unlock();

Unlocks the Qmail control file subsystem.

Returns nothing.

Takes no arguments.

AUTHOR

Top

Paul Prince, <princep@charter.net>

SEE ALSO

Top

perl. Qmail::Control::Lock.


Qmail-Control documentation Contained in the Qmail-Control distribution.
## This file is Copyright (C) 2002, Paul Prince <princep@charter.net>.
## It is licensed and distributed under the terms of Perl itself.

package Qmail::Control::Lock;

use 5.006;
use strict;
use warnings;

our $VERSION = '0.01';

use Fcntl qw/:flock/;

require Exporter;

######################
# METHODS START HERE #
######################

sub new {
#    Do I need this stuff?? 
#    my $invocant = shift;
#    my $class = ref($invocant) || $invocant;
#    bless($class);

    # Create a new Qmail::Control::Lock object.
    my $self = { };
    bless($self);

    # Locking is achieved by calling flock() on QMAILHOME/control/.lock .
    # This is ususally /var/qmail/control/.lock , which is what we
    # assume.

    # Open the lockfile for reading.
    my $lockfile_handle;
    open($lockfile_handle, '<', '/var/qmail/control/.lock');

    # Put a reference to the filehandle into the hash which represents the
    # object.
    $self->{'lockfile_handle'} = $lockfile_handle;

    # Return a reference to the newly created object.
    return $self;
}

sub lock_shared {
    my $self = shift;

    # Confirm that there is a filehandle in $self.
    exists $self->{'lockfile_handle'} or return undef;

    # Lock that filehandle.
    flock ($self->{'lockfile_handle'},           LOCK_SH) or return undef;
    
    # Let the object know that it is locked, and how.
    $self->{'locked'} = 'shared';

    # Return true.
    return 1;
}

sub lock_exclusive {
    my $self = shift;

    # Confirm that there is a filehandle in $self.
    exists $self->{'lockfile_handle'} or return undef;

    # Lock that filehandle.
    flock ($self->{'lockfile_handle'},           LOCK_EX) or return undef;
    
    # Let the object know that it is locked, and how.
    $self->{'locked'} = 'exclusive';

    # Return true.
    return 1;
}

sub relock {
    my $self = shift;

    # Confirm that there is a filehandle in $self.
    exists $self->{'lockfile_handle'} or return undef;

#    # Unlock that filehandle.
#    flock ($self->{'lockfile_handle'}, LOCK_UN) or return undef;

    # If we got 'shared', lock 'shared'.
    if ($_[0] eq 'shared') {
        flock ($self->{'lockfile_handle'}, LOCK_SH) or return undef;
    }
    # Else, if we got 'exclusive', lock 'exclusive'.
    elsif ($_[0] eq 'exclusive') {
        flock ($self->{'lockfile_handle'}, LOCK_EX) or return undef;
    }
    # Else, we got an invalud param, return undef.
    else {
        return undef;
    }

    # Let the object know that it is locked, and how.
    $self->{'locked'} = $_[0];

    # Return true.
    return 1;
}

sub unlock {
    my $self = shift;

    # Unlock that filehandle.
    flock ($self->{'lockfile_handle'}, LOCK_UN);
    
    # Let the object know that it is not locked.
    delete $self->{'locked'};

    # Return true.
    return 1;
}

sub DESTROY {
    my $self = shift;
    close $self->{'lockfile_handle'};
}

# End the package.
1;