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

#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!~

所以要细心一点。。。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值