1LOCKF(3) Linux Programmer's Manual LOCKF(3)
2
3
4
6 lockf - apply, test or remove a POSIX lock on an open file
7
9 #include <unistd.h>
10
11 int lockf(int fd, int cmd, off_t len);
12
13 Feature Test Macro Requirements for glibc (see feature_test_macros(7)):
14
15 lockf():
16 _BSD_SOURCE || _SVID_SOURCE || _XOPEN_SOURCE >= 500 ||
17 _XOPEN_SOURCE && _XOPEN_SOURCE_EXTENDED
18
20 Apply, test or remove a POSIX lock on a section of an open file. The
21 file is specified by fd, a file descriptor open for writing, the action
22 by cmd, and the section consists of byte positions pos..pos+len-1 if
23 len is positive, and pos-len..pos-1 if len is negative, where pos is
24 the current file position, and if len is zero, the section extends from
25 the current file position to infinity, encompassing the present and
26 future end-of-file positions. In all cases, the section may extend
27 past current end-of-file.
28
29 On Linux, lockf() is just an interface on top of fcntl(2) locking.
30 Many other systems implement lockf() in this way, but note that
31 POSIX.1-2001 leaves the relationship between lockf() and fcntl(2) locks
32 unspecified. A portable application should probably avoid mixing calls
33 to these interfaces.
34
35 Valid operations are given below:
36
37 F_LOCK Set an exclusive lock on the specified section of the file. If
38 (part of) this section is already locked, the call blocks until
39 the previous lock is released. If this section overlaps an ear‐
40 lier locked section, both are merged. File locks are released
41 as soon as the process holding the locks closes some file
42 descriptor for the file. A child process does not inherit these
43 locks.
44
45 F_TLOCK
46 Same as F_LOCK but the call never blocks and returns an error
47 instead if the file is already locked.
48
49 F_ULOCK
50 Unlock the indicated section of the file. This may cause a
51 locked section to be split into two locked sections.
52
53 F_TEST Test the lock: return 0 if the specified section is unlocked or
54 locked by this process; return -1, set errno to EAGAIN (EACCES
55 on some other systems), if another process holds a lock.
56
58 On success, zero is returned. On error, -1 is returned, and errno is
59 set appropriately.
60
62 EACCES or EAGAIN
63 The file is locked and F_TLOCK or F_TEST was specified, or the
64 operation is prohibited because the file has been memory-mapped
65 by another process.
66
67 EBADF fd is not an open file descriptor; or cmd is F_LOCK or F_TLOCK
68 and fd is not a writable file descriptor.
69
70 EDEADLK
71 The command was F_LOCK and this lock operation would cause a
72 deadlock.
73
74 EINVAL An invalid operation was specified in fd.
75
76 ENOLCK Too many segment locks open, lock table is full.
77
79 SVr4, POSIX.1-2001.
80
82 fcntl(2), flock(2)
83
84 locks.txt and mandatory-locking.txt in the Linux kernel source direc‐
85 tory Documentation/filesystems (on older kernels, these files are
86 directly under the Documentation directory, and mandatory-locking.txt
87 is called mandatory.txt)
88
90 This page is part of release 3.53 of the Linux man-pages project. A
91 description of the project, and information about reporting bugs, can
92 be found at http://www.kernel.org/doc/man-pages/.
93
94
95
96GNU 2012-07-07 LOCKF(3)