]> git.ipfire.org Git - thirdparty/man-pages.git/blame - man2/fcntl.2
syscalls.2: SEE ALSO: add intro(2)
[thirdparty/man-pages.git] / man2 / fcntl.2
CommitLineData
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
63fcntl \- 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 ()
73performs one of the operations described below on the open file descriptor
fea681da 74.IR fd .
cff88e99 75The operation is determined by
fea681da 76.IR cmd .
68e0696a
MK
77
78.BR fcntl ()
79can take an optional third argument.
80Whether or not this argument is required is determined by
81.IR cmd .
63a6efda 82The required argument type is indicated in parentheses after each
68e0696a
MK
83.I cmd
84name (in most cases, the required type is
ed9821bb 85.IR int ,
68e0696a
MK
86and we identify the argument using the name
87.IR arg ),
88or
89.I void
90is 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
94Find the lowest numbered available file descriptor
95greater than or equal to
96.I arg
97and make it be a copy of
98.IR fd .
cff88e99 99This is different from
68e0696a 100.BR dup2 (2),
fea681da 101which uses exactly the descriptor specified.
ca92ce95 102.IP
fea681da 103On success, the new descriptor is returned.
ca92ce95 104.IP
cff88e99
MK
105See
106.BR dup (2)
107for further details.
ef203e45 108.TP
ed9821bb 109.BR F_DUPFD_CLOEXEC " (\fIint\fP; since Linux 2.6.24)"
635d3959 110As for
ef203e45
MK
111.BR F_DUPFD ,
112but additionally set the
113close-on-exec flag for the duplicate descriptor.
114Specifying this flag permits a program to avoid an additional
0e92e8cc 115.BR fcntl ()
ef203e45
MK
116.B F_SETFD
117operation to set the
118.B FD_CLOEXEC
119flag.
120For an explanation of why this flag is useful,
e4b4caa6 121see the description of
ef203e45
MK
122.B O_CLOEXEC
123in
124.BR open (2).
73d8cece 125.SS File descriptor flags
cff88e99 126The following commands manipulate the flags associated with
c13182ef 127a file descriptor.
cff88e99
MK
128Currently, only one such flag is defined:
129.BR FD_CLOEXEC ,
130the close-on-exec flag.
131If the
fea681da 132.B FD_CLOEXEC
cff88e99
MK
133bit is 0, the file descriptor will remain open across an
134.BR execve (2),
fea681da
MK
135otherwise it will be closed.
136.TP
68e0696a
MK
137.BR F_GETFD " (\fIvoid\fP)"
138Read the file descriptor flags;
139.I arg
140is ignored.
cff88e99 141.TP
ed9821bb 142.BR F_SETFD " (\fIint\fP)"
cff88e99 143Set the file descriptor flags to the value specified by
fea681da 144.IR arg .
73d8cece 145.SS File status flags
08478724
MK
146Each open file description has certain associated status flags,
147initialized by
fea681da
MK
148.BR open (2)
149.\" or
150.\" .BR creat (2),
151and possibly modified by
2777b1ca 152.BR fcntl ().
c13182ef 153Duplicated file descriptors
cff88e99 154(made with
0bfa087b 155.BR dup (2),
cff88e99 156.BR fcntl (F_DUPFD),
0bfa087b 157.BR fork (2),
cff88e99
MK
158etc.) refer to the same open file description, and thus
159share the same file status flags.
ca92ce95 160
cff88e99 161The 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 165Get the file access mode and the file status flags;
68e0696a
MK
166.I arg
167is ignored.
fea681da 168.TP
ed9821bb 169.BR F_SETFL " (\fIint\fP)"
cff88e99 170Set the file status flags to the value specified by
fea681da 171.IR arg .
c13182ef 172File access mode
cff88e99
MK
173.RB ( O_RDONLY ", " O_WRONLY ", " O_RDWR )
174and file creation flags
68b93ce2
MK
175(i.e.,
176.BR O_CREAT ", " O_EXCL ", " O_NOCTTY ", " O_TRUNC )
cff88e99 177in
fea681da
MK
178.I arg
179are ignored.
79592066 180On 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 185and
0daa9e92 186.B O_NONBLOCK
1c1e15ed 187flags.
639b33d7
MK
188It is not possible to change the
189.BR O_DSYNC
190and
191.BR O_SYNC
192flags; see BUGS, below.
73d8cece 193.SS Advisory locking
a797afac 194.BR F_GETLK ", " F_SETLK ", and " F_SETLKW
fea681da
MK
195are used to acquire, release, and test for the existence of record
196locks (also known as file-segment or file-region locks).
68e0696a
MK
197The third argument,
198.IR lock ,
fea681da
MK
199is 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
204struct 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
219The
220.IR l_whence ", " l_start ", and " l_len
221fields of this structure specify the range of bytes we wish to lock.
057dce78
MK
222Bytes past the end of the file may be locked,
223but not bytes before the start of the file.
224
fea681da
MK
225.I l_start
226is the starting offset for the lock, and is interpreted
227relative to either:
228the start of the file (if
229.I l_whence
230is
231.BR SEEK_SET );
232the current file offset (if
233.I l_whence
234is
235.BR SEEK_CUR );
236or the end of the file (if
237.I l_whence
238is
239.BR SEEK_END ).
240In the final two cases,
241.I l_start
242can be a negative number provided the
243offset does not lie before the start of the file.
057dce78 244
fea681da 245.I l_len
057dce78
MK
246specifies the number of bytes to be locked.
247If
248.I l_len
249is positive, then the range to be locked covers bytes
250.I l_start
251up to and including
95dadb39 252.IR l_start + l_len \-1.
fea681da
MK
253Specifying 0 for
254.I l_len
255has the special meaning: lock all bytes starting at the
256location specified by
257.IR l_whence " and " l_start
258through to the end of file, no matter how large the file grows.
057dce78
MK
259
260POSIX.1-2001 allows (but does not require)
261an implementation to support a negative
262.I l_len
263value; if
264.I l_len
265is negative, the interval described by
266.I lock
267covers bytes
268.IR l_start + l_len
269up to and including
270.IR l_start \-1.
271This is supported by Linux since kernel versions 2.4.21 and 2.5.49.
272
fea681da
MK
273The
274.I l_type
275field can be used to place a read
276.RB ( F_RDLCK )
277or a write
1c287bbf 278.RB ( F_WRLCK )
fea681da
MK
279lock on a file.
280Any number of processes may hold a read lock (shared lock)
281on a file region, but only one process may hold a write lock
7c93fec0
MK
282(exclusive lock).
283An exclusive lock excludes all other locks,
fea681da
MK
284both shared and exclusive.
285A single process can hold only one type of lock on a file region;
286if a new lock is applied to an already-locked region,
8c450534 287then the existing lock is converted to the new lock type.
fea681da
MK
288(Such conversions may involve splitting, shrinking, or coalescing with
289an existing lock if the byte range specified by the new lock does not
290precisely coincide with the range of the existing lock.)
291.TP
68e0696a 292.BR F_SETLK " (\fIstruct flock *\fP)"
fea681da
MK
293Acquire a lock (when
294.I l_type
295is
296.B F_RDLCK
297or
298.BR F_WRLCK )
299or release a lock (when
300.I l_type
301is
302.BR F_UNLCK )
303on the bytes specified by the
304.IR l_whence ", " l_start ", and " l_len
305fields of
306.IR lock .
307If a conflicting lock is held by another process,
308this call returns \-1 and sets
309.I errno
310to
311.B EACCES
312or
313.BR EAGAIN .
314.TP
68e0696a 315.BR F_SETLKW " (\fIstruct flock *\fP)"
fea681da
MK
316As for
317.BR F_SETLK ,
318but if a conflicting lock is held on the file, then wait for that
319lock to be released.
320If a signal is caught while waiting, then the call is interrupted
321and (after the signal handler has returned)
322returns immediately (with return value \-1 and
323.I errno
324set to
01538d0d
MK
325.BR EINTR ;
326see
327.BR signal (7)).
fea681da 328.TP
68e0696a 329.BR F_GETLK " (\fIstruct flock *\fP)"
fea681da
MK
330On input to this call,
331.I lock
332describes a lock we would like to place on the file.
333If the lock could be placed,
334.BR fcntl ()
335does not actually place it, but returns
336.B F_UNLCK
337in the
338.I l_type
339field of
340.I lock
341and leaves the other fields of the structure unchanged.
342If one or more incompatible locks would prevent
343this lock being placed, then
344.BR fcntl ()
345returns details about one of these locks in the
346.IR l_type ", " l_whence ", " l_start ", and " l_len
347fields of
348.I lock
349and sets
350.I l_pid
351to be the PID of the process holding that lock.
352.P
353In order to place a read lock,
354.I fd
355must be open for reading.
356In order to place a write lock,
357.I fd
358must be open for writing.
359To place both types of lock, open a file read-write.
360.P
361As well as being removed by an explicit
362.BR F_UNLCK ,
363record locks are automatically released when the process
364terminates or if it closes
365.I any
366file 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
370This is bad: it means that a process can lose the locks on
371a file like
372.I /etc/passwd
373or
374.I /etc/mtab
375when for some reason a library function decides to open, read
376and close it.
377.P
378Record locks are not inherited by a child created via
379.BR fork (2),
380but are preserved across an
381.BR execve (2).
382.P
383Because of the buffering performed by the
384.BR stdio (3)
385library, the use of record locking with routines in that package
386should be avoided; use
35e21ba7 387.BR read (2)
c13182ef 388and
35e21ba7 389.BR write (2)
fea681da 390instead.
73d8cece 391.SS Mandatory locking
fea681da
MK
392(Non-POSIX.)
393The above record locks may be either advisory or mandatory,
394and are advisory by default.
fea681da
MK
395
396Advisory locks are not enforced and are useful only between
c13182ef 397cooperating processes.
00d7bbcf 398
c13182ef 399Mandatory locks are enforced for all processes.
00d7bbcf
MK
400If a process tries to perform an incompatible access (e.g.,
401.BR read (2)
402or
403.BR write (2))
404on a file region that has an incompatible mandatory lock,
405then the result depends upon whether the
406.B O_NONBLOCK
407flag is enabled for its open file description.
c13182ef 408If the
00d7bbcf
MK
409.B O_NONBLOCK
410flag is not enabled, then
411system call is blocked until the lock is removed
412or converted to a mode that is compatible with the access.
c13182ef
MK
413If the
414.B O_NONBLOCK
00d7bbcf 415flag is enabled, then the system call fails with the error