无法在 sysfs 中将属性权限设置为 0666
Cannot set Attribute Permissions to 0666 in sysfs
我正在使用 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权限读写权限,其他人只剩下读权限。
如果您按照错误消息进行操作,第二条是
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)
__ATTR_RW(id)
应该是正确的方法(而且 eudyptula 接受了 ;))。 sysfs.h
中的定义说,它将权限设置为 0644
,这是您想要的正确权限 - 没有人,除了 root 用户,可以不要写入 /sys/kernel
文件(它也在任务中指定)。
sysfs.h 部分:
#define __ATTR_RW(_name) __ATTR(_name, (S_IWUSR | S_IRUGO), \
_name##_show, _name##_store)
我正在使用 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权限读写权限,其他人只剩下读权限。
如果您按照错误消息进行操作,第二条是
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)
__ATTR_RW(id)
应该是正确的方法(而且 eudyptula 接受了 ;))。 sysfs.h
中的定义说,它将权限设置为 0644
,这是您想要的正确权限 - 没有人,除了 root 用户,可以不要写入 /sys/kernel
文件(它也在任务中指定)。
sysfs.h 部分:
#define __ATTR_RW(_name) __ATTR(_name, (S_IWUSR | S_IRUGO), \
_name##_show, _name##_store)