20

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?

8
  • 1
    Google for mandatory locking linux which has many issues... Commented Apr 2, 2013 at 11:35
  • 1
    here is link on mandatory and advisory locking in linux.... thegeekstuff.com/2012/04/linux-file-locking-types Commented Apr 2, 2013 at 11:52
  • @KinjalPatel That will only work if you are using flock(), otherwise it doesn't prevent writing. The only way to restrict exclusive opens on devices automatically is to have the device driver perform open reference counts itself. Commented Apr 2, 2013 at 13:07
  • Does the (effective UID of the) first process to open the modem become the owner of the device for the duration? If so, can your first-to-open process then set the permissions on the modem device to 000 so no-one can open it from there on? There's a race condition, of course, between opening the device and changing its mode, but if the system automatically reverts the ownership back to the system when the process finishes, it might give more-or-less the right effect. A sufficiently privileged user, or another process by the same user, could dink with the permissions before opening it too. Commented Feb 18, 2019 at 22:22
  • 1
    What about creating a login user for your application and set the device file permissions to allow to be read/written only by that user?
    – Fusho
    Commented Feb 23, 2019 at 19:43

4 Answers 4

8
+50

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.

Edit: see comment below.

1
  • 1
    From the man page: "Warning: the Linux implementation of mandatory locking is unreliable. See BUGS below. Because of these bugs, and the fact that the feature is believed to be little used, since Linux 4.5, mandatory locking has been made an optional feature, governed by a configuration option (CONFIG_MANDATORY_FILE_LOCKING). This feature is no longer supported at all in Linux 5.15 and above." Commented Jul 24, 2023 at 6:57
0

try lockf() : apply, test or remove a POSIX lock on an open file

-1

If you want to get exclusive access to a device, create a lock file in /var/lock. The process that can create the lock file with open("my_device.lock", O_CREAT|O_EXCL, 0777) gets access to the device, the other processes have to wait. After the process is done using the device, it closes the file.

Such a lock is only advisory and doesn't guarantee that no other process (that you are not aware of) accesses the device.

2
  • 1
    What happens when multiple unrelated programs attempt to open the device?
    – jww
    Commented Feb 17, 2019 at 15:25
  • @jww other processes can of course still open the device and do nasty stuff. The trick is that they should try opening the lock file with O_EXCL first before doing something. Best is you write a library that provides e.g. a write() function which handles opening and closing the lock file. Each process would then use this library to interact with the device. Commented Feb 18, 2019 at 3:35
-2

To open a device you should use open system call in linux and check the list of available devices for example /dev/ttyUSB0 or /dev/ttyS0 etc. and open it and you will get a descriptor to write and read on to the device is you open a device to communicate. To know the further details follow the link : http://www.firmcodes.com/lower-level-file-handling-in-linux/

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.