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

user3595668 picture user3595668 · May 2, 2014 · Viewed 7.4k times · Source

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.

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?

Answer

askb picture askb · Nov 5, 2014
  1. You can write a small udev rules for achieving this.

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

You can use the member (struct miscdevice *)->mode to set the appropriate permissions (S_IRUGO | S_IRWXUGO | S_IALLUGO | etc ...)

hope this helps.