Diff

Differences From Artifact [98f3aad3ae]:

To Artifact [8cdc000c32]:


     1      1   #define _GNU_SOURCE
     2      2   #include <dlfcn.h>
     3      3   
     4      4   #include "config.h"
     5      5   
     6         -#include <stdlib.h>
     7      6   #include <string.h>
     8      7   #include <stdio.h>
            8  +#include <time.h>
     9      9   
    10     10   #define CK_PTR *
    11     11   #define CK_DEFINE_FUNCTION(returnType, name) returnType name
    12     12   #define CK_DECLARE_FUNCTION(returnType, name) returnType name
    13     13   #define CK_DECLARE_FUNCTION_POINTER(returnType, name) returnType (* name)
    14     14   #define CK_CALLBACK_FUNCTION(returnType, name) returnType (* name)
    15     15   #ifndef NULL_PTR
................................................................................
    18     18   
    19     19   #include "./pkcs11/pkcs11.h"
    20     20   
    21     21   #ifndef CACKEY_LIBRARY_FILE
    22     22   #define CACKEY_LIBRARY_FILE "libcackey_g.so"
    23     23   #endif
    24     24   
           25  +void abort(void);
           26  +void free(void *ptr);
           27  +
    25     28   static void *libcackey_wrap_handle = NULL_PTR;
    26     29   
    27         -static void libcackey_wrap_init(void);
    28         -
    29         -static CK_RV libcackey_wrap_createmutex(CK_VOID_PTR_PTR ppMutex) {
    30         -	int (*create_mutex)(pthread_mutex_t *restrict mutex, const pthread_mutexattr_t *restrict attr);
    31         -	pthread_mutex_t *pthread_mutex;
    32         -	int pthread_retval;
    33         -
    34         -	libcackey_wrap_init();
    35         -
    36         -	pthread_mutex = malloc(sizeof(*pthread_mutex));
    37         -
    38         -	create_mutex = dlsym(libcackey_wrap_handle, "pthread_mutex_init");
    39         -	pthread_retval = create_mutex(pthread_mutex, NULL);
    40         -	if (pthread_retval != 0) {
    41         -		return(CKR_GENERAL_ERROR);
           30  +typedef enum {
           31  +	LIBCACKEY_WRAP_MUTEX_UNINIT = 0,
           32  +	LIBCACKEY_WRAP_MUTEX_INIT,
           33  +	LIBCACKEY_WRAP_MUTEX_UNLOCKED,
           34  +	LIBCACKEY_WRAP_MUTEX_LOCKED,
           35  +} libcackey_wrap_mutexes_states_t;
           36  +static libcackey_wrap_mutexes_states_t libcackey_wrap_mutexes[16] = {LIBCACKEY_WRAP_MUTEX_UNINIT};
           37  +
           38  +typedef libcackey_wrap_mutexes_states_t *pthread_mutex_t;
           39  +typedef int pthread_mutexattr_t;
           40  +
           41  +int pthread_mutex_init(pthread_mutex_t *restrict mutex, const pthread_mutexattr_t *restrict attr) {
           42  +	libcackey_wrap_mutexes_states_t *mutex_;
           43  +	int idx;
           44  +
           45  +	for (idx = 0; idx < (sizeof(libcackey_wrap_mutexes) / sizeof(libcackey_wrap_mutexes[0])); idx++) {
           46  +		if (libcackey_wrap_mutexes[idx] == LIBCACKEY_WRAP_MUTEX_UNINIT) {
           47  +			libcackey_wrap_mutexes[idx] = LIBCACKEY_WRAP_MUTEX_INIT;
           48  +			mutex_ = &libcackey_wrap_mutexes[idx];
           49  +			break;
           50  +		}
           51  +	}
           52  +
           53  +	if (!mutex_) {
           54  +		return(1);
           55  +	}
           56  +
           57  +	*mutex = mutex_;
           58  +
           59  +	return(0);
           60  +}
           61  +
           62  +int pthread_mutex_destroy(pthread_mutex_t *mutex) {
           63  +	**mutex = LIBCACKEY_WRAP_MUTEX_UNINIT;
           64  +
           65  +	return(0);
           66  +}
           67  +
           68  +int pthread_mutex_lock(pthread_mutex_t *mutex) {
           69  +	struct timespec sleeptime;
           70  +
           71  +	while (**mutex == LIBCACKEY_WRAP_MUTEX_LOCKED) {
           72  +		sleeptime.tv_sec = 0;
           73  +		sleeptime.tv_nsec = 1000000;
           74  +		nanosleep(&sleeptime, NULL);
           75  +fprintf(stderr, "mutex %p is locked\n", mutex); fflush(stderr);
           76  +		/* Do nothing */
    42     77   	}
    43     78   
    44         -	*ppMutex = pthread_mutex;
           79  +fprintf(stderr, "locking mutex %p\n", mutex); fflush(stderr);
           80  +	**mutex = LIBCACKEY_WRAP_MUTEX_LOCKED;
           81  +fprintf(stderr, "locked mutex %p\n", mutex); fflush(stderr);
    45     82   
    46     83   	return(CKR_OK);
    47     84   }
    48     85   
    49         -#define libcackey_wrap_genericmutexfunc(funcName, pthreadName) \
    50         -	static CK_RV funcName(CK_VOID_PTR pMutex) { \
    51         -		int (*func)(pthread_mutex_t *mutex); \
    52         -		pthread_mutex_t *pthread_mutex; \
    53         -		int pthread_retval; \
    54         -		libcackey_wrap_init(); \
    55         -		pthread_mutex = pMutex; \
    56         -		func = dlsym(libcackey_wrap_handle, pthreadName); \
    57         -		pthread_retval = func(pthread_mutex); \
    58         -		if (pthread_retval != 0) { \
    59         -			return(CKR_GENERAL_ERROR); \
    60         -		} \
    61         -		if (strcmp(pthreadName, "pthread_mutex_destroy") == 0) { \
    62         -			free(pthread_mutex); \
    63         -		} \
    64         -		return(CKR_OK); \
    65         -	}
           86  +int pthread_mutex_unlock(pthread_mutex_t *mutex) {
           87  +fprintf(stderr, "unlocking mutex %p\n", mutex); fflush(stderr);
           88  +	**mutex = LIBCACKEY_WRAP_MUTEX_UNLOCKED;
           89  +fprintf(stderr, "unlocked mutex %p\n", mutex); fflush(stderr);
    66     90   
    67         -libcackey_wrap_genericmutexfunc(libcackey_wrap_destroymutex, "pthread_mutex_destroy")
    68         -libcackey_wrap_genericmutexfunc(libcackey_wrap_lockmutex, "pthread_mutex_lock")
    69         -libcackey_wrap_genericmutexfunc(libcackey_wrap_unlockmutex, "pthread_mutex_unlock")
           91  +	return(CKR_OK);
           92  +}
    70     93   
    71     94   static void libcackey_wrap_init(void) {
    72     95   	Dl_info libinfo;
    73     96   	int dladdr_ret;
    74     97   	char *library, *libraryDir, *libraryDirLastSlash;
    75     98   
    76     99   	if (libcackey_wrap_handle) {
................................................................................
   104    127   		return;
   105    128   	}
   106    129   	*libraryDirLastSlash = '\0';
   107    130   
   108    131   	asprintf(&library, "%s/" CACKEY_LIBRARY_FILE, libraryDir);
   109    132   
   110    133   	libcackey_wrap_handle = dlmopen(LM_ID_NEWLM, library, RTLD_LOCAL | RTLD_NOW);
          134  +	libcackey_wrap_handle = dlmopen(LM_ID_NEWLM, , RTLD_LOCAL | RTLD_NOW);
   111    135   
   112    136   	if (!libcackey_wrap_handle) {
   113    137   		fprintf(stderr, "Unable to load \"%s\": %s\n", library, dlerror());
   114    138   
   115    139   		abort();
   116    140   
   117    141   		return;