OSDN Git Service

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