qsharedmemory_systemv.cpp

Absolute File Name:/home/qt/qt5_coco/qt5/qtbase/src/corelib/kernel/qsharedmemory_systemv.cpp
Source codeSwitch to Preprocessed file
LineSourceCount
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 "qplatformdefs.h"-
41-
42#include "qsharedmemory.h"-
43#include "qsharedmemory_p.h"-
44#include "qsystemsemaphore.h"-
45#include <qdir.h>-
46#include <qdebug.h>-
47-
48#include <errno.h>-
49-
50#ifndef QT_POSIX_IPC-
51-
52#ifndef QT_NO_SHAREDMEMORY-
53#include <sys/types.h>-
54#include <sys/ipc.h>-
55#include <sys/shm.h>-
56#include <sys/stat.h>-
57#include <fcntl.h>-
58#include <unistd.h>-
59#endif //QT_NO_SHAREDMEMORY-
60-
61#include "private/qcore_unix_p.h"-
62-
63#ifndef QT_NO_SHAREDMEMORY-
64QT_BEGIN_NAMESPACE-
65-
66/*!-
67 \internal-
68-
69 If not already made create the handle used for accessing the shared memory.-
70*/-
71key_t QSharedMemoryPrivate::handle()-
72{-
73 // already made-
74 if (unix_key)
unix_keyDescription
TRUEnever evaluated
FALSEevaluated 3537 times by 2 tests
Evaluated by:
  • tst_QSharedMemory
  • tst_qsharedmemory - unknown status
0-3537
75 return unix_key;
never executed: return unix_key;
0
76-
77 // don't allow making handles on empty keys-
78 if (nativeKey.isEmpty()) {
nativeKey.isEmpty()Description
TRUEevaluated 1 time by 1 test
Evaluated by:
  • tst_QSharedMemory
FALSEevaluated 3536 times by 2 tests
Evaluated by:
  • tst_QSharedMemory
  • tst_qsharedmemory - unknown status
1-3536
79 errorString = QSharedMemory::tr("%1: key is empty").arg(QLatin1String("QSharedMemory::handle:"));-
80 error = QSharedMemory::KeyError;-
81 return 0;
executed 1 time by 1 test: return 0;
Executed by:
  • tst_QSharedMemory
1
82 }-
83-
84 // ftok requires that an actual file exists somewhere-
85 if (!QFile::exists(nativeKey)) {
!QFile::exists(nativeKey)Description
TRUEevaluated 2 times by 1 test
Evaluated by:
  • tst_QSharedMemory
FALSEevaluated 3534 times by 2 tests
Evaluated by:
  • tst_QSharedMemory
  • tst_qsharedmemory - unknown status
2-3534
86 errorString = QSharedMemory::tr("%1: UNIX key file doesn't exist").arg(QLatin1String("QSharedMemory::handle:"));-
87 error = QSharedMemory::NotFound;-
88 return 0;
executed 2 times by 1 test: return 0;
Executed by:
  • tst_QSharedMemory
2
89 }-
90-
91 unix_key = qt_safe_ftok(QFile::encodeName(nativeKey), 'Q');-
92 if (-1 == unix_key) {
-1 == unix_keyDescription
TRUEnever evaluated
FALSEevaluated 3534 times by 2 tests
Evaluated by:
  • tst_QSharedMemory
  • tst_qsharedmemory - unknown status
0-3534
93 errorString = QSharedMemory::tr("%1: ftok failed").arg(QLatin1String("QSharedMemory::handle:"));-
94 error = QSharedMemory::KeyError;-
95 unix_key = 0;-
96 }
never executed: end of block
0
97 return unix_key;
executed 3534 times by 2 tests: return unix_key;
Executed by:
  • tst_QSharedMemory
  • tst_qsharedmemory - unknown status
3534
98}-
99-
100#endif // QT_NO_SHAREDMEMORY-
101-
102#if !(defined(QT_NO_SHAREDMEMORY) && defined(QT_NO_SYSTEMSEMAPHORE))-
103/*!-
104 \internal-
105 Creates the unix file if needed.-
106 returns \c true if the unix file was created.-
107-
108 -1 error-
109 0 already existed-
110 1 created-
111 */-
112int QSharedMemoryPrivate::createUnixKeyFile(const QString &fileName)-
113{-
114 int fd = qt_safe_open(QFile::encodeName(fileName).constData(),-
115 O_EXCL | O_CREAT | O_RDWR, 0640);-
116 if (-1 == fd) {
-1 == fdDescription
TRUEevaluated 3666 times by 3 tests
Evaluated by:
  • tst_QSharedMemory
  • tst_qsharedmemory - unknown status
  • tst_qsystemsemaphore - unknown status
FALSEevaluated 6986 times by 3 tests
Evaluated by:
  • tst_QSharedMemory
  • tst_QSystemSemaphore
  • tst_qsharedmemory - unknown status
3666-6986
117 if (errno == EEXIST)
(*__errno_location ()) == 17Description
TRUEevaluated 3613 times by 3 tests
Evaluated by:
  • tst_QSharedMemory
  • tst_qsharedmemory - unknown status
  • tst_qsystemsemaphore - unknown status
FALSEevaluated 53 times by 1 test
Evaluated by:
  • tst_QSharedMemory
53-3613
118 return 0;
executed 3613 times by 3 tests: return 0;
Executed by:
  • tst_QSharedMemory
  • tst_qsharedmemory - unknown status
  • tst_qsystemsemaphore - unknown status
3613
119 return -1;
executed 53 times by 1 test: return -1;
Executed by:
  • tst_QSharedMemory
53
120 } else {-
121 close(fd);-
122 }
executed 6986 times by 3 tests: end of block
Executed by:
  • tst_QSharedMemory
  • tst_QSystemSemaphore
  • tst_qsharedmemory - unknown status
6986
123 return 1;
executed 6986 times by 3 tests: return 1;
Executed by:
  • tst_QSharedMemory
  • tst_QSystemSemaphore
  • tst_qsharedmemory - unknown status
6986
124}-
125#endif // QT_NO_SHAREDMEMORY && QT_NO_SYSTEMSEMAPHORE-
126-
127#ifndef QT_NO_SHAREDMEMORY-
128-
129bool QSharedMemoryPrivate::cleanHandle()-
130{-
131 unix_key = 0;-
132 return true;
executed 14289 times by 2 tests: return true;
Executed by:
  • tst_QSharedMemory
  • tst_qsharedmemory - unknown status
14289
133}-
134-
135bool QSharedMemoryPrivate::create(int size)-
136{-
137 // build file if needed-
138 bool createdFile = false;-
139 int built = createUnixKeyFile(nativeKey);-
140 if (built == -1) {
built == -1Description
TRUEevaluated 53 times by 1 test
Evaluated by:
  • tst_QSharedMemory
FALSEevaluated 3467 times by 2 tests
Evaluated by:
  • tst_QSharedMemory
  • tst_qsharedmemory - unknown status
53-3467
141 errorString = QSharedMemory::tr("%1: unable to make key").arg(QLatin1String("QSharedMemory::handle:"));-
142 error = QSharedMemory::KeyError;-
143 return false;
executed 53 times by 1 test: return false;
Executed by:
  • tst_QSharedMemory
53
144 }-
145 if (built == 1) {
built == 1Description
TRUEevaluated 3465 times by 2 tests
Evaluated by:
  • tst_QSharedMemory
  • tst_qsharedmemory - unknown status
FALSEevaluated 2 times by 1 test
Evaluated by:
  • tst_QSharedMemory
2-3465
146 createdFile = true;-
147 }
executed 3465 times by 2 tests: end of block
Executed by:
  • tst_QSharedMemory
  • tst_qsharedmemory - unknown status
3465
148-
149 // get handle-
150 if (!handle()) {
!handle()Description
TRUEnever evaluated
FALSEevaluated 3467 times by 2 tests
Evaluated by:
  • tst_QSharedMemory
  • tst_qsharedmemory - unknown status
0-3467
151 if (createdFile)
createdFileDescription
TRUEnever evaluated
FALSEnever evaluated
0
152 QFile::remove(nativeKey);
never executed: QFile::remove(nativeKey);
0
153 return false;
never executed: return false;
0
154 }-
155-
156 // create-
157 if (-1 == shmget(unix_key, size, 0600 | IPC_CREAT | IPC_EXCL)) {
-1 == shmget(u...01000 | 02000)Description
TRUEevaluated 2 times by 1 test
Evaluated by:
  • tst_QSharedMemory
FALSEevaluated 3465 times by 2 tests
Evaluated by:
  • tst_QSharedMemory
  • tst_qsharedmemory - unknown status
2-3465
158 const QLatin1String function("QSharedMemory::create");-
159 switch (errno) {-
160 case EINVAL:
never executed: case 22:
0
161 errorString = QSharedMemory::tr("%1: system-imposed size restrictions").arg(QLatin1String("QSharedMemory::handle"));-
162 error = QSharedMemory::InvalidSize;-
163 break;
never executed: break;
0
164 default:
executed 2 times by 1 test: default:
Executed by:
  • tst_QSharedMemory
2
165 setErrorString(function);-
166 }
executed 2 times by 1 test: end of block
Executed by:
  • tst_QSharedMemory
2
167 if (createdFile && error != QSharedMemory::AlreadyExists)
createdFileDescription
TRUEnever evaluated
FALSEevaluated 2 times by 1 test
Evaluated by:
  • tst_QSharedMemory
error != QShar...:AlreadyExistsDescription
TRUEnever evaluated
FALSEnever evaluated
0-2
168 QFile::remove(nativeKey);
never executed: QFile::remove(nativeKey);
0
169 return false;
executed 2 times by 1 test: return false;
Executed by:
  • tst_QSharedMemory
2
170 }-
171-
172 return true;
executed 3465 times by 2 tests: return true;
Executed by:
  • tst_QSharedMemory
  • tst_qsharedmemory - unknown status
3465
173}-
174-
175bool QSharedMemoryPrivate::attach(QSharedMemory::AccessMode mode)-
176{-
177 // grab the shared memory segment id-
178 int id = shmget(unix_key, 0, (mode == QSharedMemory::ReadOnly ? 0400 : 0600));-
179 if (-1 == id) {
-1 == idDescription
TRUEnever evaluated
FALSEevaluated 3532 times by 2 tests
Evaluated by:
  • tst_QSharedMemory
  • tst_qsharedmemory - unknown status
0-3532
180 setErrorString(QLatin1String("QSharedMemory::attach (shmget)"));-
181 return false;
never executed: return false;
0
182 }-
183-
184 // grab the memory-
185 memory = shmat(id, 0, (mode == QSharedMemory::ReadOnly ? SHM_RDONLY : 0));-
186 if ((void*) - 1 == memory) {
(void*) - 1 == memoryDescription
TRUEnever evaluated
FALSEevaluated 3532 times by 2 tests
Evaluated by:
  • tst_QSharedMemory
  • tst_qsharedmemory - unknown status
0-3532
187 memory = 0;-
188 setErrorString(QLatin1String("QSharedMemory::attach (shmat)"));-
189 return false;
never executed: return false;
0
190 }-
191-
192 // grab the size-
193 shmid_ds shmid_ds;-
194 if (!shmctl(id, IPC_STAT, &shmid_ds)) {
!shmctl(id, 2, &shmid_ds)Description
TRUEevaluated 3532 times by 2 tests
Evaluated by:
  • tst_QSharedMemory
  • tst_qsharedmemory - unknown status
FALSEnever evaluated
0-3532
195 size = (int)shmid_ds.shm_segsz;-
196 } else {
executed 3532 times by 2 tests: end of block
Executed by:
  • tst_QSharedMemory
  • tst_qsharedmemory - unknown status
3532
197 setErrorString(QLatin1String("QSharedMemory::attach (shmctl)"));-
198 return false;
never executed: return false;
0
199 }-
200-
201 return true;
executed 3532 times by 2 tests: return true;
Executed by:
  • tst_QSharedMemory
  • tst_qsharedmemory - unknown status
3532
202}-
203-
204bool QSharedMemoryPrivate::detach()-
205{-
206 // detach from the memory segment-
207 if (-1 == shmdt(memory)) {
-1 == shmdt(memory)Description
TRUEnever evaluated
FALSEevaluated 3531 times by 1 test
Evaluated by:
  • tst_QSharedMemory
0-3531
208 const QLatin1String function("QSharedMemory::detach");-
209 switch (errno) {-
210 case EINVAL:
never executed: case 22:
0
211 errorString = QSharedMemory::tr("%1: not attached").arg(function);-
212 error = QSharedMemory::NotFound;-
213 break;
never executed: break;
0
214 default:
never executed: default:
0
215 setErrorString(function);-
216 }
never executed: end of block
0
217 return false;
never executed: return false;
0
218 }-
219 memory = 0;-
220 size = 0;-
221-
222 // Get the number of current attachments-
223 int id = shmget(unix_key, 0, 0400);-
224 cleanHandle();-
225-
226 struct shmid_ds shmid_ds;-
227 if (0 != shmctl(id, IPC_STAT, &shmid_ds)) {
0 != shmctl(id, 2, &shmid_ds)Description
TRUEnever evaluated
FALSEevaluated 3531 times by 1 test
Evaluated by:
  • tst_QSharedMemory
0-3531
228 switch (errno) {-
229 case EINVAL:
never executed: case 22:
0
230 return true;
never executed: return true;
0
231 default:
never executed: default:
0
232 return false;
never executed: return false;
0
233 }-
234 }-
235 // If there are no attachments then remove it.-
236 if (shmid_ds.shm_nattch == 0) {
shmid_ds.shm_nattch == 0Description
TRUEevaluated 3465 times by 1 test
Evaluated by:
  • tst_QSharedMemory
FALSEevaluated 66 times by 1 test
Evaluated by:
  • tst_QSharedMemory
66-3465
237 // mark for removal-
238 if (-1 == shmctl(id, IPC_RMID, &shmid_ds)) {
-1 == shmctl(id, 0, &shmid_ds)Description
TRUEnever evaluated
FALSEevaluated 3465 times by 1 test
Evaluated by:
  • tst_QSharedMemory
0-3465
239 setErrorString(QLatin1String("QSharedMemory::remove"));-
240 switch (errno) {-
241 case EINVAL:
never executed: case 22:
0
242 return true;
never executed: return true;
0
243 default:
never executed: default:
0
244 return false;
never executed: return false;
0
245 }-
246 }-
247-
248 // remove file-
249 if (!QFile::remove(nativeKey))
!QFile::remove(nativeKey)Description
TRUEnever evaluated
FALSEevaluated 3465 times by 1 test
Evaluated by:
  • tst_QSharedMemory
0-3465
250 return false;
never executed: return false;
0
251 }
executed 3465 times by 1 test: end of block
Executed by:
  • tst_QSharedMemory
3465
252 return true;
executed 3531 times by 1 test: return true;
Executed by:
  • tst_QSharedMemory
3531
253}-
254-
255-
256QT_END_NAMESPACE-
257-
258#endif // QT_NO_SHAREDMEMORY-
259-
260#endif // QT_POSIX_IPC-
Source codeSwitch to Preprocessed file

Generated by Squish Coco Non-Commercial 4.3.0-BETA-master-30-08-2018-4cb69e9