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

标签 c linux module kernel

嘿, 我正在尝试使用 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/

相关文章:

Java 将 xml 文件发布到带有身份验证的 url

javascript - 如何使用 ECMAScript 6 模块导入 PlotlyJS

perl - 我如何告诉 Module::Build 不要将 lib/perl5 子目录添加到安装路径?

c - 在伽罗华域算法中优化 y = x*x

python - 可扩展供应协议(protocol)实现?

c - 在邻接矩阵中随机添加 INFTY 值

c - c语言中求最大数和最小数的方法

java - 如何在 Rhel 5 终端中运行 Java 文件?

linux - 如何在oracle linux 7.2中安装postgresql

html - Dotnetnuke - 在创建新页面时选择哪些 Pane 中包含 HTML 模块