From ffbfc5c76ba8100d21d0141478a6c0d761ecfb2f Mon Sep 17 00:00:00 2001 From: Leon Varga Date: Tue, 3 Mar 2020 14:18:53 +0100 Subject: [PATCH] Open the file descriptor of a serial port on POSIX systems exclusively This fixes bug #1510. --- configure.ac | 4 ++++ libserialport_internal.h | 3 +++ serialport.c | 33 +++++++++++++++++++++++++++++++++ 3 files changed, 40 insertions(+) diff --git a/configure.ac b/configure.ac index 4d643e2..b1af16f 100644 --- a/configure.ac +++ b/configure.ac @@ -129,6 +129,10 @@ AC_CHECK_TYPES([struct serial_struct],,, [[#include ]]) # Check for realpath(). AC_CHECK_FUNC([realpath], [AC_DEFINE(HAVE_REALPATH, 1, [realpath is available.])], []) +# Check for flock(). +AC_CHECK_HEADER([sys/file.h], [AC_DEFINE(HAVE_SYS_FILE_H, 1, [sys/file.h is available.])], []) +AC_CHECK_FUNC([flock], [AC_DEFINE(HAVE_FLOCK, 1, [flock is available.])], []) + # Check for clock_gettime(). AC_CHECK_FUNC([clock_gettime], [AC_DEFINE(HAVE_CLOCK_GETTIME, 1, [clock_gettime is available.])], []) diff --git a/libserialport_internal.h b/libserialport_internal.h index 5b811cc..ecf8fe9 100644 --- a/libserialport_internal.h +++ b/libserialport_internal.h @@ -82,6 +82,9 @@ #include #include #include +#ifdef HAVE_SYS_FILE_H +#include +#endif #endif #ifdef __APPLE__ #include diff --git a/serialport.c b/serialport.c index c22e94f..51fe078 100644 --- a/serialport.c +++ b/serialport.c @@ -551,6 +551,39 @@ SP_API enum sp_return sp_open(struct sp_port *port, enum sp_mode flags) if ((port->fd = open(port->name, flags_local)) < 0) RETURN_FAIL("open() failed"); + + /* + * On POSIX in the default case the file descriptor of a serial port + * is not opened exclusively. Therefore the settings of a port are + * overwritten if the serial port is opened a second time. Windows + * opens all serial ports exclusively. + * So the idea is to open the serial ports alike in the exclusive mode. + * + * ioctl(*, TIOCEXCL) defines the file descriptor as exclusive. So all + * further open calls on the serial port will fail. + * + * There is a race condition if two processes open the same serial + * port. None of the processes will notice the exclusive ownership of + * the other process because ioctl() doesn't return an error code if + * the file descriptor is already marked as exclusive. + * This can be solved with flock(). It returns an error if the file + * descriptor is already locked by another process. + */ +#ifdef HAVE_FLOCK + if (flock(port->fd, LOCK_EX | LOCK_NB) < 0) + RETURN_FAIL("flock() failed"); +#endif + +#ifdef TIOCEXCL + /* + * Before Linux 3.8 ioctl(*, TIOCEXCL) was not implemented and could + * lead to EINVAL or ENOTTY. + * These errors aren't fatal and can be ignored. + */ + if (ioctl(port->fd, TIOCEXCL) < 0 && errno != EINVAL && errno != ENOTTY) + RETURN_FAIL("ioctl() failed"); +#endif + #endif ret = get_config(port, &data, &config);