The goal is to "lock" access to a serial device or other Linux device, to ensure exclusive access to the device while it's in-use. This prevents, for example, two programs both opening the same serial device and "competing" to read bytes from the device.
The advice has been to use SYSV-style UUCP device lock files such as /var/lock/LCK..ttyS1
. That is what is recommended by Linux Serial HOTWO: Locking Out Others. It is also documented in the Filesystem Heirarchy Standard. It is implemented by serial terminal programs such as gtkterm, picocom. There are libraries such as liblockdev
and liblockfile
to support this (although the implementation details differ between these two libraries).
However, I have found Debian bug #734086, which says on Linux, SYSV-style UUCP device locks are deprecated, and flock()
advisory locks should be used instead.
However, I can't find a reliable document source to describe deprecation of these SYSV-style UUCP device locks, and recommendation of flock()
, other than that Debian bug itself.
I've also found ioctl(fd, TIOCEXCL)
which is used by the screen
utility to lock a terminal.
Which is the modern "best practice" for locking serial ports and other devices in Linux? Where can we find up-to-date documentation describing this?