gpt4 book ai didi

从内核模块创建 sysfs 条目

转载 作者:太空狗 更新时间:2023-10-29 11:44:47 28 4
gpt4 key购买 nike

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

我试图包括 this example在我的 super.c 类中为我的模块在 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 个字符字符串条目?

最佳答案

这是将数据传输到“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并放入属性列表。

关于从内核模块创建 sysfs 条目,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23280635/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com