]> git.ipfire.org Git - thirdparty/man-pages.git/blame - man2/fcntl.2
remove superflous white space in text
[thirdparty/man-pages.git] / man2 / fcntl.2
CommitLineData
fea681da
MK
1'\" t
2.\" Hey Emacs! This file is -*- nroff -*- source.
3.\"
4.\" This manpage is Copyright (C) 1992 Drew Eckhardt;
6883b3e7
MK
5.\" and Copyright (C) 1993 Michael Haardt, Ian Jackson;
6.\" and Copyright (C) 1998 Jamie Lokier;
7.\" and Copyright (C) 2002 Michael Kerrisk.
fea681da
MK
8.\"
9.\" Permission is granted to make and distribute verbatim copies of this
10.\" manual provided the copyright notice and this permission notice are
11.\" preserved on all copies.
12.\"
13.\" Permission is granted to copy and distribute modified versions of this
14.\" manual under the conditions for verbatim copying, provided that the
15.\" entire resulting derived work is distributed under the terms of a
16.\" permission notice identical to this one.
c13182ef 17.\"
fea681da
MK
18.\" Since the Linux kernel and libraries are constantly changing, this
19.\" manual page may be incorrect or out-of-date. The author(s) assume no
20.\" responsibility for errors or omissions, or for damages resulting from
21.\" the use of the information contained herein. The author(s) may not
22.\" have taken the same level of care in the production of this manual,
23.\" which is licensed free of charge, as they might when working
24.\" professionally.
c13182ef 25.\"
fea681da
MK
26.\" Formatted or processed versions of this manual, if unaccompanied by
27.\" the source, must acknowledge the copyright and authors of this work.
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
2f36a807
MK
50.\" multi-threaded processes, and generally cleaned
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.
fea681da 55.\"
ef203e45 56.TH FCNTL 2 2007-12-12 "Linux" "Linux Programmer's Manual"
fea681da
MK
57.SH NAME
58fcntl \- manipulate file descriptor
59.SH SYNOPSIS
60.nf
61.B #include <unistd.h>
62.B #include <fcntl.h>
63.sp
64.BI "int fcntl(int " fd ", int " cmd );
65.BI "int fcntl(int " fd ", int " cmd ", long " arg );
66.BI "int fcntl(int " fd ", int " cmd ", struct flock *" lock );
67.fi
68.SH DESCRIPTION
cff88e99
MK
69.BR fcntl ()
70performs one of the operations described below on the open file descriptor
fea681da 71.IR fd .
cff88e99 72The operation is determined by
fea681da 73.IR cmd .
cff88e99 74.SS "Duplicating a file descriptor"
fea681da
MK
75.TP
76.B F_DUPFD
77Find the lowest numbered available file descriptor
78greater than or equal to
79.I arg
80and make it be a copy of
81.IR fd .
cff88e99 82This is different from
fea681da
MK
83.BR dup2 (2)
84which uses exactly the descriptor specified.
85.sp
fea681da 86On success, the new descriptor is returned.
cff88e99
MK
87.sp
88See
89.BR dup (2)
90for further details.
ef203e45
MK
91.TP
92.BR F_DUPFD_CLOEXEC " (since Linux 2.6.24)"
93As for a
94.BR F_DUPFD ,
95but additionally set the
96close-on-exec flag for the duplicate descriptor.
97Specifying this flag permits a program to avoid an additional
0e92e8cc 98.BR fcntl ()
ef203e45
MK
99.B F_SETFD
100operation to set the
101.B FD_CLOEXEC
102flag.
103For an explanation of why this flag is useful,
104see the explanation of
105.B O_CLOEXEC
106in
107.BR open (2).
72da38ce 108.SS "File descriptor flags"
cff88e99 109The following commands manipulate the flags associated with
c13182ef 110a file descriptor.
cff88e99
MK
111Currently, only one such flag is defined:
112.BR FD_CLOEXEC ,
113the close-on-exec flag.
114If the
fea681da 115.B FD_CLOEXEC
cff88e99
MK
116bit is 0, the file descriptor will remain open across an
117.BR execve (2),
fea681da
MK
118otherwise it will be closed.
119.TP
cff88e99
MK
120.B F_GETFD
121Read the file descriptor flags.
122.TP
fea681da 123.B F_SETFD
cff88e99 124Set the file descriptor flags to the value specified by
fea681da 125.IR arg .
72da38ce 126.SS "File status flags"
08478724
MK
127Each open file description has certain associated status flags,
128initialized by
fea681da
MK
129.BR open (2)
130.\" or
131.\" .BR creat (2),
132and possibly modified by
2777b1ca 133.BR fcntl ().
c13182ef 134Duplicated file descriptors
cff88e99 135(made with
0bfa087b 136.BR dup (2),
cff88e99 137.BR fcntl (F_DUPFD),
0bfa087b 138.BR fork (2),
cff88e99
MK
139etc.) refer to the same open file description, and thus
140share the same file status flags.
fea681da 141.sp
cff88e99 142The file status flags and their semantics are described in
fea681da
MK
143.BR open (2).
144.TP
145.B F_GETFL
cff88e99 146Read the file status flags.
fea681da
MK
147.TP
148.B F_SETFL
cff88e99 149Set the file status flags to the value specified by
fea681da 150.IR arg .
c13182ef 151File access mode
cff88e99
MK
152.RB ( O_RDONLY ", " O_WRONLY ", " O_RDWR )
153and file creation flags
68b93ce2
MK
154(i.e.,
155.BR O_CREAT ", " O_EXCL ", " O_NOCTTY ", " O_TRUNC )
cff88e99 156in
fea681da
MK
157.I arg
158are ignored.
1c1e15ed 159On Linux this command can only change the
c13182ef 160.BR O_APPEND ,
1c1e15ed
MK
161.BR O_ASYNC ,
162.BR O_DIRECT ,
163.BR O_NOATIME ,
c13182ef 164and
0daa9e92 165.B O_NONBLOCK
1c1e15ed 166flags.
0e1ad98c 167.\" FIXME . According to POSIX.1-2001, O_SYNC should also be modifiable
c533af9d 168.\" via fcntl(2), but currently Linux does not permit this
92057f4d 169.\" See http://bugzilla.kernel.org/show_bug.cgi?id=5994
fea681da
MK
170.SS "Advisory locking"
171.BR F_GETLK ", " F_SETLK " and " F_SETLKW
172are used to acquire, release, and test for the existence of record
173locks (also known as file-segment or file-region locks).
174The third argument
175.I lock
176is a pointer to a structure that has at least the following fields
177(in unspecified order).
178.in +2n
179.nf
180.sp
181struct flock {
182 ...
183 short l_type; /* Type of lock: F_RDLCK,
184 F_WRLCK, F_UNLCK */
185 short l_whence; /* How to interpret l_start:
186 SEEK_SET, SEEK_CUR, SEEK_END */
187 off_t l_start; /* Starting offset for lock */
188 off_t l_len; /* Number of bytes to lock */
189 pid_t l_pid; /* PID of process blocking our lock
190 (F_GETLK only) */
191 ...
192};
193.fi
194.in -2n
195.P
196The
197.IR l_whence ", " l_start ", and " l_len
198fields of this structure specify the range of bytes we wish to lock.
199.I l_start
200is the starting offset for the lock, and is interpreted
201relative to either:
202the start of the file (if
203.I l_whence
204is
205.BR SEEK_SET );
206the current file offset (if
207.I l_whence
208is
209.BR SEEK_CUR );
210or the end of the file (if
211.I l_whence
212is
213.BR SEEK_END ).
214In the final two cases,
215.I l_start
216can be a negative number provided the
217offset does not lie before the start of the file.
218.I l_len
219is a non-negative integer (but see the NOTES below) specifying
220the number of bytes to be locked.
221Bytes past the end of the file may be locked,
222but not bytes before the start of the file.
223Specifying 0 for
224.I l_len
225has the special meaning: lock all bytes starting at the
226location specified by
227.IR l_whence " and " l_start
228through to the end of file, no matter how large the file grows.
229.P
230The
231.I l_type
232field can be used to place a read
233.RB ( F_RDLCK )
234or a write
1c287bbf 235.RB ( F_WRLCK )
fea681da
MK
236lock on a file.
237Any number of processes may hold a read lock (shared lock)
238on a file region, but only one process may hold a write lock
7c93fec0
MK
239(exclusive lock).
240An exclusive lock excludes all other locks,
fea681da
MK
241both shared and exclusive.
242A single process can hold only one type of lock on a file region;
243if a new lock is applied to an already-locked region,
8c450534 244then the existing lock is converted to the new lock type.
fea681da
MK
245(Such conversions may involve splitting, shrinking, or coalescing with
246an existing lock if the byte range specified by the new lock does not
247precisely coincide with the range of the existing lock.)
248.TP
249.B F_SETLK
250Acquire a lock (when
251.I l_type
252is
253.B F_RDLCK
254or
255.BR F_WRLCK )
256or release a lock (when
257.I l_type
258is
259.BR F_UNLCK )
260on the bytes specified by the
261.IR l_whence ", " l_start ", and " l_len
262fields of
263.IR lock .
264If a conflicting lock is held by another process,
265this call returns \-1 and sets
266.I errno
267to
268.B EACCES
269or
270.BR EAGAIN .
271.TP
272.B F_SETLKW
273As for
274.BR F_SETLK ,
275but if a conflicting lock is held on the file, then wait for that
276lock to be released.
277If a signal is caught while waiting, then the call is interrupted
278and (after the signal handler has returned)
279returns immediately (with return value \-1 and
280.I errno
281set to
282.BR EINTR ).
283.TP
284.B F_GETLK
285On input to this call,
286.I lock
287describes a lock we would like to place on the file.
288If the lock could be placed,
289.BR fcntl ()
290does not actually place it, but returns
291.B F_UNLCK
292in the
293.I l_type
294field of
295.I lock
296and leaves the other fields of the structure unchanged.
297If one or more incompatible locks would prevent
298this lock being placed, then
299.BR fcntl ()
300returns details about one of these locks in the
301.IR l_type ", " l_whence ", " l_start ", and " l_len
302fields of
303.I lock
304and sets
305.I l_pid
306to be the PID of the process holding that lock.
307.P
308In order to place a read lock,
309.I fd
310must be open for reading.
311In order to place a write lock,
312.I fd
313must be open for writing.
314To place both types of lock, open a file read-write.
315.P
316As well as being removed by an explicit
317.BR F_UNLCK ,
318record locks are automatically released when the process
319terminates or if it closes
320.I any
321file descriptor referring to a file on which locks are held.
322.\" (Additional file descriptors referring to the same file
323.\" may have been obtained by calls to
2777b1ca 324.\" .BR open "(2), " dup "(2), " dup2 "(2), or " fcntl ().)
fea681da
MK
325This is bad: it means that a process can lose the locks on
326a file like
327.I /etc/passwd
328or
329.I /etc/mtab
330when for some reason a library function decides to open, read
331and close it.
332.P
333Record locks are not inherited by a child created via
334.BR fork (2),
335but are preserved across an
336.BR execve (2).
337.P
338Because of the buffering performed by the
339.BR stdio (3)
340library, the use of record locking with routines in that package
341should be avoided; use
35e21ba7 342.BR read (2)
c13182ef 343and
35e21ba7 344.BR write (2)
fea681da 345instead.
fea681da
MK
346.SS "Mandatory locking"
347(Non-POSIX.)
348The above record locks may be either advisory or mandatory,
349and are advisory by default.
fea681da
MK
350
351Advisory locks are not enforced and are useful only between
c13182ef 352cooperating processes.
00d7bbcf 353
c13182ef 354Mandatory locks are enforced for all processes.
00d7bbcf
MK
355If a process tries to perform an incompatible access (e.g.,
356.BR read (2)
357or
358.BR write (2))
359on a file region that has an incompatible mandatory lock,
360then the result depends upon whether the
361.B O_NONBLOCK
362flag is enabled for its open file description.
c13182ef 363If the
00d7bbcf
MK
364.B O_NONBLOCK
365flag is not enabled, then
366system call is blocked until the lock is removed
367or converted to a mode that is compatible with the access.
c13182ef
MK
368If the
369.B O_NONBLOCK
00d7bbcf 370flag is enabled, then the system call fails with the error
0daa9e92 371.B EAGAIN
cb1c9135 372or
4df883b9 373.BR EWOULDBLOCK .
00d7bbcf
MK
374
375To make use of mandatory locks, mandatory locking must be enabled
c13182ef 376both on the file system that contains the file to be locked,
00d7bbcf
MK
377and on the file itself.
378Mandatory locking is enabled on a file system
379using the "\-o mand" option to
380.BR mount (8),
c13182ef 381or the
00d7bbcf 382.B MS_MANDLOCK
c13182ef 383flag for
00d7bbcf
MK
384.BR mount (2).
385Mandatory locking is enabled on a file by disabling
386group execute permission on the file and enabling the set-group-ID
387permission bit (see
388.BR chmod (1)
c13182ef 389and
00d7bbcf 390.BR chmod (2)).
a406395c
MK
391
392The Linux implementation of mandatory locking is unreliable. See BUGS
393below.
fea681da
MK
394.SS "Managing signals"
395.BR F_GETOWN ", " F_SETOWN ", " F_GETSIG " and " F_SETSIG
396are used to manage I/O availability signals:
397.TP
398.B F_GETOWN
8bd58774
MK
399Get the process ID or process group currently receiving
400.B SIGIO
401and
402.B SIGURG
403signals for events on file descriptor
fea681da 404.IR fd .
2f36a807
MK
405Process IDs are returned as positive values;
406process group IDs are returned as negative values (but see BUGS below).
fea681da
MK
407.TP
408.B F_SETOWN
8bd58774
MK
409Set the process ID or process group ID that will receive
410.B SIGIO
411and
412.B SIGURG
413signals for events on file descriptor
fea681da 414.IR fd .
2f36a807
MK
415A process ID is specified as a positive value;
416a process group ID is specified as a negative value.
9d2a7b1f
MK
417Most commonly, the calling process specifies itself as the owner
418(that is,
419.I arg
420is specified as
0bfa087b 421.BR getpid (2)).
fea681da
MK
422
423.\" From glibc.info:
424If you set the
425.B O_ASYNC
87eca6b7 426status flag on a file descriptor by using the
fea681da
MK
427.B F_SETFL
428command of
fc15ae54 429.BR fcntl (),
8bd58774
MK
430a
431.B SIGIO
432signal is sent whenever input or output becomes possible
fea681da 433on that file descriptor.
2f36a807 434.B F_SETSIG
8bd58774
MK
435can be used to obtain delivery of a signal other than
436.BR SIGIO .
9d2a7b1f
MK
437If this permission check fails, then the signal is
438silently discarded.
439
440Sending a signal to the owner process (group) specified by
441.B F_SETOWN
442is subject to the same permissions checks as are described for
443.BR kill (2),
444where the sending process is the one that employs
0daa9e92 445.B F_SETOWN
9d2a7b1f 446(but see BUGS below).
fea681da 447.sp
2f36a807
MK
448If the file descriptor
449.I fd
450refers to a socket,
fea681da 451.B F_SETOWN
2f36a807 452also selects
8bd58774
MK
453the recipient of
454.B SIGURG
455signals that are delivered when out-of-band
c13182ef 456data arrives on that socket.
8bd58774
MK
457.RB ( SIGURG
458is sent in any situation where
fea681da
MK
459.BR select (2)
460would report the socket as having an "exceptional condition".)
2f36a807 461.\" The following appears to be rubbish. It doesn't seem to
c13182ef 462.\" be true according to the kernel source, and I can write
2f36a807
MK
463.\" a program that gets a terminal-generated SIGIO even though
464.\" it is not the foreground process group of the terminal.
465.\" -- MTK, 8 Apr 05
c13182ef 466.\"
2f36a807
MK
467.\" If the file descriptor
468.\" .I fd
469.\" refers to a terminal device, then SIGIO
470.\" signals are sent to the foreground process group of the terminal.
471
472If a non-zero value is given to
473.B F_SETSIG
7c088cb1
MK
474in a multi-threaded process running with a threading library
475that supports thread groups (e.g., NPTL),
c13182ef 476then a positive value given to
2f36a807
MK
477.B F_SETOWN
478has a different meaning:
c13182ef 479.\" The relevant place in the (2.6) kernel source is the
2f36a807
MK
480.\" 'switch' in fs/fcntl.c::send_sigio_to_task() -- MTK, Apr 2005
481instead of being a process ID identifying a whole process,
482it is a thread ID identifying a specific thread within a process.
483Consequently, it may be necessary to pass
484.B F_SETOWN
485the result of
0bfa087b 486.BR gettid (2)
2f36a807 487instead of
0bfa087b 488.BR getpid (2)
2f36a807
MK
489to get sensible results when
490.B F_SETSIG
491is used.
492(In current Linux threading implementations,
493a main thread's thread ID is the same as its process ID.
494This means that a single-threaded program can equally use
0bfa087b 495.BR gettid (2)
2f36a807 496or
0bfa087b 497.BR getpid (2)
2f36a807
MK
498in this scenario.)
499Note, however, that the statements in this paragraph do not apply
8bd58774
MK
500to the
501.B SIGURG
502signal generated for out-of-band data on a socket:
2f36a807
MK
503this signal is always sent to either a process or a process group,
504depending on the value given to
505.BR F_SETOWN .
c13182ef 506.\" send_sigurg()/send_sigurg_to_task() bypasses
2f36a807 507.\" kill_fasync()/send_sigio()/send_sigio_to_task()
c13182ef 508.\" to directly call send_group_sig_info()
2f36a807 509.\" -- MTK, Apr 2005 (kernel 2.6.11)
7c088cb1
MK
510Note also that Linux imposes a limit on the
511number of real-time signals that may be queued to a
512process (see
513.BR getrlimit (2)
514and
515.BR signal (7))
c13182ef 516and if this limit is reached, then the kernel reverts to
8bd58774
MK
517delivering
518.BR SIGIO ,
519and this signal is delivered to the entire
7502db97 520process rather than to a specific thread.
7c088cb1 521.\" See fs/fcntl.c::send_sigio_to_task() (2.4/2.6) sources -- MTK, Apr 05
fea681da
MK
522.TP
523.B F_GETSIG
c13182ef 524Get the signal sent when input or output becomes possible.
8bd58774
MK
525A value of zero means
526.B SIGIO
527is sent.
528Any other value (including
529.BR SIGIO )
530is the
fea681da 531signal sent instead, and in this case additional info is available to
8bd58774
MK
532the signal handler if installed with
533.BR SA_SIGINFO .
fea681da
MK
534.TP
535.B F_SETSIG
c13182ef 536Sets the signal sent when input or output becomes possible.
8bd58774
MK
537A value of zero means to send the default
538.B SIGIO
539signal.
c13182ef 540Any other value (including
8bd58774
MK
541.BR SIGIO )
542is the signal to send instead, and in this case additional info
543is available to the signal handler if installed with
544.BR SA_SIGINFO .
fea681da 545.sp
2f36a807
MK
546Additionally, passing a non-zero value to
547.B F_SETSIG
548changes the signal recipient from a whole process to a specific thread
549within a process.
704a18f0 550See the description of
c13182ef 551.B F_SETOWN
2f36a807
MK
552for more details.
553.sp
554By using
555.B F_SETSIG
8bd58774
MK
556with a non-zero value, and setting
557.B SA_SIGINFO
558for the
fea681da
MK
559signal handler (see
560.BR sigaction (2)),
561extra information about I/O events is passed to
562the handler in a
563.I siginfo_t
564structure.
565If the
566.I si_code
8bd58774
MK
567field indicates the source is
568.BR SI_SIGIO ,
569the
fea681da 570.I si_fd
c13182ef
MK
571field gives the file descriptor associated with the event.
572Otherwise,
fea681da
MK
573there is no indication which file descriptors are pending, and you
574should use the usual mechanisms
575.RB ( select (2),
576.BR poll (2),
577.BR read (2)
578with
579.B O_NONBLOCK
580set etc.) to determine which file descriptors are available for I/O.
581.sp
988db661 582By selecting a real time signal (value >=
8bd58774
MK
583.BR SIGRTMIN ),
584multiple I/O events may be queued using the same signal numbers.
c13182ef
MK
585(Queuing is dependent on available memory).
586Extra information is available
8bd58774
MK
587if
588.B SA_SIGINFO
589is set for the signal handler, as above.
fea681da
MK
590.PP
591Using these mechanisms, a program can implement fully asynchronous I/O
592without using
593.BR select (2)
594or
595.BR poll (2)
596most of the time.
597.PP
598The use of
599.BR O_ASYNC ,
600.BR F_GETOWN ,
601.B F_SETOWN
602is specific to BSD and Linux.
603.B F_GETSIG
604and
605.B F_SETSIG
c13182ef
MK
606are Linux specific.
607POSIX has asynchronous I/O and the
fea681da
MK
608.I aio_sigevent
609structure to achieve similar things; these are also available
610in Linux as part of the GNU C Library (Glibc).
fea681da
MK
611.SS Leases
612.B F_SETLEASE
613and
614.B F_GETLEASE
ea53895c
MK
615(Linux 2.4 onwards) are used (respectively) to establish a new lease,
616and retrieve the current lease, on the open file description
617referred to by the file descriptor
fea681da
MK
618.IR fd .
619A file lease provides a mechanism whereby the process holding
620the lease (the "lease holder") is notified (via delivery of a signal)
a7a05888 621when a process (the "lease breaker") tries to
fea681da
MK
622.BR open (2)
623or
624.BR truncate (2)
ea53895c 625the file referred to by that file descriptor.
fea681da
MK
626.TP
627.B F_SETLEASE
628Set or remove a file lease according to which of the following
629values is specified in the integer
630.IR arg :
fea681da 631.RS
cedab449 632.TP
fea681da
MK
633.B F_RDLCK
634Take out a read lease.
1a6d974a 635This will cause the calling process to be notified when
a7a05888 636the file is opened for writing or is truncated.
1a6d974a
MK
637.\" The following became true in kernel 2.6.10:
638.\" See the man-pages-2.09 Changelog for further info.
c13182ef 639A read lease can only be placed on a file descriptor that
1a6d974a 640is opened read-only.
fea681da
MK
641.TP
642.B F_WRLCK
643Take out a write lease.
c13182ef 644This will cause the caller to be notified when
a7a05888 645the file is opened for reading or writing or is truncated.
ea53895c
MK
646A write lease may be placed on a file only if there are no
647other open file descriptors for the file.
fea681da
MK
648.TP
649.B F_UNLCK
650Remove our lease from the file.
651.RE
652.P
ea53895c
MK
653Leases are associated with an open file description (see
654.BR open (2)).
655This means that duplicate file descriptors (created by, for example,
656.BR fork (2)
657or
658.BR dup (2))
659refer to the same lease, and this lease may be modified
660or released using any of these descriptors.
661Furthermore, the lease is released by either an explicit
662.B F_UNLCK
663operation on any of these duplicate descriptors, or when all
664such descriptors have been closed.
fea681da
MK
665.P
666Leases may only be taken out on regular files.
667An unprivileged process may only take out a lease on a file whose
ea53895c 668UID (owner) matches the file system UID of the process.
fea681da
MK
669A process with the
670.B CAP_LEASE
671capability may take out leases on arbitrary files.
672.TP
673.B F_GETLEASE
ea53895c 674Indicates what type of lease is associated with the file descriptor
fea681da
MK
675.I fd
676by returning either
4df883b9 677.BR F_RDLCK ", " F_WRLCK ", or " F_UNLCK ,
ea53895c 678indicating, respectively, a read lease , a write lease, or no lease.
fea681da
MK
679(The third argument to
680.BR fcntl ()
681is omitted.)
682.PP
683When a process (the "lease breaker") performs an
0bfa087b 684.BR open (2)
fea681da 685or
0bfa087b 686.BR truncate (2)
fea681da
MK
687that conflicts with a lease established via
688.BR F_SETLEASE ,
b2216ffe
MK
689the system call is blocked by the kernel and
690the kernel notifies the lease holder by sending it a signal
8bd58774
MK
691.RB ( SIGIO
692by default).
fea681da
MK
693The lease holder should respond to receipt of this signal by doing
694whatever cleanup is required in preparation for the file to be
695accessed by another process (e.g., flushing cached buffers) and
696then either remove or downgrade its lease.
697A lease is removed by performing an
698.B F_SETLEASE
699command specifying
700.I arg
701as
702.BR F_UNLCK .
ea53895c 703If the lease holder currently holds a write lease on the file,
fea681da 704and the lease breaker is opening the file for reading,
ea53895c
MK
705then it is sufficient for the lease holder to downgrade
706the lease to a read lease.
fea681da
MK
707This is done by performing an
708.B F_SETLEASE
709command specifying
710.I arg
711as
712.BR F_RDLCK .
713
714If the lease holder fails to downgrade or remove the lease within
715the number of seconds specified in
716.I /proc/sys/fs/lease-break-time
717then the kernel forcibly removes or downgrades the lease holder's lease.
718
719Once the lease has been voluntarily or forcibly removed or downgraded,
720and assuming the lease breaker has not unblocked its system call,
721the kernel permits the lease breaker's system call to proceed.
722
b2216ffe 723If the lease breaker's blocked
0bfa087b 724.BR open (2)
c13182ef 725or
0bfa087b 726.BR truncate (2)
c13182ef
MK
727is interrupted by a signal handler,
728then the system call fails with the error
729.BR EINTR ,
b2216ffe 730but the other steps still occur as described above.
c13182ef 731If the lease breaker is killed by a signal while blocked in
0bfa087b 732.BR open (2)
c13182ef 733or
0bfa087b 734.BR truncate (2),
b2216ffe 735then the other steps still occur as described above.
c13182ef
MK
736If the lease breaker specifies the
737.B O_NONBLOCK
738flag when calling
0bfa087b 739.BR open (2),
c13182ef
MK
740then the call immediately fails with the error
741.BR EWOULDBLOCK ,
b2216ffe
MK
742but the other steps still occur as described above.
743
8bd58774
MK
744The default signal used to notify the lease holder is
745.BR SIGIO ,
fea681da
MK
746but this can be changed using the
747.B F_SETSIG
748command to
a7a05888 749.BR fcntl ().
fea681da
MK
750If a
751.B F_SETSIG
8bd58774
MK
752command is performed (even one specifying
753.BR SIGIO ),
754and the signal
755handler is established using
756.BR SA_SIGINFO ,
757then the handler will receive a
fea681da 758.I siginfo_t
704a18f0 759structure as its second argument, and the
fea681da
MK
760.I si_fd
761field of this argument will hold the descriptor of the leased file
762that has been accessed by another process.
763(This is useful if the caller holds leases against multiple files).
ace17ecf 764.SS "File and directory change notification (dnotify)"
fea681da
MK
765.TP
766.B F_NOTIFY
767(Linux 2.4 onwards)
768Provide notification when the directory referred to by
769.I fd
770or any of the files that it contains is changed.
771The events to be notified are specified in
772.IR arg ,
773which is a bit mask specified by ORing together zero or more of
774the following bits:
fea681da
MK
775.TS
776l l
777----
778lB l.
779Bit Description (event in directory)
780DN_ACCESS A file was accessed (read, pread, readv)
781DN_MODIFY A file was modified (write, pwrite,
782 writev, truncate, ftruncate)
783DN_CREATE A file was created (open, creat, mknod,
784 mkdir, link, symlink, rename)
785DN_DELETE A file was unlinked (unlink, rename to
786 another directory, rmdir)
787DN_RENAME A file was renamed within this
788 directory (rename)
789DN_ATTRIB The attributes of a file were changed
790 (chown, chmod, utime[s])
791.TE
792.sp
682edefb
MK
793(In order to obtain these definitions, the
794.B _GNU_SOURCE
795feature test macro must be defined.)
fea681da
MK
796.sp
797Directory notifications are normally "one-shot", and the application
798must re-register to receive further notifications.
799Alternatively, if
800.B DN_MULTISHOT
801is included in
802.IR arg ,
803then notification will remain in effect until explicitly removed.
804
805.\" The following does seem a poor API-design choice...
806A series of
807.B F_NOTIFY
808requests is cumulative, with the events in
809.I arg
810being added to the set already monitored.
811To disable notification of all events, make an
812.B F_NOTIFY
813call specifying
814.I arg
815as 0.
816.sp
817Notification occurs via delivery of a signal.
8bd58774
MK
818The default signal is
819.BR SIGIO ,
820but this can be changed using the
fea681da
MK
821.B F_SETSIG
822command to
823.BR fcntl ().
824In the latter case, the signal handler receives a
825.I siginfo_t
826structure as its second argument (if the handler was
8bd58774
MK
827established using
828.BR SA_SIGINFO )
829and the
fea681da
MK
830.I si_fd
831field of this structure contains the file descriptor which
832generated the notification (useful when establishing notification
833on multiple directories).
834.sp
835Especially when using
836.BR DN_MULTISHOT ,
a7fadb55 837a real time signal should be used for notification,
fea681da 838so that multiple notifications can be queued.
1363cf95
MK
839
840.B NOTE:
841New applications should consider using the
c13182ef 842.I inotify
1363cf95 843interface (available since kernel 2.6.13),
c13182ef
MK
844which provides a superior interface for obtaining notifications of
845file system events.
846See
1363cf95 847.BR inotify (7).
fea681da
MK
848.SH "RETURN VALUE"
849For a successful call, the return value depends on the operation:
850.TP 0.9i
851.B F_DUPFD
852The new descriptor.
853.TP
854.B F_GETFD
72da38ce 855Value of flags.
fea681da
MK
856.TP
857.B F_GETFL
858Value of flags.
859.TP
276adee1
MK
860.B F_GETLEASE
861Type of lease held on file descriptor.
862.TP
fea681da
MK
863.B F_GETOWN
864Value of descriptor owner.
865.TP
866.B F_GETSIG
867Value of signal sent when read or write becomes possible, or zero
8bd58774
MK
868for traditional
869.B SIGIO
870behavior.
fea681da
MK
871.TP
872All other commands
873Zero.
874.PP
875On error, \-1 is returned, and
876.I errno
877is set appropriately.
878.SH ERRORS
879.TP
880.BR EACCES " or " EAGAIN
881Operation is prohibited by locks held by other processes.
8548d8e2
MK
882.TP
883.B EAGAIN
884The operation is prohibited because the file has been memory-mapped by
fea681da
MK
885another process.
886.TP
887.B EBADF
888.I fd
889is not an open file descriptor, or the command was
890.B F_SETLK
891or
892.B F_SETLKW
893and the file descriptor open mode doesn't match with the
894type of lock requested.
895.TP
896.B EDEADLK
897It was detected that the specified
898.B F_SETLKW
899command would cause a deadlock.
900.TP
901.B EFAULT
902.I lock
903is outside your accessible address space.
904.TP
905.B EINTR
906For
907.BR F_SETLKW ,
908the command was interrupted by a signal.
909For
910.BR F_GETLK " and " F_SETLK ,
911the command was interrupted by a signal before the lock was checked or
c13182ef 912acquired.
75b94dc3 913Most likely when locking a remote file (e.g., locking over
fea681da
MK
914NFS), but can sometimes happen locally.
915.TP
916.B EINVAL
917For
918.BR F_DUPFD ,
919.I arg
c13182ef
MK
920is negative or is greater than the maximum allowable value.
921For
fea681da
MK
922.BR F_SETSIG ,
923.I arg
924is not an allowable signal number.
925.TP
926.B EMFILE
927For
928.BR F_DUPFD ,
929the process already has the maximum number of file descriptors open.
930.TP
931.B ENOLCK
932Too many segment locks open, lock table is full, or a remote locking
75b94dc3 933protocol failed (e.g., locking over NFS).
fea681da
MK
934.TP
935.B EPERM
936Attempted to clear the
937.B O_APPEND
938flag on a file that has the append-only attribute set.
a1d5f77c
MK
939.SH "CONFORMING TO"
940SVr4, 4.3BSD, POSIX.1-2001.
8bd58774
MK
941Only the operations
942.BR F_DUPFD ,
943.BR F_GETFD ,
944.BR F_SETFD ,
945.BR F_GETFL ,
946.BR F_SETFL ,
947.BR F_GETLK ,
948.BR F_SETLK ,
949.BR F_SETLKW ,
950.BR F_GETOWN ,
951and
0daa9e92 952.B F_SETOWN
8bd58774 953are specified in POSIX.1-2001.
a1d5f77c 954
8bd58774
MK
955.BR F_GETSIG ,
956.BR F_SETSIG ,
957.BR F_NOTIFY ,
958.BR F_GETLEASE ,
959and
0daa9e92 960.B F_SETLEASE
a1d5f77c 961are Linux specific.
988db661 962(Define the
0daa9e92 963.B _GNU_SOURCE
8bd58774 964macro to obtain these definitions.)
a1d5f77c
MK
965.\" .PP
966.\" SVr4 documents additional EIO, ENOLINK and EOVERFLOW error conditions.
fea681da
MK
967.SH NOTES
968The errors returned by
0bfa087b 969.BR dup2 (2)
fea681da
MK
970are different from those returned by
971.BR F_DUPFD .
972
973Since kernel 2.0, there is no interaction between the types of lock
974placed by
975.BR flock (2)
976and
2777b1ca 977.BR fcntl ().
fea681da 978
97c1eac8 979POSIX.1-2001 allows
fea681da 980.I l_len
c13182ef
MK
981to be negative.
982(And if it is, the interval described by the lock
fea681da
MK
983covers bytes
984.IR l_start + l_len
985up to and including
8729177b 986.IR l_start \-1.)
fea681da
MK
987This is supported by Linux since Linux 2.4.21 and 2.5.49.
988
989Several systems have more fields in
990.I "struct flock"
75b94dc3 991such as, for example,
fea681da 992.IR l_sysid .
a3c945b3
MK
993.\" e.g., Solaris 8 documents this field in fcntl(2), and Irix 6.5
994.\" documents it in fcntl(5). mtk, May 2007
fea681da
MK
995Clearly,
996.I l_pid
997alone is not going to be very useful if the process holding the lock
998may live on a different machine.
7c3b0e95 999.SH BUGS
8505c1f1
MK
1000A limitation of the Linux system call conventions on some
1001architectures (notably x86) means that if a (negative)
1002process group ID to be returned by
7c3b0e95 1003.B F_GETOWN
8729177b 1004falls in the range \-1 to \-4095, then the return value is wrongly
7c3b0e95 1005interpreted by glibc as an error in the system call;
8505c1f1 1006.\" glibc source: sysdeps/unix/sysv/linux/i386/sysdep.h
7c3b0e95
MK
1007that is, the return value of
1008.BR fcntl ()
1009will be \-1, and
1010.I errno
1011will contain the (positive) process group ID.
0e1ad98c 1012.\" mtk, Dec 04: some limited testing on alpha and ia64 seems to
8505c1f1 1013.\" indicate that ANY negative PGID value will cause F_GETOWN
704a18f0 1014.\" to misinterpret the return as an error. Some other architectures
988db661 1015.\" seem to have the same range check as x86.
9d2a7b1f
MK
1016
1017In Linux 2.4 and earlier, there is bug that can occur
1018when an unprivileged process uses
1019.B F_SETOWN
1020to specify the owner
c13182ef 1021of a socket file descriptor
9d2a7b1f
MK
1022as a process (group) other than the caller.
1023In this case,
1024.BR fcntl ()
1025can return \-1 with
1026.I errno
1027set to
1028.BR EPERM ,
1029even when the owner process (group) is one that the caller
1030has permission to send signals to.
1031Despite this error return, the file descriptor owner is set,
1032and signals will be sent to the owner.
a406395c
MK
1033
1034The implementation of mandatory locking in all known versions of Linux
43ea3545
MK
1035is subject to races which render it unreliable:
1036.\" http://marc.info/?l=linux-kernel&m=119013491707153&w=2
1037a
a406395c
MK
1038.BR write (2)
1039call that overlaps with a lock may modify data after the mandatory lock is
43ea3545 1040acquired;
a406395c
MK
1041a
1042.BR read (2)
1043call that overlaps with a lock may detect changes to data that were made
43ea3545
MK
1044only after a write lock was acquired.
1045Similar races exist between mandatory locks and
1046.BR mmap (2).
1047It is therefore inadvisable to rely on mandatory locking.
fea681da
MK
1048.SH "SEE ALSO"
1049.BR dup2 (2),
1050.BR flock (2),
1051.BR open (2),
1052.BR socket (2),
1053.BR lockf (3),
50e5322c 1054.BR capabilities (7),
a8e7c990 1055.BR feature_test_macros (7)
fea681da 1056.P
d880b21c 1057See also
988db661
MK
1058.IR Documentation/locks.txt ,
1059.IR Documentation/mandatory.txt ,
1060and
d880b21c
MK
1061.I Documentation/dnotify.txt
1062in the kernel source.