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