1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
|
/****************************************************************************
**
** Copyright (C) 2012 Nokia Corporation and/or its subsidiary(-ies).
** All rights reserved.
** Contact: Nokia Corporation (qt-info@nokia.com)
**
** This file is part of the QtCore module of the Qt Toolkit.
**
** $QT_BEGIN_LICENSE:LGPL$
** GNU Lesser General Public License Usage
** This file may be used under the terms of the GNU Lesser General Public
** License version 2.1 as published by the Free Software Foundation and
** appearing in the file LICENSE.LGPL included in the packaging of this
** file. Please review the following information to ensure the GNU Lesser
** General Public License version 2.1 requirements will be met:
** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
**
** In addition, as a special exception, Nokia gives you certain additional
** rights. These rights are described in the Nokia Qt LGPL Exception
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
**
** GNU General Public License Usage
** Alternatively, this file may be used under the terms of the GNU General
** Public License version 3.0 as published by the Free Software Foundation
** and appearing in the file LICENSE.GPL included in the packaging of this
** file. Please review the following information to ensure the GNU General
** Public License version 3.0 requirements will be met:
** http://www.gnu.org/copyleft/gpl.html.
**
** Other Usage
** Alternatively, this file may be used in accordance with the terms and
** conditions contained in a signed written agreement between you and Nokia.
**
**
**
**
**
** $QT_END_LICENSE$
**
****************************************************************************/
#include "qsystemsemaphore.h"
#include "qsystemsemaphore_p.h"
#include <qcoreapplication.h>
#include <qdebug.h>
#include <qfile.h>
#ifndef QT_NO_SYSTEMSEMAPHORE
#include <sys/types.h>
#include <sys/ipc.h>
#ifndef QT_POSIX_IPC
#include <sys/sem.h>
#endif
#include <fcntl.h>
#include <errno.h>
#include "private/qcore_unix_p.h"
// OpenBSD 4.2 doesn't define EIDRM, see BUGS section:
// http://www.openbsd.org/cgi-bin/man.cgi?query=semop&manpath=OpenBSD+4.2
#if defined(Q_OS_OPENBSD) && !defined(EIDRM)
#define EIDRM EINVAL
#endif
//#define QSYSTEMSEMAPHORE_DEBUG
QT_BEGIN_NAMESPACE
QSystemSemaphorePrivate::QSystemSemaphorePrivate() :
#ifndef QT_POSIX_IPC
unix_key(-1), semaphore(-1), createdFile(false),
#else
semaphore(SEM_FAILED),
#endif
createdSemaphore(false), error(QSystemSemaphore::NoError)
{
}
void QSystemSemaphorePrivate::setErrorString(const QString &function)
{
// EINVAL is handled in functions so they can give better error strings
switch (errno) {
case EPERM:
case EACCES:
errorString = QCoreApplication::translate("QSystemSemaphore", "%1: permission denied").arg(function);
error = QSystemSemaphore::PermissionDenied;
break;
case EEXIST:
errorString = QCoreApplication::translate("QSystemSemaphore", "%1: already exists").arg(function);
error = QSystemSemaphore::AlreadyExists;
break;
case ENOENT:
errorString = QCoreApplication::translate("QSystemSemaphore", "%1: does not exist").arg(function);
error = QSystemSemaphore::NotFound;
break;
case ERANGE:
case ENOMEM:
case ENOSPC:
case EMFILE:
case ENFILE:
case EOVERFLOW:
errorString = QCoreApplication::translate("QSystemSemaphore", "%1: out of resources").arg(function);
error = QSystemSemaphore::OutOfResources;
break;
case ENAMETOOLONG:
errorString = QCoreApplication::translate("QSystemSemaphore", "%1: name error").arg(function);
error = QSystemSemaphore::KeyError;
break;
default:
errorString = QCoreApplication::translate("QSystemSemaphore", "%1: unknown error %2").arg(function).arg(errno);
error = QSystemSemaphore::UnknownError;
#ifdef QSYSTEMSEMAPHORE_DEBUG
qDebug() << errorString << "key" << key << "errno" << errno << EINVAL;
#endif
break;
}
}
/*!
\internal
Initialise the semaphore
*/
#ifndef QT_POSIX_IPC
key_t QSystemSemaphorePrivate::handle(QSystemSemaphore::AccessMode mode)
{
if (-1 != unix_key)
return unix_key;
if (key.isEmpty()) {
errorString = QCoreApplication::tr("%1: key is empty", "QSystemSemaphore").arg(QLatin1String("QSystemSemaphore::handle"));
error = QSystemSemaphore::KeyError;
return -1;
}
// ftok requires that an actual file exists somewhere
int built = QSharedMemoryPrivate::createUnixKeyFile(fileName);
if (-1 == built) {
errorString = QCoreApplication::tr("%1: unable to make key", "QSystemSemaphore").arg(QLatin1String("QSystemSemaphore::handle"));
error = QSystemSemaphore::KeyError;
return -1;
}
createdFile = (1 == built);
// Get the unix key for the created file
unix_key = ftok(QFile::encodeName(fileName).constData(), 'Q');
if (-1 == unix_key) {
errorString = QCoreApplication::tr("%1: ftok failed", "QSystemSemaphore").arg(QLatin1String("QSystemSemaphore::handle"));
error = QSystemSemaphore::KeyError;
return -1;
}
// Get semaphore
semaphore = semget(unix_key, 1, 0666 | IPC_CREAT | IPC_EXCL);
if (-1 == semaphore) {
if (errno == EEXIST)
semaphore = semget(unix_key, 1, 0666 | IPC_CREAT);
if (-1 == semaphore) {
setErrorString(QLatin1String("QSystemSemaphore::handle"));
cleanHandle();
return -1;
}
if (mode == QSystemSemaphore::Create) {
createdSemaphore = true;
createdFile = true;
}
} else {
createdSemaphore = true;
// Force cleanup of file, it is possible that it can be left over from a crash
createdFile = true;
}
// Created semaphore so initialize its value.
if (createdSemaphore && initialValue >= 0) {
qt_semun init_op;
init_op.val = initialValue;
if (-1 == semctl(semaphore, 0, SETVAL, init_op)) {
setErrorString(QLatin1String("QSystemSemaphore::handle"));
cleanHandle();
return -1;
}
}
return unix_key;
}
#else
bool QSystemSemaphorePrivate::handle(QSystemSemaphore::AccessMode mode)
{
if (semaphore != SEM_FAILED)
return true; // we already have a semaphore
if (fileName.isEmpty()) {
errorString = QCoreApplication::tr("%1: key is empty", "QSystemSemaphore").arg(QLatin1String("QSystemSemaphore::handle"));
error = QSystemSemaphore::KeyError;
return false;
}
QByteArray semName = QFile::encodeName(fileName);
// Always try with O_EXCL so we know whether we created the semaphore.
int oflag = O_CREAT | O_EXCL;
for (int tryNum = 0, maxTries = 1; tryNum < maxTries; ++tryNum) {
do {
semaphore = sem_open(semName.constData(), oflag, 0666, initialValue);
} while (semaphore == SEM_FAILED && errno == EINTR);
if (semaphore == SEM_FAILED && errno == EEXIST) {
if (mode == QSystemSemaphore::Create) {
if (sem_unlink(semName.constData()) == -1 && errno != ENOENT) {
setErrorString(QLatin1String("QSystemSemaphore::handle (sem_unlink)"));
return false;
}
// Race condition: the semaphore might be recreated before
// we call sem_open again, so we'll retry several times.
maxTries = 3;
} else {
// Race condition: if it no longer exists at the next sem_open
// call, we won't realize we created it, so we'll leak it later.
oflag &= ~O_EXCL;
maxTries = 2;
}
} else {
break;
}
}
if (semaphore == SEM_FAILED) {
setErrorString(QLatin1String("QSystemSemaphore::handle"));
return false;
}
createdSemaphore = (oflag & O_EXCL) != 0;
return true;
}
#endif // QT_POSIX_IPC
/*!
\internal
Clean up the semaphore
*/
void QSystemSemaphorePrivate::cleanHandle()
{
#ifndef QT_POSIX_IPC
unix_key = -1;
// remove the file if we made it
if (createdFile) {
QFile::remove(fileName);
createdFile = false;
}
if (createdSemaphore) {
if (-1 != semaphore) {
if (-1 == semctl(semaphore, 0, IPC_RMID, 0)) {
setErrorString(QLatin1String("QSystemSemaphore::cleanHandle"));
#ifdef QSYSTEMSEMAPHORE_DEBUG
qDebug("QSystemSemaphore::cleanHandle semctl failed.");
#endif
}
semaphore = -1;
}
createdSemaphore = false;
}
#else
if (semaphore != SEM_FAILED) {
if (sem_close(semaphore) == -1) {
setErrorString(QLatin1String("QSystemSemaphore::cleanHandle (sem_close)"));
#ifdef QSYSTEMSEMAPHORE_DEBUG
qDebug() << QLatin1String("QSystemSemaphore::cleanHandle sem_close failed.");
#endif
}
semaphore = SEM_FAILED;
}
if (createdSemaphore) {
if (sem_unlink(QFile::encodeName(fileName).constData()) == -1 && errno != ENOENT) {
setErrorString(QLatin1String("QSystemSemaphore::cleanHandle (sem_unlink)"));
#ifdef QSYSTEMSEMAPHORE_DEBUG
qDebug() << QLatin1String("QSystemSemaphore::cleanHandle sem_unlink failed.");
#endif
}
createdSemaphore = false;
}
#endif // QT_POSIX_IPC
}
/*!
\internal
*/
bool QSystemSemaphorePrivate::modifySemaphore(int count)
{
#ifndef QT_POSIX_IPC
if (-1 == handle())
return false;
struct sembuf operation;
operation.sem_num = 0;
operation.sem_op = count;
operation.sem_flg = SEM_UNDO;
register int res;
EINTR_LOOP(res, semop(semaphore, &operation, 1));
if (-1 == res) {
// If the semaphore was removed be nice and create it and then modifySemaphore again
if (errno == EINVAL || errno == EIDRM) {
semaphore = -1;
cleanHandle();
handle();
return modifySemaphore(count);
}
setErrorString(QLatin1String("QSystemSemaphore::modifySemaphore"));
#ifdef QSYSTEMSEMAPHORE_DEBUG
qDebug() << QLatin1String("QSystemSemaphore::modify failed") << count << semctl(semaphore, 0, GETVAL) << errno << EIDRM << EINVAL;
#endif
return false;
}
#else
if (!handle())
return false;
if (count > 0) {
int cnt = count;
do {
if (sem_post(semaphore) == -1) {
setErrorString(QLatin1String("QSystemSemaphore::modifySemaphore (sem_post)"));
#ifdef QSYSTEMSEMAPHORE_DEBUG
qDebug() << QLatin1String("QSystemSemaphore::modify sem_post failed") << count << errno;
#endif
// rollback changes to preserve the SysV semaphore behavior
for ( ; cnt < count; ++cnt) {
register int res;
EINTR_LOOP(res, sem_wait(semaphore));
}
return false;
}
--cnt;
} while (cnt > 0);
} else {
register int res;
EINTR_LOOP(res, sem_wait(semaphore));
if (res == -1) {
// If the semaphore was removed be nice and create it and then modifySemaphore again
if (errno == EINVAL || errno == EIDRM) {
semaphore = SEM_FAILED;
return modifySemaphore(count);
}
setErrorString(QLatin1String("QSystemSemaphore::modifySemaphore (sem_wait)"));
#ifdef QSYSTEMSEMAPHORE_DEBUG
qDebug() << QLatin1String("QSystemSemaphore::modify sem_wait failed") << count << errno;
#endif
return false;
}
}
#endif // QT_POSIX_IPC
return true;
}
QT_END_NAMESPACE
#endif // QT_NO_SYSTEMSEMAPHORE
|