OSDN Git Service

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