如何在Linux内核中使用__ATTR宏?

时间:2017-01-20 19:55:16

标签: linux-kernel sysfs

我想创建一个sysfs条目,使用"属性"结构体。建议这样做的方法是使用__ATTR宏,如下所示:

static ssize_t functionCalledOnRead(struct kobject *, struct attribute *, char *);
static ssize_t functionCalledOnRead(struct kobject *, struct attribute *, char *);

static struct attribute genericSysfsAttribute = __ATTR(attr_name, S_IRUGO | S_IRUSR, functionCalledOnRead, functionCalledOnWrite);

然而,这不起作用。

/home/osboxes/Documents/kernel_modules/sysfsmodule/sysfsmodule.c:32:15: warning: braces around scalar initializer
/home/osboxes/Documents/kernel_modules/sysfsmodule/sysfsmodule.c:32:15: warning: (near initialization for 'genericSysfsAttribute.name')
/home/osboxes/Documents/kernel_modules/sysfsmodule/sysfsmodule.c:32:15: error: field name not in record or union initializer
/home/osboxes/Documents/kernel_modules/sysfsmodule/sysfsmodule.c:32:15: error: (near initialization for 'genericSysfsAttribute.name')
/home/osboxes/Documents/kernel_modules/sysfsmodule/sysfsmodule.c:32:15: error: field name not in record or union initializer
/home/osboxes/Documents/kernel_modules/sysfsmodule/sysfsmodule.c:32:15: error: (near initialization for 'genericSysfsAttribute.name')
In file included from include/linux/thread_info.h:11:0,
                 from include/asm-generic/preempt.h:4,
                 from arch/arm/include/generated/asm/preempt.h:1,
                 from include/linux/preempt.h:59,
                 from include/linux/spinlock.h:50,
                 from include/linux/seqlock.h:35,
                 from include/linux/time.h:5,
                 from include/linux/stat.h:18,
                 from include/linux/module.h:10,
                 from /home/osboxes/Documents/kernel_modules/sysfsmodule/sysfsmodule.c:2:
include/linux/bug.h:34:45: warning: excess elements in scalar initializer
 #define BUILD_BUG_ON_ZERO(e) (sizeof(struct { int:-!!(e); }))
                                             ^
include/linux/kernel.h:842:3: note: in expansion of macro 'BUILD_BUG_ON_ZERO'
   BUILD_BUG_ON_ZERO((perms) & 2) +     \
   ^
include/linux/sysfs.h:102:12: note: in expansion of macro 'VERIFY_OCTAL_PERMISSIONS'
    .mode = VERIFY_OCTAL_PERMISSIONS(_mode) },  \
            ^
/home/osboxes/Documents/kernel_modules/sysfsmodule/sysfsmodule.c:32:49: note: in expansion of macro '__ATTR'
 static struct attribute genericSysfsAttribute = __ATTR(attr_name,S_IRUGO | S_IRUSR,functionCalledOnRead,functionCalledOnWrite);
                                                 ^
include/linux/bug.h:34:45: warning: (near initialization for 'genericSysfsAttribute.name')
 #define BUILD_BUG_ON_ZERO(e) (sizeof(struct { int:-!!(e); }))
                                             ^
include/linux/kernel.h:842:3: note: in expansion of macro 'BUILD_BUG_ON_ZERO'
   BUILD_BUG_ON_ZERO((perms) & 2) +     \
   ^
include/linux/sysfs.h:102:12: note: in expansion of macro 'VERIFY_OCTAL_PERMISSIONS'
    .mode = VERIFY_OCTAL_PERMISSIONS(_mode) },  \
            ^
/home/osboxes/Documents/kernel_modules/sysfsmodule/sysfsmodule.c:32:49: note: in expansion of macro '__ATTR'
 static struct attribute genericSysfsAttribute = __ATTR(attr_name,S_IRUGO | S_IRUSR,functionCalledOnRead,functionCalledOnWrite);
                                                 ^
include/linux/bug.h:34:45: error: unknown field 'show' specified in initializer
 #define BUILD_BUG_ON_ZERO(e) (sizeof(struct { int:-!!(e); }))
                                             ^
include/linux/kernel.h:842:3: note: in expansion of macro 'BUILD_BUG_ON_ZERO'
   BUILD_BUG_ON_ZERO((perms) & 2) +     \
   ^
include/linux/sysfs.h:102:12: note: in expansion of macro 'VERIFY_OCTAL_PERMISSIONS'
    .mode = VERIFY_OCTAL_PERMISSIONS(_mode) },  \
            ^
