Create sysfs entry from kernel module

蹲街弑〆低调 提交于 2020-01-02 09:09:23

问题


I want to pass a string > 1024 chars to my module (filesystem). As kernel parameters are limited to 1024 chars, someone recommended to use sysfs instead.

I tried to include this example in my super.c class to create a string 'filename' & string 'code' entry in sysfs for my module.

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):

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?
  2. How could I create 2 char string entries for my module in sysfs?

回答1:


Here is the source code for transmitting data to a "param_buf" string. 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.



来源:https://stackoverflow.com/questions/23280635/create-sysfs-entry-from-kernel-module

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!