Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Exclusively open a device file in Linux

Tags:

c

linux

posix

flock

What ways are there available, for exclusively opening a device file (say, the display frame buffer)?

[Info: I already know about flock() & friends, which have an effect only when the other applications are also using it (in other words: open() will succeed but flock() will fail if already locked) --> but still the device handle retrieved from open() can be used to write to the display..]

What about cases when I want to enforce such an exclusive access on a device files? How would such an enforcement be possible?

Thanks in advance!

like image 444
user2075654 Avatar asked Apr 02 '13 11:04

user2075654


People also ask

How do I access a device file in Linux?

All Linux device files are located in the /dev directory, which is an integral part of the root (/) filesystem because these device files must be available to the operating system during the boot process.

What is a device file in Linux?

In Linux various special files can be found under the directory /dev . These files are called device files and behave unlike ordinary files. The most common types of device files are for block devices and character devices.

How do I open a block file in Linux?

The block devices on a system can be discovered with the lsblk (list block devices) command. Try it in the VM below. Type lsblk at the command prompt and then press Enter.

Which are the two types of device files in Linux?

There are two types of device files; character and block, as well as two modes of access.


1 Answers

From fcntl(2):

To make use of mandatory locks, mandatory locking must be enabled both on the filesystem that contains the file to be locked, and on the file itself.

...also, you need to enable CONFIG_MANDATORY_FILE_LOCKING in the kernel.

Mandatory locking is enabled on a filesystem using the "-o mand" option to mount(8), or the MS_MANDLOCK flag for mount(2). Mandatory locking is enabled on a file by disabling group execute permission on the file and enabling the set-group-ID permis‐ sion bit (see chmod(1) and chmod(2)).

Mandatory locking is not specified by POSIX. Some other systems also support mandatory locking, although the details of how to enable it vary across systems.

So, as you request a posix-compliant solution, the answer is: no, there is not such a feature in the POSIX standard.

like image 106
Fusho Avatar answered Oct 01 '22 00:10

Fusho