簡體   English   中英

從內核模塊使用sysfs時出現未知的符號錯誤

[英]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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM