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