Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
46 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
3c2afc8
Merge branch 'master' into serial-flock
taw10 Sep 14, 2025
ae7de43
Merge branch 'master' into serial-flock
taw10 Mar 28, 2026
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
49 changes: 49 additions & 0 deletions common/io/Serial.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
#include <signal.h>
#include <string.h>
#include <sys/stat.h>
#include <sys/file.h>
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Has sys/file.h existed forever, so we're not breaking UUCP builds which don't have it?

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

I'm not sure, I'll investigate. However the header is checked by the configure script, and used without any #ifdefs in one other place (KarateLight).

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Ah okay, obviously that plugin breaking wouldn't have quite so much impact, but the plugin has been around for a while and no-one has complained so we're probably good...

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

he flock call itself conforms to 4.4BSD, i.e. it's been around since 1997. So I think we're pretty safe here.

I could add guards around the #include (ifdef HAVE_SYS_FILE_H), but if it's worked so far then I don't see any reason to add the extra complexity. On inspection, I'm not actually sure why KarateLight includes this header at all.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Yeah very good point about the fact it's worked!

I'm not sure about KarateLight either; @cpresser ?

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Unfortunately, I am not sure what to answer here. My C++ is quite rusty, I have not used it in years and sure have no idea which include contains which functionality.

I recall that I did include file locking of the serial-port-device in the karatelight code. A quick search turned up this piece of code here:
https://github.com/OpenLightingProject/ola/blob/master/plugins/karate/KarateLight.cpp#L121

Does that help?

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

The #include <sys/file.h> has been there since the first version of KarateDevice.cpp (f69a26b), but the flock call is in a different file (KarateLight.cpp). I suspect the flock calls were originally in KarateDevice.cpp and got moved.

It does mean, however, that there's not only the #include but also a real use of flock() in OLA already without any tests! Therefore we're probably pretty safe using it for all serial devices. Nevertheless, we'll keep the tests and the conditionals :)

#ifdef _WIN32
#define VC_EXTRALEAN
#define WIN32_LEAN_AND_MEAN
Expand Down Expand Up @@ -57,6 +58,7 @@ using std::string;

namespace {

#ifdef UUCP_LOCKING
Comment thread
peternewman marked this conversation as resolved.
Outdated
string GetLockFile(const string &path) {
Comment thread
peternewman marked this conversation as resolved.
Outdated
const string base_name = ola::file::FilenameFromPath(path);
return ola::file::JoinPaths(UUCP_LOCK_DIR, "LCK.." + base_name);
Expand Down Expand Up @@ -113,6 +115,7 @@ bool RemoveLockFile(const string &lock_file) {
}
Comment thread
peternewman marked this conversation as resolved.
return true;
}
#endif // UUCP_LOCKING

} // namespace

Expand Down Expand Up @@ -140,7 +143,41 @@ bool UIntToSpeedT(uint32_t value, speed_t *output) {
return false;
}

#ifdef HAVE_FLOCK
bool OpenAndFlock(const std::string &path, int oflag, int *fd) {
Comment thread
peternewman marked this conversation as resolved.
Outdated
// 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;
}

if (flock(*fd, LOCK_EX | LOCK_NB) == -1) {
OLA_INFO << "Failed to flock() device " << 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);
close(*fd);
return false;
}
#endif // HAVE_SYS_IOCTL_H
return true;
}
#endif // HAVE_FLOCK


#ifdef UUCP_LOCKING
bool AcquireUUCPLockAndOpen(const std::string &path, int oflag, int *fd) {
// 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
Expand Down Expand Up @@ -231,9 +268,11 @@ bool AcquireUUCPLockAndOpen(const std::string &path, int oflag, int *fd) {
#endif // HAVE_SYS_IOCTL_H
return true;
}
#endif // UUCP_LOCKING


void ReleaseUUCPLock(const std::string &path) {
#ifdef UUCP_LOCKING
Comment thread
peternewman marked this conversation as resolved.
Outdated
const string lock_file = GetLockFile(path);

pid_t locked_pid;
Expand All @@ -247,6 +286,16 @@ void ReleaseUUCPLock(const std::string &path) {
OLA_INFO << "Released " << lock_file;
}
}
#endif /* else no action needed */
Comment thread
peternewman marked this conversation as resolved.
Outdated
}

