- Move all dynamic loading related code to src/common/scdl.c,

probably needs some more work.

As a side bonus, we now have a working CT-API support for MacOS X.


git-svn-id: https://www.opensc-project.org/svnp/opensc/trunk@1402 c6295689-39f2-0310-b995-f0e70906c6a9
This commit is contained in:
aet 2003-08-29 16:29:52 +00:00
parent 5835e752b5
commit bd54ea7a62
3 changed files with 308 additions and 361 deletions

View File

@ -23,81 +23,7 @@
#include <stdlib.h>
#include <string.h>
#include <assert.h>
#ifdef HAVE_DLFCN_H
#include <dlfcn.h>
int sc_module_open(struct sc_context *ctx, void **mod_handle, const char *filename)
{
const char *error;
void *handle;
assert(ctx != NULL);
if (!filename)
return SC_ERROR_UNKNOWN;
handle = dlopen(filename, RTLD_LAZY);
if ((error = dlerror()) != NULL) {
if (ctx->debug)
sc_debug(ctx, "sc_module_open: %s\n", error);
return SC_ERROR_UNKNOWN;
}
*mod_handle = handle;
return SC_SUCCESS;
}
int sc_module_close(struct sc_context *ctx, void *mod_handle)
{
const char *error;
assert(ctx != NULL);
if (!mod_handle)
return SC_ERROR_UNKNOWN;
dlclose(mod_handle);
if ((error = dlerror()) != NULL) {
if (ctx->debug)
sc_debug(ctx, "sc_module_close: %s\n", error);
return SC_ERROR_UNKNOWN;
}
return SC_SUCCESS;
}
int sc_module_get_address(struct sc_context *ctx, void *mod_handle, void **sym_address, const char *sym_name)
{
const char *error;
char name[256];
void *address;
assert(ctx != NULL);
if (!mod_handle || !sym_name)
return SC_ERROR_UNKNOWN;
/* Some platforms might need a leading underscore for the symbol */
name[0] = '_';
strncpy(&name[1], sym_name, sizeof(name) - 1);
address = dlsym(mod_handle, name);
/* Failed? Try again without the leading underscore */
if (address == NULL)
address = dlsym(mod_handle, sym_name);
if ((error = dlerror()) != NULL) {
if (ctx->debug)
sc_debug(ctx, "sc_module_get_address: %s\n", error);
return SC_ERROR_UNKNOWN;
}
*sym_address = address;
return SC_SUCCESS;
}
#elif defined(_WIN32)
#include <windows.h>
#include "../common/scdl.c"
int sc_module_open(struct sc_context *ctx, void **mod_handle, const char *filename)
{
@ -108,11 +34,11 @@ int sc_module_open(struct sc_context *ctx, void **mod_handle, const char *filena
if (!filename)
return SC_ERROR_UNKNOWN;
handle = LoadLibrary(filename);
handle = scdl_open(filename);
if (handle == NULL) {
if (ctx->debug)
/* TODO: GetLastError */
/* TODO: scdl_error */
sc_debug(ctx, "sc_module_open: unknown error");
return SC_ERROR_UNKNOWN;
}
@ -127,9 +53,7 @@ int sc_module_close(struct sc_context *ctx, void *mod_handle)
if (!mod_handle)
return SC_ERROR_UNKNOWN;
FreeLibrary(mod_handle);
/* TODO: GetLastError */
scdl_close(mod_handle);
return SC_SUCCESS;
}
@ -142,34 +66,14 @@ int sc_module_get_address(struct sc_context *ctx, void *mod_handle, void **sym_a
if (!mod_handle || !sym_name)
return SC_ERROR_UNKNOWN;
address = GetProcAddress(mod_handle, sym_name);
address = scdl_get_address(mod_handle, sym_name);
if (address == NULL) {
if (ctx->debug)
/* TODO: GetLastError */
/* TODO: scdl_error */
sc_debug(ctx, "sc_module_get_address: unknown error");
return SC_ERROR_UNKNOWN;
}
*sym_address = address;
return SC_SUCCESS;
}
#else
int sc_module_open(struct sc_context *ctx, void **mod_handle, const char *filename)
{
return SC_ERROR_UNKNOWN;
}
int sc_module_close(struct sc_context *ctx, void *mod_handle)
{
return SC_ERROR_UNKNOWN;
}
int sc_module_get_address(struct sc_context *ctx, void *mod_handle, void **sym_address, const char *sym_name)
{
return SC_ERROR_UNKNOWN;
}
#endif

View File

@ -12,16 +12,10 @@
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#ifdef __APPLE__
#include <Carbon/Carbon.h>
#endif
#include "../common/scdl.c"
#define MAGIC 0xd00bed00
#define DL_TYPE_DLFCN 0x01
#define DL_TYPE_WIN32 0x02
#define DL_TYPE_MAC 0x03
#if defined(_WIN32)
#define DEFAULT_MODULE_NAME "opensc-pkcs11";
#elif defined(HAVE_DLFCN_H) && defined(__APPLE__)
@ -34,17 +28,9 @@
struct sc_pkcs11_module {
unsigned int _magic;
void *_dl_handle;
unsigned int _type;
#if defined(__APPLE__)
CFBundleRef bundleRef;
#endif
scdl_context_t *handle;
};
static int sys_dlopen(sc_pkcs11_module_t *, const char *);
static int sys_dlclose(sc_pkcs11_module_t *);
static void * sys_dlsym(sc_pkcs11_module_t *, const char *);
/*
* Load a module - this will load the shared object, call
* C_Initialize, and get the list of function pointers
@ -58,12 +44,16 @@ C_LoadModule(const char *mspec, CK_FUNCTION_LIST_PTR_PTR funcs)
mod = (sc_pkcs11_module_t *) calloc(1, sizeof(*mod));
mod->_magic = MAGIC;
if (sys_dlopen(mod, mspec) < 0)
if (mspec == NULL)
mspec = DEFAULT_MODULE_NAME;
mod->handle = scdl_open(mspec);
if (mod->handle == NULL)
goto failed;
/* Get the list of function pointers */
c_get_function_list = (CK_RV (*)(CK_FUNCTION_LIST_PTR_PTR))
sys_dlsym(mod, "C_GetFunctionList");
scdl_get_address(mod->handle, "C_GetFunctionList");
if (!c_get_function_list)
goto failed;
rv = c_get_function_list(funcs);
@ -85,250 +75,10 @@ C_UnloadModule(sc_pkcs11_module_t *mod)
if (!mod || mod->_magic != MAGIC)
return CKR_ARGUMENTS_BAD;
if (sys_dlclose(mod) < 0)
if (scdl_close(mod->handle) < 0)
return CKR_FUNCTION_FAILED;
memset(mod, 0, sizeof(*mod));
free(mod);
return CKR_OK;
}
#if defined(HAVE_DLFCN_H)
#include <dlfcn.h>
/*
* Module loader for platforms that have dlopen
*
* This is intentionally primitive; we may want a more
* elaborate loader in libopensc one day
*/
int
dl_dlopen(struct sc_pkcs11_module *mod, const char *name)
{
const char **dir, *ldlist[64];
char pathbuf[4096], *ldenv;
unsigned int n = 0;
if ((ldenv = getenv("LD_LIBRARY_PATH"))
&& (ldenv = strdup(ldenv))) {
ldlist[n] = strtok(ldenv, ":");
while (ldlist[n] != NULL && ++n < 63)
ldlist[n] = strtok(NULL, ":");
}
ldlist[n] = NULL;
for (dir = ldlist; *dir; dir++) {
snprintf(pathbuf, sizeof(pathbuf), "%s/%s", *dir, name);
mod->_dl_handle = dlopen(pathbuf, RTLD_NOW);
if (mod->_dl_handle != NULL)
break;
}
if (mod->_dl_handle == NULL)
mod->_dl_handle = dlopen(name, RTLD_NOW);
if (ldenv)
free(ldenv);
mod->_type = DL_TYPE_DLFCN;
return (mod->_dl_handle? 0 : -1);
}
int
dl_dlclose(struct sc_pkcs11_module *mod)
{
if (mod->_dl_handle)
dlclose(mod->_dl_handle);
mod->_dl_handle = NULL;
return 0;
}
void *
dl_dlsym(sc_pkcs11_module_t *mod, const char *name)
{
char sym_name[256];
void *address;
if (!mod->_dl_handle)
return NULL;
/* Some platforms might need a leading underscore for the symbol */
snprintf(sym_name, sizeof(sym_name), "_%s", name);
address = dlsym(mod->_dl_handle, sym_name);
/* Failed? Try again without the leading underscore */
if (address == NULL)
address = dlsym(mod->_dl_handle, name);
return address;
}
#endif
#ifdef _WIN32
#include <windows.h>
/*
* Module loader for the Windows platform.
*/
int
win32_dlopen(struct sc_pkcs11_module *mod, const char *name)
{
mod->_dl_handle = LoadLibrary(name);
mod->_type = DL_TYPE_WIN32;
return (mod->_dl_handle? 0 : GetLastError());
}
int
win32_dlclose(struct sc_pkcs11_module *mod)
{
if (mod->_dl_handle) {
if (FreeLibrary(mod->_dl_handle)) {
mod->_dl_handle = NULL;
return 0;
}
else
return -1;
}
return 0;
}
void *
win32_dlsym(sc_pkcs11_module_t *mod, const char *name)
{
if (!mod->_dl_handle)
return NULL;
return GetProcAddress(mod->_dl_handle, name);
}
#endif
#if defined(__APPLE__)
#include <mach-o/dyld.h>
/*
* Module loader for MacOS X
*/
int
mac_dlopen(struct sc_pkcs11_module *mod, const char *name)
{
if (strstr(name, ".bundle")) {
CFStringRef text = CFStringCreateWithFormat(
NULL, NULL, CFSTR("%s"), name);
CFURLRef urlRef = CFURLCreateWithFileSystemPath(
kCFAllocatorDefault, text, kCFURLPOSIXPathStyle, 1);
mod->bundleRef = CFBundleCreate(kCFAllocatorDefault, urlRef);
CFRelease(urlRef);
CFRelease(text);
mod->_dl_handle = NULL;
} else {
mod->_dl_handle = (struct mach_header *) NSAddImage(name,
NSADDIMAGE_OPTION_WITH_SEARCHING);
mod->bundleRef = NULL;
}
mod->_type = DL_TYPE_MAC;
return (mod->_dl_handle == NULL && mod->bundleRef == NULL ? -1 : 0);
}
int
mac_dlclose(struct sc_pkcs11_module *mod)
{
if (mod->bundleRef != NULL) {
CFBundleUnloadExecutable(mod->bundleRef);
CFRelease(mod->bundleRef);
}
return 0;
}
void *
mac_dlsym(sc_pkcs11_module_t *mod, const char *name)
{
NSSymbol symbol = NULL;
if (mod->bundleRef != NULL) {
CFStringRef text = CFStringCreateWithFormat(
NULL, NULL, CFSTR("%s"), name);
symbol = CFBundleGetFunctionPointerForName(
mod->bundleRef, text);
CFRelease(text);
return symbol;
} else {
char sym_name[4096];
snprintf(sym_name, sizeof(sym_name), "_%s", name);
symbol = NSLookupSymbolInImage((const struct mach_header *)
mod->_dl_handle, sym_name,
NSLOOKUPSYMBOLINIMAGE_OPTION_BIND_NOW);
if (symbol == NULL)
return NULL;
return NSAddressOfSymbol(symbol);
}
}
#endif
int
sys_dlopen(struct sc_pkcs11_module *mod, const char *name)
{
if (name == NULL)
name = DEFAULT_MODULE_NAME;
#if defined(_WIN32)
return win32_dlopen(mod, name);
#elif defined(HAVE_DLFCN_H) && defined(__APPLE__)
if (strstr(name, ".bundle") || strstr(name, ".dylib")) {
return mac_dlopen(mod, name);
} else {
return dl_dlopen(mod, name);
}
#elif defined(__APPLE__)
return mac_dlopen(mod, name);
#elif defined(HAVE_DLFCN_H)
return dl_dlopen(mod, name);
#endif
return -1;
}
int
sys_dlclose(struct sc_pkcs11_module *mod)
{
#if defined(_WIN32)
return win32_dlclose(mod);
#elif defined(HAVE_DLFCN_H) && defined(__APPLE__)
switch(mod->_type) {
case DL_TYPE_MAC:
return mac_dlclose(mod);
break;
case DL_TYPE_DLFCN:
return dl_dlclose(mod);
break;
}
#elif defined(__APPLE__)
return mac_dlclose(mod);
#elif defined(HAVE_DLFCN_H)
return dl_dlclose(mod);
#endif
return 0;
}
void *
sys_dlsym(sc_pkcs11_module_t *mod, const char *name)
{
#if defined(_WIN32)
return win32_dlsym(mod, name);
#elif defined(HAVE_DLFCN_H) && defined(__APPLE__)
switch(mod->_type) {
case DL_TYPE_MAC:
return mac_dlsym(mod, name);
break;
case DL_TYPE_DLFCN:
return dl_dlsym(mod, name);
break;
}
#elif defined(__APPLE__)
return mac_dlsym(mod, name);
#elif defined(HAVE_DLFCN_H)
return dl_dlsym(mod, name);
#endif
return NULL;
}

