| 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 /* | 5 /* |
| 6 * mutex.c | 6 * mutex.c |
| 7 * | 7 * |
| 8 * This file implements a mutual-exclusion locking facility for Modules | 8 * This file implements a mutual-exclusion locking facility for Modules |
| 9 * using the NSS Cryptoki Framework. | 9 * using the NSS Cryptoki Framework. |
| 10 */ | 10 */ |
| (...skipping 13 matching lines...) Expand all Loading... |
| 24 * nssCKFWMutex_Destroy | 24 * nssCKFWMutex_Destroy |
| 25 * nssCKFWMutex_Lock | 25 * nssCKFWMutex_Lock |
| 26 * nssCKFWMutex_Unlock | 26 * nssCKFWMutex_Unlock |
| 27 * | 27 * |
| 28 * -- debugging versions only -- | 28 * -- debugging versions only -- |
| 29 * nssCKFWMutex_verifyPointer | 29 * nssCKFWMutex_verifyPointer |
| 30 * | 30 * |
| 31 */ | 31 */ |
| 32 | 32 |
| 33 struct NSSCKFWMutexStr { | 33 struct NSSCKFWMutexStr { |
| 34 PRLock *lock; | 34 PRLock *lock; |
| 35 }; | 35 }; |
| 36 | 36 |
| 37 #ifdef DEBUG | 37 #ifdef DEBUG |
| 38 /* | 38 /* |
| 39 * But first, the pointer-tracking stuff. | 39 * But first, the pointer-tracking stuff. |
| 40 * | 40 * |
| 41 * NOTE: the pointer-tracking support in NSS/base currently relies | 41 * NOTE: the pointer-tracking support in NSS/base currently relies |
| 42 * upon NSPR's CallOnce support. That, however, relies upon NSPR's | 42 * upon NSPR's CallOnce support. That, however, relies upon NSPR's |
| 43 * locking, which is tied into the runtime. We need a pointer-tracker | 43 * locking, which is tied into the runtime. We need a pointer-tracker |
| 44 * implementation that uses the locks supplied through C_Initialize. | 44 * implementation that uses the locks supplied through C_Initialize. |
| 45 * That support, however, can be filled in later. So for now, I'll | 45 * That support, however, can be filled in later. So for now, I'll |
| 46 * just do this routines as no-ops. | 46 * just do this routines as no-ops. |
| 47 */ | 47 */ |
| 48 | 48 |
| 49 static CK_RV | 49 static CK_RV |
| 50 mutex_add_pointer | 50 mutex_add_pointer( |
| 51 ( | 51 const NSSCKFWMutex *fwMutex) |
| 52 const NSSCKFWMutex *fwMutex | |
| 53 ) | |
| 54 { | 52 { |
| 55 return CKR_OK; | 53 return CKR_OK; |
| 56 } | 54 } |
| 57 | 55 |
| 58 static CK_RV | 56 static CK_RV |
| 59 mutex_remove_pointer | 57 mutex_remove_pointer( |
| 60 ( | 58 const NSSCKFWMutex *fwMutex) |
| 61 const NSSCKFWMutex *fwMutex | |
| 62 ) | |
| 63 { | 59 { |
| 64 return CKR_OK; | 60 return CKR_OK; |
| 65 } | 61 } |
| 66 | 62 |
| 67 NSS_IMPLEMENT CK_RV | 63 NSS_IMPLEMENT CK_RV |
| 68 nssCKFWMutex_verifyPointer | 64 nssCKFWMutex_verifyPointer( |
| 69 ( | 65 const NSSCKFWMutex *fwMutex) |
| 70 const NSSCKFWMutex *fwMutex | |
| 71 ) | |
| 72 { | 66 { |
| 73 return CKR_OK; | 67 return CKR_OK; |
| 74 } | 68 } |
| 75 | 69 |
| 76 #endif /* DEBUG */ | 70 #endif /* DEBUG */ |
| 77 | 71 |
| 78 /* | 72 /* |
| 79 * nssCKFWMutex_Create | 73 * nssCKFWMutex_Create |
| 80 * | 74 * |
| 81 */ | 75 */ |
| 82 NSS_EXTERN NSSCKFWMutex * | 76 NSS_EXTERN NSSCKFWMutex * |
| 83 nssCKFWMutex_Create | 77 nssCKFWMutex_Create( |
| 84 ( | 78 CK_C_INITIALIZE_ARGS_PTR pInitArgs, |
| 85 CK_C_INITIALIZE_ARGS_PTR pInitArgs, | 79 CryptokiLockingState LockingState, |
| 86 CryptokiLockingState LockingState, | 80 NSSArena *arena, |
| 87 NSSArena *arena, | 81 CK_RV *pError) |
| 88 CK_RV *pError | |
| 89 ) | |
| 90 { | 82 { |
| 91 NSSCKFWMutex *mutex; | 83 NSSCKFWMutex *mutex; |
| 92 | 84 |
| 93 mutex = nss_ZNEW(arena, NSSCKFWMutex); | 85 mutex = nss_ZNEW(arena, NSSCKFWMutex); |
| 94 if (!mutex) { | 86 if (!mutex) { |
| 95 *pError = CKR_HOST_MEMORY; | 87 *pError = CKR_HOST_MEMORY; |
| 96 return (NSSCKFWMutex *)NULL; | 88 return (NSSCKFWMutex *)NULL; |
| 97 } | |
| 98 *pError = CKR_OK; | |
| 99 mutex->lock = NULL; | |
| 100 if (LockingState == MultiThreaded) { | |
| 101 mutex->lock = PR_NewLock(); | |
| 102 if (!mutex->lock) { | |
| 103 *pError = CKR_HOST_MEMORY; /* we couldn't get the resource */ | |
| 104 } | 89 } |
| 105 } | 90 *pError = CKR_OK; |
| 106 | 91 mutex->lock = NULL; |
| 107 if( CKR_OK != *pError ) { | 92 if (LockingState == MultiThreaded) { |
| 108 (void)nss_ZFreeIf(mutex); | 93 mutex->lock = PR_NewLock(); |
| 109 return (NSSCKFWMutex *)NULL; | 94 if (!mutex->lock) { |
| 110 } | 95 *pError = CKR_HOST_MEMORY; /* we couldn't get the resource */ |
| 96 } |
| 97 } |
| 98 |
| 99 if (CKR_OK != *pError) { |
| 100 (void)nss_ZFreeIf(mutex); |
| 101 return (NSSCKFWMutex *)NULL; |
| 102 } |
| 111 | 103 |
| 112 #ifdef DEBUG | 104 #ifdef DEBUG |
| 113 *pError = mutex_add_pointer(mutex); | 105 *pError = mutex_add_pointer(mutex); |
| 114 if( CKR_OK != *pError ) { | 106 if (CKR_OK != *pError) { |
| 115 if (mutex->lock) { | 107 if (mutex->lock) { |
| 116 PR_DestroyLock(mutex->lock); | 108 PR_DestroyLock(mutex->lock); |
| 109 } |
| 110 (void)nss_ZFreeIf(mutex); |
| 111 return (NSSCKFWMutex *)NULL; |
| 117 } | 112 } |
| 118 (void)nss_ZFreeIf(mutex); | |
| 119 return (NSSCKFWMutex *)NULL; | |
| 120 } | |
| 121 #endif /* DEBUG */ | 113 #endif /* DEBUG */ |
| 122 | 114 |
| 123 return mutex; | 115 return mutex; |
| 124 } | 116 } |
| 125 | 117 |
| 126 /* | 118 /* |
| 127 * nssCKFWMutex_Destroy | 119 * nssCKFWMutex_Destroy |
| 128 * | 120 * |
| 129 */ | 121 */ |
| 130 NSS_EXTERN CK_RV | 122 NSS_EXTERN CK_RV |
| 131 nssCKFWMutex_Destroy | 123 nssCKFWMutex_Destroy( |
| 132 ( | 124 NSSCKFWMutex *mutex) |
| 133 NSSCKFWMutex *mutex | |
| 134 ) | |
| 135 { | 125 { |
| 136 CK_RV rv = CKR_OK; | 126 CK_RV rv = CKR_OK; |
| 137 | 127 |
| 138 #ifdef NSSDEBUG | 128 #ifdef NSSDEBUG |
| 139 rv = nssCKFWMutex_verifyPointer(mutex); | 129 rv = nssCKFWMutex_verifyPointer(mutex); |
| 140 if( CKR_OK != rv ) { | 130 if (CKR_OK != rv) { |
| 141 return rv; | 131 return rv; |
| 142 } | 132 } |
| 143 #endif /* NSSDEBUG */ | 133 #endif /* NSSDEBUG */ |
| 144 | 134 |
| 145 if (mutex->lock) { | 135 if (mutex->lock) { |
| 146 PR_DestroyLock(mutex->lock); | 136 PR_DestroyLock(mutex->lock); |
| 147 } | 137 } |
| 148 | 138 |
| 149 #ifdef DEBUG | 139 #ifdef DEBUG |
| 150 (void)mutex_remove_pointer(mutex); | 140 (void)mutex_remove_pointer(mutex); |
| 151 #endif /* DEBUG */ | 141 #endif /* DEBUG */ |
| 152 | 142 |
| 153 (void)nss_ZFreeIf(mutex); | 143 (void)nss_ZFreeIf(mutex); |
| 154 return rv; | 144 return rv; |
| 155 } | 145 } |
| 156 | 146 |
| 157 /* | 147 /* |
| 158 * nssCKFWMutex_Lock | 148 * nssCKFWMutex_Lock |
| 159 * | 149 * |
| 160 */ | 150 */ |
| 161 NSS_EXTERN CK_RV | 151 NSS_EXTERN CK_RV |
| 162 nssCKFWMutex_Lock | 152 nssCKFWMutex_Lock( |
| 163 ( | 153 NSSCKFWMutex *mutex) |
| 164 NSSCKFWMutex *mutex | |
| 165 ) | |
| 166 { | 154 { |
| 167 #ifdef NSSDEBUG | 155 #ifdef NSSDEBUG |
| 168 CK_RV rv = nssCKFWMutex_verifyPointer(mutex); | 156 CK_RV rv = nssCKFWMutex_verifyPointer(mutex); |
| 169 if( CKR_OK != rv ) { | 157 if (CKR_OK != rv) { |
| 170 return rv; | 158 return rv; |
| 171 } | 159 } |
| 172 #endif /* NSSDEBUG */ | 160 #endif /* NSSDEBUG */ |
| 173 if (mutex->lock) { | 161 if (mutex->lock) { |
| 174 PR_Lock(mutex->lock); | 162 PR_Lock(mutex->lock); |
| 175 } | 163 } |
| 176 | 164 |
| 177 return CKR_OK; | 165 return CKR_OK; |
| 178 } | 166 } |
| 179 | 167 |
| 180 /* | 168 /* |
| 181 * nssCKFWMutex_Unlock | 169 * nssCKFWMutex_Unlock |
| 182 * | 170 * |
| 183 */ | 171 */ |
| 184 NSS_EXTERN CK_RV | 172 NSS_EXTERN CK_RV |
| 185 nssCKFWMutex_Unlock | 173 nssCKFWMutex_Unlock( |
| 186 ( | 174 NSSCKFWMutex *mutex) |
| 187 NSSCKFWMutex *mutex | |
| 188 ) | |
| 189 { | 175 { |
| 190 PRStatus nrv; | 176 PRStatus nrv; |
| 191 #ifdef NSSDEBUG | 177 #ifdef NSSDEBUG |
| 192 CK_RV rv = nssCKFWMutex_verifyPointer(mutex); | 178 CK_RV rv = nssCKFWMutex_verifyPointer(mutex); |
| 193 | 179 |
| 194 if( CKR_OK != rv ) { | 180 if (CKR_OK != rv) { |
| 195 return rv; | 181 return rv; |
| 196 } | 182 } |
| 197 #endif /* NSSDEBUG */ | 183 #endif /* NSSDEBUG */ |
| 198 | 184 |
| 199 if (!mutex->lock) | 185 if (!mutex->lock) |
| 200 return CKR_OK; | 186 return CKR_OK; |
| 201 | 187 |
| 202 nrv = PR_Unlock(mutex->lock); | 188 nrv = PR_Unlock(mutex->lock); |
| 203 | 189 |
| 204 /* if unlock fails, either we have a programming error, or we have | 190 /* if unlock fails, either we have a programming error, or we have |
| 205 * some sort of hardware failure... in either case return CKR_DEVICE_ERROR. | 191 * some sort of hardware failure... in either case return CKR_DEVICE_ERROR. |
| 206 */ | 192 */ |
| 207 return nrv == PR_SUCCESS ? CKR_OK : CKR_DEVICE_ERROR; | 193 return nrv == PR_SUCCESS ? CKR_OK : CKR_DEVICE_ERROR; |
| 208 } | 194 } |
| 209 | 195 |
| 210 /* | 196 /* |
| 211 * NSSCKFWMutex_Destroy | 197 * NSSCKFWMutex_Destroy |
| 212 * | 198 * |
| 213 */ | 199 */ |
| 214 NSS_EXTERN CK_RV | 200 NSS_EXTERN CK_RV |
| 215 NSSCKFWMutex_Destroy | 201 NSSCKFWMutex_Destroy( |
| 216 ( | 202 NSSCKFWMutex *mutex) |
| 217 NSSCKFWMutex *mutex | |
| 218 ) | |
| 219 { | 203 { |
| 220 #ifdef DEBUG | 204 #ifdef DEBUG |
| 221 CK_RV rv = nssCKFWMutex_verifyPointer(mutex); | 205 CK_RV rv = nssCKFWMutex_verifyPointer(mutex); |
| 222 if( CKR_OK != rv ) { | 206 if (CKR_OK != rv) { |
| 223 return rv; | 207 return rv; |
| 224 } | 208 } |
| 225 #endif /* DEBUG */ | 209 #endif /* DEBUG */ |
| 226 | 210 |
| 227 return nssCKFWMutex_Destroy(mutex); | 211 return nssCKFWMutex_Destroy(mutex); |
| 228 } | 212 } |
| 229 | 213 |
| 230 /* | 214 /* |
| 231 * NSSCKFWMutex_Lock | 215 * NSSCKFWMutex_Lock |
| 232 * | 216 * |
| 233 */ | 217 */ |
| 234 NSS_EXTERN CK_RV | 218 NSS_EXTERN CK_RV |
| 235 NSSCKFWMutex_Lock | 219 NSSCKFWMutex_Lock( |
| 236 ( | 220 NSSCKFWMutex *mutex) |
| 237 NSSCKFWMutex *mutex | |
| 238 ) | |
| 239 { | 221 { |
| 240 #ifdef DEBUG | 222 #ifdef DEBUG |
| 241 CK_RV rv = nssCKFWMutex_verifyPointer(mutex); | 223 CK_RV rv = nssCKFWMutex_verifyPointer(mutex); |
| 242 if( CKR_OK != rv ) { | 224 if (CKR_OK != rv) { |
| 243 return rv; | 225 return rv; |
| 244 } | 226 } |
| 245 #endif /* DEBUG */ | 227 #endif /* DEBUG */ |
| 246 | 228 |
| 247 return nssCKFWMutex_Lock(mutex); | 229 return nssCKFWMutex_Lock(mutex); |
| 248 } | 230 } |
| 249 | 231 |
| 250 /* | 232 /* |
| 251 * NSSCKFWMutex_Unlock | 233 * NSSCKFWMutex_Unlock |
| 252 * | 234 * |
| 253 */ | 235 */ |
| 254 NSS_EXTERN CK_RV | 236 NSS_EXTERN CK_RV |
| 255 NSSCKFWMutex_Unlock | 237 NSSCKFWMutex_Unlock( |
| 256 ( | 238 NSSCKFWMutex *mutex) |
| 257 NSSCKFWMutex *mutex | |
| 258 ) | |
| 259 { | 239 { |
| 260 #ifdef DEBUG | 240 #ifdef DEBUG |
| 261 CK_RV rv = nssCKFWMutex_verifyPointer(mutex); | 241 CK_RV rv = nssCKFWMutex_verifyPointer(mutex); |
| 262 if( CKR_OK != rv ) { | 242 if (CKR_OK != rv) { |
| 263 return rv; | 243 return rv; |
| 264 } | 244 } |
| 265 #endif /* DEBUG */ | 245 #endif /* DEBUG */ |
| 266 | 246 |
| 267 return nssCKFWMutex_Unlock(mutex); | 247 return nssCKFWMutex_Unlock(mutex); |
| 268 } | 248 } |
| 269 | |
| OLD | NEW |