/// \brief Constructor
///
/// Creates a interprocess synchronization object
- InterprocessSync(const std::string component_name) : component_name_(component_name) {}
+ InterprocessSync(const std::string component_name) :
+ component_name_(component_name)
+ {}
/// \brief Destructor
virtual ~InterprocessSync() {}
virtual ~InterprocessSyncLocker() {}
protected:
- InterprocessSyncLocker(InterprocessSync* sync) : sync_(sync), is_locked_(false) {}
- InterprocessSync *sync_;
+ InterprocessSyncLocker(InterprocessSync* sync) :
+ sync_(sync), is_locked_(false)
+ {}
+ InterprocessSync* sync_;
bool is_locked_;
};
namespace util {
InterprocessSyncFile::InterprocessSyncFile(const std::string component_name) :
- InterprocessSync(component_name) {
-
+ InterprocessSync(component_name)
+{
std::string lockfile_path = LOCKFILE_DIR;
const char* const env = getenv("B10_FROM_SOURCE");
// Open the lockfile in the constructor so it doesn't do the access
// checks every time a message is logged.
- mode_t mode = umask(0111);
+ const mode_t mode = umask(0111);
fd_ = open(lockfile_path.c_str(), O_CREAT | O_RDWR, 0660);
umask(mode);
if (fd_ == -1) {
isc_throw(InterprocessSyncFileError,
- "Unable to use interprocess sync lockfile: " + lockfile_path);
+ "Unable to use interprocess sync lockfile: " +
+ lockfile_path);
}
}
InterprocessSyncLocker*
InterprocessSyncFile::getLocker() {
- InterprocessSyncLocker *locker = new InterprocessSyncFileLocker(this);
- return locker;
+ InterprocessSyncLocker* locker = new InterprocessSyncFileLocker(this);
+ return (locker);
}
///////////////////////////////////////////////////////////////////////////////////
-InterprocessSyncFileLocker::InterprocessSyncFileLocker(InterprocessSync* sync) :
- InterprocessSyncLocker(sync) {
+InterprocessSyncFileLocker::InterprocessSyncFileLocker(InterprocessSync* sync)
+ : InterprocessSyncLocker(sync)
+{
}
InterprocessSyncFileLocker::~InterprocessSyncFileLocker() {
return (true);
}
- InterprocessSyncFile *sync = dynamic_cast<InterprocessSyncFile*>(sync_);
- int fd = sync->getFd();
+ InterprocessSyncFile* sync = dynamic_cast<InterprocessSyncFile*>(sync_);
+ const int fd = sync->getFd();
if (fd != -1) {
struct flock lock;
- int status;
// Acquire the exclusive lock
memset(&lock, 0, sizeof lock);
lock.l_start = 0;
lock.l_len = 1;
- status = fcntl(fd, F_SETLKW, &lock);
+ const int status = fcntl(fd, F_SETLKW, &lock);
if (status == 0) {
is_locked_ = true;
return (true);
return (true);
}
- InterprocessSyncFile *sync = dynamic_cast<InterprocessSyncFile*>(sync_);
- int fd = sync->getFd();
+ InterprocessSyncFile* sync = dynamic_cast<InterprocessSyncFile*>(sync_);
+ const int fd = sync->getFd();
if (fd != -1) {
struct flock lock;
- int status;
// Acquire the exclusive lock
memset(&lock, 0, sizeof lock);
lock.l_start = 0;
lock.l_len = 1;
- status = fcntl(fd, F_SETLK, &lock);
+ const int status = fcntl(fd, F_SETLK, &lock);
if (status == 0) {
is_locked_ = true;
return (true);
}
InterprocessSyncFile *sync = dynamic_cast<InterprocessSyncFile*>(sync_);
- int fd = sync->getFd();
+ const int fd = sync->getFd();
if (fd != -1) {
struct flock lock;
- int status;
// Release the exclusive lock
memset(&lock, 0, sizeof lock);
lock.l_start = 0;
lock.l_len = 1;
- status = fcntl(fd, F_SETLKW, &lock);
+ const int status = fcntl(fd, F_SETLKW, &lock);
if (status == 0) {
is_locked_ = false;
return (true);
///
class InterprocessSyncFileError : public Exception {
public:
- InterprocessSyncFileError(const char* file, size_t line, const char* what) :
+ InterprocessSyncFileError(const char* file, size_t line,
+ const char* what) :
isc::Exception(file, line, what) {}
};
InterprocessSyncLocker* getLocker();
- int getFd() {
- return fd_;
+ int getFd() const {
+ return (fd_);
}
private:
};
TEST_F(InterprocessSyncFileTest, TestLock) {
- InterprocessSync *sync = new InterprocessSyncFile("test");
- InterprocessSyncLocker *locker = sync->getLocker();
+ InterprocessSync* sync = new InterprocessSyncFile("test");
+ InterprocessSyncLocker* locker = sync->getLocker();
EXPECT_TRUE(locker->lock());
// Child writes to pipe
close(fds[0]);
- InterprocessSync *sync2 = new InterprocessSyncFile("test");
- InterprocessSyncLocker *locker2 = sync2->getLocker();
+ InterprocessSync* sync2 = new InterprocessSyncFile("test");
+ InterprocessSyncLocker* locker2 = sync2->getLocker();
if (!locker2->tryLock()) {
locked = 1;
delete locker2;
delete sync2;
- write(fds[1], &locked, sizeof locked);
+ write(fds[1], &locked, sizeof(locked));
close(fds[1]);
exit(0);
} else {
close(fds[1]);
// Read status and set flag
- read(fds[0], &locked, sizeof locked);
+ read(fds[0], &locked, sizeof(locked));
if (locked == 1) {
was_locked = true;
} else {
}
TEST_F(InterprocessSyncFileTest, TestMultipleFilesDirect) {
- InterprocessSync *sync = new InterprocessSyncFile("test1");
- InterprocessSyncLocker *locker = sync->getLocker();
+ InterprocessSync* sync = new InterprocessSyncFile("test1");
+ InterprocessSyncLocker* locker = sync->getLocker();
EXPECT_TRUE(locker->lock());
- InterprocessSync *sync2 = new InterprocessSyncFile("test2");
- InterprocessSyncLocker *locker2 = sync2->getLocker();
+ InterprocessSync* sync2 = new InterprocessSyncFile("test2");
+ InterprocessSyncLocker* locker2 = sync2->getLocker();
EXPECT_TRUE(locker2->lock());
EXPECT_TRUE(locker2->unlock());
delete sync2;
}
TEST_F(InterprocessSyncFileTest, TestMultipleFilesForked) {
- InterprocessSync *sync = new InterprocessSyncFile("test");
- InterprocessSyncLocker *locker = sync->getLocker();
+ InterprocessSync* sync = new InterprocessSyncFile("test");
+ InterprocessSyncLocker* locker = sync->getLocker();
EXPECT_TRUE(locker->lock());
// Child writes to pipe
close(fds[0]);
- InterprocessSync *sync2 = new InterprocessSyncFile("test2");
- InterprocessSyncLocker *locker2 = sync2->getLocker();
+ InterprocessSync* sync2 = new InterprocessSyncFile("test2");
+ InterprocessSyncLocker* locker2 = sync2->getLocker();
if (locker2->tryLock()) {
locked = 0;
delete locker2;
delete sync2;
- write(fds[1], &locked, sizeof locked);
+ write(fds[1], &locked, sizeof(locked));
close(fds[1]);
exit(0);
} else {
close(fds[1]);
// Read status and set flag
- read(fds[0], &locked, sizeof locked);
+ read(fds[0], &locked, sizeof(locked));
if (locked == 0) {
was_not_locked = true;
} else {