<html><head><meta name="color-scheme" content="light dark"></head><body><pre style="word-wrap: break-word; white-space: pre-wrap;"># -*- cperl -*-
#
# This program is free software; you can redistribute it and/or
# modify it under the same terms as Perl itself.
#
# Copyright (C) 2002-2011 Jens Thoms Toerring &lt;jt@toerring.de&gt;


package File::FcntlLock;

use 5.006;
use strict;
use warnings;
use POSIX;
use Errno;
use Carp;

require Exporter;
require DynaLoader;

our @ISA = qw( Exporter DynaLoader );

# Items to export into callers namespace by default.

our @EXPORT = qw( F_GETLK F_SETLK F_SETLKW
                  F_RDLCK F_WRLCK F_UNLCK
                  SEEK_SET SEEK_CUR SEEK_END );

our $VERSION = '0.14';


=pod

=head1 NAME

File::FcntlLock - File locking with L&lt;fcntl(2)&gt;

=head1 SYNOPSIS

  use File::FcntlLock;

  my $fs = new File::FcntlLock;
  $fs-&gt;l_type( F_RDLCK );
  $fs-&gt;l_whence( SEEK_CUR );
  $fs-&gt;l_start( 100 );
  $fs-&gt;l_len( 123 );

  open my $fh, '&lt;', 'file_name' or die "Can't open file: $!\n";
  $fs-&gt;lock( $fh, F_SETLK )
      or print "Locking failed: " . $fs-&gt;error . "\n";
  $fs-&gt;l_type( F_UNLCK );
  $fs-&gt;lock( $fh, F_SETLK )
      or print "Unlocking failed: " . $fs-&gt;error . "\n";

=head1 DESCRIPTION

File locking in Perl is usually done using the L&lt;flock()&gt; function.
Unfortunately, this only allows locks on whole files and is often
implemented in terms of L&lt;flock(2)&gt;, which has some shortcomings.

Using this module file locking via L&lt;fcntl(2)&gt; can be done (obviously,
this restricts the use of the module to systems that have a L&lt;fcntl(2)&gt;
system call). Before a file (or parts of a file) can be locked, an
object simulating a flock structure must be created and its properties
set. Afterwards, by calling the C&lt;lock()&gt; method a lock can be set or it
can be determined if and which process currently holds the lock.

=cut

# Set up a hash with the error messages, but only for errno's that Errno
# knows about. The texts represent what's written in SUSv3 and in the man
# pages for Linux, TRUE64, OpenBSD3 and Solaris8.

my %fcntl_error_texts;

BEGIN {
    my $err;

    if ( $err = eval { &amp;Errno::EACCES } ) {
        $fcntl_error_texts{ $err } = "File or segment already locked " .
                                     "by other process(es) or file is " .
                                     "mmap()ed to virtual memory";
    }
    if ( $err = eval { &amp;Errno::EAGAIN } ) {
        $fcntl_error_texts{ $err } = "File or segment already locked " .
                                     "by other process(es)";
    }
    if ( $err = eval { &amp;Errno::EBADF } ) {
        $fcntl_error_texts{ $err } = "Not an open file handle or descriptor " .
                                     "or not open for writing (with F_WRLCK)" .
                                     " or reading (with F_RDLCK)";
    }
    if ( $err = eval { &amp;Errno::EDEADLK } ) {
        $fcntl_error_texts{ $err } = "Operation would cause a deadlock";
    }
    if ( $err = eval { &amp;Errno::EFAULT } ) {
        $fcntl_error_texts{ $err } = "Lock outside accessible address space " .
                                     "or to many locked regions";
    }
    if ( $err = eval { &amp;Errno::EINTR } ) {
        $fcntl_error_texts{ $err } = "Operation interrupted by a signal";
    }
    if ( $err = eval { &amp;Errno::ENOLCK } ) {
        $fcntl_error_texts{ $err } = "Too many segment locks open, lock " .
                                     "table full or remote locking protocol " .
                                     "failure (e.g. NFS)";
    }
    if ( $err = eval { &amp;Errno::EINVAL } ) {
        $fcntl_error_texts{ $err } = "Illegal parameter or file does not " .
                                     "support locking";
    }
    if ( $err = eval { &amp;Errno::EOVERFLOW } ) {
        $fcntl_error_texts{ $err } = "One of the parameters to be returned " .
                                     "can not be represented correctly";
    }
    if ( $err = eval { &amp;Errno::ENETUNREACH } ) {
        $fcntl_error_texts{ $err } = "File is on remote machine that can " .
                                     "not be reached anymore";
    }
    if ( $err = eval { &amp;Errno::ENOLINK } ) {
        $fcntl_error_texts{ $err } = "File is on remote machine that can " .
                                     "not be reached anymore";
    }
}


