I'm trying to learn how to write device drivers for kernel 2.6. I cam across the following piece of code, part of a module which is supposed to create a /proc file, and if several processes try to open it at the same time, put all but one to sleep.
Code:
static int module_open(struct inode *inode, struct file *file)
{
/*
* If the file's flags include O_NONBLOCK, it means the process doesn't
* want to wait for the file. In this case, if the file is already
* open, we should fail with -EAGAIN, meaning "you'll have to try
* again", instead of blocking a process which would rather stay awake.
*/
if ((file->f_flags & O_NONBLOCK) && Already_Open)
return -EAGAIN
try_module_get(THIS_MODULE);
/* If the file is already open, wait until it isn't */
while (Already_Open) {
int i, is_sig = 0;
/*
* This function puts the current process, including any system
* calls, such as us, to sleep. Execution will be resumed right
* after the function call, either because somebody called
* wake_up(&WaitQ) (only module_close does that, when the file
* is closed) or when a signal, such as Ctrl-C, is sent
* to the process
*/
wait_event_interruptible(WaitQ, !Already_Open);
/*
* If we woke up because we got a signal we're not blocking,
* return -EINTR (fail the system call). This allows processes
* to be killed or stopped.
*/
for (i = 0; i < _NSIG_WORDS && !is_sig; i++)
is_sig =
current->pending.signal.sig[i] & ~current->
blocked.sig[i];
if (is_sig) {
module_put(THIS_MODULE);
return -EINTR;
}
}
/* Open the file */
Already_Open = 1;
return 0; /* Allow the access */
}
Now, I don't understand when this function
module_open is called. How can I demonstrate to myself an example of what this module does? That is, how to "open the file" so that another one can't access it and goes to sleep so that I have to Ctrl-C ?
Thanks