A class creating advisory lock on provided file.
The file must exist and be openable by the process.
Change-Id: Ia5fa4ef51e1a413beb81efd56f807e1434145691
SET(COMMON_SOURCES
${COMMON_PATH}/config/PathConfig.cpp
${COMMON_PATH}/containers/BinaryQueue.cpp
+ ${COMMON_PATH}/lock/FileLock.cpp
${COMMON_PATH}/log/log.cpp
${COMMON_PATH}/plugin/PluginManager.cpp
${COMMON_PATH}/protocol/ProtocolAdmin.cpp
--- /dev/null
+/*
+ * Copyright (c) 2014 Samsung Electronics Co., Ltd All Rights Reserved
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+/**
+ * @file src/common/exceptions/DatabaseBusyException.h
+ * @author Aleksander Zdyb <a.zdyb@samsung.com>
+ * @version 1.0
+ * @brief This file defines exception thrown when database is busy
+ */
+
+#ifndef SRC_COMMON_EXCEPTIONS_DATABASEBUSYEXCEPTION_H_
+#define SRC_COMMON_EXCEPTIONS_DATABASEBUSYEXCEPTION_H_
+
+#include <string>
+
+#include <exceptions/DatabaseException.h>
+
+namespace Cynara {
+
+class DatabaseBusyException : public DatabaseException {
+public:
+ DatabaseBusyException() : m_message("Database busy") {};
+ virtual ~DatabaseBusyException() {};
+
+ const std::string &message(void) const {
+ return m_message;
+ }
+
+private:
+ std::string m_message;
+};
+
+} /* namespace Cynara */
+
+#endif /* SRC_COMMON_EXCEPTIONS_DATABASEBUSYEXCEPTION_H_ */
--- /dev/null
+/*
+ * Copyright (c) 2014 Samsung Electronics Co., Ltd All Rights Reserved
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+/**
+ * @file src/common/exceptions/FileLockAcquiringException.h
+ * @author Aleksander Zdyb <a.zdyb@samsung.com>
+ * @version 1.0
+ * @brief This file defines exception thrown when file lock cannot be acquired
+ */
+
+#ifndef SRC_COMMON_EXCEPTIONS_FILELOCKACQUIRINGEXCEPTION_H_
+#define SRC_COMMON_EXCEPTIONS_FILELOCKACQUIRINGEXCEPTION_H_
+
+#include <cstring>
+#include <string>
+
+#include <exceptions/Exception.h>
+
+namespace Cynara {
+
+class FileLockAcquiringException {
+public:
+ FileLockAcquiringException(const int errNumber) : m_errno(errNumber) {
+ m_message = "File lock acquiring error [" + std::to_string(errorNumber()) + "]"
+ + " <" + errorString() + ">";
+ };
+ virtual ~FileLockAcquiringException() {};
+
+ const std::string &message(void) const {
+ return m_message;
+ }
+
+ int errorNumber(void) const {
+ return m_errno;
+ }
+
+ const std::string errorString(void) const {
+ return strerror(m_errno);
+ }
+
+private:
+ std::string m_message;
+ int m_errno;
+};
+
+} /* namespace Cynara */
+
+#endif /* SRC_COMMON_EXCEPTIONS_FILELOCKACQUIRINGEXCEPTION_H_ */
--- /dev/null
+/*
+ * Copyright (c) 2014 Samsung Electronics Co., Ltd All Rights Reserved
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+/**
+ * @file src/common/lock/FileLock.cpp
+ * @author Aleksander Zdyb <a.zdyb@samsung.com>
+ * @version 1.0
+ * @brief A class for acquiring and holding file lock
+ */
+
+#include <errno.h>
+#include <fcntl.h>
+#include <string.h>
+#include <sys/file.h>
+#include <sys/stat.h>
+#include <sys/types.h>
+#include <unistd.h>
+
+#include <exceptions/CannotCreateFileException.h>
+#include <exceptions/FileLockAcquiringException.h>
+#include <log/log.h>
+
+#include "FileLock.h"
+
+namespace Cynara {
+
+FileLock::FileLock(const std::string &lockFilename) : m_lockFilename(lockFilename) {
+ m_fd = ::open(m_lockFilename.c_str(), O_RDONLY);
+
+ if (m_fd < 0) {
+ LOGE("Could not open lock file <%s>", lockFilename.c_str());
+ throw FileLockAcquiringException(errno);
+ }
+
+ LOGD("File lock file opened");
+}
+
+FileLock::~FileLock() {
+ ::close(m_fd);
+}
+
+bool FileLock::tryLock(void) {
+ int lock = TEMP_FAILURE_RETRY(::flock(m_fd, LOCK_EX | LOCK_NB));
+
+ if (lock == 0) {
+ LOGI("File lock acquired");
+ return true;
+ } else if (errno == EWOULDBLOCK) {
+ LOGI("File lock NOT acquired");
+ return false;
+ }
+
+ throw FileLockAcquiringException(errno);
+}
+
+void FileLock::lock(void) {
+ int lock = TEMP_FAILURE_RETRY(::flock(m_fd, LOCK_EX));
+
+ if (lock == -1)
+ throw FileLockAcquiringException(errno);
+
+ LOGI("File lock acquired");
+}
+
+} /* namespace Cynara */
--- /dev/null
+/*
+ * Copyright (c) 2014 Samsung Electronics Co., Ltd All Rights Reserved
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+/**
+ * @file src/common/lock/FileLock.h
+ * @author Aleksander Zdyb <a.zdyb@samsung.com>
+ * @version 1.0
+ * @brief A class for acquiring and holding file lock
+ */
+
+#ifndef SRC_COMMON_LOCK_FILELOCK_H_
+#define SRC_COMMON_LOCK_FILELOCK_H_
+
+#include <string>
+
+namespace Cynara {
+
+class FileLock {
+public:
+ explicit FileLock(const std::string &lockFilename);
+ ~FileLock();
+ bool tryLock(void);
+ void lock(void);
+
+private:
+ std::string m_lockFilename;
+ int m_fd;
+};
+
+} /* namespace Cynara */
+
+#endif /* SRC_COMMON_LOCK_FILELOCK_H_ */
class StorageBackend;
typedef std::shared_ptr<StorageBackend> StorageBackendPtr;
+class FileLock;
+typedef std::unique_ptr<FileLock> FileLockUniquePtr;
+
} // namespace Cynara
#endif /* SRC_SERVICE_MAIN_POINTERS_H_ */