1.\" $NetBSD: ioctl.2,v 1.21 2010/05/11 16:17:18 mbalmer Exp $ 2.\" 3.\" Copyright (c) 1980, 1991, 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. Neither the name of the University nor the names of its contributors 15.\" may be used to endorse or promote products derived from this software 16.\" without specific prior written permission. 17.\" 18.\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 19.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 20.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 21.\" ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 22.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 23.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 24.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 25.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 26.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 27.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 28.\" SUCH DAMAGE. 29.\" 30.\" @(#)ioctl.2 8.2 (Berkeley) 12/11/93 31.\" 32.Dd May 11, 2010 33.Dt IOCTL 2 34.Os 35.Sh NAME 36.Nm ioctl 37.Nd control device 38.Sh LIBRARY 39.Lb libc 40.Sh SYNOPSIS 41.In sys/ioctl.h 42.Ft int 43.Fn ioctl "int d" "unsigned long request" "void *argp" 44.Sh DESCRIPTION 45The 46.Fn ioctl 47function manipulates the underlying device parameters of special files. 48In particular, many operating 49characteristics of character special files (e.g. terminals) 50may be controlled with 51.Fn ioctl 52requests. 53The argument 54.Fa d 55must be an open file descriptor. 56.Pp 57An ioctl 58.Fa request 59has encoded in it whether the argument is an 60.Dq in 61parameter 62or 63.Dq out 64parameter, and the size of the argument 65.Fa argp 66in bytes. 67Macros and defines used in specifying an ioctl 68.Fa request 69are located in the file 70.Ao Pa sys/ioctl.h Ac . 71.Sh GENERIC IOCTLS 72Some ioctls are applicable to any file descriptor. 73These include: 74.Bl -tag -width "xxxxxx" 75.It Dv FIOCLEX 76Set close-on-exec flag. 77The file will be closed when 78.Xr exec 3 79is invoked. 80.It Dv FIONCLEX 81Clear close-on-exec flag. 82The file will remain open across 83.Xr exec 3 . 84.El 85.Pp 86Some generic ioctls are not implemented for all types of file 87descriptors. 88These include: 89.Bl -tag -width "xxxxxx" 90.It Dv FIONREAD "int" 91Get the number of bytes that are immediately available for reading. 92.It Dv FIONWRITE "int" 93Get the number of bytes in the descriptor's send queue. 94These bytes are data which has been written to the descriptor but 95which are being held by the kernel for further processing. 96The nature of the required processing depends on the underlying device. 97For tty devices, these bytes are typically queued for delivery 98to the tty hardware. 99For TCP sockets, these bytes have not yet been acknowledged by the 100other side of the connection. 101For files, this operation always returns zero as files do not have 102send queues. 103.It Dv FIONSPACE "int" 104Get the free space in the descriptor's send queue. 105This value is the size of the send queue minus the number of bytes 106being held in the queue. 107Note: while this value represents the number of bytes that may be 108added to the queue, other resource limitations may cause a write 109not larger than the send queue's space to be blocked. 110One such limitation would be a lack of network buffers for a write 111to a network connection. 112.It Dv FIONBIO "int" 113Set non-blocking I/O mode if the argument is non-zero. 114In non-blocking mode, 115.Xr read 2 116or 117.Xr write 2 118calls return \-1 and set 119.Va errno 120to 121.Er EAGAIN 122immediately when no data is available. 123.It Dv FIOASYNC "int" 124Set asynchronous I/O mode if the argument is non-zero. 125In asynchronous mode, the process or process group specified by 126.Dv FIOSETOWN 127will start receiving 128.Dv SIGIO 129signals when data is available. 130The 131.Dv SIGIO 132signal will be delivered when data is available on the file 133descriptor. 134.It Dv FIOSETOWN, FIOGETOWN "int" 135Set/get the process or the process group (if negative) that should receive 136.Dv SIGIO 137signals when data is available. 138.El 139.Sh RETURN VALUES 140If an error has occurred, a value of \-1 is returned and 141.Va errno 142is set to indicate the error. 143.Sh ERRORS 144.Fn ioctl 145will fail if: 146.Bl -tag -width Er 147.It Bq Er EBADF 148.Fa d 149is not a valid descriptor. 150.It Bq Er ENOTTY 151.Fa d 152is not associated with a character 153special device. 154.It Bq Er ENOTTY 155The specified request does not apply to the kind 156of object that the descriptor 157.Fa d 158references. 159.It Bq Er EINVAL 160.Fa request 161or 162.Fa argp 163is not valid. 164.It Bq Er EFAULT 165.Fa argp 166points outside the process's allocated address space. 167.El 168.Sh SEE ALSO 169.Xr mt 1 , 170.Xr execve 2 , 171.Xr fcntl 2 , 172.Xr intro 4 , 173.Xr tty 4 174.Sh HISTORY 175An 176.Fn ioctl 177function call appeared in 178.At v7 . 179