Why is File :: FcntlLock l_type always "F_UNLCK", even if the file is locked?

The Perl routine below is used File::FcntlLockto check if a file is locked.

Why does it return 0and print /tmp/test.pid is unlocked.even if the file is locked?

sub getPidOwningLock {
    my $filename = shift;

    my $fs = new File::FcntlLock;
    $fs->l_type( F_WRLCK );
    $fs->l_whence( SEEK_SET );
    $fs->l_start( 0 );
    $fs->l_len( 0 );

    my $fd;
    if (!open($fd, '+<', $filename)) {
        print "Could not open $filename\n";
        return -1;
    }

    if (!$fs->lock($fd, F_GETLK)) {
        print "Could not get lock information on $filename, error: $fs->error\n";
        close($fd);
        return -1;
    }

    close($fd);

    if ($fs->l_type() == F_UNLCK) {
        print "$filename is unlocked.\n";
        return 0;
    }

    return $fs->l_pid();
}

The file is locked as follows (lock.sh):

#!/bin/sh
(
    flock -n 200
    while true; do sleep 1; done
) 200>/tmp/test.pid

The file is really locked:

~$ ./lock.sh &
[2] 16803
~$ lsof /tmp/test.pid
COMMAND   PID  USER   FD   TYPE DEVICE SIZE/OFF   NODE NAME
bash    26002 admin  200w   REG    8,5        0 584649 test.pid
sleep   26432 admin  200w   REG    8,5        0 584649 test.pid
+4
source share
1 answer

fcntland flocklocks are invisible to each other.

, flock, script, flock: script flock, . ( ), .

fcntl, fcntl . , flock, fcntl, ( ).

perl script, , , :

  • script zsh zsystem flock zsh/system ( : , fcntl, , flock)
  • script perl
  • flock perl script ( "get lock PID", Linux, /proc/locks)
  • fcntl C script ( - script , , - - , , , ... , , ).
  • perl script script, ( , fcntl)

. fcntl flock.

+6

Source: https://habr.com/ru/post/1669835/


All Articles