bootstrap File::FcntlLock $VERSION;


###########################################################

=pod

To create a new object representing a flock structure call C&lt;new()&gt;:

  $fs = new File::FcntlLock;

You also can pass the C&lt;new()&gt; method a set of key-value pairs to
initialize the objects properties, e.g. use

  $fs = new File::FcntlLock l_type   =&gt; F_WRLCK,
                            l_whence =&gt; SEEK_SET,
                            l_start  =&gt; 0,
                            l_len    =&gt; 100;

if you plan to obtain a write lock for the first 100 bytes of a file.

=cut

sub new {
    my $inv = shift;
    my $pkg = ref( $inv ) || $inv;

    my $self = { l_type        =&gt; F_RDLCK,
                 l_whence      =&gt; SEEK_SET,
                 l_start       =&gt; 0,
                 l_len         =&gt; 0,
                 l_pid         =&gt; 0,
                 errno         =&gt; undef,
                 error_message =&gt; undef      };

    if ( @_ % 2 ) {
        carp "Missing value in key-value initializer list " .
             "in call of new method";
        return;
    }

    while ( @_ ) {
        my $key = shift;
        no strict 'refs';
        unless ( defined &amp;$key ) {
            carp "Flock structure has no '$key' member " .
                 "in call of new method";
            return;
        }
        &amp;$key( $self, shift );
        use strict 'refs';
    }

    bless $self, $pkg;
}


###########################################################

=pod

Once you have created the object simulating the flock structure
the following methods allow to query and in most cases also to
modify the properties of the object.

=over 4

=item C&lt;l_type()&gt;

If called without an argument returns the current setting of the
lock type, otherwise the lock type is set to the argument, which
must be either C&lt;F_RDLCK&gt;, C&lt;F_WRLCK&gt; or C&lt;F_UNLCK&gt; (for read lock,
write lock or unlock).

=cut

sub l_type {
    my $flock_struct = shift;

    if ( @_ ) {
        my $l_type = shift;
        unless (    $l_type == F_RDLCK
                 or $l_type == F_WRLCK
                 or $l_type == F_UNLCK ) {
            carp "Invalid argument in call of l_type method";
            return;
        }
        $flock_struct-&gt;{ l_type } = $l_type;
    }
    return $flock_struct-&gt;{ l_type };
}


###########################################################

=pod

=item C&lt;l_whence()&gt;

Queries or sets the C&lt;l_whence&gt; property of the flock object,
determining if the C&lt;l_start&gt; value is relative to the start of
the file, to the current position in the file or to the end of
the file. The corresponding values are C&lt;SEEK_SET&gt;, C&lt;SEEK_CUR&gt;
and C&lt;SEEK_END&gt;. See also the man page for L&lt;lseek(2)&gt;.

=cut

sub l_whence {
    my $flock_struct = shift;

    if ( @_ ) {
        my $l_whence = shift;
        unless (    $l_whence == SEEK_SET
                 or $l_whence == SEEK_CUR
                 or $l_whence == SEEK_END ) {
            carp "Invalid argument in call of l_whence method";
            return;
        }
        $flock_struct-&gt;{ l_whence } = $l_whence;
    }
    return $flock_struct-&gt;{ l_whence };
}


