1LOCKDEV(3) Linux Programmer's Manual LOCKDEV(3)
2
3
4
6 lockdev, liblockdev, dev_testlock, dev_lock, dev_relock, dev_unlock -
7 manage device lockfiles
8
10 #include <lockdev.h>
11
12 pid_t dev_testlock( const char * devname);
13 pid_t dev_lock( const char * devname);
14 pid_t dev_relock( const char * devname, pid_t pid);
15 pid_t dev_unlock( const char * devname, pid_t pid);
16
17 cc [ flag ... ] file ... -llockdev [ library ]
18
20 The lockdev functions act on device locks normally located in /var/lock
21 . The lock is acquired creating a pair of files hardlinked between
22 them and named after the device name (as mandated by FSSTND) and the
23 device's major and minor numbers (as in SVr4 locks). This permits to
24 circumvent a problem using only the FSSTND lock method when the same
25 device exists under different names (for convenience or when a device
26 must be accessable by more than one group of users).
27 The lock file names are typically in the form LCK..ttyS1 and
28 LK.000.004.065 , but is provided a way to easily modify them to use the
29 library on different architectures. The content of those files is the
30 pid of the process who owns the lock.
31
32 The dev_testlock() function simply checks if the device is in some way
33 locked and if the owner of the lock is still active (otherwise it
34 removes the lock). It recognise a valid lock even if only one of the
35 two lock files exists (and is owned by an existing process), thus per‐
36 mitting a safe use of this library together with programs using only
37 FSSTND or SVr4 lock style.
38
39 The dev_lock() function first checks if the device is already locked
40 and then tries to acquire the lock building the two lock files. First
41 it creates the file which name contains the major and minor numbers (in
42 SVr4 style), then it creates the file with the device name in its name.
43 This order reduces the clashes with other processes trying to lock the
44 same device (even with a different name) and using this library. It has
45 no problem with processes that uses only the FSSTND algorithm.
46
47 The dev_relock() function changes the owner of an existing lock; if the
48 pid of the old owner is provided, then it checks if the lock was cor‐
49 rectly assigned (otherwise there is the possibility of a process
50 acquiring a lock which was owned by another unrelated process). If the
51 device was not locked, locks it.
52
53 The dev_unlock() function removes the existing locks on the device. If
54 the pid of the owner of the lock is provided, then it checks if the
55 lock is assigned to that process, avoiding to remove locks assigned to
56 other existing processes.
57
58
60 All the functions in lockdev library return ZERO on successfull comple‐
61 tion of the function (dev_testlock returns zero if there is no lock on
62 the device), otherwise, if the device is currently locked by an exist‐
63 ing process, they return the pid of the process owner of the lock. They
64 return a negative number when some kind of error happens. Actually they
65 all return only (-1).
66
67
69 /var/lock/LCK..<device>
70 /var/lock/LK.<devmajor>.<major>.<minor>
71 /usr/lib/liblockdev.so.1
72
73
75 (c) 1997 by Fabrizio Polacco <fab@prosa.it>.
76 This program is free software; you can redistribute it and/or modify it
77 under the terms of the GNU Lesser General Public License as published
78 by the Free Software Foundation; version 2 dated June, 1991.
79
80
81
82Linux Manpage 26 Dec 1997 LOCKDEV(3)