内核模块里的一点错误直接导致系统崩溃

来源:互联网 发布:电视直播软件哪个最好 编辑:程序博客网 时间:2024/05/20 07:58
#include <linux/module.h>
#include <linux/init.h>
#include <linux/device.h>
#include <linux/sysfs.h>


MODULE_LICENSE("Dual BSD/GPL");
MODULE_AUTHOR("Anfo");

ssize_t show_def(struct bus_type *bus,char *buf){
    buf="what?";
    return 0;
}
ssize_t store_def(struct bus_type *bus,const char *buf,size_t count){
    return 0;
}

static struct device_attribute dev_attrs[] = {  
  __ATTR(demo, 0764, show_def, store_def),  
  __ATTR_NULL,  
};

static struct bus_type MyBus={
    .name="MyBus",
    .dev_attrs=dev_attrs
};

static struct device MyDev={
    .init_name="Coffee",
    .bus=&MyBus
};

static int __init helloModule_init(void){
    printk("hello world!\n");
    if(bus_register(&MyBus))
    {
        printk("bus_register error!\n");
        bus_unregister(&MyBus);
    }
    printk("bus_register success!\n");
    if(device_register(&MyDev))
    {
        printk("device_register error!\n");
        device_unregister(&MyDev);
    }
    printk("device_register success!\n");
    return 0;
}

static void __exit helloModule_exit(void){
    device_unregister(&MyDev);
    printk("device_unregister success!\n");
    bus_unregister(&MyBus);
    printk("bus_unregister success!\n");
    printk("bye world!\n");
}

module_init(helloModule_init);

module_exit(helloModule_exit);


上面的程序是有问题的。 在insmod与rmmod之后发现 内核警告必须增加 release函数。。

当然傻逼的我一开始还先注销bus再注销device直接导致系统崩掉,或者再注册device的时候填了bus。。。Oh my god!~

所以要细心一点。。。

0 0