]> git.saurik.com Git - apple/xnu.git/blame - bsd/man/man2/fcntl.2
xnu-6153.141.1.tar.gz
[apple/xnu.git] / bsd / man / man2 / fcntl.2
CommitLineData
b0d623f7 1.\"
6d2010ae 2.\" Copyright (c) 2011 Apple Inc. All rights reserved.
b0d623f7
A
3.\"
4.\" @APPLE_LICENSE_HEADER_START@
5.\"
6.\" This file contains Original Code and/or Modifications of Original Code
7.\" as defined in and that are subject to the Apple Public Source License
8.\" Version 2.0 (the 'License'). You may not use this file except in
9.\" compliance with the License. Please obtain a copy of the License at
10.\" http://www.opensource.apple.com/apsl/ and read it before using this
11.\" file.
12.\"
13.\" The Original Code and all software distributed under the License are
14.\" distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
15.\" EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
16.\" INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
17.\" FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
18.\" Please see the License for the specific language governing rights and
19.\" limitations under the License.
20.\"
21.\" @APPLE_LICENSE_HEADER_END@
22.\"
23.\"
9bccf70c
A
24.\" $NetBSD: fcntl.2,v 1.6 1995/02/27 12:32:29 cgd Exp $
25.\"
26.\" Copyright (c) 1983, 1993
27.\" The Regents of the University of California. All rights reserved.
28.\"
29.\" Redistribution and use in source and binary forms, with or without
30.\" modification, are permitted provided that the following conditions
31.\" are met:
32.\" 1. Redistributions of source code must retain the above copyright
33.\" notice, this list of conditions and the following disclaimer.
34.\" 2. Redistributions in binary form must reproduce the above copyright
35.\" notice, this list of conditions and the following disclaimer in the
36.\" documentation and/or other materials provided with the distribution.
37.\" 3. All advertising materials mentioning features or use of this software
38.\" must display the following acknowledgement:
39.\" This product includes software developed by the University of
40.\" California, Berkeley and its contributors.
41.\" 4. Neither the name of the University nor the names of its contributors
42.\" may be used to endorse or promote products derived from this software
43.\" without specific prior written permission.
44.\"
45.\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
46.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
47.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
48.\" ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
49.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
50.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
51.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
52.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
53.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
54.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
55.\" SUCH DAMAGE.
56.\"
57.\" @(#)fcntl.2 8.2 (Berkeley) 1/12/94
58.\"
0a7de745 59.Dd August 8, 2018
9bccf70c
A
60.Dt FCNTL 2
61.Os BSD 4.2
62.Sh NAME
63.Nm fcntl
64.Nd file control
65.Sh SYNOPSIS
66.Fd #include <fcntl.h>
67.Ft int
2d21ac55
A
68.Fo fcntl
69.Fa "int fildes"
70.Fa "int cmd"
71.Fa "..."
72.Fc
9bccf70c 73.Sh DESCRIPTION
39037602 74.Fn fcntl
9bccf70c
A
75provides for control over descriptors.
76The argument
2d21ac55 77.Fa fildes
9bccf70c
A
78is a descriptor to be operated on by
79.Fa cmd
80as follows:
81.Bl -tag -width F_WRITEBOOTSTRAPX
82.It Dv F_DUPFD
83Return a new descriptor as follows:
84.Pp
85.Bl -bullet -compact -offset 4n
86.It
87Lowest numbered available descriptor greater than or equal to
88.Fa arg .
89.It
90Same object references as the original descriptor.
91.It
92New descriptor shares the same file offset if the object
93was a file.
94.It
95Same access mode (read, write or read/write).
96.It
97Same file status flags (i.e., both file descriptors
98share the same file status flags).
99.It
100The close-on-exec flag associated with the new file descriptor
6d2010ae 101is cleared so that the descriptor remains open across an
9bccf70c 102.Xr execv 2
6d2010ae 103system call.
9bccf70c 104.El
6d2010ae
A
105.It Dv F_DUPFD_CLOEXEC
106Like
107.Dv F_DUPFD ,
108except that the close-on-exec flag associated with the new file descriptor
109is set.
9bccf70c 110.It Dv F_GETFD
6d2010ae
A
111Get the flags associated with the file descriptor
112.Fa fildes ,
113as described below
9bccf70c
A
114.Fa ( arg
115is ignored).
116.It Dv F_SETFD
6d2010ae
A
117Set the file descriptor flags to
118.Fa arg .
9bccf70c
A
119.It Dv F_GETFL
120Get descriptor status flags, as described below
121.Fa ( arg
122is ignored).
123.It Dv F_SETFL
124Set descriptor status flags to
125.Fa arg .
126.It Dv F_GETOWN
127Get the process ID or process group
128currently receiving
129.Dv SIGIO
130and
131.Dv SIGURG
132signals; process groups are returned
133as negative values
134.Fa ( arg
135is ignored).
136.It Dv F_SETOWN
137Set the process or process group
138to receive
139.Dv SIGIO
140and
141.Dv SIGURG
142signals;
143process groups are specified by supplying
144.Fa arg
145as negative, otherwise
146.Fa arg
147is interpreted as a process ID.
2d21ac55 148.It Dv F_GETPATH
cb323159
A
149Get the path of the file descriptor
150.It Dv F_GETPATH_NOFIRMLINK
151Get the non firmlinked path of the file descriptor
2d21ac55
A
152.Fa Fildes .
153The argument must be a buffer of size
154.Sy MAXPATHLEN
155or greater.
9bccf70c 156.It Dv F_PREALLOCATE
b0d623f7 157Preallocate file storage space. Note: upon success,
a39ff7e2
A
158the space that is allocated can be the size requested,
159larger than the size requested, or (if the
160.Dv F_ALLOCATEALL
161flag is not provided) smaller than the space requested.
813fb2f6
A
162.It Dv F_PUNCHHOLE
163Deallocate a region and replace it with a hole. Subsequent reads of the
164affected region will return bytes of zeros that are usually not backed by
165physical blocks. This will not change the actual file size. Holes must be
166aligned to file system block boundaries. This will fail on
167file systems that do not support this interface.
9bccf70c
A
168.It Dv F_SETSIZE
169Truncate a file without zeroing space.
170The calling process must have root privileges.
171.It Dv F_RDADVISE
172Issue an advisory read async with no copy to user.
173.It Dv F_RDAHEAD
174Turn read ahead off/on.
175A zero value in
176.Fa arg
177disables read ahead.
178A non-zero value in
179.Fa arg
180turns read ahead on.
9bccf70c
A
181.It Dv F_NOCACHE
182Turns data caching off/on. A non-zero value in
183.Fa arg
184turns data caching off.
185A value of zero in
186.Fa arg
187turns data caching on.
188.It Dv F_LOG2PHYS
189Get disk device information.
5ba3f43e 190Currently this only returns the
9bccf70c 191disk device address that corresponds
5ba3f43e
A
192to the current file offset. Note that the system
193may return -1 as the disk device address if the file is not
194backed by physical blocks. This is subject
39236c6e 195to change.
6d2010ae
A
196.It Dv F_LOG2PHYS_EXT
197Variant of F_LOG2PHYS that uses the passed in
198file offset and length.
cb323159
A
199.It Dv F_BARRIERFSYNC
200Does the same thing as
201.Xr fsync 2
202then issues a barrier command to the drive
203.Fa ( arg
204is ignored).
205The barrier applies to I/O that have been flushed with
206.Xr fsync 2
207on the same device before. These operations are guaranteed to
208be persisted before any other I/O that would follow the barrier,
209although no assumption should be made on what has been persisted
210or not when this call returns. After the barrier has been issued,
211operations on other FDs that have been fsync'd before can still be
212re-ordered by the device, but not after the barrier. This is
213typically useful to guarantee valid state on disk when ordering is a
214concern but durability is not. A barrier can be used to order two phases of operations on
215a set of file descriptors and ensure that no file can possibly get persisted
216with the effect of the second phase without the effect of the first one. To do so,
217execute operations of phase one, then
218.Xr fsync 2
219each FD and issue a single barrier. Finally execute operations of phase two.
220This is currently implemented on HFS and APFS. It requires hardware support,
221which Apple SSDs are guaranteed to provide.
91447636
A
222.It Dv F_FULLFSYNC
223Does the same thing as
224.Xr fsync 2
225then asks the drive to
226flush all buffered data to
227the permanent storage device
228.Fa ( arg
229is ignored).
cb323159
A
230As this drains the entire queue of the device and acts as a
231barrier, data that had been fsync'd on the same device before
232is guaranteed to be persisted when this call returns.
2d21ac55 233This is currently implemented on HFS, MS-DOS (FAT),
cb323159 234Universal Disk Format (UDF) and APFS file systems.
2d21ac55
A
235The operation may take quite a while to complete.
236Certain FireWire drives have also been known
237to ignore the request to flush their buffered data.
6d2010ae
A
238.It Dv F_SETNOSIGPIPE
239Determines whether a
240.Dv SIGPIPE
241signal will be generated when a write fails on a pipe or socket for
242which there is no reader. If
243.Fa arg
244is non-zero,
245.Dv SIGPIPE
246generation is disabled for descriptor
247.Fa fildes ,
248while an
249.Fa arg
250of zero enables it (the default).
251.It Dv F_GETNOSIGPIPE
252Returns whether a
253.Dv SIGPIPE
254signal will be generated when a write fails on a pipe or socket
255for which there is no reader. The semantics of the return value
256match those of the
257.Fa arg
258of
259.Dv F_SETNOSIGPIPE .
260.El
261.Pp
262The flags for the
263.Dv F_GETFD
264and
265.Dv F_SETFD
266commands are as follows:
267.Bl -tag -width FD_CLOEXECX -offset indent
268.It Dv FD_CLOEXEC
269Close-on-exec; the given file descriptor will be automatically
270closed in the successor process image when one of the
271.Xr execv 2
272or
273.Xr posix_spawn 2
274family of system calls is invoked.
9bccf70c
A
275.El
276.Pp
277The flags for the
278.Dv F_GETFL
279and
280.Dv F_SETFL
281commands are as follows:
282.Bl -tag -width O_NONBLOCKX -offset indent
283.It Dv O_NONBLOCK
284Non-blocking I/O; if no data is available to a
285.Xr read
286call, or if a
287.Xr write
288operation would block,
289the read or write call returns -1 with the error
290.Er EAGAIN .
291.It Dv O_APPEND
292Force each write to append at the end of file;
293corresponds to the
294.Dv O_APPEND
295flag of
296.Xr open 2 .
297.It Dv O_ASYNC
298Enable the
299.Dv SIGIO
300signal to be sent to the process group
301when I/O is possible, e.g.,
302upon availability of data to be read.
303.El
304.Pp
305Several commands are available for doing advisory file locking;
306they all operate on the following structure:
307.ne 7v
308.Bd -literal
309 struct flock {
310 off_t l_start; /* starting offset */
311 off_t l_len; /* len = 0 means until end of file */
312 pid_t l_pid; /* lock owner */
313 short l_type; /* lock type: read/write, etc. */
314 short l_whence; /* type of l_start */
315 };
316.Ed
317.Pp
318The commands available for advisory record locking are as follows:
319.Bl -tag -width F_SETLKWX
320.It Dv F_GETLK
321Get the first lock that blocks the lock description pointed to by the
322third argument,
323.Fa arg ,
324taken as a pointer to a
325.Fa "struct flock"
326(see above).
327The information retrieved overwrites the information passed to
328.Nm fcntl
329in the
330.Fa flock
331structure.
332If no lock is found that would prevent this lock from being created,
333the structure is left unchanged by this function call except for the
334lock type which is set to
335.Dv F_UNLCK .
336.It Dv F_SETLK
337Set or clear a file segment lock according to the lock description
338pointed to by the third argument,
339.Fa arg ,
340taken as a pointer to a
341.Fa "struct flock"
342(see above).
343.Dv F_SETLK
344is used to establish shared (or read) locks
345.Dv (F_RDLCK)
346or exclusive (or write) locks,
347.Dv (F_WRLCK) ,
348as well as remove either type of lock
349.Dv (F_UNLCK) .
350If a shared or exclusive lock cannot be set,
351.Nm fcntl
352returns immediately with
b0d623f7 353.Er EAGAIN .
9bccf70c
A
354.It Dv F_SETLKW
355This command is the same as
356.Dv F_SETLK
357except that if a shared or exclusive lock is blocked by other locks,
358the process waits until the request can be satisfied.
359If a signal that is to be caught is received while
360.Nm fcntl
361is waiting for a region, the
362.Nm fcntl
363will be interrupted if the signal handler has not specified the
364.Dv SA_RESTART
365(see
366.Xr sigaction 2 ) .
367.El
368.Pp
369When a shared lock has been set on a segment of a file,
370other processes can set shared locks on that segment
371or a portion of it.
372A shared lock prevents any other process from setting an exclusive
373lock on any portion of the protected area.
374A request for a shared lock fails if the file descriptor was not
375opened with read access.
376.Pp
377An exclusive lock prevents any other process from setting a shared lock or
378an exclusive lock on any portion of the protected area.
379A request for an exclusive lock fails if the file was not
380opened with write access.
381.Pp
382The value of
383.Fa l_whence
384is
385.Dv SEEK_SET ,
386.Dv SEEK_CUR ,
387or
388.Dv SEEK_END
389to indicate that the relative offset,
390.Fa l_start
391bytes, will be measured from the start of the file,
392current position, or end of the file, respectively.
393The value of
394.Fa l_len
395is the number of consecutive bytes to be locked.
396If
397.Fa l_len
398is negative, the result is undefined.
399The
400.Fa l_pid
401field is only used with
402.Dv F_GETLK
403to return the process ID of the process holding a blocking lock.
404After a successful
405.Dv F_GETLK
406request, the value of
407.Fa l_whence
408is
409.Dv SEEK_SET .
410.Pp
411Locks may start and extend beyond the current end of a file,
412but may not start or extend before the beginning of the file.
413A lock is set to extend to the largest possible value of the
414file offset for that file if
415.Fa l_len
416is set to zero. If
417.Fa l_whence
418and
419.Fa l_start
420point to the beginning of the file, and
421.Fa l_len
422is zero, the entire file is locked.
423If an application wishes only to do entire file locking, the
424.Xr flock 2
425system call is much more efficient.
426.Pp
427There is at most one type of lock set for each byte in the file.
428Before a successful return from an
429.Dv F_SETLK
430or an
431.Dv F_SETLKW
432request when the calling process has previously existing locks
433on bytes in the region specified by the request,
434the previous lock type for each byte in the specified
435region is replaced by the new lock type.
436As specified above under the descriptions
437of shared locks and exclusive locks, an
438.Dv F_SETLK
439or an
440.Dv F_SETLKW
441request fails or blocks respectively when another process has existing
442locks on bytes in the specified region and the type of any of those
443locks conflicts with the type specified in the request.
444.Pp
445This interface follows the completely stupid semantics of System V and
446.St -p1003.1-88
447that require that all locks associated with a file for a given process are
448removed when \fIany\fP file descriptor for that file is closed by that process.
449This semantic means that applications must be aware of any files that
450a subroutine library may access.
451For example if an application for updating the password file locks the
452password file database while making the update, and then calls
453.Xr getpwname 3
454to retrieve a record,
455the lock will be lost because
456.Xr getpwname 3
457opens, reads, and closes the password database.
458The database close will release all locks that the process has
459associated with the database, even if the library routine never
460requested a lock on the database.
461Another minor semantic problem with this interface is that
462locks are not inherited by a child process created using the
463.Xr fork 2
464function.
465The
466.Xr flock 2
467interface has much more rational last close semantics and
468allows locks to be inherited by child processes.
469.Xr Flock 2
470is recommended for applications that want to ensure the integrity
471of their locks when using library routines or wish to pass locks
472to their children.
473Note that
474.Xr flock 2
475and
476.Xr fcntl 2
477locks may be safely used concurrently.
478.Pp
479All locks associated with a file for a given process are
480removed when the process terminates.
481.Pp
482A potential for deadlock occurs if a process controlling a locked region
483is put to sleep by attempting to lock the locked region of another process.
484This implementation detects that sleeping until a locked region is unlocked
485would cause a deadlock and fails with an
486.Er EDEADLK
487error.
488.Pp
489The
490.Dv F_PREALLOCATE
491command operates on the following structure:
492.ne 7v
493.Bd -literal
494 typedef struct fstore {
495 u_int32_t fst_flags; /* IN: flags word */
496 int fst_posmode; /* IN: indicates offset field */
497 off_t fst_offset; /* IN: start of the region */
498 off_t fst_length; /* IN: size of the region */
499 off_t fst_bytesalloc; /* OUT: number of bytes allocated */
500 } fstore_t;
501.Ed
502.Pp
503The flags (fst_flags) for the
504.Dv F_PREALLOCATE
505command are as follows:
506.Bl -tag -width F_ALLOCATECONTIGX -offset indent
507.It Dv F_ALLOCATECONTIG
508Allocate contiguous space.
509.It Dv F_ALLOCATEALL
510Allocate all requested space or no space at all.
511.El
512.Pp
513The position modes (fst_posmode) for the
514.Dv F_PREALLOCATE
515command indicate how to use the offset field.
516The modes are as follows:
517.Bl -tag -width F_PEOFPOSMODEX -offset indent
518.It Dv F_PEOFPOSMODE
519Allocate from the physical end of file.
0a7de745
A
520In this case,
521.Fa fst_length
522indicates the number of newly allocated bytes desired.
9bccf70c
A
523.It Dv F_VOLPOSMODE
524Allocate from the volume offset.
525.El
526.Pp
527The
813fb2f6
A
528.Dv F_PUNCHHOLE
529command operates on the following structure:
530.ne 7v
531.Bd -literal
532 typedef struct fpunchhole {
533 u_int32_t fp_flags; /* unused */
534 u_int32_t reserved; /* (to maintain 8-byte alignment) */
535 off_t fp_offset; /* IN: start of the region */
536 off_t fp_length; /* IN: size of the region */
537 } fpunchhole_t;
538.Ed
539.Pp
540The
9bccf70c
A
541.Dv F_RDADVISE
542command operates on the following structure
543which holds information passed from the
544user to the system:
545.ne 7v
546.Bd -literal
547 struct radvisory {
548 off_t ra_offset; /* offset into the file */
549 int ra_count; /* size of the read */
550 };
551.Ed
552.Pp
553The
554.Dv F_READBOOTSTRAP and F_WRITEBOOTSTRAP
555commands operate on the following structure.
556.ne 7v
557.Bd -literal
558 typedef struct fbootstraptransfer {
559 off_t fbt_offset; /* IN: offset to start read/write */
560 size_t fbt_length; /* IN: number of bytes to transfer */
561 void *fbt_buffer; /* IN: buffer to be read/written */
562 } fbootstraptransfer_t;
563.Ed
564.Pp
565The
566.Dv F_LOG2PHYS
6d2010ae 567command operates on the following structure:
9bccf70c
A
568.ne 7v
569.Bd -literal
570 struct log2phys {
6d2010ae
A
571 u_int32_t l2p_flags; /* unused so far */
572 off_t l2p_contigbytes; /* unused so far */
573 off_t l2p_devoffset; /* bytes into device */
9bccf70c
A
574 };
575.Ed
6d2010ae
A
576.Pp
577The
578.Dv F_LOG2PHYS_EXT
579command operates on the same structure as F_LOG2PHYS but treats it as an in/out:
580.ne 7v
581.Bd -literal
582 struct log2phys {
583 u_int32_t l2p_flags; /* unused so far */
584 off_t l2p_contigbytes; /* IN: number of bytes to be queried;
585 OUT: number of contiguous bytes allocated at this position */
586 off_t l2p_devoffset; /* IN: bytes into file;
587 OUT: bytes into device */
588 };
589.Ed
590.Pp
591If
592.Fa fildes
593is a socket, then the
594.Dv F_SETNOSIGPIPE
595and
596.Dv F_GETNOSIGPIPE
597commands are directly analogous, and fully interoperate with the
598.Dv SO_NOSIGPIPE
599option of
600.Xr setsockopt 2
601and
602.Xr getsockopt 2
603respectively.
9bccf70c
A
604.Sh RETURN VALUES
605Upon successful completion, the value returned depends on
606.Fa cmd
607as follows:
608.Bl -tag -width F_GETOWNX -offset indent
609.It Dv F_DUPFD
610A new file descriptor.
611.It Dv F_GETFD
612Value of flag (only the low-order bit is defined).
613.It Dv F_GETFL
614Value of flags.
615.It Dv F_GETOWN
616Value of file descriptor owner.
617.It other
618Value other than -1.
619.El
620.Pp
621Otherwise, a value of -1 is returned and
622.Va errno
623is set to indicate the error.
624.Sh ERRORS
2d21ac55
A
625The
626.Fn fcntl
627system call will fail if:
9bccf70c 628.Bl -tag -width Er
2d21ac55 629.\" ==========
b0d623f7 630.It Bq Er EAGAIN
9bccf70c
A
631The argument
632.Fa cmd
633is
634.Dv F_SETLK ,
635the type of lock
636.Fa (l_type)
637is a shared lock
638.Dv (F_RDLCK)
639or exclusive lock
640.Dv (F_WRLCK) ,
641and the segment of a file to be locked is already
642exclusive-locked by another process;
643or the type is an exclusive lock and some portion of the
644segment of a file to be locked is already shared-locked or
645exclusive-locked by another process.
b0d623f7 646.It Bq Er EACCESS
9bccf70c
A
647The argument
648.Fa cmd
649is either
650.Dv F_SETSIZE
651or
652.Dv F_WRITEBOOTSTRAP
653and the calling process does not have root privileges.
2d21ac55 654.\" ==========
9bccf70c
A
655.It Bq Er EBADF
656.Fa Fildes
657is not a valid open file descriptor.
658.Pp
659The argument
660.Fa cmd
661is
662.Dv F_SETLK
663or
664.Dv F_SETLKW ,
665the type of lock
666.Fa (l_type)
667is a shared lock
668.Dv (F_RDLCK) ,
669and
670.Fa fildes
671is not a valid file descriptor open for reading.
672.Pp
673The argument
674.Fa cmd
675is
676.Dv F_SETLK
677or
678.Dv F_SETLKW ,
679the type of lock
680.Fa (l_type)
681is an exclusive lock
682.Dv (F_WRLCK) ,
683and
684.Fa fildes
685is not a valid file descriptor open for writing.
686.Pp
687The argument
688.Fa cmd
689is
690.Dv F_PREALLOCATE
691and the calling process does not have
692file write permission.
693.Pp
694The argument
695.Fa cmd
696is
697.Dv F_LOG2PHYS
6d2010ae
A
698or
699.Dv F_LOG2PHYS_EXT
9bccf70c
A
700and
701.Fa fildes
702is not a valid file descriptor open for reading.
2d21ac55 703.\" ==========
9bccf70c
A
704.It Bq Er EDEADLK
705The argument
706.Fa cmd
707is
708.Dv F_SETLKW ,
709and a deadlock condition was detected.
2d21ac55 710.\" ==========
9bccf70c
A
711.It Bq Er EINTR
712The argument
713.Fa cmd
714is
715.Dv F_SETLKW ,
716and the function was interrupted by a signal.
2d21ac55 717.\" ==========
9bccf70c
A
718.It Bq Er EINVAL
719.Fa Cmd
720is
721.Dv F_DUPFD
722and
723.Fa arg
724is negative or greater than the maximum allowable number
725(see
726.Xr getdtablesize 2 ) .
727.Pp
728The argument
729.Fa cmd
730is
731.Dv F_GETLK ,
732.Dv F_SETLK ,
733or
734.Dv F_SETLKW
735and the data to which
736.Fa arg
737points is not valid, or
738.Fa fildes
739refers to a file that does not support locking.
740.Pp
741The argument
742.Fa cmd
743is
744.Dv F_PREALLOCATE
745and the
746.Fa fst_posmode
747is not a valid mode,
748or when
749.Dv F_PEOFPOSMODE
750is set and
751.Fa fst_offset
752is a non-zero value,
753or when
754.Dv F_VOLPOSMODE
755is set and
756.Fa fst_offset
757is a negative or zero value.
758.Pp
759The argument
760.Fa cmd
813fb2f6
A
761is
762.Dv F_PUNCHHOLE
763and
764either
765.Fa fp_offset
766or
767.Fa fp_length
768are negative, or both
769.Fa fp_offset
770and
771.Fa fp_length
772are not multiples of the file system block size.
773.Pp
774The argument
775.Fa cmd
9bccf70c
A
776is either
777.Dv F_READBOOTSTRAP
778or
779.Dv F_WRITEBOOTSTRAP
780and the operation was attempted on a non-HFS disk type.
2d21ac55
A
781.\" ==========
782.It Bq Er EMFILE
783.Fa Cmd
784is
785.Dv F_DUPFD
786and the maximum allowed number of file descriptors are currently
787open.
788.\" ==========
9bccf70c
A
789.It Bq Er EMFILE
790The argument
791.Fa cmd
792is
793.Dv F_DUPED
794and the maximum number of file descriptors permitted for the
795process are already in use,
796or no file descriptors greater than or equal to
797.Fa arg
798are available.
2d21ac55 799.\" ==========
9bccf70c
A
800.It Bq Er ENOLCK
801The argument
802.Fa cmd
803is
804.Dv F_SETLK
805or
806.Dv F_SETLKW ,
807and satisfying the lock or unlock request would result in the
808number of locked regions in the system exceeding a system-imposed limit.
2d21ac55 809.\" ==========
a39ff7e2
A
810.It Bq Er ENOSPC
811The argument
812.Fa cmd
813is
814.Dv F_PREALLOCATE
815and either there is no space available on the volume containing
816.Fa fildes
817or
818.Fa fst_flags
819contains
820.Dv F_ALLOCATEALL
821and there is not enough space available on the volume containing
822.Fa fildes
823to satisfy the entire request.
824.Pp
825The argument
826.Fa cmd
827is
828.Dv F_PUNCHHOLE
829and there is not enough space available on the volume containing
830.Fa fildes
831to satisfy the request. As an example, a filesystem that supports
832cloned files may return this error if punching a hole requires the
833creation of a clone and there is not enough space available to do so.
834.\" ==========
2d21ac55
A
835.It Bq Er EOVERFLOW
836A return value would overflow its representation.
837For example,
838.Fa cmd
839is F_GETLK, F_SETLK, or F_SETLKW
840and the smallest (or, if l_len is non-zero, the largest) offset
841of a byte in the requested segment
842will not fit in an object of type off_t.
843.\" ==========
a39ff7e2
A
844.It Bq Er EPERM
845The argument cmd is
846.Dv F_PUNCHHOLE
847and the calling process does not have file write permission.
848.\" ==========
9bccf70c
A
849.It Bq Er ESRCH
850.Fa Cmd
851is
852.Dv F_SETOWN
853and
854the process ID given as argument is not in use.
855.El
856.Sh SEE ALSO
857.Xr close 2 ,
858.Xr execve 2 ,
859.Xr flock 2 ,
860.Xr getdtablesize 2 ,
861.Xr open 2 ,
6d2010ae
A
862.Xr pipe 2 ,
863.Xr socket 2 ,
864.Xr setsockopt 2 ,
9bccf70c
A
865.Xr sigaction 3
866.Sh HISTORY
867The
868.Fn fcntl
869function call appeared in
870.Bx 4.2 .