The ioctl() system call is a general-purpose mechanism for performing file and device operations that fall outside the universal I/O model described earlier in this chapter.
#include <sys/ioctl.h>
int ioctl
(int fd, int request, ... /* argp */);
Value returned on success depends on request, or -1 on error
The fd argument is an open file descriptor for the device or file upon which the control operation specified by request is to be performed. Device-specific header files define constants that can be passed in the request argument.
As indicated by the standard C ellipsis (...
) notation, the third argument to ioctl(), which we label argp, can be of any type. The value of the request argument enables ioctl() to determine what type of value to expect in argp. Typically, argp is a pointer to either an integer or a structure; in some cases, it is unused.
We’ll see a number of uses for ioctl() in later chapters (see, for example, I-node Flags (ext2 Extended File Attributes)).
The only specification that SUSv3 makes for ioctl() is for operations to control STREAMS devices. (The STREAMS facility is a System V feature that is not supported by the mainline Linux kernel, although a few add-on implementations have been developed.) None of the other ioctl() operations described in this book is specified in SUSv3. However, the ioctl() call has been part of the UNIX system since early versions, and consequently several of the ioctl() operations that we describe are provided on many other UNIX implementations. As we describe each ioctl() operation, we note portability issues.