| 1 | /**************************************************************************** |
| 2 | ** |
| 3 | ** Copyright (C) 2015 Konstantin Ritt <ritt.ks@gmail.com> |
| 4 | ** Copyright (C) 2016 The Qt Company Ltd. |
| 5 | ** Copyright (C) 2015 Klarälvdalens Datakonsult AB, a KDAB Group company, info@kdab.com, author Tobias Koenig <tobias.koenig@kdab.com> |
| 6 | ** Contact: https://www.qt.io/licensing/ |
| 7 | ** |
| 8 | ** This file is part of the QtCore module of the Qt Toolkit. |
| 9 | ** |
| 10 | ** $QT_BEGIN_LICENSE:LGPL$ |
| 11 | ** Commercial License Usage |
| 12 | ** Licensees holding valid commercial Qt licenses may use this file in |
| 13 | ** accordance with the commercial license agreement provided with the |
| 14 | ** Software or, alternatively, in accordance with the terms contained in |
| 15 | ** a written agreement between you and The Qt Company. For licensing terms |
| 16 | ** and conditions see https://www.qt.io/terms-conditions. For further |
| 17 | ** information use the contact form at https://www.qt.io/contact-us. |
| 18 | ** |
| 19 | ** GNU Lesser General Public License Usage |
| 20 | ** Alternatively, this file may be used under the terms of the GNU Lesser |
| 21 | ** General Public License version 3 as published by the Free Software |
| 22 | ** Foundation and appearing in the file LICENSE.LGPL3 included in the |
| 23 | ** packaging of this file. Please review the following information to |
| 24 | ** ensure the GNU Lesser General Public License version 3 requirements |
| 25 | ** will be met: https://www.gnu.org/licenses/lgpl-3.0.html. |
| 26 | ** |
| 27 | ** GNU General Public License Usage |
| 28 | ** Alternatively, this file may be used under the terms of the GNU |
| 29 | ** General Public License version 2.0 or (at your option) the GNU General |
| 30 | ** Public license version 3 or any later version approved by the KDE Free |
| 31 | ** Qt Foundation. The licenses are as published by the Free Software |
| 32 | ** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3 |
| 33 | ** included in the packaging of this file. Please review the following |
| 34 | ** information to ensure the GNU General Public License requirements will |
| 35 | ** be met: https://www.gnu.org/licenses/gpl-2.0.html and |
| 36 | ** https://www.gnu.org/licenses/gpl-3.0.html. |
| 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 | #ifdef QT_POSIX_IPC |
| 50 | |
| 51 | #ifndef QT_NO_SYSTEMSEMAPHORE |
| 52 | |
| 53 | #include <sys/types.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 | bool QSystemSemaphorePrivate::handle(QSystemSemaphore::AccessMode mode) |
| 68 | { |
| 69 | if (semaphore != SEM_FAILED) |
| 70 | return true; // we already have a semaphore |
| 71 | |
| 72 | if (fileName.isEmpty()) { |
| 73 | errorString = QCoreApplication::translate("%1: key is empty" , "QSystemSemaphore" ).arg(QLatin1String("QSystemSemaphore::handle" )); |
| 74 | error = QSystemSemaphore::KeyError; |
| 75 | return false; |
| 76 | } |
| 77 | |
| 78 | const QByteArray semName = QFile::encodeName(fileName); |
| 79 | |
| 80 | // Always try with O_EXCL so we know whether we created the semaphore. |
| 81 | int oflag = O_CREAT | O_EXCL; |
| 82 | for (int tryNum = 0, maxTries = 1; tryNum < maxTries; ++tryNum) { |
| 83 | do { |
| 84 | semaphore = ::sem_open(semName.constData(), oflag, 0600, initialValue); |
| 85 | } while (semaphore == SEM_FAILED && errno == EINTR); |
| 86 | if (semaphore == SEM_FAILED && errno == EEXIST) { |
| 87 | if (mode == QSystemSemaphore::Create) { |
| 88 | if (::sem_unlink(semName.constData()) == -1 && errno != ENOENT) { |
| 89 | setErrorString(QLatin1String("QSystemSemaphore::handle (sem_unlink)" )); |
| 90 | return false; |
| 91 | } |
| 92 | // Race condition: the semaphore might be recreated before |
| 93 | // we call sem_open again, so we'll retry several times. |
| 94 | maxTries = 3; |
| 95 | } else { |
| 96 | // Race condition: if it no longer exists at the next sem_open |
| 97 | // call, we won't realize we created it, so we'll leak it later. |
| 98 | oflag &= ~O_EXCL; |
| 99 | maxTries = 2; |
| 100 | } |
| 101 | } else { |
| 102 | break; |
| 103 | } |
| 104 | } |
| 105 | |
| 106 | if (semaphore == SEM_FAILED) { |
| 107 | setErrorString(QLatin1String("QSystemSemaphore::handle" )); |
| 108 | return false; |
| 109 | } |
| 110 | |
| 111 | createdSemaphore = (oflag & O_EXCL) != 0; |
| 112 | |
| 113 | return true; |
| 114 | } |
| 115 | |
| 116 | void QSystemSemaphorePrivate::cleanHandle() |
| 117 | { |
| 118 | if (semaphore != SEM_FAILED) { |
| 119 | if (::sem_close(semaphore) == -1) { |
| 120 | setErrorString(QLatin1String("QSystemSemaphore::cleanHandle (sem_close)" )); |
| 121 | #if defined QSYSTEMSEMAPHORE_DEBUG |
| 122 | qDebug("QSystemSemaphore::cleanHandle sem_close failed." ); |
| 123 | #endif |
| 124 | } |
| 125 | semaphore = SEM_FAILED; |
| 126 | } |
| 127 | |
| 128 | if (createdSemaphore) { |
| 129 | if (::sem_unlink(QFile::encodeName(fileName).constData()) == -1 && errno != ENOENT) { |
| 130 | setErrorString(QLatin1String("QSystemSemaphore::cleanHandle (sem_unlink)" )); |
| 131 | #if defined QSYSTEMSEMAPHORE_DEBUG |
| 132 | qDebug("QSystemSemaphore::cleanHandle sem_unlink failed." ); |
| 133 | #endif |
| 134 | } |
| 135 | createdSemaphore = false; |
| 136 | } |
| 137 | } |
| 138 | |
| 139 | bool QSystemSemaphorePrivate::modifySemaphore(int count) |
| 140 | { |
| 141 | if (!handle()) |
| 142 | return false; |
| 143 | |
| 144 | if (count > 0) { |
| 145 | int cnt = count; |
| 146 | do { |
| 147 | if (::sem_post(semaphore) == -1) { |
| 148 | setErrorString(QLatin1String("QSystemSemaphore::modifySemaphore (sem_post)" )); |
| 149 | #if defined QSYSTEMSEMAPHORE_DEBUG |
| 150 | qDebug("QSystemSemaphore::modify sem_post failed %d %d" , count, errno); |
| 151 | #endif |
| 152 | // rollback changes to preserve the SysV semaphore behavior |
| 153 | for ( ; cnt < count; ++cnt) { |
| 154 | int res; |
| 155 | EINTR_LOOP(res, ::sem_wait(semaphore)); |
| 156 | } |
| 157 | return false; |
| 158 | } |
| 159 | --cnt; |
| 160 | } while (cnt > 0); |
| 161 | } else { |
| 162 | int res; |
| 163 | EINTR_LOOP(res, ::sem_wait(semaphore)); |
| 164 | if (res == -1) { |
| 165 | // If the semaphore was removed be nice and create it and then modifySemaphore again |
| 166 | if (errno == EINVAL || errno == EIDRM) { |
| 167 | semaphore = SEM_FAILED; |
| 168 | return modifySemaphore(count); |
| 169 | } |
| 170 | setErrorString(QLatin1String("QSystemSemaphore::modifySemaphore (sem_wait)" )); |
| 171 | #if defined QSYSTEMSEMAPHORE_DEBUG |
| 172 | qDebug("QSystemSemaphore::modify sem_wait failed %d %d" , count, errno); |
| 173 | #endif |
| 174 | return false; |
| 175 | } |
| 176 | } |
| 177 | |
| 178 | clearError(); |
| 179 | return true; |
| 180 | } |
| 181 | |
| 182 | QT_END_NAMESPACE |
| 183 | |
| 184 | #endif // QT_NO_SYSTEMSEMAPHORE |
| 185 | |
| 186 | #endif // QT_POSIX_IPC |
| 187 | |