From: Aleksander Zdyb Date: Wed, 26 Nov 2014 08:23:06 +0000 (+0100) Subject: Add FileLock class X-Git-Tag: accepted/tizen/common/20150119.084431~81 X-Git-Url: http://review.tizen.org/git/?a=commitdiff_plain;h=6fc739e0eeacb82e9ace84c793f365fdcb529b88;p=platform%2Fcore%2Fsecurity%2Fcynara.git Add FileLock class A class creating advisory lock on provided file. The file must exist and be openable by the process. Change-Id: Ia5fa4ef51e1a413beb81efd56f807e1434145691 --- diff --git a/src/common/CMakeLists.txt b/src/common/CMakeLists.txt index e8974cb..a8e0632 100644 --- a/src/common/CMakeLists.txt +++ b/src/common/CMakeLists.txt @@ -28,6 +28,7 @@ INCLUDE_DIRECTORIES( 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 diff --git a/src/common/exceptions/DatabaseBusyException.h b/src/common/exceptions/DatabaseBusyException.h new file mode 100644 index 0000000..78c90cb --- /dev/null +++ b/src/common/exceptions/DatabaseBusyException.h @@ -0,0 +1,47 @@ +/* + * 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 + * @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 + +#include + +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_ */ diff --git a/src/common/exceptions/FileLockAcquiringException.h b/src/common/exceptions/FileLockAcquiringException.h new file mode 100644 index 0000000..44bc54a --- /dev/null +++ b/src/common/exceptions/FileLockAcquiringException.h @@ -0,0 +1,60 @@ +/* + * 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 + * @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 +#include + +#include + +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_ */ diff --git a/src/common/lock/FileLock.cpp b/src/common/lock/FileLock.cpp new file mode 100644 index 0000000..b8bd34a --- /dev/null +++ b/src/common/lock/FileLock.cpp @@ -0,0 +1,77 @@ +/* + * 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 + * @version 1.0 + * @brief A class for acquiring and holding file lock + */ + +#include +#include +#include +#include +#include +#include +#include + +#include +#include +#include + +#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 */ diff --git a/src/common/lock/FileLock.h b/src/common/lock/FileLock.h new file mode 100644 index 0000000..53f048f --- /dev/null +++ b/src/common/lock/FileLock.h @@ -0,0 +1,44 @@ +/* + * 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 + * @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 + +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_ */ diff --git a/src/service/main/pointers.h b/src/service/main/pointers.h index f32c4ab..db734ae 100644 --- a/src/service/main/pointers.h +++ b/src/service/main/pointers.h @@ -45,6 +45,9 @@ typedef std::shared_ptr StoragePtr; class StorageBackend; typedef std::shared_ptr StorageBackendPtr; +class FileLock; +typedef std::unique_ptr FileLockUniquePtr; + } // namespace Cynara #endif /* SRC_SERVICE_MAIN_POINTERS_H_ */