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
20 argument is an untyped pointer to memory. It's traditionally char
21 *argp (from the days before void * was valid C), and will be so named
22 for this discussion.
23
24 An ioctl() request has encoded in it whether the argument is an in
25 parameter 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 appropriately.
33
35 EBADF fd is not a valid file descriptor.
36
37 EFAULT argp references an inaccessible memory area.
38
39 EINVAL request or argp is not valid.
40
41 ENOTTY fd is not associated with a character special device.
42
43 ENOTTY The specified request does not apply to the kind of object that
44 the file descriptor fd references.
45
47 No single standard. Arguments, returns, and semantics of ioctl() vary
48 according to the device driver in question (the call is used as a
49 catch-all for operations that don't cleanly fit the UNIX stream I/O
50 model).
51
52 The ioctl() system call appeared in Version 7 AT&T UNIX.
53
55 In order to use this call, one needs an open file descriptor. Often
56 the open(2) call has unwanted side effects, that can be avoided under
57 Linux by giving it the O_NONBLOCK flag.
58
59 ioctl structure
60 Ioctl command values are 32-bit constants. In principle these con‐
61 stants are completely arbitrary, but people have tried to build some
62 structure into them.
63
64 The old Linux situation was that of mostly 16-bit constants, where the
65 last byte is a serial number, and the preceding byte(s) give a type
66 indicating the driver. Sometimes the major number was used: 0x03 for
67 the HDIO_* ioctls, 0x06 for the LP* ioctls. And sometimes one or more
68 ASCII letters were used. For example, TCGETS has value 0x00005401,
69 with 0x54 = 'T' indicating the terminal driver, and CYGETTIMEOUT has
70 value 0x00435906, with 0x43 0x59 = 'C' 'Y' indicating the cyclades
71 driver.
72
73 Later (0.98p5) some more information was built into the number. One
74 has 2 direction bits (00: none, 01: write, 10: read, 11: read/write)
75 followed by 14 size bits (giving the size of the argument), followed by
76 an 8-bit type (collecting the ioctls in groups for a common purpose or
77 a common driver), and an 8-bit serial number.
78
79 The macros describing this structure live in <asm/ioctl.h> and are
80 _IO(type,nr) and {_IOR,_IOW,_IOWR}(type,nr,size). They use
81 sizeof(size) so that size is a misnomer here: this third argument is a
82 data type.
83
84 Note that the size bits are very unreliable: in lots of cases they are
85 wrong, either because of buggy macros using sizeof(sizeof(struct)), or
86 because of legacy values.
87
88 Thus, it seems that the new structure only gave disadvantages: it does
89 not help in checking, but it causes varying values for the various
90 architectures.
91
93 execve(2), fcntl(2), ioctl_console(2), ioctl_fat(2), ioctl_ficlon‐
94 erange(2), ioctl_fideduperange(2), ioctl_fslabel(2), ioctl_getfsmap(2),
95 ioctl_iflags(2), ioctl_ns(2), ioctl_tty(2), ioctl_userfaultfd(2),
96 open(2), sd(4), tty(4)
97
99 This page is part of release 5.07 of the Linux man-pages project. A
100 description of the project, information about reporting bugs, and the
101 latest version of this page, can be found at
102 https://www.kernel.org/doc/man-pages/.
103
104
105
106Linux 2020-04-11 IOCTL(2)