对创建二进制 sysfs 条目感到困惑
Confused on creation of binary sysfs entry
在内核 4.0 上,当单步执行 sysfs_create_bin_file
的内核源代码时,我注意到它传递给 sysfs_add_file(kobj->sd, &attr->attr, true);
&attr->attr
是 struct attribute
结构中的 bin_attribute
结构。
在我访问 sysfs_add_file_mode_ns
之前这是有意义的,它是直接从 sysfs_add_file
调用的,并且在 line #277 上设置临时变量 stuct bin_attribute *battr = (void*)attr;
此时这不是指向 struct attribute
吗,如何将其解析为正确的结构(由于在 [= 上使用 &attr->attr
调用 sysfs_add_file
23=])?
代码
int sysfs_create_bin_file(struct kobject *kobj,
const struct bin_attribute *attr)
{
BUG_ON(!kobj || !kobj->sd || !attr);
return sysfs_add_file(kobj->sd, &attr->attr, true);
}
int sysfs_add_file(struct kernfs_node *parent, const struct attribute *attr,
bool is_bin)
{
return sysfs_add_file_mode_ns(parent, attr, is_bin, attr->mode, NULL);
}
int sysfs_add_file_mode_ns(struct kernfs_node *parent,
const struct attribute *attr, bool is_bin,
umode_t mode, const void *ns)
{
struct lock_class_key *key = NULL;
const struct kernfs_ops *ops;
struct kernfs_node *kn;
loff_t size;
if (!is_bin) {
...
} else {
struct bin_attribute *battr = (void *)attr;
...
}
行
stuct bin_attribute *battr = (void*)attr;
从指向其 第一个字段 attr
类型 struct attribute
.[=18= 的指针正确获取指向 bin_attribute
结构的指针]
通常,Linux 内核开发人员倾向于使用 container_of
宏来获取指向结构类型的指针,知道指向其字段的指针。上述转换的更多 "canonical" 方式是:
stuct bin_attribute *battr = container_of(attr, struct bin_attribute, attr);
(在此调用中,第一个 attr
参数引用 指针 ,第三个 attr
参数引用 字段的姓名).
在内核 4.0 上,当单步执行 sysfs_create_bin_file
的内核源代码时,我注意到它传递给 sysfs_add_file(kobj->sd, &attr->attr, true);
&attr->attr
是 struct attribute
结构中的 bin_attribute
结构。
在我访问 sysfs_add_file_mode_ns
之前这是有意义的,它是直接从 sysfs_add_file
调用的,并且在 line #277 上设置临时变量 stuct bin_attribute *battr = (void*)attr;
此时这不是指向 struct attribute
吗,如何将其解析为正确的结构(由于在 [= 上使用 &attr->attr
调用 sysfs_add_file
23=])?
代码
int sysfs_create_bin_file(struct kobject *kobj,
const struct bin_attribute *attr)
{
BUG_ON(!kobj || !kobj->sd || !attr);
return sysfs_add_file(kobj->sd, &attr->attr, true);
}
int sysfs_add_file(struct kernfs_node *parent, const struct attribute *attr,
bool is_bin)
{
return sysfs_add_file_mode_ns(parent, attr, is_bin, attr->mode, NULL);
}
int sysfs_add_file_mode_ns(struct kernfs_node *parent,
const struct attribute *attr, bool is_bin,
umode_t mode, const void *ns)
{
struct lock_class_key *key = NULL;
const struct kernfs_ops *ops;
struct kernfs_node *kn;
loff_t size;
if (!is_bin) {
...
} else {
struct bin_attribute *battr = (void *)attr;
...
}
行
stuct bin_attribute *battr = (void*)attr;
从指向其 第一个字段 attr
类型 struct attribute
.[=18= 的指针正确获取指向 bin_attribute
结构的指针]
通常,Linux 内核开发人员倾向于使用 container_of
宏来获取指向结构类型的指针,知道指向其字段的指针。上述转换的更多 "canonical" 方式是:
stuct bin_attribute *battr = container_of(attr, struct bin_attribute, attr);
(在此调用中,第一个 attr
参数引用 指针 ,第三个 attr
参数引用 字段的姓名).