1 /****************************************************************************
2 **
3 ** Copyright (C) 2015 The Qt Company Ltd.
4 ** Contact: http://www.qt.io/licensing/
5 **
6 ** This file is part of the QtGui module of the Qt Toolkit.
7 **
8 ** $QT_BEGIN_LICENSE:LGPL$
9 ** Commercial License Usage
10 ** Licensees holding valid commercial Qt licenses may use this file in
11 ** accordance with the commercial license agreement provided with the
12 ** Software or, alternatively, in accordance with the terms contained in
13 ** a written agreement between you and The Qt Company. For licensing terms
14 ** and conditions see http://www.qt.io/terms-conditions. For further
15 ** information use the contact form at http://www.qt.io/contact-us.
16 **
17 ** GNU Lesser General Public License Usage
18 ** Alternatively, this file may be used under the terms of the GNU Lesser
19 ** General Public License version 2.1 or version 3 as published by the Free
20 ** Software Foundation and appearing in the file LICENSE.LGPLv21 and
21 ** LICENSE.LGPLv3 included in the packaging of this file. Please review the
22 ** following information to ensure the GNU Lesser General Public License
23 ** requirements will be met: https://www.gnu.org/licenses/lgpl.html and
24 ** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
25 **
26 ** As a special exception, The Qt Company gives you certain additional
27 ** rights. These rights are described in The Qt Company LGPL Exception
28 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
29 **
30 ** GNU General Public License Usage
31 ** Alternatively, this file may be used under the terms of the GNU
32 ** General Public License version 3.0 as published by the Free Software
33 ** Foundation and appearing in the file LICENSE.GPL included in the
34 ** packaging of this file. Please review the following information to
35 ** ensure the GNU General Public License version 3.0 requirements will be
36 ** met: http://www.gnu.org/copyleft/gpl.html.
37 **
38 ** $QT_END_LICENSE$
39 **
40 ****************************************************************************/
41
42 #include "qwslock_p.h"
43
44 #ifndef QT_NO_QWS_MULTIPROCESS
45
46 #include "qwssignalhandler_p.h"
47
48 #include <stdint.h>
49 #include <stdio.h>
50 #include <errno.h>
51 #include <string.h>
52 #include <sys/types.h>
53 #include <sys/ipc.h>
54 #ifndef QT_POSIX_IPC
55 #include <sys/sem.h>
56 #endif
57 #include <sys/time.h>
58 #include <time.h>
59 #ifdef Q_OS_LINUX
60 #include <linux/version.h>
61 #endif
62 #include <unistd.h>
63
64 #include <private/qcore_unix_p.h>
65
66 QT_BEGIN_NAMESPACE
67
68 #ifdef QT_NO_SEMAPHORE
69 #error QWSLock currently requires semaphores
70 #endif
71
72 #ifdef QT_POSIX_IPC
73 #include <QtCore/QAtomicInt>
74
75 static QBasicAtomicInt localUniqueId = Q_BASIC_ATOMIC_INITIALIZER(1);
76 #endif
77
QWSLock(int id)78 QWSLock::QWSLock(int id) : semId(id)
79 {
80 static unsigned short initialValues[3] = { 1, 1, 0 };
81
82 #ifndef QT_NO_QWS_SIGNALHANDLER
83 QWSSignalHandler::instance()->addWSLock(this);
84 #endif
85
86 #ifndef QT_POSIX_IPC
87 if (semId == -1) {
88 semId = semget(IPC_PRIVATE, 3, IPC_CREAT | 0666);
89 if (semId == -1) {
90 perror("QWSLock::QWSLock");
91 qFatal("Unable to create semaphore");
92 }
93
94 qt_semun semval;
95 semval.array = initialValues;
96 if (semctl(semId, 0, SETALL, semval) == -1) {
97 perror("QWSLock::QWSLock");
98 qFatal("Unable to initialize semaphores");
99 }
100 }
101 #else
102 sems[0] = sems[1] = sems[2] = SEM_FAILED;
103 owned = false;
104
105 if (semId == -1) {
106 // ### generate really unique IDs
107 semId = (getpid() << 16) + (localUniqueId.fetchAndAddRelaxed(1) % ushort(-1));
108 owned = true;
109 }
110
111 QByteArray pfx = "/qwslock_" + QByteArray::number(semId, 16) + '_';
112 QByteArray keys[3] = { pfx + "BackingStore", pfx + "Communication", pfx + "RegionEvent" };
113 for (int i = 0; i < 3; ++i) {
114 if (owned)
115 sem_unlink(keys[i].constData());
116 do {
117 sems[i] = sem_open(keys[i].constData(), (owned ? O_CREAT : 0), 0666, initialValues[i]);
118 } while (sems[i] == SEM_FAILED && errno == EINTR);
119 if (sems[i] == SEM_FAILED) {
120 perror("QWSLock::QWSLock");
121 qFatal("Unable to %s semaphore", (owned ? "create" : "open"));
122 }
123 }
124 #endif
125
126 lockCount[0] = lockCount[1] = 0;
127 }
128
~QWSLock()129 QWSLock::~QWSLock()
130 {
131 #ifndef QT_NO_QWS_SIGNALHANDLER
132 QWSSignalHandler::instance()->removeWSLock(this);
133 #endif
134
135 if (semId != -1) {
136 #ifndef QT_POSIX_IPC
137 qt_semun semval;
138 semval.val = 0;
139 semctl(semId, 0, IPC_RMID, semval);
140 semId = -1;
141 #else
142 // emulate the SEM_UNDO behavior for the BackingStore lock
143 while (hasLock(BackingStore))
144 unlock(BackingStore);
145
146 QByteArray pfx = "/qwslock_" + QByteArray::number(semId, 16) + '_';
147 QByteArray keys[3] = { pfx + "BackingStore", pfx + "Communication", pfx + "RegionEvent" };
148 for (int i = 0; i < 3; ++i) {
149 if (sems[i] != SEM_FAILED) {
150 sem_close(sems[i]);
151 sems[i] = SEM_FAILED;
152 }
153 if (owned)
154 sem_unlink(keys[i].constData());
155 }
156 #endif
157 }
158 }
159
up(unsigned short semNum)160 bool QWSLock::up(unsigned short semNum)
161 {
162 int ret;
163
164 #ifndef QT_POSIX_IPC
165 sembuf sops = { semNum, 1, 0 };
166 // As the BackingStore lock is a mutex, and only one process may own
167 // the lock, it's safe to use SEM_UNDO. On the other hand, the
168 // Communication lock is locked by the client but unlocked by the
169 // server and therefore can't use SEM_UNDO.
170 if (semNum == BackingStore)
171 sops.sem_flg |= SEM_UNDO;
172
173 EINTR_LOOP(ret, semop(semId, &sops, 1));
174 #else
175 ret = sem_post(sems[semNum]);
176 #endif
177 if (ret == -1) {
178 qDebug("QWSLock::up(): %s", strerror(errno));
179 return false;
180 }
181
182 return true;
183 }
184
down(unsigned short semNum,int)185 bool QWSLock::down(unsigned short semNum, int)
186 {
187 int ret;
188
189 #ifndef QT_POSIX_IPC
190 sembuf sops = { semNum, -1, 0 };
191 // As the BackingStore lock is a mutex, and only one process may own
192 // the lock, it's safe to use SEM_UNDO. On the other hand, the
193 // Communication lock is locked by the client but unlocked by the
194 // server and therefore can't use SEM_UNDO.
195 if (semNum == BackingStore)
196 sops.sem_flg |= SEM_UNDO;
197
198 EINTR_LOOP(ret, semop(semId, &sops, 1));
199 #else
200 EINTR_LOOP(ret, sem_wait(sems[semNum]));
201 #endif
202 if (ret == -1) {
203 qDebug("QWSLock::down(): %s", strerror(errno));
204 return false;
205 }
206
207 return true;
208 }
209
getValue(unsigned short semNum) const210 int QWSLock::getValue(unsigned short semNum) const
211 {
212 int ret;
213 #ifndef QT_POSIX_IPC
214 ret = semctl(semId, semNum, GETVAL, 0);
215 #else
216 if (sem_getvalue(sems[semNum], &ret) == -1)
217 ret = -1;
218 #endif
219 if (ret == -1)
220 qDebug("QWSLock::getValue(): %s", strerror(errno));
221 return ret;
222 }
223
lock(LockType type,int timeout)224 bool QWSLock::lock(LockType type, int timeout)
225 {
226 if (type == RegionEvent)
227 return up(type);
228
229 if (lockCount[type] > 0) {
230 ++lockCount[type];
231 return true;
232 }
233
234 if (down(type, timeout)) {
235 ++lockCount[type];
236 return true;
237 }
238
239 return false;
240 }
241
hasLock(LockType type)242 bool QWSLock::hasLock(LockType type)
243 {
244 if (type == RegionEvent)
245 return getValue(type) == 0;
246
247 return lockCount[type] > 0;
248 }
249
unlock(LockType type)250 void QWSLock::unlock(LockType type)
251 {
252 if (type == RegionEvent) {
253 down(type, -1);
254 return;
255 }
256
257 if (lockCount[type] > 0) {
258 --lockCount[type];
259 if (lockCount[type] > 0)
260 return;
261 }
262
263 up(type);
264 }
265
wait(LockType type,int timeout)266 bool QWSLock::wait(LockType type, int timeout)
267 {
268 bool ok = down(type, timeout);
269 if (ok)
270 unlock(type);
271 return ok;
272 }
273
274 QT_END_NAMESPACE
275
276 #endif // QT_NO_QWS_MULTIPROCESS
277