从内核模块创建sysfs条目

时间:2014-04-24 21:58:25

标签: c linux linux-kernel kernel-module sysfs

我想传递一个字符串> 1024个字符到我的模块(文件系统)。 由于内核参数限制为1024个字符,someone recommended代替使用sysfs。

我尝试在我的super.c类中包含this example来创建一个字符串' filename' &安培;字符串'代码'在我的模块的sysfs中输入。

static decl_subsys(myfs, NULL, NULL);

struct myfs_attr {
    struct attribute attr;
    char *value;
};

static struct myfs_attr fname = {
    .attr.name="filename",
    .attr.owner = THIS_MODULE,
    .attr.mode = 0644,
    .value = "/my/test/path",
};

static struct myfs_attr code = {
    .attr.name="code",
    .attr.owner = THIS_MODULE,
    .attr.mode = 0644,
    .value = "0101",
};

编译模块时,我遇到很多错误(第41行是decl_subsys):

fs/myfs/super.c:41:26: error: expected ‘)’ before ‘(’ token
fs/myfs/super.c:50:2: error: unknown field ‘owner’ specified in initializer
fs/myfs/super.c:50:2: warning: initialization from incompatible pointer type [enabled by default]
fs/myfs/super.c:50:2: warning: (near initialization for ‘fname.attr.name’) [enabled by default]
...
fs/myfs/super.c: At top level:
fs/myfs/super.c:83:15: error: variable ‘myfsops’ has initializer but incomplete type
fs/myfs/super.c:84:2: error: unknown field ‘show’ specified in initializer
fs/myfs/super.c:84:2: warning: excess elements in struct initializer [enabled by default]
fs/myfs/super.c:84:2: warning: (near initialization for ‘myfsops’) [enabled by default]
fs/myfs/super.c:85:2: error: unknown field ‘store’ specified in initializer
fs/myfs/super.c:85:2: warning: excess elements in struct initializer [enabled by default]
fs/myfs/super.c:85:2: warning: (near initialization for ‘myfsops’) [enabled by default]
fs/myfs/super.c:89:2: error: unknown field ‘myfs_ops’ specified in initializer
fs/myfs/super.c:89:2: warning: initialization from incompatible pointer type [enabled by default]
fs/myfs/super.c:89:2: warning: (near initialization for ‘myfstype.release’) [enabled by default]
fs/myfs/super.c: In function ‘init_myfs_fs’:
fs/myfs/super.c:1554:2: error: implicit declaration of function ‘kobj_set_kset_s’ [-Werror=implicit-function-declaration]
fs/myfs/super.c:1554:19: error: ‘myfs_subsys’ undeclared (first use in this function)
fs/myfs/super.c:1554:19: note: each undeclared identifier is reported only once for each function it appears in
fs/myfs/super.c:1554:32: error: ‘fs_subsys’ undeclared (first use in this function)
fs/myfs/super.c:1557:2: error: implicit declaration of function ‘subsystem_register’ [-Werror=implicit-function-declaration]
fs/myfs/super.c: In function ‘exit_myfs_fs’:
fs/myfs/super.c:1579:2: error: implicit declaration of function ‘subsystem_unregister’ [-Werror=implicit-function-declaration]
fs/myfs/super.c:1579:24: error: ‘myfs_subsys’ undeclared (first use in this function)
  1. 本教程刚刚过时使用我的3.5内核还是我遗漏了其他内容?
  2. 如何在sysfs中为我的模块创建2个char字符串条目?

1 个答案:

答案 0 :(得分:3)

以下是将数据传输到“param_buf”字符串的源代码。根据要求,没有读取方法。只存储。

#include <linux/init.h>
#include <linux/module.h>
#include <linux/slab.h>
#include <asm/string.h>

static struct kobject *register_kobj;
static char *param_buf;

// function for many symbol data enter
static ssize_t __used store_value(struct kobject *kobj, struct kobj_attribute *attr, const char *buf, size_t count){ 
    printk(KERN_ALERT "you entered %s\n", buf);
    strncpy(param_buf, buf, PAGE_SIZE - 1);
    return count;
}

// register function to attribute
static struct kobj_attribute store_val_attribute = __ATTR( put_parameters, 0220, NULL, store_value);

// put attribute to attribute group
static struct attribute *register_attrs[] = {
    &store_val_attribute.attr,
    NULL,   /* NULL terminate the list*/
};
static struct attribute_group  reg_attr_group = {
    .attrs = register_attrs
};

static int hello_init(void){
    param_buf = kzalloc(PAGE_SIZE, GFP_KERNEL);
    // create sysfs object ( /sys/kernel/test_1025_sym directory )
    register_kobj = kobject_create_and_add("test_1025_sym", kernel_kobj);
    if (!register_kobj)
    return -ENOMEM;

    //create attributes (files)
    if(sysfs_create_group(register_kobj, &reg_attr_group)){
        kobject_put(register_kobj);
        return -ENOMEM;
    }

    return 0;
}

static void hello_exit(void){
    printk(KERN_ALERT "last value was %s\n", param_buf);
    kfree(param_buf);
    kobject_put(register_kobj);
}

MODULE_LICENSE("Dual BSD/GPL");
module_init(hello_init);
module_exit(hello_exit);

你可以用这样的方式测试它:

cat /etc/fstab > /sys/kernel/test_1025_sym/put_parameters

对于两个字符串条目:复制store_value函数,再注册一个store_val_attribute并将其放入属性列表。