]> git.saurik.com Git - apple/xnu.git/blame - bsd/man/man2/fcntl.2
xnu-792.12.6.tar.gz
[apple/xnu.git] / bsd / man / man2 / fcntl.2
CommitLineData
9bccf70c
A
1.\" $NetBSD: fcntl.2,v 1.6 1995/02/27 12:32:29 cgd Exp $
2.\"
3.\" Copyright (c) 1983, 1993
4.\" The Regents of the University of California. All rights reserved.
5.\"
6.\" Redistribution and use in source and binary forms, with or without
7.\" modification, are permitted provided that the following conditions
8.\" are met:
9.\" 1. Redistributions of source code must retain the above copyright
10.\" notice, this list of conditions and the following disclaimer.
11.\" 2. Redistributions in binary form must reproduce the above copyright
12.\" notice, this list of conditions and the following disclaimer in the
13.\" documentation and/or other materials provided with the distribution.
14.\" 3. All advertising materials mentioning features or use of this software
15.\" must display the following acknowledgement:
16.\" This product includes software developed by the University of
17.\" California, Berkeley and its contributors.
18.\" 4. Neither the name of the University nor the names of its contributors
19.\" may be used to endorse or promote products derived from this software
20.\" without specific prior written permission.
21.\"
22.\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
23.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25.\" ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
26.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32.\" SUCH DAMAGE.
33.\"
34.\" @(#)fcntl.2 8.2 (Berkeley) 1/12/94
35.\"
36.Dd January 12, 1994
37.Dt FCNTL 2
38.Os BSD 4.2
39.Sh NAME
40.Nm fcntl
41.Nd file control
42.Sh SYNOPSIS
43.Fd #include <fcntl.h>
44.Ft int
45.Fn fcntl "int fd" "int cmd" "int arg"
46.Sh DESCRIPTION
47.Fn Fcntl
48provides for control over descriptors.
49The argument
50.Fa fd
51is a descriptor to be operated on by
52.Fa cmd
53as follows:
54.Bl -tag -width F_WRITEBOOTSTRAPX
55.It Dv F_DUPFD
56Return a new descriptor as follows:
57.Pp
58.Bl -bullet -compact -offset 4n
59.It
60Lowest numbered available descriptor greater than or equal to
61.Fa arg .
62.It
63Same object references as the original descriptor.
64.It
65New descriptor shares the same file offset if the object
66was a file.
67.It
68Same access mode (read, write or read/write).
69.It
70Same file status flags (i.e., both file descriptors
71share the same file status flags).
72.It
73The close-on-exec flag associated with the new file descriptor
74is set to remain open across
75.Xr execv 2
76system calls.
77.El
78.It Dv F_GETFD
79Get the close-on-exec flag associated with the file descriptor
80.Fa fd .
81If the low-order bit of the returned value is 0,
82the file will remain open across
83.Fn exec ,
84otherwise the file will be closed upon execution of
85.Fn exec
86.Fa ( arg
87is ignored).
88.It Dv F_SETFD
89Set the close-on-exec flag associated with
90.Fa fd
91to the low order bit of
92.Fa arg
93(0 or 1 as above).
94.It Dv F_GETFL
95Get descriptor status flags, as described below
96.Fa ( arg
97is ignored).
98.It Dv F_SETFL
99Set descriptor status flags to
100.Fa arg .
101.It Dv F_GETOWN
102Get the process ID or process group
103currently receiving
104.Dv SIGIO
105and
106.Dv SIGURG
107signals; process groups are returned
108as negative values
109.Fa ( arg
110is ignored).
111.It Dv F_SETOWN
112Set the process or process group
113to receive
114.Dv SIGIO
115and
116.Dv SIGURG
117signals;
118process groups are specified by supplying
119.Fa arg
120as negative, otherwise
121.Fa arg
122is interpreted as a process ID.
123.It Dv F_PREALLOCATE
124Preallocate file storage space.
125.It Dv F_SETSIZE
126Truncate a file without zeroing space.
127The calling process must have root privileges.
128.It Dv F_RDADVISE
129Issue an advisory read async with no copy to user.
130.It Dv F_RDAHEAD
131Turn read ahead off/on.
132A zero value in
133.Fa arg
134disables read ahead.
135A non-zero value in
136.Fa arg
137turns read ahead on.
138.It Dv F_READBOOTSTRAP
139Read bootstrap from disk.
140.It Dv F_WRITEBOOTSTRAP
141Write bootstrap on disk.
142The calling process must have root privileges.
143.It Dv F_NOCACHE
144Turns data caching off/on. A non-zero value in
145.Fa arg
146turns data caching off.
147A value of zero in
148.Fa arg
149turns data caching on.
150.It Dv F_LOG2PHYS
151Get disk device information.
152Currently this only includes the
153disk device address that corresponds
154to the current file offset.
91447636
A
155.It Dv F_FULLFSYNC
156Does the same thing as
157.Xr fsync 2
158then asks the drive to
159flush all buffered data to
160the permanent storage device
161.Fa ( arg
162is ignored).
163This is currently
164only implemented on HFS filesystems and
165the operation may take quite a while to
166complete. Certain FireWire drives have
167also been known to ignore this request.
9bccf70c
A
168.El
169.Pp
170The flags for the
171.Dv F_GETFL
172and
173.Dv F_SETFL
174commands are as follows:
175.Bl -tag -width O_NONBLOCKX -offset indent
176.It Dv O_NONBLOCK
177Non-blocking I/O; if no data is available to a
178.Xr read
179call, or if a
180.Xr write
181operation would block,
182the read or write call returns -1 with the error
183.Er EAGAIN .
184.It Dv O_APPEND
185Force each write to append at the end of file;
186corresponds to the
187.Dv O_APPEND
188flag of
189.Xr open 2 .
190.It Dv O_ASYNC
191Enable the
192.Dv SIGIO
193signal to be sent to the process group
194when I/O is possible, e.g.,
195upon availability of data to be read.
196.El
197.Pp
198Several commands are available for doing advisory file locking;
199they all operate on the following structure:
200.ne 7v
201.Bd -literal
202 struct flock {
203 off_t l_start; /* starting offset */
204 off_t l_len; /* len = 0 means until end of file */
205 pid_t l_pid; /* lock owner */
206 short l_type; /* lock type: read/write, etc. */
207 short l_whence; /* type of l_start */
208 };
209.Ed
210.Pp
211The commands available for advisory record locking are as follows:
212.Bl -tag -width F_SETLKWX
213.It Dv F_GETLK
214Get the first lock that blocks the lock description pointed to by the
215third argument,
216.Fa arg ,
217taken as a pointer to a
218.Fa "struct flock"
219(see above).
220The information retrieved overwrites the information passed to
221.Nm fcntl
222in the
223.Fa flock
224structure.
225If no lock is found that would prevent this lock from being created,
226the structure is left unchanged by this function call except for the
227lock type which is set to
228.Dv F_UNLCK .
229.It Dv F_SETLK
230Set or clear a file segment lock according to the lock description
231pointed to by the third argument,
232.Fa arg ,
233taken as a pointer to a
234.Fa "struct flock"
235(see above).
236.Dv F_SETLK
237is used to establish shared (or read) locks
238.Dv (F_RDLCK)
239or exclusive (or write) locks,
240.Dv (F_WRLCK) ,
241as well as remove either type of lock
242.Dv (F_UNLCK) .
243If a shared or exclusive lock cannot be set,
244.Nm fcntl
245returns immediately with
246.Er EACCES .
247.It Dv F_SETLKW
248This command is the same as
249.Dv F_SETLK
250except that if a shared or exclusive lock is blocked by other locks,
251the process waits until the request can be satisfied.
252If a signal that is to be caught is received while
253.Nm fcntl
254is waiting for a region, the
255.Nm fcntl
256will be interrupted if the signal handler has not specified the
257.Dv SA_RESTART
258(see
259.Xr sigaction 2 ) .
260.El
261.Pp
262When a shared lock has been set on a segment of a file,
263other processes can set shared locks on that segment
264or a portion of it.
265A shared lock prevents any other process from setting an exclusive
266lock on any portion of the protected area.
267A request for a shared lock fails if the file descriptor was not
268opened with read access.
269.Pp
270An exclusive lock prevents any other process from setting a shared lock or
271an exclusive lock on any portion of the protected area.
272A request for an exclusive lock fails if the file was not
273opened with write access.
274.Pp
275The value of
276.Fa l_whence
277is
278.Dv SEEK_SET ,
279.Dv SEEK_CUR ,
280or
281.Dv SEEK_END
282to indicate that the relative offset,
283.Fa l_start
284bytes, will be measured from the start of the file,
285current position, or end of the file, respectively.
286The value of
287.Fa l_len
288is the number of consecutive bytes to be locked.
289If
290.Fa l_len
291is negative, the result is undefined.
292The
293.Fa l_pid
294field is only used with
295.Dv F_GETLK
296to return the process ID of the process holding a blocking lock.
297After a successful
298.Dv F_GETLK
299request, the value of
300.Fa l_whence
301is
302.Dv SEEK_SET .
303.Pp
304Locks may start and extend beyond the current end of a file,
305but may not start or extend before the beginning of the file.
306A lock is set to extend to the largest possible value of the
307file offset for that file if
308.Fa l_len
309is set to zero. If
310.Fa l_whence
311and
312.Fa l_start
313point to the beginning of the file, and
314.Fa l_len
315is zero, the entire file is locked.
316If an application wishes only to do entire file locking, the
317.Xr flock 2
318system call is much more efficient.
319.Pp
320There is at most one type of lock set for each byte in the file.
321Before a successful return from an
322.Dv F_SETLK
323or an
324.Dv F_SETLKW
325request when the calling process has previously existing locks
326on bytes in the region specified by the request,
327the previous lock type for each byte in the specified
328region is replaced by the new lock type.
329As specified above under the descriptions
330of shared locks and exclusive locks, an
331.Dv F_SETLK
332or an
333.Dv F_SETLKW
334request fails or blocks respectively when another process has existing
335locks on bytes in the specified region and the type of any of those
336locks conflicts with the type specified in the request.
337.Pp
338This interface follows the completely stupid semantics of System V and
339.St -p1003.1-88
340that require that all locks associated with a file for a given process are
341removed when \fIany\fP file descriptor for that file is closed by that process.
342This semantic means that applications must be aware of any files that
343a subroutine library may access.
344For example if an application for updating the password file locks the
345password file database while making the update, and then calls
346.Xr getpwname 3
347to retrieve a record,
348the lock will be lost because
349.Xr getpwname 3
350opens, reads, and closes the password database.
351The database close will release all locks that the process has
352associated with the database, even if the library routine never
353requested a lock on the database.
354Another minor semantic problem with this interface is that
355locks are not inherited by a child process created using the
356.Xr fork 2
357function.
358The
359.Xr flock 2
360interface has much more rational last close semantics and
361allows locks to be inherited by child processes.
362.Xr Flock 2
363is recommended for applications that want to ensure the integrity
364of their locks when using library routines or wish to pass locks
365to their children.
366Note that
367.Xr flock 2
368and
369.Xr fcntl 2
370locks may be safely used concurrently.
371.Pp
372All locks associated with a file for a given process are
373removed when the process terminates.
374.Pp
375A potential for deadlock occurs if a process controlling a locked region
376is put to sleep by attempting to lock the locked region of another process.
377This implementation detects that sleeping until a locked region is unlocked
378would cause a deadlock and fails with an
379.Er EDEADLK
380error.
381.Pp
382The
383.Dv F_PREALLOCATE
384command operates on the following structure:
385.ne 7v
386.Bd -literal
387 typedef struct fstore {
388 u_int32_t fst_flags; /* IN: flags word */
389 int fst_posmode; /* IN: indicates offset field */
390 off_t fst_offset; /* IN: start of the region */
391 off_t fst_length; /* IN: size of the region */
392 off_t fst_bytesalloc; /* OUT: number of bytes allocated */
393 } fstore_t;
394.Ed
395.Pp
396The flags (fst_flags) for the
397.Dv F_PREALLOCATE
398command are as follows:
399.Bl -tag -width F_ALLOCATECONTIGX -offset indent
400.It Dv F_ALLOCATECONTIG
401Allocate contiguous space.
402.It Dv F_ALLOCATEALL
403Allocate all requested space or no space at all.
404.El
405.Pp
406The position modes (fst_posmode) for the
407.Dv F_PREALLOCATE
408command indicate how to use the offset field.
409The modes are as follows:
410.Bl -tag -width F_PEOFPOSMODEX -offset indent
411.It Dv F_PEOFPOSMODE
412Allocate from the physical end of file.
413.It Dv F_VOLPOSMODE
414Allocate from the volume offset.
415.El
416.Pp
417The
418.Dv F_RDADVISE
419command operates on the following structure
420which holds information passed from the
421user to the system:
422.ne 7v
423.Bd -literal
424 struct radvisory {
425 off_t ra_offset; /* offset into the file */
426 int ra_count; /* size of the read */
427 };
428.Ed
429.Pp
430The
431.Dv F_READBOOTSTRAP and F_WRITEBOOTSTRAP
432commands operate on the following structure.
433.ne 7v
434.Bd -literal
435 typedef struct fbootstraptransfer {
436 off_t fbt_offset; /* IN: offset to start read/write */
437 size_t fbt_length; /* IN: number of bytes to transfer */
438 void *fbt_buffer; /* IN: buffer to be read/written */
439 } fbootstraptransfer_t;
440.Ed
441.Pp
442The
443.Dv F_LOG2PHYS
444command operates on the following structure.
445.ne 7v
446.Bd -literal
447 struct log2phys {
448 u_int32_t l2p_flags; /* unused so far */
449 off_t l2p_contigbytes; /* unused so far */
450 off_t l2p_devoffset; /* bytes into device */
451 };
452.Ed
453.Sh RETURN VALUES
454Upon successful completion, the value returned depends on
455.Fa cmd
456as follows:
457.Bl -tag -width F_GETOWNX -offset indent
458.It Dv F_DUPFD
459A new file descriptor.
460.It Dv F_GETFD
461Value of flag (only the low-order bit is defined).
462.It Dv F_GETFL
463Value of flags.
464.It Dv F_GETOWN
465Value of file descriptor owner.
466.It other
467Value other than -1.
468.El
469.Pp
470Otherwise, a value of -1 is returned and
471.Va errno
472is set to indicate the error.
473.Sh ERRORS
474.Fn Fcntl
475will fail if:
476.Bl -tag -width Er
477.It Bq Er EACCES
478The argument
479.Fa cmd
480is
481.Dv F_SETLK ,
482the type of lock
483.Fa (l_type)
484is a shared lock
485.Dv (F_RDLCK)
486or exclusive lock
487.Dv (F_WRLCK) ,
488and the segment of a file to be locked is already
489exclusive-locked by another process;
490or the type is an exclusive lock and some portion of the
491segment of a file to be locked is already shared-locked or
492exclusive-locked by another process.
493.Pp
494The argument
495.Fa cmd
496is either
497.Dv F_SETSIZE
498or
499.Dv F_WRITEBOOTSTRAP
500and the calling process does not have root privileges.
501.It Bq Er EBADF
502.Fa Fildes
503is not a valid open file descriptor.
504.Pp
505The argument
506.Fa cmd
507is
508.Dv F_SETLK
509or
510.Dv F_SETLKW ,
511the type of lock
512.Fa (l_type)
513is a shared lock
514.Dv (F_RDLCK) ,
515and
516.Fa fildes
517is not a valid file descriptor open for reading.
518.Pp
519The argument
520.Fa cmd
521is
522.Dv F_SETLK
523or
524.Dv F_SETLKW ,
525the type of lock
526.Fa (l_type)
527is an exclusive lock
528.Dv (F_WRLCK) ,
529and
530.Fa fildes
531is not a valid file descriptor open for writing.
532.Pp
533The argument
534.Fa cmd
535is
536.Dv F_PREALLOCATE
537and the calling process does not have
538file write permission.
539.Pp
540The argument
541.Fa cmd
542is
543.Dv F_LOG2PHYS
544and
545.Fa fildes
546is not a valid file descriptor open for reading.
547.It Bq Er EMFILE
548.Fa Cmd
549is
550.Dv F_DUPFD
551and the maximum allowed number of file descriptors are currently
552open.
553.It Bq Er EDEADLK
554The argument
555.Fa cmd
556is
557.Dv F_SETLKW ,
558and a deadlock condition was detected.
559.It Bq Er EINTR
560The argument
561.Fa cmd
562is
563.Dv F_SETLKW ,
564and the function was interrupted by a signal.
565.It Bq Er EINVAL
566.Fa Cmd
567is
568.Dv F_DUPFD
569and
570.Fa arg
571is negative or greater than the maximum allowable number
572(see
573.Xr getdtablesize 2 ) .
574.Pp
575The argument
576.Fa cmd
577is
578.Dv F_GETLK ,
579.Dv F_SETLK ,
580or
581.Dv F_SETLKW
582and the data to which
583.Fa arg
584points is not valid, or
585.Fa fildes
586refers to a file that does not support locking.
587.Pp
588The argument
589.Fa cmd
590is
591.Dv F_PREALLOCATE
592and the
593.Fa fst_posmode
594is not a valid mode,
595or when
596.Dv F_PEOFPOSMODE
597is set and
598.Fa fst_offset
599is a non-zero value,
600or when
601.Dv F_VOLPOSMODE
602is set and
603.Fa fst_offset
604is a negative or zero value.
605.Pp
606The argument
607.Fa cmd
608is either
609.Dv F_READBOOTSTRAP
610or
611.Dv F_WRITEBOOTSTRAP
612and the operation was attempted on a non-HFS disk type.
613.It Bq Er EMFILE
614The argument
615.Fa cmd
616is
617.Dv F_DUPED
618and the maximum number of file descriptors permitted for the
619process are already in use,
620or no file descriptors greater than or equal to
621.Fa arg
622are available.
623.It Bq Er ENOLCK
624The argument
625.Fa cmd
626is
627.Dv F_SETLK
628or
629.Dv F_SETLKW ,
630and satisfying the lock or unlock request would result in the
631number of locked regions in the system exceeding a system-imposed limit.
632.It Bq Er ESRCH
633.Fa Cmd
634is
635.Dv F_SETOWN
636and
637the process ID given as argument is not in use.
638.El
639.Sh SEE ALSO
640.Xr close 2 ,
641.Xr execve 2 ,
642.Xr flock 2 ,
643.Xr getdtablesize 2 ,
644.Xr open 2 ,
645.Xr sigaction 3
646.Sh HISTORY
647The
648.Fn fcntl
649function call appeared in
650.Bx 4.2 .