| Line | Source Code | Coverage |
|---|
| 1 | /**************************************************************************** | - |
| 2 | ** | - |
| 3 | ** Copyright (C) 2013 Digia Plc and/or its subsidiary(-ies). | - |
| 4 | ** Contact: http://www.qt-project.org/legal | - |
| 5 | ** | - |
| 6 | ** This file is part of the QtCore 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 Digia. For licensing terms and | - |
| 14 | ** conditions see http://qt.digia.com/licensing. For further information | - |
| 15 | ** use the contact form at http://qt.digia.com/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 as published by the Free Software | - |
| 20 | ** Foundation and appearing in the file LICENSE.LGPL included in the | - |
| 21 | ** packaging of this file. Please review the following information to | - |
| 22 | ** ensure the GNU Lesser General Public License version 2.1 requirements | - |
| 23 | ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. | - |
| 24 | ** | - |
| 25 | ** In addition, as a special exception, Digia gives you certain additional | - |
| 26 | ** rights. These rights are described in the Digia Qt LGPL Exception | - |
| 27 | ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package. | - |
| 28 | ** | - |
| 29 | ** GNU General Public License Usage | - |
| 30 | ** Alternatively, this file may be used under the terms of the GNU | - |
| 31 | ** General Public License version 3.0 as published by the Free Software | - |
| 32 | ** Foundation and appearing in the file LICENSE.GPL included in the | - |
| 33 | ** packaging of this file. Please review the following information to | - |
| 34 | ** ensure the GNU General Public License version 3.0 requirements will be | - |
| 35 | ** met: http://www.gnu.org/copyleft/gpl.html. | - |
| 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 <sys/sem.h> | - |
| 54 | #include <fcntl.h> | - |
| 55 | #include <errno.h> | - |
| 56 | | - |
| 57 | #include "private/qcore_unix_p.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 | QSystemSemaphorePrivate::QSystemSemaphorePrivate() : | - |
| 68 | semaphore(-1), createdFile(false), | - |
| 69 | createdSemaphore(false), unix_key(-1), error(QSystemSemaphore::NoError) | - |
| 70 | { | - |
| 71 | } executed: }Execution Count:47 | 47 |
| 72 | | - |
| 73 | void QSystemSemaphorePrivate::setErrorString(const QString &function) | - |
| 74 | { | - |
| 75 | // EINVAL is handled in functions so they can give better error strings | - |
| 76 | switch (errno) { | - |
| 77 | case EPERM: | - |
| 78 | case EACCES: | - |
| 79 | errorString = QCoreApplication::translate("QSystemSemaphore", "%1: permission denied").arg(function); never executed (the execution status of this line is deduced): errorString = QCoreApplication::translate("QSystemSemaphore", "%1: permission denied").arg(function); | - |
| 80 | error = QSystemSemaphore::PermissionDenied; never executed (the execution status of this line is deduced): error = QSystemSemaphore::PermissionDenied; | - |
| 81 | break; | 0 |
| 82 | case EEXIST: | - |
| 83 | errorString = QCoreApplication::translate("QSystemSemaphore", "%1: already exists").arg(function); never executed (the execution status of this line is deduced): errorString = QCoreApplication::translate("QSystemSemaphore", "%1: already exists").arg(function); | - |
| 84 | error = QSystemSemaphore::AlreadyExists; never executed (the execution status of this line is deduced): error = QSystemSemaphore::AlreadyExists; | - |
| 85 | break; | 0 |
| 86 | case ENOENT: | - |
| 87 | errorString = QCoreApplication::translate("QSystemSemaphore", "%1: does not exist").arg(function); never executed (the execution status of this line is deduced): errorString = QCoreApplication::translate("QSystemSemaphore", "%1: does not exist").arg(function); | - |
| 88 | error = QSystemSemaphore::NotFound; never executed (the execution status of this line is deduced): error = QSystemSemaphore::NotFound; | - |
| 89 | break; | 0 |
| 90 | case ERANGE: | - |
| 91 | case ENOSPC: | - |
| 92 | errorString = QCoreApplication::translate("QSystemSemaphore", "%1: out of resources").arg(function); never executed (the execution status of this line is deduced): errorString = QCoreApplication::translate("QSystemSemaphore", "%1: out of resources").arg(function); | - |
| 93 | error = QSystemSemaphore::OutOfResources; never executed (the execution status of this line is deduced): error = QSystemSemaphore::OutOfResources; | - |
| 94 | break; | 0 |
| 95 | default: | - |
| 96 | errorString = QCoreApplication::translate("QSystemSemaphore", "%1: unknown error %2").arg(function).arg(errno); never executed (the execution status of this line is deduced): errorString = QCoreApplication::translate("QSystemSemaphore", "%1: unknown error %2").arg(function).arg((*__errno_location ())); | - |
| 97 | error = QSystemSemaphore::UnknownError; never executed (the execution status of this line is deduced): error = QSystemSemaphore::UnknownError; | - |
| 98 | #if defined QSYSTEMSEMAPHORE_DEBUG | - |
| 99 | qDebug() << errorString << "key" << key << "errno" << errno << EINVAL; | - |
| 100 | #endif | - |
| 101 | } | 0 |
| 102 | } | 0 |
| 103 | | - |
| 104 | /*! | - |
| 105 | \internal | - |
| 106 | | - |
| 107 | Setup unix_key | - |
| 108 | */ | - |
| 109 | key_t QSystemSemaphorePrivate::handle(QSystemSemaphore::AccessMode mode) | - |
| 110 | { | - |
| 111 | if (key.isEmpty()){ partially evaluated: key.isEmpty()| no Evaluation Count:0 | yes Evaluation Count:56 |
| 0-56 |
| 112 | errorString = QCoreApplication::tr("%1: key is empty", "QSystemSemaphore").arg(QLatin1String("QSystemSemaphore::handle:")); never executed (the execution status of this line is deduced): errorString = QCoreApplication::tr("%1: key is empty", "QSystemSemaphore").arg(QLatin1String("QSystemSemaphore::handle:")); | - |
| 113 | error = QSystemSemaphore::KeyError; never executed (the execution status of this line is deduced): error = QSystemSemaphore::KeyError; | - |
| 114 | return -1; never executed: return -1; | 0 |
| 115 | } | - |
| 116 | | - |
| 117 | // ftok requires that an actual file exists somewhere | - |
| 118 | if (-1 != unix_key) evaluated: -1 != unix_key| yes Evaluation Count:10 | yes Evaluation Count:46 |
| 10-46 |
| 119 | return unix_key; executed: return unix_key;Execution Count:10 | 10 |
| 120 | | - |
| 121 | // Create the file needed for ftok | - |
| 122 | int built = QSharedMemoryPrivate::createUnixKeyFile(fileName); executed (the execution status of this line is deduced): int built = QSharedMemoryPrivate::createUnixKeyFile(fileName); | - |
| 123 | if (-1 == built) { partially evaluated: -1 == built| no Evaluation Count:0 | yes Evaluation Count:46 |
| 0-46 |
| 124 | errorString = QCoreApplication::tr("%1: unable to make key", "QSystemSemaphore").arg(QLatin1String("QSystemSemaphore::handle:")); never executed (the execution status of this line is deduced): errorString = QCoreApplication::tr("%1: unable to make key", "QSystemSemaphore").arg(QLatin1String("QSystemSemaphore::handle:")); | - |
| 125 | error = QSystemSemaphore::KeyError; never executed (the execution status of this line is deduced): error = QSystemSemaphore::KeyError; | - |
| 126 | return -1; never executed: return -1; | 0 |
| 127 | } | - |
| 128 | createdFile = (1 == built); executed (the execution status of this line is deduced): createdFile = (1 == built); | - |
| 129 | | - |
| 130 | // Get the unix key for the created file | - |
| 131 | unix_key = ftok(QFile::encodeName(fileName).constData(), 'Q'); executed (the execution status of this line is deduced): unix_key = ftok(QFile::encodeName(fileName).constData(), 'Q'); | - |
| 132 | if (-1 == unix_key) { partially evaluated: -1 == unix_key| no Evaluation Count:0 | yes Evaluation Count:46 |
| 0-46 |
| 133 | errorString = QCoreApplication::tr("%1: ftok failed", "QSystemSemaphore").arg(QLatin1String("QSystemSemaphore::handle:")); never executed (the execution status of this line is deduced): errorString = QCoreApplication::tr("%1: ftok failed", "QSystemSemaphore").arg(QLatin1String("QSystemSemaphore::handle:")); | - |
| 134 | error = QSystemSemaphore::KeyError; never executed (the execution status of this line is deduced): error = QSystemSemaphore::KeyError; | - |
| 135 | return -1; never executed: return -1; | 0 |
| 136 | } | - |
| 137 | | - |
| 138 | // Get semaphore | - |
| 139 | semaphore = semget(unix_key, 1, 0600 | IPC_CREAT | IPC_EXCL); executed (the execution status of this line is deduced): semaphore = semget(unix_key, 1, 0600 | 01000 | 02000); | - |
| 140 | if (-1 == semaphore) { partially evaluated: -1 == semaphore| no Evaluation Count:0 | yes Evaluation Count:46 |
| 0-46 |
| 141 | if (errno == EEXIST) never evaluated: (*__errno_location ()) == 17 | 0 |
| 142 | semaphore = semget(unix_key, 1, 0600 | IPC_CREAT); never executed: semaphore = semget(unix_key, 1, 0600 | 01000); | 0 |
| 143 | if (-1 == semaphore) { never evaluated: -1 == semaphore | 0 |
| 144 | setErrorString(QLatin1String("QSystemSemaphore::handle")); never executed (the execution status of this line is deduced): setErrorString(QLatin1String("QSystemSemaphore::handle")); | - |
| 145 | cleanHandle(); never executed (the execution status of this line is deduced): cleanHandle(); | - |
| 146 | return -1; never executed: return -1; | 0 |
| 147 | } | - |
| 148 | } else { | 0 |
| 149 | createdSemaphore = true; executed (the execution status of this line is deduced): createdSemaphore = true; | - |
| 150 | // Force cleanup of file, it is possible that it can be left over from a crash | - |
| 151 | createdFile = true; executed (the execution status of this line is deduced): createdFile = true; | - |
| 152 | } executed: }Execution Count:46 | 46 |
| 153 | | - |
| 154 | if (mode == QSystemSemaphore::Create) { evaluated: mode == QSystemSemaphore::Create| yes Evaluation Count:43 | yes Evaluation Count:3 |
| 3-43 |
| 155 | createdSemaphore = true; executed (the execution status of this line is deduced): createdSemaphore = true; | - |
| 156 | createdFile = true; executed (the execution status of this line is deduced): createdFile = true; | - |
| 157 | } executed: }Execution Count:43 | 43 |
| 158 | | - |
| 159 | // Created semaphore so initialize its value. | - |
| 160 | if (createdSemaphore && initialValue >= 0) { partially evaluated: createdSemaphore| yes Evaluation Count:46 | no Evaluation Count:0 |
partially evaluated: initialValue >= 0| yes Evaluation Count:46 | no Evaluation Count:0 |
| 0-46 |
| 161 | qt_semun init_op; executed (the execution status of this line is deduced): qt_semun init_op; | - |
| 162 | init_op.val = initialValue; executed (the execution status of this line is deduced): init_op.val = initialValue; | - |
| 163 | if (-1 == semctl(semaphore, 0, SETVAL, init_op)) { partially evaluated: -1 == semctl(semaphore, 0, 16, init_op)| no Evaluation Count:0 | yes Evaluation Count:46 |
| 0-46 |
| 164 | setErrorString(QLatin1String("QSystemSemaphore::handle")); never executed (the execution status of this line is deduced): setErrorString(QLatin1String("QSystemSemaphore::handle")); | - |
| 165 | cleanHandle(); never executed (the execution status of this line is deduced): cleanHandle(); | - |
| 166 | return -1; never executed: return -1; | 0 |
| 167 | } | - |
| 168 | } executed: }Execution Count:46 | 46 |
| 169 | | - |
| 170 | return unix_key; executed: return unix_key;Execution Count:46 | 46 |
| 171 | } | - |
| 172 | | - |
| 173 | /*! | - |
| 174 | \internal | - |
| 175 | | - |
| 176 | Cleanup the unix_key | - |
| 177 | */ | - |
| 178 | void QSystemSemaphorePrivate::cleanHandle() | - |
| 179 | { | - |
| 180 | unix_key = -1; executed (the execution status of this line is deduced): unix_key = -1; | - |
| 181 | | - |
| 182 | // remove the file if we made it | - |
| 183 | if (createdFile) { evaluated: createdFile| yes Evaluation Count:46 | yes Evaluation Count:47 |
| 46-47 |
| 184 | QFile::remove(fileName); executed (the execution status of this line is deduced): QFile::remove(fileName); | - |
| 185 | createdFile = false; executed (the execution status of this line is deduced): createdFile = false; | - |
| 186 | } executed: }Execution Count:46 | 46 |
| 187 | | - |
| 188 | if (createdSemaphore) { evaluated: createdSemaphore| yes Evaluation Count:46 | yes Evaluation Count:47 |
| 46-47 |
| 189 | if (-1 != semaphore) { partially evaluated: -1 != semaphore| yes Evaluation Count:46 | no Evaluation Count:0 |
| 0-46 |
| 190 | if (-1 == semctl(semaphore, 0, IPC_RMID, 0)) { partially evaluated: -1 == semctl(semaphore, 0, 0, 0)| no Evaluation Count:0 | yes Evaluation Count:46 |
| 0-46 |
| 191 | setErrorString(QLatin1String("QSystemSemaphore::cleanHandle")); never executed (the execution status of this line is deduced): setErrorString(QLatin1String("QSystemSemaphore::cleanHandle")); | - |
| 192 | #if defined QSYSTEMSEMAPHORE_DEBUG | - |
| 193 | qDebug() << QLatin1String("QSystemSemaphore::cleanHandle semctl failed."); | - |
| 194 | #endif | - |
| 195 | } | 0 |
| 196 | semaphore = -1; executed (the execution status of this line is deduced): semaphore = -1; | - |
| 197 | } executed: }Execution Count:46 | 46 |
| 198 | createdSemaphore = false; executed (the execution status of this line is deduced): createdSemaphore = false; | - |
| 199 | } executed: }Execution Count:46 | 46 |
| 200 | } executed: }Execution Count:93 | 93 |
| 201 | | - |
| 202 | /*! | - |
| 203 | \internal | - |
| 204 | */ | - |
| 205 | bool QSystemSemaphorePrivate::modifySemaphore(int count) | - |
| 206 | { | - |
| 207 | if (-1 == handle()) partially evaluated: -1 == handle()| no Evaluation Count:0 | yes Evaluation Count:10 |
| 0-10 |
| 208 | return false; never executed: return false; | 0 |
| 209 | | - |
| 210 | struct sembuf operation; executed (the execution status of this line is deduced): struct sembuf operation; | - |
| 211 | operation.sem_num = 0; executed (the execution status of this line is deduced): operation.sem_num = 0; | - |
| 212 | operation.sem_op = count; executed (the execution status of this line is deduced): operation.sem_op = count; | - |
| 213 | operation.sem_flg = SEM_UNDO; executed (the execution status of this line is deduced): operation.sem_flg = 0x1000; | - |
| 214 | | - |
| 215 | register int res; executed (the execution status of this line is deduced): register int res; | - |
| 216 | EINTR_LOOP(res, semop(semaphore, &operation, 1)); executed: }Execution Count:10 partially evaluated: res == -1| no Evaluation Count:0 | yes Evaluation Count:10 |
never evaluated: (*__errno_location ()) == 4 | 0-10 |
| 217 | if (-1 == res) { partially evaluated: -1 == res| no Evaluation Count:0 | yes Evaluation Count:10 |
| 0-10 |
| 218 | // If the semaphore was removed be nice and create it and then modifySemaphore again | - |
| 219 | if (errno == EINVAL || errno == EIDRM) { never evaluated: (*__errno_location ()) == 22 never evaluated: (*__errno_location ()) == 43 | 0 |
| 220 | semaphore = -1; never executed (the execution status of this line is deduced): semaphore = -1; | - |
| 221 | cleanHandle(); never executed (the execution status of this line is deduced): cleanHandle(); | - |
| 222 | handle(); never executed (the execution status of this line is deduced): handle(); | - |
| 223 | return modifySemaphore(count); never executed: return modifySemaphore(count); | 0 |
| 224 | } | - |
| 225 | setErrorString(QLatin1String("QSystemSemaphore::modifySemaphore")); never executed (the execution status of this line is deduced): setErrorString(QLatin1String("QSystemSemaphore::modifySemaphore")); | - |
| 226 | #if defined QSYSTEMSEMAPHORE_DEBUG | - |
| 227 | qDebug() << QLatin1String("QSystemSemaphore::modify failed") << count << semctl(semaphore, 0, GETVAL) << errno << EIDRM << EINVAL; | - |
| 228 | #endif | - |
| 229 | return false; never executed: return false; | 0 |
| 230 | } | - |
| 231 | | - |
| 232 | return true; executed: return true;Execution Count:10 | 10 |
| 233 | } | - |
| 234 | | - |
| 235 | | - |
| 236 | QT_END_NAMESPACE | - |
| 237 | | - |
| 238 | #endif // QT_NO_SYSTEMSEMAPHORE | - |
| 239 | | - |
| | |