###########################################################

=pod

=item C&lt;l_start()&gt;

Queries or sets the start position (offset) of the lock in the
file according to the mode selected by the C&lt;l_whence&gt; member.
See also the man page for L&lt;lseek(2)&gt;.

=cut

sub l_start {
    my $flock_struct = shift;

    $flock_struct-&gt;{ l_start } = shift if @_;
    return $flock_struct-&gt;{ l_start };
}


###########################################################

=pod

=item C&lt;l_len()&gt;

Queries or sets the length of the region (in bytes) in the file
to be locked. A value of 0 is interpreted as to mean a lock (starting
at C&lt;l_start&gt;) up to the very end of the file.

According to SUSv3 negative values for C&lt;l_start&gt; are allowed
(resulting in a lock ranging from C&lt;l_start + l_len&gt; to
C&lt;l_start - 1&gt;) Unfortunately, not all systems allow negative
arguments and will return an error when you try to obtain the
lock, so please read the L&lt;fcntl(2)&gt; man page of your system
carefully for details.

=cut

sub l_len {
    my $flock_struct = shift;

    $flock_struct-&gt;{ l_len } = shift if @_;
    return $flock_struct-&gt;{ l_len };
}


###########################################################

=pod

=item C&lt;l_pid()&gt;

This method allows retrieving the PID of a process currently
holding the lock after a call of C&lt;lock()&gt; with C&lt;F_SETLK&gt; indicated
that another process is holding the lock. A call to C&lt;lock()&gt;
with C&lt;F_GETLK&gt; will fill in this value so C&lt;l_pid()&gt; can be called.

=back

=cut

sub l_pid {
    return shift-&gt;{ l_pid };
}


###########################################################

=pod

When not initialized the flock objects C&lt;l_type&gt; property is set
to C&lt;F_RDLCK&gt; by default, C&lt;l_whence&gt; to C&lt;SEEK_SET&gt;, and both
C&lt;l_start&gt; and C&lt;l_len&gt; to 0, i.e. the settings for a read lock
on the whole file.


After having set up the object representing a flock structure you
can determine the current holder of a lock or try to obtain a lock
by invoking the C&lt;lock()&gt; method with two arguments, a file handle
(or a file descriptor, the module figures out automatically what
it got) and a flag indicating the action to be taken, e.g.

  $fs-&gt;lock( $fh, F_SETLK );

There are three values that can be used as the second argument:

=over 4

=item C&lt;F_GETLK&gt;

For C&lt;F_GETLK&gt; the C&lt;lock()&gt; method determines if and who currently
is holding the lock.  If no other process is holding the lock the
C&lt;l_type&gt; field is set to C&lt;F_UNLCK&gt;. Otherwise the flock structure
object is set to the values that prevent us from obtaining a lock.
There may be multiple such blocking processes, including some that
are themselves blocked waiting to obtain a lock. C&lt;F_GETLK&gt; will
only make details of one of these visible, and one has no control
over which process this is.

=item C&lt;F_SETLK&gt;

For C&lt;F_SETLK&gt; the C&lt;lock()&gt; method tries to obtain the lock (when
C&lt;l_type&gt; is set to either C&lt;F_WRLCK&gt; or C&lt;F_RDLCK&gt;) or releases
the lock (if C&lt;l_type&gt; is set to C&lt;F_UNLCK&gt;). If a lock is held
by some other process the method call returns C&lt;undef&gt; and errno
is set to C&lt;EACCESS&gt; or C&lt;EAGAIN&gt; (please see the the man page for
L&lt;fcntl(2)&gt; for the details).

=item C&lt;F_SETLKW&gt;

is similar to C&lt;F_SETLK&gt; but instead of returning an error if the
lock can't be obtained immediately it blocks until the lock is
obtained. If a signal is received while waiting for the lock the
method returns C&lt;undef&gt; and errno is set to C&lt;EINTR&gt;.