/home/osboxes/Documents/kernel_modules/sysfsmodule/sysfsmodule.c:32:49: note: in expansion of macro '__ATTR'
 static struct attribute genericSysfsAttribute = __ATTR(attr_name,S_IRUGO | S_IRUSR,functionCalledOnRead,functionCalledOnWrite);
                                                 ^
include/linux/bug.h:34:45: warning: initialization makes integer from pointer without a cast
 #define BUILD_BUG_ON_ZERO(e) (sizeof(struct { int:-!!(e); }))
                                             ^
include/linux/kernel.h:842:3: note: in expansion of macro 'BUILD_BUG_ON_ZERO'
   BUILD_BUG_ON_ZERO((perms) & 2) +     \
   ^
include/linux/sysfs.h:102:12: note: in expansion of macro 'VERIFY_OCTAL_PERMISSIONS'
    .mode = VERIFY_OCTAL_PERMISSIONS(_mode) },  \
            ^
/home/osboxes/Documents/kernel_modules/sysfsmodule/sysfsmodule.c:32:49: note: in expansion of macro '__ATTR'
 static struct attribute genericSysfsAttribute = __ATTR(attr_name,S_IRUGO | S_IRUSR,functionCalledOnRead,functionCalledOnWrite);
                                                 ^
include/linux/bug.h:34:45: warning: (near initialization for 'genericSysfsAttribute.mode')
 #define BUILD_BUG_ON_ZERO(e) (sizeof(struct { int:-!!(e); }))
                                             ^
include/linux/kernel.h:842:3: note: in expansion of macro 'BUILD_BUG_ON_ZERO'
   BUILD_BUG_ON_ZERO((perms) & 2) +     \
   ^
include/linux/sysfs.h:102:12: note: in expansion of macro 'VERIFY_OCTAL_PERMISSIONS'
    .mode = VERIFY_OCTAL_PERMISSIONS(_mode) },  \
            ^
/home/osboxes/Documents/kernel_modules/sysfsmodule/sysfsmodule.c:32:49: note: in expansion of macro '__ATTR'
 static struct attribute genericSysfsAttribute = __ATTR(attr_name,S_IRUGO | S_IRUSR,functionCalledOnRead,functionCalledOnWrite);
                                                 ^
include/linux/bug.h:34:45: error: initializer element is not computable at load time
 #define BUILD_BUG_ON_ZERO(e) (sizeof(struct { int:-!!(e); }))
                                             ^
include/linux/kernel.h:842:3: note: in expansion of macro 'BUILD_BUG_ON_ZERO'
   BUILD_BUG_ON_ZERO((perms) & 2) +     \
   ^
include/linux/sysfs.h:102:12: note: in expansion of macro 'VERIFY_OCTAL_PERMISSIONS'
    .mode = VERIFY_OCTAL_PERMISSIONS(_mode) },  \
            ^
/home/osboxes/Documents/kernel_modules/sysfsmodule/sysfsmodule.c:32:49: note: in expansion of macro '__ATTR'
 static struct attribute genericSysfsAttribute = __ATTR(attr_name,S_IRUGO | S_IRUSR,functionCalledOnRead,functionCalledOnWrite);
                                                 ^
include/linux/bug.h:34:45: error: (near initialization for 'genericSysfsAttribute.mode')
 #define BUILD_BUG_ON_ZERO(e) (sizeof(struct { int:-!!(e); }))
                                             ^
include/linux/kernel.h:842:3: note: in expansion of macro 'BUILD_BUG_ON_ZERO'
   BUILD_BUG_ON_ZERO((perms) & 2) +     \
   ^
include/linux/sysfs.h:102:12: note: in expansion of macro 'VERIFY_OCTAL_PERMISSIONS'
    .mode = VERIFY_OCTAL_PERMISSIONS(_mode) },  \
            ^
/home/osboxes/Documents/kernel_modules/sysfsmodule/sysfsmodule.c:32:49: note: in expansion of macro '__ATTR'
 static struct attribute genericSysfsAttribute = __ATTR(attr_name,S_IRUGO | S_IRUSR,functionCalledOnRead,functionCalledOnWrite);
                                                 ^
include/linux/bug.h:34:45: error: unknown field 'store' specified in initializer
 #define BUILD_BUG_ON_ZERO(e) (sizeof(struct { int:-!!(e); }))
                                             ^
include/linux/kernel.h:842:3: note: in expansion of macro 'BUILD_BUG_ON_ZERO'
   BUILD_BUG_ON_ZERO((perms) & 2) +     \
   ^
