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