Check-in [e14e862239]
Overview
Comment:Started work on runtime loading of dependencies
Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | runtime-loading-of-deps
Files: files | file ages | folders
SHA1:e14e862239766461a52f4523c2ac998b8d8e1403
User & Date: rkeene on 2017-12-27 17:52:34
Other Links: manifest | tags
Context
2017-12-27
17:52
Started work on runtime loading of dependencies Leaf check-in: e14e862239 user: rkeene tags: runtime-loading-of-deps
2017-07-17
13:56
Updated to treat returning a zero-length signed message as an error check-in: 0c7c510048 user: rkeene tags: trunk
Changes

Modified aclocal/Makefile from [2810947b90] to [7b6874b298].

     1         -../aclocal.m4: acx_pthread.m4 dc_shobjs.m4 dc_pcsc.m4 dc_versionscript.m4
            1  +../aclocal.m4: acx_pthread.m4 dc_shobjs.m4 dc_pcsc.m4 dc_versionscript.m4 dc_dlfuncs.m4
     2      2   	cat $^ > "$@"

Added aclocal/dc_dlfuncs.m4 version [d34cc673cd].

            1  +AC_DEFUN([DC_DLFUNCS], [
            2  +	dnl Check for dl headers and functions
            3  +	AC_CHECK_HEADERS(dlfcn.h)
            4  +	if test -n "${ac_cv_func_dlsym}"; then
            5  +		AC_CHECK_FUNC(dlsym, [
            6  +			AC_DEFINE([HAVE_DLSYM], [1], [Have dlsym()])
            7  +		])
            8  +	else
            9  +		SAVE_LIBS="${LIBS}"
           10  +		for addlibs in '' '-ldl'; do
           11  +			LIBS="${SAVE_LIBS} ${addlibs}"
           12  +			unset ac_cv_func_dlsym
           13  +			AC_CHECK_FUNC(dlsym, [
           14  +				AC_DEFINE([HAVE_DLSYM], [1], [Have dlsym()])
           15  +				SAVE_LIBS="${LIBS}"
           16  +				break
           17  +			])
           18  +		done
           19  +		LIBS="${SAVE_LIBS}"
           20  +	fi
           21  +])

Modified cackey.c from [de5cf6edd0] to [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;

Modified configure.ac from [a419c60439] to [383d36814d].

    77     77   ])
    78     78   
    79     79   dnl Check for PC/SC headers and libraries
    80     80   DC_PCSC
    81     81   
    82     82   dnl Check for ZLIB libraries
    83     83   AC_CHECK_LIB(z, uncompress)
           84  +
           85  +dnl Check for dl headers and functions
           86  +DC_DLFUNCS
    84     87   
    85     88   dnl Verify that a basic program will compile
    86     89   AC_CACHE_CHECK([if basic PC/SC program works], cackey_cv_pcsc_works, [
    87     90   	AC_LINK_IFELSE(
    88     91   		AC_LANG_PROGRAM([[
    89     92   #ifdef HAVE_WINTYPES_H
    90     93   #  include <wintypes.h>