| Absolute File Name: | /home/qt/qt5_coco/qt5/qtbase/src/corelib/kernel/qsystemsemaphore_systemv.cpp |
| Source code | Switch to Preprocessed file |
| Line | Source | Count | ||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| 1 | /**************************************************************************** | - | ||||||||||||
| 2 | ** | - | ||||||||||||
| 3 | ** Copyright (C) 2016 The Qt Company Ltd. | - | ||||||||||||
| 4 | ** Contact: https://www.qt.io/licensing/ | - | ||||||||||||
| 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 The Qt Company. For licensing terms | - | ||||||||||||
| 14 | ** and conditions see https://www.qt.io/terms-conditions. For further | - | ||||||||||||
| 15 | ** information use the contact form at https://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 3 as published by the Free Software | - | ||||||||||||
| 20 | ** Foundation and appearing in the file LICENSE.LGPL3 included in the | - | ||||||||||||
| 21 | ** packaging of this file. Please review the following information to | - | ||||||||||||
| 22 | ** ensure the GNU Lesser General Public License version 3 requirements | - | ||||||||||||
| 23 | ** will be met: https://www.gnu.org/licenses/lgpl-3.0.html. | - | ||||||||||||
| 24 | ** | - | ||||||||||||
| 25 | ** GNU General Public License Usage | - | ||||||||||||
| 26 | ** Alternatively, this file may be used under the terms of the GNU | - | ||||||||||||
| 27 | ** General Public License version 2.0 or (at your option) the GNU General | - | ||||||||||||
| 28 | ** Public license version 3 or any later version approved by the KDE Free | - | ||||||||||||
| 29 | ** Qt Foundation. The licenses are as published by the Free Software | - | ||||||||||||
| 30 | ** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3 | - | ||||||||||||
| 31 | ** included in the packaging of this file. Please review the following | - | ||||||||||||
| 32 | ** information to ensure the GNU General Public License requirements will | - | ||||||||||||
| 33 | ** be met: https://www.gnu.org/licenses/gpl-2.0.html and | - | ||||||||||||
| 34 | ** https://www.gnu.org/licenses/gpl-3.0.html. | - | ||||||||||||
| 35 | ** | - | ||||||||||||
| 36 | ** $QT_END_LICENSE$ | - | ||||||||||||
| 37 | ** | - | ||||||||||||
| 38 | ****************************************************************************/ | - | ||||||||||||
| 39 | - | |||||||||||||
| 40 | #include "qsystemsemaphore.h" | - | ||||||||||||
| 41 | #include "qsystemsemaphore_p.h" | - | ||||||||||||
| 42 | - | |||||||||||||
| 43 | #include <qdebug.h> | - | ||||||||||||
| 44 | #include <qfile.h> | - | ||||||||||||
| 45 | #include <qcoreapplication.h> | - | ||||||||||||
| 46 | - | |||||||||||||
| 47 | #ifndef QT_POSIX_IPC | - | ||||||||||||
| 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 | /*! | - | ||||||||||||
| 68 | \internal | - | ||||||||||||
| 69 | - | |||||||||||||
| 70 | Setup unix_key | - | ||||||||||||
| 71 | */ | - | ||||||||||||
| 72 | key_t QSystemSemaphorePrivate::handle(QSystemSemaphore::AccessMode mode) | - | ||||||||||||
| 73 | { | - | ||||||||||||
| 74 | if (key.isEmpty()){
| 59-22452 | ||||||||||||
| 75 | errorString = QCoreApplication::tr("%1: key is empty", "QSystemSemaphore").arg(QLatin1String("QSystemSemaphore::handle:")); | - | ||||||||||||
| 76 | error = QSystemSemaphore::KeyError; | - | ||||||||||||
| 77 | return -1; executed 59 times by 1 test: return -1;Executed by:
| 59 | ||||||||||||
| 78 | } | - | ||||||||||||
| 79 | - | |||||||||||||
| 80 | // ftok requires that an actual file exists somewhere | - | ||||||||||||
| 81 | if (-1 != unix_key)
| 7132-15320 | ||||||||||||
| 82 | return unix_key; executed 15320 times by 4 tests: return unix_key;Executed by:
| 15320 | ||||||||||||
| 83 | - | |||||||||||||
| 84 | // Create the file needed for ftok | - | ||||||||||||
| 85 | int built = QSharedMemoryPrivate::createUnixKeyFile(fileName); | - | ||||||||||||
| 86 | if (-1 == built) {
| 0-7132 | ||||||||||||
| 87 | errorString = QCoreApplication::tr("%1: unable to make key", "QSystemSemaphore").arg(QLatin1String("QSystemSemaphore::handle:")); | - | ||||||||||||
| 88 | error = QSystemSemaphore::KeyError; | - | ||||||||||||
| 89 | return -1; never executed: return -1; | 0 | ||||||||||||
| 90 | } | - | ||||||||||||
| 91 | createdFile = (1 == built); | - | ||||||||||||
| 92 | - | |||||||||||||
| 93 | // Get the unix key for the created file | - | ||||||||||||
| 94 | unix_key = qt_safe_ftok(QFile::encodeName(fileName), 'Q'); | - | ||||||||||||
| 95 | if (-1 == unix_key) {
| 0-7132 | ||||||||||||
| 96 | errorString = QCoreApplication::tr("%1: ftok failed", "QSystemSemaphore").arg(QLatin1String("QSystemSemaphore::handle:")); | - | ||||||||||||
| 97 | error = QSystemSemaphore::KeyError; | - | ||||||||||||
| 98 | return -1; never executed: return -1; | 0 | ||||||||||||
| 99 | } | - | ||||||||||||
| 100 | - | |||||||||||||
| 101 | // Get semaphore | - | ||||||||||||
| 102 | semaphore = semget(unix_key, 1, 0600 | IPC_CREAT | IPC_EXCL); | - | ||||||||||||
| 103 | if (-1 == semaphore) {
| 3519-3613 | ||||||||||||
| 104 | if (errno == EEXIST)
| 2-3611 | ||||||||||||
| 105 | semaphore = semget(unix_key, 1, 0600 | IPC_CREAT); executed 3611 times by 3 tests: semaphore = semget(unix_key, 1, 0600 | 01000);Executed by:
| 3611 | ||||||||||||
| 106 | if (-1 == semaphore) {
| 2-3611 | ||||||||||||
| 107 | setErrorString(QLatin1String("QSystemSemaphore::handle")); | - | ||||||||||||
| 108 | cleanHandle(); | - | ||||||||||||
| 109 | return -1; executed 2 times by 1 test: return -1;Executed by:
| 2 | ||||||||||||
| 110 | } | - | ||||||||||||
| 111 | } else { executed 3611 times by 3 tests: end of blockExecuted by:
| 3611 | ||||||||||||
| 112 | createdSemaphore = true; | - | ||||||||||||
| 113 | // Force cleanup of file, it is possible that it can be left over from a crash | - | ||||||||||||
| 114 | createdFile = true; | - | ||||||||||||
| 115 | } executed 3519 times by 3 tests: end of blockExecuted by:
| 3519 | ||||||||||||
| 116 | - | |||||||||||||
| 117 | if (mode == QSystemSemaphore::Create) {
| 3513-3617 | ||||||||||||
| 118 | createdSemaphore = true; | - | ||||||||||||
| 119 | createdFile = true; | - | ||||||||||||
| 120 | } executed 3513 times by 3 tests: end of blockExecuted by:
| 3513 | ||||||||||||
| 121 | - | |||||||||||||
| 122 | // Created semaphore so initialize its value. | - | ||||||||||||
| 123 | if (createdSemaphore && initialValue >= 0) {
| 0-6987 | ||||||||||||
| 124 | qt_semun init_op; | - | ||||||||||||
| 125 | init_op.val = initialValue; | - | ||||||||||||
| 126 | if (-1 == semctl(semaphore, 0, SETVAL, init_op)) {
| 0-6987 | ||||||||||||
| 127 | setErrorString(QLatin1String("QSystemSemaphore::handle")); | - | ||||||||||||
| 128 | cleanHandle(); | - | ||||||||||||
| 129 | return -1; never executed: return -1; | 0 | ||||||||||||
| 130 | } | - | ||||||||||||
| 131 | } executed 6987 times by 3 tests: end of blockExecuted by:
| 6987 | ||||||||||||
| 132 | - | |||||||||||||
| 133 | return unix_key; executed 7130 times by 4 tests: return unix_key;Executed by:
| 7130 | ||||||||||||
| 134 | } | - | ||||||||||||
| 135 | - | |||||||||||||
| 136 | /*! | - | ||||||||||||
| 137 | \internal | - | ||||||||||||
| 138 | - | |||||||||||||
| 139 | Cleanup the unix_key | - | ||||||||||||
| 140 | */ | - | ||||||||||||
| 141 | void QSystemSemaphorePrivate::cleanHandle() | - | ||||||||||||
| 142 | { | - | ||||||||||||
| 143 | unix_key = -1; | - | ||||||||||||
| 144 | - | |||||||||||||
| 145 | // remove the file if we made it | - | ||||||||||||
| 146 | if (createdFile) {
| 3522-3937 | ||||||||||||
| 147 | QFile::remove(fileName); | - | ||||||||||||
| 148 | createdFile = false; | - | ||||||||||||
| 149 | } executed 3522 times by 2 tests: end of blockExecuted by:
| 3522 | ||||||||||||
| 150 | - | |||||||||||||
| 151 | if (createdSemaphore) {
| 3520-3939 | ||||||||||||
| 152 | if (-1 != semaphore) {
| 1-3519 | ||||||||||||
| 153 | if (-1 == semctl(semaphore, 0, IPC_RMID, 0)) {
| 0-3519 | ||||||||||||
| 154 | setErrorString(QLatin1String("QSystemSemaphore::cleanHandle")); | - | ||||||||||||
| 155 | #if defined QSYSTEMSEMAPHORE_DEBUG | - | ||||||||||||
| 156 | qDebug("QSystemSemaphore::cleanHandle semctl failed."); | - | ||||||||||||
| 157 | #endif | - | ||||||||||||
| 158 | } never executed: end of block | 0 | ||||||||||||
| 159 | semaphore = -1; | - | ||||||||||||
| 160 | } executed 3519 times by 2 tests: end of blockExecuted by:
| 3519 | ||||||||||||
| 161 | createdSemaphore = false; | - | ||||||||||||
| 162 | } executed 3520 times by 2 tests: end of blockExecuted by:
| 3520 | ||||||||||||
| 163 | } executed 7459 times by 5 tests: end of blockExecuted by:
| 7459 | ||||||||||||
| 164 | - | |||||||||||||
| 165 | /*! | - | ||||||||||||
| 166 | \internal | - | ||||||||||||
| 167 | */ | - | ||||||||||||
| 168 | bool QSystemSemaphorePrivate::modifySemaphore(int count) | - | ||||||||||||
| 169 | { | - | ||||||||||||
| 170 | if (-1 == handle())
| 2-15320 | ||||||||||||
| 171 | return false; executed 2 times by 1 test: return false;Executed by:
| 2 | ||||||||||||
| 172 | - | |||||||||||||
| 173 | struct sembuf operation; | - | ||||||||||||
| 174 | operation.sem_num = 0; | - | ||||||||||||
| 175 | operation.sem_op = count; | - | ||||||||||||
| 176 | operation.sem_flg = SEM_UNDO; | - | ||||||||||||
| 177 | - | |||||||||||||
| 178 | int res; | - | ||||||||||||
| 179 | EINTR_LOOP(res, semop(semaphore, &operation, 1)); executed 15320 times by 4 tests: end of blockExecuted by:
| 0-15320 | ||||||||||||
| 180 | if (-1 == res) {
| 2-15318 | ||||||||||||
| 181 | // If the semaphore was removed be nice and create it and then modifySemaphore again | - | ||||||||||||
| 182 | if (errno == EINVAL || errno == EIDRM) {
| 0-2 | ||||||||||||
| 183 | semaphore = -1; | - | ||||||||||||
| 184 | cleanHandle(); | - | ||||||||||||
| 185 | handle(); | - | ||||||||||||
| 186 | return modifySemaphore(count); executed 2 times by 1 test: return modifySemaphore(count);Executed by:
| 2 | ||||||||||||
| 187 | } | - | ||||||||||||
| 188 | setErrorString(QLatin1String("QSystemSemaphore::modifySemaphore")); | - | ||||||||||||
| 189 | #if defined QSYSTEMSEMAPHORE_DEBUG | - | ||||||||||||
| 190 | qDebug() << QLatin1String("QSystemSemaphore::modify failed") << count << semctl(semaphore, 0, GETVAL) << errno << EIDRM << EINVAL; | - | ||||||||||||
| 191 | #endif | - | ||||||||||||
| 192 | return false; never executed: return false; | 0 | ||||||||||||
| 193 | } | - | ||||||||||||
| 194 | - | |||||||||||||
| 195 | clearError(); | - | ||||||||||||
| 196 | return true; executed 15318 times by 4 tests: return true;Executed by:
| 15318 | ||||||||||||
| 197 | } | - | ||||||||||||
| 198 | - | |||||||||||||
| 199 | - | |||||||||||||
| 200 | QT_END_NAMESPACE | - | ||||||||||||
| 201 | - | |||||||||||||
| 202 | #endif // QT_NO_SYSTEMSEMAPHORE | - | ||||||||||||
| 203 | - | |||||||||||||
| 204 | #endif // QT_POSIX_IPC | - | ||||||||||||
| Source code | Switch to Preprocessed file |