configurable OS thread locking

This commit is contained in:
Michał Trojnara 2015-12-01 09:19:03 +01:00
parent a454dd7fc9
commit fa9da7ad01
2 changed files with 42 additions and 7 deletions

View File

@ -142,6 +142,13 @@ AC_ARG_ENABLE(
[enable_pedantic="no"] [enable_pedantic="no"]
) )
AC_ARG_ENABLE(
[thread_locking],
[AS_HELP_STRING([--disable-thread-locking],[disable OS thread locking @<:@enabled@:>@])],
,
[enable_thread_locking="yes"]
)
AC_ARG_ENABLE( AC_ARG_ENABLE(
[zlib], [zlib],
[AS_HELP_STRING([--enable-zlib],[enable zlib linkage @<:@detect@:>@])], [AS_HELP_STRING([--enable-zlib],[enable zlib linkage @<:@detect@:>@])],
@ -369,6 +376,10 @@ if test "${WIN32}" = "no"; then
CC="${PTHREAD_CC}" CC="${PTHREAD_CC}"
fi fi
if test "${enable_thread_locking}" = "yes"; then
CPPFLAGS="${CPPFLAGS} -DPKCS11_THREAD_LOCKING"
fi
if test "${enable_minidriver}" = "yes"; then if test "${enable_minidriver}" = "yes"; then
dnl win32 special test for minidriver dnl win32 special test for minidriver
AC_CHECK_HEADER( AC_CHECK_HEADER(
@ -638,6 +649,9 @@ if test "${enable_man}" = "yes" -o "${enable_doc}" = "yes"; then
fi fi
OPENSC_FEATURES="" OPENSC_FEATURES=""
if test "${enable_thread_locking}" = "yes"; then
OPENSC_FEATURES="${OPENSC_FEATURES} locking"
fi
if test "${enable_zlib}" = "yes"; then if test "${enable_zlib}" = "yes"; then
OPENSC_FEATURES="${OPENSC_FEATURES} zlib" OPENSC_FEATURES="${OPENSC_FEATURES} zlib"
OPTIONAL_ZLIB_CFLAGS="${ZLIB_CFLAGS}" OPTIONAL_ZLIB_CFLAGS="${ZLIB_CFLAGS}"
@ -719,6 +733,7 @@ AC_SUBST([PROFILE_DIR])
AC_SUBST([PROFILE_DIR_DEFAULT]) AC_SUBST([PROFILE_DIR_DEFAULT])
AM_CONDITIONAL([ENABLE_MAN], [test "${enable_man}" = "yes"]) AM_CONDITIONAL([ENABLE_MAN], [test "${enable_man}" = "yes"])
AM_CONDITIONAL([ENABLE_THREAD_LOCKING], [test "${enable_thread_locking}" = "yes"])
AM_CONDITIONAL([ENABLE_ZLIB], [test "${enable_zlib}" = "yes"]) AM_CONDITIONAL([ENABLE_ZLIB], [test "${enable_zlib}" = "yes"])
AM_CONDITIONAL([ENABLE_READLINE], [test "${enable_readline}" = "yes"]) AM_CONDITIONAL([ENABLE_READLINE], [test "${enable_readline}" = "yes"])
AM_CONDITIONAL([ENABLE_OPENSSL], [test "${enable_openssl}" = "yes"]) AM_CONDITIONAL([ENABLE_OPENSSL], [test "${enable_openssl}" = "yes"])
@ -805,6 +820,7 @@ XSL stylesheets: ${xslstylesheetsdir}
man support: ${enable_man} man support: ${enable_man}
doc support: ${enable_doc} doc support: ${enable_doc}
thread locking support: ${enable_thread_locking}
zlib support: ${enable_zlib} zlib support: ${enable_zlib}
readline support: ${enable_readline} readline support: ${enable_readline}
OpenSSL support: ${enable_openssl} OpenSSL support: ${enable_openssl}

View File

@ -26,6 +26,14 @@
#include <sys/time.h> #include <sys/time.h>
#endif #endif
#ifdef PKCS11_THREAD_LOCKING
#if defined(HAVE_PTHREAD)
#include <pthread.h>
#elif defined(_WIN32)
#include <windows.h>
#endif
#endif /* PKCS11_THREAD_LOCKING */
#include "sc-pkcs11.h" #include "sc-pkcs11.h"
#ifndef MODULE_APP_NAME #ifndef MODULE_APP_NAME
@ -42,11 +50,15 @@ pid_t initialized_pid = (pid_t)-1;
static int in_finalize = 0; static int in_finalize = 0;
extern CK_FUNCTION_LIST pkcs11_function_list; extern CK_FUNCTION_LIST pkcs11_function_list;
#if defined(HAVE_PTHREAD) && defined(PKCS11_THREAD_LOCKING) #ifdef PKCS11_THREAD_LOCKING
#include <pthread.h>
#if defined(HAVE_PTHREAD)
CK_RV mutex_create(void **mutex) CK_RV mutex_create(void **mutex)
{ {
pthread_mutex_t *m = calloc(1, sizeof(*mutex)); pthread_mutex_t *m;
m = calloc(1, sizeof(*m));
if (m == NULL) if (m == NULL)
return CKR_GENERAL_ERROR;; return CKR_GENERAL_ERROR;;
pthread_mutex_init(m, NULL); pthread_mutex_init(m, NULL);
@ -79,7 +91,10 @@ CK_RV mutex_destroy(void *p)
static CK_C_INITIALIZE_ARGS _def_locks = { static CK_C_INITIALIZE_ARGS _def_locks = {
mutex_create, mutex_destroy, mutex_lock, mutex_unlock, 0, NULL }; mutex_create, mutex_destroy, mutex_lock, mutex_unlock, 0, NULL };
#elif defined(_WIN32) && defined (PKCS11_THREAD_LOCKING) #define HAVE_OS_LOCKING
#elif defined(_WIN32)
CK_RV mutex_create(void **mutex) CK_RV mutex_create(void **mutex)
{ {
CRITICAL_SECTION *m; CRITICAL_SECTION *m;
@ -112,14 +127,18 @@ CK_RV mutex_destroy(void *p)
free(p); free(p);
return CKR_OK; return CKR_OK;
} }
static CK_C_INITIALIZE_ARGS _def_locks = { static CK_C_INITIALIZE_ARGS _def_locks = {
mutex_create, mutex_destroy, mutex_lock, mutex_unlock, 0, NULL }; mutex_create, mutex_destroy, mutex_lock, mutex_unlock, 0, NULL };
#define HAVE_OS_LOCKING
#endif #endif
#endif /* PKCS11_THREAD_LOCKING */
static CK_C_INITIALIZE_ARGS_PTR global_locking; static CK_C_INITIALIZE_ARGS_PTR global_locking;
static void * global_lock = NULL; static void *global_lock = NULL;
#if (defined(HAVE_PTHREAD) || defined(_WIN32)) && defined(PKCS11_THREAD_LOCKING) #ifdef HAVE_OS_LOCKING
#define HAVE_OS_LOCKING
static CK_C_INITIALIZE_ARGS_PTR default_mutex_funcs = &_def_locks; static CK_C_INITIALIZE_ARGS_PTR default_mutex_funcs = &_def_locks;
#else #else
static CK_C_INITIALIZE_ARGS_PTR default_mutex_funcs = NULL; static CK_C_INITIALIZE_ARGS_PTR default_mutex_funcs = NULL;