Line | Source Code | Coverage |
---|
1 | | - |
2 | | - |
3 | | - |
4 | | - |
5 | | - |
6 | | - |
7 | | - |
8 | QSharedMemoryPrivate::QSharedMemoryPrivate() | - |
9 | : QObjectPrivate(), memory(0), size(0), error(QSharedMemory::NoError), | - |
10 | | - |
11 | systemSemaphore(QString()), lockedByMe(false), | - |
12 | | - |
13 | unix_key(0) | - |
14 | { | - |
15 | } executed: } Execution Count:1 | 1 |
16 | | - |
17 | void QSharedMemoryPrivate::setErrorString(const QString &function) | - |
18 | { | - |
19 | | - |
20 | switch ((*__errno_location ())) { | - |
21 | case 13: | - |
22 | errorString = QSharedMemory::tr("%1: permission denied").arg(function); | - |
23 | error = QSharedMemory::PermissionDenied; | - |
24 | break; | 0 |
25 | case 17: | - |
26 | errorString = QSharedMemory::tr("%1: already exists").arg(function); | - |
27 | error = QSharedMemory::AlreadyExists; | - |
28 | break; | 0 |
29 | case 2: | - |
30 | errorString = QSharedMemory::tr("%1: doesn't exist").arg(function); | - |
31 | error = QSharedMemory::NotFound; | - |
32 | break; | 0 |
33 | case 24: | - |
34 | case 12: | - |
35 | case 28: | - |
36 | errorString = QSharedMemory::tr("%1: out of resources").arg(function); | - |
37 | error = QSharedMemory::OutOfResources; | - |
38 | break; | 0 |
39 | default: | - |
40 | errorString = QSharedMemory::tr("%1: unknown error %2").arg(function).arg((*__errno_location ())); | - |
41 | error = QSharedMemory::UnknownError; | - |
42 | | - |
43 | | - |
44 | | - |
45 | } | 0 |
46 | } | 0 |
47 | | - |
48 | | - |
49 | | - |
50 | | - |
51 | | - |
52 | | - |
53 | key_t QSharedMemoryPrivate::handle() | - |
54 | { | - |
55 | | - |
56 | if (unix_key) never evaluated: unix_key | 0 |
57 | return unix_key; never executed: return unix_key; | 0 |
58 | | - |
59 | | - |
60 | if (nativeKey.isEmpty()) { never evaluated: nativeKey.isEmpty() | 0 |
61 | errorString = QSharedMemory::tr("%1: key is empty").arg(QLatin1String("QSharedMemory::handle:")); | - |
62 | error = QSharedMemory::KeyError; | - |
63 | return 0; never executed: return 0; | 0 |
64 | } | - |
65 | | - |
66 | | - |
67 | if (!QFile::exists(nativeKey)) { never evaluated: !QFile::exists(nativeKey) | 0 |
68 | errorString = QSharedMemory::tr("%1: UNIX key file doesn't exist").arg(QLatin1String("QSharedMemory::handle:")); | - |
69 | error = QSharedMemory::NotFound; | - |
70 | return 0; never executed: return 0; | 0 |
71 | } | - |
72 | | - |
73 | unix_key = ftok(QFile::encodeName(nativeKey).constData(), 'Q'); | - |
74 | if (-1 == unix_key) { never evaluated: -1 == unix_key | 0 |
75 | errorString = QSharedMemory::tr("%1: ftok failed").arg(QLatin1String("QSharedMemory::handle:")); | - |
76 | error = QSharedMemory::KeyError; | - |
77 | unix_key = 0; | - |
78 | } | 0 |
79 | return unix_key; never executed: return unix_key; | 0 |
80 | } | - |
81 | int QSharedMemoryPrivate::createUnixKeyFile(const QString &fileName) | - |
82 | { | - |
83 | if (QFile::exists(fileName)) partially evaluated: QFile::exists(fileName) no Evaluation Count:0 | yes Evaluation Count:46 |
| 0-46 |
84 | return 0; never executed: return 0; | 0 |
85 | | - |
86 | int fd = qt_safe_open(QFile::encodeName(fileName).constData(), | - |
87 | 0200 | 0100 | 02, 0640); | - |
88 | if (-1 == fd) { partially evaluated: -1 == fd no Evaluation Count:0 | yes Evaluation Count:46 |
| 0-46 |
89 | if ((*__errno_location ()) == 17) never evaluated: (*__errno_location ()) == 17 | 0 |
90 | return 0; never executed: return 0; | 0 |
91 | return -1; never executed: return -1; | 0 |
92 | } else { | - |
93 | close(fd); | - |
94 | } executed: } Execution Count:46 | 46 |
95 | return 1; executed: return 1; Execution Count:46 | 46 |
96 | } | - |
97 | | - |
98 | | - |
99 | | - |
100 | | - |
101 | bool QSharedMemoryPrivate::cleanHandle() | - |
102 | { | - |
103 | unix_key = 0; | - |
104 | return true; never executed: return true; | 0 |
105 | } | - |
106 | | - |
107 | bool QSharedMemoryPrivate::create(int size) | - |
108 | { | - |
109 | | - |
110 | bool createdFile = false; | - |
111 | int built = createUnixKeyFile(nativeKey); | - |
112 | if (built == -1) { never evaluated: built == -1 | 0 |
113 | errorString = QSharedMemory::tr("%1: unable to make key").arg(QLatin1String("QSharedMemory::handle:")); | - |
114 | error = QSharedMemory::KeyError; | - |
115 | return false; never executed: return false; | 0 |
116 | } | - |
117 | if (built == 1) { never evaluated: built == 1 | 0 |
118 | createdFile = true; | - |
119 | } | 0 |
120 | | - |
121 | | - |
122 | if (!handle()) { never evaluated: !handle() | 0 |
123 | if (createdFile) never evaluated: createdFile | 0 |
124 | QFile::remove(nativeKey); never executed: QFile::remove(nativeKey); | 0 |
125 | return false; never executed: return false; | 0 |
126 | } | - |
127 | | - |
128 | | - |
129 | if (-1 == shmget(unix_key, size, 0600 | 01000 | 02000)) { never evaluated: -1 == shmget(unix_key, size, 0600 | 01000 | 02000) | 0 |
130 | QString function = QLatin1String("QSharedMemory::create"); | - |
131 | switch ((*__errno_location ())) { | - |
132 | case 22: | - |
133 | errorString = QSharedMemory::tr("%1: system-imposed size restrictions").arg(QLatin1String("QSharedMemory::handle")); | - |
134 | error = QSharedMemory::InvalidSize; | - |
135 | break; | 0 |
136 | default: | - |
137 | setErrorString(function); | - |
138 | } | 0 |
139 | if (createdFile && error != QSharedMemory::AlreadyExists) never evaluated: createdFile never evaluated: error != QSharedMemory::AlreadyExists | 0 |
140 | QFile::remove(nativeKey); never executed: QFile::remove(nativeKey); | 0 |
141 | return false; never executed: return false; | 0 |
142 | } | - |
143 | | - |
144 | return true; never executed: return true; | 0 |
145 | } | - |
146 | | - |
147 | bool QSharedMemoryPrivate::attach(QSharedMemory::AccessMode mode) | - |
148 | { | - |
149 | | - |
150 | int id = shmget(unix_key, 0, (mode == QSharedMemory::ReadOnly ? 0400 : 0600)); | - |
151 | if (-1 == id) { never evaluated: -1 == id | 0 |
152 | setErrorString(QLatin1String("QSharedMemory::attach (shmget)")); | - |
153 | return false; never executed: return false; | 0 |
154 | } | - |
155 | | - |
156 | | - |
157 | memory = shmat(id, 0, (mode == QSharedMemory::ReadOnly ? 010000 : 0)); | - |
158 | if ((void*) - 1 == memory) { never evaluated: (void*) - 1 == memory | 0 |
159 | memory = 0; | - |
160 | setErrorString(QLatin1String("QSharedMemory::attach (shmat)")); | - |
161 | return false; never executed: return false; | 0 |
162 | } | - |
163 | | - |
164 | | - |
165 | shmid_ds shmid_ds; | - |
166 | if (!shmctl(id, 2, &shmid_ds)) { never evaluated: !shmctl(id, 2, &shmid_ds) | 0 |
167 | size = (int)shmid_ds.shm_segsz; | - |
168 | } else { | 0 |
169 | setErrorString(QLatin1String("QSharedMemory::attach (shmctl)")); | - |
170 | return false; never executed: return false; | 0 |
171 | } | - |
172 | | - |
173 | return true; never executed: return true; | 0 |
174 | } | - |
175 | | - |
176 | bool QSharedMemoryPrivate::detach() | - |
177 | { | - |
178 | | - |
179 | if (-1 == shmdt(memory)) { never evaluated: -1 == shmdt(memory) | 0 |
180 | QString function = QLatin1String("QSharedMemory::detach"); | - |
181 | switch ((*__errno_location ())) { | - |
182 | case 22: | - |
183 | errorString = QSharedMemory::tr("%1: not attached").arg(function); | - |
184 | error = QSharedMemory::NotFound; | - |
185 | break; | 0 |
186 | default: | - |
187 | setErrorString(function); | - |
188 | } | 0 |
189 | return false; never executed: return false; | 0 |
190 | } | - |
191 | memory = 0; | - |
192 | size = 0; | - |
193 | | - |
194 | | - |
195 | int id = shmget(unix_key, 0, 0400); | - |
196 | cleanHandle(); | - |
197 | | - |
198 | struct shmid_ds shmid_ds; | - |
199 | if (0 != shmctl(id, 2, &shmid_ds)) { never evaluated: 0 != shmctl(id, 2, &shmid_ds) | 0 |
200 | switch ((*__errno_location ())) { | - |
201 | case 22: | - |
202 | return true; never executed: return true; | 0 |
203 | default: | - |
204 | return false; never executed: return false; | 0 |
205 | } | - |
206 | } | 0 |
207 | | - |
208 | if (shmid_ds.shm_nattch == 0) { never evaluated: shmid_ds.shm_nattch == 0 | 0 |
209 | | - |
210 | if (-1 == shmctl(id, 0, &shmid_ds)) { never evaluated: -1 == shmctl(id, 0, &shmid_ds) | 0 |
211 | setErrorString(QLatin1String("QSharedMemory::remove")); | - |
212 | switch ((*__errno_location ())) { | - |
213 | case 22: | - |
214 | return true; never executed: return true; | 0 |
215 | default: | - |
216 | return false; never executed: return false; | 0 |
217 | } | - |
218 | } | 0 |
219 | | - |
220 | | - |
221 | if (!QFile::remove(nativeKey)) never evaluated: !QFile::remove(nativeKey) | 0 |
222 | return false; never executed: return false; | 0 |
223 | } | 0 |
224 | return true; never executed: return true; | 0 |
225 | } | - |
226 | | - |
227 | | - |
228 | | - |
229 | | - |
| | |