include/linux/sysfs.h:102:12: note: in expansion of macro 'VERIFY_OCTAL_PERMISSIONS'
    .mode = VERIFY_OCTAL_PERMISSIONS(_mode) },  \
            ^
/home/osboxes/Documents/kernel_modules/sysfsmodule/sysfsmodule.c:32:49: note: in expansion of macro '__ATTR'
 static struct attribute genericSysfsAttribute = __ATTR(attr_name,S_IRUGO | S_IRUSR,functionCalledOnRead,functionCalledOnWrite);
                                                 ^
include/linux/bug.h:34:45: warning: excess elements in struct initializer
 #define BUILD_BUG_ON_ZERO(e) (sizeof(struct { int:-!!(e); }))
                                             ^
include/linux/kernel.h:842:3: note: in expansion of macro 'BUILD_BUG_ON_ZERO'
   BUILD_BUG_ON_ZERO((perms) & 2) +     \
   ^
include/linux/sysfs.h:102:12: note: in expansion of macro 'VERIFY_OCTAL_PERMISSIONS'
    .mode = VERIFY_OCTAL_PERMISSIONS(_mode) },  \
            ^
/home/osboxes/Documents/kernel_modules/sysfsmodule/sysfsmodule.c:32:49: note: in expansion of macro '__ATTR'
 static struct attribute genericSysfsAttribute = __ATTR(attr_name,S_IRUGO | S_IRUSR,functionCalledOnRead,functionCalledOnWrite);
                                                 ^
include/linux/bug.h:34:45: warning: (near initialization for 'genericSysfsAttribute')
 #define BUILD_BUG_ON_ZERO(e) (sizeof(struct { int:-!!(e); }))
                                             ^
include/linux/kernel.h:842:3: note: in expansion of macro 'BUILD_BUG_ON_ZERO'
   BUILD_BUG_ON_ZERO((perms) & 2) +     \
   ^
include/linux/sysfs.h:102:12: note: in expansion of macro 'VERIFY_OCTAL_PERMISSIONS'
    .mode = VERIFY_OCTAL_PERMISSIONS(_mode) },  \
            ^
/home/osboxes/Documents/kernel_modules/sysfsmodule/sysfsmodule.c:32:49: note: in expansion of macro '__ATTR'
 static struct attribute genericSysfsAttribute = __ATTR(attr_name,S_IRUGO | S_IRUSR,functionCalledOnRead,functionCalledOnWrite);
                                                 ^
/home/osboxes/Documents/kernel_modules/sysfsmodule/sysfsmodule.c:32:25: warning: 'genericSysfsAttribute' defined but not used [-Wunused-variable]
 static struct attribute genericSysfsAttribute = __ATTR(attr_name,S_IRUGO | S_IRUSR,functionCalledOnRead,functionCalledOnWrite);
                         ^
scripts/Makefile.build:297: recipe for target '/home/osboxes/Documents/kernel_modules/sysfsmodule/sysfsmodule.o' failed
make[2]: *** [/home/osboxes/Documents/kernel_modules/sysfsmodule/sysfsmodule.o] Error 1
Makefile:1425: recipe for target '_module_/home/osboxes/Documents/kernel_modules/sysfsmodule' failed
make[1]: *** [_module_/home/osboxes/Documents/kernel_modules/sysfsmodule] Error 2
make[1]: Leaving directory '/home/osboxes/Documents/linux-mykonos'
Makefile:7: recipe for target 'cross' failed
make: *** [cross] Error 2

我真的不明白大多数错误在说什么,因为他们正在谈论包含的代码,而不是我写的内容。但是,由于第一个参数只是一个名称,第二个参数只是一个数字,我不认为这些有任何问题,所以它似乎与我如何给出这两个名称有关。功能不起作用。如何传递这样的函数?

2 个答案:

答案 0 :(得分:0)

if df['time'].total_seconds() == 291.0: print("true") 不是直接使用,而是供子系统开发人员使用,他们为kobject定义类型并创建属性结构"衍生"来自__ATTR

此外,普通struct attributea little sence

  

裸属性不包含读取或写入值的方法   属性。鼓励子系统定义自己的属性   用于添加和删除属性的结构和包装函数   特定的对象类型。

答案 1 :(得分:0)

由于传递给宏的模式值S_IRUGO|S_IRUSR引起错误。在<linux/kernel.h>模块中,如果权限违反以下任一条件,则权限检查将引发错误:

  1. USER_READABLE> = GROUP_READABLE> = OTHER_READABLE
  2. <777和> 0
  3. USER_WRITABLE> = GROUP_WRITABLE
  4. OTHER_WRITABLE = 0(不允许在此处写入内容不是一个好主意)