This file is indexed.

/usr/share/perl5/Paranoid/Lockfile.pm is in libparanoid-perl 0.36-3.

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
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
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
# Paranoid::Lockfile -- Paranoid Lockfile support
#
# (c) 2005, Arthur Corliss <corliss@digitalmages.com>
#
# $Id: Lockfile.pm,v 0.65 2011/12/08 07:53:07 acorliss Exp $
#
#    This software is licensed under the same terms as Perl, itself.
#    Please see http://dev.perl.org/licenses/ for more information.
#
#####################################################################

#####################################################################
#
# Environment definitions
#
#####################################################################

package Paranoid::Lockfile;

use 5.006;

use strict;
use warnings;
use vars qw($VERSION @EXPORT @EXPORT_OK %EXPORT_TAGS);
use base qw(Exporter);
use Fcntl qw(:flock O_RDWR O_CREAT O_EXCL);
use Paranoid;
use Paranoid::Debug qw(:all);
use Paranoid::Filesystem;
use Carp;

($VERSION) = ( q$Revision: 0.65 $ =~ /(\d+(?:\.(\d+))+)/sm );

@EXPORT      = qw(plock punlock pcloseLockfile);
@EXPORT_OK   = qw(plock punlock pcloseLockfile);
%EXPORT_TAGS = ( all => [qw(plock punlock pcloseLockfile)], );

use constant PRIV_UMASK => 0600;

#####################################################################
#
# Module code follows
#
#####################################################################

{

    # file descriptor stash
    my %fd;     # File descriptor stash
    my %pid;    # PID tracking for fd

    sub _clearLocks {

        # Purpose:  Cleanly closes all lockfiles
        # Returns:  True/false
        # Usage:    $rv = _clearLocks();

        my ( $frv, $rv );

        pdebug( 'entering', PDLEVEL2 );
        pIn();

        $frv = 1;
        foreach ( keys %fd ) {
            $rv = pcloseLockfile($_);
            $frv = 0 unless $rv;
            pdebug( "$_ rv: $rv", PDLEVEL3 );
        }

        pOut();
        pdebug( "leaving w/rv: $frv", PDLEVEL2 );

        return $frv;
    }

    sub plock ($;$$) {

        # Purpose:  Opens and locks the specified file.
        # Returns:  True/false
        # Usage:    $rv = plock( $filename );
        # Usage:    $rv = plock( $filename, $lockType, $fileMode );

        my $filename = shift;
        my $type     = shift;
        my $mode     = shift;
        my $targ     = defined $type ? $type : 'undef';
        my $marg     = defined $mode ? $mode : 'undef';
        my $rv       = 0;
        my ( $fd, $irv );

        # Validate arguments
        croak 'Mandatory first argument must be a defined filename'
            unless defined $filename && length $filename > 0;
        croak 'Optional second argument must be a valid lock type'
            unless !defined $type
                || ( defined $type && $type =~ /^(?:write|shared)$/sm );

        pdebug( "entering w/($filename)($targ)($marg)", PDLEVEL1 );
        pIn();

        # Get the filehandle
        if ( exists $fd{$filename} ) {

            # Retrieve a previously stored filehandle
            $fd = $fd{$filename};

            # Make sure PID is the same, otherwise we'll need to
            # reopen the file
            $irv = $pid{$filename} == $$ ? 1 : 0;
        }

        unless ($irv) {

            # Open a new filehandle
            #
            # Set the default perms if needed
            $mode = PRIV_UMASK unless defined $mode;

            # To avoid race conditions with multiple files opening (and
            # overwriting) the same file, and hence doing flocks on
            # descriptors with a different # (f#*&ing lock isn't working!)
            # we attempt to do an exclusive open first.  If that fails, then
            # we do reopen to get a filehandle to the (possibly) newly
            # created file.
            $irv = sysopen( $fd, $filename, O_RDWR | O_CREAT | O_EXCL, $mode )
                || sysopen( $fd, $filename, O_RDWR );

            # Store the new filehandle
            if ($irv) {
                $fd{$filename}  = $fd;
                $pid{$filename} = $$;
            }
        }

        # Flock it
        if ($irv) {

            # Assign the lock type according to $type
            $type = 'write' unless defined $type;
            $type = $type eq 'write' ? LOCK_EX : LOCK_SH;
            $rv = flock $fd, $type;
        }

        pOut();
        pdebug( "leaving w/rv: $rv", PDLEVEL1 );

        return $rv;
    }

    sub punlock ($) {

        # Purpose:  Removes any existing locks on the file
        # Returns:  True/false
        # Usage:    $rv = punlock();

        my $filename = shift;
        my $rv       = 1;

        # Validate arguments
        croak 'Mandatory first argument must be a defined filename'
            unless defined $filename && length $filename > 0;

        pdebug( "entering w/($filename)", PDLEVEL1 );
        pIn();

        $rv = flock $fd{$filename}, LOCK_UN if exists $fd{$filename};

        pOut();
        pdebug( "leaving w/rv: $rv", PDLEVEL1 );

        return $rv;
    }

    sub pcloseLockfile ($) {

        # Purpose:  Unlocks and closes the passed filename
        # Returns:  True/false
        # Usage:    $rv = pcloseLockfile( $filename );

        my $filename = shift;
        my $rv       = 1;

        # Validate arguments
        croak 'Mandatory first argument must be a defined filename'
            unless defined $filename && length $filename > 0;

        pdebug( "entering w/($filename)", PDLEVEL1 );
        pIn();

        if ( exists $fd{$filename} ) {
            $rv = flock( $fd{$filename}, LOCK_UN )
                and close $fd{$filename};
            delete $fd{$filename} if $rv;
        }

        pOut();
        pdebug( "leaving w/rv: $rv", PDLEVEL1 );

        return $rv;
    }
}

