RJCP.DLL.SerialPortStream icon indicating copy to clipboard operation
RJCP.DLL.SerialPortStream copied to clipboard

On Linux, lock serial ports for exclusive access using flock()

Open cmcqueen opened this issue 6 months ago • 3 comments

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.

cmcqueen avatar Jun 13 '25 06:06 cmcqueen

The LWN post suggests it is more complex. https://lwn.net/Articles/1041316/

jcurl avatar Oct 24 '25 10:10 jcurl

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)).

cmcqueen avatar Nov 30 '25 22:11 cmcqueen

I'll get around to it eventually.

jcurl avatar Dec 01 '25 00:12 jcurl