Chinaunix首页 | 论坛 | 博客
  • 博客访问: 9181817
  • 博文数量: 1728
  • 博客积分: 12961
  • 博客等级: 上将
  • 技术积分: 19870
  • 用 户 组: 普通用户
  • 注册时间: 2009-01-09 11:25
个人简介

偷得浮生半桶水(半日闲), 好记性不如抄下来(烂笔头). 信息爆炸的时代, 学习是一项持续的工作.

文章分类

全部博文(1728)

文章存档

2024年(4)

2023年(26)

2022年(112)

2021年(217)

2020年(157)

2019年(192)

2018年(81)

2017年(78)

2016年(70)

2015年(52)

2014年(40)

2013年(51)

2012年(85)

2011年(45)

2010年(231)

2009年(287)

分类: LINUX

2010-11-23 14:44:33

  1. 当要实现一个设备一次只能被一个进程打开. 实现方式最好是通过维护一个atiomic_t 变量。 当变量为1,表示设备可用, 设备被OPEN时递减并测试以确定当前是否被打开。

static atomic_t scull_is_available = ATOMIC_INIT(1);

static int scull_s_open(struct inode *inode, struct file *filp)
{
  struct scull_dev *dev= &scull_s_device; /* device information */

  if (!atomic_dec_and_test(&scull_is_available)) {

    atomic_inc(&scull_is_available);
    return -EBUSY; /* already open */

  }

  ...

  filp->private_data = dev;

  return 0; /*OK*/

}


static int scull_s_release(struct inode *inode, struct file *filp)

{

  atomic_inc(&scull_is_avlaible);

  return 0;

}



  1. 当要求设备只对某用户访问 (多进程打开)时。最好在设备的结构中维护一个打开计数和拥有者UID。
    • open调用时,第一次打开记住设备的拥有者。 以后的打开动作和当前UID作比较即可。

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;
}


阅读(1743) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~