Thanks for the response to everyone. I am attaching my program files.
Here are the steps I followed.
1. first insmod module
2. get major number from /proc/devices
3. create device node with major number
4. use the test program to see whether the printk messages are printed to
/var/log/messages
5. rmmod module
6. again use test program to open the device. But this time it gives seg
fault.
7. Later When I try to insert module the whole system freezes.This is the kernel I am using Linux localhost.localdomain 2.6.24.7-92.fc8 #1 SMP Wed May 7 16:50:09 EDT 2008 i686 i686 i386 GNU/Linux Sri On Mon, Nov 17, 2008 at 11:34 AM, Belisko Marek <[EMAIL PROTECTED]>wrote: > Hi, > > > On Sun, Nov 16, 2008 at 5:36 AM, Sri Ram K Vemulpali > <[EMAIL PROTECTED]> wrote: > > Hi all, > > > > I am writing a kernel module. Where in the module, major number > is > > allocated on fly (alloc_chrdev_region). And using major number from > > /proc/devices I create a device node in /dev (mknod). To see what happens > if > > I create device node with major number first and then asking the kernel > to > > create same major number on fly when I use insmod(kernel is picking the > > major number on fly what I used for device node). When I type insmod the > > whole kernel is freezing. > No Oops? > Could you please send some code to check? > Do you think the kernel should avoid such > > situation gracefully. Any comments would be helpful. Thank you. > > > > > > Sri. > > > > > > > > > > > thanks, > > Marek > > -- > as simple as primitive as possible > ---------------------------------------------- > Marek Beliško - open-nandra > Ruská Nová Ves 219 > 08005 Prešov > Slovakia > http://open-nandra.com >
#include <linux/init.h>
#include <linux/module.h>
#include "char_dev.h"
MODULE_LICENSE("Dual BSD/GPL");
unsigned int device_range = 3;
char *device_name = "sri";
extern struct file_operations fops;
static int hello_init(void)
{
if(!char_cdev_init(&dev,&fops,&cdev_ops))
printk(KERN_ALERT"Init Failed\n");
printk(KERN_ALERT"Hello World\n");
return 0;
}
static void hello_exit(void)
{
unregister_chrdev_region(dev,device_range);
printk(KERN_ALERT "GoodBye, Cruel World\n");
}
//////////////////////////////////////////
//
// this function allocates devices from range 0 to 3
// and initializes the cdev operations
//
//
int char_cdev_init(dev_t *devno, struct file_operations *fops, struct cdev *cdevfops)
{
if(alloc_chrdev_region(devno,0,device_range,device_name) < 0)
return 0;
//init cdev struct
cdev_init(cdevfops,fops);
cdevfops->owner = THIS_MODULE;
cdevfops->ops = fops;
if(cdev_add(cdevfops, *devno,1) < 0)
return 0;
return 1;
}
module_init(hello_init);
module_exit(hello_exit);
#include <linux/fs.h>
#include <linux/module.h>
#include "chardev_fops.h"
//declare file operations
struct file_operations fops = { .owner = THIS_MODULE,
.read = chardev_read,
.write = chardev_write,
.open = chardev_open,
};
///////////////////////////////////////
//
// preconditions: open should be called on the device
//
// postconditions: get the chardev struct and store it in private_data
// field for access to other functions
// check the access permissions based on mode do
// required operation like cleaning the memory or initializing the
// memory
//
// arguments: 1. inode 2. file
//
// returns: 0 if success
int chardev_open(struct inode *inodep, struct file *filep)
{
unsigned int minor_num;
unsigned int major_num;
printk(KERN_ALERT "From Open Method Call\n");
//based on the type of access we need initialize the
//buffer
return 0;
}
///////////////////////////////////////
//
// preconditions: device must be opened
//
// postconditions: reads from the buffer the specified number of bytes
// and copies to user, if requested number of bytes are not
// present in the buffer then copies available bytes and returns
// num of bytes copied and other conditions 0
//
//
// arguments: 1. file 2. user buffer addr 3. num bytes 4. offset where user is at in buffer
//
// returns: num bytes read
//
ssize_t chardev_read(struct file *filep, char __user* user_buf, size_t count, loff_t *off)
{
unsigned int bytes_read = 0;
return bytes_read;
}
//////////////////////////////////////////////////
//
// preconditions: device must be opened
//
// postconditions: writes the given user buf data in to memory by copying
// from user space, if allocated buffer size exceeds then writes
// only the data fit in the buffer and returns the num bytes written
//
// arguments: 1. file 2. user buff addr 3. num bytes to write 4. offset from where to write
//
// returns: num bytes written
//
ssize_t chardev_write(struct file *filep,const char __user *user_buf, size_t count, loff_t *off)
{
unsigned int bytes_written = 0;
return bytes_written;
}
Makefile
Description: Binary data
char_dev.h
Description: Binary data
chardev_fops.h
Description: Binary data
#include <fcntl.h>
#include <sys/types.h>
int main()
{
char *path = "/dev/sri0";
int fd = 0;
fd = open(path,O_RDONLY);
close(fd);
return 0;
}
