Provided by: liblockdev1-perl_1.0.3-1.4build2_amd64
LockDev - Perl extension to manage device lockfiles
use LockDev; $pid = LockDev::dev_testlock( $device); $pid = LockDev::dev_lock( $device); $pid = LockDev::dev_relock( $device, $oldpid); $pid = LockDev::dev_unlock( $device, $oldpid);
The LockDev methods act on device locks normally located in /var/lock . The lock is acquired creating a pair of files hardlinked between them and named after the device name (as mandated by FSSTND) and the device's major and minor numbers (as in SVr4 locks). This permits to circumvent a problem using only the FSSTND lock method when the same device exists under different names (for convenience or when a device must be accessable by more than one group of users). The lock file names are typically in the form LCK..ttyS1 and LCK.004.065 , and their content is the pid of the process who owns the lock.
$pid = LockDev::dev_testlock( $device); This method simply checks if the device is in some way locked and if the owner of the lock is still active (otherways it removes the lock). It recognise a valid lock even if only one of the two lock files exists (and is owned by an existing process), thus permitting a safe use of this library toghether with programs using only FSSTND or SVr4 lock style. $pid = LockDev::dev_lock( $device); This method first checks if the device is already locked and then tryes to acquire the lock building the two lock files. First it creates the file which name contains the major and minor numbers (in SVr4 style), then it creates the file with the device name in its name. This order reduces the clashes with other processes trying to lock the same device (even with a different name) and using this library. It has no problem with processes that uses only the FSSTND algorithm. $pid = LockDev::dev_relock( $device, $oldpid); This method changes the owner of an existing lock; if the pid of the old owner is provided, then it checks if the lock was correctly assigned (otherways there is the possibility of a process acquiring a lock which was owned by another unrelated process). If the device was not locked, locks it. $pid = LockDev::dev_unlock( $device, $oldpid); This method removes the existing locks on the device. If the pid of the owner of the lock is provided, then it check if the locks are assigned to that process, avoiding to remove locks assigned to other existing processes.
All the methods in LockDev library return ZERO on successfull completion of the function (LockDev::dev_testlock returns zero if there is no lock on the device), otherways, if the device is currently locked by an existing process, they return the pid of the process owner of the lock. They return a negative number when some kind of error happens. Actually they all return only (-1).
(c) 1997 by Fabrizio Polacco <email@example.com>. The LockDev extension library and the liblockdev 'C' library are licensed under the FSF's LGPL.