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