### 13.17 Generic I/O Control operations

GNU systems can handle most input/output operations on many different devices and objects in terms of a few file primitives - `read`, `write` and `lseek`. However, most devices also have a few peculiar operations which do not fit into this model. Such as:

- Changing the character font used on a terminal.
- Telling a magnetic tape system to rewind or fast forward. (Since they cannot move in byte increments, `lseek` is inapplicable).
- Ejecting a disk from a drive.
- Playing an audio track from a CD-ROM drive.
- Maintaining routing tables for a network.

Although some such objects such as sockets and terminals 1 have special functions of their own, it would not be practical to create functions for all these cases.

Instead these minor operations, known as IOCTLs, are assigned code numbers and multiplexed through the `ioctl` function, defined in `sys/ioctl.h`. The code numbers themselves are defined in many different headers.

— Function: int ioctl (int filedes, int command, ...)

> Preliminary: | MT-Safe | AS-Safe | AC-Safe | See POSIX Safety Concepts.

> The `ioctl` function performs the generic I/O operation command on filedes.

> A third argument is usually present, either a single number or a pointer to a structure. The meaning of this argument, the returned value, and any error codes depends upon the command used. Often -1 is returned for a failure.

On some systems, IOCTLs used by different devices share the same numbers. Thus, although use of an inappropriate IOCTL usually only produces an error, you should not attempt to use device-specific IOCTLs on an unknown device.

Most IOCTLs are OS-specific and/or only used in special system utilities, and are thus beyond the scope of this document. For an example of the use of an IOCTL, see Out-of-Band Data.

#### Footnotes

[1] Actually, the terminal-specific functions are implemented with IOCTLs on many platforms.