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 _XOPEN_SOURCE >= 500
17 || /* Glibc since 2.19: */ _DEFAULT_SOURCE
18 || /* Glibc <= 2.19: */ _BSD_SOURCE || _SVID_SOURCE
19
21 Apply, test, or remove a POSIX lock on a section of an open file. The
22 file is specified by fd, a file descriptor open for writing, the action
23 by cmd, and the section consists of byte positions pos..pos+len-1 if
24 len is positive, and pos-len..pos-1 if len is negative, where pos is
25 the current file position, and if len is zero, the section extends from
26 the current file position to infinity, encompassing the present and fu‐
27 ture end-of-file positions. In all cases, the section may extend past
28 current end-of-file.
29
30 On Linux, lockf() is just an interface on top of fcntl(2) locking.
31 Many other systems implement lockf() in this way, but note that POSIX.1
32 leaves the relationship between lockf() and fcntl(2) locks unspecified.
33 A portable application should probably avoid mixing calls to these in‐
34 terfaces.
35
36 Valid operations are given below:
37
38 F_LOCK Set an exclusive lock on the specified section of the file. If
39 (part of) this section is already locked, the call blocks until
40 the previous lock is released. If this section overlaps an ear‐
41 lier locked section, both are merged. File locks are released
42 as soon as the process holding the locks closes some file de‐
43 scriptor for the file. A child process does not inherit these
44 locks.
45
46 F_TLOCK
47 Same as F_LOCK but the call never blocks and returns an error
48 instead if the file is already locked.
49
50 F_ULOCK
51 Unlock the indicated section of the file. This may cause a
52 locked section to be split into two locked sections.
53
54 F_TEST Test the lock: return 0 if the specified section is unlocked or
55 locked by this process; return -1, set errno to EAGAIN (EACCES
56 on some other systems), if another process holds a lock.
57
59 On success, zero is returned. On error, -1 is returned, and errno is
60 set to indicate the error.
61
63 EACCES or EAGAIN
64 The file is locked and F_TLOCK or F_TEST was specified, or the
65 operation is prohibited because the file has been memory-mapped
66 by another process.
67
68 EBADF fd is not an open file descriptor; or cmd is F_LOCK or F_TLOCK
69 and fd is not a writable file descriptor.
70
71 EDEADLK
72 The command was F_LOCK and this lock operation would cause a
73 deadlock.
74
75 EINTR While waiting to acquire a lock, the call was interrupted by de‐
76 livery of a signal caught by a handler; see signal(7).
77
78 EINVAL An invalid operation was specified in cmd.
79
80 ENOLCK Too many segment locks open, lock table is full.
81
83 For an explanation of the terms used in this section, see at‐
84 tributes(7).
85
86 ┌────────────────────────────────────────────┬───────────────┬─────────┐
87 │Interface │ Attribute │ Value │
88 ├────────────────────────────────────────────┼───────────────┼─────────┤
89 │lockf() │ Thread safety │ MT-Safe │
90 └────────────────────────────────────────────┴───────────────┴─────────┘
91
93 POSIX.1-2001, POSIX.1-2008, SVr4.
94
96 fcntl(2), flock(2)
97
98 locks.txt and mandatory-locking.txt in the Linux kernel source directo‐
99 ry Documentation/filesystems (on older kernels, these files are direct‐
100 ly under the Documentation directory, and mandatory-locking.txt is
101 called mandatory.txt)
102
104 This page is part of release 5.12 of the Linux man-pages project. A
105 description of the project, information about reporting bugs, and the
106 latest version of this page, can be found at
107 https://www.kernel.org/doc/man-pages/.
108
109
110
111GNU 2021-03-22 LOCKF(3)