I've recently inherited some code at work, this is old 2.4.X linux kernel drivers and I've been tasked with getting them working on a newer kernel 2.6 or greater. I'm running on OpenSUSE 12.1 with a 3.1.10 kernel.
I've updated the original code from register_chrdev() to use class_create()/device_create() calls and I can see my devices show up in /dev correctly. My current issue is that the permissions for my device are being set to r/w for user only:
crw------- 1 root root 244, 0 Aug 7 07:57 gcanain
I know I can "chmod" the file via command line, and or I can set up udev permissions... but is there anyway to do this programmatically, such that when I issue the insmod command, the dev will be mounted with the correct rules in place already?
Are there any APIs that might exist that I can call to do this, any options I'm missing in one of these creation APIs?
Just to clarify, part of the reason I don't want to use udev rules is that I don't know the names of the device drivers ahead of time. The device drivers are spawned in a loop and so the names are appended with a digit, nNumDevs can be pretty much anything:
for (i = 0; i < nNumDevs; i++) {
strcpy(Modname,GC_ANAIN_MODULE_NAME);
strcat(Modname,"%d");
device_create(c1, NULL, MKDEV(nMajor, GC_ANAIN_MINOR_VERSION+i), NULL, Modname, i);
}
Try this:
#include <sys/stat.h>
int chmod(const char *path, mode_t mode);
ORint fchmod(int fd, mode_t mode);
Source: man -s 2 chmod
udev has rules for permissions, you need to create them under
/etc/udev/rules.d
First try this:
In the file
/etc/udev/udev.conf
, add this line:If this doesn't work add a rule in /etc/udev/rules.d, more on that here : http://www.reactivated.net/writing_udev_rules.html
This is the method used by the TTY driver to set permission to 0666 on creation:
The devnode attribute in
struct class
has a parameter pointermode
that will allow you to set permissions.Beware,
mode
may beNULL
when the device gets destroyed.