2011-03-04 31 views
2

嘿, 我想用sysfs稍微玩一下,從用戶空間獲取一些數據到我的內核模塊中。 下面是代碼:從內核模塊使用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註釋爲簡單返回(如圖所示),錯誤也是一樣的。所以錯誤不在其他地方......

回答

7

您的示例中的sysfs_remove_file和其他函數僅導出GPL,並且只能從標記爲MODULE_LICENSE("GPL");的模塊訪問。有關更多信息,請參見Linux Kernel FAQ。如果您的模塊用於內部使用,或者您計劃以開源方式分發,這應該不成問題。否則,您可能需要重新考慮如何連接到內核。

+0

謝謝,因爲我剛剛瞭解這一點,我沒有照顧這樣的事情obv :) 但除此之外:任何人都可以看到代碼中的錯誤(尤其是kobject和sysfil創建)?因爲現在它會被段錯誤(可能是在kobject_init_and_add調用中)殺死 – michael 2011-03-04 14:45:46

相關問題