无法在sysfs中将属性权限设置为0666

时间:2016-11-23 06:53:26

标签: permissions linux-kernel kernel sysfs eudyptula-challenge

我正在使用sysfs,我需要在sysfs下创建一个文件,该文件应该是所有用户都可读写的,我将'__ATTR'中的权限设置为0666。但是模块没有编译,一旦我将权限更改为0660,它就会正确编译。

我获得0666权限的错误消息如下

`/home/rishabh/kernel_modules/Task09/task9.c: At top level:
include/linux/bug.h:33:45: error: negative width in bit-field ‘<anonymous>’
 #define BUILD_BUG_ON_ZERO(e) (sizeof(struct { int:-!!(e); }))
                                             ^
include/linux/kernel.h:859: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/rishabh/kernel_modules/Task09/task9.c:65:2: note: in expansion of macro ‘__ATTR’
  __ATTR(id, 0666, id_show, id_store);
  ^
include/linux/bug.h:33:45: warning: initialization from incompatible pointer type [enabled by default]
 #define BUILD_BUG_ON_ZERO(e) (sizeof(struct { int:-!!(e); }))
                                             ^
include/linux/kernel.h:859: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/rishabh/kernel_modules/Task09/task9.c:65:2: note: in expansion of macro ‘__ATTR’
  __ATTR(id, 0666, id_show, id_store);
  ^
include/linux/bug.h:33:45: warning: (near initialization for ‘id_attribute.show’) [enabled by default]
 #define BUILD_BUG_ON_ZERO(e) (sizeof(struct { int:-!!(e); }))
                                             ^
include/linux/kernel.h:859: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/rishabh/kernel_modules/Task09/task9.c:65:2: note: in expansion of macro ‘__ATTR’
  __ATTR(id, 0666, id_show, id_store);
  ^
`

我也尝试使用__ATTR_RW(_name)宏,但它只向root提供读写权限,而其他所有权限都具有读取权限。

2 个答案:

答案 0 :(得分:3)

如果您按照错误消息,第二个

kernel.h:859:3: note: in expansion of macro ‘BUILD_BUG_ON_ZERO’
BUILD_BUG_ON_ZERO((perms) & 2)

如果您查看kernel.h,您会看到评论

#define VERIFY_OCTAL_PERMISSIONS(perms)                      
     ...
     /* OTHER_WRITABLE?  Generally considered a bad idea. */ \
     BUILD_BUG_ON_ZERO((perms) & 2) + \
 ...

所以你可以看到你被告知让一个sysfs文件可写入所有人是一个坏主意。如果您确实想这样做,则必须绕过此宏检查。例如,在调用__ATTR()之前添加宏的重新定义:

/* warning! need write-all permission so overriding check */ 
#undef VERIFY_OCTAL_PERMISSIONS
#define VERIFY_OCTAL_PERMISSIONS(perms) (perms)

答案 1 :(得分:1)

__ATTR_RW(id)应该是正确的方式(并且eudyptula接受了;))。 sysfs.h中的定义表示,它设置了0644的权限,这是您想要的正确权限 - 无人,除root用户外,无法写入{ {1}}个文件(它也在任务中指定)。

sysfs.h部分:

/sys/kernel