How can I programmatically set permissions on my char device

拜拜、爱过 提交于 2019-12-03 04:16:42
vincent

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.

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

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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!