Can I open my own device driver at the same time from a custom Linux program?

I read somewhere that opening the same file twice has undefined semantics and should be avoided. In my situation, I would like to open my own device by associating multiple file descriptors with it multiple times. My device's file operations are safe. Is there some part of Linux between the sys call to open () and the point it invokes a registered .open () file operation that is unsafe?

+2


a source to share


3 answers


It's okay to open the same device file twice if your driver is fine. There is no hidden part that makes it insecure if it is safe in the kernel.

For example, some video applications use one process to display or capture, while another opens a device file to manipulate controls.



If the driver does not support multiple openings, then it should return an error when the second open occurs.

+3


a source


You can open the device twice in the same process, if the driver allows you to do so. The driver is responsible for synchronization.



However, if you are opening, say, a raw disk device as a root user, you will want to make sure that you are not collecting your own data in your process.

+1


a source


Opening the same file twice has well-defined semantics in cases that make sense. Processes still need some form of synchronization if they are all reading / writing, otherwise the file is likely to be filled with garbage.

For a device driver, the semantics of multiple openings is entirely driver dependent - some drivers disallow it, others are fine (for example, think / dev / null). In some drivers, it has a special meaning (for example, sound cards can mix sound between several applications)

+1


a source







All Articles