|
Device control: ioctl
The C function ´ioctl' (I/O
control) is used to send special control commands to devices like the disk and
the network interface. The syntax of the function is
int ioctl(fd, request,
arg)
int fd, request;
long arg;
The first parameter is normally as device handle or socket descriptor. The
second is a control parameter. Lists of valid control parameters are normally
defined in the system ´include' files for a particular device. They are
device and system dependent so you need a local manual and som detective work to
find out what they are. The final parameter is a pointer to a variable which
receives return data from the device.
´ioctl' commands are device
specific, by their nature. The commands for the ethernet interface device are
only partially standardized, for example. We could read the ethernet device
(which is called ´le0' on a Sun workstation), using the following command:
# include <sys/socket.h> /* Typical
includes for internet */
# include
<sys/ioctl.h>
# include
<net/if.h>
# include
<netinet/in.h>
# include
<arpa/inet.h>
# include
<netdb.h>
# include
<sys/protosw.h>
# include
<net/route.h>
struct ifreq IFR;
int sk;
struct sockaddr_in sin;
strcpy(IFR.ifr_name,"le0");
IFR.ifr_addr.sa_family =
AF_INET;
if ((sk =
socket(AF_INET,SOCK_DGRAM,IPPROTO_IP)) == -1)
{
perror("socket");
exit(1);
}
if (ioctl(sk,SIOCGIFFLAGS, (caddr_t) &IFR)
== -1)
{
perror ("ioctl");
exit(1);
}
We shall not go into the further details of
´ioctl', but simply note its role
in system programming.
|