=back

On success the method returns the string "0 but true". If the
method fails (as indicated by an C&lt;undef&gt; return value) you can
either immediately evaluate the error number (usingf $!, $ERRNO
or $OS_ERROR) or check for it at some later time via the methods
discussed below.

=cut

sub lock {
    my ( $flock_struct, $fh, $action ) = @_;
    my ( $ret, $err );

    # Figure out the file descriptor - we might get a file handle, a
    # typeglob or already a file descriptor) and set it to a value which
    # will make fcntl(2) fail with EBADF if the argument is undefined or
    # is a file handle that's invalid.

    my $fd = ( ref( $fh ) or $fh =~ /^\*/ ) ? fileno( $fh ) : $fh;
    $fd = -1 unless defined $fd;

    # Set the action argument to something invalid if it's not defined
    # which then fcntl(2) fails and errno gets set accordingly

    $action = -1 unless defined $action;

    if ( $ret = C_fcntl_lock( $fd, $action, $flock_struct, $err ) ) {
        $flock_struct-&gt;{ errno } = $flock_struct-&gt;{ error } = undef;
    } elsif ( $err ) {
        die "Internal error in File::FcntlLock module detected";
    } else {
        $flock_struct-&gt;{ errno } = $! + 0;
        $flock_struct-&gt;{ error } = defined $fcntl_error_texts{ $! + 0 } ?
                         $fcntl_error_texts{ $! + 0 } : "Unexpected error: $!";
    }

    return $ret;
}


###########################################################

=pod

There are three methods for obtaining information about the
reason the the last call of C&lt;lock()&gt; for the object failed:

=over 4

=item C&lt;lock_errno()&gt;

Returns the error number from the latest call of C&lt;lock()&gt;. If the
last call did not result in an error the method returns C&lt;undef&gt;.

=cut

sub lock_errno {
    return shift-&gt;{ errno };
}


###########################################################

=pod

=item C&lt;error()&gt;

Returns a short description of the error that happened during the
latest call of C&lt;lock()&gt; with the object. Please take the messages
with a grain of salt, they represent what SUSv3 (IEEE 1003.1-2001)
and the Linux, TRUE64, OpenBSD3 and Solaris8 man pages tell what
the error numbers mean, there could be differences (and additional
error numbers) on other systems. If there was no error the method
returns C&lt;undef&gt;.

=cut

sub error {
    return shift-&gt;{ error };
}


###########################################################

=pod

=item C&lt;system_error()&gt;

While the previous method, C&lt;error()&gt;, tries to return a string with
some relevance to the locking operation (i.e. "File or segment already
locked by other process(es)" instead of "Permission denied") this
method returns the "normal" system error message associated with
errno. The method returns C&lt;undef&gt; if there was no error.

=back

=cut

sub system_error {
    local $!;
    my $flock_struct = shift;
    return $flock_struct-&gt;{ errno } ? $! = $flock_struct-&gt;{ errno } : undef;
}


=pod

=head2 EXPORT

F_GETLK F_SETLK F_SETLKW
F_RDLCK F_WRLCK F_UNLCK
SEEK_SET SEEK_CUR SEEK_END

=head1 CREDITS

Thanks to Mark Jason Dominus (MJD) and Benjamin Goldberg (GOLDBB) for
helpful discussions, code examples and encouragement. Glenn Herteg
pointed out several problems and also helped improve the documentation.
Julian Moreno Patino also helped correcting the documentation and
pointed out problems arising on GNU Hurd (which seems to have only
very rudimentary support for locking with fcntl()).

=head1 AUTHOR

Jens Thoms Toerring &lt;jt@toerring.de&gt;

=head1 SEE ALSO

L&lt;perl(1)&gt;, L&lt;fcntl(2)&gt;, L&lt;lseek(2)&gt;.

=cut


1;

# Local variables:
# tab-width: 4
# indent-tabs-mode: nil
# End:
</pre></body></html>