RE: Device driver query

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



You should not call sys_open() or filp_open() functions from your
module's open function, as the controls come to your module's open()
function through sys_open() system call only.

When a process calls the sys_open() system call, it further calls
filp_open() function to perform its activity and making a 'file'
structure for open file. When the control reaches the filp_open()
function, it eventually reaches the open function of your system call,
so that is why you should not call the open system call from inside you
module. The control flows as follows during open system call.

User Process -> sys_open() -> filp_open() -> ..... -> module_open()
[this is a function defined in you module] 

You need to define your file as chracter file while creating it with
mknod (mknod <device name> c <major number> <minor number>), only then
at opening time kernel will look for the your module functions
registered with kernel. Kernel keeps the pointer to file operations
structure in chrdevs[] array (kerel data structure) If the module is
registering it with kernel as file operations for chracter file/device.
At file opening time, if the fie is of character type, kernel picks this
pointer to file operations from chrdevs[] array and places it in the
'file' structure of the opened file, which actually represents the open
file in kernel.

Take note of one thing, file created like this will not be like a normal
file on hard disk, which actually is read/write by buffer mechanism of
file system and is finally written on HDD using  HDD device driver,
rather this will be a file somewhat like FIFO, which actually keeps the
data in RAM and does not put it on HDD.

I might be wrong at some places, if I am wrong please do correct me.

Gaurav


-----Original Message-----
From: linux lover [mailto:linux_lover2004@xxxxxxxxx] 
Sent: Monday, September 13, 2004 2:27 PM
To: Dhiman, Gaurav
Cc: kernelnewbies@xxxxxxxxxxxx
Subject: RE: Device driver query


Hi dhiman,
          I want to know that you mean i can use
sys_open in my kernel module to open a file through
myfile_open function call?

          can a character file be act as a normal file
to store and update that file periodically?

--- "Dhiman, Gaurav" <Gaurav.Dhiman@xxxxxx> wrote:

>  
> 
> Hi,
> 
>  
> 
> You said that you want to create a file at loading
> time and want to
> remove that file forom file system at unloading
> time, but the code you
> have pasted is actually opening a file at loading
> time, assuming that
> you already have a file in file system.
> 
>  
> 
> For reading or wrting a simple file, do following
> things:
> 
> -          Create a chracter file (using mknode)
> either dynamically (at
> loading time) or statically by mknode.
> 
> -          Do not open it at loading time, as you
> are doing right now.
> 
> -          Open it through your open function
> (myfile_open), which will
> be called from sys_open system call.
> 
> -          Do not close the file at unloading time,
> as you are doing
> right now.
> 
> -          Close the file in your function
> (myfile_release), which will
> be called form close system call.
> 
> -          Do not use filp_open() and filp_close()
> functions at all,
> they are called by open and close system calls
> before the control comes
> to your driver.
> 
>  
> 
> Regards.
> 
> Gaurav
> 
>  
> 
>   _____  
> 
> From: kernelnewbies-bounce@xxxxxxxxxxxx
> [mailto:kernelnewbies-bounce@xxxxxxxxxxxx] On Behalf
> Of linux lover
> Sent: Saturday, September 11, 2004 8:32 PM
> To: kernelnewbies@xxxxxxxxxxxx
> Subject: Device driver query
> 
>  
> 
>  i know how to write a character device driver but
> the same will not
> work for
> simple file why? that mean if i create a file
> /root/myfile and want to
> write
> and read to it periodically from a kernel module it
> is not working cause
> i am doing
> 
> .....fragment of my code.........
> static struct file_operations myfile_file_operations
> = {
>     open:       myfile_open,
>     release:    myfile_release,
>     read:       myfile_read,
>     write:      myfile_write,
> };
> 
> int init_module(void)
> {
> 
>     printk("Loading myfile Module\n");
> 
>  ent = filp_open("/root/myfile", O_RDWR,S_IRUSR |
> S_IWUSR);
>   if ( ent == NULL)
>                       printk(KERN_DEBUG, "Error
> opening file...\n");
>     return 0;
> }
> 
> void cleanup_module(void)
> {
>     printk("Unloading myfile Module\n");
> 
>     filp_close(ent,NULL);
> }
> 
> int myfile_open(struct inode *inodep, struct file
> *filp)
> {
>     printk("Opening a file....\n");
>     MOD_INC_USE_COUNT;
> 
>     return 0;
> }
> 
> int myfile_release(struct inode *inodep, struct file
> *filp)
> {
>     MOD_DEC_USE_COUNT;
>     return 0;
> }
> 
> I want whenever module is loaded it should create a
> file and when it
> unloaded should 
> remove file. but above code is fail to create a file
> and also causing
> problem in unloading
> a module with warning for arg 1 parameter of
> flip_close.
> 
> help to write a module to read/write a normal file
> from kernel module.
> 
> __________________________________________________
> Do You Yahoo!?
> Tired of spam? Yahoo! Mail has the best spam
> protection around 
> http://mail.yahoo.com 
> 
> 


__________________________________________________
Do You Yahoo!?
Tired of spam?  Yahoo! Mail has the best spam protection around 
http://mail.yahoo.com 


--
Kernelnewbies: Help each other learn about the Linux kernel.
Archive:       http://mail.nl.linux.org/kernelnewbies/
FAQ:           http://kernelnewbies.org/faq/



[Index of Archives]     [Newbies FAQ]     [Linux Kernel Mentors]     [Linux Kernel Development]     [IETF Annouce]     [Git]     [Networking]     [Security]     [Bugtraq]     [Yosemite]     [MIPS Linux]     [ARM Linux]     [Linux RAID]     [Linux SCSI]     [Linux ACPI]
  Powered by Linux