How can I programmatically set permissions on my c

2019-03-20 17:43发布

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);
}

3条回答
一夜七次
2楼-- · 2019-03-20 17:53

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

查看更多
该账号已被封号
3楼-- · 2019-03-20 17:57

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:

# My default permissions
default_mode="0660"

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

查看更多
劳资没心,怎么记你
4楼-- · 2019-03-20 18:01

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.

查看更多
登录 后发表回答