BIND 10 trac1704, updated. edb1ef2c48d4be26c1b52b992910f9bc3adb2d00 [1704] Move the actual fd locking ops to a separate class to work with exceptions
BIND 10 source code commits
bind10-changes at lists.isc.org
Mon May 21 07:37:30 UTC 2012
The branch, trac1704 has been updated
via edb1ef2c48d4be26c1b52b992910f9bc3adb2d00 (commit)
from a90425936c557c3c0fce2c6e82e7d649ccb05a8b (commit)
Those revisions listed above that are new to this repository have
not appeared on any other notification email; so we list those
revisions in full, below.
- Log -----------------------------------------------------------------
commit edb1ef2c48d4be26c1b52b992910f9bc3adb2d00
Author: Mukund Sivaraman <muks at isc.org>
Date: Mon May 21 13:04:47 2012 +0530
[1704] Move the actual fd locking ops to a separate class to work with exceptions
-----------------------------------------------------------------------
Summary of changes:
src/lib/log/logger_impl.cc | 28 +------
src/lib/util/Makefile.am | 1 +
src/lib/util/file_lock.cc | 84 ++++++++++++++++++++
.../util/{unittests/resource.cc => file_lock.h} | 37 +++++----
4 files changed, 112 insertions(+), 38 deletions(-)
create mode 100644 src/lib/util/file_lock.cc
copy src/lib/util/{unittests/resource.cc => file_lock.h} (65%)
-----------------------------------------------------------------------
diff --git a/src/lib/log/logger_impl.cc b/src/lib/log/logger_impl.cc
index d36721b..0a1cb91 100644
--- a/src/lib/log/logger_impl.cc
+++ b/src/lib/log/logger_impl.cc
@@ -18,12 +18,9 @@
#include <stdarg.h>
#include <stdio.h>
-#include <stdlib.h>
-#include <string.h>
-#include <unistd.h>
-#include <fcntl.h>
#include <sys/types.h>
#include <sys/stat.h>
+#include <fcntl.h>
#include <boost/lexical_cast.hpp>
#include <boost/static_assert.hpp>
@@ -38,6 +35,7 @@
#include <log/message_types.h>
#include <util/strutil.h>
+#include <util/file_lock.h>
// Note: as log4cplus and the BIND 10 logger have many concepts in common, and
// thus many similar names, to disambiguate types we don't "use" the log4cplus
@@ -141,21 +139,12 @@ LoggerImpl::outputRaw(const Severity& severity, const string& message) {
// Use a lock file for mutual exclusion from other processes to
// avoid log messages getting interspersed
- struct flock lock;
- int status;
+ isc::util::file_lock l(lock_fd_);
if (lock_fd_ != -1) {
- // Acquire the exclusive lock
- memset(&lock, 0, sizeof lock);
- lock.l_type = F_WRLCK;
- lock.l_whence = SEEK_SET;
- lock.l_start = 0;
- lock.l_len = 1;
- status = fcntl(lock_fd_, F_SETLKW, &lock);
- if (status != 0) {
+ if (!l.lock()) {
LOG4CPLUS_ERROR(logger_, "Unable to lock logger lockfile: " +
lockfile_path_);
- return;
}
}
@@ -182,14 +171,7 @@ LoggerImpl::outputRaw(const Severity& severity, const string& message) {
}
if (lock_fd_ != -1) {
- // Release the exclusive lock
- memset(&lock, 0, sizeof lock);
- lock.l_type = F_UNLCK;
- lock.l_whence = SEEK_SET;
- lock.l_start = 0;
- lock.l_len = 1;
- status = fcntl(lock_fd_, F_SETLKW, &lock);
- if (status != 0) {
+ if (!l.unlock()) {
LOG4CPLUS_ERROR(logger_, "Unable to unlock logger lockfile: " +
lockfile_path_);
}
diff --git a/src/lib/util/Makefile.am b/src/lib/util/Makefile.am
index c2b3020..8e3503c 100644
--- a/src/lib/util/Makefile.am
+++ b/src/lib/util/Makefile.am
@@ -13,6 +13,7 @@ libutil_la_SOURCES += strutil.h strutil.cc
libutil_la_SOURCES += buffer.h io_utilities.h
libutil_la_SOURCES += time_utilities.h time_utilities.cc
libutil_la_SOURCES += range_utilities.h
+libutil_la_SOURCES += file_lock.h file_lock.cc
libutil_la_SOURCES += hash/sha1.h hash/sha1.cc
libutil_la_SOURCES += encode/base16_from_binary.h
libutil_la_SOURCES += encode/base32hex.h encode/base64.h
diff --git a/src/lib/util/file_lock.cc b/src/lib/util/file_lock.cc
new file mode 100644
index 0000000..8759636
--- /dev/null
+++ b/src/lib/util/file_lock.cc
@@ -0,0 +1,84 @@
+// Copyright (C) 2012 Internet Systems Consortium, Inc. ("ISC")
+//
+// Permission to use, copy, modify, and/or distribute this software for any
+// purpose with or without fee is hereby granted, provided that the above
+// copyright notice and this permission notice appear in all copies.
+//
+// THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
+// REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
+// AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
+// INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
+// LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
+// OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
+// PERFORMANCE OF THIS SOFTWARE.
+
+#include "file_lock.h"
+
+#include <stdlib.h>
+#include <string.h>
+#include <unistd.h>
+#include <fcntl.h>
+
+namespace isc {
+namespace util {
+
+file_lock::~file_lock() {
+ unlock();
+}
+
+bool
+file_lock::lock() {
+ if (is_locked_) {
+ return true;
+ }
+
+ if (fd_ != -1) {
+ struct flock lock;
+ int status;
+
+ // Acquire the exclusive lock
+ memset(&lock, 0, sizeof lock);
+ lock.l_type = F_WRLCK;
+ lock.l_whence = SEEK_SET;
+ lock.l_start = 0;
+ lock.l_len = 1;
+
+ status = fcntl(fd_, F_SETLKW, &lock);
+ if (status == 0) {
+ is_locked_ = true;
+ return true;
+ }
+ }
+
+ return false;
+}
+
+bool
+file_lock::unlock() {
+ if (!is_locked_) {
+ return true;
+ }
+
+ if (fd_ != -1) {
+ struct flock lock;
+ int status;
+
+ // Release the exclusive lock
+ memset(&lock, 0, sizeof lock);
+ lock.l_type = F_UNLCK;
+ lock.l_whence = SEEK_SET;
+ lock.l_start = 0;
+ lock.l_len = 1;
+
+ status = fcntl(fd_, F_SETLKW, &lock);
+ if (status == 0) {
+ is_locked_ = false;
+ return true;
+ }
+ }
+
+ return false;
+}
+
+} // namespace util
+} // namespace isc
diff --git a/src/lib/util/file_lock.h b/src/lib/util/file_lock.h
new file mode 100644
index 0000000..eb0cd8f
--- /dev/null
+++ b/src/lib/util/file_lock.h
@@ -0,0 +1,42 @@
+// Copyright (C) 2012 Internet Systems Consortium, Inc. ("ISC")
+//
+// Permission to use, copy, modify, and/or distribute this software for any
+// purpose with or without fee is hereby granted, provided that the above
+// copyright notice and this permission notice appear in all copies.
+//
+// THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
+// REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
+// AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
+// INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
+// LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
+// OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
+// PERFORMANCE OF THIS SOFTWARE.
+
+#ifndef __FILE_LOCK_H__
+#define __FILE_LOCK_H__
+
+namespace isc {
+namespace util {
+
+class file_lock {
+public:
+ /// \brief Constructor
+ ///
+ /// Creates a file locker on the specific fd (for mutual exclusion).
+ file_lock(int fd) : fd_(fd), is_locked_(false) {
+ }
+
+ /// \brief Destructor
+ virtual ~file_lock();
+
+ bool lock();
+ bool unlock();
+private:
+ int fd_;
+ bool is_locked_;
+};
+
+} // namespace util
+} // namespace isc
+
+#endif // __FILE_LOCK_H__
More information about the bind10-changes
mailing list