| 1 | // Copyright (C) 2016 The Qt Company Ltd. |
| 2 | // SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only |
| 3 | // Qt-Security score:significant reason:default |
| 4 | |
| 5 | #ifndef QSHAREDMEMORY_P_H |
| 6 | #define QSHAREDMEMORY_P_H |
| 7 | |
| 8 | // |
| 9 | // W A R N I N G |
| 10 | // ------------- |
| 11 | // |
| 12 | // This file is not part of the Qt API. It exists purely as an |
| 13 | // implementation detail. This header file may change from version to |
| 14 | // version without notice, or even be removed. |
| 15 | // |
| 16 | // We mean it. |
| 17 | // |
| 18 | |
| 19 | #include "qsharedmemory.h" |
| 20 | |
| 21 | #include <QtCore/qstring.h> |
| 22 | |
| 23 | #if QT_CONFIG(sharedmemory) |
| 24 | #include "qsystemsemaphore.h" |
| 25 | #include "qtipccommon_p.h" |
| 26 | #include "private/qobject_p.h" |
| 27 | |
| 28 | #if QT_CONFIG(posix_shm) |
| 29 | # include <sys/mman.h> |
| 30 | #endif |
| 31 | #if QT_CONFIG(sysv_shm) |
| 32 | # include <sys/shm.h> |
| 33 | #endif |
| 34 | |
| 35 | QT_BEGIN_NAMESPACE |
| 36 | |
| 37 | class QSharedMemoryPrivate; |
| 38 | |
| 39 | #if QT_CONFIG(systemsemaphore) |
| 40 | /*! |
| 41 | Helper class |
| 42 | */ |
| 43 | class QSharedMemoryLocker |
| 44 | { |
| 45 | Q_DISABLE_COPY(QSharedMemoryLocker) |
| 46 | public: |
| 47 | Q_NODISCARD_CTOR explicit QSharedMemoryLocker(QSharedMemory *sharedMemory) |
| 48 | : q_sm(sharedMemory) |
| 49 | { |
| 50 | Q_ASSERT(q_sm); |
| 51 | } |
| 52 | |
| 53 | Q_NODISCARD_CTOR QSharedMemoryLocker(QSharedMemoryLocker &&other) noexcept |
| 54 | : q_sm{std::exchange(obj&: other.q_sm, new_val: nullptr)} |
| 55 | {} |
| 56 | |
| 57 | QT_MOVE_ASSIGNMENT_OPERATOR_IMPL_VIA_MOVE_AND_SWAP(QSharedMemoryLocker) |
| 58 | |
| 59 | void swap(QSharedMemoryLocker &other) noexcept |
| 60 | { qt_ptr_swap(lhs&: q_sm, rhs&: other.q_sm); } |
| 61 | |
| 62 | inline ~QSharedMemoryLocker() |
| 63 | { |
| 64 | if (q_sm) |
| 65 | q_sm->unlock(); |
| 66 | } |
| 67 | |
| 68 | inline bool lock() |
| 69 | { |
| 70 | if (q_sm && q_sm->lock()) |
| 71 | return true; |
| 72 | q_sm = nullptr; |
| 73 | return false; |
| 74 | } |
| 75 | |
| 76 | private: |
| 77 | friend void swap(QSharedMemoryLocker &lhs, QSharedMemoryLocker &rhs) noexcept |
| 78 | { lhs.swap(other&: rhs); } |
| 79 | |
| 80 | QSharedMemory *q_sm; |
| 81 | }; |
| 82 | #endif // QT_CONFIG(systemsemaphore) |
| 83 | |
| 84 | class QSharedMemoryPosix |
| 85 | { |
| 86 | public: |
| 87 | static constexpr bool Enabled = QT_CONFIG(posix_shm); |
| 88 | static bool supports(QNativeIpcKey::Type type) |
| 89 | { return type == QNativeIpcKey::Type::PosixRealtime; } |
| 90 | static bool runtimeSupportCheck(); |
| 91 | |
| 92 | bool handle(QSharedMemoryPrivate *self); |
| 93 | bool cleanHandle(QSharedMemoryPrivate *self); |
| 94 | bool create(QSharedMemoryPrivate *self, qsizetype size); |
| 95 | bool attach(QSharedMemoryPrivate *self, QSharedMemory::AccessMode mode); |
| 96 | bool detach(QSharedMemoryPrivate *self); |
| 97 | |
| 98 | int hand = -1; |
| 99 | }; |
| 100 | |
| 101 | class QSharedMemorySystemV |
| 102 | { |
| 103 | public: |
| 104 | static constexpr bool Enabled = QT_CONFIG(sysv_shm); |
| 105 | static bool supports(QNativeIpcKey::Type type) |
| 106 | { return quint16(type) <= 0xff; } |
| 107 | static bool runtimeSupportCheck(); |
| 108 | |
| 109 | #if QT_CONFIG(sysv_shm) |
| 110 | key_t handle(QSharedMemoryPrivate *self); |
| 111 | bool cleanHandle(QSharedMemoryPrivate *self); |
| 112 | bool create(QSharedMemoryPrivate *self, qsizetype size); |
| 113 | bool attach(QSharedMemoryPrivate *self, QSharedMemory::AccessMode mode); |
| 114 | bool detach(QSharedMemoryPrivate *self); |
| 115 | |
| 116 | private: |
| 117 | void updateNativeKeyFile(const QNativeIpcKey &nativeKey); |
| 118 | |
| 119 | QByteArray nativeKeyFile; |
| 120 | key_t unix_key = 0; |
| 121 | #endif |
| 122 | }; |
| 123 | |
| 124 | class QSharedMemoryWin32 |
| 125 | { |
| 126 | public: |
| 127 | #ifdef Q_OS_WIN32 |
| 128 | static constexpr bool Enabled = true; |
| 129 | #else |
| 130 | static constexpr bool Enabled = false; |
| 131 | #endif |
| 132 | static bool runtimeSupportCheck() { return Enabled; } |
| 133 | static bool supports(QNativeIpcKey::Type type) |
| 134 | { return type == QNativeIpcKey::Type::Windows; } |
| 135 | |
| 136 | Qt::HANDLE handle(QSharedMemoryPrivate *self); |
| 137 | bool cleanHandle(QSharedMemoryPrivate *self); |
| 138 | bool create(QSharedMemoryPrivate *self, qsizetype size); |
| 139 | bool attach(QSharedMemoryPrivate *self, QSharedMemory::AccessMode mode); |
| 140 | bool detach(QSharedMemoryPrivate *self); |
| 141 | |
| 142 | Qt::HANDLE hand = nullptr; |
| 143 | }; |
| 144 | |
| 145 | class Q_AUTOTEST_EXPORT QSharedMemoryPrivate : public QObjectPrivate |
| 146 | { |
| 147 | Q_DECLARE_PUBLIC(QSharedMemory) |
| 148 | |
| 149 | public: |
| 150 | QSharedMemoryPrivate(QNativeIpcKey::Type type) : nativeKey(type) |
| 151 | { constructBackend(); } |
| 152 | ~QSharedMemoryPrivate(); |
| 153 | |
| 154 | void *memory = nullptr; |
| 155 | qsizetype size = 0; |
| 156 | QNativeIpcKey nativeKey; |
| 157 | QString errorString; |
| 158 | #if QT_CONFIG(systemsemaphore) |
| 159 | using SemaphoreAccessMode = QSystemSemaphore::AccessMode; |
| 160 | QSystemSemaphore systemSemaphore{ QNativeIpcKey() }; |
| 161 | bool lockedByMe = false; |
| 162 | #else |
| 163 | enum SemaphoreAccessMode {}; |
| 164 | #endif |
| 165 | QSharedMemory::SharedMemoryError error = QSharedMemory::NoError; |
| 166 | |
| 167 | union Backend { |
| 168 | Backend() {} |
| 169 | ~Backend() {} |
| 170 | QSharedMemoryPosix posix; |
| 171 | QSharedMemorySystemV sysv; |
| 172 | QSharedMemoryWin32 win32; |
| 173 | }; |
| 174 | QtIpcCommon::IpcStorageVariant<&Backend::posix, &Backend::sysv, &Backend::win32> backend; |
| 175 | |
| 176 | void constructBackend(); |
| 177 | void destructBackend(); |
| 178 | bool initKey(SemaphoreAccessMode mode); |
| 179 | |
| 180 | template <typename Lambda> auto visit(const Lambda &lambda) |
| 181 | { |
| 182 | return backend.visit(nativeKey.type(), lambda); |
| 183 | } |
| 184 | |
| 185 | bool handle() |
| 186 | { |
| 187 | return visit(lambda: [&](auto p) { return !!p->handle(this); }); |
| 188 | } |
| 189 | bool cleanHandle() |
| 190 | { |
| 191 | return visit(lambda: [&](auto p) { return p->cleanHandle(this); }); |
| 192 | } |
| 193 | bool create(qsizetype sz) |
| 194 | { |
| 195 | return visit(lambda: [&](auto p) { return p->create(this, sz); }); |
| 196 | } |
| 197 | bool attach(QSharedMemory::AccessMode mode) |
| 198 | { |
| 199 | return visit(lambda: [&](auto p) { return p->attach(this, mode); }); |
| 200 | } |
| 201 | bool detach() |
| 202 | { |
| 203 | return visit(lambda: [&](auto p) { return p->detach(this); }); |
| 204 | } |
| 205 | |
| 206 | inline void setError(QSharedMemory::SharedMemoryError e, const QString &message) |
| 207 | { error = e; errorString = message; } |
| 208 | void setUnixErrorString(QLatin1StringView function); |
| 209 | void setWindowsErrorString(QLatin1StringView function); |
| 210 | |
| 211 | #if QT_CONFIG(systemsemaphore) |
| 212 | bool tryLocker(QSharedMemoryLocker *locker, const QString &function) { |
| 213 | if (!locker->lock()) { |
| 214 | errorString = QSharedMemory::tr(s: "%1: unable to lock" ).arg(a: function); |
| 215 | error = QSharedMemory::LockError; |
| 216 | return false; |
| 217 | } |
| 218 | return true; |
| 219 | } |
| 220 | QNativeIpcKey semaphoreNativeKey() const; |
| 221 | #endif // QT_CONFIG(systemsemaphore) |
| 222 | }; |
| 223 | |
| 224 | QT_END_NAMESPACE |
| 225 | |
| 226 | #endif // QT_CONFIG(sharedmemory) |
| 227 | |
| 228 | #endif // QSHAREDMEMORY_P_H |
| 229 | |
| 230 | |