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