xref: /openbsd/lib/libc/sys/ioctl.2 (revision d415bd75)
1.\"	$OpenBSD: ioctl.2,v 1.20 2022/09/11 06:38:11 jmc Exp $
2.\"	$NetBSD: ioctl.2,v 1.5 1995/02/27 12:33:47 cgd Exp $
3.\"
4.\" Copyright (c) 1980, 1991, 1993
5.\"	The Regents of the University of California.  All rights reserved.
6.\"
7.\" Redistribution and use in source and binary forms, with or without
8.\" modification, are permitted provided that the following conditions
9.\" are met:
10.\" 1. Redistributions of source code must retain the above copyright
11.\"    notice, this list of conditions and the following disclaimer.
12.\" 2. Redistributions in binary form must reproduce the above copyright
13.\"    notice, this list of conditions and the following disclaimer in the
14.\"    documentation and/or other materials provided with the distribution.
15.\" 3. Neither the name of the University nor the names of its contributors
16.\"    may be used to endorse or promote products derived from this software
17.\"    without specific prior written permission.
18.\"
19.\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
20.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22.\" ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
23.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29.\" SUCH DAMAGE.
30.\"
31.\"     @(#)ioctl.2	8.2 (Berkeley) 12/11/93
32.\"
33.Dd $Mdocdate: September 11 2022 $
34.Dt IOCTL 2
35.Os
36.Sh NAME
37.Nm ioctl
38.Nd control device
39.Sh SYNOPSIS
40.In sys/ioctl.h
41.Ft int
42.Fn ioctl "int d" "unsigned long request" "..."
43.Sh DESCRIPTION
44The
45.Fn ioctl
46function manipulates the underlying device parameters of special files.
47In particular, many operating
48characteristics of character special files (e.g., terminals)
49may be controlled with
50.Fn ioctl
51requests.
52.Pp
53The argument
54.Fa d
55must be an open file descriptor.
56The third argument is called
57.Fa arg
58and contains additional information needed by this device
59to perform the requested function.
60.Fa arg
61is either an
62.Vt int
63or a pointer to a device-specific data structure, depending upon
64the given
65.Fa request .
66.Pp
67An
68.Nm
69.Fa request
70has encoded in it whether the argument is an
71.Dq in
72parameter
73or
74.Dq out
75parameter, and the size of the third argument
76.Pq Fa arg
77in bytes.
78Macros and defines used in specifying an ioctl
79.Fa request
80are located in the file
81.In sys/ioctl.h .
82.Sh GENERIC IOCTLS
83Some ioctls are applicable to any file descriptor.
84These include:
85.Bl -tag -width "xxxxxx"
86.It Dv FIOCLEX
87Set close-on-exec flag.
88The file will be closed when
89.Xr execve 2
90is invoked.
91.It Dv FIONCLEX
92Clear close-on-exec flag.
93The file will remain open across
94.Xr execve 2 .
95.El
96.Pp
97Some generic ioctls are not implemented for all types of file
98descriptors.
99These include:
100.Bl -tag -width "xxxxxx"
101.It Dv FIONREAD Fa "int *"
102Get the number of bytes that are immediately available for reading.
103.It Dv FIONBIO Fa "int *"
104Set non-blocking I/O mode if the argument is non-zero.
105In non-blocking mode,
106.Xr read 2
107or
108.Xr write 2
109calls return \-1 and set
110.Va errno
111to
112.Er EAGAIN
113immediately when no data is available.
114.It Dv FIOASYNC Fa "int *"
115Set asynchronous I/O mode if the argument is non-zero.
116In asynchronous mode, the process or process group specified by
117.Dv FIOSETOWN
118will start receiving
119.Dv SIGIO
120signals when data is available.
121The
122.Dv SIGIO
123signal will be delivered when data is available on the file
124descriptor.
125.It Dv FIOSETOWN, FIOGETOWN Fa "int *"
126Set/get the process or the process group (if negative) that should receive
127.Dv SIGIO
128signals when data is available.
129.El
130.Sh RETURN VALUES
131If an error has occurred, a value of \-1 is returned and
132.Va errno
133is set to indicate the error.
134.Sh ERRORS
135.Fn ioctl
136will fail if:
137.Bl -tag -width Er
138.It Bq Er EBADF
139.Fa d
140is not a valid descriptor.
141.It Bq Er ENOTTY
142.Fa d
143is not associated with a character
144special device.
145.It Bq Er ENOTTY
146The specified request does not apply to the kind
147of object that the descriptor
148.Fa d
149references.
150.It Bq Er EINVAL
151.Fa request
152or
153.Fa arg
154is not valid.
155.It Bq Er EFAULT
156.Fa arg
157points outside the process's allocated address space.
158.El
159.Sh SEE ALSO
160.Xr cdio 1 ,
161.Xr chio 1 ,
162.Xr mt 1 ,
163.Xr execve 2 ,
164.Xr fcntl 2 ,
165.Xr intro 4 ,
166.Xr tty 4
167.Sh HISTORY
168An
169.Fn ioctl
170function call appeared in
171.At v7 .
172