1IOCTL(2) Linux Programmer's Manual IOCTL(2)
2
3
4
6 ioctl - control device
7
9 #include <sys/ioctl.h>
10
11 int ioctl(int fd, unsigned long request, ...);
12
14 The ioctl() system call manipulates the underlying device parameters of
15 special files. In particular, many operating characteristics of char‐
16 acter special files (e.g., terminals) may be controlled with ioctl()
17 requests. The argument fd must be an open file descriptor.
18
19 The second argument is a device-dependent request code. The third ar‐
20 gument is an untyped pointer to memory. It's traditionally char *argp
21 (from the days before void * was valid C), and will be so named for
22 this discussion.
23
24 An ioctl() request has encoded in it whether the argument is an in pa‐
25 rameter or out parameter, and the size of the argument argp in bytes.
26 Macros and defines used in specifying an ioctl() request are located in
27 the file <sys/ioctl.h>. See NOTES.
28
30 Usually, on success zero is returned. A few ioctl() requests use the
31 return value as an output parameter and return a nonnegative value on
32 success. On error, -1 is returned, and errno is set to indicate the
33 error.
34
36 EBADF fd is not a valid file descriptor.
37
38 EFAULT argp references an inaccessible memory area.
39
40 EINVAL request or argp is not valid.
41
42 ENOTTY fd is not associated with a character special device.
43
44 ENOTTY The specified request does not apply to the kind of object that
45 the file descriptor fd references.
46
48 No single standard. Arguments, returns, and semantics of ioctl() vary
49 according to the device driver in question (the call is used as a
50 catch-all for operations that don't cleanly fit the UNIX stream I/O
51 model).
52
53 The ioctl() system call appeared in Version 7 AT&T UNIX.
54
56 In order to use this call, one needs an open file descriptor. Often
57 the open(2) call has unwanted side effects, that can be avoided under
58 Linux by giving it the O_NONBLOCK flag.
59
60 ioctl structure
61 Ioctl command values are 32-bit constants. In principle these con‐
62 stants are completely arbitrary, but people have tried to build some
63 structure into them.
64
65 The old Linux situation was that of mostly 16-bit constants, where the
66 last byte is a serial number, and the preceding byte(s) give a type in‐
67 dicating the driver. Sometimes the major number was used: 0x03 for the
68 HDIO_* ioctls, 0x06 for the LP* ioctls. And sometimes one