你好,游客 登录 注册 搜索
背景:
阅读新闻

Linux之configfs简介和编程入门

[日期:2014-01-23] 来源:Linux社区  作者:liumangxiong [字体:大 中 小]

属性非常简单,有属性名称、所属模块和访问权限。


示例1:创建一个最简单的configfs目录和可读写的属性
创建目录01-childless,下面有三个属性,其中属性storeme为可写。
# ll /sys/kernel/config/01-childless/
-r--r--r--. 1 root root 4096 Sep 27 05:16 description
-r--r--r--. 1 root root 4096 Sep 27 05:16 showme
-rw-r--r--. 1 root root 4096 Sep 27 05:16 storeme

37/*
38 * 01-childless
39 *
40 * This first example is a childless subsystem.  It cannot create
41 * any config_items.  It just has attributes.
42 *
43 * Note that we are enclosing the configfs_subsystem inside a container.
44 * This is not necessary if a subsystem has no attributes directly
45 * on the subsystem.  See the next example, 02-simple-children, for
46 * such a subsystem.
47 */
48
49struct childless {
50    struct configfs_subsystem subsys;
51    int showme;
52    int storeme;
53};

创建一个结构体包含了configfs子系统,两个属性showme, storeme放在了这里。
虽然注释里说明了为什么把这两个属性放这里,但还是没太理解,既然属性description可以放子系统下,为什么其他两个属性不行呢?有谁知道的告知一下。
子系统定义如下:

138static struct childless childless_subsys = {
139    .subsys = {
140          .su_group = {
141              .cg_item = {
142                    .ci_namebuf = "01-childless",
143                    .ci_type = &childless_type,
144              },
145          },
146    },
147};

142行,定义目录名称。
143行,定义属性和操作。
接着看属性和操作的定义:

114CHILDLESS_ATTR_RO(showme, childless_showme_read);
115CHILDLESS_ATTR(storeme, S_IRUGO | S_IWUSR, childless_storeme_read,
116            childless_storeme_write);
117CHILDLESS_ATTR_RO(description, childless_description_read);
118
119static struct configfs_attribute *childless_attrs[] = {
120    &childless_attr_showme.attr,
121    &childless_attr_storeme.attr,
122    &childless_attr_description.attr,
123    NULL,
124};
125
126CONFIGFS_ATTR_OPS(childless);
127static struct configfs_item_operations childless_item_ops = {
128    .show_attribute          = childless_attr_show,
129    .store_attribute    = childless_attr_store,
130};
131
132static struct config_item_type childless_type = {
133    .ct_item_ops    = &childless_item_ops,
134    .ct_attrs    = childless_attrs,
135    .ct_owner    = THIS_MODULE,
136};

133行,属性操作定义在128行和129行,这两个函数又是由126行宏定义的。126行宏依赖一个函数to_##_item,其中_item是宏的传入参数,这个函数的作用是从struct config_item转换到第49行的结构,然后作为函数show_attribute和store_attribute的传入参数。
134行,关联了属性数组,属性数组在119行定义,第120-122行属性又分别在114-117行定义。
一切准备就绪之后,在模块初始化地方调用子系统注册函数之后,就可以在/sys/kernel/config/下看到创建了子系统了。

407static int __init configfs_example_init(void)
408{
409    int ret;
410    int i;
411    struct configfs_subsystem *subsys;
412
413    for (i = 0; example_subsys[i]; i++) {
414          subsys = example_subsys[i];
415
416          config_group_init(&subsys->su_group);
417          mutex_init(&subsys->su_mutex);
418          ret = configfs_register_subsystem(subsys);
419          if (ret) {
420              printk(KERN_ERR "Error %d while registering subsystem %s\n",
421                      ret,
422                      subsys->su_group.cg_item.ci_namebuf);
423              goto out_unregister;
424          }
425    }
426
427    return 0;

416-417行,初始化子系统。
418行,注册子系统。
这里做个小结:
1、创建子系统struct configfs_subsystem
2、创建子系统下config_item_type,对应位置configfs_subsystem->config_group->config_item->config_item_type
3、创建config_item_type对应的属性数组和操作,操作主要是show_attribute和store_attribute
4、注册子系统configfs_register_subsystem

linux
相关资讯       configfs 
本文评论   查看全部评论 (0)
表情: 表情 姓名: 字数

       

评论声明
  • 尊重网上道德,遵守中华人民共和国的各项有关法律法规
  • 承担一切因您的行为而直接或间接导致的民事或刑事法律责任
  • 本站管理人员有权保留或删除其管辖留言中的任意内容
  • 本站有权在网站内转载或引用您的评论
  • 参与本评论即表明您已经阅读并接受上述条款