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