OSDN Git Service

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