-
Notifications
You must be signed in to change notification settings - Fork 225
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
base: master
Are you sure you want to change the base?
Changes from all commits
b142600
0556472
dd34c1d
cf4be90
638daf7
5d0cb39
15a299b
10e4ac6
d195639
ce8f9b4
d9f2721
4ea7de4
6951d76
61752ab
d45704f
53b8c2c
9cd59a8
3e072f1
4bcf0f6
d035bde
379db11
662b6e9
b394083
8ca5f34
11b2f40
ff04282
6138823
3d9ae1d
7fe0fa5
f9dec96
0bfe200
9fe41fe
f60bd33
4806d31
0dfa3f9
f97bc8d
a528123
98d4b66
64d4943
3ada1bd
277e151
227a613
185b881
a93bde9
3c2afc8
ae7de43
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -27,6 +27,7 @@ | |
| #include <signal.h> | ||
| #include <string.h> | ||
| #include <sys/stat.h> | ||
| #include <sys/file.h> | ||
|
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. Has sys/file.h existed forever, so we're not breaking UUCP builds which don't have it?
Author
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. 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).
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. 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...
Author
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. 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.
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. Yeah very good point about the fact it's worked! I'm not sure about KarateLight either; @cpresser ? 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. 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: Does that help?
Author
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. The 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 | ||
|
|
@@ -57,7 +58,7 @@ using std::string; | |
|
|
||
| namespace { | ||
|
|
||
| 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); | ||
| } | ||
|
|
@@ -106,7 +107,7 @@ 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; | ||
|
|
@@ -141,27 +142,20 @@ bool UIntToSpeedT(uint32_t value, speed_t *output) { | |
| } | ||
|
|
||
|
|
||
| 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 = GetLockFile(path); | ||
| const string lock_file = GetUUCPLockFile(path); | ||
| OLA_DEBUG << "Checking for " << lock_file; | ||
| pid_t locked_pid; | ||
| if (!GetPidFromFile(lock_file, &locked_pid)) { | ||
| OLA_INFO << "Failed to get PID from " << lock_file; | ||
| 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 | ||
|
|
@@ -173,7 +167,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"; | ||
|
|
@@ -207,34 +201,63 @@ 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; | ||
| } | ||
|
|
||
| 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 TIOCEXCL on " << path << " - ioctl.h unavailable"; | ||
| #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"; | ||
| RemoveLockFile(lock_file); | ||
| close(*fd); | ||
| ReleaseUUCPLock(path); | ||
| 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.
|
||
| close(*fd); | ||
|
taw10 marked this conversation as resolved.
|
||
| RemoveLockFile(lock_file); | ||
| ReleaseUUCPLock(path); | ||
| 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; | ||
| } | ||
|
|
||
|
|
||
| void ReleaseUUCPLock(const std::string &path) { | ||
| const string lock_file = GetLockFile(path); | ||
| const string lock_file = GetUUCPLockFile(path); | ||
|
|
||
| pid_t locked_pid; | ||
| if (!GetPidFromFile(lock_file, &locked_pid)) { | ||
|
|
@@ -243,10 +266,72 @@ 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; | ||
| } | ||
| } | ||
| } | ||
|
|
||
| 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 // UUCP_LOCKING | ||
|
|
||
| // Now try to open the serial device. | ||
| if (!TryOpen(path, oflag, fd)) { | ||
| #ifdef UUCP_LOCKING | ||
| OLA_DEBUG << "Failed to open device " << path << " despite having the " | ||
| << "lock file"; | ||
| ReleaseUUCPLock(path); | ||
| #else | ||
| OLA_DEBUG << "Failed to open device " << path; | ||
| #endif // UUCP_LOCKING | ||
| return false; | ||
| } | ||
|
|
||
| #ifdef HAVE_FLOCK | ||
| if (flock(*fd, LOCK_EX | LOCK_NB) == -1) { | ||
| OLA_INFO << "Failed to flock() device " << path; | ||
| close(*fd); | ||
| #ifdef UUCP_LOCKING | ||
| ReleaseUUCPLock(path); | ||
| #endif // UUCP_LOCKING | ||
| return false; | ||
| } else { | ||
| OLA_INFO << "Locked " << path << " using flock()"; | ||
| } | ||
| #endif // HAVE_FLOCK | ||
|
|
||
| // As a final safety mechanism, use ioctl(TIOCEXCL) if available to prevent | ||
| // further opens. | ||
| if (!LockTIOCEXCL(*fd, path)) { | ||
| close(*fd); | ||
| #ifdef UUCP_LOCKING | ||
| ReleaseUUCPLock(path); | ||
| #endif // UUCP_LOCKING | ||
| return false; | ||
| } | ||
|
|
||
| return true; | ||
| } | ||
|
|
||
| void ReleaseSerialPortLock(const std::string &path) { | ||
| #ifdef UUCP_LOCKING | ||
| ReleaseUUCPLock(path); | ||
| #else | ||
| OLA_INFO << "No unlock necessary for " << path << " (UUCP locking not used)"; | ||
| #endif // UUCP_LOCKING | ||
| } | ||
|
|
||
|
|
||
| } // namespace io | ||
| } // namespace ola | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,83 @@ | ||
| /* | ||
| * This library is free software; you can redistribute it and/or | ||
| * modify it under the terms of the GNU Lesser General Public | ||
| * License as published by the Free Software Foundation; either | ||
| * version 2.1 of the License, or (at your option) any later version. | ||
| * | ||
| * This library is distributed in the hope that it will be useful, | ||
| * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
| * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | ||
| * Lesser General Public License for more details. | ||
| * | ||
| * You should have received a copy of the GNU Lesser General Public | ||
| * License along with this library; if not, write to the Free Software | ||
| * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA | ||
| * | ||
| * SerialLockTester.cpp | ||
| * Test for serial port locking | ||
| * Copyright (C) 2022 Thomas White | ||
| */ | ||
|
|
||
| #if HAVE_CONFIG_H | ||
| #include <config.h> | ||
| #endif // HAVE_CONFIG_H | ||
|
|
||
| #include <sys/file.h> | ||
| #include <unistd.h> | ||
| #include <fcntl.h> | ||
| #include <cppunit/extensions/HelperMacros.h> | ||
| #include <string> | ||
|
|
||
| #include "ola/io/Serial.h" | ||
| #include "ola/io/IOUtils.h" | ||
| #include "ola/testing/TestUtils.h" | ||
|
|
||
| class SerialLockTest: public CppUnit::TestFixture { | ||
| public: | ||
| CPPUNIT_TEST_SUITE(SerialLockTest); | ||
| CPPUNIT_TEST(testLock); | ||
| CPPUNIT_TEST_SUITE_END(); | ||
|
|
||
| public: | ||
| void testLock(); | ||
| }; | ||
|
|
||
|
|
||
| CPPUNIT_TEST_SUITE_REGISTRATION(SerialLockTest); | ||
|
|
||
| void SerialLockTest::testLock() { | ||
| int fd; | ||
| pid_t our_pid = getpid(); | ||
|
|
||
| std::stringstream str; | ||
| str << "serialLockTestFile." << our_pid << ".pid"; | ||
| const std::string path = str.str(); | ||
|
|
||
| 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_MSG(fd < 0, "Couldn't create test file"); | ||
|
|
||
| #ifdef HAVE_FLOCK | ||
| OLA_ASSERT_TRUE(flock(fd, LOCK_EX | LOCK_NB) != -1); | ||
| #else | ||
|
|
||
| #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 // 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. Perhaps check fd1 is valid/can be written to? |
||
| #endif // HAVE_FLOCK | ||
|
|
||
|
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. Perhaps check fd2 is invalid/can't be written to? |
||
| close(fd); | ||
|
|
||
| OLA_ASSERT_FALSE_MSG(unlink(path.c_str()), | ||
| "Couldn't delete test file"); | ||
| } | ||
Uh oh!
There was an error while loading. Please reload this page.