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