Question

I have registered a character driver using the api register_chrdrv().

The driver creates an entry in /dev/myfile. User space applications can access my driver by opening the /dev/myfile. Now I want to make my character driver such that only one user space application can access my driver at a time; only when that user space application closes the file can another user space program access my driver.

Can this be done in the kernel level?

I don't have control of the number of applications running in user space so i have to make changes only in kernel level.

Was it helpful?

Solution

In the open method of your driver: use a global flag to say whether the device is in use. Unset when closed (in the release method). Pseudo-code:

am_open=False

open() method:
if (!am_open)
  am_open=True
  ...proceed...
else
  return -EAGAIN; // try again later
 -or-
  return -EBUSY;


release() method:
  am_open=False

Having said that, drivers (usually) should be a 100% reentrant :)

OTHER TIPS

With a kernel char device you usually have the following methods.

.open
.read
.write
.release

(struct file_operations fops which will be used in the method register_chrdev(...,&fops).)

I would create your clients in a way that they open the device when they get started, and release it upon exiting. Like this you could easily handle / hinder a new (second) program of doing reads and writes on this device.

There may be other possibilities as well.

As per your question you want to synchronize your application so that at a time only one process can access it. You can go for semaphore in kernel or locking methods in drivers that may help you. You may refer this http://www.cs.fsu.edu/~baker/devices/lxr/http/source/linux/kernel/semaphore.c

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