简体   繁体   English

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

[英]Unknown symbol error when using sysfs from kernel module

Hey, I'm trying to play around a with sysfs a little bit, to get some data from user space into my kernel module. 嘿,我正在尝试使用sysfs进行一些操作,以将一些数据从用户空间获取到我的内核模块中。 Here is the code: 这是代码:

#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);

When I compile my module, all works fine, but when I try to run it I get a insmod: error inserting 'mytester.ko': -1 Unknown symbol in module 当我编译模块时,一切正常,但是当我尝试运行它时,我得到一个insmod:在模块中插入“ mytester.ko”时出错:-1未知符号

Using dmesg I get more details: 使用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)

And that's the point. 这就是重点。 I don't understand this message, because both kobject.h and sysfs.h are included. 我不明白此消息,因为同时包含了kobject.h和sysfs.h。 So I can't really understand what is happening here. 所以我真的不明白这里发生了什么。 Even if I comment out my whole function mystore to a simple return (as shown) the error is the same. 即使我将整个函数mystore注释为简单的返回值(如图所示),错误也是一样的。 So the error is not elsewhere.... 因此错误不在其他地方。

The sysfs_remove_file and other functions in your example are exported GPL only, and can only be accessed from a module marked with MODULE_LICENSE("GPL"); 您的示例中的sysfs_remove_file和其他函数仅导出为GPL,并且只能从标有MODULE_LICENSE("GPL");的模块中进行访问MODULE_LICENSE("GPL"); . See the Linux Kernel FAQ for some more on this. 有关更多信息,请参见Linux Kernel FAQ If your module is for in-house use, or you plan to distribute as open source, this shouldn't be a problem. 如果您的模块是供内部使用的,或者您打算以开放源代码的形式分发,那么这应该不是问题。 Otherwise, you may need to reconsider how you interface to the kernel. 否则,您可能需要重新考虑如何与内核接口。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM