从内核模块创buildsysfs条目

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

我试图在我的super.c类中包含这个例子,在我的模块的sysfs中创build一个string'filename'和string'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", }; 

编译我的模块时,我得到了很多错误(第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内核还是缺less别的东西?
  2. 我怎么能在sysfs中为我的模块创build2个charstring条目?

Solutions Collecting From Web of "从内核模块创buildsysfs条目"

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