Question

I am writing a character device driver. In the sample code which I found over internet, mentions that we need to attach some file operations to this character device. In those file_operations there is one function named as open. But in that open call, there are not doing anything significant. But if we want to use this character device, first we need to open the device and then only we can read/write anything on it. So I want to know how open() call is working exactly. Here is the link I am referring for character device driver : http://appusajeev.wordpress.com/2011/06/18/writing-a-linux-character-device-driver/

Was it helpful?

Solution

The sequence for open() on the user side is very straightforward: it will invoke sys_open() on the kernel path, will do some path resolution and permission checking, then will path everything its got to dev_open() (and would not do anything else).

dev_open() gets parameters you have passed to it through the open() system call (+ quite a lot of information specific to kernel vfs subsystem, but this is rarely of concern).

Notice, that you're getting struct file parameter passed in. It has several useful fields:

struct file {
   ....
   struct path        f_path; // path of the file passed to open()
   ....
   unsigned int       f_flags; // 'flags' + 'mode' as passed to open()
   fmode_t            f_mode;  // 'mode' as set by kernel (FMODE_READ/FMODE_WRITE)
   loff_t             f_pos;   // position in file used by _llseek
   struct fown_struct f_owner; // opening process credentials, like uid and euid
   ....
}

The rest you can dig out yourself by checking out examples in the source.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top