OLD | NEW |
1 /* This Source Code Form is subject to the terms of the Mozilla Public | 1 /* This Source Code Form is subject to the terms of the Mozilla Public |
2 * License, v. 2.0. If a copy of the MPL was not distributed with this | 2 * License, v. 2.0. If a copy of the MPL was not distributed with this |
3 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ | 3 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
4 | 4 |
5 #include "seccomon.h" | 5 #include "seccomon.h" |
6 /* This ifdef should match the one in sslsnce.c */ | 6 /* This ifdef should match the one in sslsnce.c */ |
7 #if defined(XP_UNIX) || defined(XP_WIN32) || defined (XP_OS2) || defined(XP_BEOS
) | 7 #if defined(XP_UNIX) || defined(XP_WIN32) || defined(XP_OS2) || defined(XP_BEOS) |
8 | 8 |
9 #include "sslmutex.h" | 9 #include "sslmutex.h" |
10 #include "prerr.h" | 10 #include "prerr.h" |
11 | 11 |
12 static SECStatus single_process_sslMutex_Init(sslMutex* pMutex) | 12 static SECStatus |
| 13 single_process_sslMutex_Init(sslMutex* pMutex) |
13 { | 14 { |
14 PR_ASSERT(pMutex != 0 && pMutex->u.sslLock == 0 ); | 15 PR_ASSERT(pMutex != 0 && pMutex->u.sslLock == 0); |
15 | 16 |
16 pMutex->u.sslLock = PR_NewLock(); | 17 pMutex->u.sslLock = PR_NewLock(); |
17 if (!pMutex->u.sslLock) { | 18 if (!pMutex->u.sslLock) { |
18 return SECFailure; | 19 return SECFailure; |
19 } | 20 } |
20 return SECSuccess; | 21 return SECSuccess; |
21 } | 22 } |
22 | 23 |
23 static SECStatus single_process_sslMutex_Destroy(sslMutex* pMutex) | 24 static SECStatus |
| 25 single_process_sslMutex_Destroy(sslMutex* pMutex) |
24 { | 26 { |
25 PR_ASSERT(pMutex != 0); | 27 PR_ASSERT(pMutex != 0); |
26 PR_ASSERT(pMutex->u.sslLock!= 0); | 28 PR_ASSERT(pMutex->u.sslLock != 0); |
27 if (!pMutex->u.sslLock) { | 29 if (!pMutex->u.sslLock) { |
28 PORT_SetError(PR_INVALID_ARGUMENT_ERROR); | 30 PORT_SetError(PR_INVALID_ARGUMENT_ERROR); |
29 return SECFailure; | 31 return SECFailure; |
30 } | 32 } |
31 PR_DestroyLock(pMutex->u.sslLock); | 33 PR_DestroyLock(pMutex->u.sslLock); |
32 return SECSuccess; | 34 return SECSuccess; |
33 } | 35 } |
34 | 36 |
35 static SECStatus single_process_sslMutex_Unlock(sslMutex* pMutex) | 37 static SECStatus |
| 38 single_process_sslMutex_Unlock(sslMutex* pMutex) |
36 { | 39 { |
37 PR_ASSERT(pMutex != 0 ); | 40 PR_ASSERT(pMutex != 0); |
38 PR_ASSERT(pMutex->u.sslLock !=0); | 41 PR_ASSERT(pMutex->u.sslLock != 0); |
39 if (!pMutex->u.sslLock) { | 42 if (!pMutex->u.sslLock) { |
40 PORT_SetError(PR_INVALID_ARGUMENT_ERROR); | 43 PORT_SetError(PR_INVALID_ARGUMENT_ERROR); |
41 return SECFailure; | 44 return SECFailure; |
42 } | 45 } |
43 PR_Unlock(pMutex->u.sslLock); | 46 PR_Unlock(pMutex->u.sslLock); |
44 return SECSuccess; | 47 return SECSuccess; |
45 } | 48 } |
46 | 49 |
47 static SECStatus single_process_sslMutex_Lock(sslMutex* pMutex) | 50 static SECStatus |
| 51 single_process_sslMutex_Lock(sslMutex* pMutex) |
48 { | 52 { |
49 PR_ASSERT(pMutex != 0); | 53 PR_ASSERT(pMutex != 0); |
50 PR_ASSERT(pMutex->u.sslLock != 0 ); | 54 PR_ASSERT(pMutex->u.sslLock != 0); |
51 if (!pMutex->u.sslLock) { | 55 if (!pMutex->u.sslLock) { |
52 PORT_SetError(PR_INVALID_ARGUMENT_ERROR); | 56 PORT_SetError(PR_INVALID_ARGUMENT_ERROR); |
53 return SECFailure; | 57 return SECFailure; |
54 } | 58 } |
55 PR_Lock(pMutex->u.sslLock); | 59 PR_Lock(pMutex->u.sslLock); |
56 return SECSuccess; | 60 return SECSuccess; |
57 } | 61 } |
58 | 62 |
59 #if defined(LINUX) || defined(AIX) || defined(BEOS) || defined(BSDI) || (defined
(NETBSD) && __NetBSD_Version__ < 500000000) || defined(OPENBSD) | 63 #if defined(LINUX) || defined(AIX) || defined(BEOS) || defined(BSDI) || (defined
(NETBSD) && __NetBSD_Version__ < 500000000) || defined(OPENBSD) |
60 | 64 |
61 #include <unistd.h> | 65 #include <unistd.h> |
62 #include <fcntl.h> | 66 #include <fcntl.h> |
63 #include <string.h> | 67 #include <string.h> |
64 #include <errno.h> | 68 #include <errno.h> |
65 #include "unix_err.h" | 69 #include "unix_err.h" |
66 #include "pratom.h" | 70 #include "pratom.h" |
67 | 71 |
68 #define SSL_MUTEX_MAGIC 0xfeedfd | 72 #define SSL_MUTEX_MAGIC 0xfeedfd |
69 #define NONBLOCKING_POSTS 1» /* maybe this is faster */ | 73 #define NONBLOCKING_POSTS 1 /* maybe this is faster */ |
70 | 74 |
71 #if NONBLOCKING_POSTS | 75 #if NONBLOCKING_POSTS |
72 | 76 |
73 #ifndef FNONBLOCK | 77 #ifndef FNONBLOCK |
74 #define FNONBLOCK O_NONBLOCK | 78 #define FNONBLOCK O_NONBLOCK |
75 #endif | 79 #endif |
76 | 80 |
77 static int | 81 static int |
78 setNonBlocking(int fd, int nonBlocking) | 82 setNonBlocking(int fd, int nonBlocking) |
79 { | 83 { |
80 int flags; | 84 int flags; |
81 int err; | 85 int err; |
82 | 86 |
83 flags = fcntl(fd, F_GETFL, 0); | 87 flags = fcntl(fd, F_GETFL, 0); |
84 if (0 > flags) | 88 if (0 > flags) |
85 » return flags; | 89 return flags; |
86 if (nonBlocking) | 90 if (nonBlocking) |
87 » flags |= FNONBLOCK; | 91 flags |= FNONBLOCK; |
88 else | 92 else |
89 » flags &= ~FNONBLOCK; | 93 flags &= ~FNONBLOCK; |
90 err = fcntl(fd, F_SETFL, flags); | 94 err = fcntl(fd, F_SETFL, flags); |
91 return err; | 95 return err; |
92 } | 96 } |
93 #endif | 97 #endif |
94 | 98 |
95 SECStatus | 99 SECStatus |
96 sslMutex_Init(sslMutex *pMutex, int shared) | 100 sslMutex_Init(sslMutex* pMutex, int shared) |
97 { | 101 { |
98 int err; | 102 int err; |
99 PR_ASSERT(pMutex); | 103 PR_ASSERT(pMutex); |
100 pMutex->isMultiProcess = (PRBool)(shared != 0); | 104 pMutex->isMultiProcess = (PRBool)(shared != 0); |
101 if (!shared) { | 105 if (!shared) { |
102 return single_process_sslMutex_Init(pMutex); | 106 return single_process_sslMutex_Init(pMutex); |
103 } | 107 } |
104 pMutex->u.pipeStr.mPipes[0] = -1; | 108 pMutex->u.pipeStr.mPipes[0] = -1; |
105 pMutex->u.pipeStr.mPipes[1] = -1; | 109 pMutex->u.pipeStr.mPipes[1] = -1; |
106 pMutex->u.pipeStr.mPipes[2] = -1; | 110 pMutex->u.pipeStr.mPipes[2] = -1; |
107 pMutex->u.pipeStr.nWaiters = 0; | 111 pMutex->u.pipeStr.nWaiters = 0; |
108 | 112 |
109 err = pipe(pMutex->u.pipeStr.mPipes); | 113 err = pipe(pMutex->u.pipeStr.mPipes); |
110 if (err) { | 114 if (err) { |
111 » nss_MD_unix_map_default_error(errno); | 115 nss_MD_unix_map_default_error(errno); |
112 » return err; | 116 return err; |
113 } | 117 } |
114 #if NONBLOCKING_POSTS | 118 #if NONBLOCKING_POSTS |
115 err = setNonBlocking(pMutex->u.pipeStr.mPipes[1], 1); | 119 err = setNonBlocking(pMutex->u.pipeStr.mPipes[1], 1); |
116 if (err) | 120 if (err) |
117 » goto loser; | 121 goto loser; |
118 #endif | 122 #endif |
119 | 123 |
120 pMutex->u.pipeStr.mPipes[2] = SSL_MUTEX_MAGIC; | 124 pMutex->u.pipeStr.mPipes[2] = SSL_MUTEX_MAGIC; |
121 | 125 |
122 #if defined(LINUX) && defined(i386) | 126 #if defined(LINUX) && defined(i386) |
123 /* Pipe starts out empty */ | 127 /* Pipe starts out empty */ |
124 return SECSuccess; | 128 return SECSuccess; |
125 #else | 129 #else |
126 /* Pipe starts with one byte. */ | 130 /* Pipe starts with one byte. */ |
127 return sslMutex_Unlock(pMutex); | 131 return sslMutex_Unlock(pMutex); |
128 #endif | 132 #endif |
129 | 133 |
130 loser: | 134 loser: |
131 nss_MD_unix_map_default_error(errno); | 135 nss_MD_unix_map_default_error(errno); |
132 close(pMutex->u.pipeStr.mPipes[0]); | 136 close(pMutex->u.pipeStr.mPipes[0]); |
133 close(pMutex->u.pipeStr.mPipes[1]); | 137 close(pMutex->u.pipeStr.mPipes[1]); |
134 return SECFailure; | 138 return SECFailure; |
135 } | 139 } |
136 | 140 |
137 SECStatus | 141 SECStatus |
138 sslMutex_Destroy(sslMutex *pMutex, PRBool processLocal) | 142 sslMutex_Destroy(sslMutex* pMutex, PRBool processLocal) |
139 { | 143 { |
140 if (PR_FALSE == pMutex->isMultiProcess) { | 144 if (PR_FALSE == pMutex->isMultiProcess) { |
141 return single_process_sslMutex_Destroy(pMutex); | 145 return single_process_sslMutex_Destroy(pMutex); |
142 } | 146 } |
143 if (pMutex->u.pipeStr.mPipes[2] != SSL_MUTEX_MAGIC) { | 147 if (pMutex->u.pipeStr.mPipes[2] != SSL_MUTEX_MAGIC) { |
144 » PORT_SetError(PR_INVALID_ARGUMENT_ERROR); | 148 PORT_SetError(PR_INVALID_ARGUMENT_ERROR); |
145 » return SECFailure; | 149 return SECFailure; |
146 } | 150 } |
147 close(pMutex->u.pipeStr.mPipes[0]); | 151 close(pMutex->u.pipeStr.mPipes[0]); |
148 close(pMutex->u.pipeStr.mPipes[1]); | 152 close(pMutex->u.pipeStr.mPipes[1]); |
149 | 153 |
150 if (processLocal) { | 154 if (processLocal) { |
151 » return SECSuccess; | 155 return SECSuccess; |
152 } | 156 } |
153 | 157 |
154 pMutex->u.pipeStr.mPipes[0] = -1; | 158 pMutex->u.pipeStr.mPipes[0] = -1; |
155 pMutex->u.pipeStr.mPipes[1] = -1; | 159 pMutex->u.pipeStr.mPipes[1] = -1; |
156 pMutex->u.pipeStr.mPipes[2] = -1; | 160 pMutex->u.pipeStr.mPipes[2] = -1; |
157 pMutex->u.pipeStr.nWaiters = 0; | 161 pMutex->u.pipeStr.nWaiters = 0; |
158 | 162 |
159 return SECSuccess; | 163 return SECSuccess; |
160 } | 164 } |
161 | 165 |
162 #if defined(LINUX) && defined(i386) | 166 #if defined(LINUX) && defined(i386) |
163 /* No memory barrier needed for this platform */ | 167 /* No memory barrier needed for this platform */ |
164 | 168 |
165 /* nWaiters includes the holder of the lock (if any) and the number | 169 /* nWaiters includes the holder of the lock (if any) and the number |
166 ** threads waiting for it. After incrementing nWaiters, if the count | 170 ** threads waiting for it. After incrementing nWaiters, if the count |
167 ** is exactly 1, then you have the lock and may proceed. If the | 171 ** is exactly 1, then you have the lock and may proceed. If the |
168 ** count is greater than 1, then you must wait on the pipe. | 172 ** count is greater than 1, then you must wait on the pipe. |
169 */ | 173 */ |
170 | 174 |
171 | 175 SECStatus |
172 SECStatus | 176 sslMutex_Unlock(sslMutex* pMutex) |
173 sslMutex_Unlock(sslMutex *pMutex) | |
174 { | 177 { |
175 PRInt32 newValue; | 178 PRInt32 newValue; |
176 if (PR_FALSE == pMutex->isMultiProcess) { | 179 if (PR_FALSE == pMutex->isMultiProcess) { |
177 return single_process_sslMutex_Unlock(pMutex); | 180 return single_process_sslMutex_Unlock(pMutex); |
178 } | 181 } |
179 | 182 |
180 if (pMutex->u.pipeStr.mPipes[2] != SSL_MUTEX_MAGIC) { | 183 if (pMutex->u.pipeStr.mPipes[2] != SSL_MUTEX_MAGIC) { |
181 » PORT_SetError(PR_INVALID_ARGUMENT_ERROR); | 184 PORT_SetError(PR_INVALID_ARGUMENT_ERROR); |
182 » return SECFailure; | 185 return SECFailure; |
183 } | 186 } |
184 /* Do Memory Barrier here. */ | 187 /* Do Memory Barrier here. */ |
185 newValue = PR_ATOMIC_DECREMENT(&pMutex->u.pipeStr.nWaiters); | 188 newValue = PR_ATOMIC_DECREMENT(&pMutex->u.pipeStr.nWaiters); |
186 if (newValue > 0) { | 189 if (newValue > 0) { |
187 » int cc; | 190 int cc; |
188 » char c = 1; | 191 char c = 1; |
189 » do { | 192 do { |
190 » cc = write(pMutex->u.pipeStr.mPipes[1], &c, 1); | 193 cc = write(pMutex->u.pipeStr.mPipes[1], &c, 1); |
191 » } while (cc < 0 && (errno == EINTR || errno == EAGAIN)); | 194 } while (cc < 0 && (errno == EINTR || errno == EAGAIN)); |
192 » if (cc != 1) { | 195 if (cc != 1) { |
193 » if (cc < 0) | 196 if (cc < 0) |
194 » » nss_MD_unix_map_default_error(errno); | 197 nss_MD_unix_map_default_error(errno); |
195 » else | 198 else |
196 » » PORT_SetError(PR_UNKNOWN_ERROR); | 199 PORT_SetError(PR_UNKNOWN_ERROR); |
197 » return SECFailure; | 200 return SECFailure; |
198 » } | 201 } |
199 } | 202 } |
200 return SECSuccess; | 203 return SECSuccess; |
201 } | 204 } |
202 | 205 |
203 SECStatus | 206 SECStatus |
204 sslMutex_Lock(sslMutex *pMutex) | 207 sslMutex_Lock(sslMutex* pMutex) |
205 { | 208 { |
206 PRInt32 newValue; | 209 PRInt32 newValue; |
207 if (PR_FALSE == pMutex->isMultiProcess) { | 210 if (PR_FALSE == pMutex->isMultiProcess) { |
208 return single_process_sslMutex_Lock(pMutex); | 211 return single_process_sslMutex_Lock(pMutex); |
209 } | 212 } |
210 | 213 |
211 if (pMutex->u.pipeStr.mPipes[2] != SSL_MUTEX_MAGIC) { | 214 if (pMutex->u.pipeStr.mPipes[2] != SSL_MUTEX_MAGIC) { |
212 » PORT_SetError(PR_INVALID_ARGUMENT_ERROR); | 215 PORT_SetError(PR_INVALID_ARGUMENT_ERROR); |
213 » return SECFailure; | 216 return SECFailure; |
214 } | 217 } |
215 newValue = PR_ATOMIC_INCREMENT(&pMutex->u.pipeStr.nWaiters); | 218 newValue = PR_ATOMIC_INCREMENT(&pMutex->u.pipeStr.nWaiters); |
216 /* Do Memory Barrier here. */ | 219 /* Do Memory Barrier here. */ |
217 if (newValue > 1) { | 220 if (newValue > 1) { |
218 » int cc; | 221 int cc; |
219 » char c; | 222 char c; |
220 » do { | 223 do { |
221 » cc = read(pMutex->u.pipeStr.mPipes[0], &c, 1); | 224 cc = read(pMutex->u.pipeStr.mPipes[0], &c, 1); |
222 » } while (cc < 0 && errno == EINTR); | 225 } while (cc < 0 && errno == EINTR); |
223 » if (cc != 1) { | 226 if (cc != 1) { |
224 » if (cc < 0) | 227 if (cc < 0) |
225 » » nss_MD_unix_map_default_error(errno); | 228 nss_MD_unix_map_default_error(errno); |
226 » else | 229 else |
227 » » PORT_SetError(PR_UNKNOWN_ERROR); | 230 PORT_SetError(PR_UNKNOWN_ERROR); |
228 » return SECFailure; | 231 return SECFailure; |
229 » } | 232 } |
230 } | 233 } |
231 return SECSuccess; | 234 return SECSuccess; |
232 } | 235 } |
233 | 236 |
234 #else | 237 #else |
235 | 238 |
236 /* Using Atomic operations requires the use of a memory barrier instruction | 239 /* Using Atomic operations requires the use of a memory barrier instruction |
237 ** on PowerPC, Sparc, and Alpha. NSPR's PR_Atomic functions do not perform | 240 ** on PowerPC, Sparc, and Alpha. NSPR's PR_Atomic functions do not perform |
238 ** them, and NSPR does not provide a function that does them (e.g. PR_Barrier). | 241 ** them, and NSPR does not provide a function that does them (e.g. PR_Barrier). |
239 ** So, we don't use them on those platforms. | 242 ** So, we don't use them on those platforms. |
240 */ | 243 */ |
241 | 244 |
242 SECStatus | 245 SECStatus |
243 sslMutex_Unlock(sslMutex *pMutex) | 246 sslMutex_Unlock(sslMutex* pMutex) |
244 { | 247 { |
245 int cc; | 248 int cc; |
246 char c = 1; | 249 char c = 1; |
247 | 250 |
248 if (PR_FALSE == pMutex->isMultiProcess) { | 251 if (PR_FALSE == pMutex->isMultiProcess) { |
249 return single_process_sslMutex_Unlock(pMutex); | 252 return single_process_sslMutex_Unlock(pMutex); |
250 } | 253 } |
251 | 254 |
252 if (pMutex->u.pipeStr.mPipes[2] != SSL_MUTEX_MAGIC) { | 255 if (pMutex->u.pipeStr.mPipes[2] != SSL_MUTEX_MAGIC) { |
253 » PORT_SetError(PR_INVALID_ARGUMENT_ERROR); | 256 PORT_SetError(PR_INVALID_ARGUMENT_ERROR); |
254 » return SECFailure; | 257 return SECFailure; |
255 } | 258 } |
256 do { | 259 do { |
257 » cc = write(pMutex->u.pipeStr.mPipes[1], &c, 1); | 260 cc = write(pMutex->u.pipeStr.mPipes[1], &c, 1); |
258 } while (cc < 0 && (errno == EINTR || errno == EAGAIN)); | 261 } while (cc < 0 && (errno == EINTR || errno == EAGAIN)); |
259 if (cc != 1) { | 262 if (cc != 1) { |
260 » if (cc < 0) | 263 if (cc < 0) |
261 » nss_MD_unix_map_default_error(errno); | 264 nss_MD_unix_map_default_error(errno); |
262 » else | 265 else |
263 » PORT_SetError(PR_UNKNOWN_ERROR); | 266 PORT_SetError(PR_UNKNOWN_ERROR); |
264 » return SECFailure; | 267 return SECFailure; |
265 } | 268 } |
266 | 269 |
267 return SECSuccess; | 270 return SECSuccess; |
268 } | 271 } |
269 | 272 |
270 SECStatus | 273 SECStatus |
271 sslMutex_Lock(sslMutex *pMutex) | 274 sslMutex_Lock(sslMutex* pMutex) |
272 { | 275 { |
273 int cc; | 276 int cc; |
274 char c; | 277 char c; |
275 | 278 |
276 if (PR_FALSE == pMutex->isMultiProcess) { | 279 if (PR_FALSE == pMutex->isMultiProcess) { |
277 return single_process_sslMutex_Lock(pMutex); | 280 return single_process_sslMutex_Lock(pMutex); |
278 } | 281 } |
279 | 282 |
280 if (pMutex->u.pipeStr.mPipes[2] != SSL_MUTEX_MAGIC) { | 283 if (pMutex->u.pipeStr.mPipes[2] != SSL_MUTEX_MAGIC) { |
281 » PORT_SetError(PR_INVALID_ARGUMENT_ERROR); | 284 PORT_SetError(PR_INVALID_ARGUMENT_ERROR); |
282 » return SECFailure; | 285 return SECFailure; |
283 } | 286 } |
284 | 287 |
285 do { | 288 do { |
286 » cc = read(pMutex->u.pipeStr.mPipes[0], &c, 1); | 289 cc = read(pMutex->u.pipeStr.mPipes[0], &c, 1); |
287 } while (cc < 0 && errno == EINTR); | 290 } while (cc < 0 && errno == EINTR); |
288 if (cc != 1) { | 291 if (cc != 1) { |
289 » if (cc < 0) | 292 if (cc < 0) |
290 » nss_MD_unix_map_default_error(errno); | 293 nss_MD_unix_map_default_error(errno); |
291 » else | 294 else |
292 » PORT_SetError(PR_UNKNOWN_ERROR); | 295 PORT_SetError(PR_UNKNOWN_ERROR); |
293 » return SECFailure; | 296 return SECFailure; |
294 } | 297 } |
295 | 298 |
296 return SECSuccess; | 299 return SECSuccess; |
297 } | 300 } |
298 | 301 |
299 #endif | 302 #endif |
300 | 303 |
301 #elif defined(WIN32) | 304 #elif defined(WIN32) |
302 | 305 |
303 #include "win32err.h" | 306 #include "win32err.h" |
304 | 307 |
305 /* on Windows, we need to find the optimal type of locking mechanism to use | 308 /* on Windows, we need to find the optimal type of locking mechanism to use |
306 for the sslMutex. | 309 for the sslMutex. |
307 | 310 |
308 There are 3 cases : | 311 There are 3 cases : |
309 1) single-process, use a PRLock, as for all other platforms | 312 1) single-process, use a PRLock, as for all other platforms |
310 2) Win95 multi-process, use a Win32 mutex | 313 2) Win95 multi-process, use a Win32 mutex |
311 3) on WINNT multi-process, use a PRLock + a Win32 mutex | 314 3) on WINNT multi-process, use a PRLock + a Win32 mutex |
312 | 315 |
313 */ | 316 */ |
314 | 317 |
315 #ifdef WINNT | 318 #ifdef WINNT |
316 | 319 |
317 SECStatus sslMutex_2LevelInit(sslMutex *sem) | 320 SECStatus |
| 321 sslMutex_2LevelInit(sslMutex *sem) |
318 { | 322 { |
319 /* the following adds a PRLock to sslMutex . This is done in each | 323 /* the following adds a PRLock to sslMutex . This is done in each |
320 process of a multi-process server and is only needed on WINNT, if | 324 process of a multi-process server and is only needed on WINNT, if |
321 using fibers. We can't tell if native threads or fibers are used, so | 325 using fibers. We can't tell if native threads or fibers are used, so |
322 we always do it on WINNT | 326 we always do it on WINNT |
323 */ | 327 */ |
324 PR_ASSERT(sem); | 328 PR_ASSERT(sem); |
325 if (sem) { | 329 if (sem) { |
326 /* we need to reset the sslLock in the children or the single_process in
it | 330 /* we need to reset the sslLock in the children or the single_process in
it |
327 function below will assert */ | 331 function below will assert */ |
328 sem->u.sslLock = NULL; | 332 sem->u.sslLock = NULL; |
329 } | 333 } |
330 return single_process_sslMutex_Init(sem); | 334 return single_process_sslMutex_Init(sem); |
331 } | 335 } |
332 | 336 |
333 static SECStatus sslMutex_2LevelDestroy(sslMutex *sem) | 337 static SECStatus |
| 338 sslMutex_2LevelDestroy(sslMutex *sem) |
334 { | 339 { |
335 return single_process_sslMutex_Destroy(sem); | 340 return single_process_sslMutex_Destroy(sem); |
336 } | 341 } |
337 | 342 |
338 #endif | 343 #endif |
339 | 344 |
340 SECStatus | 345 SECStatus |
341 sslMutex_Init(sslMutex *pMutex, int shared) | 346 sslMutex_Init(sslMutex *pMutex, int shared) |
342 { | 347 { |
343 #ifdef WINNT | 348 #ifdef WINNT |
344 SECStatus retvalue; | 349 SECStatus retvalue; |
345 #endif | 350 #endif |
346 HANDLE hMutex; | 351 HANDLE hMutex; |
347 SECURITY_ATTRIBUTES attributes = | 352 SECURITY_ATTRIBUTES attributes = |
348 { sizeof(SECURITY_ATTRIBUTES), NULL, TRUE }; | 353 { sizeof(SECURITY_ATTRIBUTES), NULL, TRUE }; |
349 | 354 |
350 PR_ASSERT(pMutex != 0 && (pMutex->u.sslMutx == 0 || | 355 PR_ASSERT(pMutex != 0 && (pMutex->u.sslMutx == 0 || |
351 pMutex->u.sslMutx == INVALID_HANDLE_VALUE) ); | 356 pMutex->u.sslMutx == |
352 | 357 INVALID_HANDLE_VALUE)); |
| 358 |
353 pMutex->isMultiProcess = (PRBool)(shared != 0); | 359 pMutex->isMultiProcess = (PRBool)(shared != 0); |
354 | 360 |
355 if (PR_FALSE == pMutex->isMultiProcess) { | 361 if (PR_FALSE == pMutex->isMultiProcess) { |
356 return single_process_sslMutex_Init(pMutex); | 362 return single_process_sslMutex_Init(pMutex); |
357 } | 363 } |
358 | 364 |
359 #ifdef WINNT | 365 #ifdef WINNT |
360 /* we need a lock on WINNT for fibers in the parent process */ | 366 /* we need a lock on WINNT for fibers in the parent process */ |
361 retvalue = sslMutex_2LevelInit(pMutex); | 367 retvalue = sslMutex_2LevelInit(pMutex); |
362 if (SECSuccess != retvalue) | 368 if (SECSuccess != retvalue) |
363 return SECFailure; | 369 return SECFailure; |
364 #endif | 370 #endif |
365 | 371 |
366 if (!pMutex || ((hMutex = pMutex->u.sslMutx) != 0 && | 372 if (!pMutex || ((hMutex = pMutex->u.sslMutx) != 0 && |
367 hMutex != INVALID_HANDLE_VALUE)) { | 373 hMutex != |
| 374 INVALID_HANDLE_VALUE)) { |
368 PORT_SetError(PR_INVALID_ARGUMENT_ERROR); | 375 PORT_SetError(PR_INVALID_ARGUMENT_ERROR); |
369 return SECFailure; | 376 return SECFailure; |
370 } | 377 } |
371 attributes.bInheritHandle = (shared ? TRUE : FALSE); | 378 attributes.bInheritHandle = (shared ? TRUE : FALSE); |
372 hMutex = CreateMutex(&attributes, FALSE, NULL); | 379 hMutex = CreateMutex(&attributes, FALSE, NULL); |
373 if (hMutex == NULL) { | 380 if (hMutex == NULL) { |
374 hMutex = INVALID_HANDLE_VALUE; | 381 hMutex = INVALID_HANDLE_VALUE; |
375 nss_MD_win32_map_default_error(GetLastError()); | 382 nss_MD_win32_map_default_error(GetLastError()); |
376 return SECFailure; | 383 return SECFailure; |
377 } | 384 } |
378 pMutex->u.sslMutx = hMutex; | 385 pMutex->u.sslMutx = hMutex; |
379 return SECSuccess; | 386 return SECSuccess; |
380 } | 387 } |
381 | 388 |
382 SECStatus | 389 SECStatus |
383 sslMutex_Destroy(sslMutex *pMutex, PRBool processLocal) | 390 sslMutex_Destroy(sslMutex *pMutex, PRBool processLocal) |
384 { | 391 { |
385 HANDLE hMutex; | 392 HANDLE hMutex; |
386 int rv; | 393 int rv; |
387 int retvalue = SECSuccess; | 394 int retvalue = SECSuccess; |
388 | 395 |
389 PR_ASSERT(pMutex != 0); | 396 PR_ASSERT(pMutex != 0); |
390 if (PR_FALSE == pMutex->isMultiProcess) { | 397 if (PR_FALSE == pMutex->isMultiProcess) { |
391 return single_process_sslMutex_Destroy(pMutex); | 398 return single_process_sslMutex_Destroy(pMutex); |
392 } | 399 } |
393 | 400 |
394 /* multi-process mode */ | 401 /* multi-process mode */ |
395 #ifdef WINNT | 402 #ifdef WINNT |
396 /* on NT, get rid of the PRLock used for fibers within a process */ | 403 /* on NT, get rid of the PRLock used for fibers within a process */ |
397 retvalue = sslMutex_2LevelDestroy(pMutex); | 404 retvalue = sslMutex_2LevelDestroy(pMutex); |
398 #endif | 405 #endif |
399 | 406 |
400 PR_ASSERT( pMutex->u.sslMutx != 0 && | 407 PR_ASSERT(pMutex->u.sslMutx != 0 && |
401 pMutex->u.sslMutx != INVALID_HANDLE_VALUE); | 408 pMutex->u.sslMutx != INVALID_HANDLE_VALUE); |
402 if (!pMutex || (hMutex = pMutex->u.sslMutx) == 0 | 409 if (!pMutex || (hMutex = pMutex->u.sslMutx) == 0 || |
403 || hMutex == INVALID_HANDLE_VALUE) { | 410 hMutex == INVALID_HANDLE_VALUE) { |
404 PORT_SetError(PR_INVALID_ARGUMENT_ERROR); | 411 PORT_SetError(PR_INVALID_ARGUMENT_ERROR); |
405 return SECFailure; | 412 return SECFailure; |
406 } | 413 } |
407 | 414 |
408 rv = CloseHandle(hMutex); /* ignore error */ | 415 rv = CloseHandle(hMutex); /* ignore error */ |
409 if (!processLocal && rv) { | 416 if (!processLocal && rv) { |
410 pMutex->u.sslMutx = hMutex = INVALID_HANDLE_VALUE; | 417 pMutex->u.sslMutx = hMutex = INVALID_HANDLE_VALUE; |
411 } | 418 } |
412 if (!rv) { | 419 if (!rv) { |
413 nss_MD_win32_map_default_error(GetLastError()); | 420 nss_MD_win32_map_default_error(GetLastError()); |
414 retvalue = SECFailure; | 421 retvalue = SECFailure; |
415 } | 422 } |
416 return retvalue; | 423 return retvalue; |
417 } | 424 } |
418 | 425 |
419 int | 426 int |
420 sslMutex_Unlock(sslMutex *pMutex) | 427 sslMutex_Unlock(sslMutex *pMutex) |
421 { | 428 { |
422 BOOL success = FALSE; | 429 BOOL success = FALSE; |
423 HANDLE hMutex; | 430 HANDLE hMutex; |
424 | 431 |
425 PR_ASSERT(pMutex != 0 ); | 432 PR_ASSERT(pMutex != 0); |
426 if (PR_FALSE == pMutex->isMultiProcess) { | 433 if (PR_FALSE == pMutex->isMultiProcess) { |
427 return single_process_sslMutex_Unlock(pMutex); | 434 return single_process_sslMutex_Unlock(pMutex); |
428 } | 435 } |
429 | 436 |
430 PR_ASSERT(pMutex->u.sslMutx != 0 && | 437 PR_ASSERT(pMutex->u.sslMutx != 0 && |
431 pMutex->u.sslMutx != INVALID_HANDLE_VALUE); | 438 pMutex->u.sslMutx != INVALID_HANDLE_VALUE); |
432 if (!pMutex || (hMutex = pMutex->u.sslMutx) == 0 || | 439 if (!pMutex || (hMutex = pMutex->u.sslMutx) == 0 || |
433 hMutex == INVALID_HANDLE_VALUE) { | 440 hMutex == INVALID_HANDLE_VALUE) { |
434 PORT_SetError(PR_INVALID_ARGUMENT_ERROR); | 441 PORT_SetError(PR_INVALID_ARGUMENT_ERROR); |
435 return SECFailure; | 442 return SECFailure; |
436 } | 443 } |
437 success = ReleaseMutex(hMutex); | 444 success = ReleaseMutex(hMutex); |
438 if (!success) { | 445 if (!success) { |
439 nss_MD_win32_map_default_error(GetLastError()); | 446 nss_MD_win32_map_default_error(GetLastError()); |
440 return SECFailure; | 447 return SECFailure; |
441 } | 448 } |
442 #ifdef WINNT | 449 #ifdef WINNT |
443 return single_process_sslMutex_Unlock(pMutex); | 450 return single_process_sslMutex_Unlock(pMutex); |
444 /* release PRLock for other fibers in the process */ | 451 /* release PRLock for other fibers in the process */ |
445 #else | 452 #else |
446 return SECSuccess; | 453 return SECSuccess; |
447 #endif | 454 #endif |
448 } | 455 } |
449 | 456 |
450 int | 457 int |
451 sslMutex_Lock(sslMutex *pMutex) | 458 sslMutex_Lock(sslMutex *pMutex) |
452 { | 459 { |
453 HANDLE hMutex; | 460 HANDLE hMutex; |
454 DWORD event; | 461 DWORD event; |
455 DWORD lastError; | 462 DWORD lastError; |
456 SECStatus rv; | 463 SECStatus rv; |
457 SECStatus retvalue = SECSuccess; | 464 SECStatus retvalue = SECSuccess; |
458 PR_ASSERT(pMutex != 0); | 465 PR_ASSERT(pMutex != 0); |
459 | 466 |
460 if (PR_FALSE == pMutex->isMultiProcess) { | 467 if (PR_FALSE == pMutex->isMultiProcess) { |
461 return single_process_sslMutex_Lock(pMutex); | 468 return single_process_sslMutex_Lock(pMutex); |
462 } | 469 } |
463 #ifdef WINNT | 470 #ifdef WINNT |
464 /* lock first to preserve from other threads/fibers | 471 /* lock first to preserve from other threads/fibers |
465 in the same process */ | 472 in the same process */ |
466 retvalue = single_process_sslMutex_Lock(pMutex); | 473 retvalue = single_process_sslMutex_Lock(pMutex); |
467 #endif | 474 #endif |
468 PR_ASSERT(pMutex->u.sslMutx != 0 && | 475 PR_ASSERT(pMutex->u.sslMutx != 0 && |
469 pMutex->u.sslMutx != INVALID_HANDLE_VALUE); | 476 pMutex->u.sslMutx != INVALID_HANDLE_VALUE); |
470 if (!pMutex || (hMutex = pMutex->u.sslMutx) == 0 || | 477 if (!pMutex || (hMutex = pMutex->u.sslMutx) == 0 || |
471 hMutex == INVALID_HANDLE_VALUE) { | 478 hMutex == INVALID_HANDLE_VALUE) { |
472 PORT_SetError(PR_INVALID_ARGUMENT_ERROR); | 479 PORT_SetError(PR_INVALID_ARGUMENT_ERROR); |
473 return SECFailure; /* what else ? */ | 480 return SECFailure; /* what else ? */ |
474 } | 481 } |
475 /* acquire the mutex to be the only owner accross all other processes */ | 482 /* acquire the mutex to be the only owner accross all other processes */ |
476 event = WaitForSingleObject(hMutex, INFINITE); | 483 event = WaitForSingleObject(hMutex, INFINITE); |
477 switch (event) { | 484 switch (event) { |
478 case WAIT_OBJECT_0: | 485 case WAIT_OBJECT_0: |
479 case WAIT_ABANDONED: | 486 case WAIT_ABANDONED: |
480 rv = SECSuccess; | 487 rv = SECSuccess; |
481 break; | 488 break; |
482 | 489 |
483 case WAIT_TIMEOUT: | 490 case WAIT_TIMEOUT: |
484 #if defined(WAIT_IO_COMPLETION) | 491 #if defined(WAIT_IO_COMPLETION) |
485 case WAIT_IO_COMPLETION: | 492 case WAIT_IO_COMPLETION: |
486 #endif | 493 #endif |
487 default: /* should never happen. nothing we can do. */ | 494 default: /* should never happen. nothing we can do. */ |
488 PR_ASSERT(!("WaitForSingleObject returned invalid value.")); | 495 PR_ASSERT(!("WaitForSingleObject returned invalid value.")); |
489 » PORT_SetError(PR_UNKNOWN_ERROR); | 496 PORT_SetError(PR_UNKNOWN_ERROR); |
490 » rv = SECFailure; | 497 rv = SECFailure; |
491 » break; | 498 break; |
492 | 499 |
493 case WAIT_FAILED: /* failure returns this */ | 500 case WAIT_FAILED: /* failure returns this */ |
494 rv = SECFailure; | 501 rv = SECFailure; |
495 lastError = GetLastError(); /* for debugging */ | 502 lastError = GetLastError(); /* for debugging */ |
496 nss_MD_win32_map_default_error(lastError); | 503 nss_MD_win32_map_default_error(lastError); |
497 break; | 504 break; |
498 } | 505 } |
499 | 506 |
500 if (! (SECSuccess == retvalue && SECSuccess == rv)) { | 507 if (!(SECSuccess == retvalue && SECSuccess == rv)) { |
501 return SECFailure; | 508 return SECFailure; |
502 } | 509 } |
503 | 510 |
504 return SECSuccess; | 511 return SECSuccess; |
505 } | 512 } |
506 | 513 |
507 #elif defined(XP_UNIX) && !defined(DARWIN) | 514 #elif defined(XP_UNIX) && !defined(DARWIN) |
508 | 515 |
509 #include <errno.h> | 516 #include <errno.h> |
510 #include "unix_err.h" | 517 #include "unix_err.h" |
511 | 518 |
512 SECStatus | 519 SECStatus |
513 sslMutex_Init(sslMutex *pMutex, int shared) | 520 sslMutex_Init(sslMutex* pMutex, int shared) |
514 { | 521 { |
515 int rv; | 522 int rv; |
516 PR_ASSERT(pMutex); | 523 PR_ASSERT(pMutex); |
517 pMutex->isMultiProcess = (PRBool)(shared != 0); | 524 pMutex->isMultiProcess = (PRBool)(shared != 0); |
518 if (!shared) { | 525 if (!shared) { |
519 return single_process_sslMutex_Init(pMutex); | 526 return single_process_sslMutex_Init(pMutex); |
520 } | 527 } |
521 do { | 528 do { |
522 rv = sem_init(&pMutex->u.sem, shared, 1); | 529 rv = sem_init(&pMutex->u.sem, shared, 1); |
523 } while (rv < 0 && errno == EINTR); | 530 } while (rv < 0 && errno == EINTR); |
524 if (rv < 0) { | 531 if (rv < 0) { |
525 nss_MD_unix_map_default_error(errno); | 532 nss_MD_unix_map_default_error(errno); |
526 return SECFailure; | 533 return SECFailure; |
527 } | 534 } |
528 return SECSuccess; | 535 return SECSuccess; |
529 } | 536 } |
530 | 537 |
531 SECStatus | 538 SECStatus |
532 sslMutex_Destroy(sslMutex *pMutex, PRBool processLocal) | 539 sslMutex_Destroy(sslMutex* pMutex, PRBool processLocal) |
533 { | 540 { |
534 int rv; | 541 int rv; |
535 if (PR_FALSE == pMutex->isMultiProcess) { | 542 if (PR_FALSE == pMutex->isMultiProcess) { |
536 return single_process_sslMutex_Destroy(pMutex); | 543 return single_process_sslMutex_Destroy(pMutex); |
537 } | 544 } |
538 | 545 |
539 /* semaphores are global resources. See SEM_DESTROY(3) man page */ | 546 /* semaphores are global resources. See SEM_DESTROY(3) man page */ |
540 if (processLocal) { | 547 if (processLocal) { |
541 » return SECSuccess; | 548 return SECSuccess; |
542 } | 549 } |
543 do { | 550 do { |
544 » rv = sem_destroy(&pMutex->u.sem); | 551 rv = sem_destroy(&pMutex->u.sem); |
545 } while (rv < 0 && errno == EINTR); | 552 } while (rv < 0 && errno == EINTR); |
546 if (rv < 0) { | 553 if (rv < 0) { |
547 » nss_MD_unix_map_default_error(errno); | 554 nss_MD_unix_map_default_error(errno); |
548 » return SECFailure; | 555 return SECFailure; |
549 } | 556 } |
550 return SECSuccess; | 557 return SECSuccess; |
551 } | 558 } |
552 | 559 |
553 SECStatus | 560 SECStatus |
554 sslMutex_Unlock(sslMutex *pMutex) | 561 sslMutex_Unlock(sslMutex* pMutex) |
555 { | 562 { |
556 int rv; | 563 int rv; |
557 if (PR_FALSE == pMutex->isMultiProcess) { | 564 if (PR_FALSE == pMutex->isMultiProcess) { |
558 return single_process_sslMutex_Unlock(pMutex); | 565 return single_process_sslMutex_Unlock(pMutex); |
559 } | 566 } |
560 do { | 567 do { |
561 » rv = sem_post(&pMutex->u.sem); | 568 rv = sem_post(&pMutex->u.sem); |
562 } while (rv < 0 && errno == EINTR); | 569 } while (rv < 0 && errno == EINTR); |
563 if (rv < 0) { | 570 if (rv < 0) { |
564 » nss_MD_unix_map_default_error(errno); | 571 nss_MD_unix_map_default_error(errno); |
565 » return SECFailure; | 572 return SECFailure; |
566 } | 573 } |
567 return SECSuccess; | 574 return SECSuccess; |
568 } | 575 } |
569 | 576 |
570 SECStatus | 577 SECStatus |
571 sslMutex_Lock(sslMutex *pMutex) | 578 sslMutex_Lock(sslMutex* pMutex) |
572 { | 579 { |
573 int rv; | 580 int rv; |
574 if (PR_FALSE == pMutex->isMultiProcess) { | 581 if (PR_FALSE == pMutex->isMultiProcess) { |
575 return single_process_sslMutex_Lock(pMutex); | 582 return single_process_sslMutex_Lock(pMutex); |
576 } | 583 } |
577 do { | 584 do { |
578 » rv = sem_wait(&pMutex->u.sem); | 585 rv = sem_wait(&pMutex->u.sem); |
579 } while (rv < 0 && errno == EINTR); | 586 } while (rv < 0 && errno == EINTR); |
580 if (rv < 0) { | 587 if (rv < 0) { |
581 » nss_MD_unix_map_default_error(errno); | 588 nss_MD_unix_map_default_error(errno); |
582 » return SECFailure; | 589 return SECFailure; |
583 } | 590 } |
584 return SECSuccess; | 591 return SECSuccess; |
585 } | 592 } |
586 | 593 |
587 #else | 594 #else |
588 | 595 |
589 SECStatus | 596 SECStatus |
590 sslMutex_Init(sslMutex *pMutex, int shared) | 597 sslMutex_Init(sslMutex* pMutex, int shared) |
591 { | 598 { |
592 PR_ASSERT(pMutex); | 599 PR_ASSERT(pMutex); |
593 pMutex->isMultiProcess = (PRBool)(shared != 0); | 600 pMutex->isMultiProcess = (PRBool)(shared != 0); |
594 if (!shared) { | 601 if (!shared) { |
595 return single_process_sslMutex_Init(pMutex); | 602 return single_process_sslMutex_Init(pMutex); |
596 } | 603 } |
597 PORT_Assert(!("sslMutex_Init not implemented for multi-process applications
!")); | 604 PORT_Assert(!("sslMutex_Init not implemented for multi-process applications
!")); |
598 PORT_SetError(PR_NOT_IMPLEMENTED_ERROR); | 605 PORT_SetError(PR_NOT_IMPLEMENTED_ERROR); |
599 return SECFailure; | 606 return SECFailure; |
600 } | 607 } |
601 | 608 |
602 SECStatus | 609 SECStatus |
603 sslMutex_Destroy(sslMutex *pMutex, PRBool processLocal) | 610 sslMutex_Destroy(sslMutex* pMutex, PRBool processLocal) |
604 { | 611 { |
605 PR_ASSERT(pMutex); | 612 PR_ASSERT(pMutex); |
606 if (PR_FALSE == pMutex->isMultiProcess) { | 613 if (PR_FALSE == pMutex->isMultiProcess) { |
607 return single_process_sslMutex_Destroy(pMutex); | 614 return single_process_sslMutex_Destroy(pMutex); |
608 } | 615 } |
609 PORT_Assert(!("sslMutex_Destroy not implemented for multi-process applicatio
ns !")); | 616 PORT_Assert(!("sslMutex_Destroy not implemented for multi-process applicatio
ns !")); |
610 PORT_SetError(PR_NOT_IMPLEMENTED_ERROR); | 617 PORT_SetError(PR_NOT_IMPLEMENTED_ERROR); |
611 return SECFailure; | 618 return SECFailure; |
612 } | 619 } |
613 | 620 |
614 SECStatus | 621 SECStatus |
615 sslMutex_Unlock(sslMutex *pMutex) | 622 sslMutex_Unlock(sslMutex* pMutex) |
616 { | 623 { |
617 PR_ASSERT(pMutex); | 624 PR_ASSERT(pMutex); |
618 if (PR_FALSE == pMutex->isMultiProcess) { | 625 if (PR_FALSE == pMutex->isMultiProcess) { |
619 return single_process_sslMutex_Unlock(pMutex); | 626 return single_process_sslMutex_Unlock(pMutex); |
620 } | 627 } |
621 PORT_Assert(!("sslMutex_Unlock not implemented for multi-process application
s !")); | 628 PORT_Assert(!("sslMutex_Unlock not implemented for multi-process application
s !")); |
622 PORT_SetError(PR_NOT_IMPLEMENTED_ERROR); | 629 PORT_SetError(PR_NOT_IMPLEMENTED_ERROR); |
623 return SECFailure; | 630 return SECFailure; |
624 } | 631 } |
625 | 632 |
626 SECStatus | 633 SECStatus |
627 sslMutex_Lock(sslMutex *pMutex) | 634 sslMutex_Lock(sslMutex* pMutex) |
628 { | 635 { |
629 PR_ASSERT(pMutex); | 636 PR_ASSERT(pMutex); |
630 if (PR_FALSE == pMutex->isMultiProcess) { | 637 if (PR_FALSE == pMutex->isMultiProcess) { |
631 return single_process_sslMutex_Lock(pMutex); | 638 return single_process_sslMutex_Lock(pMutex); |
632 } | 639 } |
633 PORT_Assert(!("sslMutex_Lock not implemented for multi-process applications
!")); | 640 PORT_Assert(!("sslMutex_Lock not implemented for multi-process applications
!")); |
634 PORT_SetError(PR_NOT_IMPLEMENTED_ERROR); | 641 PORT_SetError(PR_NOT_IMPLEMENTED_ERROR); |
635 return SECFailure; | 642 return SECFailure; |
636 } | 643 } |
637 | 644 |
638 #endif | 645 #endif |
639 | 646 |
640 #endif | 647 #endif |
OLD | NEW |