gpt4 book ai didi

c - 从内核模块使用 sysfs 时出现未知符号错误

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

嘿,我正在尝试使用 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"); 的模块访问。查看Linux Kernel FAQ有关更多信息。如果您的模块供内部使用,或者您打算以开源方式分发,这应该不是问题。否则,您可能需要重新考虑如何与内核交互。

关于c - 从内核模块使用 sysfs 时出现未知符号错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5194495/

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