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