signal.7: Since Linux 3.8, read(2) on an inotify FD is restartable with SA_RESTART
[man-pages.git] / man2 / fcntl.2
blob70758d2fcd99efbe3d5029a6c18b650ac2791a67
1 '\" t
2 .\" This manpage is Copyright (C) 1992 Drew Eckhardt;
3 .\" and Copyright (C) 1993 Michael Haardt, Ian Jackson;
4 .\" and Copyright (C) 1998 Jamie Lokier;
5 .\" and Copyright (C) 2002-2010, 2014 Michael Kerrisk;
6 .\" and Copyright (C) 2014 Jeff Layton
7 .\" and Copyright (C) 2014 David Herrmann
8 .\"
9 .\" %%%LICENSE_START(VERBATIM)
10 .\" Permission is granted to make and distribute verbatim copies of this
11 .\" manual provided the copyright notice and this permission notice are
12 .\" preserved on all copies.
13 .\"
14 .\" Permission is granted to copy and distribute modified versions of this
15 .\" manual under the conditions for verbatim copying, provided that the
16 .\" entire resulting derived work is distributed under the terms of a
17 .\" permission notice identical to this one.
18 .\"
19 .\" Since the Linux kernel and libraries are constantly changing, this
20 .\" manual page may be incorrect or out-of-date.  The author(s) assume no
21 .\" responsibility for errors or omissions, or for damages resulting from
22 .\" the use of the information contained herein.  The author(s) may not
23 .\" have taken the same level of care in the production of this manual,
24 .\" which is licensed free of charge, as they might when working
25 .\" professionally.
26 .\"
27 .\" Formatted or processed versions of this manual, if unaccompanied by
28 .\" the source, must acknowledge the copyright and authors of this work.
29 .\" %%%LICENSE_END
30 .\"
31 .\" Modified 1993-07-24 by Rik Faith <faith@cs.unc.edu>
32 .\" Modified 1995-09-26 by Andries Brouwer <aeb@cwi.nl>
33 .\" and again on 960413 and 980804 and 981223.
34 .\" Modified 1998-12-11 by Jamie Lokier <jamie@imbolc.ucc.ie>
35 .\" Applied correction by Christian Ehrhardt - aeb, 990712
36 .\" Modified 2002-04-23 by Michael Kerrisk <mtk.manpages@gmail.com>
37 .\"     Added note on F_SETFL and O_DIRECT
38 .\"     Complete rewrite + expansion of material on file locking
39 .\"     Incorporated description of F_NOTIFY, drawing on
40 .\"             Stephen Rothwell's notes in Documentation/dnotify.txt.
41 .\"     Added description of F_SETLEASE and F_GETLEASE
42 .\" Corrected and polished, aeb, 020527.
43 .\" Modified 2004-03-03 by Michael Kerrisk <mtk.manpages@gmail.com>
44 .\"     Modified description of file leases: fixed some errors of detail
45 .\"     Replaced the term "lease contestant" by "lease breaker"
46 .\" Modified, 27 May 2004, Michael Kerrisk <mtk.manpages@gmail.com>
47 .\"     Added notes on capability requirements
48 .\" Modified 2004-12-08, added O_NOATIME after note from Martin Pool
49 .\" 2004-12-10, mtk, noted F_GETOWN bug after suggestion from aeb.
50 .\" 2005-04-08 Jamie Lokier <jamie@shareable.org>, mtk
51 .\"     Described behavior of F_SETOWN/F_SETSIG in
52 .\"     multithreaded processes, and generally cleaned
53 .\"     up the discussion of F_SETOWN.
54 .\" 2005-05-20, Johannes Nicolai <johannes.nicolai@hpi.uni-potsdam.de>,
55 .\"     mtk: Noted F_SETOWN bug for socket file descriptor in Linux 2.4
56 .\"     and earlier.  Added text on permissions required to send signal.
57 .\" 2009-09-30, Michael Kerrisk
58 .\"     Note obsolete F_SETOWN behavior with threads.
59 .\"     Document F_SETOWN_EX and F_GETOWN_EX
60 .\" 2010-06-17, Michael Kerrisk
61 .\"     Document F_SETPIPE_SZ and F_GETPIPE_SZ.
62 .\" 2014-07-08, David Herrmann <dh.herrmann@gmail.com>
63 .\"     Document F_ADD_SEALS and F_GET_SEALS
64 .\"
65 .TH FCNTL 2 2017-05-03 "Linux" "Linux Programmer's Manual"
66 .SH NAME
67 fcntl \- manipulate file descriptor
68 .SH SYNOPSIS
69 .nf
70 .B #include <unistd.h>
71 .B #include <fcntl.h>
72 .sp
73 .BI "int fcntl(int " fd ", int " cmd ", ... /* " arg " */ );"
74 .fi
75 .SH DESCRIPTION
76 .BR fcntl ()
77 performs one of the operations described below on the open file descriptor
78 .IR fd .
79 The operation is determined by
80 .IR cmd .
82 .BR fcntl ()
83 can take an optional third argument.
84 Whether or not this argument is required is determined by
85 .IR cmd .
86 The required argument type is indicated in parentheses after each
87 .I cmd
88 name (in most cases, the required type is
89 .IR int ,
90 and we identify the argument using the name
91 .IR arg ),
93 .I void
94 is specified if the argument is not required.
96 Certain of the operations below are supported only since a particular
97 Linux kernel version.
98 The preferred method of checking whether the host kernel supports
99 a particular operation is to invoke
100 .BR fcntl ()
101 with the desired
102 .IR cmd
103 value and then test whether the call failed with
104 .BR EINVAL ,
105 indicating that the kernel does not recognize this value.
106 .SS Duplicating a file descriptor
108 .BR F_DUPFD " (\fIint\fP)"
109 Duplicate the file descriptor
110 .IR fd
111 using the lowest-numbered available file descriptor greater than or equal to
112 .IR arg .
113 This is different from
114 .BR dup2 (2),
115 which uses exactly the file descriptor specified.
117 On success, the new file descriptor is returned.
120 .BR dup (2)
121 for further details.
123 .BR F_DUPFD_CLOEXEC " (\fIint\fP; since Linux 2.6.24)"
124 As for
125 .BR F_DUPFD ,
126 but additionally set the
127 close-on-exec flag for the duplicate file descriptor.
128 Specifying this flag permits a program to avoid an additional
129 .BR fcntl ()
130 .B F_SETFD
131 operation to set the
132 .B FD_CLOEXEC
133 flag.
134 For an explanation of why this flag is useful,
135 see the description of
136 .B O_CLOEXEC
138 .BR open (2).
139 .SS File descriptor flags
140 The following commands manipulate the flags associated with
141 a file descriptor.
142 Currently, only one such flag is defined:
143 .BR FD_CLOEXEC ,
144 the close-on-exec flag.
145 If the
146 .B FD_CLOEXEC
147 bit is set,
148 the file descriptor will automatically be closed during a successful
149 .BR execve (2).
150 (If the
151 .BR execve (2)
152 fails, the file descriptor is left open.)
153 If the
154 .B FD_CLOEXEC
155 bit is not set, the file descriptor will remain open across an
156 .BR execve (2).
158 .BR F_GETFD " (\fIvoid\fP)"
159 Return (as the function result) the file descriptor flags;
160 .I arg
161 is ignored.
163 .BR F_SETFD " (\fIint\fP)"
164 Set the file descriptor flags to the value specified by
165 .IR arg .
167 In multithreaded programs, using
168 .BR fcntl ()
169 .B F_SETFD
170 to set the close-on-exec flag at the same time as another thread performs a
171 .BR fork (2)
172 plus
173 .BR execve (2)
174 is vulnerable to a race condition that may unintentionally leak
175 the file descriptor to the program executed in the child process.
176 See the discussion of the
177 .BR O_CLOEXEC
178 flag in
179 .BR open (2)
180 for details and a remedy to the problem.
181 .SS File status flags
182 Each open file description has certain associated status flags,
183 initialized by
184 .BR open (2)
185 .\" or
186 .\" .BR creat (2),
187 and possibly modified by
188 .BR fcntl ().
189 Duplicated file descriptors
190 (made with
191 .BR dup (2),
192 .BR fcntl (F_DUPFD),
193 .BR fork (2),
194 etc.) refer to the same open file description, and thus
195 share the same file status flags.
197 The file status flags and their semantics are described in
198 .BR open (2).
200 .BR F_GETFL " (\fIvoid\fP)"
201 Return (as the function result)
202 the file access mode and the file status flags;
203 .I arg
204 is ignored.
206 .BR F_SETFL " (\fIint\fP)"
207 Set the file status flags to the value specified by
208 .IR arg .
209 File access mode
210 .RB ( O_RDONLY ", " O_WRONLY ", " O_RDWR )
211 and file creation flags
212 (i.e.,
213 .BR O_CREAT ", " O_EXCL ", " O_NOCTTY ", " O_TRUNC )
215 .I arg
216 are ignored.
217 On Linux, this command can change only the
218 .BR O_APPEND ,
219 .BR O_ASYNC ,
220 .BR O_DIRECT ,
221 .BR O_NOATIME ,
223 .B O_NONBLOCK
224 flags.
225 It is not possible to change the
226 .BR O_DSYNC
228 .BR O_SYNC
229 flags; see BUGS, below.
230 .SS Advisory record locking
231 Linux implements traditional ("process-associated") UNIX record locks,
232 as standardized by POSIX.
233 For a Linux-specific alternative with better semantics,
234 see the discussion of open file description locks below.
236 .BR F_SETLK ,
237 .BR F_SETLKW ,
239 .BR F_GETLK
240 are used to acquire, release, and test for the existence of record
241 locks (also known as byte-range, file-segment, or file-region locks).
242 The third argument,
243 .IR lock ,
244 is a pointer to a structure that has at least the following fields
245 (in unspecified order).
246 .in +4n
249 struct flock {
250     ...
251     short l_type;    /* Type of lock: F_RDLCK,
252                         F_WRLCK, F_UNLCK */
253     short l_whence;  /* How to interpret l_start:
254                         SEEK_SET, SEEK_CUR, SEEK_END */
255     off_t l_start;   /* Starting offset for lock */
256     off_t l_len;     /* Number of bytes to lock */
257     pid_t l_pid;     /* PID of process blocking our lock
258                         (set by F_GETLK and F_OFD_GETLK) */
259     ...
265 .IR l_whence ", " l_start ", and " l_len
266 fields of this structure specify the range of bytes we wish to lock.
267 Bytes past the end of the file may be locked,
268 but not bytes before the start of the file.
270 .I l_start
271 is the starting offset for the lock, and is interpreted
272 relative to either:
273 the start of the file (if
274 .I l_whence
276 .BR SEEK_SET );
277 the current file offset (if
278 .I l_whence
280 .BR SEEK_CUR );
281 or the end of the file (if
282 .I l_whence
284 .BR SEEK_END ).
285 In the final two cases,
286 .I l_start
287 can be a negative number provided the
288 offset does not lie before the start of the file.
290 .I l_len
291 specifies the number of bytes to be locked.
293 .I l_len
294 is positive, then the range to be locked covers bytes
295 .I l_start
296 up to and including
297 .IR l_start + l_len \-1.
298 Specifying 0 for
299 .I l_len
300 has the special meaning: lock all bytes starting at the
301 location specified by
302 .IR l_whence " and " l_start
303 through to the end of file, no matter how large the file grows.
305 POSIX.1-2001 allows (but does not require)
306 an implementation to support a negative
307 .I l_len
308 value; if
309 .I l_len
310 is negative, the interval described by
311 .I lock
312 covers bytes
313 .IR l_start + l_len
314 up to and including
315 .IR l_start \-1.
316 This is supported by Linux since kernel versions 2.4.21 and 2.5.49.
319 .I l_type
320 field can be used to place a read
321 .RB ( F_RDLCK )
322 or a write
323 .RB ( F_WRLCK )
324 lock on a file.
325 Any number of processes may hold a read lock (shared lock)
326 on a file region, but only one process may hold a write lock
327 (exclusive lock).
328 An exclusive lock excludes all other locks,
329 both shared and exclusive.
330 A single process can hold only one type of lock on a file region;
331 if a new lock is applied to an already-locked region,
332 then the existing lock is converted to the new lock type.
333 (Such conversions may involve splitting, shrinking, or coalescing with
334 an existing lock if the byte range specified by the new lock does not
335 precisely coincide with the range of the existing lock.)
337 .BR F_SETLK " (\fIstruct flock *\fP)"
338 Acquire a lock (when
339 .I l_type
341 .B F_RDLCK
343 .BR F_WRLCK )
344 or release a lock (when
345 .I l_type
347 .BR F_UNLCK )
348 on the bytes specified by the
349 .IR l_whence ", " l_start ", and " l_len
350 fields of
351 .IR lock .
352 If a conflicting lock is held by another process,
353 this call returns \-1 and sets
354 .I errno
356 .B EACCES
358 .BR EAGAIN .
359 (The error returned in this case differs across implementations,
360 so POSIX requires a portable application to check for both errors.)
362 .BR F_SETLKW " (\fIstruct flock *\fP)"
363 As for
364 .BR F_SETLK ,
365 but if a conflicting lock is held on the file, then wait for that
366 lock to be released.
367 If a signal is caught while waiting, then the call is interrupted
368 and (after the signal handler has returned)
369 returns immediately (with return value \-1 and
370 .I errno
371 set to
372 .BR EINTR ;
374 .BR signal (7)).
376 .BR F_GETLK " (\fIstruct flock *\fP)"
377 On input to this call,
378 .I lock
379 describes a lock we would like to place on the file.
380 If the lock could be placed,
381 .BR fcntl ()
382 does not actually place it, but returns
383 .B F_UNLCK
384 in the
385 .I l_type
386 field of
387 .I lock
388 and leaves the other fields of the structure unchanged.
390 If one or more incompatible locks would prevent
391 this lock being placed, then
392 .BR fcntl ()
393 returns details about one of those locks in the
394 .IR l_type ", " l_whence ", " l_start ", and " l_len
395 fields of
396 .IR lock .
397 If the conflicting lock is a traditional (process-associated) record lock,
398 then the
399 .I l_pid
400 field is set to the PID of the process holding that lock.
401 If the conflicting lock is an open file description lock, then
402 .I l_pid
403 is set to \-1.
404 Note that the returned information
405 may already be out of date by the time the caller inspects it.
407 In order to place a read lock,
408 .I fd
409 must be open for reading.
410 In order to place a write lock,
411 .I fd
412 must be open for writing.
413 To place both types of lock, open a file read-write.
415 When placing locks with
416 .BR F_SETLKW ,
417 the kernel detects
418 .IR deadlocks ,
419 whereby two or more processes have their
420 lock requests mutually blocked by locks held by the other processes.
421 For example, suppose process A holds a write lock on byte 100 of a file,
422 and process B holds a write lock on byte 200.
423 If each process then attempts to lock the byte already
424 locked by the other process using
425 .BR F_SETLKW ,
426 then, without deadlock detection,
427 both processes would remain blocked indefinitely.
428 When the kernel detects such deadlocks,
429 it causes one of the blocking lock requests to immediately fail with the error
430 .BR EDEADLK ;
431 an application that encounters such an error should release
432 some of its locks to allow other applications to proceed before
433 attempting regain the locks that it requires.
434 Circular deadlocks involving more than two processes are also detected.
435 Note, however, that there are limitations to the kernel's
436 deadlock-detection algorithm; see BUGS.
438 As well as being removed by an explicit
439 .BR F_UNLCK ,
440 record locks are automatically released when the process terminates.
442 Record locks are not inherited by a child created via
443 .BR fork (2),
444 but are preserved across an
445 .BR execve (2).
447 Because of the buffering performed by the
448 .BR stdio (3)
449 library, the use of record locking with routines in that package
450 should be avoided; use
451 .BR read (2)
453 .BR write (2)
454 instead.
456 The record locks described above are associated with the process
457 (unlike the open file description locks described below).
458 This has some unfortunate consequences:
459 .IP * 3
460 If a process closes
461 .I any
462 file descriptor referring to a file,
463 then all of the process's locks on that file are released,
464 regardless of the file descriptor(s) on which the locks were obtained.
465 .\" (Additional file descriptors referring to the same file
466 .\" may have been obtained by calls to
467 .\" .BR open "(2), " dup "(2), " dup2 "(2), or " fcntl ().)
468 This is bad: it means that a process can lose its locks on
469 a file such as
470 .I /etc/passwd
472 .I /etc/mtab
473 when for some reason a library function decides to open, read,
474 and close the same file.
475 .IP *
476 The threads in a process share locks.
477 In other words,
478 a multithreaded program can't use record locking to ensure
479 that threads don't simultaneously access the same region of a file.
481 Open file description locks solve both of these problems.
482 .SS Open file description locks (non-POSIX)
483 Open file description locks are advisory byte-range locks whose operation is
484 in most respects identical to the traditional record locks described above.
485 This lock type is Linux-specific,
486 and available since Linux 3.15.
487 (There is a proposal with the Austin Group
488 .\" FIXME . Review progress into POSIX
489 .\" http://austingroupbugs.net/view.php?id=768
490 to include this lock type in the next revision of POSIX.1.)
491 For an explanation of open file descriptions, see
492 .BR open (2).
494 The principal difference between the two lock types
495 is that whereas traditional record locks
496 are associated with a process,
497 open file description locks are associated with the
498 open file description on which they are acquired,
499 much like locks acquired with
500 .BR flock (2).
501 Consequently (and unlike traditional advisory record locks),
502 open file description locks are inherited across
503 .BR fork (2)
504 (and
505 .BR clone (2)
506 with
507 .BR CLONE_FILES ),
508 and are only automatically released on the last close
509 of the open file description,
510 instead of being released on any close of the file.
512 Conflicting lock combinations
513 (i.e., a read lock and a write lock or two write locks)
514 where one lock is an open file description lock and the other
515 is a traditional record lock conflict
516 even when they are acquired by the same process on the same file descriptor.
518 Open file description locks placed via the same open file description
519 (i.e., via the same file descriptor,
520 or via a duplicate of the file descriptor created by
521 .BR fork (2),
522 .BR dup (2),
523 .BR fcntl ()
524 .BR F_DUPFD ,
525 and so on) are always compatible:
526 if a new lock is placed on an already locked region,
527 then the existing lock is converted to the new lock type.
528 (Such conversions may result in splitting, shrinking, or coalescing with
529 an existing lock as discussed above.)
531 On the other hand, open file description locks may conflict with
532 each other when they are acquired via different open file descriptions.
533 Thus, the threads in a multithreaded program can use
534 open file description locks to synchronize access to a file region
535 by having each thread perform its own
536 .BR open (2)
537 on the file and applying locks via the resulting file descriptor.
539 As with traditional advisory locks, the third argument to
540 .BR fcntl (),
541 .IR lock ,
542 is a pointer to an
543 .IR flock
544 structure.
545 By contrast with traditional record locks, the
546 .I l_pid
547 field of that structure must be set to zero
548 when using the commands described below.
550 The commands for working with open file description locks are analogous
551 to those used with traditional locks:
553 .BR F_OFD_SETLK " (\fIstruct flock *\fP)"
554 Acquire an open file description lock (when
555 .I l_type
557 .B F_RDLCK
559 .BR F_WRLCK )
560 or release an open file description lock (when
561 .I l_type
563 .BR F_UNLCK )
564 on the bytes specified by the
565 .IR l_whence ", " l_start ", and " l_len
566 fields of
567 .IR lock .
568 If a conflicting lock is held by another process,
569 this call returns \-1 and sets
570 .I errno
572 .BR EAGAIN .
574 .BR F_OFD_SETLKW " (\fIstruct flock *\fP)"
575 As for
576 .BR F_OFD_SETLK ,
577 but if a conflicting lock is held on the file, then wait for that lock to be
578 released.
579 If a signal is caught while waiting, then the call is interrupted
580 and (after the signal handler has returned) returns immediately
581 (with return value \-1 and
582 .I errno
583 set to
584 .BR EINTR ;
586 .BR signal (7)).
588 .BR F_OFD_GETLK " (\fIstruct flock *\fP)"
589 On input to this call,
590 .I lock
591 describes an open file description lock we would like to place on the file.
592 If the lock could be placed,
593 .BR fcntl ()
594 does not actually place it, but returns
595 .B F_UNLCK
596 in the
597 .I l_type
598 field of
599 .I lock
600 and leaves the other fields of the structure unchanged.
601 If one or more incompatible locks would prevent this lock being placed,
602 then details about one of these locks are returned via
603 .IR lock ,
604 as described above for
605 .BR F_GETLK .
607 In the current implementation,
608 .\" commit 57b65325fe34ec4c917bc4e555144b4a94d9e1f7
609 no deadlock detection is performed for open file description locks.
610 (This contrasts with process-associated record locks,
611 for which the kernel does perform deadlock detection.)
613 .SS Mandatory locking
614 .IR Warning :
615 the Linux implementation of mandatory locking is unreliable.
616 See BUGS below.
617 Because of these bugs,
618 and the fact that the feature is believed to be little used,
619 since Linux 4.5, mandatory locking has been made an optional feature,
620 governed by a configuration option
621 .RB ( CONFIG_MANDATORY_FILE_LOCKING ).
622 This is an initial step toward removing this feature completely.
624 By default, both traditional (process-associated) and open file description
625 record locks are advisory.
626 Advisory locks are not enforced and are useful only between
627 cooperating processes.
629 Both lock types can also be mandatory.
630 Mandatory locks are enforced for all processes.
631 If a process tries to perform an incompatible access (e.g.,
632 .BR read (2)
634 .BR write (2))
635 on a file region that has an incompatible mandatory lock,
636 then the result depends upon whether the
637 .B O_NONBLOCK
638 flag is enabled for its open file description.
639 If the
640 .B O_NONBLOCK
641 flag is not enabled, then
642 the system call is blocked until the lock is removed
643 or converted to a mode that is compatible with the access.
644 If the
645 .B O_NONBLOCK
646 flag is enabled, then the system call fails with the error
647 .BR EAGAIN .
649 To make use of mandatory locks, mandatory locking must be enabled
650 both on the filesystem that contains the file to be locked,
651 and on the file itself.
652 Mandatory locking is enabled on a filesystem
653 using the "\-o mand" option to
654 .BR mount (8),
655 or the
656 .B MS_MANDLOCK
657 flag for
658 .BR mount (2).
659 Mandatory locking is enabled on a file by disabling
660 group execute permission on the file and enabling the set-group-ID
661 permission bit (see
662 .BR chmod (1)
664 .BR chmod (2)).
666 Mandatory locking is not specified by POSIX.
667 Some other systems also support mandatory locking,
668 although the details of how to enable it vary across systems.
669 .SS Managing signals
670 .BR F_GETOWN ,
671 .BR F_SETOWN ,
672 .BR F_GETOWN_EX ,
673 .BR F_SETOWN_EX ,
674 .BR F_GETSIG
676 .B F_SETSIG
677 are used to manage I/O availability signals:
679 .BR F_GETOWN " (\fIvoid\fP)"
680 Return (as the function result)
681 the process ID or process group currently receiving
682 .B SIGIO
684 .B SIGURG
685 signals for events on file descriptor
686 .IR fd .
687 Process IDs are returned as positive values;
688 process group IDs are returned as negative values (but see BUGS below).
689 .I arg
690 is ignored.
692 .BR F_SETOWN " (\fIint\fP)"
693 Set the process ID or process group ID that will receive
694 .B SIGIO
696 .B SIGURG
697 signals for events on the file descriptor
698 .IR fd .
699 The target process or process group ID is specified in
700 .IR arg .
701 A process ID is specified as a positive value;
702 a process group ID is specified as a negative value.
703 Most commonly, the calling process specifies itself as the owner
704 (that is,
705 .I arg
706 is specified as
707 .BR getpid (2)).
709 As well as setting the file descriptor owner,
710 one must also enable generation of signals on the file descriptor.
711 This is done by using the
712 .BR fcntl ()
713 .B F_SETFL
714 command to set the
715 .B O_ASYNC
716 file status flag on the file descriptor.
717 Subsequently, a
718 .B SIGIO
719 signal is sent whenever input or output becomes possible
720 on the file descriptor.
722 .BR fcntl ()
723 .B F_SETSIG
724 command can be used to obtain delivery of a signal other than
725 .BR SIGIO .
727 Sending a signal to the owner process (group) specified by
728 .B F_SETOWN
729 is subject to the same permissions checks as are described for
730 .BR kill (2),
731 where the sending process is the one that employs
732 .B F_SETOWN
733 (but see BUGS below).
734 If this permission check fails, then the signal is
735 silently discarded.
736 .IR Note :
738 .BR F_SETOWN
739 operation records the caller's credentials at the time of the
740 .BR fcntl ()
741 call,
742 and it is these saved credentials that are used for the permission checks.
744 If the file descriptor
745 .I fd
746 refers to a socket,
747 .B F_SETOWN
748 also selects
749 the recipient of
750 .B SIGURG
751 signals that are delivered when out-of-band
752 data arrives on that socket.
753 .RB ( SIGURG
754 is sent in any situation where
755 .BR select (2)
756 would report the socket as having an "exceptional condition".)
757 .\" The following appears to be rubbish.  It doesn't seem to
758 .\" be true according to the kernel source, and I can write
759 .\" a program that gets a terminal-generated SIGIO even though
760 .\" it is not the foreground process group of the terminal.
761 .\" -- MTK, 8 Apr 05
763 .\" If the file descriptor
764 .\" .I fd
765 .\" refers to a terminal device, then SIGIO
766 .\" signals are sent to the foreground process group of the terminal.
768 The following was true in 2.6.x kernels up to and including
769 kernel 2.6.11:
772 If a nonzero value is given to
773 .B F_SETSIG
774 in a multithreaded process running with a threading library
775 that supports thread groups (e.g., NPTL),
776 then a positive value given to
777 .B F_SETOWN
778 has a different meaning:
779 .\" The relevant place in the (2.6) kernel source is the
780 .\" 'switch' in fs/fcntl.c::send_sigio_to_task() -- MTK, Apr 2005
781 instead of being a process ID identifying a whole process,
782 it is a thread ID identifying a specific thread within a process.
783 Consequently, it may be necessary to pass
784 .B F_SETOWN
785 the result of
786 .BR gettid (2)
787 instead of
788 .BR getpid (2)
789 to get sensible results when
790 .B F_SETSIG
791 is used.
792 (In current Linux threading implementations,
793 a main thread's thread ID is the same as its process ID.
794 This means that a single-threaded program can equally use
795 .BR gettid (2)
797 .BR getpid (2)
798 in this scenario.)
799 Note, however, that the statements in this paragraph do not apply
800 to the
801 .B SIGURG
802 signal generated for out-of-band data on a socket:
803 this signal is always sent to either a process or a process group,
804 depending on the value given to
805 .BR F_SETOWN .
806 .\" send_sigurg()/send_sigurg_to_task() bypasses
807 .\" kill_fasync()/send_sigio()/send_sigio_to_task()
808 .\" to directly call send_group_sig_info()
809 .\"     -- MTK, Apr 2005 (kernel 2.6.11)
812 The above behavior was accidentally dropped in Linux 2.6.12,
813 and won't be restored.
814 From Linux 2.6.32 onward, use
815 .BR F_SETOWN_EX
816 to target
817 .B SIGIO
819 .B SIGURG
820 signals at a particular thread.
822 .BR F_GETOWN_EX " (\fIstruct f_owner_ex *\fP) (since Linux 2.6.32)"
823 Return the current file descriptor owner settings
824 as defined by a previous
825 .BR F_SETOWN_EX
826 operation.
827 The information is returned in the structure pointed to by
828 .IR arg ,
829 which has the following form:
831 .in +4n
833 struct f_owner_ex {
834     int   type;
835     pid_t pid;
841 .I type
842 field will have one of the values
843 .BR F_OWNER_TID ,
844 .BR F_OWNER_PID ,
846 .BR F_OWNER_PGRP .
848 .I pid
849 field is a positive integer representing a thread ID, process ID,
850 or process group ID.
852 .B F_SETOWN_EX
853 for more details.
855 .BR F_SETOWN_EX " (\fIstruct f_owner_ex *\fP) (since Linux 2.6.32)"
856 This operation performs a similar task to
857 .BR F_SETOWN .
858 It allows the caller to direct I/O availability signals
859 to a specific thread, process, or process group.
860 The caller specifies the target of signals via
861 .IR arg ,
862 which is a pointer to a
863 .IR f_owner_ex
864 structure.
866 .I type
867 field has one of the following values, which define how
868 .I pid
869 is interpreted:
872 .BR F_OWNER_TID
873 Send the signal to the thread whose thread ID
874 (the value returned by a call to
875 .BR clone (2)
877 .BR gettid (2))
878 is specified in
879 .IR pid .
881 .BR F_OWNER_PID
882 Send the signal to the process whose ID
883 is specified in
884 .IR pid .
886 .BR F_OWNER_PGRP
887 Send the signal to the process group whose ID
888 is specified in
889 .IR pid .
890 (Note that, unlike with
891 .BR F_SETOWN ,
892 a process group ID is specified as a positive value here.)
895 .BR F_GETSIG " (\fIvoid\fP)"
896 Return (as the function result)
897 the signal sent when input or output becomes possible.
898 A value of zero means
899 .B SIGIO
900 is sent.
901 Any other value (including
902 .BR SIGIO )
903 is the
904 signal sent instead, and in this case additional info is available to
905 the signal handler if installed with
906 .BR SA_SIGINFO .
907 .I arg
908 is ignored.
910 .BR F_SETSIG " (\fIint\fP)"
911 Set the signal sent when input or output becomes possible
912 to the value given in
913 .IR arg .
914 A value of zero means to send the default
915 .B SIGIO
916 signal.
917 Any other value (including
918 .BR SIGIO )
919 is the signal to send instead, and in this case additional info
920 is available to the signal handler if installed with
921 .BR SA_SIGINFO .
923 .\" The following was true only up until 2.6.11:
925 .\" Additionally, passing a nonzero value to
926 .\" .B F_SETSIG
927 .\" changes the signal recipient from a whole process to a specific thread
928 .\" within a process.
929 .\" See the description of
930 .\" .B F_SETOWN
931 .\" for more details.
933 By using
934 .B F_SETSIG
935 with a nonzero value, and setting
936 .B SA_SIGINFO
937 for the
938 signal handler (see
939 .BR sigaction (2)),
940 extra information about I/O events is passed to
941 the handler in a
942 .I siginfo_t
943 structure.
944 If the
945 .I si_code
946 field indicates the source is
947 .BR SI_SIGIO ,
949 .I si_fd
950 field gives the file descriptor associated with the event.
951 Otherwise,
952 there is no indication which file descriptors are pending, and you
953 should use the usual mechanisms
954 .RB ( select (2),
955 .BR poll (2),
956 .BR read (2)
957 with
958 .B O_NONBLOCK
959 set etc.) to determine which file descriptors are available for I/O.
961 Note that the file descriptor provided in
962 .I si_fd
963 is the one that was specified during the
964 .BR F_SETSIG
965 operation.
966 This can lead to an unusual corner case.
967 If the file descriptor is duplicated
968 .RB ( dup (2)
969 or similar), and the original file descriptor is closed,
970 then I/O events will continue to be generated, but the
971 .I si_fd
972 field will contain the number of the now closed file descriptor.
974 By selecting a real time signal (value >=
975 .BR SIGRTMIN ),
976 multiple I/O events may be queued using the same signal numbers.
977 (Queuing is dependent on available memory.)
978 Extra information is available
980 .B SA_SIGINFO
981 is set for the signal handler, as above.
983 Note that Linux imposes a limit on the
984 number of real-time signals that may be queued to a
985 process (see
986 .BR getrlimit (2)
988 .BR signal (7))
989 and if this limit is reached, then the kernel reverts to
990 delivering
991 .BR SIGIO ,
992 and this signal is delivered to the entire
993 process rather than to a specific thread.
994 .\" See fs/fcntl.c::send_sigio_to_task() (2.4/2.6) sources -- MTK, Apr 05
996 Using these mechanisms, a program can implement fully asynchronous I/O
997 without using
998 .BR select (2)
1000 .BR poll (2)
1001 most of the time.
1003 The use of
1004 .BR O_ASYNC
1005 is specific to BSD and Linux.
1006 The only use of
1007 .BR F_GETOWN
1009 .B F_SETOWN
1010 specified in POSIX.1 is in conjunction with the use of the
1011 .B SIGURG
1012 signal on sockets.
1013 (POSIX does not specify the
1014 .BR SIGIO
1015 signal.)
1016 .BR F_GETOWN_EX ,
1017 .BR F_SETOWN_EX ,
1018 .BR F_GETSIG ,
1020 .B F_SETSIG
1021 are Linux-specific.
1022 POSIX has asynchronous I/O and the
1023 .I aio_sigevent
1024 structure to achieve similar things; these are also available
1025 in Linux as part of the GNU C Library (Glibc).
1026 .SS Leases
1027 .B F_SETLEASE
1029 .B F_GETLEASE
1030 (Linux 2.4 onward) are used (respectively) to establish a new lease,
1031 and retrieve the current lease, on the open file description
1032 referred to by the file descriptor
1033 .IR fd .
1034 A file lease provides a mechanism whereby the process holding
1035 the lease (the "lease holder") is notified (via delivery of a signal)
1036 when a process (the "lease breaker") tries to
1037 .BR open (2)
1039 .BR truncate (2)
1040 the file referred to by that file descriptor.
1042 .BR F_SETLEASE " (\fIint\fP)"
1043 Set or remove a file lease according to which of the following
1044 values is specified in the integer
1045 .IR arg :
1048 .B F_RDLCK
1049 Take out a read lease.
1050 This will cause the calling process to be notified when
1051 the file is opened for writing or is truncated.
1052 .\" The following became true in kernel 2.6.10:
1053 .\" See the man-pages-2.09 Changelog for further info.
1054 A read lease can be placed only on a file descriptor that
1055 is opened read-only.
1057 .B F_WRLCK
1058 Take out a write lease.
1059 This will cause the caller to be notified when
1060 the file is opened for reading or writing or is truncated.
1061 A write lease may be placed on a file only if there are no
1062 other open file descriptors for the file.
1064 .B F_UNLCK
1065 Remove our lease from the file.
1068 Leases are associated with an open file description (see
1069 .BR open (2)).
1070 This means that duplicate file descriptors (created by, for example,
1071 .BR fork (2)
1073 .BR dup (2))
1074 refer to the same lease, and this lease may be modified
1075 or released using any of these descriptors.
1076 Furthermore, the lease is released by either an explicit
1077 .B F_UNLCK
1078 operation on any of these duplicate file descriptors, or when all
1079 such file descriptors have been closed.
1081 Leases may be taken out only on regular files.
1082 An unprivileged process may take out a lease only on a file whose
1083 UID (owner) matches the filesystem UID of the process.
1084 A process with the
1085 .B CAP_LEASE
1086 capability may take out leases on arbitrary files.
1088 .BR F_GETLEASE " (\fIvoid\fP)"
1089 Indicates what type of lease is associated with the file descriptor
1090 .I fd
1091 by returning either
1092 .BR F_RDLCK ", " F_WRLCK ", or " F_UNLCK ,
1093 indicating, respectively, a read lease , a write lease, or no lease.
1094 .I arg
1095 is ignored.
1097 When a process (the "lease breaker") performs an
1098 .BR open (2)
1100 .BR truncate (2)
1101 that conflicts with a lease established via
1102 .BR F_SETLEASE ,
1103 the system call is blocked by the kernel and
1104 the kernel notifies the lease holder by sending it a signal
1105 .RB ( SIGIO
1106 by default).
1107 The lease holder should respond to receipt of this signal by doing
1108 whatever cleanup is required in preparation for the file to be
1109 accessed by another process (e.g., flushing cached buffers) and
1110 then either remove or downgrade its lease.
1111 A lease is removed by performing an
1112 .B F_SETLEASE
1113 command specifying
1114 .I arg
1116 .BR F_UNLCK .
1117 If the lease holder currently holds a write lease on the file,
1118 and the lease breaker is opening the file for reading,
1119 then it is sufficient for the lease holder to downgrade
1120 the lease to a read lease.
1121 This is done by performing an
1122 .B F_SETLEASE
1123 command specifying
1124 .I arg
1126 .BR F_RDLCK .
1128 If the lease holder fails to downgrade or remove the lease within
1129 the number of seconds specified in
1130 .IR /proc/sys/fs/lease-break-time ,
1131 then the kernel forcibly removes or downgrades the lease holder's lease.
1133 Once a lease break has been initiated,
1134 .B F_GETLEASE
1135 returns the target lease type (either
1136 .B F_RDLCK
1138 .BR F_UNLCK ,
1139 depending on what would be compatible with the lease breaker)
1140 until the lease holder voluntarily downgrades or removes the lease or
1141 the kernel forcibly does so after the lease break timer expires.
1143 Once the lease has been voluntarily or forcibly removed or downgraded,
1144 and assuming the lease breaker has not unblocked its system call,
1145 the kernel permits the lease breaker's system call to proceed.
1147 If the lease breaker's blocked
1148 .BR open (2)
1150 .BR truncate (2)
1151 is interrupted by a signal handler,
1152 then the system call fails with the error
1153 .BR EINTR ,
1154 but the other steps still occur as described above.
1155 If the lease breaker is killed by a signal while blocked in
1156 .BR open (2)
1158 .BR truncate (2),
1159 then the other steps still occur as described above.
1160 If the lease breaker specifies the
1161 .B O_NONBLOCK
1162 flag when calling
1163 .BR open (2),
1164 then the call immediately fails with the error
1165 .BR EWOULDBLOCK ,
1166 but the other steps still occur as described above.
1168 The default signal used to notify the lease holder is
1169 .BR SIGIO ,
1170 but this can be changed using the
1171 .B F_SETSIG
1172 command to
1173 .BR fcntl ().
1174 If a
1175 .B F_SETSIG
1176 command is performed (even one specifying
1177 .BR SIGIO ),
1178 and the signal
1179 handler is established using
1180 .BR SA_SIGINFO ,
1181 then the handler will receive a
1182 .I siginfo_t
1183 structure as its second argument, and the
1184 .I si_fd
1185 field of this argument will hold the file descriptor of the leased file
1186 that has been accessed by another process.
1187 (This is useful if the caller holds leases against multiple files.)
1188 .SS File and directory change notification (dnotify)
1190 .BR F_NOTIFY " (\fIint\fP)"
1191 (Linux 2.4 onward)
1192 Provide notification when the directory referred to by
1193 .I fd
1194 or any of the files that it contains is changed.
1195 The events to be notified are specified in
1196 .IR arg ,
1197 which is a bit mask specified by ORing together zero or more of
1198 the following bits:
1201 .PD 0
1202 .TP 12
1203 .B DN_ACCESS
1204 A file was accessed
1205 .RB ( read (2),
1206 .BR pread (2),
1207 .BR readv (2),
1208 and similar)
1210 .B DN_MODIFY
1211 A file was modified
1212 .RB ( write (2),
1213 .BR pwrite (2),
1214 .BR writev (2),
1215 .BR truncate (2),
1216 .BR ftruncate (2),
1217 and similar).
1219 .B DN_CREATE
1220 A file was created
1221 .RB ( open (2),
1222 .BR creat (2),
1223 .BR mknod (2),
1224 .BR mkdir (2),
1225 .BR link (2),
1226 .BR symlink (2),
1227 .BR rename (2)
1228 into this directory).
1230 .B DN_DELETE
1231 A file was unlinked
1232 .RB ( unlink (2),
1233 .BR rename (2)
1234 to another directory,
1235 .BR rmdir (2)).
1237 .B DN_RENAME
1238 A file was renamed within this directory
1239 .RB ( rename (2)).
1241 .B DN_ATTRIB
1242 The attributes of a file were changed
1243 .RB ( chown (2),
1244 .BR chmod (2),
1245 .BR utime (2),
1246 .BR utimensat (2),
1247 and similar).
1251 (In order to obtain these definitions, the
1252 .B _GNU_SOURCE
1253 feature test macro must be defined before including
1254 .I any
1255 header files.)
1257 Directory notifications are normally "one-shot", and the application
1258 must reregister to receive further notifications.
1259 Alternatively, if
1260 .B DN_MULTISHOT
1261 is included in
1262 .IR arg ,
1263 then notification will remain in effect until explicitly removed.
1265 .\" The following does seem a poor API-design choice...
1266 A series of
1267 .B F_NOTIFY
1268 requests is cumulative, with the events in
1269 .I arg
1270 being added to the set already monitored.
1271 To disable notification of all events, make an
1272 .B F_NOTIFY
1273 call specifying
1274 .I arg
1275 as 0.
1277 Notification occurs via delivery of a signal.
1278 The default signal is
1279 .BR SIGIO ,
1280 but this can be changed using the
1281 .B F_SETSIG
1282 command to
1283 .BR fcntl ().
1284 (Note that
1285 .B SIGIO
1286 is one of the nonqueuing standard signals;
1287 switching to the use of a real-time signal means that
1288 multiple notifications can be queued to the process.)
1289 In the latter case, the signal handler receives a
1290 .I siginfo_t
1291 structure as its second argument (if the handler was
1292 established using
1293 .BR SA_SIGINFO )
1294 and the
1295 .I si_fd
1296 field of this structure contains the file descriptor which
1297 generated the notification (useful when establishing notification
1298 on multiple directories).
1300 Especially when using
1301 .BR DN_MULTISHOT ,
1302 a real time signal should be used for notification,
1303 so that multiple notifications can be queued.
1305 .B NOTE:
1306 New applications should use the
1307 .I inotify
1308 interface (available since kernel 2.6.13),
1309 which provides a much superior interface for obtaining notifications of
1310 filesystem events.
1312 .BR inotify (7).
1313 .SS Changing the capacity of a pipe
1315 .BR F_SETPIPE_SZ " (\fIint\fP; since Linux 2.6.35)"
1316 Change the capacity of the pipe referred to by
1317 .I fd
1318 to be at least
1319 .I arg
1320 bytes.
1321 An unprivileged process can adjust the pipe capacity to any value
1322 between the system page size and the limit defined in
1323 .IR /proc/sys/fs/pipe-max-size
1324 (see
1325 .BR proc (5)).
1326 Attempts to set the pipe capacity below the page size are silently
1327 rounded up to the page size.
1328 Attempts by an unprivileged process to set the pipe capacity above the limit in
1329 .IR /proc/sys/fs/pipe-max-size
1330 yield the error
1331 .BR EPERM ;
1332 a privileged process
1333 .RB ( CAP_SYS_RESOURCE )
1334 can override the limit.
1336 When allocating the buffer for the pipe,
1337 the kernel may use a capacity larger than
1338 .IR arg ,
1339 if that is convenient for the implementation.
1340 (In the current implementation,
1341 the allocation is the next higher power-of-two page-size multiple
1342 of the requested size.)
1343 The actual capacity (in bytes) that is set is returned as the function result.
1345 Attempting to set the pipe capacity smaller than the amount
1346 of buffer space currently used to store data produces the error
1347 .BR EBUSY .
1349 .BR F_GETPIPE_SZ " (\fIvoid\fP; since Linux 2.6.35)"
1350 Return (as the function result) the capacity of the pipe referred to by
1351 .IR fd .
1353 .SS File Sealing
1354 File seals limit the set of allowed operations on a given file.
1355 For each seal that is set on a file,
1356 a specific set of operations will fail with
1357 .B EPERM
1358 on this file from now on.
1359 The file is said to be sealed.
1360 The default set of seals depends on the type of the underlying
1361 file and filesystem.
1362 For an overview of file sealing, a discussion of its purpose,
1363 and some code examples, see
1364 .BR memfd_create (2).
1366 Currently,
1367 file seals can be applied only to a file descriptor returned by
1368 .BR memfd_create (2)
1369 (if the
1370 .B MFD_ALLOW_SEALING
1371 was employed).
1372 On other filesystems, all
1373 .BR fcntl ()
1374 operations that operate on seals will return
1375 .BR EINVAL .
1377 Seals are a property of an inode.
1378 Thus, all open file descriptors referring to the same inode share
1379 the same set of seals.
1380 Furthermore, seals can never be removed, only added.
1382 .BR F_ADD_SEALS " (\fIint\fP; since Linux 3.17)"
1383 Add the seals given in the bit-mask argument
1384 .I arg
1385 to the set of seals of the inode referred to by the file descriptor
1386 .IR fd .
1387 Seals cannot be removed again.
1388 Once this call succeeds, the seals are enforced by the kernel immediately.
1389 If the current set of seals includes
1390 .BR F_SEAL_SEAL
1391 (see below), then this call will be rejected with
1392 .BR EPERM .
1393 Adding a seal that is already set is a no-op, in case
1394 .B F_SEAL_SEAL
1395 is not set already.
1396 In order to place a seal, the file descriptor
1397 .I fd
1398 must be writable.
1400 .BR F_GET_SEALS " (\fIvoid\fP; since Linux 3.17)"
1401 Return (as the function result) the current set of seals
1402 of the inode referred to by
1403 .IR fd .
1404 If no seals are set, 0 is returned.
1405 If the file does not support sealing, \-1 is returned and
1406 .I errno
1407 is set to
1408 .BR EINVAL .
1410 The following seals are available:
1412 .BR F_SEAL_SEAL
1413 If this seal is set, any further call to
1414 .BR fcntl ()
1415 with
1416 .B F_ADD_SEALS
1417 will fail with
1418 .BR EPERM .
1419 Therefore, this seal prevents any modifications to the set of seals itself.
1420 If the initial set of seals of a file includes
1421 .BR F_SEAL_SEAL ,
1422 then this effectively causes the set of seals to be constant and locked.
1424 .BR F_SEAL_SHRINK
1425 If this seal is set, the file in question cannot be reduced in size.
1426 This affects
1427 .BR open (2)
1428 with the
1429 .B O_TRUNC
1430 flag as well as
1431 .BR truncate (2)
1433 .BR ftruncate (2).
1434 Those calls will fail with
1435 .B EPERM
1436 if you try to shrink the file in question.
1437 Increasing the file size is still possible.
1439 .BR F_SEAL_GROW
1440 If this seal is set, the size of the file in question cannot be increased.
1441 This affects
1442 .BR write (2)
1443 beyond the end of the file,
1444 .BR truncate (2),
1445 .BR ftruncate (2),
1447 .BR fallocate (2).
1448 These calls will fail with
1449 .B EPERM
1450 if you use them to increase the file size.
1451 If you keep the size or shrink it, those calls still work as expected.
1453 .BR F_SEAL_WRITE
1454 If this seal is set, you cannot modify the contents of the file.
1455 Note that shrinking or growing the size of the file is
1456 still possible and allowed.
1457 .\" One or more other seals are typically used with F_SEAL_WRITE
1458 .\" because, given a file with the F_SEAL_WRITE seal set, then,
1459 .\" while it would no longer be possible to (say) write zeros into
1460 .\" the last 100 bytes of a file, it would still be possible
1461 .\" to (say) shrink the file by 100 bytes using ftruncate(), and
1462 .\" then increase the file size by 100 bytes, which would have
1463 .\" the effect of replacing the last hundred bytes by zeros.
1465 Thus, this seal is normally used in combination with one of the other seals.
1466 This seal affects
1467 .BR write (2)
1469 .BR fallocate (2)
1470 (only in combination with the
1471 .B FALLOC_FL_PUNCH_HOLE
1472 flag).
1473 Those calls will fail with
1474 .B EPERM
1475 if this seal is set.
1476 Furthermore, trying to create new shared, writable memory-mappings via
1477 .BR mmap (2)
1478 will also fail with
1479 .BR EPERM .
1481 Using the
1482 .B F_ADD_SEALS
1483 operation to set the
1484 .B F_SEAL_WRITE
1485 seal will fail with
1486 .B EBUSY
1487 if any writable, shared mapping exists.
1488 Such mappings must be unmapped before you can add this seal.
1489 Furthermore, if there are any asynchronous I/O operations
1490 .RB ( io_submit (2))
1491 pending on the file,
1492 all outstanding writes will be discarded.
1493 .SH RETURN VALUE
1494 For a successful call, the return value depends on the operation:
1495 .TP 0.9i
1496 .B F_DUPFD
1497 The new file descriptor.
1499 .B F_GETFD
1500 Value of file descriptor flags.
1502 .B F_GETFL
1503 Value of file status flags.
1505 .B F_GETLEASE
1506 Type of lease held on file descriptor.
1508 .B F_GETOWN
1509 Value of file descriptor owner.
1511 .B F_GETSIG
1512 Value of signal sent when read or write becomes possible, or zero
1513 for traditional
1514 .B SIGIO
1515 behavior.
1517 .BR F_GETPIPE_SZ ", " F_SETPIPE_SZ
1518 The pipe capacity.
1520 .BR F_GET_SEALS
1521 A bit mask identifying the seals that have been set
1522 for the inode referred to by
1523 .IR fd .
1525 All other commands
1526 Zero.
1528 On error, \-1 is returned, and
1529 .I errno
1530 is set appropriately.
1531 .SH ERRORS
1533 .BR EACCES " or " EAGAIN
1534 Operation is prohibited by locks held by other processes.
1536 .B EAGAIN
1537 The operation is prohibited because the file has been memory-mapped by
1538 another process.
1540 .B EBADF
1541 .I fd
1542 is not an open file descriptor
1544 .B EBADF
1545 .I cmd
1547 .B F_SETLK
1549 .B F_SETLKW
1550 and the file descriptor open mode doesn't match with the
1551 type of lock requested.
1553 .BR EBUSY
1554 .I cmd
1556 .BR F_SETPIPE_SZ
1557 and the new pipe capacity specified in
1558 .I arg
1559 is smaller than the amount of buffer space currently
1560 used to store data in the pipe.
1562 .B EBUSY
1563 .I cmd
1565 .BR F_ADD_SEALS ,
1566 .IR arg
1567 includes
1568 .BR F_SEAL_WRITE ,
1569 and there exists a writable, shared mapping on the file referred to by
1570 .IR fd .
1572 .B EDEADLK
1573 It was detected that the specified
1574 .B F_SETLKW
1575 command would cause a deadlock.
1577 .B EFAULT
1578 .I lock
1579 is outside your accessible address space.
1581 .B EINTR
1582 .I cmd
1584 .BR F_SETLKW
1586 .BR F_OFD_SETLKW
1587 and the operation was interrupted by a signal; see
1588 .BR signal (7).
1590 .B EINTR
1591 .I cmd
1593 .BR F_GETLK ,
1594 .BR F_SETLK ,
1595 .BR F_OFD_GETLK ,
1597 .BR F_OFD_SETLK ,
1598 and the operation was interrupted by a signal before the lock was checked or
1599 acquired.
1600 Most likely when locking a remote file (e.g., locking over
1601 NFS), but can sometimes happen locally.
1603 .B EINVAL
1604 The value specified in
1605 .I cmd
1606 is not recognized by this kernel.
1608 .B EINVAL
1609 .I cmd
1611 .BR F_ADD_SEALS
1613 .I arg
1614 includes an unrecognized sealing bit.
1616 .BR EINVAL
1617 .I cmd
1619 .BR F_ADD_SEALS
1621 .BR F_GET_SEALS
1622 and the filesystem containing the inode referred to by
1623 .I fd
1624 does not support sealing.
1626 .B EINVAL
1627 .I cmd
1629 .BR F_DUPFD
1631 .I arg
1632 is negative or is greater than the maximum allowable value
1633 (see the discussion of
1634 .BR RLIMIT_NOFILE
1636 .BR getrlimit (2)).
1638 .B EINVAL
1639 .I cmd
1641 .BR F_SETSIG
1643 .I arg
1644 is not an allowable signal number.
1646 .B EINVAL
1647 .I cmd
1649 .BR F_OFD_SETLK ,
1650 .BR F_OFD_SETLKW ,
1652 .BR F_OFD_GETLK ,
1654 .I l_pid
1655 was not specified as zero.
1657 .B EMFILE
1658 .I cmd
1660 .BR F_DUPFD
1661 and the per-process limit on the number of open file descriptors
1662 has been reached.
1664 .B ENOLCK
1665 Too many segment locks open, lock table is full, or a remote locking
1666 protocol failed (e.g., locking over NFS).
1668 .B ENOTDIR
1669 .B F_NOTIFY
1670 was specified in
1671 .IR cmd ,
1673 .IR fd
1674 does not refer to a directory.
1676 .BR EPERM
1677 .I cmd
1679 .BR F_SETPIPE_SZ
1680 and the soft or hard user pipe limit has been reached; see
1681 .BR pipe (7).
1683 .B EPERM
1684 Attempted to clear the
1685 .B O_APPEND
1686 flag on a file that has the append-only attribute set.
1688 .B EPERM
1689 .I cmd
1691 .BR F_ADD_SEALS ,
1693 .I fd
1694 was not open for writing
1695 or the current set of seals on the file already includes
1696 .BR F_SEAL_SEAL .
1697 .SH CONFORMING TO
1698 SVr4, 4.3BSD, POSIX.1-2001.
1699 Only the operations
1700 .BR F_DUPFD ,
1701 .BR F_GETFD ,
1702 .BR F_SETFD ,
1703 .BR F_GETFL ,
1704 .BR F_SETFL ,
1705 .BR F_GETLK ,
1706 .BR F_SETLK ,
1708 .BR F_SETLKW
1709 are specified in POSIX.1-2001.
1711 .BR F_GETOWN
1713 .B F_SETOWN
1714 are specified in POSIX.1-2001.
1715 (To get their definitions, define either
1716 .\" .BR _BSD_SOURCE ,
1717 .\" or
1718 .BR _XOPEN_SOURCE
1719 with the value 500 or greater, or
1720 .BR _POSIX_C_SOURCE
1721 with the value 200809L or greater.)
1723 .B F_DUPFD_CLOEXEC
1724 is specified in POSIX.1-2008.
1725 (To get this definition, define
1726 .B _POSIX_C_SOURCE
1727 with the value 200809L or greater, or
1728 .B _XOPEN_SOURCE
1729 with the value 700 or greater.)
1731 .BR F_GETOWN_EX ,
1732 .BR F_SETOWN_EX ,
1733 .BR F_SETPIPE_SZ ,
1734 .BR F_GETPIPE_SZ ,
1735 .BR F_GETSIG ,
1736 .BR F_SETSIG ,
1737 .BR F_NOTIFY ,
1738 .BR F_GETLEASE ,
1740 .B F_SETLEASE
1741 are Linux-specific.
1742 (Define the
1743 .B _GNU_SOURCE
1744 macro to obtain these definitions.)
1745 .\" .PP
1746 .\" SVr4 documents additional EIO, ENOLINK and EOVERFLOW error conditions.
1748 .BR F_OFD_SETLK ,
1749 .BR F_OFD_SETLKW ,
1751 .BR F_OFD_GETLK
1752 are Linux-specific (and one must define
1753 .BR _GNU_SOURCE
1754 to obtain their definitions),
1755 but work is being done to have them included in the next version of POSIX.1.
1757 .BR F_ADD_SEALS
1759 .BR F_GET_SEALS
1760 are Linux-specific.
1761 .\" FIXME . Once glibc adds support, add a note about FTM requirements
1762 .SH NOTES
1763 The errors returned by
1764 .BR dup2 (2)
1765 are different from those returned by
1766 .BR F_DUPFD .
1768 .SS File locking
1769 The original Linux
1770 .BR fcntl ()
1771 system call was not designed to handle large file offsets
1772 (in the
1773 .I flock
1774 structure).
1775 Consequently, an
1776 .BR fcntl64 ()
1777 system call was added in Linux 2.4.
1778 The newer system call employs a different structure for file locking,
1779 .IR flock64 ,
1780 and corresponding commands,
1781 .BR F_GETLK64 ,
1782 .BR F_SETLK64 ,
1784 .BR F_SETLKW64 .
1785 However, these details can be ignored by applications using glibc, whose
1786 .BR fcntl ()
1787 wrapper function transparently employs the more recent system call
1788 where it is available.
1790 .SS Record locks
1791 Since kernel 2.0, there is no interaction between the types of lock
1792 placed by
1793 .BR flock (2)
1795 .BR fcntl ().
1797 Several systems have more fields in
1798 .I "struct flock"
1799 such as, for example,
1800 .IR l_sysid .
1801 .\" e.g., Solaris 8 documents this field in fcntl(2), and Irix 6.5
1802 .\" documents it in fcntl(5).  mtk, May 2007
1803 .\" Also, FreeBSD documents it (Apr 2014).
1804 Clearly,
1805 .I l_pid
1806 alone is not going to be very useful if the process holding the lock
1807 may live on a different machine.
1809 The original Linux
1810 .BR fcntl ()
1811 system call was not designed to handle large file offsets
1812 (in the
1813 .I flock
1814 structure).
1815 Consequently, an
1816 .BR fcntl64 ()
1817 system call was added in Linux 2.4.
1818 The newer system call employs a different structure for file locking,
1819 .IR flock64 ,
1820 and corresponding commands,
1821 .BR F_GETLK64 ,
1822 .BR F_SETLK64 ,
1824 .BR F_SETLKW64 .
1825 However, these details can be ignored by applications using glibc, whose
1826 .BR fcntl ()
1827 wrapper function transparently employs the more recent system call
1828 where it is available.
1829 .SS Record locking and NFS
1830 Before Linux 3.12, if an NFSv4 client
1831 loses contact with the server for a period of time
1832 (defined as more than 90 seconds with no communication),
1834 .\" Neil Brown: With NFSv3 the failure mode is the reverse.  If
1835 .\"     the server loses contact with a client then any lock stays in place
1836 .\"     indefinitely ("why can't I read my mail"... I remember it well).
1838 it might lose and regain a lock without ever being aware of the fact.
1839 (The period of time after which contact is assumed lost is known as
1840 the NFSv4 leasetime.
1841 On a Linux NFS server, this can be determined by looking at
1842 .IR /proc/fs/nfsd/nfsv4leasetime ,
1843 which expresses the period in seconds.
1844 The default value for this file is 90.)
1846 .\" Jeff Layton:
1847 .\"     Note that this is not a firm timeout. The server runs a job
1848 .\"     periodically to clean out expired stateful objects, and it's likely
1849 .\"     that there is some time (maybe even up to another whole lease period)
1850 .\"     between when the timeout expires and the job actually runs. If the
1851 .\"     client gets a RENEW in there within that window, its lease will be
1852 .\"     renewed and its state preserved.
1854 This scenario potentially risks data corruption,
1855 since another process might acquire a lock in the intervening period
1856 and perform file I/O.
1858 Since Linux 3.12,
1859 .\" commit ef1820f9be27b6ad158f433ab38002ab8131db4d
1860 if an NFSv4 client loses contact with the server,
1861 any I/O to the file by a process which "thinks" it holds
1862 a lock will fail until that process closes and reopens the file.
1863 A kernel parameter,
1864 .IR nfs.recover_lost_locks ,
1865 can be set to 1 to obtain the pre-3.12 behavior,
1866 whereby the client will attempt to recover lost locks
1867 when contact is reestablished with the server.
1868 Because of the attendant risk of data corruption,
1869 .\" commit f6de7a39c181dfb8a2c534661a53c73afb3081cd
1870 this parameter defaults to 0 (disabled).
1871 .SH BUGS
1872 .SS F_SETFL
1873 It is not possible to use
1874 .BR F_SETFL
1875 to change the state of the
1876 .BR O_DSYNC
1878 .BR O_SYNC
1879 flags.
1880 .\" FIXME . According to POSIX.1-2001, O_SYNC should also be modifiable
1881 .\" via fcntl(2), but currently Linux does not permit this
1882 .\" See http://bugzilla.kernel.org/show_bug.cgi?id=5994
1883 Attempts to change the state of these flags are silently ignored.
1884 .SS F_GETOWN
1885 A limitation of the Linux system call conventions on some
1886 architectures (notably i386) means that if a (negative)
1887 process group ID to be returned by
1888 .B F_GETOWN
1889 falls in the range \-1 to \-4095, then the return value is wrongly
1890 interpreted by glibc as an error in the system call;
1891 .\" glibc source: sysdeps/unix/sysv/linux/i386/sysdep.h
1892 that is, the return value of
1893 .BR fcntl ()
1894 will be \-1, and
1895 .I errno
1896 will contain the (positive) process group ID.
1897 The Linux-specific
1898 .BR F_GETOWN_EX
1899 operation avoids this problem.
1900 .\" mtk, Dec 04: some limited testing on alpha and ia64 seems to
1901 .\" indicate that ANY negative PGID value will cause F_GETOWN
1902 .\" to misinterpret the return as an error. Some other architectures
1903 .\" seem to have the same range check as i386.
1904 Since glibc version 2.11, glibc makes the kernel
1905 .B F_GETOWN
1906 problem invisible by implementing
1907 .B F_GETOWN
1908 using
1909 .BR F_GETOWN_EX .
1910 .SS F_SETOWN
1911 In Linux 2.4 and earlier, there is bug that can occur
1912 when an unprivileged process uses
1913 .B F_SETOWN
1914 to specify the owner
1915 of a socket file descriptor
1916 as a process (group) other than the caller.
1917 In this case,
1918 .BR fcntl ()
1919 can return \-1 with
1920 .I errno
1921 set to
1922 .BR EPERM ,
1923 even when the owner process (group) is one that the caller
1924 has permission to send signals to.
1925 Despite this error return, the file descriptor owner is set,
1926 and signals will be sent to the owner.
1928 .SS Deadlock detection
1929 The deadlock-detection algorithm employed by the kernel when dealing with
1930 .BR F_SETLKW
1931 requests can yield both
1932 false negatives (failures to detect deadlocks,
1933 leaving a set of deadlocked processes blocked indefinitely)
1934 and false positives
1935 .RB ( EDEADLK
1936 errors when there is no deadlock).
1937 For example,
1938 the kernel limits the lock depth of its dependency search to 10 steps,
1939 meaning that circular deadlock chains that exceed
1940 that size will not be detected.
1941 In addition, the kernel may falsely indicate a deadlock
1942 when two or more processes created using the
1943 .BR clone (2)
1944 .B CLONE_FILES
1945 flag place locks that appear (to the kernel) to conflict.
1947 .SS Mandatory locking
1948 The Linux implementation of mandatory locking
1949 is subject to race conditions which render it unreliable:
1950 .\" http://marc.info/?l=linux-kernel&m=119013491707153&w=2
1952 .\" Reconfirmed by Jeff Layton
1953 .\"     From: Jeff Layton <jlayton <at> redhat.com>
1954 .\"     Subject: Re: Status of fcntl() mandatory locking
1955 .\"     Newsgroups: gmane.linux.file-systems
1956 .\"     Date: 2014-04-28 10:07:57 GMT
1957 .\"     http://thread.gmane.org/gmane.linux.file-systems/84481/focus=84518
1959 .BR write (2)
1960 call that overlaps with a lock may modify data after the mandatory lock is
1961 acquired;
1963 .BR read (2)
1964 call that overlaps with a lock may detect changes to data that were made
1965 only after a write lock was acquired.
1966 Similar races exist between mandatory locks and
1967 .BR mmap (2).
1968 It is therefore inadvisable to rely on mandatory locking.
1969 .SH SEE ALSO
1970 .BR dup2 (2),
1971 .BR flock (2),
1972 .BR open (2),
1973 .BR socket (2),
1974 .BR lockf (3),
1975 .BR capabilities (7),
1976 .BR feature_test_macros (7),
1977 .BR lslocks (8)
1979 .IR locks.txt ,
1980 .IR mandatory-locking.txt ,
1982 .I dnotify.txt
1983 in the Linux kernel source directory
1984 .IR Documentation/filesystems/
1985 (on older kernels, these files are directly under the
1986 .I Documentation/
1987 directory, and
1988 .I mandatory-locking.txt
1989 is called
1990 .IR mandatory.txt )