我正在制作一个小型 Linux 模块,它是 char 设备的驱动程序。
在我的代码中,我创建了设备类,而不是设备本身,因此 /dev 文件是
在我的系统中创建。问题是/dev文件只有root权限,而用户
对该文件既没有读、写也没有执行权限,我想更改
/dev 文件权限。
我在网上搜索了答案,发现是更改 udev 文件,但是这个
解决方案在我的情况下不起作用,因为我需要在模块加载到内核时动态更改权限。我正在编写的模块并不总是在我的机器上运行,因此我需要它来“动态”更改权限。
major_number_firewall = register_chrdev(0, device_name_firewall, &my_file_implementation_firewall);
device_class = class_create(THIS_MODULE, class_name_firewall);
log_file_device = device_create(device_class, NULL, MKDEV(major_number_firewall, MINOR_LOG), NULL, device_name_log_file);
有没有修改权限的功能?
你可以写一个小udev规则 http://www.reactivated.net/writing_udev_rules.html为了实现这一目标。
-
如果您正在实现字符设备驱动程序,请考虑使用misc_register()
and misc_unregister()
这是上述调用的包装(device_create()
...)。参考结构杂项设备 http://lxr.linux.no/linux+v3.3.3/include/linux/miscdevice.h#L50
struct miscdevice {
int minor;
const char *name;
const struct file_operations *fops;
struct list_head list;
struct device *parent;
struct device *this_device;
const char *nodename;
umode_t mode;
};
您可以使用会员(struct miscdevice *)->mode
设置适当的权限(S_IRUGO | S_IRWXUGO | S_IALLUGO | 等...)
希望这可以帮助。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)