linux 0.11 内核学习 -- ioctl.c

/*

 * ioctl.c 文件实现了输入/输出控制系统调用ioctl(),该函数

 * 主要是调用函数tty_ioctl()对终端的IO进行控制

 */

/*

 *  linux/fs/ioctl.c

 *

 *  (C) 1991  Linus Torvalds

 */

#include <string.h>

#include <errno.h>

#include <sys/stat.h>

#include <linux/sched.h>

extern int tty_ioctl(int dev, int cmd, int arg);

typedef int (*ioctl_ptr)(int dev,int cmd,int arg);

/* 定义系统中设备种数 */

#define NRDEVS ((sizeof (ioctl_table))/(sizeof (ioctl_ptr)))

/* ioctl 操作函数指针表 */

static ioctl_ptr ioctl_table[]={

NULL, /* nodev */

NULL, /* /dev/mem */

NULL, /* /dev/fd */

NULL, /* /dev/hd */

tty_ioctl, /* /dev/ttyx */

tty_ioctl, /* /dev/tty */

NULL, /* /dev/lp */

NULL}; /* named pipes */

/* 输入输出控制函数 */

int sys_ioctl(unsigned int fd, unsigned int cmd, unsigned long arg)

{

struct file * filp;

int dev,mode;

// 如果文件描述符超出可打开的文件数,或者对应描述符的文件结构指针为空,则返回出错码

if (fd >= NR_OPEN || !(filp = current->filp[fd]))

return -EBADF;

// 取对应文件的属性

mode=filp->f_inode->i_mode;

// 如果该文件不是字符文件,也不是块设备文件,则返回出错码

if (!S_ISCHR(mode) && !S_ISBLK(mode))

return -EINVAL;

// 从字符或块设备文件的i 节点中取设备号。如果设备号大于系统现有的设备数,则返回出错号

dev = filp->f_inode->i_zone[0];

if (MAJOR(dev) >= NRDEVS)

return -ENODEV;

// 如果该设备在ioctl 函数指针表中没有对应函数

if (!ioctl_table[MAJOR(dev)])

return -ENOTTY;

// 否则返回实际ioctl 函数返回码

return ioctl_table[MAJOR(dev)](dev,cmd,arg);

}

参考《linux内核完全注释》和网上相关文章

posted @ 2010-02-16 09:17  qiang.xu  阅读(999)  评论(0编辑  收藏  举报