Linux内核:设置通过create_device()创建的/dev文件的权限 [英] Linux kernel : Setting the permissions for a /dev file that was created via create_device()
问题描述
我正在制作一个小的linux模块,该模块是char设备的驱动程序.在我的代码中,我创建了设备类,而不是设备本身,因此创建了/dev文件在我的系统中创建.问题是/dev文件仅具有root权限,而用户没有对该文件的读取,写入或执行权限,我想更改/dev文件权限.
I am making a small linux module that is a driver to a char device. In my code i create the device class, than the device it self and thus a /dev file is created in my system. the problem is that the /dev file has only root permissions and the user has neither read nor write nor execute permissions on that file, i would like to change the /dev file permissions.
我已经在网上搜索了答案,发现是要更改udev文件,但这解决方案不适用于我的情况,因为当模块加载到内核时,我需要动态更改权限.我正在编写的模块不会总是在我的机器上运行,因此我需要它来动态"更改权限.
I have searched the web for answers and what i found was to change the udev file, but this solution will not work in my case because i need the permissions to change dynamicly when the module is loaded into the kernel. The module i am writing will not always run on my machine, thus i need it to change the permissions "on the fly".
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);
是否具有更改权限的功能?
Is there a function for changing the permissions?
推荐答案
-
您可以编写一个小的 udev规则来实现此目的.
如果要实现char设备驱动程序,请考虑使用 misc_register()
和 misc_unregister()
,它们是上述调用的包装(device_create()
...).请参考结构混合设备
If you are implementing a char device driver, then consider using misc_register()
and misc_unregister()
which is wrapper over the above calls (device_create()
...). Refer to struct miscdevice
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 |等...)
You can use the member (struct miscdevice *)->mode
to set the appropriate permissions (S_IRUGO | S_IRWXUGO | S_IALLUGO | etc ...)
希望这会有所帮助.
这篇关于Linux内核:设置通过create_device()创建的/dev文件的权限的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!