620d757955aed093fc4f0b8b89cbff029bc1ff1b
[platform/core/security/security-manager.git] / src / common / include / file-lock.h
1 /*
2  *  Copyright (c) 2000 - 2014 Samsung Electronics Co., Ltd All Rights Reserved
3  *
4  *  Contact: Rafal Krypa <r.krypa@samsung.com>
5  *
6  *  Licensed under the Apache License, Version 2.0 (the "License");
7  *  you may not use this file except in compliance with the License.
8  *  You may obtain a copy of the License at
9  *
10  *      http://www.apache.org/licenses/LICENSE-2.0
11  *
12  *  Unless required by applicable law or agreed to in writing, software
13  *  distributed under the License is distributed on an "AS IS" BASIS,
14  *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  *  See the License for the specific language governing permissions and
16  *  limitations under the License
17  */
18 /*
19  * @file        file-lock.h
20  * @author      Sebastian Grabowski (s.grabowski@samsung.com)
21  * @version     1.0
22  * @brief       Implementation of simple file locking for a service
23  */
24 /* vim: set ts=4 et sw=4 tw=78 : */
25
26 #pragma once
27
28 #include <boost/interprocess/sync/file_lock.hpp>
29
30 #include <dpl/exception.h>
31 #include <dpl/noncopyable.h>
32 #include <tzplatform_config.h>
33
34 namespace SecurityManager {
35
36 extern char const * const SERVICE_LOCK_FILE;
37
38 class FileLocker :
39     public Noncopyable
40 {
41 public:
42     class Exception
43     {
44     public:
45         DECLARE_EXCEPTION_TYPE(SecurityManager::Exception, Base)
46         DECLARE_EXCEPTION_TYPE(Base, LockFailed)
47     };
48
49     FileLocker(const std::string &lockFile, bool blocking = false);
50     ~FileLocker();
51
52     bool Locked();
53
54 protected:
55     void Lock();
56     void Unlock();
57
58 private:
59     std::string m_lockFile;
60     boost::interprocess::file_lock m_flock;
61     bool m_blocking;
62     bool m_locked;
63 };
64
65 } // namespace SecurityManager
66