Check-in [3fdad99785]
Overview
Comment:More work on improving wrapper
Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | better-wrapping
Files: files | file ages | folders
SHA1:3fdad997851d2257c862d949d78bb5795f1e468b
User & Date: rkeene on 2018-06-29 18:58:27
Other Links: manifest | tags
Context
2018-06-29
18:58
More work on improving wrapper Leaf check-in: 3fdad99785 user: rkeene tags: better-wrapping
2017-12-28
01:32
More work on improving the wrapper check-in: 3b7b2eddb4 user: rkeene tags: better-wrapping
Changes

Modified build/libcackey_wrap.c.in from [98f3aad3ae] to [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;

Modified build/make-libcackey_wrap from [f1fa196b14] to [d43bff44b6].

    37     37   	echo ''
    38     38   	echo "${line}"
    39     39   	echo $'\t'"CK_RV (*func)($(echo "${args}" | tr $'\n' ',' | sed 's@,*$@@;s@,@, @g'));"
    40     40   	case "${function}" in
    41     41   		C_Finalize)
    42     42   			echo $'\t''CK_RV retval;'
    43     43   			;;
    44         -		C_Initialize)
    45         -			echo $'\t''CK_C_INITIALIZE_ARGS CK_PTR args, localargs;'
    46         -			;;
    47     44   		C_GetFunctionList)
    48     45   			echo $'\t''CK_RV retval;'
    49     46   			echo $'\t''CK_FUNCTION_LIST_PTR pFunctionList;'
    50     47   			;;
    51     48   	esac
    52     49   
    53     50   	echo ''
    54     51   	echo $'\t''libcackey_wrap_init();'
    55     52   
    56         -	if [ "${function}" = 'C_Initialize' ]; then
    57         -		echo ''
    58         -		echo $'\t''if (pInitArgs) {'
    59         -		echo $'\t\t''args = pInitArgs;'
    60         -		echo $'\t\t''if ((args->flags & CKF_OS_LOCKING_OK) == CKF_OS_LOCKING_OK) {'
    61         -#echo 'fprintf(stderr, "replacing=%p\n", pInitArgs); fflush(stderr); abort();'
    62         -		echo $'\t\t\t''memcpy(&localargs, args, sizeof(*args));'
    63         -		echo $'\t\t\t''localargs.CreateMutex = libcackey_wrap_createmutex;'
    64         -		echo $'\t\t\t''localargs.DestroyMutex = libcackey_wrap_destroymutex;'
    65         -		echo $'\t\t\t''localargs.LockMutex = libcackey_wrap_lockmutex;'
    66         -		echo $'\t\t\t''localargs.UnlockMutex = libcackey_wrap_unlockmutex;'
    67         -		echo $'\t\t\t''localargs.flags &= ~CKF_OS_LOCKING_OK;'
    68         -		echo $'\t\t\t''pInitArgs = &localargs;'
    69         -		echo $'\t\t''}'
    70         -		echo $'\t''}'
    71         -	fi
    72     53   	echo ''
    73     54   	echo $'\t'"func = dlsym(libcackey_wrap_handle, \"${function}\");"
    74     55   	echo ''
    75     56   	if [ "${function}" = 'C_Finalize' ]; then
    76     57   		echo $'\t'"retval = func($argNamesList);"
    77     58   		echo ''
    78     59   		echo $'\t''libcackey_wrap_fini();'

Modified test.c from [b3c16cba8c] to [b9686fc1be].

   553    553   			if ((tokenInfo.flags & CKF_SO_PIN_TO_BE_CHANGED) == CKF_SO_PIN_TO_BE_CHANGED) {
   554    554   				printf("CKF_SO_PIN_TO_BE_CHANGED ");
   555    555   			}
   556    556   			printf("\n");
   557    557   		}
   558    558   	}
   559    559   
   560         -	chk_rv = C_OpenSession(slots[0], CKF_SERIAL_SESSION, NULL, NULL, &hSession);
          560  +	chk_rv = C_OpenSession(slots[1], CKF_SERIAL_SESSION, NULL, NULL, &hSession);
   561    561   	if (chk_rv == CKR_OK) {
   562         -		chk_rv = C_GetTokenInfo(slots[0], &tokenInfo);
          562  +		chk_rv = C_GetTokenInfo(slots[1], &tokenInfo);
   563    563   		if (chk_rv != CKR_OK) {
   564    564   			return(1);
   565    565   		}
   566    566   
   567    567   		if ((tokenInfo.flags & CKF_LOGIN_REQUIRED) == CKF_LOGIN_REQUIRED && (tokenInfo.flags & CKF_PROTECTED_AUTHENTICATION_PATH) == 0) {
   568    568   			fgets_ret = NULL;
   569    569