atomic_inc(&scull_is_available); return -EBUSY; /* already open */
}
...
filp->private_data = dev;
return 0; /*OK*/
}
static int scull_s_release(struct inode *inode,structfile*filp)
{
atomic_inc(&scull_is_avlaible);
return 0;
}
当要求设备只对某用户访问 (多进程打开)时。最好在设备的结构中维护一个打开计数和拥有者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 */