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?
You can write a small udev rules for achieving this.
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.