How to open a device for exclusive access?

Dave Johnson dave-gnhlug at davej.org
Sat Jul 14 15:46:45 EDT 2007


Steven W. Orr writes:
> I have a device (it happens to be a somewhat exotic serial port) which is 
> managed by a server process. I want my server to detect whether another 
> instance of that server already has that device open. I'm guaranteed that 
> no other program other that the one I'm in control of is capable of 
> opening that particular device.
> 
> I was hoping that I could maybe not have to use either mandatory or 
> advisory file locking. I have tried O_EXCL | O_RDWR | O_NONBLOCK when 
> opening the device node hoping to get back EBUSY or EAGAIN.
> 
> Does anyone know if I'm SOOL or is there a way to do it?

Block devices support O_EXCL for exclusive access (something that
fdisk can make use of to prevent mounting of a partition while it has
the device open).

Since char devices are more generic, it's up to the individual
driver to either support or not support O_EXCL on open.  The driver
can also support exclusive opens all the time if it wants.


-- 
Dave



More information about the gnhlug-discuss mailing list