Issue
This is a newbie kernel module question... I have mymodule.c with a function:
static int mymodule_open(struct inode *inode, struct file *filp)
{
//printk(KERN_INFO "open called\n");
/* Success */
return 0;
}
and a user level program where the first line after variable initializations is:
FILE *pFile = fopen("/dev/mymodule", "r+");
When I run the user level program this fopen
somehow calls the mymodule_open
command in mymodule.c (compiled to mymodule.ko). How does it know to do this? I can't connect the dots as to how mymodule_open()
knows when fopen
opens up /dev/mymodule
.
Solution
There is a module registration mechanism in kernel for device driver or kernel modules.
/dev/module
will be linked with your module.
The Device operations and file operations structure is mapped with the device file.
something like
struct file_operations fops = {
open : my_module_open,
release : my_module_release,
ioctl : my_module_ioctl,
};
Device file will identify and open the module with help of major and minor number.First with device file and then file operations structure.
Also look into device registration
and device file operations
Answered By - Dilip Kumar