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: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 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 ** GNU General Public License Usage
31 ** Alternatively, this file may be used under the terms of the GNU
32 ** General Public License version 3.0 as published by the Free Software
33 ** Foundation and appearing in the file LICENSE.GPL included in the
34 ** packaging of this file. Please review the following information to
35 ** ensure the GNU General Public License version 3.0 requirements will be
36 ** met: http://www.gnu.org/copyleft/gpl.html.
37 **
38 ** $QT_END_LICENSE$
39 **
40 ****************************************************************************/
41
42 #include "qsharedmemory.h"
43 #include "qsharedmemory_p.h"
44
45 #include <qdebug.h>
46
47 #ifndef QT_NO_SHAREDMEMORY
48
49 //#define QSHAREDMEMORY_DEBUG
50
51 QT_BEGIN_NAMESPACE
52
QSharedMemoryPrivate()53 QSharedMemoryPrivate::QSharedMemoryPrivate()
54 : QObjectPrivate(), memory(0), size(0), error(QSharedMemory::NoError),
55 #ifndef QT_NO_SYSTEMSEMAPHORE
56 systemSemaphore(QString()), lockedByMe(false),
57 #endif
58 hand(0)
59 {
60 }
61
setErrorString(const QString & function)62 void QSharedMemoryPrivate::setErrorString(const QString &function)
63 {
64 DWORD windowsError = GetLastError();
65 if (windowsError == 0)
66 return;
67
68 switch (windowsError) {
69 case ERROR_ALREADY_EXISTS:
70 error = QSharedMemory::AlreadyExists;
71 errorString = QSharedMemory::tr("%1: already exists").arg(function);
72 break;
73 case ERROR_FILE_NOT_FOUND:
74 #ifdef Q_OS_WINCE
75 // This happens on CE only if no file is present as CreateFileMappingW
76 // bails out with this error code
77 case ERROR_INVALID_PARAMETER:
78 #endif
79 error = QSharedMemory::NotFound;
80 errorString = QSharedMemory::tr("%1: doesn't exist").arg(function);
81 break;
82 case ERROR_COMMITMENT_LIMIT:
83 error = QSharedMemory::InvalidSize;
84 errorString = QSharedMemory::tr("%1: invalid size").arg(function);
85 break;
86 case ERROR_NO_SYSTEM_RESOURCES:
87 case ERROR_NOT_ENOUGH_MEMORY:
88 error = QSharedMemory::OutOfResources;
89 errorString = QSharedMemory::tr("%1: out of resources").arg(function);
90 break;
91 case ERROR_ACCESS_DENIED:
92 error = QSharedMemory::PermissionDenied;
93 errorString = QSharedMemory::tr("%1: permission denied").arg(function);
94 break;
95 default:
96 errorString = QSharedMemory::tr("%1: unknown error %2").arg(function).arg(windowsError);
97 error = QSharedMemory::UnknownError;
98 #ifdef QSHAREDMEMORY_DEBUG
99 qDebug() << errorString << "key" << key;
100 #endif
101 break;
102 }
103 }
104
handle()105 HANDLE QSharedMemoryPrivate::handle()
106 {
107 if (!hand) {
108 // don't allow making handles on empty keys
109 if (nativeKey.isEmpty()) {
110 error = QSharedMemory::KeyError;
111 errorString = QSharedMemory::tr("%1: key is empty").arg(QLatin1String("QSharedMemory::handle"));
112 return 0;
113 }
114 #ifndef Q_OS_WINCE
115 hand = OpenFileMapping(FILE_MAP_ALL_ACCESS, false, (wchar_t*)nativeKey.utf16());
116 #else
117 // This works for opening a mapping too, but always opens it with read/write access in
118 // attach as it seems.
119 hand = CreateFileMapping(INVALID_HANDLE_VALUE, 0, PAGE_READWRITE, 0, 0, (wchar_t*)nativeKey.utf16());
120 #endif
121 if (!hand)
122 setErrorString(QLatin1String("QSharedMemory::handle"));
123 }
124
125 return hand;
126 }
127
cleanHandle()128 void QSharedMemoryPrivate::cleanHandle()
129 {
130 if (hand != 0 && !CloseHandle(hand))
131 setErrorString(QLatin1String("QSharedMemory::cleanHandle"));
132 hand = 0;
133 }
134
create(int size)135 bool QSharedMemoryPrivate::create(int size)
136 {
137 if (nativeKey.isEmpty()) {
138 error = QSharedMemory::KeyError;
139 errorString = QSharedMemory::tr("%1: key is empty").arg(QLatin1String("QSharedMemory::create"));
140 return false;
141 }
142
143 // Create the file mapping.
144 hand = CreateFileMapping(INVALID_HANDLE_VALUE, 0, PAGE_READWRITE, 0, size, (wchar_t*)nativeKey.utf16());
145 setErrorString(QLatin1String("QSharedMemory::create"));
146
147 // hand is valid when it already exists unlike unix so explicitly check
148 return !(error == QSharedMemory::AlreadyExists || !hand);
149 }
150
attach(QSharedMemory::AccessMode mode)151 bool QSharedMemoryPrivate::attach(QSharedMemory::AccessMode mode)
152 {
153 // Grab a pointer to the memory block
154 int permissions = (mode == QSharedMemory::ReadOnly ? FILE_MAP_READ : FILE_MAP_ALL_ACCESS);
155 memory = (void *)MapViewOfFile(handle(), permissions, 0, 0, 0);
156 if (0 == memory) {
157 setErrorString(QLatin1String("QSharedMemory::attach"));
158 cleanHandle();
159 return false;
160 }
161
162 // Grab the size of the memory we have been given (a multiple of 4K on windows)
163 MEMORY_BASIC_INFORMATION info;
164 if (!VirtualQuery(memory, &info, sizeof(info))) {
165 // Windows doesn't set an error code on this one,
166 // it should only be a kernel memory error.
167 error = QSharedMemory::UnknownError;
168 errorString = QSharedMemory::tr("%1: size query failed").arg(QLatin1String("QSharedMemory::attach"));
169 return false;
170 }
171 size = info.RegionSize;
172
173 return true;
174 }
175
detach()176 bool QSharedMemoryPrivate::detach()
177 {
178 // umap memory
179 if (!UnmapViewOfFile(memory)) {
180 setErrorString(QLatin1String("QSharedMemory::detach"));
181 return false;
182 }
183 memory = 0;
184 size = 0;
185
186 // close handle
187 cleanHandle();
188
189 return true;
190 }
191
192 QT_END_NAMESPACE
193
194 #endif // QT_NO_SHAREDMEMORY
195