Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use flock() instead of UUCP-style locking for serial devices #1770

Open
wants to merge 44 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 10 commits
Commits
Show all changes
44 commits
Select commit Hold shift + click to select a range
b142600
Use flock() instead of UUCP-style locking for serial devices
taw10 Feb 6, 2022
0556472
Ignore a codespell false positive due to line wrapping
peternewman Feb 7, 2022
dd34c1d
Always compile UUCP code
taw10 Feb 13, 2022
cf4be90
Rename UUCP lock routines
taw10 Feb 13, 2022
638daf7
Rename AcquireLockAndOpen -> AcquireLockAndOpenSerialPort
taw10 Feb 13, 2022
5d0cb39
Add ReleaseSerialPortLock (to match AcquireLockAndOpenSerialPort)
taw10 Feb 13, 2022
15a299b
Fix indentation
taw10 Feb 13, 2022
10e4ac6
OpenAndFlock: Return false if flock() is not available
taw10 Feb 13, 2022
d195639
Add a message when locking with flock()
taw10 Feb 13, 2022
ce8f9b4
Restore documentation of deprecated UUCP locking routines
taw10 Feb 13, 2022
d9f2721
OpenAndFlock: close FD if flock() is not available
taw10 Feb 13, 2022
4ea7de4
Add extra spaces before comments
taw10 Feb 13, 2022
6951d76
Wrap a long line
taw10 Feb 19, 2022
61752ab
Add some more debug messages
taw10 Feb 19, 2022
d45704f
Add deprecation dates
taw10 Feb 19, 2022
53b8c2c
Add parentheses to make Doxygen link a reference
taw10 Feb 19, 2022
9cd59a8
Add SerialLockTester
taw10 Feb 19, 2022
3e072f1
Don't fail if TIOCEXCL doesn't work
taw10 Feb 19, 2022
4bcf0f6
SerialLockTester: formatting fixes
taw10 Feb 20, 2022
d035bde
SerialLockTester: rearrange includes
taw10 Feb 20, 2022
379db11
SerialLockTester: Rearrange includes again
taw10 Feb 20, 2022
662b6e9
SerialLockTester: Create the test file
taw10 Feb 21, 2022
b394083
Update include/ola/io/Serial.h
taw10 Mar 5, 2022
8ca5f34
Remove pre-processor comment
taw10 Mar 5, 2022
11b2f40
Mention flock() when saying "No unlock necessary"
taw10 Mar 5, 2022
ff04282
Update include/ola/io/Serial.h
taw10 Mar 5, 2022
6138823
Update common/io/SerialLockTester.cpp
taw10 Mar 5, 2022
3d9ae1d
Fix debug message and over-long line
taw10 Mar 6, 2022
7fe0fa5
SerialLockTester: Eliminate unused variable
taw10 Mar 6, 2022
f9dec96
Split serial port locking into three separate steps
taw10 Mar 6, 2022
0bfe200
Formatting
taw10 Mar 6, 2022
9fe41fe
Fix include order
taw10 Mar 6, 2022
f60bd33
Clarify info message about TIOCEXCL
taw10 Mar 20, 2022
4806d31
Restore deleted comment
taw10 Mar 20, 2022
0dfa3f9
Add explanatory comment to #endif
taw10 Mar 20, 2022
f97bc8d
SerialLockTester: Add ".pid" to test filename
taw10 Mar 20, 2022
a528123
Update comments about serial port locking
taw10 Mar 20, 2022
98d4b66
Fix ordering of RemoveUUCPLockFile() and close()
taw10 Mar 20, 2022
64d4943
Add more #endif comments
taw10 Mar 20, 2022
3ada1bd
AcquireLockAndOpenSerialPort: Release UUCP lock on error paths
taw10 Mar 20, 2022
277e151
Merge branch 'master' into serial-flock
taw10 Nov 11, 2022
227a613
Apply suggestions from code review
taw10 Nov 13, 2022
185b881
Merge branch 'master' into serial-flock
taw10 Apr 3, 2023
a93bde9
Merge branch 'master' into serial-flock
taw10 Oct 28, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
138 changes: 80 additions & 58 deletions common/io/Serial.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -141,57 +141,12 @@ bool UIntToSpeedT(uint32_t value, speed_t *output) {
return false;
}

bool OpenAndFlock(const std::string &path, int oflag, int *fd) {
// First, check if the path exists, there's no point trying to open it if not
if (!FileExists(path)) {
OLA_INFO << "Device " << path << " doesn't exist.";
return false;
}

// Now try to open the serial device.
if (!TryOpen(path, oflag, fd)) {
OLA_DEBUG << "Failed to open device " << path;
return false;
}

#ifdef HAVE_FLOCK
if (flock(*fd, LOCK_EX | LOCK_NB) == -1) {
OLA_INFO << "Failed to flock() device " << path;
close(*fd);
return false;
}
#else // HAVE_FLOCK
OLA_WARN << "Tried to flock " << path << ", but flock() is unavailable";
close(*fd);
return false;
#endif // HAVE_FLOCK

#if HAVE_SYS_IOCTL_H
// As a final safety mechanism, use ioctl(TIOCEXCL) if available to prevent
// further opens.
if (ioctl(*fd, TIOCEXCL) == -1) {
OLA_WARN << "TIOCEXCL " << path << " failed: " << strerror(errno);
}
#endif // HAVE_SYS_IOCTL_H

OLA_INFO << "Locked " << path << " using flock()";
return true;
}


bool AcquireUUCPLockAndOpen(const std::string &path, int oflag, int *fd) {
bool AcquireUUCPLock(const std::string &path) {
// This is rather tricky since there is no real convention for LCK files.
// If it was only a single process doing the locking we could use fnctl as
// described in 55.6 of the Linux Programming Interface book.

// First, check if the path exists, there's no point trying to open it if not
if (!FileExists(path)) {
OLA_INFO << "Device " << path << " doesn't exist, so there's no point "
"trying to acquire a lock";
return false;
}

// Second, clean up a stale lockfile.
const string lock_file = GetUUCPLockFile(path);
OLA_DEBUG << "Checking for " << lock_file;
pid_t locked_pid;
Expand All @@ -200,6 +155,7 @@ bool AcquireUUCPLockAndOpen(const std::string &path, int oflag, int *fd) {
return false;
}

// Clean up a stale lockfile.
if (locked_pid) {
// This will return false even if we have the lock, this is what we want
// since different plugins may try to open the same serial port - see issue
Expand Down Expand Up @@ -249,21 +205,51 @@ bool AcquireUUCPLockAndOpen(const std::string &path, int oflag, int *fd) {
return false;
}

return true;
}

bool LockTIOCEXCL(int fd, const std::string &path) {
#if HAVE_SYS_IOCTL_H
// This is a final safety mechanism, on top of UUCP locking or flock()
if (ioctl(fd, TIOCEXCL) == -1) {
OLA_WARN << "TIOCEXCL " << path << " failed: " << strerror(errno);
return false;
} else {
OLA_INFO << "Set TIOCEXCL on " << path;
}
#else
OLA_INFO << "Not setting " << path << " - ioctl.h unavailable";
taw10 marked this conversation as resolved.
Show resolved Hide resolved
#endif // HAVE_SYS_IOCTL_H
return true;
}

bool AcquireUUCPLockAndOpen(const std::string &path, int oflag, int *fd) {
// First, check if the path exists, there's no point trying to open it if not
if (!FileExists(path)) {
OLA_INFO << "Device " << path << " doesn't exist, so there's no point "
"trying to acquire a lock";
return false;
}

if (!AcquireUUCPLock(path)) {
return false;
}

// Now try to open the serial device.
if (!TryOpen(path, oflag, fd)) {
OLA_DEBUG << "Failed to open device " << path << " despite having the "
<< "lock file";
RemoveUUCPLockFile(lock_file);
ReleaseUUCPLock(path);
close(*fd);
return false;
}

#if HAVE_SYS_IOCTL_H
// As a final safety mechanism, use ioctl(TIOCEXCL) if available to prevent
// further opens.
if (ioctl(*fd, TIOCEXCL) == -1) {
OLA_WARN << "TIOCEXCL " << path << " failed: " << strerror(errno);
if (!LockTIOCEXCL(*fd, path)) {
taw10 marked this conversation as resolved.
Show resolved Hide resolved
ReleaseUUCPLock(path);
close(*fd);
return false;
}
#endif // HAVE_SYS_IOCTL_H

return true;
}

Expand All @@ -285,19 +271,55 @@ void ReleaseUUCPLock(const std::string &path) {
}

bool AcquireLockAndOpenSerialPort(const std::string &path, int oflag, int *fd) {
// First, check if the path exists, there's no point trying to open it if not
if (!FileExists(path)) {
OLA_INFO << "Device " << path << " doesn't exist, so there's no point "
"trying to acquire a lock";
return false;
}

#ifdef UUCP_LOCKING
if (!AcquireUUCPLock(path)) {
return false;
}
#endif

// Now try to open the serial device.
if (!TryOpen(path, oflag, fd)) {
#ifdef UUCP_LOCKING
return AcquireUUCPLockAndOpen(path, oflag, fd);
OLA_DEBUG << "Failed to open device " << path << " despite having the "
<< "lock file";
ReleaseUUCPLock(path);
#else
return OpenAndFlock(path, oflag, fd);
OLA_DEBUG << "Failed to open device " << path;
#endif
return false;
}

#ifdef HAVE_FLOCK
if (flock(*fd, LOCK_EX | LOCK_NB) == -1) {
OLA_INFO << "Failed to flock() device " << path;
close(*fd);
return false;
} else {
OLA_INFO << "Locked " << path << " using flock()";
}
#endif

if (!LockTIOCEXCL(*fd, path)) {
close(*fd);
return false;
}

return true;
}

void ReleaseSerialPortLock(const std::string &path) {
#ifdef UUCP_LOCKING
ReleaseUUCPLock(path);
#else // UUCP_LOCKING
OLA_INFO << "No unlock necessary for " << path;
#endif // UUCP_LOCKING
#else
OLA_INFO << "No unlock necessary for " << path << " (UUCP locking not used)";
#endif
taw10 marked this conversation as resolved.
Show resolved Hide resolved
}


Expand Down
38 changes: 24 additions & 14 deletions common/io/SerialLockTester.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
#include <config.h>
#endif // HAVE_CONFIG_H

#include <sys/file.h>
#include <unistd.h>
#include <fcntl.h>
#include <cppunit/extensions/HelperMacros.h>
Expand All @@ -45,29 +46,38 @@ class SerialLockTest: public CppUnit::TestFixture {
CPPUNIT_TEST_SUITE_REGISTRATION(SerialLockTest);

void SerialLockTest::testLock() {
bool r1, r2;
int fd1, fd2, fd3;
const std::string path = "serialLockTestFile";
int fd;
pid_t our_pid = getpid();

OLA_ASSERT_FALSE(ola::io::FileExists(path));
std::stringstream str;
str << "serialLockTestFile." << our_pid;
taw10 marked this conversation as resolved.
Show resolved Hide resolved
const std::string path = str.str();

fd3 = open(path.c_str(), O_CREAT | O_RDWR,
OLA_ASSERT_FALSE_MSG(ola::io::FileExists(path),
"Test file already exists");

fd = open(path.c_str(), O_CREAT | O_RDWR,
S_IRUSR | S_IWUSR
#ifndef _WIN32
| S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH
#endif // !_WIN32
); // NOLINT(whitespace/parens)
OLA_ASSERT_FALSE(fd3 < 0);
close(fd3);
OLA_ASSERT_FALSE_MSG(fd < 0, "Couldn't create test file");

#ifdef HAVE_FLOCK
OLA_ASSERT_TRUE(flock(fd, LOCK_EX | LOCK_NB) != -1);
#else

r1 = ola::io::AcquireLockAndOpenSerialPort(path, O_RDWR, &fd1);
OLA_ASSERT_TRUE(r1);
#ifdef UUCP_LOCKING
// flock() is not available, but UUCP locking was selected. OK.
#else
OLA_FAIL("Not using UUCP locking, and flock() is not available");
#endif

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Perhaps check fd1 is valid/can be written to?

r2 = ola::io::AcquireLockAndOpenSerialPort(path, O_RDWR, &fd2);
OLA_ASSERT_FALSE(r2);
#endif

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Perhaps check fd2 is invalid/can't be written to?

ola::io::ReleaseSerialPortLock(path);
close(fd1);
close(fd);

OLA_ASSERT_FALSE(unlink(path.c_str()));
OLA_ASSERT_FALSE_MSG(unlink(path.c_str()),
"Couldn't delete test file");
}
9 changes: 5 additions & 4 deletions configure.ac
Original file line number Diff line number Diff line change
Expand Up @@ -797,11 +797,12 @@ AC_CHECK_FUNC([flock], [AC_DEFINE([HAVE_FLOCK], [1],
[Define if flock() is available])
have_flock=yes], [])

UUCP_LOCKING="no"
AS_IF([test "x$enable_uucp_locking" = xyes || test "x$have_flock" != xyes],
[AC_DEFINE([UUCP_LOCKING], [1],
[Define if UUCP locking should be used instead of flock()])
SERIAL_LOCK_METHOD=UUCP],
[SERIAL_LOCK_METHOD=flock])
[Define if UUCP locking should be used])
UUCP_LOCKING=yes],
[])

# UUCP Lock directory
AC_ARG_WITH([uucp-lock],
Expand Down Expand Up @@ -1038,7 +1039,7 @@ Enable HTTP Server: ${have_microhttpd}
RDM Responder Tests: ${enable_rdm_tests}
Ja Rule: ${BUILDING_JA_RULE}
Enabled Plugins:${PLUGINS}
Serial port locking: $SERIAL_LOCK_METHOD
UUCP Locking: $UUCP_LOCKING
UUCP Lock Directory: $UUCPLOCK

Now type 'make @<:@<target>@:>@'
Expand Down
4 changes: 2 additions & 2 deletions include/ola/io/Serial.h
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ bool UIntToSpeedT(uint32_t value, speed_t *output);
*
* This fails-fast, it we can't get the lock immediately, we'll return false.
*
* @deprecated Use AcquireLockAndOpenSerialPort() instead (19 Feb 2022).
* @deprecated Use the more general AcquireLockAndOpenSerialPort() instead (19 Feb 2022).
* @see ReleaseUUCPLock()
*/
bool AcquireUUCPLockAndOpen(const std::string &path, int oflag, int *fd);
Expand All @@ -79,7 +79,7 @@ bool AcquireUUCPLockAndOpen(const std::string &path, int oflag, int *fd);
*
* The lock is only removed if the PID matches.
*
* @deprecated Use ReleaseSerialPortLock() instead (19 Feb 2022).
* @deprecated Use the more general ReleaseSerialPortLock() instead (19 Feb 2022).
* @see AcquireUUCPLockAndOpen()
*/
void ReleaseUUCPLock(const std::string &path);
Expand Down