Diff

Differences From Artifact [de5cf6edd0]:

To Artifact [47e7747ccc]:


    43     43   #  ifdef HAVE_LIBZ
    44     44   #    include <zlib.h>
    45     45   #  endif
    46     46   #else
    47     47   #  ifdef HAVE_LIBZ
    48     48   #    undef HAVE_LIBZ
    49     49   #  endif
           50  +#endif
           51  +#ifndef _THREAD_EMULATION
           52  +#  ifdef HAVE_DLFCN_H
           53  +#    ifdef HAVE_DLSYM
           54  +#      include <dlfcn.h>
           55  +#      define HAVE_CACKEY_MUTEX_PTHREAD_FUNCS 1
           56  +#    endif
           57  +#  endif
    50     58   #endif
    51     59   #ifdef CACKEY_DEBUG_SEARCH_SPEEDTEST
    52     60   #  include <sys/time.h>
    53     61   #endif
    54     62   
    55     63   #define CK_PTR *
    56     64   #define CK_DEFINE_FUNCTION(returnType, name) returnType name
................................................................................
  3734   3742   		return(-1);
  3735   3743   	}
  3736   3744   #  endif
  3737   3745   #endif
  3738   3746   
  3739   3747   	return(x509_read_ret);
  3740   3748   }
         3749  +
         3750  +/* Dynamically load pthreads from the running application */
         3751  +#ifdef HAVE_CACKEY_MUTEX_PTHREAD_FUNCS
         3752  +#warning building with pthreads opened via dlsym
         3753  +struct cackey_mutex_pthread_funcs_st {
         3754  +	int (*pthread_mutex_init)(pthread_mutex_t *restrict mutex, const pthread_mutexattr_t *restrict attr);
         3755  +	int (*pthread_mutex_lock)(pthread_mutex_t *mutex);
         3756  +	int (*pthread_mutex_unlock)(pthread_mutex_t *mutex);
         3757  +};
         3758  +
         3759  +static struct cackey_mutex_pthread_funcs_st *cackey_mutex_pthread_funcs(void) {
         3760  +	static struct cackey_mutex_pthread_funcs_st funcs = {0};
         3761  +	static int init = 0;
         3762  +
         3763  +	if (init != 0) {
         3764  +		return(&funcs);
         3765  +	}
         3766  +
         3767  +	funcs.pthread_mutex_init = dlsym(RTLD_DEFAULT, "pthread_mutex_init");
         3768  +	funcs.pthread_mutex_lock = dlsym(RTLD_DEFAULT, "pthread_mutex_lock");
         3769  +	funcs.pthread_mutex_unlock = dlsym(RTLD_DEFAULT, "pthread_mutex_unlock");
         3770  +
         3771  +	init = 1;
         3772  +
         3773  +	return(&funcs);
         3774  +}
         3775  +#define cackey_pthread_mutex_init cackey_mutex_pthread_funcs()->pthread_mutex_init
         3776  +#define cackey_pthread_mutex_lock cackey_mutex_pthread_funcs()->pthread_mutex_lock
         3777  +#define cackey_pthread_mutex_unlock cackey_mutex_pthread_funcs()->pthread_mutex_unlock
         3778  +#else
         3779  +#define cackey_pthread_mutex_init pthread_mutex_init
         3780  +#define cackey_pthread_mutex_lock pthread_mutex_lock
         3781  +#define cackey_pthread_mutex_unlock pthread_mutex_unlock
         3782  +#endif
  3741   3783   
  3742   3784   /* Returns 0 on success */
  3743   3785   static int cackey_mutex_create(void **mutex) {
  3744   3786   	pthread_mutex_t *pthread_mutex;
  3745   3787   	int pthread_retval;
  3746   3788   	CK_RV custom_retval;
  3747   3789   
................................................................................
  3751   3793   		pthread_mutex = malloc(sizeof(*pthread_mutex));
  3752   3794   		if (!pthread_mutex) {
  3753   3795   			CACKEY_DEBUG_PRINTF("Failed to allocate memory.");
  3754   3796   
  3755   3797   			return(-1);
  3756   3798   		}
  3757   3799   
  3758         -		pthread_retval = pthread_mutex_init(pthread_mutex, NULL);
         3800  +		pthread_retval = cackey_pthread_mutex_init(pthread_mutex, NULL);
  3759   3801   		if (pthread_retval != 0) {
  3760   3802   			CACKEY_DEBUG_PRINTF("pthread_mutex_init() returned error (%i).", pthread_retval);
  3761   3803   
  3762   3804   			return(-1);
  3763   3805   		}
  3764   3806   
  3765   3807   		*mutex = pthread_mutex;
................................................................................
  3787   3829   	CK_RV custom_retval;
  3788   3830   
  3789   3831   	CACKEY_DEBUG_PRINTF("Called.");
  3790   3832   
  3791   3833   	if ((cackey_args.flags & CKF_OS_LOCKING_OK) == CKF_OS_LOCKING_OK) {
  3792   3834   		pthread_mutex = mutex;
  3793   3835   
  3794         -		pthread_retval = pthread_mutex_lock(pthread_mutex);
         3836  +		pthread_retval = cackey_pthread_mutex_lock(pthread_mutex);
  3795   3837   		if (pthread_retval != 0) {
  3796   3838   			CACKEY_DEBUG_PRINTF("pthread_mutex_lock() returned error (%i).", pthread_retval);
  3797   3839   
  3798   3840   			return(-1);
  3799   3841   		}
  3800   3842   	} else {
  3801   3843   		if (cackey_args.LockMutex) {
................................................................................
  3821   3863   	CK_RV custom_retval;
  3822   3864   
  3823   3865   	CACKEY_DEBUG_PRINTF("Called.");
  3824   3866   
  3825   3867   	if ((cackey_args.flags & CKF_OS_LOCKING_OK) == CKF_OS_LOCKING_OK) {
  3826   3868   		pthread_mutex = mutex;
  3827   3869   
  3828         -		pthread_retval = pthread_mutex_unlock(pthread_mutex);
         3870  +		pthread_retval = cackey_pthread_mutex_unlock(pthread_mutex);
  3829   3871   		if (pthread_retval != 0) {
  3830   3872   			CACKEY_DEBUG_PRINTF("pthread_mutex_unlock() returned error (%i).", pthread_retval);
  3831   3873   
  3832   3874   			return(-1);
  3833   3875   		}
  3834   3876   	} else {
  3835   3877   		if (cackey_args.UnlockMutex) {
................................................................................
  4614   4656   
  4615   4657   		if (args->CreateMutex == NULL || args->DestroyMutex == NULL || args->LockMutex == NULL || args->UnlockMutex == NULL) {
  4616   4658   			if (args->CreateMutex != NULL || args->DestroyMutex != NULL || args->LockMutex != NULL || args->UnlockMutex != NULL) {
  4617   4659   				CACKEY_DEBUG_PRINTF("Error. Some, but not All threading primitives provided.");
  4618   4660   
  4619   4661   				return(CKR_ARGUMENTS_BAD);
  4620   4662   			}
         4663  +		} else {
         4664  +#ifdef HAVE_CACKEY_MUTEX_PTHREAD_FUNCS
         4665  +			if (cackey_mutex_pthread_funcs()->pthread_mutex_init == NULL || cackey_mutex_pthread_funcs()->pthread_mutex_lock == NULL || cackey_mutex_pthread_funcs()->pthread_mutex_unlock == NULL) {
         4666  +				CACKEY_DEBUG_PRINTF("Error. Library is not linked to pthreads and we are unable to find them at runtime.");
         4667  +
         4668  +				return(CKR_GENERAL_ERROR);
         4669  +			}
         4670  +#endif
  4621   4671   		}
  4622   4672   	} else {
  4623   4673   		cackey_args.CreateMutex = NULL;
  4624   4674   		cackey_args.DestroyMutex = NULL;
  4625   4675   		cackey_args.LockMutex = NULL;
  4626   4676   		cackey_args.UnlockMutex = NULL;
  4627   4677   		cackey_args.flags = 0;