0
|
1 |
/****************************************************************************
|
|
2 |
**
|
|
3 |
** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies).
|
|
4 |
** All rights reserved.
|
|
5 |
** Contact: Nokia Corporation (qt-info@nokia.com)
|
|
6 |
**
|
|
7 |
** This file is part of the QtCore module of the Qt Toolkit.
|
|
8 |
**
|
|
9 |
** $QT_BEGIN_LICENSE:LGPL$
|
|
10 |
** No Commercial Usage
|
|
11 |
** This file contains pre-release code and may not be distributed.
|
|
12 |
** You may use this file in accordance with the terms and conditions
|
|
13 |
** contained in the Technology Preview License Agreement accompanying
|
|
14 |
** this package.
|
|
15 |
**
|
|
16 |
** GNU Lesser General Public License Usage
|
|
17 |
** Alternatively, this file may be used under the terms of the GNU Lesser
|
|
18 |
** General Public License version 2.1 as published by the Free Software
|
|
19 |
** Foundation and appearing in the file LICENSE.LGPL included in the
|
|
20 |
** packaging of this file. Please review the following information to
|
|
21 |
** ensure the GNU Lesser General Public License version 2.1 requirements
|
|
22 |
** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
|
|
23 |
**
|
|
24 |
** In addition, as a special exception, Nokia gives you certain additional
|
|
25 |
** rights. These rights are described in the Nokia Qt LGPL Exception
|
|
26 |
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
|
|
27 |
**
|
|
28 |
** If you have questions regarding the use of this file, please contact
|
|
29 |
** Nokia at qt-info@nokia.com.
|
|
30 |
**
|
|
31 |
**
|
|
32 |
**
|
|
33 |
**
|
|
34 |
**
|
|
35 |
**
|
|
36 |
**
|
|
37 |
**
|
|
38 |
** $QT_END_LICENSE$
|
|
39 |
**
|
|
40 |
****************************************************************************/
|
|
41 |
|
|
42 |
#include "qsystemsemaphore.h"
|
|
43 |
#include "qsystemsemaphore_p.h"
|
|
44 |
|
|
45 |
#include <qdebug.h>
|
|
46 |
#include <qfile.h>
|
|
47 |
#include <qcoreapplication.h>
|
|
48 |
|
|
49 |
#ifndef QT_NO_SYSTEMSEMAPHORE
|
|
50 |
|
|
51 |
#include <sys/types.h>
|
|
52 |
#include <sys/ipc.h>
|
|
53 |
#include <fcntl.h>
|
|
54 |
#include <errno.h>
|
|
55 |
#include <sys/shm.h>
|
|
56 |
|
|
57 |
#include <sys/sem.h>
|
|
58 |
|
|
59 |
// OpenBSD 4.2 doesn't define EIDRM, see BUGS section:
|
|
60 |
// http://www.openbsd.org/cgi-bin/man.cgi?query=semop&manpath=OpenBSD+4.2
|
|
61 |
#if defined(Q_OS_OPENBSD) && !defined(EIDRM)
|
|
62 |
#define EIDRM EINVAL
|
|
63 |
#endif
|
|
64 |
|
|
65 |
QT_BEGIN_NAMESPACE
|
|
66 |
|
|
67 |
// We have to define this as on some sem.h will have it
|
|
68 |
union qt_semun {
|
|
69 |
int val; /* value for SETVAL */
|
|
70 |
struct semid_ds *buf; /* buffer for IPC_STAT, IPC_SET */
|
|
71 |
unsigned short *array; /* array for GETALL, SETALL */
|
|
72 |
};
|
|
73 |
|
|
74 |
QSystemSemaphorePrivate::QSystemSemaphorePrivate() :
|
|
75 |
semaphore(-1), createdFile(false),
|
|
76 |
createdSemaphore(false), unix_key(-1), error(QSystemSemaphore::NoError)
|
|
77 |
{
|
|
78 |
}
|
|
79 |
|
|
80 |
void QSystemSemaphorePrivate::setErrorString(const QString &function)
|
|
81 |
{
|
|
82 |
// EINVAL is handled in functions so they can give better error strings
|
|
83 |
switch (errno) {
|
|
84 |
case EPERM:
|
|
85 |
case EACCES:
|
|
86 |
errorString = QCoreApplication::translate("QSystemSemaphore", "%1: permission denied").arg(function);
|
|
87 |
error = QSystemSemaphore::PermissionDenied;
|
|
88 |
break;
|
|
89 |
case EEXIST:
|
|
90 |
errorString = QCoreApplication::translate("QSystemSemaphore", "%1: already exists").arg(function);
|
|
91 |
error = QSystemSemaphore::AlreadyExists;
|
|
92 |
break;
|
|
93 |
case ENOENT:
|
|
94 |
errorString = QCoreApplication::translate("QSystemSemaphore", "%1: does not exist").arg(function);
|
|
95 |
error = QSystemSemaphore::NotFound;
|
|
96 |
break;
|
|
97 |
case ERANGE:
|
|
98 |
case ENOSPC:
|
|
99 |
errorString = QCoreApplication::translate("QSystemSemaphore", "%1: out of resources").arg(function);
|
|
100 |
error = QSystemSemaphore::OutOfResources;
|
|
101 |
break;
|
|
102 |
default:
|
|
103 |
errorString = QCoreApplication::translate("QSystemSemaphore", "%1: unknown error %2").arg(function).arg(errno);
|
|
104 |
error = QSystemSemaphore::UnknownError;
|
|
105 |
#if defined QSYSTEMSEMAPHORE_DEBUG
|
|
106 |
qDebug() << errorString << "key" << key << "errno" << errno << EINVAL;
|
|
107 |
#endif
|
|
108 |
}
|
|
109 |
}
|
|
110 |
|
|
111 |
/*!
|
|
112 |
\internal
|
|
113 |
|
|
114 |
Setup unix_key
|
|
115 |
*/
|
|
116 |
key_t QSystemSemaphorePrivate::handle(QSystemSemaphore::AccessMode mode)
|
|
117 |
{
|
|
118 |
if (key.isEmpty()){
|
|
119 |
errorString = QCoreApplication::tr("%1: key is empty", "QSystemSemaphore").arg(QLatin1String("QSystemSemaphore::handle:"));
|
|
120 |
error = QSystemSemaphore::KeyError;
|
|
121 |
return -1;
|
|
122 |
}
|
|
123 |
|
|
124 |
// ftok requires that an actual file exists somewhere
|
|
125 |
if (-1 != unix_key)
|
|
126 |
return unix_key;
|
|
127 |
|
|
128 |
// Create the file needed for ftok
|
|
129 |
int built = QSharedMemoryPrivate::createUnixKeyFile(fileName);
|
|
130 |
if (-1 == built) {
|
|
131 |
errorString = QCoreApplication::tr("%1: unable to make key", "QSystemSemaphore").arg(QLatin1String("QSystemSemaphore::handle:"));
|
|
132 |
error = QSystemSemaphore::KeyError;
|
|
133 |
return -1;
|
|
134 |
}
|
|
135 |
createdFile = (1 == built);
|
|
136 |
|
|
137 |
// Get the unix key for the created file
|
|
138 |
unix_key = ftok(QFile::encodeName(fileName).constData(), 'Q');
|
|
139 |
if (-1 == unix_key) {
|
|
140 |
errorString = QCoreApplication::tr("%1: ftok failed", "QSystemSemaphore").arg(QLatin1String("QSystemSemaphore::handle:"));
|
|
141 |
error = QSystemSemaphore::KeyError;
|
|
142 |
return -1;
|
|
143 |
}
|
|
144 |
|
|
145 |
// Get semaphore
|
|
146 |
semaphore = semget(unix_key, 1, 0666 | IPC_CREAT | IPC_EXCL);
|
|
147 |
if (-1 == semaphore) {
|
|
148 |
if (errno == EEXIST)
|
|
149 |
semaphore = semget(unix_key, 1, 0666 | IPC_CREAT);
|
|
150 |
if (-1 == semaphore) {
|
|
151 |
setErrorString(QLatin1String("QSystemSemaphore::handle"));
|
|
152 |
cleanHandle();
|
|
153 |
return -1;
|
|
154 |
}
|
|
155 |
} else {
|
|
156 |
createdSemaphore = true;
|
|
157 |
// Force cleanup of file, it is possible that it can be left over from a crash
|
|
158 |
createdFile = true;
|
|
159 |
}
|
|
160 |
|
|
161 |
if (mode == QSystemSemaphore::Create) {
|
|
162 |
createdSemaphore = true;
|
|
163 |
createdFile = true;
|
|
164 |
}
|
|
165 |
|
|
166 |
// Created semaphore so initialize its value.
|
|
167 |
if (createdSemaphore && initialValue >= 0) {
|
|
168 |
qt_semun init_op;
|
|
169 |
init_op.val = initialValue;
|
|
170 |
if (-1 == semctl(semaphore, 0, SETVAL, init_op)) {
|
|
171 |
setErrorString(QLatin1String("QSystemSemaphore::handle"));
|
|
172 |
cleanHandle();
|
|
173 |
return -1;
|
|
174 |
}
|
|
175 |
}
|
|
176 |
|
|
177 |
return unix_key;
|
|
178 |
}
|
|
179 |
|
|
180 |
/*!
|
|
181 |
\internal
|
|
182 |
|
|
183 |
Cleanup the unix_key
|
|
184 |
*/
|
|
185 |
void QSystemSemaphorePrivate::cleanHandle()
|
|
186 |
{
|
|
187 |
unix_key = -1;
|
|
188 |
|
|
189 |
// remove the file if we made it
|
|
190 |
if (createdFile) {
|
|
191 |
QFile::remove(fileName);
|
|
192 |
createdFile = false;
|
|
193 |
}
|
|
194 |
|
|
195 |
if (createdSemaphore) {
|
|
196 |
if (-1 != semaphore) {
|
|
197 |
if (-1 == semctl(semaphore, 0, IPC_RMID, 0)) {
|
|
198 |
setErrorString(QLatin1String("QSystemSemaphore::cleanHandle"));
|
|
199 |
#if defined QSYSTEMSEMAPHORE_DEBUG
|
|
200 |
qDebug() << QLatin1String("QSystemSemaphore::cleanHandle semctl failed.");
|
|
201 |
#endif
|
|
202 |
}
|
|
203 |
semaphore = -1;
|
|
204 |
}
|
|
205 |
createdSemaphore = false;
|
|
206 |
}
|
|
207 |
}
|
|
208 |
|
|
209 |
/*!
|
|
210 |
\internal
|
|
211 |
*/
|
|
212 |
bool QSystemSemaphorePrivate::modifySemaphore(int count)
|
|
213 |
{
|
|
214 |
if (-1 == handle())
|
|
215 |
return false;
|
|
216 |
|
|
217 |
struct sembuf operation;
|
|
218 |
operation.sem_num = 0;
|
|
219 |
operation.sem_op = count;
|
|
220 |
operation.sem_flg = SEM_UNDO;
|
|
221 |
if (-1 == semop(semaphore, &operation, 1)) {
|
|
222 |
// If the semaphore was removed be nice and create it and then modifySemaphore again
|
|
223 |
if (errno == EINVAL || errno == EIDRM) {
|
|
224 |
semaphore = -1;
|
|
225 |
cleanHandle();
|
|
226 |
handle();
|
|
227 |
return modifySemaphore(count);
|
|
228 |
}
|
|
229 |
setErrorString(QLatin1String("QSystemSemaphore::modifySemaphore"));
|
|
230 |
#if defined QSYSTEMSEMAPHORE_DEBUG
|
|
231 |
qDebug() << QLatin1String("QSystemSemaphore::modify failed") << count << semctl(semaphore, 0, GETVAL) << errno << EIDRM << EINVAL;
|
|
232 |
#endif
|
|
233 |
return false;
|
|
234 |
}
|
|
235 |
|
|
236 |
return true;
|
|
237 |
}
|
|
238 |
|
|
239 |
|
|
240 |
QT_END_NAMESPACE
|
|
241 |
|
|
242 |
#endif // QT_NO_SYSTEMSEMAPHORE
|