-
Notifications
You must be signed in to change notification settings - Fork 226
Use flock() instead of UUCP-style locking for serial devices #1770
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
Open
taw10
wants to merge
46
commits into
OpenLightingProject:master
Choose a base branch
from
taw10:serial-flock
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 11 commits
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 0556472
Ignore a codespell false positive due to line wrapping
peternewman dd34c1d
Always compile UUCP code
taw10 cf4be90
Rename UUCP lock routines
taw10 638daf7
Rename AcquireLockAndOpen -> AcquireLockAndOpenSerialPort
taw10 5d0cb39
Add ReleaseSerialPortLock (to match AcquireLockAndOpenSerialPort)
taw10 15a299b
Fix indentation
taw10 10e4ac6
OpenAndFlock: Return false if flock() is not available
taw10 d195639
Add a message when locking with flock()
taw10 ce8f9b4
Restore documentation of deprecated UUCP locking routines
taw10 d9f2721
OpenAndFlock: close FD if flock() is not available
taw10 4ea7de4
Add extra spaces before comments
taw10 6951d76
Wrap a long line
taw10 61752ab
Add some more debug messages
taw10 d45704f
Add deprecation dates
taw10 53b8c2c
Add parentheses to make Doxygen link a reference
taw10 9cd59a8
Add SerialLockTester
taw10 3e072f1
Don't fail if TIOCEXCL doesn't work
taw10 4bcf0f6
SerialLockTester: formatting fixes
taw10 d035bde
SerialLockTester: rearrange includes
taw10 379db11
SerialLockTester: Rearrange includes again
taw10 662b6e9
SerialLockTester: Create the test file
taw10 b394083
Update include/ola/io/Serial.h
taw10 8ca5f34
Remove pre-processor comment
taw10 11b2f40
Mention flock() when saying "No unlock necessary"
taw10 ff04282
Update include/ola/io/Serial.h
taw10 6138823
Update common/io/SerialLockTester.cpp
taw10 3d9ae1d
Fix debug message and over-long line
taw10 7fe0fa5
SerialLockTester: Eliminate unused variable
taw10 f9dec96
Split serial port locking into three separate steps
taw10 0bfe200
Formatting
taw10 9fe41fe
Fix include order
taw10 f60bd33
Clarify info message about TIOCEXCL
taw10 4806d31
Restore deleted comment
taw10 0dfa3f9
Add explanatory comment to #endif
taw10 f97bc8d
SerialLockTester: Add ".pid" to test filename
taw10 a528123
Update comments about serial port locking
taw10 98d4b66
Fix ordering of RemoveUUCPLockFile() and close()
taw10 64d4943
Add more #endif comments
taw10 3ada1bd
AcquireLockAndOpenSerialPort: Release UUCP lock on error paths
taw10 277e151
Merge branch 'master' into serial-flock
taw10 227a613
Apply suggestions from code review
taw10 185b881
Merge branch 'master' into serial-flock
taw10 a93bde9
Merge branch 'master' into serial-flock
taw10 3c2afc8
Merge branch 'master' into serial-flock
taw10 ae7de43
Merge branch 'master' into serial-flock
taw10 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -58,8 +58,7 @@ using std::string; | |
|
|
||
| namespace { | ||
|
|
||
| #ifdef UUCP_LOCKING | ||
| string GetLockFile(const string &path) { | ||
| string GetUUCPLockFile(const string &path) { | ||
| const string base_name = ola::file::FilenameFromPath(path); | ||
| return ola::file::JoinPaths(UUCP_LOCK_DIR, "LCK.." + base_name); | ||
| } | ||
|
|
@@ -108,14 +107,13 @@ bool ProcessExists(pid_t pid) { | |
| #endif // _WIN32 | ||
| } | ||
|
|
||
| bool RemoveLockFile(const string &lock_file) { | ||
| bool RemoveUUCPLockFile(const string &lock_file) { | ||
| if (unlink(lock_file.c_str())) { | ||
| OLA_WARN << "Failed to remove UUCP lock file: " << lock_file; | ||
| return false; | ||
| } | ||
| return true; | ||
| } | ||
| #endif // UUCP_LOCKING | ||
|
|
||
| } // namespace | ||
|
|
||
|
|
@@ -143,7 +141,6 @@ bool UIntToSpeedT(uint32_t value, speed_t *output) { | |
| return false; | ||
| } | ||
|
|
||
| #ifdef HAVE_FLOCK | ||
| 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)) { | ||
|
|
@@ -157,11 +154,16 @@ bool OpenAndFlock(const std::string &path, int oflag, int *fd) { | |
| 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 | ||
|
taw10 marked this conversation as resolved.
Outdated
|
||
| close(*fd); | ||
|
peternewman marked this conversation as resolved.
Outdated
|
||
| return false; | ||
| #endif // HAVE_FLOCK | ||
|
|
||
| #if HAVE_SYS_IOCTL_H | ||
| // As a final safety mechanism, use ioctl(TIOCEXCL) if available to prevent | ||
|
|
@@ -172,12 +174,12 @@ bool OpenAndFlock(const std::string &path, int oflag, int *fd) { | |
| return false; | ||
|
taw10 marked this conversation as resolved.
Outdated
|
||
| } | ||
| #endif // HAVE_SYS_IOCTL_H | ||
|
|
||
| OLA_INFO << "Locked " << path << " using flock()"; | ||
| 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 | ||
|
|
@@ -191,7 +193,7 @@ bool AcquireUUCPLockAndOpen(const std::string &path, int oflag, int *fd) { | |
| } | ||
|
|
||
| // Second, clean up a stale lockfile. | ||
| const string lock_file = GetLockFile(path); | ||
| const string lock_file = GetUUCPLockFile(path); | ||
| OLA_DEBUG << "Checking for " << lock_file; | ||
| pid_t locked_pid; | ||
| if (!GetPidFromFile(lock_file, &locked_pid)) { | ||
|
|
@@ -210,7 +212,7 @@ bool AcquireUUCPLockAndOpen(const std::string &path, int oflag, int *fd) { | |
| } | ||
| // There is a race between the read & the unlink here. I'm not convinced it | ||
| // can be solved. | ||
| if (!RemoveLockFile(lock_file)) { | ||
| if (!RemoveUUCPLockFile(lock_file)) { | ||
| OLA_INFO << "Device " << path << " was locked by PID " << locked_pid | ||
| << " which is no longer active, however failed to remove stale " | ||
| << "lock file"; | ||
|
|
@@ -244,15 +246,15 @@ bool AcquireUUCPLockAndOpen(const std::string &path, int oflag, int *fd) { | |
| close(lock_fd); | ||
| if (r != pid_file_contents.size()) { | ||
| OLA_WARN << "Failed to write complete LCK file: " << lock_file; | ||
| RemoveLockFile(lock_file); | ||
| RemoveUUCPLockFile(lock_file); | ||
| 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"; | ||
| RemoveLockFile(lock_file); | ||
| RemoveUUCPLockFile(lock_file); | ||
| return false; | ||
| } | ||
|
|
||
|
|
@@ -262,18 +264,16 @@ bool AcquireUUCPLockAndOpen(const std::string &path, int oflag, int *fd) { | |
| if (ioctl(*fd, TIOCEXCL) == -1) { | ||
| OLA_WARN << "TIOCEXCL " << path << " failed: " << strerror(errno); | ||
| close(*fd); | ||
|
taw10 marked this conversation as resolved.
|
||
| RemoveLockFile(lock_file); | ||
| RemoveUUCPLockFile(lock_file); | ||
| return false; | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Again we should return false. |
||
| } | ||
| #endif // HAVE_SYS_IOCTL_H | ||
| return true; | ||
| } | ||
| #endif // UUCP_LOCKING | ||
|
|
||
|
|
||
| void ReleaseUUCPLock(const std::string &path) { | ||
| #ifdef UUCP_LOCKING | ||
| const string lock_file = GetLockFile(path); | ||
| const string lock_file = GetUUCPLockFile(path); | ||
|
|
||
| pid_t locked_pid; | ||
| if (!GetPidFromFile(lock_file, &locked_pid)) { | ||
|
|
@@ -282,20 +282,26 @@ void ReleaseUUCPLock(const std::string &path) { | |
|
|
||
| pid_t our_pid = getpid(); | ||
| if (our_pid == locked_pid) { | ||
| if (RemoveLockFile(lock_file)) { | ||
| if (RemoveUUCPLockFile(lock_file)) { | ||
| OLA_INFO << "Released " << lock_file; | ||
| } | ||
| } | ||
| #endif /* else no action needed */ | ||
| } | ||
|
|
||
| bool AcquireLockAndOpen(const std::string &path, int oflag, int *fd) { | ||
| bool AcquireLockAndOpenSerialPort(const std::string &path, int oflag, int *fd) { | ||
|
taw10 marked this conversation as resolved.
Outdated
|
||
| #ifdef UUCP_LOCKING | ||
| return AcquireUUCPLockAndOpen(path, oflag, fd); | ||
| return AcquireUUCPLockAndOpen(path, oflag, fd); | ||
| #else | ||
| return OpenAndFlock(path, oflag, fd); | ||
| return OpenAndFlock(path, oflag, fd); | ||
| #endif | ||
| } | ||
|
|
||
| void ReleaseSerialPortLock(const std::string &path) { | ||
| #ifdef UUCP_LOCKING | ||
| ReleaseUUCPLock(path); | ||
| #endif // UUCP_LOCKING | ||
| } | ||
|
|
||
|
|
||
| } // namespace io | ||
| } // namespace ola | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -59,6 +59,31 @@ typedef enum { | |
| */ | ||
| bool UIntToSpeedT(uint32_t value, speed_t *output); | ||
|
|
||
| /** | ||
| * @brief Try to open the path, respecting UUCP locking. | ||
| * @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. | ||
| * | ||
| * This fails-fast, it we can't get the lock immediately, we'll return false. | ||
| * | ||
| * @deprecated Use AcquireLockAndOpenSerialPort() instead. | ||
|
taw10 marked this conversation as resolved.
Outdated
|
||
| * @see ReleaseUUCPLock() | ||
| */ | ||
| bool AcquireUUCPLockAndOpen(const std::string &path, int oflag, int *fd); | ||
|
|
||
| /** | ||
| * @brief Remove a UUCP lock file for the device. | ||
| * @param path The path to unlock. | ||
| * | ||
| * The lock is only removed if the PID matches. | ||
| * | ||
| * @deprecated Use ReleaseSerialPortLock() instead. | ||
|
taw10 marked this conversation as resolved.
Outdated
|
||
| * @see AcquireUUCPLockAndOpen() | ||
| */ | ||
| void ReleaseUUCPLock(const std::string &path); | ||
|
|
||
| /** | ||
| * @brief Try to open the path and obtain a lock to control access | ||
| * @param path the path to open | ||
|
|
@@ -70,19 +95,24 @@ bool UIntToSpeedT(uint32_t value, speed_t *output); | |
| * (the default) or UUCP locking. See: ./configure --enable-uucp-locking. | ||
| * | ||
| * This fails-fast, it we can't get the lock immediately, we'll return false. | ||
| * | ||
| * @see ReleaseSerialPortLock() | ||
| */ | ||
| bool AcquireLockAndOpen(const std::string &path, int oflag, int *fd); | ||
| bool AcquireLockAndOpenSerialPort(const std::string &path, int oflag, int *fd); | ||
|
|
||
| /** | ||
| * @brief Remove a UUCP lock file for the device. | ||
| * @brief Release a lock for the serial port | ||
| * @param path The path to unlock. | ||
| * | ||
| * The lock is only removed if the PID matches. | ||
| * If UUCP locking was used (see AcquireLockAndOpenSerialPort), the lockfile | ||
|
peternewman marked this conversation as resolved.
Outdated
|
||
| * will be removed (but only if the PID matches). | ||
| * | ||
| * Does nothing if UUCP locking is not in use | ||
| * (see ./configure --enable-uucp-locking). | ||
| * Does nothing if flock() was used (see ./configure --enable-uucp-locking). | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Again can't this do all/more now? |
||
| * | ||
| * @see AcquireLockAndOpenSerialPort() | ||
| */ | ||
| void ReleaseUUCPLock(const std::string &path); | ||
| void ReleaseSerialPortLock(const std::string &path); | ||
|
|
||
| } // namespace io | ||
| } // namespace ola | ||
| #endif // INCLUDE_OLA_IO_SERIAL_H_ | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.