How can I programmatically set permissions for my char device

I recently inherited some code at work, these are old Linux 2.4.X kernel drivers, and I was instructed to get them to work on a newer kernel 2.6 or higher. I work on OpenSUSE 12.1 with the kernel 3.1.10.

I updated the source code from register_chrdev () to use the class_create () / device_create () calls, and I see that my devices are displayed in / dev correctly. My current problem is that the permissions for my device are set to r / w only for the user:

crw-------  1 root root    244,   0 Aug  7 07:57 gcanain

I know that I can "chmod" the file through the command line, and I can also configure the udev permissions ... but is there anyway to do this programmatically, for example, when I issue the insmod command, will dev be installed with the correct rules already?

Are there any APIs that can exist that I can call for this, any parameters that are missing from one of these creation APIs?

Just to clarify, part of the reason I don’t want to use udev rules is because I don’t know the names of the device drivers in advance. Device drivers are generated in a loop, so names are added using a number, nNumDevs can be almost 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);
}
+5
source share
3 answers

This is the method used by the TTY driver to set the permission for 0666 to create:

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 struct classhas a pointer to parameters modethat will allow you to set permissions.

, mode NULL, .

+4

: #include <sys/stat.h>

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

: man -s 2 chmod

+3

udev has rules for permissions, you need to create them under /etc/udev/rules.d

First try the following:

In the file, /etc/udev/udev.confadd this line:

# My default permissions
default_mode="0660"

If this does not work, add the rule to /etc/udev/rules.d, in more detail here: http://www.reactivated.net/writing_udev_rules.html

+1
source

All Articles