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