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