| 1 |
/* |
| 2 |
* Copyright (c) 2008-2010 .SE (The Internet Infrastructure Foundation). |
| 3 |
* Copyright (c) 2010 SURFnet bv |
| 4 |
* All rights reserved. |
| 5 |
* |
| 6 |
* Redistribution and use in source and binary forms, with or without |
| 7 |
* modification, are permitted provided that the following conditions |
| 8 |
* are met: |
| 9 |
* 1. Redistributions of source code must retain the above copyright |
| 10 |
* notice, this list of conditions and the following disclaimer. |
| 11 |
* 2. Redistributions in binary form must reproduce the above copyright |
| 12 |
* notice, this list of conditions and the following disclaimer in the |
| 13 |
* documentation and/or other materials provided with the distribution. |
| 14 |
* |
| 15 |
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR |
| 16 |
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED |
| 17 |
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
| 18 |
* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY |
| 19 |
* DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
| 20 |
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE |
| 21 |
* GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
| 22 |
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER |
| 23 |
* IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR |
| 24 |
* OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN |
| 25 |
* IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 26 |
*/ |
| 27 |
|
| 28 |
/***************************************************************************** |
| 29 |
osmutex.cpp |
| 30 |
|
| 31 |
Contains OS-specific implementations of intraprocess mutex functions. This |
| 32 |
implementation is based on SoftHSM v1 |
| 33 |
*****************************************************************************/ |
| 34 |
|
| 35 |
#include "config.h" |
| 36 |
#include "log.h" |
| 37 |
#include "osmutex.h" |
| 38 |
|
| 39 |
#ifdef HAVE_PTHREAD_H |
| 40 |
|
| 41 |
#include <stdlib.h> |
| 42 |
#include <pthread.h> |
| 43 |
|
| 44 |
CK_RV OSCreateMutex(CK_VOID_PTR_PTR newMutex) |
| 45 |
{ |
| 46 |
int rv; |
| 47 |
|
| 48 |
/* Allocate memory */ |
| 49 |
pthread_mutex_t* pthreadMutex = (pthread_mutex_t*) malloc(sizeof(pthread_mutex_t)); |
| 50 |
|
| 51 |
if (pthreadMutex == NULL) |
| 52 |
{ |
| 53 |
ERROR_MSG("Failed to allocate memory for a new mutex"); |
| 54 |
|
| 55 |
return CKR_HOST_MEMORY; |
| 56 |
} |
| 57 |
|
| 58 |
/* Initialise the mutex */ |
| 59 |
if ((rv = pthread_mutex_init(pthreadMutex, NULL)) != 0) |
| 60 |
{ |
| 61 |
free(pthreadMutex); |
| 62 |
|
| 63 |
ERROR_MSG("Failed to initialise POSIX mutex (0x%08X)", rv); |
| 64 |
|
| 65 |
return CKR_GENERAL_ERROR; |
| 66 |
} |
| 67 |
|
| 68 |
*newMutex = pthreadMutex; |
| 69 |
|
| 70 |
return CKR_OK; |
| 71 |
} |
| 72 |
|
| 73 |
CK_RV OSDestroyMutex(CK_VOID_PTR mutex) |
| 74 |
{ |
| 75 |
int rv; |
| 76 |
pthread_mutex_t* pthreadMutex = (pthread_mutex_t*) mutex; |
| 77 |
|
| 78 |
if (pthreadMutex == NULL) |
| 79 |
{ |
| 80 |
ERROR_MSG("Cannot destroy NULL mutex"); |
| 81 |
|
| 82 |
return CKR_ARGUMENTS_BAD; |
| 83 |
} |
| 84 |
|
| 85 |
if ((rv = pthread_mutex_destroy(pthreadMutex)) != 0) |
| 86 |
{ |
| 87 |
ERROR_MSG("Failed to destroy POSIX mutex (0x%08X)", rv); |
| 88 |
|
| 89 |
return CKR_GENERAL_ERROR; |
| 90 |
} |
| 91 |
|
| 92 |
free(pthreadMutex); |
| 93 |
|
| 94 |
return CKR_OK; |
| 95 |
} |
| 96 |
|
| 97 |
CK_RV OSLockMutex(CK_VOID_PTR mutex) |
| 98 |
{ |
| 99 |
int rv; |
| 100 |
pthread_mutex_t* pthreadMutex = (pthread_mutex_t*) mutex; |
| 101 |
|
| 102 |
if (pthreadMutex == NULL) |
| 103 |
{ |
| 104 |
ERROR_MSG("Cannot lock NULL mutex"); |
| 105 |
|
| 106 |
return CKR_ARGUMENTS_BAD; |
| 107 |
} |
| 108 |
|
| 109 |
if ((rv = pthread_mutex_lock(pthreadMutex)) != 0) |
| 110 |
{ |
| 111 |
ERROR_MSG("Failed to lock POSIX mutex 0x%08X (0x%08X)", pthreadMutex, rv); |
| 112 |
|
| 113 |
return CKR_GENERAL_ERROR; |
| 114 |
} |
| 115 |
|
| 116 |
return CKR_OK; |
| 117 |
} |
| 118 |
|
| 119 |
CK_RV OSUnlockMutex(CK_VOID_PTR mutex) |
| 120 |
{ |
| 121 |
int rv; |
| 122 |
pthread_mutex_t* pthreadMutex = (pthread_mutex_t*) mutex; |
| 123 |
|
| 124 |
if (pthreadMutex == NULL) |
| 125 |
{ |
| 126 |
ERROR_MSG("Cannot unlock NULL mutex"); |
| 127 |
|
| 128 |
return CKR_ARGUMENTS_BAD; |
| 129 |
} |
| 130 |
|
| 131 |
if ((rv = pthread_mutex_unlock(pthreadMutex)) != 0) |
| 132 |
{ |
| 133 |
ERROR_MSG("Failed to unlock POSIX mutex 0x%08X (0x%08X)", pthreadMutex, rv); |
| 134 |
|
| 135 |
return CKR_GENERAL_ERROR; |
| 136 |
} |
| 137 |
|
| 138 |
return CKR_OK; |
| 139 |
} |
| 140 |
|
| 141 |
#elif _WIN32 |
| 142 |
|
| 143 |
CK_RV OSCreateMutex(CK_VOID_PTR_PTR newMutex) |
| 144 |
{ |
| 145 |
HANDLE hMutex; |
| 146 |
|
| 147 |
hMutex = CreateMutex(NULL, FALSE, NULL); |
| 148 |
if (hMutex == NULL) |
| 149 |
{ |
| 150 |
DWORD rv = GetLastError(); |
| 151 |
|
| 152 |
ERROR_MSG("Failed to initialise WIN32 mutex (0x%08X)", rv); |
| 153 |
|
| 154 |
return CKR_GENERAL_ERROR; |
| 155 |
} |
| 156 |
|
| 157 |
*newMutex = hMutex; |
| 158 |
|
| 159 |
return CKR_OK; |
| 160 |
} |
| 161 |
|
| 162 |
CK_RV OSDestroyMutex(CK_VOID_PTR mutex) |
| 163 |
{ |
| 164 |
HANDLE hMutex = (HANDLE) mutex; |
| 165 |
|
| 166 |
if (hMutex == NULL) |
| 167 |
{ |
| 168 |
ERROR_MSG("Cannot destroy NULL mutex"); |
| 169 |
|
| 170 |
return CKR_ARGUMENTS_BAD; |
| 171 |
} |
| 172 |
|
| 173 |
if (CloseHandle(hMutex) == 0) |
| 174 |
{ |
| 175 |
DWORD rv = GetLastError(); |
| 176 |
|
| 177 |
ERROR_MSG("Failed to destroy WIN32 mutex (0x%08X)", rv); |
| 178 |
|
| 179 |
return CKR_GENERAL_ERROR; |
| 180 |
} |
| 181 |
|
| 182 |
return CKR_OK; |
| 183 |
} |
| 184 |
|
| 185 |
CK_RV OSLockMutex(CK_VOID_PTR mutex) |
| 186 |
{ |
| 187 |
DWORD rv; |
| 188 |
HANDLE hMutex = (HANDLE) mutex; |
| 189 |
|
| 190 |
if (hMutex == NULL) |
| 191 |
{ |
| 192 |
ERROR_MSG("Cannot lock NULL mutex"); |
| 193 |
|
| 194 |
return CKR_ARGUMENTS_BAD; |
| 195 |
} |
| 196 |
|
| 197 |
rv = WaitForSingleObject(hMutex, INFINITE); |
| 198 |
if (rv != WAIT_OBJECT_0) |
| 199 |
{ |
| 200 |
// WAIT_ABANDONED 0x00000080 |
| 201 |
// WAIT_OBJECT_0 0x00000000 |
| 202 |
// WAIT_TIMEOUT 0x00000102 |
| 203 |
// WAIT_FAILED 0xFFFFFFFF |
| 204 |
|
| 205 |
if (rv == WAIT_FAILED) |
| 206 |
rv = GetLastError(); |
| 207 |
|
| 208 |
ERROR_MSG("Failed to lock WIN32 mutex 0x%08X (0x%08X)", hMutex, rv); |
| 209 |
|
| 210 |
return CKR_GENERAL_ERROR; |
| 211 |
} |
| 212 |
|
| 213 |
return CKR_OK; |
| 214 |
} |
| 215 |
|
| 216 |
CK_RV OSUnlockMutex(CK_VOID_PTR mutex) |
| 217 |
{ |
| 218 |
HANDLE hMutex = (HANDLE) mutex; |
| 219 |
|
| 220 |
if (hMutex == NULL) |
| 221 |
{ |
| 222 |
ERROR_MSG("Cannot unlock NULL mutex"); |
| 223 |
|
| 224 |
return CKR_ARGUMENTS_BAD; |
| 225 |
} |
| 226 |
|
| 227 |
if (ReleaseMutex(hMutex) == 0) |
| 228 |
{ |
| 229 |
DWORD rv = GetLastError(); |
| 230 |
|
| 231 |
ERROR_MSG("Failed to unlock WIN32 mutex 0x%08X (0x%08X)", hMutex, rv); |
| 232 |
|
| 233 |
return CKR_GENERAL_ERROR; |
| 234 |
} |
| 235 |
|
| 236 |
return CKR_OK; |
| 237 |
} |
| 238 |
|
| 239 |
#else |
| 240 |
#error "There are no mutex implementations for your operating system yet" |
| 241 |
#endif |
| 242 |
|