293
src/scdl/scdl.c Normal file
View File

@ -0,0 +1,293 @@
/*
* Dynamic loading routines for various platforms, to
* be used internally in several places.
*
* No interface for portable error handling, maybe
* later.
*
* Copyright (C) 2003 Antti Tapaninen <aet@cc.hut.fi>
* Olaf Kirch <okir@lst.de>
* Stef Hoeben <stef.hoeben@zetes.com>
*/
#ifdef HAVE_CONFIG_H
#include <config.h>
#endif
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#ifdef __APPLE__
#include <Carbon/Carbon.h>
#endif
#define SCDL_MAGIC 0xbeefd00d
#define SCDL_TYPE_DLFCN 0x01
#define SCDL_TYPE_WIN32 0x02
#define SCDL_TYPE_MAC 0x03
struct scdl_context {
unsigned int magic;
void *handle;
unsigned int type;
#if defined(__APPLE__)
CFBundleRef bundleRef;
#endif
};
typedef struct scdl_context scdl_context_t;
#if defined(HAVE_DLFCN_H)
#include <dlfcn.h>
/*
* Module loader for platforms that have dlopen
*/
static int
dlfcn_open(scdl_context_t *mod, const char *name)
{
const char **dir, *ldlist[64];
char pathbuf[4096], *ldenv;
unsigned int n = 0;
if ((ldenv = getenv("LD_LIBRARY_PATH"))
&& (ldenv = strdup(ldenv))) {
ldlist[n] = strtok(ldenv, ":");
while (ldlist[n] != NULL && ++n < 63)
ldlist[n] = strtok(NULL, ":");
}
ldlist[n] = NULL;
for (dir = ldlist; *dir; dir++) {
snprintf(pathbuf, sizeof(pathbuf), "%s/%s", *dir, name);
mod->handle = dlopen(pathbuf, RTLD_NOW);
if (mod->handle != NULL)
break;
}
if (mod->handle == NULL)
mod->handle = dlopen(name, RTLD_NOW);
if (ldenv)
free(ldenv);
mod->type = SCDL_TYPE_DLFCN;
return (mod->handle? 0 : -1);
}
static int
dlfcn_close(scdl_context_t *mod)
{
if (mod->handle)
dlclose(mod->handle);
mod->handle = NULL;
return 0;
}
static void *
dlfcn_get_address(scdl_context_t *mod, const char *symbol)
{
char sym_name[256];
void *address;
if (!mod->handle)
return NULL;
/* Some platforms might need a leading underscore for the symbol */
snprintf(sym_name, sizeof(sym_name), "_%s", symbol);
address = dlsym(mod->handle, sym_name);
/* Failed? Try again without the leading underscore */
if (address == NULL)
address = dlsym(mod->handle, symbol);
return address;
}
#endif
#ifdef _WIN32
#include <windows.h>
/*
* Module loader for the Windows platform.
*/
static int
win32_open(scdl_context_t *mod, const char *name)
{
mod->handle = LoadLibrary(name);
mod->type = SCDL_TYPE_WIN32;
return (mod->handle? 0 : GetLastError());
}
static int
win32_close(scdl_context_t *mod)
{
if (mod->handle) {
if (FreeLibrary(mod->handle)) {
mod->handle = NULL;
return 0;
}
else
return -1;
}
return 0;
}
static void *
win32_get_address(scdl_context_t *mod, const char *symbol)
{
if (!mod->handle)
return NULL;
return GetProcAddress(mod->handle, symbol);
}
#endif
#if defined(__APPLE__)
#include <mach-o/dyld.h>
/*
* Module loader for MacOS X
*/
static int
mac_open(scdl_context_t *mod, const char *name)
{
if (strstr(name, ".bundle")) {
CFStringRef text = CFStringCreateWithFormat(
NULL, NULL, CFSTR("%s"), name);
CFURLRef urlRef = CFURLCreateWithFileSystemPath(
kCFAllocatorDefault, text, kCFURLPOSIXPathStyle, 1);
mod->bundleRef = CFBundleCreate(kCFAllocatorDefault, urlRef);
CFRelease(urlRef);
CFRelease(text);
mod->handle = NULL;
} else {
mod->handle = (struct mach_header *) NSAddImage(name,
NSADDIMAGE_OPTION_WITH_SEARCHING);
mod->bundleRef = NULL;
}
mod->type = SCDL_TYPE_MAC;
return (mod->handle == NULL && mod->bundleRef == NULL ? -1 : 0);
}
static int
mac_close(scdl_context_t *mod)
{
if (mod->bundleRef != NULL) {
CFBundleUnloadExecutable(mod->bundleRef);
CFRelease(mod->bundleRef);
}
return 0;
}
static void *
mac_get_address(scdl_context_t *mod, const char *symbol)
{
NSSymbol nssym = NULL;
if (mod->bundleRef != NULL) {
CFStringRef text = CFStringCreateWithFormat(
NULL, NULL, CFSTR("%s"), symbol);
nssym = CFBundleGetFunctionPointerForName(
mod->bundleRef, text);
CFRelease(text);
return nssym;
} else {
char sym_name[4096];
snprintf(sym_name, sizeof(sym_name), "_%s", symbol);
nssym = NSLookupSymbolInImage((const struct mach_header *)
mod->handle, sym_name,
NSLOOKUPSYMBOLINIMAGE_OPTION_BIND_NOW);
if (nssym == NULL)
return NULL;
return NSAddressOfSymbol(nssym);
}
}
#endif
static scdl_context_t *
scdl_open(const char *name)
{
scdl_context_t *mod;
int rv;
mod = (scdl_context_t *) calloc(1, sizeof(*mod));
if (mod == NULL)
return NULL;
mod->magic = SCDL_MAGIC;
#if defined(_WIN32)
rv = win32_open(mod, name);
#elif defined(HAVE_DLFCN_H) && defined(__APPLE__)
if (strstr(name, ".bundle") || strstr(name, ".dylib")) {
rv = mac_open(mod, name);
} else {
rv = dlfcn_open(mod, name);
}
#elif defined(__APPLE__)
rv = mac_open(mod, name);
#elif defined(HAVE_DLFCN_H)
rv = dlfcn_open(mod, name);
#else
rv = -1;
#endif
if (rv < 0) {
memset(mod, 0, sizeof(*mod));
free(mod);
return NULL;
}
return mod;
}
static int
scdl_close(scdl_context_t *mod)
{
int rv;
if (!mod || mod->magic != SCDL_MAGIC)
return -1;
#if defined(_WIN32)
rv = win32_close(mod);
#elif defined(HAVE_DLFCN_H) && defined(__APPLE__)
switch(mod->type) {
case SCDL_TYPE_MAC:
rv = mac_close(mod);
break;
case SCDL_TYPE_DLFCN:
rv = dlfcn_close(mod);
break;
}
#elif defined(__APPLE__)
rv = mac_close(mod);
#elif defined(HAVE_DLFCN_H)
rv = dlfcn_close(mod);
#endif
memset(mod, 0, sizeof(*mod));
free(mod);
return 0;
}
static void *
scdl_get_address(scdl_context_t *mod, const char *symbol)
{
if (!mod || mod->magic != SCDL_MAGIC)
return NULL;
#if defined(_WIN32)
return win32_get_address(mod, symbol);
#elif defined(HAVE_DLFCN_H) && defined(__APPLE__)
switch(mod->type) {
case SCDL_TYPE_MAC:
return mac_get_address(mod, symbol);
break;
case SCDL_TYPE_DLFCN:
return dlfcn_get_address(mod, symbol);
break;
}
#elif defined(__APPLE__)
return mac_get_address(mod, symbol);
#elif defined(HAVE_DLFCN_H)
return dlfcn_get_address(mod, symbol);
#endif
return NULL;
}