I wrote one character device which should create network device. When I do insmod it gave a segmentation fault and lsmod shows initializing.
A dmesg shows:
EIP is at init_module [testdriver] 0xa6 (2.4.18-14)
eax: 00000000 ebx: ffffffea ecx: c05b6000 edx: c8e7f4d3
esi: 00000000 edi: 00000000 ebp: c672df18 esp: c672df00
ds: 0018 es: 0018 ss: 0018
Process insmod (pid: 9922, stackpage=c672d000)
Stack: 00200206 ffffffff 00002f3f c05b6940 00000000 00000000 c8e7f000 c011bf79
c8e7f060 08075228 00000908 00000000 080756f4 0000052c 00000060 00000060
0000000a c05b6c40 c6231000 c6e7e000 00000060 c8def000 c8e7f060 00000968
Call Trace: [<c011bf79>] sys_init_module [kernel] 0x4d9 (0xc672df1c))
[<c8e7f060>] test_open [testdriver] 0x0 (0xc672df20))
[<c8e7f060>] test_open [testdriver] 0x0 (0xc672df58))
[<c010910f>] system_call [kernel] 0x33 (0xc672dfc0))
Code: c7 40 34 fe f0 e7 c8 83 ec 08 68 d4 f4 e7 c8 8b 45 f4 ff 30
I am attaching the code, pls help me to solve this problem. Thanks a lot, -Ravi
#ifndef __KERNEL__ # define __KERNEL__ #endif #ifndef MODULE # define MODULE #endif #define DEVNAME "testdev" #include <linux/module.h> #include <linux/kernel.h> #include <linux/fs.h> #include <linux/slab.h> #include <linux/netdevice.h> #include <linux/if_ether.h> int test_open(struct inode *,struct file *); int test_release(struct inode *,struct file*); struct test_struct{ struct net_device *dev; char name[IFNAMSIZ]; }; struct file_operations test_fop={ llseek: NULL, read: NULL, write: NULL, ioctl: NULL, open: test_open, release: test_release, }; int test_open(struct inode *inode, struct file *filep) { MOD_INC_USE_COUNT; return 0; } int test_release(struct inode *inode, struct file *filep) { MOD_DEC_USE_COUNT; return 0; } int testdev_open(struct net_device *dev) { MOD_INC_USE_COUNT; return 0; } int testdev_close(struct net_device *dev) { unregister_netdev(dev); MOD_DEC_USE_COUNT; return 0; } static int test_init_dev(struct net_device *dev) { struct test_struct *c; dev->priv = kmalloc(sizeof(struct net_device),GFP_KERNEL); if(!dev->priv) { printk(KERN_ERR "out of memory while allocating to dev->priv\n"); return -ENOMEM; } c = (struct test_struct *)dev->priv; if(!c) return -ENODEV; memset(c,0,sizeof(struct test_struct)); c->dev = dev; ether_setup(dev); dev->open = testdev_open; dev->stop = testdev_close; dev->mtu = ETH_DATA_LEN; dev_init_buffers(dev); return 0; } int init_module(void) { int result,i; struct test_struct *pTestStruct; result = register_chrdev(16,"test",&test_fop); if( result < 0 ){ printk(KERN_WARNING "test: cant get major\n"); return result; } pTestStruct = (struct test_struct *)kmalloc(sizeof( struct test_struct),GFP_KERNEL); if(!pTestStruct) { printk(KERN_ERR "failed to allocate memory\n"); return -ENOMEM; } memset((void *)pTestStruct,0,sizeof(pTestStruct)); sprintf(pTestStruct->name, DEVNAME); //pTestStruct->dev->priv = pTestStruct; pTestStruct->dev->init = test_init_dev; i = dev_alloc_name(pTestStruct->dev,"testdev%d"); if(i < 0 ) { printk(KERN_ERR "cant allocate device\n"); return -ENODEV; } i = register_netdevice((pTestStruct->dev)); if( i < 0 ) printk(KERN_ERR "register netdevice failed\n"); return 0; } void cleanup_module(void) { unregister_chrdev(16,"test"); }