1.llseek
llseek方法实现了lseek和llseek的系统调用,如果设备操作为定义llseek方法,内核默认通过修改filp->f_pos而执行定位,filp->f_pos是文件的当前读取/写入位置,为了使lseek系统调用能正确工作,read和write方法必须通过更新他们收到的偏移量参数来配合。
Scrull的驱动例子
- loff_t scull_llseek(struct file *filp, loff_t off, int whence)
-
{
-
struct scull_dev *dev = filp->private_data;
-
loff_t newpos;
-
-
switch(whence) {
-
case 0: /* SEEK_SET */
-
newpos = off;
-
break;
-
-
case 1: /* SEEK_CUR */
-
newpos = filp->f_pos + off;
-
break;
-
-
case 2: /* SEEK_END */
-
newpos = dev->size + off;
-
break;
-
-
default: /* can't happen */
-
return -EINVAL;
-
}
-
if (newpos < 0) return -EINVAL;
-
filp->f_pos = newpos;
-
return newpos;
-
}
大多数设备只提供了数据流(比如串口和键盘),而不是数据区,定义这些设备没有意义,在这中情况下,不能简单的不声明llseek操作,因为默认方法是允许定位的,相反,应该在open方法中调用nonseekable_open,以便通知内核设备不支持llseek,
int nonseekable_open(struct inode *inode, struct file *filp)
该函数会吧给定的filp标记为不可定位;这样内核就不会让这种文件上的lseek调用成功。通过这种方式标记文件,还可以确保通过pread和pwrite系统调用也不能定位文件。
为了完整期间,我们还应该将file_operations结构中的llseek方法设备为特殊的辅助函数no_llseek(定义在)
2.设备文件的访问控制
提供访问控制对于一个设备节点来的可靠性来说有时是至关重要的。这部分的内容只是在open和release方法上做些修改,增加一些检查机制既可。
(1)独享设备
最生硬的访问控制方法是一次只允许一个进程打开设备(独享),最好避免使用这种技术,因为它制约了用户的灵活性。scullsingle设备维护一个atomic_t变量,称为scull_s_available,该变量初值为1,表明设备真正可用,open调用会减小并测试scull_s_available,并在其他进程已经打开该设备拒绝访问
- static int scull_s_open(struct inode *inode, struct file *filp)
-
{
-
struct scull_dev *dev; /* device information */
-
dev = container_of(inode->i_cdev, struct scull_dev, cdev);
-
-
if (! atomic_dec_and_test (&scull_s_available)) {
-
atomic_inc(&scull_s_available);
-
return -EBUSY; /* already open */
-
}
-
-
/* then, everything else is copied from the bare scull device */
-
if ( ((filp->f_flags & O_ACCMODE) && O_WRONLY))
-
scull_trim(dev);
-
filp->private_data = dev;
-
return 0; /* success */
-
}
release则标记设备为不忙
- static int scull_s_release(struct inode *inode, struct file *filp)
-
{
- atomic_inc(&scull_s_available); /* release the device */
- return 0;
-
}
建议吧打开标志scull_s_available放在私有设备结构里
(2)单用户访问
open调用在第一次打开授权,但它记录下设备的属主,这意味着一个用户可以多次打开设备,晕血多个进程并发的在设备上操作。其他用户不能打开这个设备,这样就避免了外部干扰。
- spin_lock(&scull_u_lock);
-
if (scull_u_count &&
-
(scull_u_owner != current->uid) && /* allow user */
-
(scull_u_owner != current->euid) && /* allow whoever did su */
-
!capable(CAP_DAC_OVERRIDE)) { /* still allow root */
-
spin_unlock(&scull_u_lock);
-
return -EBUSY; /* -EPERM would confuse the user */
-
}
-
-
if (scull_u_count == 0)
-
scull_u_owner = current->uid; /* grab it */
-
-
scull_u_count++;
-
spin_unlock(&scull_u_lock);
相应的释放函数
- static int scull_u_release(struct inode *inode, struct file *filp)
-
{
-
spin_lock(&scull_u_lock);
-
scull_u_count--; /* nothing else */
-
spin_unlock(&scull_u_lock);
-
return 0;
-
}
(3)替代EBUSY的阻塞型open
当设备不能访问时返回一个错误,通常这是最合理的方式,但有些情况下可能需要让进程等待设备。
代理EBUSY的一个方法是实现阻塞型open。
- static int scull_w_open(struct inode *inode, struct file *filp)
-
{
-
struct scull_dev *dev = &scull_w_device; /* device information */
-
-
spin_lock(&scull_w_lock);
-
while (! scull_w_available()) {
-
spin_unlock(&scull_w_lock);
-
if (filp->f_flags & O_NONBLOCK) return -EAGAIN;
-
if (wait_event_interruptible (scull_w_wait, scull_w_available()))
-
return -ERESTARTSYS; /* tell the fs layer to handle it */
-
spin_lock(&scull_w_lock);
-
}
-
if (scull_w_count == 0)
-
scull_w_owner = current->uid; /* grab it */
-
scull_w_count++;
-
spin_unlock(&scull_w_lock);
-
-
/* then, everything else is copied from the bare scull device */
-
if ((filp->f_flags & O_ACCMODE) == O_WRONLY)
-
scull_trim(dev);
-
filp->private_data = dev;
-
return 0; /* success */
-
}
release方法唤醒所有等待的进程
- static int scull_w_release(struct inode *inode, struct file *filp)
-
{
-
int temp;
-
-
spin_lock(&scull_w_lock);
-
scull_w_count--;
-
temp = scull_w_count;
-
spin_unlock(&scull_w_lock);
-
-
if (temp == 0)
-
wake_up_interruptible_sync(&scull_w_wait); /* awake other uid's */
-
return 0;
-
}
这类问题(对同一设备的不同的,不兼容的策略)最好通过为每一种访问策略实现一个设备节点的方法来解决。
(4)在打开时复制设备
另一个实现访问控制的方法是,在进程打开设备时创建设备的不同私有副本。
显然这种方法只有在设备没有绑定到某个硬件对象时才能实现,/dev/tty内部也使用了类似的技术,以提供给它的进程一个不同于/dev入口点所表现出的“情景”,如果复制的设备是由软件驱动程序创建,我们称它们为“虚拟设备”--就像所有的虚拟终端都是用同一个物理终端设备一样。
虽然这种访问控制并不常见,但它的实现展示了内核代码可以轻松的改变应用程序看到的外部环境。
- /************************************************************************
-
*
-
* Finally the `cloned' private device. This is trickier because it
-
* involves list management, and dynamic allocation.
-
*/
-
-
/* The clone-specific data structure includes a key field */
-
-
struct scull_listitem {
-
struct scull_dev device;
-
dev_t key;
-
struct list_head list;
-
-
};
-
-
/* The list of devices, and a lock to protect it */
-
static LIST_HEAD(scull_c_list);
-
static spinlock_t scull_c_lock = SPIN_LOCK_UNLOCKED;
-
-
/* A placeholder scull_dev which really just holds the cdev stuff. */
-
static struct scull_dev scull_c_device;
-
-
/* Look for a device or create one if missing */
-
static struct scull_dev *scull_c_lookfor_device(dev_t key)
-
{
-
struct scull_listitem *lptr;
-
-
list_for_each_entry(lptr, &scull_c_list, list) {
-
if (lptr->key == key)
-
return &(lptr->device);
-
}
-
-
/* not found */
-
lptr = kmalloc(sizeof(struct scull_listitem), GFP_KERNEL);
-
if (!lptr)
-
return NULL;
-
-
/* initialize the device */
-
memset(lptr, 0, sizeof(struct scull_listitem));
-
lptr->key = key;
-
scull_trim(&(lptr->device)); /* initialize it */
-
init_MUTEX(&(lptr->device.sem));
-
-
/* place it in the list */
-
list_add(&lptr->list, &scull_c_list);
-
-
return &(lptr->device);
-
}
-
-
static int scull_c_open(struct inode *inode, struct file *filp)
-
{
-
struct scull_dev *dev;
-
dev_t key;
-
-
if (!current->signal->tty) {
-
PDEBUG("Process \"%s\" has no ctl tty\n", current->comm);
-
return -EINVAL;
-
}
-
key = tty_devnum(current->signal->tty);
-
-
/* look for a scullc device in the list */
-
spin_lock(&scull_c_lock);
-
dev = scull_c_lookfor_device(key);
-
spin_unlock(&scull_c_lock);
-
-
if (!dev)
-
return -ENOMEM;
-
-
/* then, everything else is copied from the bare scull device */
-
if ( (filp->f_flags & O_ACCMODE) == O_WRONLY)
-
scull_trim(dev);
-
filp->private_data = dev;
-
return 0; /* success */
-
}
-
-
static int scull_c_release(struct inode *inode, struct file *filp)
-
{
-
/*
-
* Nothing to do, because the device is persistent.
-
* A `real' cloned device should be freed on last close
-
*/
-
return 0;
-
}
至此,Linux高级字符驱动程序操作已经学习完毕,(其实这部分已经看过几遍,后续还要继续看,有些没理解透)这部分笔记现在才整理,一方面是因为难度,一方面是实际工作用的不是很多,没有专门花时间来整理。anyway,到目前为止,Linux字符驱动所有基础知识笔记整理完毕,这是个辛苦的过程,但收获很大。再次的,特别感谢Tekkaman Ninja大侠,LDD3笔记整理过程中参考了他的大部分,给了我很大帮助(若涉及版权相关,全都归他)。
阅读(587) | 评论(0) | 转发(0) |