bool AcquireLockAndOpen(const std::string &path, int oflag, int *fd) {
#ifdef UUCP_LOCKING
return AcquireUUCPLockAndOpen(path, oflag, fd);
#else
return OpenAndFlock(path, oflag, fd);
#endif
Comment thread
taw10 marked this conversation as resolved.
Outdated
}

} // namespace io
} // namespace ola
17 changes: 17 additions & 0 deletions configure.ac
Original file line number Diff line number Diff line change
Expand Up @@ -787,6 +787,22 @@ AC_ARG_ENABLE(
[AS_HELP_STRING([--disable-doxygen-version],
[Substitute the Doxygen version with latest, for the website])])

# Use UUCP locking?
AC_ARG_ENABLE(
[uucp-locking],
[AS_HELP_STRING([--enable-uucp-locking],
[Use UUCP locking instead of flock()])])

AC_CHECK_FUNC([flock], [AC_DEFINE([HAVE_FLOCK], [1],
[Define if flock() is available])
have_flock=yes], [])

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

# UUCP Lock directory
AC_ARG_WITH([uucp-lock],
[AS_HELP_STRING([--with-uucp-lock],
Expand Down Expand Up @@ -1022,6 +1038,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 Lock Directory: $UUCPLOCK

Now type 'make @<:@<target>@:>@'
Expand Down
10 changes: 8 additions & 2 deletions include/ola/io/Serial.h
Original file line number Diff line number Diff line change
Expand Up @@ -60,21 +60,27 @@ typedef enum {
bool UIntToSpeedT(uint32_t value, speed_t *output);

/**
* @brief Try to open the path, respecting UUCP locking.
* @brief Try to open the path and obtain a lock to control access
* @param path the path to open
* @param oflag flags passed to open
* @param[out] fd a pointer to the fd which is returned.
* @returns true if the open succeeded, false otherwise.
*
* Depending on the compile-time configuration, this will use either flock()
* (the default) or UUCP locking. See: ./configure --enable-uucp-locking.
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Isn't this now doing both/all?

*
* This fails-fast, it we can't get the lock immediately, we'll return false.
*/
bool AcquireUUCPLockAndOpen(const std::string &path, int oflag, int *fd);
bool AcquireLockAndOpen(const std::string &path, int oflag, int *fd);
Comment thread
peternewman marked this conversation as resolved.
Outdated

/**
* @brief Remove a UUCP lock file for the device.
* @param path The path to unlock.
*
* The lock is only removed if the PID matches.
*
* Does nothing if UUCP locking is not in use
* (see ./configure --enable-uucp-locking).
*/
void ReleaseUUCPLock(const std::string &path);
} // namespace io
Expand Down
6 changes: 3 additions & 3 deletions plugins/stageprofi/StageProfiDetector.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -164,9 +164,9 @@ ConnectedDescriptor* StageProfiDetector::ConnectToUSB(
struct termios newtio;

int fd;
if (!ola::io::AcquireUUCPLockAndOpen(widget_path,
O_RDWR | O_NONBLOCK | O_NOCTTY,
&fd)) {
if (!ola::io::AcquireLockAndOpen(widget_path,
O_RDWR | O_NONBLOCK | O_NOCTTY,
&fd)) {
return NULL;
}

Expand Down
3 changes: 1 addition & 2 deletions plugins/usbpro/BaseUsbProWidget.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -129,8 +129,7 @@ ola::io::ConnectedDescriptor *BaseUsbProWidget::OpenDevice(
const string &path) {
struct termios newtio;
int fd;
if (!ola::io::AcquireUUCPLockAndOpen(path, O_RDWR | O_NONBLOCK | O_NOCTTY,
&fd)) {
if (!ola::io::AcquireLockAndOpen(path, O_RDWR | O_NONBLOCK | O_NOCTTY, &fd)) {
return NULL;
}

Expand Down