[英]Unknown symbol error when using sysfs from 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);
当我编译模块时,一切正常,但是当我尝试运行它时,我得到一个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)
这就是重点。 我不明白此消息,因为同时包含了kobject.h和sysfs.h。 所以我真的不明白这里发生了什么。 即使我将整个函数mystore注释为简单的返回值(如图所示),错误也是一样的。 因此错误不在其他地方。
您的示例中的sysfs_remove_file
和其他函数仅导出为GPL,并且只能从标有MODULE_LICENSE("GPL");
的模块中进行访问MODULE_LICENSE("GPL");
。 有关更多信息,请参见Linux Kernel FAQ 。 如果您的模块是供内部使用的,或者您打算以开放源代码的形式分发,那么这应该不是问题。 否则,您可能需要重新考虑如何与内核接口。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.