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

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