From d3fa2085bf9169eab3430ff903f75b7487c7ee82 Mon Sep 17 00:00:00 2001 From: Stef Walter Date: Sat, 6 Dec 2008 00:22:35 +0000 Subject: Build on Win32 and create VCE project. --- src/module.c | 219 +++++++++++++++++++++++++++++++++++++++++++++++------------ 1 file changed, 174 insertions(+), 45 deletions(-) (limited to 'src/module.c') diff --git a/src/module.c b/src/module.c index e47ba38..cda07a2 100644 --- a/src/module.c +++ b/src/module.c @@ -1,15 +1,27 @@ -#include "config.h" +#ifdef _WIN32 -#include "p11-tests.h" +#define WIN32_LEAN_AND_MEAN +#define _WIN32_WINNT 0x400 +#include + +static HMODULE module = NULL; + +#else /* !_WIN32 */ #include -#include #include + +static void *module = NULL; + +#endif /* !_WIN32 */ + +#include "p11-tests.h" + +#include #include #include -static void *module = NULL; CK_FUNCTION_LIST_PTR p11t_module_funcs = NULL; static const char *init_string = NULL; @@ -27,127 +39,221 @@ p11t_module_config(const char *name, const char *value) static CK_RV create_mutex(void **mutex) { - pthread_mutex_t *mut; - if(!mutex) { p11t_msg_print("CreateMutex: null mutex passed"); return CKR_ARGUMENTS_BAD; } - mut = malloc(sizeof(pthread_mutex_t)); - assert(mut); +#ifdef _WIN32 + + *mutex = CreateMutex(NULL, FALSE, NULL); + assert(*mutex != NULL); + +#else /* !_WIN32 */ + + { + pthread_mutex_t *mut; + mut = malloc(sizeof(pthread_mutex_t)); + assert(mut); + + if(pthread_mutex_init(mut, NULL) != 0) + assert(0); + *mutex = mut; + } + +#endif /* !_WIN32 */ - if(pthread_mutex_init(mut, NULL) != 0) - assert(0); - *mutex = mut; return CKR_OK; } static CK_RV destroy_mutex (void *mutex) { - int res; - if(!mutex) { p11t_msg_print("DestroyMutex: null mutex"); return CKR_MUTEX_BAD; } - res = pthread_mutex_destroy(mutex); - if(res != 0) +#ifdef _WIN32 + + if(!CloseHandle((HANDLE)mutex)) { - if(res == EBUSY) - { - p11t_msg_print("DestroyMutex: mutex is locked"); - return CKR_GENERAL_ERROR; - } - else if(res == EINVAL) + DWORD error = GetLastError(); + if(error == ERROR_INVALID_HANDLE) { p11t_msg_print("DestroyMutex: mutex is invalid"); return CKR_MUTEX_BAD; } else { - p11t_msg_print("DestroyMutex: failed: %d", res); + p11t_msg_print("DestroyMutex: failed: %d", GetLastError()); return CKR_GENERAL_ERROR; + } + } + +#else /* !_WIN32 */ + + { + int res = pthread_mutex_destroy(mutex); + if(res != 0) + { + if(res == EBUSY) + { + p11t_msg_print("DestroyMutex: mutex is locked"); + return CKR_GENERAL_ERROR; + } + else if(res == EINVAL) + { + p11t_msg_print("DestroyMutex: mutex is invalid"); + return CKR_MUTEX_BAD; + } + else + { + p11t_msg_print("DestroyMutex: failed: %d", res); + return CKR_GENERAL_ERROR; + + } } } +#endif /* !_WIN32 */ + return CKR_OK; } static CK_RV lock_mutex (void *mutex) { - int res; - if (!mutex) + if(!mutex) { p11t_msg_print("LockMutex: null mutex"); return CKR_MUTEX_BAD; } - res = pthread_mutex_lock(mutex); - if(res != 0) +#ifdef _WIN32 + { - if(res == EDEADLK) + DWORD result = WaitForSingleObject((HANDLE)mutex, INFINITE); + if(result == WAIT_ABANDONED) { - p11t_msg_print("LockMutex: would deadlock"); + p11t_msg_print("LockMutex: thread exited without releasing mutex"); return CKR_CANT_LOCK; } - else if(res == EINVAL) + else if(result == WAIT_FAILED) { - p11t_msg_print("LockMutex: invalid mutex used"); - return CKR_MUTEX_BAD; + DWORD error = GetLastError(); + if(error == ERROR_INVALID_HANDLE) + { + p11t_msg_print("LockMutex: invalid handle"); + return CKR_MUTEX_BAD; + } + else + { + p11t_msg_print("LockMutex: failed: %d", error); + return CKR_GENERAL_ERROR; + } } - else + else if(result != WAIT_OBJECT_0) { - p11t_msg_print("LockMutex: failed: %d", res); - return CKR_GENERAL_ERROR; + p11t_msg_print("LockMutex: couldn't lock"); + return CKR_CANT_LOCK; + } + } +#else /* !_WIN32 */ + + int res = pthread_mutex_lock(mutex); + if(res != 0) + { + if(res == EDEADLK) + { + p11t_msg_print("LockMutex: would deadlock"); + return CKR_CANT_LOCK; + } + else if(res == EINVAL) + { + p11t_msg_print("LockMutex: invalid mutex used"); + return CKR_MUTEX_BAD; + } + else + { + p11t_msg_print("LockMutex: failed: %d", res); + return CKR_GENERAL_ERROR; + + } } } +#endif /* !_WIN32 */ + return CKR_OK; } static CK_RV unlock_mutex (void *mutex) { - int res; - if (!mutex) + if(!mutex) { p11t_msg_print("UnlockMutex: null mutex"); return CKR_MUTEX_BAD; } - res = pthread_mutex_lock(mutex); - if(res != 0) +#ifdef _WIN32 + + if(!ReleaseMutex((HANDLE)mutex)) { - if(res == EPERM) + DWORD error = GetLastError(); + if(error == ERROR_NOT_OWNER) { p11t_msg_print("UnlockMutex: mutex not locked"); return CKR_MUTEX_NOT_LOCKED; } - else if(res == EINVAL) + else if(error == ERROR_INVALID_HANDLE) { p11t_msg_print("UnlockMutex: invalid mutex used"); return CKR_MUTEX_BAD; } else { - p11t_msg_print("UnlockMutex: failed: %d", res); + p11t_msg_print("UnlockMutex: failed: %d", error); return CKR_GENERAL_ERROR; - } } - else + +#else /* !_WIN32 */ + { - free(mutex); + int res = pthread_mutex_lock(mutex); + if(res != 0) + { + if(res == EPERM) + { + p11t_msg_print("UnlockMutex: mutex not locked"); + return CKR_MUTEX_NOT_LOCKED; + } + else if(res == EINVAL) + { + p11t_msg_print("UnlockMutex: invalid mutex used"); + return CKR_MUTEX_BAD; + } + else + { + p11t_msg_print("UnlockMutex: failed: %d", res); + return CKR_GENERAL_ERROR; + + } + } + else + { + free(mutex); + } } +#endif /* !_WIN32 */ + return CKR_OK; } @@ -158,6 +264,20 @@ p11t_module_load(const char *filename) CK_C_GetFunctionList get_function_list; CK_RV rv; +#ifdef _WIN32 + + module = LoadLibrary(filename); + if(!module) + p11t_msg_fatal("couldn't load library: %s: %s", filename, p11t_msg_lasterr()); + + /* Lookup the appropriate function in the library */ + get_function_list = (CK_C_GetFunctionList)GetProcAddress(module, "C_GetFunctionList"); + if(!get_function_list) + p11t_msg_fatal("C_GetFunctionList: couldn't find function in library: %s: %s", + filename, p11t_msg_lasterr()); + +#else /* !_WIN32 */ + module = dlopen(filename, RTLD_NOW); if(!module) p11t_msg_fatal("couldn't open library: %s: %s", filename, dlerror()); @@ -165,7 +285,10 @@ p11t_module_load(const char *filename) /* Lookup the appropriate function in library */ get_function_list = (CK_C_GetFunctionList)dlsym (module, "C_GetFunctionList"); if(!get_function_list) - p11t_msg_fatal("C_GetFunctionList: couldn't find function in library: %s: %s", filename, dlerror()); + p11t_msg_fatal("C_GetFunctionList: couldn't find function in library: %s: %s", + filename, dlerror()); + +#endif /* !_WIN32 */ /* Get the function list */ rv = (get_function_list)(&p11t_module_funcs); @@ -345,6 +468,12 @@ void p11t_module_unload(void) { if(module) + { +#ifdef _WIN32 + FreeLibrary(module); +#else dlclose(module); +#endif + } module = NULL; } -- cgit v1.2.3