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