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