END {
    _clearLocks();
}

1;

__END__

=head1 NAME

Paranoid::Lockfile - Paranoid Lockfile support

=head1 VERSION

$Id: Lockfile.pm,v 0.65 2011/12/08 07:53:07 acorliss Exp $

=head1 SYNOPSIS

  use Paranoid::Lockfile;

  $rv = plock($lockfile);
  $rv = punlock($lockfile);
  $rv = pcloseLockfile($lockfile);

=head1 DESCRIPTION

This modules provides a relatively safe locking mechanism across multiple 
processes.  This does not work over NFS or across remote systems, this is 
only intended for use on a single system at a time, and only on those that 
support B<flock>.

B<sysopen> is used to avoid race conditions with multiple process attempting
to create the same file simultaneously.

=head1 SUBROUTINES/METHODS

=head2 plock

  $rv = plock($filename);

This function attempts to safely create or open the lockfile.  It uses
B<sysopen> with B<O_CREAT | O_EXCL> to avoid race conditions with other
processes.  Returns a true if successful.

Your can pass an optional second argument which would be a string of either
'write' or 'shared'.  The default is 'write', which locks the file in
exclusive write mode.

You can pass an optional third argument which would be the lockfile
filesystem permissions if the file is created.  The default is 0600.

B<NOTE:> This function will block until the advisory lock is granted.

=head2 punlock

  $rv = punlock($filename);

This function removes any existing locks on the specified filename using
B<flock>.  If no previous lock existed or it was successful it returns true.
This does not, however, close the open filehandle to the lockfile.

=head2 pcloseLockfile

  $rv = pcloseLockfile($filename);

This function releases any existing locks and closes the open filehandle to
the lockfile.  Returns true if the file isn't currently open or the operation
succeeds.

=head1 DEPENDENCIES

=over

=item o

L<Fcntl>

=item o

L<Paranoid>

=item o

L<Paranoid::Debug>

=back

=head1 BUGS AND LIMITATIONS

=head1 AUTHOR

Arthur Corliss (corliss@digitalmages.com)

=head1 LICENSE AND COPYRIGHT

This software is licensed under the same terms as Perl, itself. 
Please see http://dev.perl.org/licenses/ for more information.

(c) 2005, Arthur Corliss (corliss@digitalmages.com)