我如何编程设置我的字符设备的权限 [英] How can I programmatically set permissions on my char device

查看:650
本文介绍了我如何编程设置我的字符设备的权限的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我最近在继承了一些工作code,这是老2.4.X Linux内核驱动程序和我一直在负责让他们在新的内核2.6或更高的工作。我用3.1.10内核在openSUSE 12.1上运行。

我已经更新从函数register_chrdev原来的code()使用class_create()/ device_create()调用,我可以看到我的设备在/ dev中正确显示。我现在的问题是,我的设备的权限被设置为R / W用户只:

  CRW ------- 1根根244,0 8月7日07:57 gcanain

我知道我可以在文件模式通过命令行的文件,或者我可以设置权限的udev ......但反正是有以编程方式做到这一点,这样,当我发出insmod命令,dev的将被安装有了正确的规则了吗?

是否有可能存在,我可以打电话要做到这一点,我缺少任何选项,这些创建API中的任何一个的API?

只是为了澄清,我不希望使用udev规则的部分原因是,我不知道时间提前的设备驱动程序的名称。设备驱动程序在一个循环催生等等名称与数字追加,nNumDevs可以pretty任何东西:

 为(i = 0; I< nNumDevs;我++){
  的strcpy(的modname,GC_ANAIN_MODULE_NAME);
  strcat的(的modname,%D);
  device_create(C1,NULL,用mkdev(nMajor,GC_ANAIN_MINOR_VERSION + I),NULL中,modname,I);
}


解决方案

试试这个:
的#include< SYS / stat.h>

INT CHMOD(为const char *路径,mode_t模式);
要么
INT fchmod(INT FD,mode_t模式);

来源:男人-s 2搭配chmod

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); OR int fchmod(int fd, mode_t mode);

Source: man -s 2 chmod

这篇关于我如何编程设置我的字符设备的权限的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