| 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) 2015 The Qt Company Ltd. | - | ||||||||||||
| 4 | ** Contact: http://www.qt.io/licensing/ | - | ||||||||||||
| 5 | ** | - | ||||||||||||
| 6 | ** This file is part of the QtCore module of the Qt Toolkit. | - | ||||||||||||
| 7 | ** | - | ||||||||||||
| 8 | ** $QT_BEGIN_LICENSE:LGPL21$ | - | ||||||||||||
| 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 | ** $QT_END_LICENSE$ | - | ||||||||||||
| 31 | ** | - | ||||||||||||
| 32 | ****************************************************************************/ | - | ||||||||||||
| 33 | - | |||||||||||||
| 34 | #include "qsystemsemaphore.h" | - | ||||||||||||
| 35 | #include "qsystemsemaphore_p.h" | - | ||||||||||||
| 36 | - | |||||||||||||
| 37 | #include <qdebug.h> | - | ||||||||||||
| 38 | #include <qfile.h> | - | ||||||||||||
| 39 | #include <qcoreapplication.h> | - | ||||||||||||
| 40 | - | |||||||||||||
| 41 | #ifndef QT_POSIX_IPC | - | ||||||||||||
| 42 | - | |||||||||||||
| 43 | #ifndef QT_NO_SYSTEMSEMAPHORE | - | ||||||||||||
| 44 | - | |||||||||||||
| 45 | #include <sys/types.h> | - | ||||||||||||
| 46 | #include <sys/ipc.h> | - | ||||||||||||
| 47 | #include <sys/sem.h> | - | ||||||||||||
| 48 | #include <fcntl.h> | - | ||||||||||||
| 49 | #include <errno.h> | - | ||||||||||||
| 50 | - | |||||||||||||
| 51 | #include "private/qcore_unix_p.h" | - | ||||||||||||
| 52 | - | |||||||||||||
| 53 | // OpenBSD 4.2 doesn't define EIDRM, see BUGS section: | - | ||||||||||||
| 54 | // http://www.openbsd.org/cgi-bin/man.cgi?query=semop&manpath=OpenBSD+4.2 | - | ||||||||||||
| 55 | #if defined(Q_OS_OPENBSD) && !defined(EIDRM) | - | ||||||||||||
| 56 | #define EIDRM EINVAL | - | ||||||||||||
| 57 | #endif | - | ||||||||||||
| 58 | - | |||||||||||||
| 59 | QT_BEGIN_NAMESPACE | - | ||||||||||||
| 60 | - | |||||||||||||
| 61 | /*! | - | ||||||||||||
| 62 | \internal | - | ||||||||||||
| 63 | - | |||||||||||||
| 64 | Setup unix_key | - | ||||||||||||
| 65 | */ | - | ||||||||||||
| 66 | key_t QSystemSemaphorePrivate::handle(QSystemSemaphore::AccessMode mode) | - | ||||||||||||
| 67 | { | - | ||||||||||||
| 68 | if (key.isEmpty()){
| 59-22452 | ||||||||||||
| 69 | errorString = QCoreApplication::tr("%1: key is empty", "QSystemSemaphore").arg(QLatin1String("QSystemSemaphore::handle:")); | - | ||||||||||||
| 70 | error = QSystemSemaphore::KeyError; | - | ||||||||||||
| 71 | return -1; executed 59 times by 1 test: return -1;Executed by:
| 59 | ||||||||||||
| 72 | } | - | ||||||||||||
| 73 | - | |||||||||||||
| 74 | // ftok requires that an actual file exists somewhere | - | ||||||||||||
| 75 | if (-1 != unix_key)
| 7132-15320 | ||||||||||||
| 76 | return unix_key; executed 15320 times by 4 tests: return unix_key;Executed by:
| 15320 | ||||||||||||
| 77 | - | |||||||||||||
| 78 | // Create the file needed for ftok | - | ||||||||||||
| 79 | int built = QSharedMemoryPrivate::createUnixKeyFile(fileName); | - | ||||||||||||
| 80 | if (-1 == built) {
| 0-7132 | ||||||||||||
| 81 | errorString = QCoreApplication::tr("%1: unable to make key", "QSystemSemaphore").arg(QLatin1String("QSystemSemaphore::handle:")); | - | ||||||||||||
| 82 | error = QSystemSemaphore::KeyError; | - | ||||||||||||
| 83 | return -1; never executed: return -1; | 0 | ||||||||||||
| 84 | } | - | ||||||||||||
| 85 | createdFile = (1 == built); | - | ||||||||||||
| 86 | - | |||||||||||||
| 87 | // Get the unix key for the created file | - | ||||||||||||
| 88 | unix_key = qt_safe_ftok(QFile::encodeName(fileName), 'Q'); | - | ||||||||||||
| 89 | if (-1 == unix_key) {
| 0-7132 | ||||||||||||
| 90 | errorString = QCoreApplication::tr("%1: ftok failed", "QSystemSemaphore").arg(QLatin1String("QSystemSemaphore::handle:")); | - | ||||||||||||
| 91 | error = QSystemSemaphore::KeyError; | - | ||||||||||||
| 92 | return -1; never executed: return -1; | 0 | ||||||||||||
| 93 | } | - | ||||||||||||
| 94 | - | |||||||||||||
| 95 | // Get semaphore | - | ||||||||||||
| 96 | semaphore = semget(unix_key, 1, 0600 | IPC_CREAT | IPC_EXCL); | - | ||||||||||||
| 97 | if (-1 == semaphore) {
| 3519-3613 | ||||||||||||
| 98 | if (errno == EEXIST)
| 2-3611 | ||||||||||||
| 99 | semaphore = semget(unix_key, 1, 0600 | IPC_CREAT); executed 3611 times by 3 tests: semaphore = semget(unix_key, 1, 0600 | 01000);Executed by:
| 3611 | ||||||||||||
| 100 | if (-1 == semaphore) {
| 2-3611 | ||||||||||||
| 101 | setErrorString(QLatin1String("QSystemSemaphore::handle")); | - | ||||||||||||
| 102 | cleanHandle(); | - | ||||||||||||
| 103 | return -1; executed 2 times by 1 test: return -1;Executed by:
| 2 | ||||||||||||
| 104 | } | - | ||||||||||||
| 105 | } else { executed 3611 times by 3 tests: end of blockExecuted by:
| 3611 | ||||||||||||
| 106 | createdSemaphore = true; | - | ||||||||||||
| 107 | // Force cleanup of file, it is possible that it can be left over from a crash | - | ||||||||||||
| 108 | createdFile = true; | - | ||||||||||||
| 109 | } executed 3519 times by 3 tests: end of blockExecuted by:
| 3519 | ||||||||||||
| 110 | - | |||||||||||||
| 111 | if (mode == QSystemSemaphore::Create) {
| 3513-3617 | ||||||||||||
| 112 | createdSemaphore = true; | - | ||||||||||||
| 113 | createdFile = true; | - | ||||||||||||
| 114 | } executed 3513 times by 3 tests: end of blockExecuted by:
| 3513 | ||||||||||||
| 115 | - | |||||||||||||
| 116 | // Created semaphore so initialize its value. | - | ||||||||||||
| 117 | if (createdSemaphore && initialValue >= 0) {
| 0-6987 | ||||||||||||
| 118 | qt_semun init_op; | - | ||||||||||||
| 119 | init_op.val = initialValue; | - | ||||||||||||
| 120 | if (-1 == semctl(semaphore, 0, SETVAL, init_op)) {
| 0-6987 | ||||||||||||
| 121 | setErrorString(QLatin1String("QSystemSemaphore::handle")); | - | ||||||||||||
| 122 | cleanHandle(); | - | ||||||||||||
| 123 | return -1; never executed: return -1; | 0 | ||||||||||||
| 124 | } | - | ||||||||||||
| 125 | } executed 6987 times by 3 tests: end of blockExecuted by:
| 6987 | ||||||||||||
| 126 | - | |||||||||||||
| 127 | return unix_key; executed 7130 times by 4 tests: return unix_key;Executed by:
| 7130 | ||||||||||||
| 128 | } | - | ||||||||||||
| 129 | - | |||||||||||||
| 130 | /*! | - | ||||||||||||
| 131 | \internal | - | ||||||||||||
| 132 | - | |||||||||||||
| 133 | Cleanup the unix_key | - | ||||||||||||
| 134 | */ | - | ||||||||||||
| 135 | void QSystemSemaphorePrivate::cleanHandle() | - | ||||||||||||
| 136 | { | - | ||||||||||||
| 137 | unix_key = -1; | - | ||||||||||||
| 138 | - | |||||||||||||
| 139 | // remove the file if we made it | - | ||||||||||||
| 140 | if (createdFile) {
| 3522-3937 | ||||||||||||
| 141 | QFile::remove(fileName); | - | ||||||||||||
| 142 | createdFile = false; | - | ||||||||||||
| 143 | } executed 3522 times by 2 tests: end of blockExecuted by:
| 3522 | ||||||||||||
| 144 | - | |||||||||||||
| 145 | if (createdSemaphore) {
| 3520-3939 | ||||||||||||
| 146 | if (-1 != semaphore) {
| 1-3519 | ||||||||||||
| 147 | if (-1 == semctl(semaphore, 0, IPC_RMID, 0)) {
| 0-3519 | ||||||||||||
| 148 | setErrorString(QLatin1String("QSystemSemaphore::cleanHandle")); | - | ||||||||||||
| 149 | #if defined QSYSTEMSEMAPHORE_DEBUG | - | ||||||||||||
| 150 | qDebug() << QLatin1String("QSystemSemaphore::cleanHandle semctl failed."); | - | ||||||||||||
| 151 | #endif | - | ||||||||||||
| 152 | } never executed: end of block | 0 | ||||||||||||
| 153 | semaphore = -1; | - | ||||||||||||
| 154 | } executed 3519 times by 2 tests: end of blockExecuted by:
| 3519 | ||||||||||||
| 155 | createdSemaphore = false; | - | ||||||||||||
| 156 | } executed 3520 times by 2 tests: end of blockExecuted by:
| 3520 | ||||||||||||
| 157 | } executed 7459 times by 5 tests: end of blockExecuted by:
| 7459 | ||||||||||||
| 158 | - | |||||||||||||
| 159 | /*! | - | ||||||||||||
| 160 | \internal | - | ||||||||||||
| 161 | */ | - | ||||||||||||
| 162 | bool QSystemSemaphorePrivate::modifySemaphore(int count) | - | ||||||||||||
| 163 | { | - | ||||||||||||
| 164 | if (-1 == handle())
| 2-15320 | ||||||||||||
| 165 | return false; executed 2 times by 1 test: return false;Executed by:
| 2 | ||||||||||||
| 166 | - | |||||||||||||
| 167 | struct sembuf operation; | - | ||||||||||||
| 168 | operation.sem_num = 0; | - | ||||||||||||
| 169 | operation.sem_op = count; | - | ||||||||||||
| 170 | operation.sem_flg = SEM_UNDO; | - | ||||||||||||
| 171 | - | |||||||||||||
| 172 | int res; | - | ||||||||||||
| 173 | EINTR_LOOP(res, semop(semaphore, &operation, 1)); executed 15320 times by 4 tests: end of blockExecuted by:
| 0-15320 | ||||||||||||
| 174 | if (-1 == res) {
| 2-15318 | ||||||||||||
| 175 | // If the semaphore was removed be nice and create it and then modifySemaphore again | - | ||||||||||||
| 176 | if (errno == EINVAL || errno == EIDRM) {
| 0-2 | ||||||||||||
| 177 | semaphore = -1; | - | ||||||||||||
| 178 | cleanHandle(); | - | ||||||||||||
| 179 | handle(); | - | ||||||||||||
| 180 | return modifySemaphore(count); executed 2 times by 1 test: return modifySemaphore(count);Executed by:
| 2 | ||||||||||||
| 181 | } | - | ||||||||||||
| 182 | setErrorString(QLatin1String("QSystemSemaphore::modifySemaphore")); | - | ||||||||||||
| 183 | #if defined QSYSTEMSEMAPHORE_DEBUG | - | ||||||||||||
| 184 | qDebug() << QLatin1String("QSystemSemaphore::modify failed") << count << semctl(semaphore, 0, GETVAL) << errno << EIDRM << EINVAL; | - | ||||||||||||
| 185 | #endif | - | ||||||||||||
| 186 | return false; never executed: return false; | 0 | ||||||||||||
| 187 | } | - | ||||||||||||
| 188 | - | |||||||||||||
| 189 | clearError(); | - | ||||||||||||
| 190 | return true; executed 15318 times by 4 tests: return true;Executed by:
| 15318 | ||||||||||||
| 191 | } | - | ||||||||||||
| 192 | - | |||||||||||||
| 193 | - | |||||||||||||
| 194 | QT_END_NAMESPACE | - | ||||||||||||
| 195 | - | |||||||||||||
| 196 | #endif // QT_NO_SYSTEMSEMAPHORE | - | ||||||||||||
| 197 | - | |||||||||||||
| 198 | #endif // QT_POSIX_IPC | - | ||||||||||||
| Source code | Switch to Preprocessed file |