]>
Commit | Line | Data |
---|---|---|
fea681da | 1 | '\" t |
fea681da | 2 | .\" This manpage is Copyright (C) 1992 Drew Eckhardt; |
6883b3e7 MK |
3 | .\" and Copyright (C) 1993 Michael Haardt, Ian Jackson; |
4 | .\" and Copyright (C) 1998 Jamie Lokier; | |
14f7d1a4 | 5 | .\" and Copyright (C) 2002-2010 Michael Kerrisk. |
fea681da | 6 | .\" |
93015253 | 7 | .\" %%%LICENSE_START(VERBATIM) |
fea681da MK |
8 | .\" Permission is granted to make and distribute verbatim copies of this |
9 | .\" manual provided the copyright notice and this permission notice are | |
10 | .\" preserved on all copies. | |
11 | .\" | |
12 | .\" Permission is granted to copy and distribute modified versions of this | |
13 | .\" manual under the conditions for verbatim copying, provided that the | |
14 | .\" entire resulting derived work is distributed under the terms of a | |
15 | .\" permission notice identical to this one. | |
c13182ef | 16 | .\" |
fea681da MK |
17 | .\" Since the Linux kernel and libraries are constantly changing, this |
18 | .\" manual page may be incorrect or out-of-date. The author(s) assume no | |
19 | .\" responsibility for errors or omissions, or for damages resulting from | |
20 | .\" the use of the information contained herein. The author(s) may not | |
21 | .\" have taken the same level of care in the production of this manual, | |
22 | .\" which is licensed free of charge, as they might when working | |
23 | .\" professionally. | |
c13182ef | 24 | .\" |
fea681da MK |
25 | .\" Formatted or processed versions of this manual, if unaccompanied by |
26 | .\" the source, must acknowledge the copyright and authors of this work. | |
4b72fb64 | 27 | .\" %%%LICENSE_END |
fea681da MK |
28 | .\" |
29 | .\" Modified 1993-07-24 by Rik Faith <faith@cs.unc.edu> | |
30 | .\" Modified 1995-09-26 by Andries Brouwer <aeb@cwi.nl> | |
31 | .\" and again on 960413 and 980804 and 981223. | |
32 | .\" Modified 1998-12-11 by Jamie Lokier <jamie@imbolc.ucc.ie> | |
33 | .\" Applied correction by Christian Ehrhardt - aeb, 990712 | |
c11b1abf | 34 | .\" Modified 2002-04-23 by Michael Kerrisk <mtk.manpages@gmail.com> |
fea681da MK |
35 | .\" Added note on F_SETFL and O_DIRECT |
36 | .\" Complete rewrite + expansion of material on file locking | |
37 | .\" Incorporated description of F_NOTIFY, drawing on | |
38 | .\" Stephen Rothwell's notes in Documentation/dnotify.txt. | |
39 | .\" Added description of F_SETLEASE and F_GETLEASE | |
40 | .\" Corrected and polished, aeb, 020527. | |
c11b1abf | 41 | .\" Modified 2004-03-03 by Michael Kerrisk <mtk.manpages@gmail.com> |
fea681da MK |
42 | .\" Modified description of file leases: fixed some errors of detail |
43 | .\" Replaced the term "lease contestant" by "lease breaker" | |
c11b1abf | 44 | .\" Modified, 27 May 2004, Michael Kerrisk <mtk.manpages@gmail.com> |
fea681da | 45 | .\" Added notes on capability requirements |
1c1e15ed | 46 | .\" Modified 2004-12-08, added O_NOATIME after note from Martin Pool |
c13182ef | 47 | .\" 2004-12-10, mtk, noted F_GETOWN bug after suggestion from aeb. |
2f36a807 | 48 | .\" 2005-04-08 Jamie Lokier <jamie@shareable.org>, mtk |
d9bfdb9c | 49 | .\" Described behavior of F_SETOWN/F_SETSIG in |
71fea607 | 50 | .\" multithreaded processes, and generally cleaned |
2f36a807 | 51 | .\" up the discussion of F_SETOWN. |
c13182ef | 52 | .\" 2005-05-20, Johannes Nicolai <johannes.nicolai@hpi.uni-potsdam.de>, |
9d2a7b1f MK |
53 | .\" mtk: Noted F_SETOWN bug for socket file descriptor in Linux 2.4 |
54 | .\" and earlier. Added text on permissions required to send signal. | |
dd164bfc MK |
55 | .\" 2009-09-30, Michael Kerrisk |
56 | .\" Note obsolete F_SETOWN behavior with threads. | |
57 | .\" Document F_SETOWN_EX and F_GETOWN_EX | |
14f7d1a4 MK |
58 | .\" 2010-06-17, Michael Kerrisk |
59 | .\" Document F_SETPIPE_SZ and F_GETPIPE_SZ. | |
fea681da | 60 | .\" |
639b33d7 | 61 | .TH FCNTL 2 2014-01-21 "Linux" "Linux Programmer's Manual" |
fea681da MK |
62 | .SH NAME |
63 | fcntl \- manipulate file descriptor | |
64 | .SH SYNOPSIS | |
65 | .nf | |
66 | .B #include <unistd.h> | |
67 | .B #include <fcntl.h> | |
68 | .sp | |
68e0696a | 69 | .BI "int fcntl(int " fd ", int " cmd ", ... /* " arg " */ );" |
fea681da MK |
70 | .fi |
71 | .SH DESCRIPTION | |
cff88e99 MK |
72 | .BR fcntl () |
73 | performs one of the operations described below on the open file descriptor | |
fea681da | 74 | .IR fd . |
cff88e99 | 75 | The operation is determined by |
fea681da | 76 | .IR cmd . |
68e0696a MK |
77 | |
78 | .BR fcntl () | |
79 | can take an optional third argument. | |
80 | Whether or not this argument is required is determined by | |
81 | .IR cmd . | |
63a6efda | 82 | The required argument type is indicated in parentheses after each |
68e0696a MK |
83 | .I cmd |
84 | name (in most cases, the required type is | |
ed9821bb | 85 | .IR int , |
68e0696a MK |
86 | and we identify the argument using the name |
87 | .IR arg ), | |
88 | or | |
89 | .I void | |
90 | is specified if the argument is not required. | |
73d8cece | 91 | .SS Duplicating a file descriptor |
fea681da | 92 | .TP |
ed9821bb | 93 | .BR F_DUPFD " (\fIint\fP)" |
fea681da MK |
94 | Find the lowest numbered available file descriptor |
95 | greater than or equal to | |
96 | .I arg | |
97 | and make it be a copy of | |
98 | .IR fd . | |
cff88e99 | 99 | This is different from |
68e0696a | 100 | .BR dup2 (2), |
fea681da | 101 | which uses exactly the descriptor specified. |
ca92ce95 | 102 | .IP |
fea681da | 103 | On success, the new descriptor is returned. |
ca92ce95 | 104 | .IP |
cff88e99 MK |
105 | See |
106 | .BR dup (2) | |
107 | for further details. | |
ef203e45 | 108 | .TP |
ed9821bb | 109 | .BR F_DUPFD_CLOEXEC " (\fIint\fP; since Linux 2.6.24)" |
635d3959 | 110 | As for |
ef203e45 MK |
111 | .BR F_DUPFD , |
112 | but additionally set the | |
113 | close-on-exec flag for the duplicate descriptor. | |
114 | Specifying this flag permits a program to avoid an additional | |
0e92e8cc | 115 | .BR fcntl () |
ef203e45 MK |
116 | .B F_SETFD |
117 | operation to set the | |
118 | .B FD_CLOEXEC | |
119 | flag. | |
120 | For an explanation of why this flag is useful, | |
e4b4caa6 | 121 | see the description of |
ef203e45 MK |
122 | .B O_CLOEXEC |
123 | in | |
124 | .BR open (2). | |
73d8cece | 125 | .SS File descriptor flags |
cff88e99 | 126 | The following commands manipulate the flags associated with |
c13182ef | 127 | a file descriptor. |
cff88e99 MK |
128 | Currently, only one such flag is defined: |
129 | .BR FD_CLOEXEC , | |
130 | the close-on-exec flag. | |
131 | If the | |
fea681da | 132 | .B FD_CLOEXEC |
cff88e99 MK |
133 | bit is 0, the file descriptor will remain open across an |
134 | .BR execve (2), | |
fea681da MK |
135 | otherwise it will be closed. |
136 | .TP | |
68e0696a MK |
137 | .BR F_GETFD " (\fIvoid\fP)" |
138 | Read the file descriptor flags; | |
139 | .I arg | |
140 | is ignored. | |
cff88e99 | 141 | .TP |
ed9821bb | 142 | .BR F_SETFD " (\fIint\fP)" |
cff88e99 | 143 | Set the file descriptor flags to the value specified by |
fea681da | 144 | .IR arg . |
73d8cece | 145 | .SS File status flags |
08478724 MK |
146 | Each open file description has certain associated status flags, |
147 | initialized by | |
fea681da MK |
148 | .BR open (2) |
149 | .\" or | |
150 | .\" .BR creat (2), | |
151 | and possibly modified by | |
2777b1ca | 152 | .BR fcntl (). |
c13182ef | 153 | Duplicated file descriptors |
cff88e99 | 154 | (made with |
0bfa087b | 155 | .BR dup (2), |
cff88e99 | 156 | .BR fcntl (F_DUPFD), |
0bfa087b | 157 | .BR fork (2), |
cff88e99 MK |
158 | etc.) refer to the same open file description, and thus |
159 | share the same file status flags. | |
ca92ce95 | 160 | |
cff88e99 | 161 | The file status flags and their semantics are described in |
fea681da MK |
162 | .BR open (2). |
163 | .TP | |
68e0696a | 164 | .BR F_GETFL " (\fIvoid\fP)" |
a9ddf390 | 165 | Get the file access mode and the file status flags; |
68e0696a MK |
166 | .I arg |
167 | is ignored. | |
fea681da | 168 | .TP |
ed9821bb | 169 | .BR F_SETFL " (\fIint\fP)" |
cff88e99 | 170 | Set the file status flags to the value specified by |
fea681da | 171 | .IR arg . |
c13182ef | 172 | File access mode |
cff88e99 MK |
173 | .RB ( O_RDONLY ", " O_WRONLY ", " O_RDWR ) |
174 | and file creation flags | |
68b93ce2 MK |
175 | (i.e., |
176 | .BR O_CREAT ", " O_EXCL ", " O_NOCTTY ", " O_TRUNC ) | |
cff88e99 | 177 | in |
fea681da MK |
178 | .I arg |
179 | are ignored. | |
79592066 | 180 | On Linux this command can change only the |
c13182ef | 181 | .BR O_APPEND , |
1c1e15ed MK |
182 | .BR O_ASYNC , |
183 | .BR O_DIRECT , | |
184 | .BR O_NOATIME , | |
c13182ef | 185 | and |
0daa9e92 | 186 | .B O_NONBLOCK |
1c1e15ed | 187 | flags. |
639b33d7 MK |
188 | It is not possible to change the |
189 | .BR O_DSYNC | |
190 | and | |
191 | .BR O_SYNC | |
192 | flags; see BUGS, below. | |
73d8cece | 193 | .SS Advisory locking |
a797afac | 194 | .BR F_GETLK ", " F_SETLK ", and " F_SETLKW |
fea681da MK |
195 | are used to acquire, release, and test for the existence of record |
196 | locks (also known as file-segment or file-region locks). | |
68e0696a MK |
197 | The third argument, |
198 | .IR lock , | |
fea681da MK |
199 | is a pointer to a structure that has at least the following fields |
200 | (in unspecified order). | |
088a639b | 201 | .in +4n |
fea681da MK |
202 | .nf |
203 | .sp | |
204 | struct flock { | |
205 | ... | |
206 | short l_type; /* Type of lock: F_RDLCK, | |
207 | F_WRLCK, F_UNLCK */ | |
208 | short l_whence; /* How to interpret l_start: | |
209 | SEEK_SET, SEEK_CUR, SEEK_END */ | |
210 | off_t l_start; /* Starting offset for lock */ | |
211 | off_t l_len; /* Number of bytes to lock */ | |
212 | pid_t l_pid; /* PID of process blocking our lock | |
213 | (F_GETLK only) */ | |
214 | ... | |
215 | }; | |
216 | .fi | |
088a639b | 217 | .in |
fea681da MK |
218 | .P |
219 | The | |
220 | .IR l_whence ", " l_start ", and " l_len | |
221 | fields of this structure specify the range of bytes we wish to lock. | |
057dce78 MK |
222 | Bytes past the end of the file may be locked, |
223 | but not bytes before the start of the file. | |
224 | ||
fea681da MK |
225 | .I l_start |
226 | is the starting offset for the lock, and is interpreted | |
227 | relative to either: | |
228 | the start of the file (if | |
229 | .I l_whence | |
230 | is | |
231 | .BR SEEK_SET ); | |
232 | the current file offset (if | |
233 | .I l_whence | |
234 | is | |
235 | .BR SEEK_CUR ); | |
236 | or the end of the file (if | |
237 | .I l_whence | |
238 | is | |
239 | .BR SEEK_END ). | |
240 | In the final two cases, | |
241 | .I l_start | |
242 | can be a negative number provided the | |
243 | offset does not lie before the start of the file. | |
057dce78 | 244 | |
fea681da | 245 | .I l_len |
057dce78 MK |
246 | specifies the number of bytes to be locked. |
247 | If | |
248 | .I l_len | |
249 | is positive, then the range to be locked covers bytes | |
250 | .I l_start | |
251 | up to and including | |
95dadb39 | 252 | .IR l_start + l_len \-1. |
fea681da MK |
253 | Specifying 0 for |
254 | .I l_len | |
255 | has the special meaning: lock all bytes starting at the | |
256 | location specified by | |
257 | .IR l_whence " and " l_start | |
258 | through to the end of file, no matter how large the file grows. | |
057dce78 MK |
259 | |
260 | POSIX.1-2001 allows (but does not require) | |
261 | an implementation to support a negative | |
262 | .I l_len | |
263 | value; if | |
264 | .I l_len | |
265 | is negative, the interval described by | |
266 | .I lock | |
267 | covers bytes | |
268 | .IR l_start + l_len | |
269 | up to and including | |
270 | .IR l_start \-1. | |
271 | This is supported by Linux since kernel versions 2.4.21 and 2.5.49. | |
272 | ||
fea681da MK |
273 | The |
274 | .I l_type | |
275 | field can be used to place a read | |
276 | .RB ( F_RDLCK ) | |
277 | or a write | |
1c287bbf | 278 | .RB ( F_WRLCK ) |
fea681da MK |
279 | lock on a file. |
280 | Any number of processes may hold a read lock (shared lock) | |
281 | on a file region, but only one process may hold a write lock | |
7c93fec0 MK |
282 | (exclusive lock). |
283 | An exclusive lock excludes all other locks, | |
fea681da MK |
284 | both shared and exclusive. |
285 | A single process can hold only one type of lock on a file region; | |
286 | if a new lock is applied to an already-locked region, | |
8c450534 | 287 | then the existing lock is converted to the new lock type. |
fea681da MK |
288 | (Such conversions may involve splitting, shrinking, or coalescing with |
289 | an existing lock if the byte range specified by the new lock does not | |
290 | precisely coincide with the range of the existing lock.) | |
291 | .TP | |
68e0696a | 292 | .BR F_SETLK " (\fIstruct flock *\fP)" |
fea681da MK |
293 | Acquire a lock (when |
294 | .I l_type | |
295 | is | |
296 | .B F_RDLCK | |
297 | or | |
298 | .BR F_WRLCK ) | |
299 | or release a lock (when | |
300 | .I l_type | |
301 | is | |
302 | .BR F_UNLCK ) | |
303 | on the bytes specified by the | |
304 | .IR l_whence ", " l_start ", and " l_len | |
305 | fields of | |
306 | .IR lock . | |
307 | If a conflicting lock is held by another process, | |
308 | this call returns \-1 and sets | |
309 | .I errno | |
310 | to | |
311 | .B EACCES | |
312 | or | |
313 | .BR EAGAIN . | |
314 | .TP | |
68e0696a | 315 | .BR F_SETLKW " (\fIstruct flock *\fP)" |
fea681da MK |
316 | As for |
317 | .BR F_SETLK , | |
318 | but if a conflicting lock is held on the file, then wait for that | |
319 | lock to be released. | |
320 | If a signal is caught while waiting, then the call is interrupted | |
321 | and (after the signal handler has returned) | |
322 | returns immediately (with return value \-1 and | |
323 | .I errno | |
324 | set to | |
01538d0d MK |
325 | .BR EINTR ; |
326 | see | |
327 | .BR signal (7)). | |
fea681da | 328 | .TP |
68e0696a | 329 | .BR F_GETLK " (\fIstruct flock *\fP)" |
fea681da MK |
330 | On input to this call, |
331 | .I lock | |
332 | describes a lock we would like to place on the file. | |
333 | If the lock could be placed, | |
334 | .BR fcntl () | |
335 | does not actually place it, but returns | |
336 | .B F_UNLCK | |
337 | in the | |
338 | .I l_type | |
339 | field of | |
340 | .I lock | |
341 | and leaves the other fields of the structure unchanged. | |
342 | If one or more incompatible locks would prevent | |
343 | this lock being placed, then | |
344 | .BR fcntl () | |
345 | returns details about one of these locks in the | |
346 | .IR l_type ", " l_whence ", " l_start ", and " l_len | |
347 | fields of | |
348 | .I lock | |
349 | and sets | |
350 | .I l_pid | |
351 | to be the PID of the process holding that lock. | |
352 | .P | |
353 | In order to place a read lock, | |
354 | .I fd | |
355 | must be open for reading. | |
356 | In order to place a write lock, | |
357 | .I fd | |
358 | must be open for writing. | |
359 | To place both types of lock, open a file read-write. | |
360 | .P | |
361 | As well as being removed by an explicit | |
362 | .BR F_UNLCK , | |
363 | record locks are automatically released when the process | |
364 | terminates or if it closes | |
365 | .I any | |
366 | file descriptor referring to a file on which locks are held. | |
367 | .\" (Additional file descriptors referring to the same file | |
368 | .\" may have been obtained by calls to | |
2777b1ca | 369 | .\" .BR open "(2), " dup "(2), " dup2 "(2), or " fcntl ().) |
fea681da MK |
370 | This is bad: it means that a process can lose the locks on |
371 | a file like | |
372 | .I /etc/passwd | |
373 | or | |
374 | .I /etc/mtab | |
375 | when for some reason a library function decides to open, read | |
376 | and close it. | |
377 | .P | |
378 | Record locks are not inherited by a child created via | |
379 | .BR fork (2), | |
380 | but are preserved across an | |
381 | .BR execve (2). | |
382 | .P | |
383 | Because of the buffering performed by the | |
384 | .BR stdio (3) | |
385 | library, the use of record locking with routines in that package | |
386 | should be avoided; use | |
35e21ba7 | 387 | .BR read (2) |
c13182ef | 388 | and |
35e21ba7 | 389 | .BR write (2) |
fea681da | 390 | instead. |
73d8cece | 391 | .SS Mandatory locking |
fea681da MK |
392 | (Non-POSIX.) |
393 | The above record locks may be either advisory or mandatory, | |
394 | and are advisory by default. | |
fea681da MK |
395 | |
396 | Advisory locks are not enforced and are useful only between | |
c13182ef | 397 | cooperating processes. |
00d7bbcf | 398 | |
c13182ef | 399 | Mandatory locks are enforced for all processes. |
00d7bbcf MK |
400 | If a process tries to perform an incompatible access (e.g., |
401 | .BR read (2) | |
402 | or | |
403 | .BR write (2)) | |
404 | on a file region that has an incompatible mandatory lock, | |
405 | then the result depends upon whether the | |
406 | .B O_NONBLOCK | |
407 | flag is enabled for its open file description. | |
c13182ef | 408 | If the |
00d7bbcf MK |
409 | .B O_NONBLOCK |
410 | flag is not enabled, then | |
411 | system call is blocked until the lock is removed | |
412 | or converted to a mode that is compatible with the access. | |
c13182ef MK |
413 | If the |
414 | .B O_NONBLOCK | |
00d7bbcf | 415 | flag is enabled, then the system call fails with the error |