Home > Articles > Operating Systems, Server > Linux/UNIX/Open Source

  • Print
  • + Share This
This chapter is from the book

This chapter is from the book

3.14 ioctl Function

The ioctl function has always been the catchall for I/O operations. Anything that couldn't be expressed using one of the other functions in this chapter usually ended up being specified with an ioctl. Terminal I/O was the biggest user of this function. (When we get to Chapter 11 we'll see that POSIX.1 has replaced the terminal I/O operations with new functions.)

    #include <unistd.h>     
/* SVR4 */ #include <sys/ioctl.h>
/* 4.3+BSD */ int ioctl(int filedes, int
request, ...); Returns: -1 on error, something else if OK

The ioctl function is not part of POSIX.1. Both SVR4 and 4.3+BSD, however, use it for many miscellaneous device operations.

The prototype that we show corresponds to SVR4. 4.3+BSD and earlier Berkeley systems declare the second argument as an unsigned long. This detail doesn't matter, since the second argument is always a #defined name from a header.

For the ANSI C prototype an ellipsis is used for the remaining arguments. Normally, however, there is just one more argument, and it's usually a pointer to a variable or a structure.

In this prototype we show only the headers required for the function itself. Normally additional device-specific headers are required. For example, the ioctls for terminal I/O, beyond the basic operations specified by POSIX.1, all require the <termios.h> header.

What are ioctls used for today? We can divide the 4.3+BSD operations into the categories shown in Figure 3.7.

03fig07.gifFigure 3.7. 4.3+BSD ioctl operations.

The mag tape operations allow us to write end-of-file marks on a tape, rewind a tape, space forward over a specified number of files or records, and the like. None of these operations is easily expressed in terms of the other functions in the chapter (read, write, lseek, etc.) so the easiest way to handle these devices has always been to access their operations using ioctl.

We use the ioctl function in Section 11.12 to fetch and set the size of a terminal's window, in Section 12.4 when we describe the streams system, and in Section 19.7 when we access the advanced features of pseudo terminals.

  • + Share This
  • 🔖 Save To Your Account