Update User Agent String
[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 <dpl/single_instance.h>
23 #include <dpl/log/log.h>
24 #include <unistd.h>
25 #include <fcntl.h>
26 #include <errno.h>
27 #include <dpl/assert.h>
28
29 namespace DPL
30 {
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
41 SingleInstance::~SingleInstance()
42 {
43     Assert(!m_locked && "Single instance must be released before exit!");
44 }
45
46 bool SingleInstance::TryLock(const std::string &lockName)
47 {
48     LogPedantic("Locking single instance: " << lockName);
49
50     struct flock lock;
51
52     lock.l_type = F_WRLCK;
53     lock.l_whence = SEEK_SET;
54     lock.l_start = 0;
55     lock.l_len = 1;
56
57     // Open lock file
58     m_fdLock = TEMP_FAILURE_RETRY(open((std::string(LOCK_PREFIX_PATH) + lockName).c_str(), O_WRONLY | O_CREAT, 0666));
59
60     if (m_fdLock == -1)
61         ThrowMsg(Exception::LockError, "Cannot open single instance lock file!");
62
63     // Lock file
64     int result = TEMP_FAILURE_RETRY(fcntl(m_fdLock, F_SETLK, &lock));
65
66     // Was the instance successfuly locked ?
67     if (result == 0)
68     {
69         LogPedantic("Instance locked: " << lockName);
70
71         // It is locked now
72         m_locked = true;
73
74         // Done
75         return true;
76     }
77
78     if (errno == EACCES || errno == EAGAIN)
79     {
80         LogPedantic("Instance is already running: " << lockName);
81         return false;
82     }
83
84     // This is lock error
85     ThrowMsg(Exception::LockError, "Cannot lock single instance lock file!");
86 }
87
88 void SingleInstance::Release()
89 {
90     if (!m_locked)
91         return;
92
93     LogPedantic("Unlocking single instance");
94
95     // Unlock file
96     struct flock lock;
97
98     lock.l_type = F_UNLCK;
99     lock.l_whence = SEEK_SET;
100     lock.l_start = 0;
101     lock.l_len = 1;
102
103     int result = TEMP_FAILURE_RETRY(fcntl(m_fdLock, F_SETLK, &lock));
104
105     // Was the instance successfuly unlocked ?
106     if (result == -1)
107         ThrowMsg(Exception::LockError, "Cannot unlock single instance lock file!");
108
109     // Close lock file
110     if (TEMP_FAILURE_RETRY(close(m_fdLock)) == -1)
111         ThrowMsg(Exception::LockError, "Cannot close single instance lock file!");
112
113     m_fdLock = -1;
114
115     // Done
116     m_locked = false;
117     LogPedantic("Instance unlocked");
118 }
119 } // namespace DPL