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 "qsharedmemory.h"
41 #include "qsharedmemory_p.h"
42 #include "qsystemsemaphore.h"
43 #include <qdebug.h>
44 #include <qt_windows.h>
45 
46 QT_BEGIN_NAMESPACE
47 
48 #ifndef QT_NO_SHAREDMEMORY
49 
QSharedMemoryPrivate()50 QSharedMemoryPrivate::QSharedMemoryPrivate() :
51 #ifndef QT_NO_QOBJECT
52     QObjectPrivate(),
53 #endif
54         memory(0), size(0), error(QSharedMemory::NoError),
55            systemSemaphore(QString()), lockedByMe(false), hand(0)
56 {
57 }
58 
setErrorString(QLatin1String function)59 void QSharedMemoryPrivate::setErrorString(QLatin1String function)
60 {
61     DWORD windowsError = GetLastError();
62     if (windowsError == 0)
63         return;
64     switch (windowsError) {
65     case ERROR_ALREADY_EXISTS:
66         error = QSharedMemory::AlreadyExists;
67         errorString = QSharedMemory::tr("%1: already exists").arg(function);
68     break;
69     case ERROR_FILE_NOT_FOUND:
70         error = QSharedMemory::NotFound;
71         errorString = QSharedMemory::tr("%1: doesn't exist").arg(function);
72         break;
73     case ERROR_COMMITMENT_LIMIT:
74         error = QSharedMemory::InvalidSize;
75         errorString = QSharedMemory::tr("%1: invalid size").arg(function);
76         break;
77     case ERROR_NO_SYSTEM_RESOURCES:
78     case ERROR_NOT_ENOUGH_MEMORY:
79         error = QSharedMemory::OutOfResources;
80         errorString = QSharedMemory::tr("%1: out of resources").arg(function);
81         break;
82     case ERROR_ACCESS_DENIED:
83         error = QSharedMemory::PermissionDenied;
84         errorString = QSharedMemory::tr("%1: permission denied").arg(function);
85         break;
86     default:
87         errorString = QSharedMemory::tr("%1: unknown error %2").arg(function).arg(windowsError);
88         error = QSharedMemory::UnknownError;
89 #if defined QSHAREDMEMORY_DEBUG
90         qDebug() << errorString << "key" << key;
91 #endif
92     }
93 }
94 
handle()95 HANDLE QSharedMemoryPrivate::handle()
96 {
97     if (!hand) {
98         const QLatin1String function("QSharedMemory::handle");
99         if (nativeKey.isEmpty()) {
100             error = QSharedMemory::KeyError;
101             errorString = QSharedMemory::tr("%1: unable to make key").arg(function);
102             return 0;
103         }
104 #if defined(Q_OS_WINRT)
105         hand = OpenFileMappingFromApp(FILE_MAP_ALL_ACCESS, FALSE, reinterpret_cast<PCWSTR>(nativeKey.utf16()));
106 #else
107         hand = OpenFileMapping(FILE_MAP_ALL_ACCESS, false,
108                                reinterpret_cast<const wchar_t*>(nativeKey.utf16()));
109 #endif
110         if (!hand) {
111             setErrorString(function);
112             return 0;
113         }
114     }
115     return hand;
116 }
117 
cleanHandle()118 bool QSharedMemoryPrivate::cleanHandle()
119 {
120     if (hand != 0 && !CloseHandle(hand)) {
121         hand = 0;
122         setErrorString(QLatin1String("QSharedMemory::cleanHandle"));
123         return false;
124     }
125     hand = 0;
126     return true;
127 }
128 
create(int size)129 bool QSharedMemoryPrivate::create(int size)
130 {
131     const QLatin1String function("QSharedMemory::create");
132     if (nativeKey.isEmpty()) {
133         error = QSharedMemory::KeyError;
134         errorString = QSharedMemory::tr("%1: key error").arg(function);
135         return false;
136     }
137 
138     // Create the file mapping.
139 #if defined(Q_OS_WINRT)
140     hand = CreateFileMappingFromApp(INVALID_HANDLE_VALUE, 0, PAGE_READWRITE, size,
141                                     reinterpret_cast<PCWSTR>(nativeKey.utf16()));
142 #else
143     hand = CreateFileMapping(INVALID_HANDLE_VALUE, 0, PAGE_READWRITE, 0, size,
144                              reinterpret_cast<const wchar_t*>(nativeKey.utf16()));
145 #endif
146     setErrorString(function);
147 
148     // hand is valid when it already exists unlike unix so explicitly check
149     return error != QSharedMemory::AlreadyExists && hand;
150 }
151 
attach(QSharedMemory::AccessMode mode)152 bool QSharedMemoryPrivate::attach(QSharedMemory::AccessMode mode)
153 {
154     // Grab a pointer to the memory block
155     int permissions = (mode == QSharedMemory::ReadOnly ? FILE_MAP_READ : FILE_MAP_ALL_ACCESS);
156 #if defined(Q_OS_WINRT)
157     memory = (void *)MapViewOfFileFromApp(handle(), permissions, 0, 0);
158 #else
159     memory = (void *)MapViewOfFile(handle(), permissions, 0, 0, 0);
160 #endif
161     if (0 == memory) {
162         setErrorString(QLatin1String("QSharedMemory::attach"));
163         cleanHandle();
164         return false;
165     }
166 
167     // Grab the size of the memory we have been given (a multiple of 4K on windows)
168     MEMORY_BASIC_INFORMATION info;
169     if (!VirtualQuery(memory, &info, sizeof(info))) {
170         // Windows doesn't set an error code on this one,
171         // it should only be a kernel memory error.
172         error = QSharedMemory::UnknownError;
173         errorString = QSharedMemory::tr("%1: size query failed").arg(QLatin1String("QSharedMemory::attach: "));
174         return false;
175     }
176     size = info.RegionSize;
177 
178     return true;
179 }
180 
detach()181 bool QSharedMemoryPrivate::detach()
182 {
183     // umap memory
184     if (!UnmapViewOfFile(memory)) {
185         setErrorString(QLatin1String("QSharedMemory::detach"));
186         return false;
187     }
188     memory = 0;
189     size = 0;
190 
191     // close handle
192     return cleanHandle();
193 }
194 
195 #endif //QT_NO_SHAREDMEMORY
196 
197 
198 QT_END_NAMESPACE
199