mic*_*ael 2 c linux kernel module
嘿,我正在尝试使用sysfs一点点,从用户空间获取一些数据到我的内核模块.这是代码:
#include <linux/kernel.h>
#include <linux/module.h>
#include <linux/init.h>
#include <linux/elf.h>
#include <linux/fs.h>
#include <linux/slab.h>
#include <linux/kobject.h>
#include <linux/sysfs.h>
#define PRINT(x) printk(KERN_INFO x "\n")
#define ERROR(fmt,arg...) printk(KERN_ALERT "Error - " fmt,##arg)
ssize_t mystore (struct kobject *obj,
struct attribute *attr, const char *buff, size_t count)
{
/* doing a little bit */
return count;
}
ssize_t myshow (struct kobject *obj, struct attribute *attr, char *buff)
{
return 0;
}
char file_name[] = "myfile";
static struct attribute myattribute = {
.name = file_name,
.mode = S_IRUSR | S_IWUSR
};
static struct sysfs_ops mysysfs_ops = {
.show = myshow,
.store = mystore
};
static struct kobj_type mykobj_type = {
.sysfs_ops = &mysysfs_ops,
};
static struct kobject mykobject = {
.name = "mykobject",
.ktype = &mykobj_type
};
static int __init start(void)
{
int tmp;
PRINT("Initializing module\n");
if (kobject_init_and_add(&mykobject, &mykobj_type, NULL, "mykobj") != 0) {
ERROR ("Digsig key failed to register properly\n");
return -1;
}
if ((tmp = sysfs_create_file(&mykobject, &myattribute)) != 0) {
ERROR ("Create file failed\n");
return -1;
}
PRINT("INIT CORRECT");
return 0;
}
static void __exit close(void)
{
PRINT("Deinitializing module\n");
sysfs_remove_file(&mykobject, &myattribute);
kobject_del(&mykobject);
}
module_init(start);
module_exit(close);
Run Code Online (Sandbox Code Playgroud)
当我编译我的模块时,一切正常,但是当我尝试运行它时,我得到一个insmod:插入'mytester.ko'时出错:-1模块中的未知符号
使用dmesg我得到更多细节:
[18335.892462] mytester: Unknown symbol sysfs_remove_file (err 0)
[18335.892462] mytester: Unknown symbol sysfs_create_file (err 0)
[18335.892646] mytester: Unknown symbol kobject_init_and_add (err 0)
Run Code Online (Sandbox Code Playgroud)
这就是重点.我不明白这条消息,因为包含了kobject.h和sysfs.h.所以我真的不明白这里发生了什么.即使我将我的整个函数mystore注释掉一个简单的返回(如图所示),错误也是一样的.所以错误不在其他地方....
在sysfs_remove_file
你的榜样和其他功能仅出口GPL,并且只能从标有模块访问MODULE_LICENSE("GPL");
.有关此内容的更多信息,请参阅Linux内核常见问题解答.如果您的模块是供内部使用,或者您计划作为开源分发,这应该不是问题.否则,您可能需要重新考虑如何与内核进行交互.
归档时间: |
|
查看次数: |
4248 次 |
最近记录: |