LinuxQuestions.org
Visit Jeremy's Blog.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
Search this Thread
Old 05-13-2005, 06:12 AM   #1
ar1
Member
 
Registered: Nov 2003
Location: Bloomington, IL, USA
Distribution: Fedora Core 3
Posts: 126

Rep: Reputation: 15
Device driver code


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
 
Old 05-13-2005, 08:02 AM   #2
jtshaw
Senior Member
 
Registered: Nov 2000
Location: Seattle, WA USA
Distribution: Ubuntu @ Home, RHEL @ Work
Posts: 3,892
Blog Entries: 1

Rep: Reputation: 66
The module_open function is called when the module gets installed into the system. Think of this like int main() for kernel modules. Usually you would also have a corresponding module_close function that cleans up when the module is removed from the system.

The Linux Devices Drivers book is a good starting point to learning how drivers work. The free on-line version covers the 2.4 kernel, but the interface is pretty much identical.

The thrid edition is now available as well, and covers the 2.6 kernel specifically.
 
Old 05-13-2005, 08:20 AM   #3
ar1
Member
 
Registered: Nov 2003
Location: Bloomington, IL, USA
Distribution: Fedora Core 3
Posts: 126

Original Poster
Rep: Reputation: 15
Thanks for the reply jtshaw. Yeah, I know about Linux Device Drivers and am using it, the 3rd edition (like the previous ones) has also been release for free under GPL!

I have a question: the prupose of this module seems to be that when the file is opened (the module is installed as per what you said) another process should not be able to open the file. What does the author mean by that? Does he mean that another process can't open the file /proc/sleep? I can still open /proc/sleep and the process does not go to sleep :/
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
SCSI error: return code = 0x10000 on USB Mass Storage device gringer Linux - Hardware 4 05-14-2007 06:43 AM
Where can i find the device driver code on my systeM dr_zayus69 Linux - Hardware 1 10-02-2005 04:59 AM
Oreilly's linux device driver example code... okeyla Linux - Hardware 2 04-27-2005 04:58 AM
Help me understand this Driver code! fbarre Linux - Newbie 2 09-24-2003 05:10 PM
Help me understand this Driver code! fbarre Linux - General 2 09-24-2003 12:51 PM


All times are GMT -5. The time now is 12:01 AM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration