问题描述
我想传递一个字符串> 1024个字符到我的模块(文件系统)。
由于内核参数被限制为1024个字符,someone推荐使用的sysfs来代替。
我想包括在我super.c类来创建一个字符串'名'和;字符串'code'sysfs中的条目我的模块。
静态decl_subsys(myfs,NULL,NULL);结构myfs_attr {
结构属性ATTR;
字符*值;
};静态结构myfs_attr FNAME = {
.attr.name =文件名,
.attr.owner = THIS_MODULE,
.attr.mode = 0644,
.value的=/我/测试/路径
};静态结构myfs_attr code = {
.attr.name =code,
.attr.owner = THIS_MODULE,
.attr.mode = 0644,
.value的=0101,
};
当编译我的模块我得到了很多错误(第41行是decl_subsys):
FS / myfs / super.c:41:26:错误:之前的预期')''('令牌
FS / myfs / super.c:50:2:错误:初始值设定项未知领域的所有者
FS / myfs / super.c:50:2:警告:默认情况下启用]从兼容的指针类型初始化
FS / myfs / super.c:50:2:警告:(近初始化fname.attr.name')[默认启用]
...
FS / myfs / super.c:在顶层:
FS / myfs / super.c:83:15:错误:变量'myfsops'有初始值设定但不完全类型
FS / myfs / super.c:84:2:错误:未知的字段'显示'初始值设定项
FS / myfs / super.c:84:2:警告:在结构初始化多余的元素[默认启用]
FS / myfs / super.c:84:2:警告:(近初始化myfsops')[默认启用]
FS / myfs / super.c:85:2:错误:未知领域'商店'初始值设定项
FS / myfs / super.c:85:2:警告:在结构初始化多余的元素[默认启用]
FS / myfs / super.c:85:2:警告:(近初始化myfsops')[默认启用]
FS / myfs / super.c:89:2:错误:未知的领域myfs_ops初始值设定项
FS / myfs / super.c:89:2:警告:默认情况下启用]从兼容的指针类型初始化
FS / myfs / super.c:89:2:警告:(近初始化myfstype.release')[默认启用]
FS / myfs / super.c:在init_myfs_fs功能:
FS / myfs / super.c:1554:2:错误:函数隐式声明kobj_set_kset_s'[-Werror =隐函数声明]
FS / myfs / super.c:1554:19:错误:'myfs_subsys'未申报(第一次使用此功能使用)
FS / myfs / super.c:1554:19:注意:每个未声明的标识符为它出现在每个函数报道只有一次
FS / myfs / super.c:1554:32:错误:'fs_subsys'未申报(第一次使用此功能使用)
FS / myfs / super.c:1557:2:错误:函数subsystem_register'隐式声明[-Werror =隐函数声明]
FS / myfs / super.c:在函数'exit_myfs_fs:
FS / myfs / super.c:1579:2:错误:函数subsystem_unregister'隐式声明[-Werror =隐函数声明]
FS / myfs / super.c:1579:24:错误:'myfs_subsys'未申报(第一次使用此功能使用)
- 是本教程只是过时的我3.5的内核还是我失去了什么东西?
- 我怎么能在sysfs中创建我的模块2字符字符串项?
下面是源$ C $ C发送数据到param_buf字符串。按照要求,没有read方法。只有商店。
的#include< Linux的/ init.h中>
#包括LT&; Linux的/ - module.h中GT;
#包括LT&; Linux的/ slab.h>
#包括LT&; ASM / string.h中>静态结构的kobject * register_kobj;
静态字符* param_buf;//功能对于很多符号数据输入
静态ssize_t供__used store_value(结构的kobject * Kobj的数据,结构kobj_attribute * ATTR,为const char * buf中,为size_t计数){
printk的(KERN_ALERT您输入%S \\ n,BUF);
函数strncpy(param_buf,BUF,PAGE_SIZE - 1);
返回计数;
}//注册功能属性
静态结构kobj_attribute store_val_attribute = __ATTR(put_parameters,0220,NULL,store_value);//把属性属性组
静态结构属性* register_attrs [] = {
&安培; store_val_attribute.attr,
NULL,/ * NULL结束列表* /
};
静态结构attribute_group reg_attr_group = {
.attrs = register_attrs
};静态INT hello_init(无效){
param_buf = kzalloc(PAGE_SIZE,GFP_KERNEL);
//创建的sysfs对象(/ SYS /内核/ test_1025_sym目录)
register_kobj = kobject_create_and_add(test_1025_sym,kernel_kobj);
如果(!register_kobj)
返回-ENOMEM; //创建属性(文件)
如果(sysfs_create_group(register_kobj,&放大器; reg_attr_group)){
kobject_put(register_kobj);
返回-ENOMEM;
} 返回0;
}静态无效hello_exit(无效){
printk的(KERN_ALERT最后一个值是%S \\ n,param_buf);
kfree(param_buf);
kobject_put(register_kobj);
}MODULE_LICENSE(双BSD / GPL);
宏module_init(hello_init);
宏module_exit(hello_exit);
可以以这样的方式进行测试:
猫/ etc / fstab文件> / SYS /内核/ test_1025_sym / put_parameters
有关两个字符串项:复制一个store_value功能,注册一个更store_val_attribute并把它的属性列表
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)
- Is this tutorial just outdated for my 3.5 kernel or am I missing something else?
- How could I create 2 char string entries for my module in sysfs?
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, ®_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.
这篇关于创建内核模块的sysfs入口的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!