1LOCKF(3P) POSIX Programmer's Manual LOCKF(3P)
2
3
4
6 This manual page is part of the POSIX Programmer's Manual. The Linux
7 implementation of this interface may differ (consult the corresponding
8 Linux manual page for details of Linux behavior), or the interface may
9 not be implemented on Linux.
10
11
13 lockf — record locking on files
14
16 #include <unistd.h>
17
18 int lockf(int fildes, int function, off_t size);
19
21 The lockf() function shall lock sections of a file with advisory-mode
22 locks. Calls to lockf() from threads in other processes which attempt
23 to lock the locked file section shall either return an error value or
24 block until the section becomes unlocked. All the locks for a process
25 are removed when the process terminates. Record locking with lockf()
26 shall be supported for regular files and may be supported for other
27 files.
28
29 The fildes argument is an open file descriptor. To establish a lock
30 with this function, the file descriptor shall be opened with write-only
31 permission (O_WRONLY) or with read/write permission (O_RDWR).
32
33 The function argument is a control value which specifies the action to
34 be taken. The permissible values for function are defined in <unistd.h>
35 as follows:
36
37 ┌─────────┬──────────────────────────────────────────────┐
38 │Function │ Description │
39 ├─────────┼──────────────────────────────────────────────┤
40 │F_ULOCK │ Unlock locked sections. │
41 │F_LOCK │ Lock a section for exclusive use. │
42 │F_TLOCK │ Test and lock a section for exclusive use. │
43 │F_TEST │ Test a section for locks by other processes. │
44 └─────────┴──────────────────────────────────────────────┘
45 F_TEST shall detect if a lock by another process is present on the
46 specified section.
47
48 F_LOCK and F_TLOCK shall both lock a section of a file if the section
49 is available.
50
51 F_ULOCK shall remove locks from a section of the file.
52
53 The size argument is the number of contiguous bytes to be locked or
54 unlocked. The section to be locked or unlocked starts at the current
55 offset in the file and extends forward for a positive size or backward
56 for a negative size (the preceding bytes up to but not including the
57 current offset). If size is 0, the section from the current offset
58 through the largest possible file offset shall be locked (that is, from
59 the current offset through the present or any future end-of-file). An
60 area need not be allocated to the file to be locked because locks may
61 exist past the end-of-file.
62
63 The sections locked with F_LOCK or F_TLOCK may, in whole or in part,
64 contain or be contained by a previously locked section for the same
65 process. When this occurs, or if adjacent locked sections would occur,
66 the sections shall be combined into a single locked section. If the
67 request would cause the number of locks to exceed a system-imposed
68 limit, the request shall fail.
69
70 F_LOCK and F_TLOCK requests differ only by the action taken if the sec‐
71 tion is not available. F_LOCK shall block the calling thread until the
72 section is available. F_TLOCK shall cause the function to fail if the
73 section is already locked by another process.
74
75 File locks shall be released on first close by the locking process of
76 any file descriptor for the file.
77
78 F_ULOCK requests may release (wholly or in part) one or more locked
79 sections controlled by the process. Locked sections shall be unlocked
80 starting at the current file offset through size bytes or to the end-
81 of-file if size is (off_t)0. When all of a locked section is not
82 released (that is, when the beginning or end of the area to be unlocked
83 falls within a locked section), the remaining portions of that section
84 shall remain locked by the process. Releasing the center portion of a
85 locked section shall cause the remaining locked beginning and end por‐
86 tions to become two separate locked sections. If the request would
87 cause the number of locks in the system to exceed a system-imposed
88 limit, the request shall fail.
89
90 A potential for deadlock occurs if the threads of a process controlling
91 a locked section are blocked by accessing a locked section of another
92 process. If the system detects that deadlock would occur, lockf() shall
93 fail with an [EDEADLK] error.
94
95 The interaction between fcntl() and lockf() locks is unspecified.
96
97 Blocking on a section shall be interrupted by any signal.
98
99 An F_ULOCK request in which size is non-zero and the offset of the last
100 byte of the requested section is the maximum value for an object of
101 type off_t, when the process has an existing lock in which size is 0
102 and which includes the last byte of the requested section, shall be
103 treated as a request to unlock from the start of the requested section
104 with a size equal to 0. Otherwise, an F_ULOCK request shall attempt to
105 unlock only the requested section.
106
107 Attempting to lock a section of a file that is associated with a
108 buffered stream produces unspecified results.
109
111 Upon successful completion, lockf() shall return 0. Otherwise, it shall
112 return −1, set errno to indicate an error, and existing locks shall not
113 be changed.
114
116 The lockf() function shall fail if:
117
118 EBADF The fildes argument is not a valid open file descriptor; or
119 function is F_LOCK or F_TLOCK and fildes is not a valid file
120 descriptor open for writing.
121
122 EACCES or EAGAIN
123 The function argument is F_TLOCK or F_TEST and the section is
124 already locked by another process.
125
126 EDEADLK
127 The function argument is F_LOCK and a deadlock is detected.
128
129 EINTR A signal was caught during execution of the function.
130
131 EINVAL The function argument is not one of F_LOCK, F_TLOCK, F_TEST, or
132 F_ULOCK; or size plus the current file offset is less than 0.
133
134 EOVERFLOW
135 The offset of the first, or if size is not 0 then the last, byte
136 in the requested section cannot be represented correctly in an
137 object of type off_t.
138
139 The lockf() function may fail if:
140
141 EAGAIN The function argument is F_LOCK or F_TLOCK and the file is
142 mapped with mmap().
143
144 EDEADLK or ENOLCK
145 The function argument is F_LOCK, F_TLOCK, or F_ULOCK, and the
146 request would cause the number of locks to exceed a system-
147 imposed limit.
148
149 EOPNOTSUPP or EINVAL
150 The implementation does not support the locking of files of the
151 type indicated by the fildes argument.
152
153 The following sections are informative.
154
156 Locking a Portion of a File
157 In the following example, a file named /home/cnd/mod1 is being modi‐
158 fied. Other processes that use locking are prevented from changing it
159 during this process. Only the first 10000 bytes are locked, and the
160 lock call fails if another process has any part of this area locked
161 already.
162
163 #include <fcntl.h>
164 #include <unistd.h>
165
166 int fildes;
167 int status;
168 ...
169 fildes = open("/home/cnd/mod1", O_RDWR);
170 status = lockf(fildes, F_TLOCK, (off_t)10000);
171
173 Record-locking should not be used in combination with the fopen(),
174 fread(), fwrite(), and other stdio functions. Instead, the more primi‐
175 tive, non-buffered functions (such as open()) should be used. Unex‐
176 pected results may occur in processes that do buffering in the user
177 address space. The process may later read/write data which is/was
178 locked. The stdio functions are the most common source of unexpected
179 buffering.
180
181 The alarm() function may be used to provide a timeout facility in
182 applications requiring it.
183
185 None.
186
188 None.
189
191 alarm(), chmod(), close(), creat(), fcntl(), fopen(), mmap(), open(),
192 read(), write()
193
194 The Base Definitions volume of POSIX.1‐2008, <unistd.h>
195
197 Portions of this text are reprinted and reproduced in electronic form
198 from IEEE Std 1003.1, 2013 Edition, Standard for Information Technology
199 -- Portable Operating System Interface (POSIX), The Open Group Base
200 Specifications Issue 7, Copyright (C) 2013 by the Institute of Electri‐
201 cal and Electronics Engineers, Inc and The Open Group. (This is
202 POSIX.1-2008 with the 2013 Technical Corrigendum 1 applied.) In the
203 event of any discrepancy between this version and the original IEEE and
204 The Open Group Standard, the original IEEE and The Open Group Standard
205 is the referee document. The original Standard can be obtained online
206 at http://www.unix.org/online.html .
207
208 Any typographical or formatting errors that appear in this page are
209 most likely to have been introduced during the conversion of the source
210 files to man page format. To report such errors, see https://www.ker‐
211 nel.org/doc/man-pages/reporting_bugs.html .
212
213
214
215IEEE/The Open Group 2013 LOCKF(3P)