简体   繁体   English

从内核模块创建sysfs条目

[英]Create sysfs entry from kernel module

I want to pass a string > 1024 chars to my module (filesystem). 我想将> 1024个字符串传递给我的模块(文件系统)。 As kernel parameters are limited to 1024 chars, someone recommended to use sysfs instead. 由于内核参数限制为1024个字符,因此有人建议使用sysfs。

I tried to include this example in my super.c class to create a string 'filename' & string 'code' entry in sysfs for my module. 我试图在我的super.c类中包含这个例子,在我的模块的sysfs中创建一个字符串'filename'和字符串'code'条目。

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",
};

When compiling my module I get a lot of errors (line 41 is decl_subsys): 在编译我的模块时,我遇到了很多错误(第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. Is this tutorial just outdated for my 3.5 kernel or am I missing something else? 这个教程刚刚过时我的3.5内核还是我遗漏了别的东西?
  2. How could I create 2 char string entries for my module in sysfs? 如何在sysfs中为我的模块创建2个char字符串条目?

Here is the source code for transmitting data to a "param_buf" string. 以下是将数据传输到“param_buf”字符串的源代码。 As requested, without read method. 根据要求,没有读取方法。 Only store. 只有商店。

#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);

You can test it in such a way: 你可以用这样的方式测试它:

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

For two string entries: copy a store_value function, register one more store_val_attribute and put it to attributes list. 对于两个字符串条目:复制store_value函数,再注册一个store_val_attribute并将其放入属性列表。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM