Linux 内核 : Setting the permissions for a/dev file that was created via create_device()

标签 linux permissions linux-kernel chardev

我正在制作一个小的 linux 模块,它是一个字符设备的驱动程序。 在我的代码中,我创建了设备类,而不是它自己的设备,因此/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);

是否有修改权限的功能?

最佳答案

  1. 你可以写一个小udev rules为了实现这一目标。

  2. 如果您正在实现一个字符设备驱动程序,那么请考虑使用 misc_register()misc_unregister(),它们是上述调用的包装器( device_create()...)。引用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 | etc ...)

希望这对您有所帮助。

关于Linux 内核 : Setting the permissions for a/dev file that was created via create_device(),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23424884/

相关文章:

Linux 设备驱动程序使用的次要编号

linux - 由于权限原因无法创建文件

通过 CreateProcess 启动时的 cl.exe 似乎没有写权限

linux - Ubuntu/Linux 庆典 : traverse directory and subdirectories to work with files

linux - 如何将 libc.a 链接到 arm-linux 中的共享库中使用 arm-none-linux-gnueabi-gcc

Python IRC 机器人帮助

iOS 不可靠位置权限警报

php - MySQL/PHP : Looking for matching records from three tables(Forum permissions)

linux - 为什么 mwifiex_sdio 找不到固件? (xe303c12 chromebook 上的 Arch Linux ARM)

linux - 设备树 - 探测驱动程序并避免竞争条件