What is the best practice for blocking serial ports and other devices on Linux?

The goal is to β€œblock” access to a serial device or other Linux device in order to provide exclusive access to the device during its use. This prevents, for example, two programs opening the same serial device and β€œcompeting” to read bytes from the device.

The tip was to use SYSV-style UUCP device lock files, such as /var/lock/LCK..ttyS1 . This is what Linux Serial HOTWO recommends: blocking others . It is also documented in the Heirarchy File System Standard . It is implemented by serial terminal programs such as gtkterm, picocom. Libraries exist such as liblockdev and liblockfile to support this (although implementation details vary between the two libraries).

However, I found a Debian bug # 734086 that talks about Linux, SYSV style UUCP device locks are outdated, and flock() should use locks instead.

However, I cannot find a reliable document source to describe the obsolescence of these SYSV-style UUCP device locks and the flock() recommendation, except for the Debian error itself.

I also found ioctl(fd, TIOCEXCL) , which is used by the screen utility to lock the terminal.

What is the current "best practice" for blocking serial ports and other devices on Linux? Where can we find the latest documentation describing this?

+5
source share
1 answer

As far as I can tell, using flock() block serial ports or other devices is probably best suited for Linux, following the Debian example in Debian Error # 734086 . Please note that the original defender of this Debian, Roger Leigh, left Debian and Linux and on FreeBSD in 2014/2015 (see his comments here ). But Debian seems to stick with the flock() method, so it costs something.

However, given how poorly this change has been conveyed to the wider Linux community at this stage, it would be useful to maintain old SYSV-style UUCP device lock files ( /var/lock/LCK..ttyS1 ) as a compile-time parameter for use on systems still using the old lock method.

eg. picocom has now been changed to use the flock() method with an optional SYSV style UUCP file lock time.

I would like to provide an update to the Serial HOWTO regarding this (since this is the first Google search result for "Linux sequential blocking"), but now it is difficult to update documents on the LDP website due to its current maintenance status.

+1
source

All Articles