linux- sysfs 文件系统 kobject_create_and_add

參考: http://www.emsym.com/blog/?p=1036
介紹sysfs 文件系统 :
linux 2.6 内核中引入了 sysfs 文件系统,是用户空间与内核空间进行交互的一个媒介。
比起古老的 proc 文件系统,它是基于内核的数据结构,因此组织结构上更加严密。
它的设计使内核的信息更易获取,而且更加清晰。内核空间与用户空间的映射关系如下表所示:

内核空间(internel) ——->用户空间(externel)
内核对象(kernel objects) ——->目录(directories)
对象属性(object attributes) ——->普通文件(regular files)
对象关系(object relationshiops) ——->符号链接(symbolic links)

先使用kobject_create_and_add() 創建目錄
再透過sysfs_create_group(example_kobj, &attr_group) 創建目录下的属性文件组, 其中 attr_group 是我们自己定义的属性相关文件,

static int __init example_init(void)
{
int retval;

/*
* Create a simple kobject with the name of “kobject_example”,
* located under /sys/kernel/
*
* As this is a simple directory, no uevent will be sent to
* userspace. That is why this function should not be used for
* any type of dynamic kobjects, where the name and number are
* not known ahead of time.
*/
example_kobj = kobject_create_and_add(“kobject_example”, kernel_kobj);
if (!example_kobj)
return -ENOMEM;

/* Create the files associated with this kobject */
retval = sysfs_create_group(example_kobj, &attr_group);
if (retval)
kobject_put(example_kobj);

return retval;
}

通过它,我们描述了目录里具有的属性文件,以及各文件的 show 和 store 函数。
对于本例来说,可用一个属性数组 attrs 描述这一属性组 attr_group。

分類: 未分類

在〈linux- sysfs 文件系统 kobject_create_and_add〉中有 1 則留言

易春木 發表迴響 取消回覆