]> git.ipfire.org Git - thirdparty/man-pages.git/blob - man2/fcntl.2
Change itacised function names and page xrefs to bold
[thirdparty/man-pages.git] / man2 / fcntl.2
1 '\" t
2 .\" Hey Emacs! This file is -*- nroff -*- source.
3 .\"
4 .\" This manpage is Copyright (C) 1992 Drew Eckhardt;
5 .\" 1993 Michael Haardt, Ian Jackson;
6 .\" 1998 Jamie Lokier;
7 .\" 2002 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@gmx.net>
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@gmx.net>
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@gmx.net>
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 behaviour of F_SETOWN/F_SETSIG in
50 .\" multi-threaded 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 .\"
56 .TH FCNTL 2 2005-20-13 "Linux 2.6.14" "Linux Programmer's Manual"
57 .SH NAME
58 fcntl \- manipulate file descriptor
59 .SH SYNOPSIS
60 .nf
61 .B #include <unistd.h>
62 .B #include <fcntl.h>
63 .sp
64 .BI "int fcntl(int " fd ", int " cmd );
65 .BI "int fcntl(int " fd ", int " cmd ", long " arg );
66 .BI "int fcntl(int " fd ", int " cmd ", struct flock *" lock );
67 .fi
68 .SH DESCRIPTION
69 .BR fcntl ()
70 performs one of the operations described below on the open file descriptor
71 .IR fd .
72 The operation is determined by
73 .IR cmd .
74 .P
75 .SS "Duplicating a file descriptor"
76 .TP
77 .B F_DUPFD
78 Find the lowest numbered available file descriptor
79 greater than or equal to
80 .I arg
81 and make it be a copy of
82 .IR fd .
83 This is different from
84 .BR dup2 (2)
85 which uses exactly the descriptor specified.
86 .sp
87 On success, the new descriptor is returned.
88 .sp
89 See
90 .BR dup (2)
91 for further details.
92 .P
93 .SS "File descriptor flags"
94 The following commands manipulate the flags associated with
95 a file descriptor.
96 Currently, only one such flag is defined:
97 .BR FD_CLOEXEC ,
98 the close-on-exec flag.
99 If the
100 .B FD_CLOEXEC
101 bit is 0, the file descriptor will remain open across an
102 .BR execve (2),
103 otherwise it will be closed.
104 .TP
105 .B F_GETFD
106 Read the file descriptor flags.
107 .TP
108 .B F_SETFD
109 Set the file descriptor flags to the value specified by
110 .IR arg .
111 .P
112 .SS "File status flags"
113 Each open file description has certain associated status flags,
114 initialized by
115 .BR open (2)
116 .\" or
117 .\" .BR creat (2),
118 and possibly modified by
119 .BR fcntl (2).
120 Duplicated file descriptors
121 (made with
122 .BR dup (),
123 .BR fcntl (F_DUPFD),
124 .BR fork (),
125 etc.) refer to the same open file description, and thus
126 share the same file status flags.
127 .sp
128 The file status flags and their semantics are described in
129 .BR open (2).
130 .TP
131 .B F_GETFL
132 Read the file status flags.
133 .TP
134 .B F_SETFL
135 Set the file status flags to the value specified by
136 .IR arg .
137 File access mode
138 .RB ( O_RDONLY ", " O_WRONLY ", " O_RDWR )
139 and file creation flags
140 .RB (e.g., O_CREAT ", " O_EXCL ", " O_TRUNC )
141 in
142 .I arg
143 are ignored.
144 On Linux this command can only change the
145 .BR O_APPEND ,
146 .BR O_ASYNC ,
147 .BR O_DIRECT ,
148 .BR O_NOATIME ,
149 and
150 .BR O_NONBLOCK
151 flags.
152 .\" FIXME According to SUSv3, O_SYNC should also be modifiable via
153 .\" fcntl(2), but currently Linux does not permit this -- MTK, Dec 04
154 .P
155 .SS "Advisory locking"
156 .BR F_GETLK ", " F_SETLK " and " F_SETLKW
157 are used to acquire, release, and test for the existence of record
158 locks (also known as file-segment or file-region locks).
159 The third argument
160 .I lock
161 is a pointer to a structure that has at least the following fields
162 (in unspecified order).
163 .in +2n
164 .nf
165 .sp
166 struct flock {
167 ...
168 short l_type; /* Type of lock: F_RDLCK,
169 F_WRLCK, F_UNLCK */
170 short l_whence; /* How to interpret l_start:
171 SEEK_SET, SEEK_CUR, SEEK_END */
172 off_t l_start; /* Starting offset for lock */
173 off_t l_len; /* Number of bytes to lock */
174 pid_t l_pid; /* PID of process blocking our lock
175 (F_GETLK only) */
176 ...
177 };
178 .fi
179 .in -2n
180 .P
181 The
182 .IR l_whence ", " l_start ", and " l_len
183 fields of this structure specify the range of bytes we wish to lock.
184 .I l_start
185 is the starting offset for the lock, and is interpreted
186 relative to either:
187 the start of the file (if
188 .I l_whence
189 is
190 .BR SEEK_SET );
191 the current file offset (if
192 .I l_whence
193 is
194 .BR SEEK_CUR );
195 or the end of the file (if
196 .I l_whence
197 is
198 .BR SEEK_END ).
199 In the final two cases,
200 .I l_start
201 can be a negative number provided the
202 offset does not lie before the start of the file.
203 .I l_len
204 is a non-negative integer (but see the NOTES below) specifying
205 the number of bytes to be locked.
206 Bytes past the end of the file may be locked,
207 but not bytes before the start of the file.
208 Specifying 0 for
209 .I l_len
210 has the special meaning: lock all bytes starting at the
211 location specified by
212 .IR l_whence " and " l_start
213 through to the end of file, no matter how large the file grows.
214 .P
215 The
216 .I l_type
217 field can be used to place a read
218 .RB ( F_RDLCK )
219 or a write
220 .RB ( F_WRLCK )
221 lock on a file.
222 Any number of processes may hold a read lock (shared lock)
223 on a file region, but only one process may hold a write lock
224 (exclusive lock). An exclusive lock excludes all other locks,
225 both shared and exclusive.
226 A single process can hold only one type of lock on a file region;
227 if a new lock is applied to an already-locked region,
228 then the existing lock is converted to the new lock type.
229 (Such conversions may involve splitting, shrinking, or coalescing with
230 an existing lock if the byte range specified by the new lock does not
231 precisely coincide with the range of the existing lock.)
232 .TP
233 .B F_SETLK
234 Acquire a lock (when
235 .I l_type
236 is
237 .B F_RDLCK
238 or
239 .BR F_WRLCK )
240 or release a lock (when
241 .I l_type
242 is
243 .BR F_UNLCK )
244 on the bytes specified by the
245 .IR l_whence ", " l_start ", and " l_len
246 fields of
247 .IR lock .
248 If a conflicting lock is held by another process,
249 this call returns \-1 and sets
250 .I errno
251 to
252 .B EACCES
253 or
254 .BR EAGAIN .
255 .TP
256 .B F_SETLKW
257 As for
258 .BR F_SETLK ,
259 but if a conflicting lock is held on the file, then wait for that
260 lock to be released.
261 If a signal is caught while waiting, then the call is interrupted
262 and (after the signal handler has returned)
263 returns immediately (with return value \-1 and
264 .I errno
265 set to
266 .BR EINTR ).
267 .TP
268 .B F_GETLK
269 On input to this call,
270 .I lock
271 describes a lock we would like to place on the file.
272 If the lock could be placed,
273 .BR fcntl ()
274 does not actually place it, but returns
275 .B F_UNLCK
276 in the
277 .I l_type
278 field of
279 .I lock
280 and leaves the other fields of the structure unchanged.
281 If one or more incompatible locks would prevent
282 this lock being placed, then
283 .BR fcntl ()
284 returns details about one of these locks in the
285 .IR l_type ", " l_whence ", " l_start ", and " l_len
286 fields of
287 .I lock
288 and sets
289 .I l_pid
290 to be the PID of the process holding that lock.
291 .P
292 In order to place a read lock,
293 .I fd
294 must be open for reading.
295 In order to place a write lock,
296 .I fd
297 must be open for writing.
298 To place both types of lock, open a file read-write.
299 .P
300 As well as being removed by an explicit
301 .BR F_UNLCK ,
302 record locks are automatically released when the process
303 terminates or if it closes
304 .I any
305 file descriptor referring to a file on which locks are held.
306 .\" (Additional file descriptors referring to the same file
307 .\" may have been obtained by calls to
308 .\" .BR open "(2), " dup "(2), " dup2 "(2), or " fcntl (2).)
309 This is bad: it means that a process can lose the locks on
310 a file like
311 .I /etc/passwd
312 or
313 .I /etc/mtab
314 when for some reason a library function decides to open, read
315 and close it.
316 .P
317 Record locks are not inherited by a child created via
318 .BR fork (2),
319 but are preserved across an
320 .BR execve (2).
321 .P
322 Because of the buffering performed by the
323 .BR stdio (3)
324 library, the use of record locking with routines in that package
325 should be avoided; use
326 .BR read (2)
327 and
328 .BR write (2)
329 instead.
330 .P
331 .SS "Mandatory locking"
332 (Non-POSIX.)
333 The above record locks may be either advisory or mandatory,
334 and are advisory by default.
335 To make use of mandatory locks, mandatory locking must be enabled
336 (using the "\-o mand" option to
337 .BR mount (8))
338 for the file system containing the
339 file to be locked and enabled on the file itself (by disabling
340 group execute permission on the file and enabling the set-group-ID
341 permission bit).
342
343 Advisory locks are not enforced and are useful only between
344 cooperating processes. Mandatory locks are enforced for all
345 processes.
346 .P
347 .SS "Managing signals"
348 .BR F_GETOWN ", " F_SETOWN ", " F_GETSIG " and " F_SETSIG
349 are used to manage I/O availability signals:
350 .TP
351 .B F_GETOWN
352 Get the process ID or process group currently receiving SIGIO
353 and SIGURG signals for events on file descriptor
354 .IR fd .
355 Process IDs are returned as positive values;
356 process group IDs are returned as negative values (but see BUGS below).
357 .TP
358 .B F_SETOWN
359 Set the process ID or process group ID that will receive SIGIO
360 and SIGURG signals for events on file descriptor
361 .IR fd .
362 A process ID is specified as a positive value;
363 a process group ID is specified as a negative value.
364 Most commonly, the calling process specifies itself as the owner
365 (that is,
366 .I arg
367 is specified as
368 .BR getpid ()).
369
370 .\" From glibc.info:
371 If you set the
372 .B O_ASYNC
373 status flag on a file descriptor (either by providing this flag with the
374 .BR open (2)
375 .\" FIXME setting O_ASYNC via open() does not seem to be effective -- MTK
376 call, or by using the
377 .B F_SETFL
378 command of
379 .BR fcntl ()),
380 a SIGIO signal is sent whenever input or output becomes possible
381 on that file descriptor.
382 .B F_SETSIG
383 can be used to obtain delivery of a signal other than SIGIO.
384 If this permission check fails, then the signal is
385 silently discarded.
386
387 Sending a signal to the owner process (group) specified by
388 .B F_SETOWN
389 is subject to the same permissions checks as are described for
390 .BR kill (2),
391 where the sending process is the one that employs
392 .BR F_SETOWN
393 (but see BUGS below).
394 .sp
395 If the file descriptor
396 .I fd
397 refers to a socket,
398 .B F_SETOWN
399 also selects
400 the recipient of SIGURG signals that are delivered when out-of-band
401 data arrives on that socket. (SIGURG is sent in any situation where
402 .BR select (2)
403 would report the socket as having an "exceptional condition".)
404 .\" The following appears to be rubbish. It doesn't seem to
405 .\" be true according to the kernel source, and I can write
406 .\" a program that gets a terminal-generated SIGIO even though
407 .\" it is not the foreground process group of the terminal.
408 .\" -- MTK, 8 Apr 05
409 .\"
410 .\" If the file descriptor
411 .\" .I fd
412 .\" refers to a terminal device, then SIGIO
413 .\" signals are sent to the foreground process group of the terminal.
414
415 If a non-zero value is given to
416 .B F_SETSIG
417 in a multi-threaded process running with a threading library
418 that supports thread groups (e.g., NPTL),
419 then a positive value given to
420 .B F_SETOWN
421 has a different meaning:
422 .\" The relevant place in the (2.6) kernel source is the
423 .\" 'switch' in fs/fcntl.c::send_sigio_to_task() -- MTK, Apr 2005
424 instead of being a process ID identifying a whole process,
425 it is a thread ID identifying a specific thread within a process.
426 Consequently, it may be necessary to pass
427 .B F_SETOWN
428 the result of
429 .BR gettid ()
430 instead of
431 .BR getpid ()
432 to get sensible results when
433 .B F_SETSIG
434 is used.
435 (In current Linux threading implementations,
436 a main thread's thread ID is the same as its process ID.
437 This means that a single-threaded program can equally use
438 .BR gettid ()
439 or
440 .BR getpid ()
441 in this scenario.)
442 Note, however, that the statements in this paragraph do not apply
443 to the SIGURG signal generated for out-of-band data on a socket:
444 this signal is always sent to either a process or a process group,
445 depending on the value given to
446 .BR F_SETOWN .
447 .\" send_sigurg()/send_sigurg_to_task() bypasses
448 .\" kill_fasync()/send_sigio()/send_sigio_to_task()
449 .\" to directly call send_group_sig_info()
450 .\" -- MTK, Apr 2005 (kernel 2.6.11)
451 Note also that Linux imposes a limit on the
452 number of real-time signals that may be queued to a
453 process (see
454 .BR getrlimit (2)
455 and
456 .BR signal (7))
457 and if this limit is reached, then the kernel reverts to
458 delivering SIGIO, and this signal is delivered to the entire
459 process rather than to a specific thread.
460 .\" See fs/fcntl.c::send_sigio_to_task() (2.4/2.6) sources -- MTK, Apr 05
461 .TP
462 .B F_GETSIG
463 Get the signal sent when input or output becomes possible. A value of
464 zero means SIGIO is sent. Any other value (including SIGIO) is the
465 signal sent instead, and in this case additional info is available to
466 the signal handler if installed with SA_SIGINFO.
467 .TP
468 .B F_SETSIG
469 Sets the signal sent when input or output becomes possible. A value of
470 zero means to send the default SIGIO signal. Any other value (including
471 SIGIO) is the signal to send instead, and in this case additional info
472 is available to the signal handler if installed with SA_SIGINFO.
473 .sp
474 Additionally, passing a non-zero value to
475 .B F_SETSIG
476 changes the signal recipient from a whole process to a specific thread
477 within a process.
478 See the description of
479 .B F_SETOWN
480 for more details.
481 .sp
482 By using
483 .B F_SETSIG
484 with a non-zero value, and setting SA_SIGINFO for the
485 signal handler (see
486 .BR sigaction (2)),
487 extra information about I/O events is passed to
488 the handler in a
489 .I siginfo_t
490 structure.
491 If the
492 .I si_code
493 field indicates the source is SI_SIGIO, the
494 .I si_fd
495 field gives the file descriptor associated with the event. Otherwise,
496 there is no indication which file descriptors are pending, and you
497 should use the usual mechanisms
498 .RB ( select (2),
499 .BR poll (2),
500 .BR read (2)
501 with
502 .B O_NONBLOCK
503 set etc.) to determine which file descriptors are available for I/O.
504 .sp
505 By selecting a POSIX.1b real time signal (value >= SIGRTMIN), multiple
506 I/O events may be queued using the same signal numbers. (Queuing is
507 dependent on available memory). Extra information is available
508 if SA_SIGINFO is set for the signal handler, as above.
509 .PP
510 Using these mechanisms, a program can implement fully asynchronous I/O
511 without using
512 .BR select (2)
513 or
514 .BR poll (2)
515 most of the time.
516 .PP
517 The use of
518 .BR O_ASYNC ,
519 .BR F_GETOWN ,
520 .B F_SETOWN
521 is specific to BSD and Linux.
522 .B F_GETSIG
523 and
524 .B F_SETSIG
525 are Linux-specific. POSIX has asynchronous I/O and the
526 .I aio_sigevent
527 structure to achieve similar things; these are also available
528 in Linux as part of the GNU C Library (Glibc).
529 .P
530 .SS Leases
531 .B F_SETLEASE
532 and
533 .B F_GETLEASE
534 (Linux 2.4 onwards) are used (respectively) to establish and
535 retrieve the current setting of the calling process's lease on
536 the file referred to by
537 .IR fd .
538 A file lease provides a mechanism whereby the process holding
539 the lease (the "lease holder") is notified (via delivery of a signal)
540 when a process (the "lease breaker") tries to
541 .BR open (2)
542 or
543 .BR truncate (2)
544 that file.
545 .TP
546 .B F_SETLEASE
547 Set or remove a file lease according to which of the following
548 values is specified in the integer
549 .IR arg :
550
551 .RS
552 .TP
553 .B F_RDLCK
554 Take out a read lease.
555 This will cause the calling process to be notified when
556 the file is opened for writing or is truncated.
557 .\" The following became true in kernel 2.6.10:
558 .\" See the man-pages-2.09 Changelog for further info.
559 A read lease can only be placed on a file descriptor that
560 is opened read-only.
561 .TP
562 .B F_WRLCK
563 Take out a write lease.
564 This will cause the caller to be notified when
565 the file is opened for reading or writing or is truncated.
566 A write lease may be placed on a file only if no other process
567 currently has the file open.
568 .TP
569 .B F_UNLCK
570 Remove our lease from the file.
571 .RE
572 .P
573 A process may hold only one type of lease on a file.
574 .P
575 Leases may only be taken out on regular files.
576 An unprivileged process may only take out a lease on a file whose
577 UID matches the file system UID of the process.
578 A process with the
579 .B CAP_LEASE
580 capability may take out leases on arbitrary files.
581 .TP
582 .B F_GETLEASE
583 Indicates what type of lease we hold on the file
584 referred to by
585 .I fd
586 by returning either
587 .BR F_RDLCK ", " F_WRLCK ", or " F_UNLCK,
588 indicating, respectively, that the calling process holds a
589 read, a write, or no lease on the file.
590 (The third argument to
591 .BR fcntl ()
592 is omitted.)
593 .PP
594 When a process (the "lease breaker") performs an
595 .BR open ()
596 or
597 .BR truncate ()
598 that conflicts with a lease established via
599 .BR F_SETLEASE ,
600 the system call is blocked by the kernel, unless the
601 .B O_NONBLOCK
602 flag was specified to
603 .BR open (),
604 in which case the system call will return with the error
605 .BR EWOULDBLOCK .
606 The kernel notifies the lease holder by sending it a signal
607 (SIGIO by default).
608 The lease holder should respond to receipt of this signal by doing
609 whatever cleanup is required in preparation for the file to be
610 accessed by another process (e.g., flushing cached buffers) and
611 then either remove or downgrade its lease.
612 A lease is removed by performing an
613 .B F_SETLEASE
614 command specifying
615 .I arg
616 as
617 .BR F_UNLCK .
618 If we currently hold a write lease on the file,
619 and the lease breaker is opening the file for reading,
620 then it is sufficient to downgrade the lease to a read lease.
621 This is done by performing an
622 .B F_SETLEASE
623 command specifying
624 .I arg
625 as
626 .BR F_RDLCK .
627
628 If the lease holder fails to downgrade or remove the lease within
629 the number of seconds specified in
630 .I /proc/sys/fs/lease-break-time
631 then the kernel forcibly removes or downgrades the lease holder's lease.
632
633 Once the lease has been voluntarily or forcibly removed or downgraded,
634 and assuming the lease breaker has not unblocked its system call,
635 the kernel permits the lease breaker's system call to proceed.
636
637 The default signal used to notify the lease holder is SIGIO,
638 but this can be changed using the
639 .B F_SETSIG
640 command to
641 .BR fcntl ().
642 If a
643 .B F_SETSIG
644 command is performed (even one specifying SIGIO), and the signal
645 handler is established using SA_SIGINFO, then the handler will
646 receive a
647 .I siginfo_t
648 structure as its second argument, and the
649 .I si_fd
650 field of this argument will hold the descriptor of the leased file
651 that has been accessed by another process.
652 (This is useful if the caller holds leases against multiple files).
653 .P
654 .SS "File and directory change notification"
655 .\" FIXME -- Eventually we may want to add a note about inotify
656 .\" here (MTK, Jun 05)
657 .TP
658 .B F_NOTIFY
659 (Linux 2.4 onwards)
660 Provide notification when the directory referred to by
661 .I fd
662 or any of the files that it contains is changed.
663 The events to be notified are specified in
664 .IR arg ,
665 which is a bit mask specified by ORing together zero or more of
666 the following bits:
667
668 .TS
669 l l
670 ----
671 lB l.
672 Bit Description (event in directory)
673 DN_ACCESS A file was accessed (read, pread, readv)
674 DN_MODIFY A file was modified (write, pwrite,
675 writev, truncate, ftruncate)
676 DN_CREATE A file was created (open, creat, mknod,
677 mkdir, link, symlink, rename)
678 DN_DELETE A file was unlinked (unlink, rename to
679 another directory, rmdir)
680 DN_RENAME A file was renamed within this
681 directory (rename)
682 DN_ATTRIB The attributes of a file were changed
683 (chown, chmod, utime[s])
684 .TE
685 .sp
686 (In order to obtain these definitions, the _GNU_SOURCE macro must be
687 defined before including <fcntl.h>.)
688 .sp
689 Directory notifications are normally "one-shot", and the application
690 must re-register to receive further notifications.
691 Alternatively, if
692 .B DN_MULTISHOT
693 is included in
694 .IR arg ,
695 then notification will remain in effect until explicitly removed.
696
697 .\" The following does seem a poor API-design choice...
698 A series of
699 .B F_NOTIFY
700 requests is cumulative, with the events in
701 .I arg
702 being added to the set already monitored.
703 To disable notification of all events, make an
704 .B F_NOTIFY
705 call specifying
706 .I arg
707 as 0.
708 .sp
709 Notification occurs via delivery of a signal.
710 The default signal is SIGIO, but this can be changed using the
711 .B F_SETSIG
712 command to
713 .BR fcntl ().
714 In the latter case, the signal handler receives a
715 .I siginfo_t
716 structure as its second argument (if the handler was
717 established using SA_SIGINFO) and the
718 .I si_fd
719 field of this structure contains the file descriptor which
720 generated the notification (useful when establishing notification
721 on multiple directories).
722 .sp
723 Especially when using
724 .BR DN_MULTISHOT ,
725 a POSIX.1b real time signal should be used for notification,
726 so that multiple notifications can be queued.
727 .SH "RETURN VALUE"
728 For a successful call, the return value depends on the operation:
729 .TP 0.9i
730 .B F_DUPFD
731 The new descriptor.
732 .TP
733 .B F_GETFD
734 Value of flags.
735 .TP
736 .B F_GETFL
737 Value of flags.
738 .TP
739 .B F_GETOWN
740 Value of descriptor owner.
741 .TP
742 .B F_GETSIG
743 Value of signal sent when read or write becomes possible, or zero
744 for traditional SIGIO behaviour.
745 .TP
746 All other commands
747 Zero.
748 .PP
749 On error, \-1 is returned, and
750 .I errno
751 is set appropriately.
752 .SH ERRORS
753 .TP
754 .BR EACCES " or " EAGAIN
755 Operation is prohibited by locks held by other processes.
756 Or, operation is prohibited because the file has been memory-mapped by
757 another process.
758 .TP
759 .B EBADF
760 .I fd
761 is not an open file descriptor, or the command was
762 .B F_SETLK
763 or
764 .B F_SETLKW
765 and the file descriptor open mode doesn't match with the
766 type of lock requested.
767 .TP
768 .B EDEADLK
769 It was detected that the specified
770 .B F_SETLKW
771 command would cause a deadlock.
772 .TP
773 .B EFAULT
774 .I lock
775 is outside your accessible address space.
776 .TP
777 .B EINTR
778 For
779 .BR F_SETLKW ,
780 the command was interrupted by a signal.
781 For
782 .BR F_GETLK " and " F_SETLK ,
783 the command was interrupted by a signal before the lock was checked or
784 acquired. Most likely when locking a remote file (e.g. locking over
785 NFS), but can sometimes happen locally.
786 .TP
787 .B EINVAL
788 For
789 .BR F_DUPFD ,
790 .I arg
791 is negative or is greater than the maximum allowable value. For
792 .BR F_SETSIG ,
793 .I arg
794 is not an allowable signal number.
795 .TP
796 .B EMFILE
797 For
798 .BR F_DUPFD ,
799 the process already has the maximum number of file descriptors open.
800 .TP
801 .B ENOLCK
802 Too many segment locks open, lock table is full, or a remote locking
803 protocol failed (e.g. locking over NFS).
804 .TP
805 .B EPERM
806 Attempted to clear the
807 .B O_APPEND
808 flag on a file that has the append-only attribute set.
809 .SH NOTES
810 The errors returned by
811 .BR dup2 ()
812 are different from those returned by
813 .BR F_DUPFD .
814
815 Since kernel 2.0, there is no interaction between the types of lock
816 placed by
817 .BR flock (2)
818 and
819 .BR fcntl (2).
820
821 POSIX 1003.1-2001 allows
822 .I l_len
823 to be negative. (And if it is, the interval described by the lock
824 covers bytes
825 .IR l_start + l_len
826 up to and including
827 .IR l_start \-1.)
828 This is supported by Linux since Linux 2.4.21 and 2.5.49.
829
830 Several systems have more fields in
831 .I "struct flock"
832 such as e.g.
833 .IR l_sysid .
834 Clearly,
835 .I l_pid
836 alone is not going to be very useful if the process holding the lock
837 may live on a different machine.
838 .SH BUGS
839 A limitation of the Linux system call conventions on some
840 architectures (notably x86) means that if a (negative)
841 process group ID to be returned by
842 .B F_GETOWN
843 falls in the range \-1 to \-4095, then the return value is wrongly
844 interpreted by glibc as an error in the system call;
845 .\" glibc source: sysdeps/unix/sysv/linux/i386/sysdep.h
846 that is, the return value of
847 .BR fcntl ()
848 will be \-1, and
849 .I errno
850 will contain the (positive) process group ID.
851 .\" FIXME Dec 04: some limited testing on alpha and ia64 seems to
852 .\" indicate that ANY negative PGID value will cause F_GETOWN
853 .\" to misinterpret the return as an error. Some other architectures
854 .\" seem to have the same range check as x86. -- MTK
855
856 In Linux 2.4 and earlier, there is bug that can occur
857 when an unprivileged process uses
858 .B F_SETOWN
859 to specify the owner
860 of a socket file descriptor
861 as a process (group) other than the caller.
862 In this case,
863 .BR fcntl ()
864 can return \-1 with
865 .I errno
866 set to
867 .BR EPERM ,
868 even when the owner process (group) is one that the caller
869 has permission to send signals to.
870 Despite this error return, the file descriptor owner is set,
871 and signals will be sent to the owner.
872 .SH "CONFORMING TO"
873 SVr4, SVID, POSIX, X/OPEN, 4.3BSD. Only the operations F_DUPFD,
874 F_GETFD, F_SETFD, F_GETFL, F_SETFL, F_GETLK, F_SETLK, F_SETLKW,
875 F_GETOWN, and F_SETOWN are specified in POSIX.1.
876 F_GETSIG, F_SETSIG, F_NOTIFY, F_GETLEASE, and F_SETLEASE
877 are Linux specific.
878 (Define the _GNU_SOURCE macro before including <fcntl.h> to
879 obtain these definitions.)
880 The flags legal for F_GETFL/F_SETFL are those supported by
881 .BR open (2)
882 and vary between these systems; O_APPEND, O_NONBLOCK, O_RDONLY,
883 and O_RDWR are specified in POSIX.1. SVr4 supports several other
884 options and flags not documented here.
885 .PP
886 SVr4 documents additional EIO, ENOLINK and EOVERFLOW error conditions.
887 .SH "SEE ALSO"
888 .BR dup2 (2),
889 .BR flock (2),
890 .BR open (2),
891 .BR socket (2),
892 .BR lockf (3),
893 .BR capabilities (7)
894 .P
895 See also locks.txt, mandatory.txt, and dnotify.txt in
896 /usr/src/linux/Documentation.