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 or more
69 ASCII letters were used. For example, TCGETS has value 0x00005401,
70 with 0x54 = 'T' indicating the terminal driver, and CYGETTIMEOUT has
71 value 0x00435906, with 0x43 0x59 = 'C' 'Y' indicating the cyclades
72 driver.
73
74 Later (0.98p5) some more information was built into the number. One
75 has 2 direction bits (00: none, 01: write, 10: read, 11: read/write)
76 followed by 14 size bits (giving the size of the argument), followed by
77 an 8-bit type (collecting the ioctls in groups for a common purpose or
78 a common driver), and an 8-bit serial number.
79
80 The macros describing this structure live in <asm/ioctl.h> and are
81 _IO(type,nr) and {_IOR,_IOW,_IOWR}(type,nr,size). They use
82 sizeof(size) so that size is a misnomer here: this third argument is a
83 data type.
84
85 Note that the size bits are very unreliable: in lots of cases they are
86 wrong, either because of buggy macros using sizeof(sizeof(struct)), or
87 because of legacy values.
88
89 Thus, it seems that the new structure only gave disadvantages: it does
90 not help in checking, but it causes varying values for the various ar‐
91 chitectures.
92
94 execve(2), fcntl(2), ioctl_console(2), ioctl_fat(2), ioctl_ficlon‐
95 erange(2), ioctl_fideduperange(2), ioctl_fslabel(2), ioctl_getfsmap(2),
96 ioctl_iflags(2), ioctl_ns(2), ioctl_tty(2), ioctl_userfaultfd(2),
97 open(2), sd(4), tty(4)
98
100 This page is part of release 5.12 of the Linux man-pages project. A
101 description of the project, information about reporting bugs, and the
102 latest version of this page, can be found at
103 https://www.kernel.org/doc/man-pages/.
104
105
106
107Linux 2021-03-22 IOCTL(2)