update from Mike Karels at BSDI
[unix-history] / usr / src / lib / libc / sys / fcntl.2
CommitLineData
88b3ccf2
KB
1.\" Copyright (c) 1983 The Regents of the University of California.
2.\" All rights reserved.
c24fefdc 3.\"
91cff1e1 4.\" %sccs.include.redist.man%
88b3ccf2 5.\"
3a2b8980 6.\" @(#)fcntl.2 6.10 (Berkeley) %G%
c24fefdc 7.\"
931b8415
CL
8.Dd
9.Dt FCNTL 2
10.Os BSD 4.2
11.Sh NAME
12.Nm fcntl
13.Nd file control
14.Sh SYNOPSIS
ac6404fc 15.Fd #include <fcntl.h>
931b8415
CL
16.Ft int
17.Fn fcntl "int fd" "int cmd" "int arg"
18.Sh DESCRIPTION
19.Fn Fcntl
c24fefdc
KM
20provides for control over descriptors.
21The argument
931b8415 22.Fa fd
c24fefdc 23is a descriptor to be operated on by
931b8415 24.Fa cmd
c24fefdc 25as follows:
931b8415
CL
26.Bl -tag -width F_GETOWNX
27.It Dv F_DUPFD
c24fefdc 28Return a new descriptor as follows:
931b8415
CL
29.Pp
30.Bl -bullet -compact -offset 4n
31.It
c24fefdc 32Lowest numbered available descriptor greater than or equal to
931b8415
CL
33.Fa arg .
34.It
c24fefdc 35Same object references as the original descriptor.
931b8415
CL
36.It
37New descriptor shares the same file offset if the object
c24fefdc 38was a file.
931b8415 39.It
c24fefdc 40Same access mode (read, write or read/write).
931b8415 41.It
c24fefdc
KM
42Same file status flags (i.e., both file descriptors
43share the same file status flags).
931b8415 44.It
c24fefdc
KM
45The close-on-exec flag associated with the new file descriptor
46is set to remain open across
931b8415 47.Xr execv 2
c24fefdc 48system calls.
931b8415
CL
49.El
50.It Dv F_GETFD
c24fefdc 51Get the close-on-exec flag associated with the file descriptor
931b8415
CL
52.Fa fd .
53If the low-order bit of the returned value is 0,
54the file will remain open across
55.Fn exec ,
c24fefdc 56otherwise the file will be closed upon execution of
931b8415
CL
57.Fn exec
58.Fa ( arg
59is ignored).
60.It Dv F_SETFD
c24fefdc 61Set the close-on-exec flag associated with
931b8415 62.Fa fd
c24fefdc 63to the low order bit of
931b8415 64.Fa arg
c24fefdc 65(0 or 1 as above).
931b8415
CL
66.It Dv F_GETFL
67Get descriptor status flags, as described below
68.Fa ( arg
69is ignored).
70.It Dv F_SETFL
71Set descriptor status flags to
72.Fa arg .
73.It Dv F_GETOWN
c24fefdc 74Get the process ID or process group
931b8415
CL
75currently receiving
76.Dv SIGIO
77and
78.Dv SIGURG
c24fefdc 79signals; process groups are returned
931b8415
CL
80as negative values
81.Fa ( arg
82is ignored).
83.It Dv F_SETOWN
c24fefdc 84Set the process or process group
931b8415
CL
85to receive
86.Dv SIGIO
87and
88.Dv SIGURG
89signals;
c24fefdc 90process groups are specified by supplying
931b8415 91.Fa arg
c24fefdc 92as negative, otherwise
931b8415 93.Fa arg
c24fefdc 94is interpreted as a process ID.
931b8415
CL
95.El
96.Pp
97The flags for the
98.Dv F_GETFL
99and
100.Dv F_SETFL
101flags are as follows:
102.Bl -tag -width F_GETOWNX
d9927876 103.It Dv O_NONBLOCK
c24fefdc 104Non-blocking I/O; if no data is available to a
931b8415
CL
105.Xr read
106call, or if a
107.Xr write
108operation would block,
109the read or write call returns -1 with the error
3a2b8980 110.Er EAGAIN .
931b8415 111.It Dv O_APPEND
c24fefdc 112Force each write to append at the end of file;
931b8415
CL
113corresponds to the
114.Dv O_APPEND
115flag of
116.Xr open 2 .
117.It Dv O_ASYNC
118Enable the
119.Dv SIGIO
120signal to be sent to the process group
d4bad45b 121when I/O is possible, e.g.,
c24fefdc 122upon availability of data to be read.
931b8415
CL
123.El
124.Sh RETURN VALUES
c24fefdc 125Upon successful completion, the value returned depends on
931b8415 126.Fa cmd
c24fefdc 127as follows:
931b8415
CL
128.Bl -tag -width F_GETOWNX -offset indent
129.It Dv F_DUPFD
130A new file descriptor.
131.It Dv F_GETFD
132Value of flag (only the low-order bit is defined).
133.It Dv F_GETFL
134Value of flags.
135.It Dv F_GETOWN
136Value of file descriptor owner.
137.It other
138Value other than -1.
139.El
140.Pp
141Otherwise, a value of -1 is returned and
142.Va errno
c24fefdc 143is set to indicate the error.
931b8415
CL
144.Sh ERRORS
145.Fn Fcntl
146will fail if:
147.Bl -tag -width Er
148.It Bq Er EBADF
149.Fa Fildes
c24fefdc 150is not a valid open file descriptor.
931b8415
CL
151.It Bq Er EMFILE
152.Fa Cmd
153is
154.Dv F_DUPFD
155and the maximum allowed number of file descriptors are currently
c24fefdc 156open.
931b8415
CL
157.It Bq Er EINVAL
158.Fa Cmd
159is
160.Dv F_DUPFD
161and
162.Fa arg
d4bad45b 163is negative or greater than the maximum allowable number
c24fefdc 164(see
931b8415
CL
165.Xr getdtablesize 2 ) .
166.It Bq Er ESRCH
167.Fa Cmd
168is
169.Dv F_SETOWN
170and
cae3bf24 171the process ID given as argument is not in use.
931b8415
CL
172.El
173.Sh SEE ALSO
174.Xr close 2 ,
175.Xr execve 2 ,
176.Xr getdtablesize 2 ,
177.Xr open 2 ,
178.Xr sigvec 2
179.Sh BUGS
180The asynchronous I/O facilities of
181.Dv FNDELAY
182and
183.Dv FASYNC
1df601cf 184are currently available only for tty and socket operations.
931b8415
CL
185.Sh HISTORY
186The
187.Nm
188function call appeared in
189.Bx 4.2 .