Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I programmatically set permissions on my char device

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);
}
like image 573
Mike Avatar asked Aug 07 '12 12:08

Mike


2 Answers

This is the method used by the TTY driver to set permission to 0666 on creation:

static char *tty_devnode(struct device *dev, umode_t *mode)
{
        if (!mode)
                return NULL;
        if (dev->devt == MKDEV(TTYAUX_MAJOR, 0) ||
            dev->devt == MKDEV(TTYAUX_MAJOR, 2))
                *mode = 0666;
        return NULL;
}

static int __init tty_class_init(void)
{
        tty_class = class_create(THIS_MODULE, "tty");
        if (IS_ERR(tty_class))
                return PTR_ERR(tty_class);
        tty_class->devnode = tty_devnode;
        return 0;
}

The devnode attribute in struct class has a parameter pointer mode that will allow you to set permissions.

Beware, mode may be NULL when the device gets destroyed.

like image 75
vincent Avatar answered Nov 02 '22 20:11

vincent


Try this: #include <sys/stat.h>

int chmod(const char *path, mode_t mode); OR int fchmod(int fd, mode_t mode);

Source: man -s 2 chmod

like image 20
askmish Avatar answered Nov 02 '22 19:11

askmish