tizen 2.4 release
[framework/web/wrt-commons.git] / modules / core / src / single_instance.cpp
1 /*
2  * Copyright (c) 2011 Samsung Electronics Co., Ltd All Rights Reserved
3  *
4  *    Licensed under the Apache License, Version 2.0 (the "License");
5  *    you may not use this file except in compliance with the License.
6  *    You may obtain a copy of the License at
7  *
8  *        http://www.apache.org/licenses/LICENSE-2.0
9  *
10  *    Unless required by applicable law or agreed to in writing, software
11  *    distributed under the License is distributed on an "AS IS" BASIS,
12  *    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  *    See the License for the specific language governing permissions and
14  *    limitations under the License.
15  */
16 /*
17  * @file        single_instance.cpp
18  * @author      Przemyslaw Dobrowolski (p.dobrowolsk@samsung.com)
19  * @version     1.0
20  * @brief       This file is the implementation file of single instance
21  */
22 #include <stddef.h>
23 #include <dpl/single_instance.h>
24 #include <dpl/log/wrt_log.h>
25 #include <unistd.h>
26 #include <fcntl.h>
27 #include <errno.h>
28 #include <dpl/assert.h>
29
30 namespace DPL {
31 namespace // anonumous
32 {
33 const char *LOCK_PREFIX_PATH = "/tmp/dpl_single_instance_";
34 }
35 SingleInstance::SingleInstance() :
36     m_locked(false),
37     m_fdLock(-1)
38 {}
39
40 SingleInstance::~SingleInstance()
41 {
42     AssertMsg(!m_locked, "Single instance must be released before exit!");
43 }
44
45 bool SingleInstance::TryLock(const std::string &lockName)
46 {
47     WrtLogD("Locking single instance: %s", lockName.c_str());
48
49     struct flock lock;
50
51     lock.l_type = F_WRLCK;
52     lock.l_whence = SEEK_SET;
53     lock.l_start = 0;
54     lock.l_len = 1;
55
56     // Open lock file
57     m_fdLock =
58         TEMP_FAILURE_RETRY(open((std::string(LOCK_PREFIX_PATH) +
59                                  lockName).c_str(),
60                                 O_WRONLY | O_CREAT, 0666));
61
62     if (m_fdLock == -1) {
63         ThrowMsg(Exception::LockError, "Cannot open single instance lock file!");
64     }
65
66     // Lock file
67     int result = TEMP_FAILURE_RETRY(fcntl(m_fdLock, F_SETLK, &lock));
68
69     // Was the instance successfuly locked ?
70     if (result == 0) {
71         WrtLogD("Instance locked: %s", lockName.c_str());
72
73         // It is locked now
74         m_locked = true;
75
76         // Done
77         return true;
78     }
79
80     if (errno == EACCES || errno == EAGAIN) {
81         WrtLogD("Instance is already running: %s", lockName.c_str());
82         return false;
83     }
84
85     // This is lock error
86     ThrowMsg(Exception::LockError, "Cannot lock single instance lock file!");
87 }
88
89 void SingleInstance::Release()
90 {
91     if (!m_locked) {
92         return;
93     }
94
95     WrtLogD("Unlocking single instance");
96
97     // Unlock file
98     struct flock lock;
99
100     lock.l_type = F_UNLCK;
101     lock.l_whence = SEEK_SET;
102     lock.l_start = 0;
103     lock.l_len = 1;
104
105     int result = TEMP_FAILURE_RETRY(fcntl(m_fdLock, F_SETLK, &lock));
106
107     // Was the instance successfuly unlocked ?
108     if (result == -1) {
109         ThrowMsg(Exception::LockError,
110                  "Cannot unlock single instance lock file!");
111     }
112
113     // Close lock file
114     if (TEMP_FAILURE_RETRY(close(m_fdLock)) == -1) {
115         ThrowMsg(Exception::LockError,
116                  "Cannot close single instance lock file!");
117     }
118
119     m_fdLock = -1;
120
121     // Done
122     m_locked = false;
123     WrtLogD("Instance unlocked");
124 }
125 } // namespace DPL