RJCP.DLL.SerialPortStream
RJCP.DLL.SerialPortStream copied to clipboard
On Linux, lock serial ports for exclusive access using flock()
In dll/serialunix/libnserial/openserial.c, I notice the code can optionally do a TIOCEXCL lock.
#if defined HAVE_TERMIOS_EXCLUSIVE
if (ioctl(handle->fd, TIOCEXCL)) {
nslog(handle, NSLOG_NOTICE, "open: error setting TIOCEXCL: errno=%d", errno);
}
#endif
But in Linux, it's common to use a flock() advisory lock on serial ports. Something like
if (flock( handle->fd, LOCK_EX | LOCK_NB )) {
nslog(handle, NSLOG_NOTICE, "open: error getting exclusive lock: errno=%d", errno);
}
See eg picocom.
The LWN post suggests it is more complex. https://lwn.net/Articles/1041316/
Yes, you're right, there is still the legacy of UUCP-style lock files to consider.
However, long-term it seems that everyone is still moving away from UUCP-style lock files, and towards flock() (preferring flock() over ioctl(fd, TIOCEXCL)).
I'll get around to it eventually.